1
2
3
4
5
6
7
8
9
10
11#ifndef ST_LSM6DSX_H
12#define ST_LSM6DSX_H
13
14#include <linux/device.h>
15#include <linux/iio/iio.h>
16#include <linux/regulator/consumer.h>
17
18#define ST_LSM6DS3_DEV_NAME "lsm6ds3"
19#define ST_LSM6DS3H_DEV_NAME "lsm6ds3h"
20#define ST_LSM6DSL_DEV_NAME "lsm6dsl"
21#define ST_LSM6DSM_DEV_NAME "lsm6dsm"
22#define ST_ISM330DLC_DEV_NAME "ism330dlc"
23#define ST_LSM6DSO_DEV_NAME "lsm6dso"
24#define ST_ASM330LHH_DEV_NAME "asm330lhh"
25#define ST_LSM6DSOX_DEV_NAME "lsm6dsox"
26#define ST_LSM6DSR_DEV_NAME "lsm6dsr"
27#define ST_LSM6DS3TRC_DEV_NAME "lsm6ds3tr-c"
28#define ST_ISM330DHCX_DEV_NAME "ism330dhcx"
29#define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu"
30#define ST_LSM6DS0_DEV_NAME "lsm6ds0"
31#define ST_LSM6DSRX_DEV_NAME "lsm6dsrx"
32#define ST_LSM6DST_DEV_NAME "lsm6dst"
33#define ST_LSM6DSOP_DEV_NAME "lsm6dsop"
34
35enum st_lsm6dsx_hw_id {
36 ST_LSM6DS3_ID,
37 ST_LSM6DS3H_ID,
38 ST_LSM6DSL_ID,
39 ST_LSM6DSM_ID,
40 ST_ISM330DLC_ID,
41 ST_LSM6DSO_ID,
42 ST_ASM330LHH_ID,
43 ST_LSM6DSOX_ID,
44 ST_LSM6DSR_ID,
45 ST_LSM6DS3TRC_ID,
46 ST_ISM330DHCX_ID,
47 ST_LSM9DS1_ID,
48 ST_LSM6DS0_ID,
49 ST_LSM6DSRX_ID,
50 ST_LSM6DST_ID,
51 ST_LSM6DSOP_ID,
52 ST_LSM6DSX_MAX_ID,
53};
54
55#define ST_LSM6DSX_BUFF_SIZE 512
56#define ST_LSM6DSX_CHAN_SIZE 2
57#define ST_LSM6DSX_SAMPLE_SIZE 6
58#define ST_LSM6DSX_TAG_SIZE 1
59#define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \
60 ST_LSM6DSX_TAG_SIZE)
61#define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
62 ST_LSM6DSX_SAMPLE_SIZE)
63#define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
64 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
65#define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
66
67#define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx) \
68{ \
69 .type = chan_type, \
70 .address = addr, \
71 .modified = 1, \
72 .channel2 = mod, \
73 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
74 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
75 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
76 .scan_index = scan_idx, \
77 .scan_type = { \
78 .sign = 's', \
79 .realbits = 16, \
80 .storagebits = 16, \
81 .endianness = IIO_LE, \
82 }, \
83 .event_spec = &st_lsm6dsx_event, \
84 .ext_info = st_lsm6dsx_accel_ext_info, \
85 .num_event_specs = 1, \
86}
87
88#define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
89{ \
90 .type = chan_type, \
91 .address = addr, \
92 .modified = 1, \
93 .channel2 = mod, \
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
96 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
97 .scan_index = scan_idx, \
98 .scan_type = { \
99 .sign = 's', \
100 .realbits = 16, \
101 .storagebits = 16, \
102 .endianness = IIO_LE, \
103 }, \
104}
105
106struct st_lsm6dsx_reg {
107 u8 addr;
108 u8 mask;
109};
110
111struct st_lsm6dsx_sensor;
112struct st_lsm6dsx_hw;
113
114struct st_lsm6dsx_odr {
115 u32 milli_hz;
116 u8 val;
117};
118
119#define ST_LSM6DSX_ODR_LIST_SIZE 8
120struct st_lsm6dsx_odr_table_entry {
121 struct st_lsm6dsx_reg reg;
122
123 struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
124 int odr_len;
125};
126
127struct st_lsm6dsx_fs {
128 u32 gain;
129 u8 val;
130};
131
132#define ST_LSM6DSX_FS_LIST_SIZE 4
133struct st_lsm6dsx_fs_table_entry {
134 struct st_lsm6dsx_reg reg;
135
136 struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
137 int fs_len;
138};
139
140
141
142
143
144
145
146
147
148struct st_lsm6dsx_fifo_ops {
149 int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
150 int (*read_fifo)(struct st_lsm6dsx_hw *hw);
151 struct {
152 u8 addr;
153 u16 mask;
154 } fifo_th;
155 struct {
156 u8 addr;
157 u16 mask;
158 } fifo_diff;
159 u8 th_wl;
160};
161
162
163
164
165
166
167
168
169
170struct st_lsm6dsx_hw_ts_settings {
171 struct st_lsm6dsx_reg timer_en;
172 struct st_lsm6dsx_reg hr_timer;
173 struct st_lsm6dsx_reg fifo_en;
174 struct st_lsm6dsx_reg decimator;
175 u8 freq_fine;
176};
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193struct st_lsm6dsx_shub_settings {
194 struct st_lsm6dsx_reg page_mux;
195 struct {
196 bool sec_page;
197 u8 addr;
198 u8 mask;
199 } master_en;
200 struct {
201 bool sec_page;
202 u8 addr;
203 u8 mask;
204 } pullup_en;
205 struct st_lsm6dsx_reg aux_sens;
206 struct st_lsm6dsx_reg wr_once;
207 struct st_lsm6dsx_reg emb_func;
208 u8 num_ext_dev;
209 struct {
210 bool sec_page;
211 u8 addr;
212 } shub_out;
213 u8 slv0_addr;
214 u8 dw_slv0_addr;
215 u8 batch_en;
216 u8 pause;
217};
218
219struct st_lsm6dsx_event_settings {
220 struct st_lsm6dsx_reg enable_reg;
221 struct st_lsm6dsx_reg wakeup_reg;
222 u8 wakeup_src_reg;
223 u8 wakeup_src_status_mask;
224 u8 wakeup_src_z_mask;
225 u8 wakeup_src_y_mask;
226 u8 wakeup_src_x_mask;
227};
228
229enum st_lsm6dsx_ext_sensor_id {
230 ST_LSM6DSX_ID_MAGN,
231};
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246struct st_lsm6dsx_ext_dev_settings {
247 u8 i2c_addr[2];
248 struct {
249 u8 addr;
250 u8 val;
251 } wai;
252 enum st_lsm6dsx_ext_sensor_id id;
253 struct st_lsm6dsx_odr_table_entry odr_table;
254 struct st_lsm6dsx_fs_table_entry fs_table;
255 struct st_lsm6dsx_reg temp_comp;
256 struct {
257 struct st_lsm6dsx_reg reg;
258 u8 off_val;
259 u8 on_val;
260 } pwr_table;
261 struct st_lsm6dsx_reg off_canc;
262 struct st_lsm6dsx_reg bdu;
263 struct {
264 u8 addr;
265 u8 len;
266 } out;
267};
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287struct st_lsm6dsx_settings {
288 struct st_lsm6dsx_reg reset;
289 struct st_lsm6dsx_reg boot;
290 struct st_lsm6dsx_reg bdu;
291 u16 max_fifo_size;
292 struct {
293 enum st_lsm6dsx_hw_id hw_id;
294 const char *name;
295 u8 wai;
296 } id[ST_LSM6DSX_MAX_ID];
297 struct {
298 const struct iio_chan_spec *chan;
299 int len;
300 } channels[2];
301 struct {
302 struct st_lsm6dsx_reg irq1;
303 struct st_lsm6dsx_reg irq2;
304 struct st_lsm6dsx_reg irq1_func;
305 struct st_lsm6dsx_reg irq2_func;
306 struct st_lsm6dsx_reg lir;
307 struct st_lsm6dsx_reg clear_on_read;
308 struct st_lsm6dsx_reg hla;
309 struct st_lsm6dsx_reg od;
310 } irq_config;
311 struct st_lsm6dsx_reg drdy_mask;
312 struct st_lsm6dsx_odr_table_entry odr_table[2];
313 struct st_lsm6dsx_fs_table_entry fs_table[2];
314 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
315 struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
316 struct st_lsm6dsx_fifo_ops fifo_ops;
317 struct st_lsm6dsx_hw_ts_settings ts_settings;
318 struct st_lsm6dsx_shub_settings shub_settings;
319 struct st_lsm6dsx_event_settings event_settings;
320};
321
322enum st_lsm6dsx_sensor_id {
323 ST_LSM6DSX_ID_GYRO,
324 ST_LSM6DSX_ID_ACC,
325 ST_LSM6DSX_ID_EXT0,
326 ST_LSM6DSX_ID_EXT1,
327 ST_LSM6DSX_ID_EXT2,
328 ST_LSM6DSX_ID_MAX,
329};
330
331enum st_lsm6dsx_fifo_mode {
332 ST_LSM6DSX_FIFO_BYPASS = 0x0,
333 ST_LSM6DSX_FIFO_CONT = 0x6,
334};
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349struct st_lsm6dsx_sensor {
350 char name[32];
351 enum st_lsm6dsx_sensor_id id;
352 struct st_lsm6dsx_hw *hw;
353
354 u32 gain;
355 u32 odr;
356
357 u16 watermark;
358 u8 decimator;
359 u8 sip;
360 s64 ts_ref;
361
362 struct {
363 const struct st_lsm6dsx_ext_dev_settings *settings;
364 u32 slv_odr;
365 u8 addr;
366 } ext_info;
367};
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393struct st_lsm6dsx_hw {
394 struct device *dev;
395 struct regmap *regmap;
396 struct regulator_bulk_data regulators[2];
397 int irq;
398
399 struct mutex fifo_lock;
400 struct mutex conf_lock;
401 struct mutex page_lock;
402
403 u8 suspend_mask;
404 u8 enable_mask;
405 u8 fifo_mask;
406 s64 ts_gain;
407 u8 ts_sip;
408 u8 sip;
409
410 const struct st_lsm6dsx_reg *irq_routing;
411 u8 event_threshold;
412 u8 enable_event;
413
414 u8 *buff;
415
416 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
417
418 const struct st_lsm6dsx_settings *settings;
419
420 struct iio_mount_matrix orientation;
421
422 struct {
423 __le16 channels[3];
424 s64 ts __aligned(8);
425 } scan[3];
426};
427
428static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = {
429 .type = IIO_EV_TYPE_THRESH,
430 .dir = IIO_EV_DIR_EITHER,
431 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
432 BIT(IIO_EV_INFO_ENABLE)
433};
434
435static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
436 0x7, 0x0,
437};
438
439extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
440
441int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
442 struct regmap *regmap);
443int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
444 bool enable);
445int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
446int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
447int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
448 u16 watermark);
449int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
450int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
451int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw);
452int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
453int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
454int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
455int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
456int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
457int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
458
459static inline int
460st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
461 unsigned int mask, unsigned int val)
462{
463 int err;
464
465 mutex_lock(&hw->page_lock);
466 err = regmap_update_bits(hw->regmap, addr, mask, val);
467 mutex_unlock(&hw->page_lock);
468
469 return err;
470}
471
472static inline int
473st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
474 void *val, unsigned int len)
475{
476 int err;
477
478 mutex_lock(&hw->page_lock);
479 err = regmap_bulk_read(hw->regmap, addr, val, len);
480 mutex_unlock(&hw->page_lock);
481
482 return err;
483}
484
485static inline int
486st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
487 unsigned int val)
488{
489 int err;
490
491 mutex_lock(&hw->page_lock);
492 err = regmap_write(hw->regmap, addr, val);
493 mutex_unlock(&hw->page_lock);
494
495 return err;
496}
497
498static inline const struct iio_mount_matrix *
499st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev,
500 const struct iio_chan_spec *chan)
501{
502 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
503 struct st_lsm6dsx_hw *hw = sensor->hw;
504
505 return &hw->orientation;
506}
507
508static const
509struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_accel_ext_info[] = {
510 IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_lsm6dsx_get_mount_matrix),
511 { }
512};
513
514#endif
515