Skip to content

Commit c91c1c8

Browse files
Lorenzo BianconiJonathan Cameron
authored andcommitted
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) Signed-off-by: Lorenzo Bianconi <lorenzo.bianconi@redhat.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 1775044 commit c91c1c8

5 files changed

Lines changed: 911 additions & 43 deletions

File tree

‎drivers/iio/imu/st_lsm6dsx/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o
1+
st_lsm6dsx-y := st_lsm6dsx_core.o st_lsm6dsx_buffer.o \
2+
st_lsm6dsx_shub.o
23

34
obj-$(CONFIG_IIO_ST_LSM6DSX) += st_lsm6dsx.o
45
obj-$(CONFIG_IIO_ST_LSM6DSX_I2C) += st_lsm6dsx_i2c.o

‎drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,53 @@ enum st_lsm6dsx_hw_id {
4343
* ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
4444
#define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
4545

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+
4664
struct st_lsm6dsx_reg {
4765
u8 addr;
4866
u8 mask;
4967
};
5068

5169
struct st_lsm6dsx_hw;
5270

71+
struct st_lsm6dsx_odr {
72+
u16 hz;
73+
u8 val;
74+
};
75+
76+
#define ST_LSM6DSX_ODR_LIST_SIZE 6
77+
struct 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+
82+
struct st_lsm6dsx_fs {
83+
u32 gain;
84+
u8 val;
85+
};
86+
87+
#define ST_LSM6DSX_FS_LIST_SIZE 4
88+
struct 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+
5393
/**
5494
* struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
5595
* @read_fifo: Read FIFO callback.
@@ -84,6 +124,66 @@ struct st_lsm6dsx_hw_ts_settings {
84124
struct st_lsm6dsx_reg decimator;
85125
};
86126

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+
*/
137+
struct 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+
147+
enum 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+
*/
164+
struct 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+
87187
/**
88188
* struct st_lsm6dsx_settings - ST IMU sensor settings
89189
* @wai: Sensor WhoAmI default value.
@@ -93,6 +193,7 @@ struct st_lsm6dsx_hw_ts_settings {
93193
* @batch: List of FIFO batching register info (addr + mask).
94194
* @fifo_ops: Sensor hw FIFO parameters.
95195
* @ts_settings: Hw timer related settings.
196+
* @shub_settings: i2c controller related settings.
96197
*/
97198
struct st_lsm6dsx_settings {
98199
u8 wai;
@@ -102,6 +203,7 @@ struct st_lsm6dsx_settings {
102203
struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
103204
struct st_lsm6dsx_fifo_ops fifo_ops;
104205
struct st_lsm6dsx_hw_ts_settings ts_settings;
206+
struct st_lsm6dsx_shub_settings shub_settings;
105207
};
106208

107209
enum st_lsm6dsx_sensor_id {
@@ -129,6 +231,7 @@ enum st_lsm6dsx_fifo_mode {
129231
* @sip: Number of samples in a given pattern.
130232
* @decimator: FIFO decimation factor.
131233
* @ts_ref: Sensor timestamp reference for hw one.
234+
* @ext_info: Sensor settings if it is connected to i2c controller
132235
*/
133236
struct st_lsm6dsx_sensor {
134237
char name[32];
@@ -142,6 +245,11 @@ struct st_lsm6dsx_sensor {
142245
u8 sip;
143246
u8 decimator;
144247
s64 ts_ref;
248+
249+
struct {
250+
const struct st_lsm6dsx_ext_dev_settings *settings;
251+
u8 addr;
252+
} ext_info;
145253
};
146254

147255
/**
@@ -181,6 +289,7 @@ struct st_lsm6dsx_hw {
181289
const struct st_lsm6dsx_settings *settings;
182290
};
183291

292+
static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
184293
extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
185294

186295
int 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,
197306
int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
198307
int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
199308
int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
309+
int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
310+
int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
311+
int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
200312

201313
static inline int
202314
st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,

‎drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c‎

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,6 @@
8888
#define ST_LSM6DSX_GYRO_FS_1000_GAIN IIO_DEGREE_TO_RAD(35000)
8989
#define ST_LSM6DSX_GYRO_FS_2000_GAIN IIO_DEGREE_TO_RAD(70000)
9090

91-
struct st_lsm6dsx_odr {
92-
u16 hz;
93-
u8 val;
94-
};
95-
96-
#define ST_LSM6DSX_ODR_LIST_SIZE 6
97-
struct 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-
10291
static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
10392
[ST_LSM6DSX_ID_ACC] = {
10493
.reg = {
@@ -126,17 +115,6 @@ static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
126115
}
127116
};
128117

129-
struct st_lsm6dsx_fs {
130-
u32 gain;
131-
u8 val;
132-
};
133-
134-
#define ST_LSM6DSX_FS_LIST_SIZE 4
135-
struct 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-
140118
static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {
141119
[ST_LSM6DSX_ID_ACC] = {
142120
.reg = {
@@ -342,27 +320,30 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
342320
.mask = GENMASK(7, 6),
343321
},
344322
},
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+
}
345344
},
346345
};
347346

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-
366347
static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
367348
ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,
368349
IIO_MOD_X, 0),
@@ -383,6 +364,21 @@ static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
383364
IIO_CHAN_SOFT_TIMESTAMP(3),
384365
};
385366

367+
int 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+
386382
static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)
387383
{
388384
int err, i, j, data;
@@ -736,8 +732,6 @@ static const struct iio_info st_lsm6dsx_gyro_info = {
736732
.hwfifo_set_watermark = st_lsm6dsx_set_watermark,
737733
};
738734

739-
static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
740-
741735
static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
742736
{
743737
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)
776770
return err;
777771
}
778772

773+
static 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+
779818
static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
780819
{
781820
const struct st_lsm6dsx_hw_ts_settings *ts_settings;
@@ -856,6 +895,10 @@ static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
856895
if (err < 0)
857896
return err;
858897

898+
err = st_lsm6dsx_init_shub(hw);
899+
if (err < 0)
900+
return err;
901+
859902
return st_lsm6dsx_init_hw_timer(hw);
860903
}
861904

@@ -909,6 +952,7 @@ static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
909952
int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
910953
struct regmap *regmap)
911954
{
955+
const struct st_lsm6dsx_shub_settings *hub_settings;
912956
struct st_lsm6dsx_hw *hw;
913957
int i, err;
914958

@@ -944,6 +988,13 @@ int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
944988
if (err < 0)
945989
return err;
946990

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+
947998
if (hw->irq > 0) {
948999
err = st_lsm6dsx_fifo_setup(hw);
9491000
if (err < 0)

0 commit comments

Comments
 (0)