History

iio: imu: st_lsm6dsx: add i2c embedded controller support

i2c controller embedded in lsm6dx series can connect up to four slave devices using accelerometer sensor as trigger for i2c read/write operations. Introduce sensor hub support for lsm6dso sensor. Add register map for lis2mdl magnetometer sensor. In order to perform single read/write operations st_lsm6dsx driver relies on SLV0 channel (hw FIFO is not supported yet)

Lorenzo Bianconi committed 8 years ago

Commit c91c1c81 parent 17750445 files · +911 −43Browse files

Unified · Split

5 files
  1. drivers/iio/imu/st_lsm6dsx/Makefile +2 −1
  2. drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +112 −0
  3. drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +93 −42
  4. drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +702 −0
  5. include/linux/platform_data/st_sensors_pdata.h +2 −0
drivers/iio/imu/st_lsm6dsx/Makefile+2−1
@@ -1,4 +1,5 @@
1st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o1st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o \
2 st_lsm6dsx_shub.o
2 3
3obj-$(CONFIG_IIO_ST_LSM6DSX) += st_lsm6dsx.o4obj-$(CONFIG_IIO_ST_LSM6DSX) += st_lsm6dsx.o
4obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) += st_lsm6dsx_i2c.o5obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) += st_lsm6dsx_i2c.o
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h+112−0
@@ -43,6 +43,24 @@ enum st_lsm6dsx_hw_id { · 42 unmodified lines
43 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)43 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
44#define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))44#define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
45 45
46#define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
47{ \
48 .type = chan_type, \
49 .address = addr, \
50 .modified = 1, \
51 .channel2 = mod, \
52 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
53 BIT(IIO_CHAN_INFO_SCALE), \
54 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
55 .scan_index = scan_idx, \
56 .scan_type = { \
57 .sign = 's', \
58 .realbits = 16, \
59 .storagebits = 16, \
60 .endianness = IIO_LE, \
61 }, \
62}
63
46struct st_lsm6dsx_reg {64struct st_lsm6dsx_reg {
47 u8 addr;65 u8 addr;
48 u8 mask;66 u8 mask;
@@ -50,6 +68,28 @@ struct st_lsm6dsx_reg { · 1 unmodified line
50 68
51struct st_lsm6dsx_hw;69struct st_lsm6dsx_hw;
52 70
71struct st_lsm6dsx_odr {
72 u16 hz;
73 u8 val;
74};
75
76#define ST_LSM6DSX_ODR_LIST_SIZE 6
77struct st_lsm6dsx_odr_table_entry {
78 struct st_lsm6dsx_reg reg;
79 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
80};
81
82struct st_lsm6dsx_fs {
83 u32 gain;
84 u8 val;
85};
86
87#define ST_LSM6DSX_FS_LIST_SIZE 4
88struct st_lsm6dsx_fs_table_entry {
89 struct st_lsm6dsx_reg reg;
90 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
91};
92
53/**93/**
54 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings94 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
55 * @read_fifo: Read FIFO callback.95 * @read_fifo: Read FIFO callback.
@@ -84,6 +124,66 @@ struct st_lsm6dsx_hw_ts_settings { · 28 unmodified lines
84 struct st_lsm6dsx_reg decimator;124 struct st_lsm6dsx_reg decimator;
85};125};
86 126
127/**
128 * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
129 * @page_mux: register page mux info (addr + mask).
130 * @master_en: master config register info (addr + mask).
131 * @pullup_en: i2c controller pull-up register info (addr + mask).
132 * @aux_sens: aux sensor register info (addr + mask).
133 * @shub_out: sensor hub first output register info.
134 * @slv0_addr: slave0 address in secondary page.
135 * @dw_slv0_addr: slave0 write register address in secondary page.
136 */
137struct st_lsm6dsx_shub_settings {
138 struct st_lsm6dsx_reg page_mux;
139 struct st_lsm6dsx_reg master_en;
140 struct st_lsm6dsx_reg pullup_en;
141 struct st_lsm6dsx_reg aux_sens;
142 u8 shub_out;
143 u8 slv0_addr;
144 u8 dw_slv0_addr;
145};
146
147enum st_lsm6dsx_ext_sensor_id {
148 ST_LSM6DSX_ID_MAGN,
149};
150
151/**
152 * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
153 * @i2c_addr: I2c slave address list.
154 * @wai: Wai address info.
155 * @id: external sensor id.
156 * @odr: Output data rate of the sensor [Hz].
157 * @gain: Configured sensor sensitivity.
158 * @temp_comp: Temperature compensation register info (addr + mask).
159 * @pwr_table: Power on register info (addr + mask).
160 * @off_canc: Offset cancellation register info (addr + mask).
161 * @bdu: Block data update register info (addr + mask).
162 * @out: Output register info.
163 */
164struct st_lsm6dsx_ext_dev_settings {
165 u8 i2c_addr[2];
166 struct {
167 u8 addr;
168 u8 val;
169 } wai;
170 enum st_lsm6dsx_ext_sensor_id id;
171 struct st_lsm6dsx_odr_table_entry odr_table;
172 struct st_lsm6dsx_fs_table_entry fs_table;
173 struct st_lsm6dsx_reg temp_comp;
174 struct {
175 struct st_lsm6dsx_reg reg;
176 u8 off_val;
177 u8 on_val;
178 } pwr_table;
179 struct st_lsm6dsx_reg off_canc;
180 struct st_lsm6dsx_reg bdu;
181 struct {
182 u8 addr;
183 u8 len;
184 } out;
185};
186
87/**187/**
88 * struct st_lsm6dsx_settings - ST IMU sensor settings188 * struct st_lsm6dsx_settings - ST IMU sensor settings
89 * @wai: Sensor WhoAmI default value.189 * @wai: Sensor WhoAmI default value.
@@ -93,6 +193,7 @@ struct st_lsm6dsx_hw_ts_settings { · 3 unmodified lines
93 * @batch: List of FIFO batching register info (addr + mask).193 * @batch: List of FIFO batching register info (addr + mask).
94 * @fifo_ops: Sensor hw FIFO parameters.194 * @fifo_ops: Sensor hw FIFO parameters.
95 * @ts_settings: Hw timer related settings.195 * @ts_settings: Hw timer related settings.
196 * @shub_settings: i2c controller related settings.
96 */197 */
97struct st_lsm6dsx_settings {198struct st_lsm6dsx_settings {
98 u8 wai;199 u8 wai;
@@ -102,6 +203,7 @@ struct st_lsm6dsx_settings { · 3 unmodified lines
102 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];203 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
103 struct st_lsm6dsx_fifo_ops fifo_ops;204 struct st_lsm6dsx_fifo_ops fifo_ops;
104 struct st_lsm6dsx_hw_ts_settings ts_settings;205 struct st_lsm6dsx_hw_ts_settings ts_settings;
206 struct st_lsm6dsx_shub_settings shub_settings;
105};207};
106 208
107enum st_lsm6dsx_sensor_id {209enum st_lsm6dsx_sensor_id {
@@ -129,6 +231,7 @@ enum st_lsm6dsx_fifo_mode { · 21 unmodified lines
129 * @sip: Number of samples in a given pattern.231 * @sip: Number of samples in a given pattern.
130 * @decimator: FIFO decimation factor.232 * @decimator: FIFO decimation factor.
131 * @ts_ref: Sensor timestamp reference for hw one.233 * @ts_ref: Sensor timestamp reference for hw one.
234 * @ext_info: Sensor settings if it is connected to i2c controller
132 */235 */
133struct st_lsm6dsx_sensor {236struct st_lsm6dsx_sensor {
134 char name[32];237 char name[32];
@@ -142,6 +245,11 @@ struct st_lsm6dsx_sensor { · 7 unmodified lines
142 u8 sip;245 u8 sip;
143 u8 decimator;246 u8 decimator;
144 s64 ts_ref;247 s64 ts_ref;
248
249 struct {
250 const struct st_lsm6dsx_ext_dev_settings *settings;
251 u8 addr;
252 } ext_info;
145};253};
146 254
147/**255/**
@@ -181,6 +289,7 @@ struct st_lsm6dsx_hw { · 33 unmodified lines
181 const struct st_lsm6dsx_settings *settings;289 const struct st_lsm6dsx_settings *settings;
182};290};
183 291
292static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
184extern const struct dev_pm_ops st_lsm6dsx_pm_ops;293extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
185 294
186int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,295int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
@@ -197,6 +306,9 @@ int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw, · 10 unmodified lines
197int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);306int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
198int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);307int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
199int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);308int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
309int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
310int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
311int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
200 312
201static inline int313static inline int
202st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,314st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c+93−42
@@ -88,17 +88,6 @@ · 87 unmodified lines
88#define ST_LSM6DSX_GYRO_FS_1000_GAIN IIO_DEGREE_TO_RAD(35000)88#define ST_LSM6DSX_GYRO_FS_1000_GAIN IIO_DEGREE_TO_RAD(35000)
89#define ST_LSM6DSX_GYRO_FS_2000_GAIN IIO_DEGREE_TO_RAD(70000)89#define ST_LSM6DSX_GYRO_FS_2000_GAIN IIO_DEGREE_TO_RAD(70000)
90 90
91struct st_lsm6dsx_odr {
92 u16 hz;
93 u8 val;
94};
95
96#define ST_LSM6DSX_ODR_LIST_SIZE 6
97struct st_lsm6dsx_odr_table_entry {
98 struct st_lsm6dsx_reg reg;
99 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
100};
101
102static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {91static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
103 [ST_LSM6DSX_ID_ACC] = {92 [ST_LSM6DSX_ID_ACC] = {
104 .reg = {93 .reg = {
@@ -126,17 +115,6 @@ static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = { · 21 unmodified lines
126 }115 }
127};116};
128 117
129struct st_lsm6dsx_fs {
130 u32 gain;
131 u8 val;
132};
133
134#define ST_LSM6DSX_FS_LIST_SIZE 4
135struct st_lsm6dsx_fs_table_entry {
136 struct st_lsm6dsx_reg reg;
137 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
138};
139
140static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {118static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {
141 [ST_LSM6DSX_ID_ACC] = {119 [ST_LSM6DSX_ID_ACC] = {
142 .reg = {120 .reg = {
@@ -342,27 +320,30 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = { · 199 unmodified lines
342 .mask = GENMASK(7, 6),320 .mask = GENMASK(7, 6),
343 },321 },
344 },322 },
323 .shub_settings = {
324 .page_mux = {
325 .addr = 0x01,
326 .mask = BIT(6),
327 },
328 .master_en = {
329 .addr = 0x14,
330 .mask = BIT(2),
331 },
332 .pullup_en = {
333 .addr = 0x14,
334 .mask = BIT(3),
335 },
336 .aux_sens = {
337 .addr = 0x14,
338 .mask = GENMASK(1, 0),
339 },
340 .shub_out = 0x02,
341 .slv0_addr = 0x15,
342 .dw_slv0_addr = 0x21,
343 }
345 },344 },
346};345};
347 346
348#define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
349{ \
350 .type = chan_type, \
351 .address = addr, \
352 .modified = 1, \
353 .channel2 = mod, \
354 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
355 BIT(IIO_CHAN_INFO_SCALE), \
356 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
357 .scan_index = scan_idx, \
358 .scan_type = { \
359 .sign = 's', \
360 .realbits = 16, \
361 .storagebits = 16, \
362 .endianness = IIO_LE, \
363 }, \
364}
365
366static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {347static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
367 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,348 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,
368 IIO_MOD_X, 0),349 IIO_MOD_X, 0),
@@ -383,6 +364,21 @@ static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = { · 14 unmodified lines
383 IIO_CHAN_SOFT_TIMESTAMP(3),364 IIO_CHAN_SOFT_TIMESTAMP(3),
384};365};
385 366
367int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
368{
369 const struct st_lsm6dsx_shub_settings *hub_settings;
370 unsigned int data;
371 int err;
372
373 hub_settings = &hw->settings->shub_settings;
374 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
375 err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
376 hub_settings->page_mux.mask, data);
377 usleep_range(100, 150);
378
379 return err;
380}
381
386static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)382static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)
387{383{
388 int err, i, j, data;384 int err, i, j, data;
@@ -736,8 +732,6 @@ static const struct iio_info st_lsm6dsx_gyro_info = { · 347 unmodified lines
736 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,732 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
737};733};
738 734
739static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
740
741static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)735static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
742{736{
743 struct device_node *np = hw->dev->of_node;737 struct device_node *np = hw->dev->of_node;
@@ -776,6 +770,51 @@ static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg) · 32 unmodified lines
776 return err;770 return err;
777}771}
778 772
773static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
774{
775 const struct st_lsm6dsx_shub_settings *hub_settings;
776 struct device_node *np = hw->dev->of_node;
777 struct st_sensors_platform_data *pdata;
778 unsigned int data;
779 int err = 0;
780
781 hub_settings = &hw->settings->shub_settings;
782
783 pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
784 if ((np && of_property_read_bool(np, "st,pullups")) ||
785 (pdata && pdata->pullups)) {
786 err = st_lsm6dsx_set_page(hw, true);
787 if (err < 0)
788 return err;
789
790 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
791 err = regmap_update_bits(hw->regmap,
792 hub_settings->pullup_en.addr,
793 hub_settings->pullup_en.mask, data);
794
795 st_lsm6dsx_set_page(hw, false);
796
797 if (err < 0)
798 return err;
799 }
800
801 if (hub_settings->aux_sens.addr) {
802 /* configure aux sensors */
803 err = st_lsm6dsx_set_page(hw, true);
804 if (err < 0)
805 return err;
806
807 data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
808 err = regmap_update_bits(hw->regmap,
809 hub_settings->aux_sens.addr,
810 hub_settings->aux_sens.mask, data);
811
812 st_lsm6dsx_set_page(hw, false);
813 }
814
815 return err;
816}
817
779static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)818static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
780{819{
781 const struct st_lsm6dsx_hw_ts_settings *ts_settings;820 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
@@ -856,6 +895,10 @@ static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw) · 74 unmodified lines
856 if (err < 0)895 if (err < 0)
857 return err;896 return err;
858 897
898 err = st_lsm6dsx_init_shub(hw);
899 if (err < 0)
900 return err;
901
859 return st_lsm6dsx_init_hw_timer(hw);902 return st_lsm6dsx_init_hw_timer(hw);
860}903}
861 904
@@ -909,6 +952,7 @@ static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw, · 47 unmodified lines
909int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,952int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
910 struct regmap *regmap)953 struct regmap *regmap)
911{954{
955 const struct st_lsm6dsx_shub_settings *hub_settings;
912 struct st_lsm6dsx_hw *hw;956 struct st_lsm6dsx_hw *hw;
913 int i, err;957 int i, err;
914 958
@@ -944,6 +988,13 @@ int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name, · 29 unmodified lines
944 if (err < 0)988 if (err < 0)
945 return err;989 return err;
946 990
991 hub_settings = &hw->settings->shub_settings;
992 if (hub_settings->master_en.addr) {
993 err = st_lsm6dsx_shub_probe(hw, name);
994 if (err < 0)
995 return err;
996 }
997
947 if (hw->irq > 0) {998 if (hw->irq > 0) {
948 err = st_lsm6dsx_fifo_setup(hw);999 err = st_lsm6dsx_fifo_setup(hw);
949 if (err < 0)1000 if (err < 0)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c+702−0
@@ -0,0 +1,702 @@
1/*
2 * STMicroelectronics st_lsm6dsx i2c controller driver
3 *
4 * i2c controller embedded in lsm6dx series can connect up to four
5 * slave devices using accelerometer sensor as trigger for i2c
6 * read/write operations. Current implementation relies on SLV0 channel
7 * for slave configuration and SLV{1,2,3} to read data and push them into
8 * the hw FIFO
9 *
10 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
11 *
12 * Permission to use, copy, modify, and/or distribute this software for any
13 * purpose with or without fee is hereby granted, provided that the above
14 * copyright notice and this permission notice appear in all copies.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
17 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
19 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 *
24 */
25#include <linux/module.h>
26#include <linux/regmap.h>
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/bitfield.h>
30
31#include "st_lsm6dsx.h"
32
33#define ST_LSM6DSX_MAX_SLV_NUM 3
34#define ST_LSM6DSX_SLV_ADDR(n, base) ((base) + (n) * 3)
35#define ST_LSM6DSX_SLV_SUB_ADDR(n, base) ((base) + 1 + (n) * 3)
36#define ST_LSM6DSX_SLV_CONFIG(n, base) ((base) + 2 + (n) * 3)
37
38#define ST_LS6DSX_READ_OP_MASK GENMASK(2, 0)
39
40static const struct st_lsm6dsx_ext_dev_settings st_lsm6dsx_ext_dev_table[] = {
41 /* LIS2MDL */
42 {
43 .i2c_addr = { 0x1e },
44 .wai = {
45 .addr = 0x4f,
46 .val = 0x40,
47 },
48 .id = ST_LSM6DSX_ID_MAGN,
49 .odr_table = {
50 .reg = {
51 .addr = 0x60,
52 .mask = GENMASK(3, 2),
53 },
54 .odr_avl[0] = { 10, 0x0 },
55 .odr_avl[1] = { 20, 0x1 },
56 .odr_avl[2] = { 50, 0x2 },
57 .odr_avl[3] = { 100, 0x3 },
58 },
59 .fs_table = {
60 .fs_avl[0] = {
61 .gain = 1500,
62 .val = 0x0,
63 }, /* 1500 uG/LSB */
64 },
65 .temp_comp = {
66 .addr = 0x60,
67 .mask = BIT(7),
68 },
69 .pwr_table = {
70 .reg = {
71 .addr = 0x60,
72 .mask = GENMASK(1, 0),
73 },
74 .off_val = 0x2,
75 .on_val = 0x0,
76 },
77 .off_canc = {
78 .addr = 0x61,
79 .mask = BIT(1),
80 },
81 .bdu = {
82 .addr = 0x62,
83 .mask = BIT(4),
84 },
85 .out = {
86 .addr = 0x68,
87 .len = 6,
88 },
89 },
90};
91
92static void st_lsm6dsx_shub_wait_complete(struct st_lsm6dsx_hw *hw)
93{
94 struct st_lsm6dsx_sensor *sensor;
95
96 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
97 msleep((2000U / sensor->odr) + 1);
98}
99
100/**
101 * st_lsm6dsx_shub_read_reg - read i2c controller register
102 *
103 * Read st_lsm6dsx i2c controller register
104 */
105static int st_lsm6dsx_shub_read_reg(struct st_lsm6dsx_hw *hw, u8 addr,
106 u8 *data, int len)
107{
108 const struct st_lsm6dsx_shub_settings *hub_settings;
109 int err;
110
111 mutex_lock(&hw->page_lock);
112
113 hub_settings = &hw->settings->shub_settings;
114 err = st_lsm6dsx_set_page(hw, true);
115 if (err < 0)
116 goto out;
117
118 err = regmap_bulk_read(hw->regmap, addr, data, len);
119
120 st_lsm6dsx_set_page(hw, false);
121out:
122 mutex_unlock(&hw->page_lock);
123
124 return err;
125}
126
127/**
128 * st_lsm6dsx_shub_write_reg - write i2c controller register
129 *
130 * Write st_lsm6dsx i2c controller register
131 */
132static int st_lsm6dsx_shub_write_reg(struct st_lsm6dsx_hw *hw, u8 addr,
133 u8 *data, int len)
134{
135 int err;
136
137 mutex_lock(&hw->page_lock);
138 err = st_lsm6dsx_set_page(hw, true);
139 if (err < 0)
140 goto out;
141
142 err = regmap_bulk_write(hw->regmap, addr, data, len);
143
144 st_lsm6dsx_set_page(hw, false);
145out:
146 mutex_unlock(&hw->page_lock);
147
148 return err;
149}
150
151static int st_lsm6dsx_shub_master_enable(struct st_lsm6dsx_sensor *sensor,
152 bool enable)
153{
154 const struct st_lsm6dsx_shub_settings *hub_settings;
155 struct st_lsm6dsx_hw *hw = sensor->hw;
156 unsigned int data;
157 int err;
158
159 /* enable acc sensor as trigger */
160 err = st_lsm6dsx_sensor_set_enable(sensor, enable);
161 if (err < 0)
162 return err;
163
164 mutex_lock(&hw->page_lock);
165
166 hub_settings = &hw->settings->shub_settings;
167 err = st_lsm6dsx_set_page(hw, true);
168 if (err < 0)
169 goto out;
170
171 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->master_en.mask);
172 err = regmap_update_bits(hw->regmap, hub_settings->master_en.addr,
173 hub_settings->master_en.mask, data);
174
175 st_lsm6dsx_set_page(hw, false);
176out:
177 mutex_unlock(&hw->page_lock);
178
179 return err;
180}
181
182/**
183 * st_lsm6dsx_shub_read - read data from slave device register
184 *
185 * Read data from slave device register. SLV0 is used for
186 * one-shot read operation
187 */
188static int
189st_lsm6dsx_shub_read(struct st_lsm6dsx_sensor *sensor, u8 addr,
190 u8 *data, int len)
191{
192 const struct st_lsm6dsx_shub_settings *hub_settings;
193 struct st_lsm6dsx_hw *hw = sensor->hw;
194 u8 config[3], slv_addr;
195 int err;
196
197 hub_settings = &hw->settings->shub_settings;
198 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr);
199
200 config[0] = (sensor->ext_info.addr << 1) | 1;
201 config[1] = addr;
202 config[2] = len & ST_LS6DSX_READ_OP_MASK;
203
204 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config,
205 sizeof(config));
206 if (err < 0)
207 return err;
208
209 err = st_lsm6dsx_shub_master_enable(sensor, true);
210 if (err < 0)
211 return err;
212
213 st_lsm6dsx_shub_wait_complete(hw);
214
215 err = st_lsm6dsx_shub_read_reg(hw, hub_settings->shub_out, data,
216 len & ST_LS6DSX_READ_OP_MASK);
217
218 st_lsm6dsx_shub_master_enable(sensor, false);
219
220 memset(config, 0, sizeof(config));
221 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config,
222 sizeof(config));
223}
224
225/**
226 * st_lsm6dsx_shub_write - write data to slave device register
227 *
228 * Write data from slave device register. SLV0 is used for
229 * one-shot write operation
230 */
231static int
232st_lsm6dsx_shub_write(struct st_lsm6dsx_sensor *sensor, u8 addr,
233 u8 *data, int len)
234{
235 const struct st_lsm6dsx_shub_settings *hub_settings;
236 struct st_lsm6dsx_hw *hw = sensor->hw;
237 u8 config[2], slv_addr;
238 int err, i;
239
240 hub_settings = &hw->settings->shub_settings;
241 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr);
242 config[0] = sensor->ext_info.addr << 1;
243 for (i = 0 ; i < len; i++) {
244 config[1] = addr + i;
245
246 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config,
247 sizeof(config));
248 if (err < 0)
249 return err;
250
251 err = st_lsm6dsx_shub_write_reg(hw, hub_settings->dw_slv0_addr,
252 &data[i], 1);
253 if (err < 0)
254 return err;
255
256 err = st_lsm6dsx_shub_master_enable(sensor, true);
257 if (err < 0)
258 return err;
259
260 st_lsm6dsx_shub_wait_complete(hw);
261
262 st_lsm6dsx_shub_master_enable(sensor, false);
263 }
264
265 memset(config, 0, sizeof(config));
266 return st_lsm6dsx_shub_write_reg(hw, slv_addr, config, sizeof(config));
267}
268
269static int
270st_lsm6dsx_shub_write_with_mask(struct st_lsm6dsx_sensor *sensor,
271 u8 addr, u8 mask, u8 val)
272{
273 int err;
274 u8 data;
275
276 err = st_lsm6dsx_shub_read(sensor, addr, &data, sizeof(data));
277 if (err < 0)
278 return err;
279
280 data = ((data & ~mask) | (val << __ffs(mask) & mask));
281
282 return st_lsm6dsx_shub_write(sensor, addr, &data, sizeof(data));
283}
284
285static int
286st_lsm6dsx_shub_get_odr_val(struct st_lsm6dsx_sensor *sensor,
287 u16 odr, u16 *val)
288{
289 const struct st_lsm6dsx_ext_dev_settings *settings;
290 int i;
291
292 settings = sensor->ext_info.settings;
293 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
294 if (settings->odr_table.odr_avl[i].hz == odr)
295 break;
296
297 if (i == ST_LSM6DSX_ODR_LIST_SIZE)
298 return -EINVAL;
299
300 *val = settings->odr_table.odr_avl[i].val;
301 return 0;
302}
303
304static int
305st_lsm6dsx_shub_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr)
306{
307 const struct st_lsm6dsx_ext_dev_settings *settings;
308 u16 val;
309 int err;
310
311 err = st_lsm6dsx_shub_get_odr_val(sensor, odr, &val);
312 if (err < 0)
313 return err;
314
315 settings = sensor->ext_info.settings;
316 return st_lsm6dsx_shub_write_with_mask(sensor,
317 settings->odr_table.reg.addr,
318 settings->odr_table.reg.mask,
319 val);
320}
321
322int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)
323{
324 const struct st_lsm6dsx_ext_dev_settings *settings;
325 int err;
326
327 settings = sensor->ext_info.settings;
328 if (enable) {
329 err = st_lsm6dsx_shub_set_odr(sensor, sensor->odr);
330 if (err < 0)
331 return err;
332 } else {
333 err = st_lsm6dsx_shub_write_with_mask(sensor,
334 settings->odr_table.reg.addr,
335 settings->odr_table.reg.mask, 0);
336 if (err < 0)
337 return err;
338 }
339
340 if (settings->pwr_table.reg.addr) {
341 u8 val;
342
343 val = enable ? settings->pwr_table.on_val
344 : settings->pwr_table.off_val;
345 err = st_lsm6dsx_shub_write_with_mask(sensor,
346 settings->pwr_table.reg.addr,
347 settings->pwr_table.reg.mask, val);
348 if (err < 0)
349 return err;
350 }
351
352 return st_lsm6dsx_shub_master_enable(sensor, enable);
353}
354
355static int
356st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor *sensor,
357 struct iio_chan_spec const *ch,
358 int *val)
359{
360 int err, delay, len = ch->scan_type.realbits >> 3;
361 __le16 data;
362
363 err = st_lsm6dsx_shub_set_enable(sensor, true);
364 if (err < 0)
365 return err;
366
367 delay = 1000000 / sensor->odr;
368 usleep_range(delay, 2 * delay);
369
370 err = st_lsm6dsx_shub_read(sensor, ch->address, (u8 *)&data, len);
371 if (err < 0)
372 return err;
373
374 st_lsm6dsx_shub_set_enable(sensor, false);
375
376 switch (len) {
377 case 2:
378 *val = (s16)le16_to_cpu(data);
379 break;
380 default:
381 return -EINVAL;
382 }
383
384 return IIO_VAL_INT;
385}
386
387static int
388st_lsm6dsx_shub_read_raw(struct iio_dev *iio_dev,
389 struct iio_chan_spec const *ch,
390 int *val, int *val2, long mask)
391{
392 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
393 int ret;
394
395 switch (mask) {
396 case IIO_CHAN_INFO_RAW:
397 ret = iio_device_claim_direct_mode(iio_dev);
398 if (ret)
399 break;
400
401 ret = st_lsm6dsx_shub_read_oneshot(sensor, ch, val);
402 iio_device_release_direct_mode(iio_dev);
403 break;
404 case IIO_CHAN_INFO_SAMP_FREQ:
405 *val = sensor->odr;
406 ret = IIO_VAL_INT;
407 break;
408 case IIO_CHAN_INFO_SCALE:
409 *val = 0;
410 *val2 = sensor->gain;
411 ret = IIO_VAL_INT_PLUS_MICRO;
412 break;
413 default:
414 ret = -EINVAL;
415 break;
416 }
417
418 return ret;
419}
420
421static int
422st_lsm6dsx_shub_write_raw(struct iio_dev *iio_dev,
423 struct iio_chan_spec const *chan,
424 int val, int val2, long mask)
425{
426 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
427 int err;
428
429 err = iio_device_claim_direct_mode(iio_dev);
430 if (err)
431 return err;
432
433 switch (mask) {
434 case IIO_CHAN_INFO_SAMP_FREQ: {
435 u16 data;
436
437 err = st_lsm6dsx_shub_get_odr_val(sensor, val, &data);
438 if (!err)
439 sensor->odr = val;
440 break;
441 }
442 default:
443 err = -EINVAL;
444 break;
445 }
446
447 iio_device_release_direct_mode(iio_dev);
448
449 return err;
450}
451
452static ssize_t
453st_lsm6dsx_shub_sampling_freq_avail(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
456{
457 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
458 const struct st_lsm6dsx_ext_dev_settings *settings;
459 int i, len = 0;
460
461 settings = sensor->ext_info.settings;
462 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) {
463 u16 val = settings->odr_table.odr_avl[i].hz;
464
465 if (val > 0)
466 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
467 val);
468 }
469 buf[len - 1] = '\n';
470
471 return len;
472}
473
474static ssize_t st_lsm6dsx_shub_scale_avail(struct device *dev,
475 struct device_attribute *attr,
476 char *buf)
477{
478 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
479 const struct st_lsm6dsx_ext_dev_settings *settings;
480 int i, len = 0;
481
482 settings = sensor->ext_info.settings;
483 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++) {
484 u16 val = settings->fs_table.fs_avl[i].gain;
485
486 if (val > 0)
487 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
488 val);
489 }
490 buf[len - 1] = '\n';
491
492 return len;
493}
494
495static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_shub_sampling_freq_avail);
496static IIO_DEVICE_ATTR(in_scale_available, 0444,
497 st_lsm6dsx_shub_scale_avail, NULL, 0);
498static struct attribute *st_lsm6dsx_ext_attributes[] = {
499 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
500 &iio_dev_attr_in_scale_available.dev_attr.attr,
501 NULL,
502};
503
504static const struct attribute_group st_lsm6dsx_ext_attribute_group = {
505 .attrs = st_lsm6dsx_ext_attributes,
506};
507
508static const struct iio_info st_lsm6dsx_ext_info = {
509 .attrs = &st_lsm6dsx_ext_attribute_group,
510 .read_raw = st_lsm6dsx_shub_read_raw,
511 .write_raw = st_lsm6dsx_shub_write_raw,
512 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
513};
514
515static struct iio_dev *
516st_lsm6dsx_shub_alloc_iiodev(struct st_lsm6dsx_hw *hw,
517 enum st_lsm6dsx_sensor_id id,
518 const struct st_lsm6dsx_ext_dev_settings *info,
519 u8 i2c_addr, const char *name)
520{
521 struct iio_chan_spec *ext_channels;
522 struct st_lsm6dsx_sensor *sensor;
523 struct iio_dev *iio_dev;
524
525 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
526 if (!iio_dev)
527 return NULL;
528
529 iio_dev->modes = INDIO_DIRECT_MODE;
530 iio_dev->dev.parent = hw->dev;
531 iio_dev->info = &st_lsm6dsx_ext_info;
532
533 sensor = iio_priv(iio_dev);
534 sensor->id = id;
535 sensor->hw = hw;
536 sensor->odr = info->odr_table.odr_avl[0].hz;
537 sensor->gain = info->fs_table.fs_avl[0].gain;
538 sensor->ext_info.settings = info;
539 sensor->ext_info.addr = i2c_addr;
540 sensor->watermark = 1;
541
542 switch (info->id) {
543 case ST_LSM6DSX_ID_MAGN: {
544 const struct iio_chan_spec magn_channels[] = {
545 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr,
546 IIO_MOD_X, 0),
547 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 2,
548 IIO_MOD_Y, 1),
549 ST_LSM6DSX_CHANNEL(IIO_MAGN, info->out.addr + 4,
550 IIO_MOD_Z, 2),
551 IIO_CHAN_SOFT_TIMESTAMP(3),
552 };
553
554 ext_channels = devm_kzalloc(hw->dev, sizeof(magn_channels),
555 GFP_KERNEL);
556 if (!ext_channels)
557 return NULL;
558
559 memcpy(ext_channels, magn_channels, sizeof(magn_channels));
560 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
561 iio_dev->channels = ext_channels;
562 iio_dev->num_channels = ARRAY_SIZE(magn_channels);
563
564 scnprintf(sensor->name, sizeof(sensor->name), "%s_magn",
565 name);
566 break;
567 }
568 default:
569 return NULL;
570 }
571 iio_dev->name = sensor->name;
572
573 return iio_dev;
574}
575
576static int st_lsm6dsx_shub_init_device(struct st_lsm6dsx_sensor *sensor)
577{
578 const struct st_lsm6dsx_ext_dev_settings *settings;
579 int err;
580
581 settings = sensor->ext_info.settings;
582 if (settings->bdu.addr) {
583 err = st_lsm6dsx_shub_write_with_mask(sensor,
584 settings->bdu.addr,
585 settings->bdu.mask, 1);
586 if (err < 0)
587 return err;
588 }
589
590 if (settings->temp_comp.addr) {
591 err = st_lsm6dsx_shub_write_with_mask(sensor,
592 settings->temp_comp.addr,
593 settings->temp_comp.mask, 1);
594 if (err < 0)
595 return err;
596 }
597
598 if (settings->off_canc.addr) {
599 err = st_lsm6dsx_shub_write_with_mask(sensor,
600 settings->off_canc.addr,
601 settings->off_canc.mask, 1);
602 if (err < 0)
603 return err;
604 }
605
606 return 0;
607}
608
609static int
610st_lsm6dsx_shub_check_wai(struct st_lsm6dsx_hw *hw, u8 *i2c_addr,
611 const struct st_lsm6dsx_ext_dev_settings *settings)
612{
613 const struct st_lsm6dsx_shub_settings *hub_settings;
614 struct st_lsm6dsx_sensor *sensor;
615 u8 config[3], data, slv_addr;
616 bool found = false;
617 int i, err;
618
619 hub_settings = &hw->settings->shub_settings;
620 slv_addr = ST_LSM6DSX_SLV_ADDR(0, hub_settings->slv0_addr);
621 sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
622
623 for (i = 0; i < ARRAY_SIZE(settings->i2c_addr); i++) {
624 if (!settings->i2c_addr[i])
625 continue;
626
627 /* read wai slave register */
628 config[0] = (settings->i2c_addr[i] << 1) | 0x1;
629 config[1] = settings->wai.addr;
630 config[2] = 0x1;
631
632 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config,
633 sizeof(config));
634 if (err < 0)
635 return err;
636
637 err = st_lsm6dsx_shub_master_enable(sensor, true);
638 if (err < 0)
639 return err;
640
641 st_lsm6dsx_shub_wait_complete(hw);
642
643 err = st_lsm6dsx_shub_read_reg(hw,
644 hub_settings->shub_out,
645 &data, sizeof(data));
646
647 st_lsm6dsx_shub_master_enable(sensor, false);
648
649 if (err < 0)
650 return err;
651
652 if (data != settings->wai.val)
653 continue;
654
655 *i2c_addr = settings->i2c_addr[i];
656 found = true;
657 break;
658 }
659
660 /* reset SLV0 channel */
661 memset(config, 0, sizeof(config));
662 err = st_lsm6dsx_shub_write_reg(hw, slv_addr, config,
663 sizeof(config));
664 if (err < 0)
665 return err;
666
667 return found ? 0 : -ENODEV;
668}
669
670int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name)
671{
672 enum st_lsm6dsx_sensor_id id = ST_LSM6DSX_ID_EXT0;
673 struct st_lsm6dsx_sensor *sensor;
674 int err, i, num_ext_dev = 0;
675 u8 i2c_addr = 0;
676
677 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_ext_dev_table); i++) {
678 err = st_lsm6dsx_shub_check_wai(hw, &i2c_addr,
679 &st_lsm6dsx_ext_dev_table[i]);
680 if (err == -ENODEV)
681 continue;
682 else if (err < 0)
683 return err;
684
685 hw->iio_devs[id] = st_lsm6dsx_shub_alloc_iiodev(hw, id,
686 &st_lsm6dsx_ext_dev_table[i],
687 i2c_addr, name);
688 if (!hw->iio_devs[id])
689 return -ENOMEM;
690
691 sensor = iio_priv(hw->iio_devs[id]);
692 err = st_lsm6dsx_shub_init_device(sensor);
693 if (err < 0)
694 return err;
695
696 if (++num_ext_dev >= ST_LSM6DSX_MAX_SLV_NUM)
697 break;
698 id++;
699 }
700
701 return 0;
702}
include/linux/platform_data/st_sensors_pdata.h+2−0
@@ -18,11 +18,13 @@ · 17 unmodified lines
18 * Accelerometer DRDY on LSM330 available only on pin 1 (see datasheet).18 * Accelerometer DRDY on LSM330 available only on pin 1 (see datasheet).
19 * @open_drain: set the interrupt line to be open drain if possible.19 * @open_drain: set the interrupt line to be open drain if possible.
20 * @spi_3wire: enable spi-3wire mode.20 * @spi_3wire: enable spi-3wire mode.
21 * @pullups: enable/disable i2c controller pullup resistors.
21 */22 */
22struct st_sensors_platform_data {23struct st_sensors_platform_data {
23 u8 drdy_int_pin;24 u8 drdy_int_pin;
24 bool open_drain;25 bool open_drain;
25 bool spi_3wire;26 bool spi_3wire;
27 bool pullups;
26};28};
27 29
28#endif /* ST_SENSORS_PDATA_H */30#endif /* ST_SENSORS_PDATA_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t