hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h
....@@ -1,3 +1,4 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 * STMicroelectronics st_lsm6dsx sensor driver
34 *
....@@ -5,20 +6,28 @@
56 *
67 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
78 * Denis Ciocca <denis.ciocca@st.com>
8
- *
9
- * Licensed under the GPL-2.
109 */
1110
1211 #ifndef ST_LSM6DSX_H
1312 #define ST_LSM6DSX_H
1413
1514 #include <linux/device.h>
15
+#include <linux/iio/iio.h>
1616
1717 #define ST_LSM6DS3_DEV_NAME "lsm6ds3"
1818 #define ST_LSM6DS3H_DEV_NAME "lsm6ds3h"
1919 #define ST_LSM6DSL_DEV_NAME "lsm6dsl"
2020 #define ST_LSM6DSM_DEV_NAME "lsm6dsm"
2121 #define ST_ISM330DLC_DEV_NAME "ism330dlc"
22
+#define ST_LSM6DSO_DEV_NAME "lsm6dso"
23
+#define ST_ASM330LHH_DEV_NAME "asm330lhh"
24
+#define ST_LSM6DSOX_DEV_NAME "lsm6dsox"
25
+#define ST_LSM6DSR_DEV_NAME "lsm6dsr"
26
+#define ST_LSM6DS3TRC_DEV_NAME "lsm6ds3tr-c"
27
+#define ST_ISM330DHCX_DEV_NAME "ism330dhcx"
28
+#define ST_LSM9DS1_DEV_NAME "lsm9ds1-imu"
29
+#define ST_LSM6DS0_DEV_NAME "lsm6ds0"
30
+#define ST_LSM6DSRX_DEV_NAME "lsm6dsrx"
2231
2332 enum st_lsm6dsx_hw_id {
2433 ST_LSM6DS3_ID,
....@@ -26,28 +35,114 @@
2635 ST_LSM6DSL_ID,
2736 ST_LSM6DSM_ID,
2837 ST_ISM330DLC_ID,
38
+ ST_LSM6DSO_ID,
39
+ ST_ASM330LHH_ID,
40
+ ST_LSM6DSOX_ID,
41
+ ST_LSM6DSR_ID,
42
+ ST_LSM6DS3TRC_ID,
43
+ ST_ISM330DHCX_ID,
44
+ ST_LSM9DS1_ID,
45
+ ST_LSM6DS0_ID,
46
+ ST_LSM6DSRX_ID,
2947 ST_LSM6DSX_MAX_ID,
3048 };
3149
32
-#define ST_LSM6DSX_BUFF_SIZE 400
50
+#define ST_LSM6DSX_BUFF_SIZE 512
3351 #define ST_LSM6DSX_CHAN_SIZE 2
3452 #define ST_LSM6DSX_SAMPLE_SIZE 6
53
+#define ST_LSM6DSX_TAG_SIZE 1
54
+#define ST_LSM6DSX_TAGGED_SAMPLE_SIZE (ST_LSM6DSX_SAMPLE_SIZE + \
55
+ ST_LSM6DSX_TAG_SIZE)
3556 #define ST_LSM6DSX_MAX_WORD_LEN ((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
3657 ST_LSM6DSX_SAMPLE_SIZE)
58
+#define ST_LSM6DSX_MAX_TAGGED_WORD_LEN ((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
59
+ * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
3760 #define ST_LSM6DSX_SHIFT_VAL(val, mask) (((val) << __ffs(mask)) & (mask))
61
+
62
+#define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx) \
63
+{ \
64
+ .type = chan_type, \
65
+ .address = addr, \
66
+ .modified = 1, \
67
+ .channel2 = mod, \
68
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
69
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
70
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
71
+ .scan_index = scan_idx, \
72
+ .scan_type = { \
73
+ .sign = 's', \
74
+ .realbits = 16, \
75
+ .storagebits = 16, \
76
+ .endianness = IIO_LE, \
77
+ }, \
78
+ .event_spec = &st_lsm6dsx_event, \
79
+ .ext_info = st_lsm6dsx_accel_ext_info, \
80
+ .num_event_specs = 1, \
81
+}
82
+
83
+#define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx) \
84
+{ \
85
+ .type = chan_type, \
86
+ .address = addr, \
87
+ .modified = 1, \
88
+ .channel2 = mod, \
89
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
90
+ .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
91
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
92
+ .scan_index = scan_idx, \
93
+ .scan_type = { \
94
+ .sign = 's', \
95
+ .realbits = 16, \
96
+ .storagebits = 16, \
97
+ .endianness = IIO_LE, \
98
+ }, \
99
+}
38100
39101 struct st_lsm6dsx_reg {
40102 u8 addr;
41103 u8 mask;
42104 };
43105
106
+struct st_lsm6dsx_sensor;
107
+struct st_lsm6dsx_hw;
108
+
109
+struct st_lsm6dsx_odr {
110
+ u32 milli_hz;
111
+ u8 val;
112
+};
113
+
114
+#define ST_LSM6DSX_ODR_LIST_SIZE 8
115
+struct st_lsm6dsx_odr_table_entry {
116
+ struct st_lsm6dsx_reg reg;
117
+
118
+ struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
119
+ int odr_len;
120
+};
121
+
122
+struct st_lsm6dsx_fs {
123
+ u32 gain;
124
+ u8 val;
125
+};
126
+
127
+#define ST_LSM6DSX_FS_LIST_SIZE 4
128
+struct st_lsm6dsx_fs_table_entry {
129
+ struct st_lsm6dsx_reg reg;
130
+
131
+ struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
132
+ int fs_len;
133
+};
134
+
44135 /**
45136 * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
137
+ * @update_fifo: Update FIFO configuration callback.
138
+ * @read_fifo: Read FIFO callback.
46139 * @fifo_th: FIFO threshold register info (addr + mask).
47140 * @fifo_diff: FIFO diff status register info (addr + mask).
48141 * @th_wl: FIFO threshold word length.
49142 */
50143 struct st_lsm6dsx_fifo_ops {
144
+ int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
145
+ int (*read_fifo)(struct st_lsm6dsx_hw *hw);
51146 struct {
52147 u8 addr;
53148 u16 mask;
....@@ -65,35 +160,167 @@
65160 * @hr_timer: Hw timer resolution register info (addr + mask).
66161 * @fifo_en: Hw timer FIFO enable register info (addr + mask).
67162 * @decimator: Hw timer FIFO decimator register info (addr + mask).
163
+ * @freq_fine: Difference in % of ODR with respect to the typical.
68164 */
69165 struct st_lsm6dsx_hw_ts_settings {
70166 struct st_lsm6dsx_reg timer_en;
71167 struct st_lsm6dsx_reg hr_timer;
72168 struct st_lsm6dsx_reg fifo_en;
73169 struct st_lsm6dsx_reg decimator;
170
+ u8 freq_fine;
171
+};
172
+
173
+/**
174
+ * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
175
+ * @page_mux: register page mux info (addr + mask).
176
+ * @master_en: master config register info (addr + mask).
177
+ * @pullup_en: i2c controller pull-up register info (addr + mask).
178
+ * @aux_sens: aux sensor register info (addr + mask).
179
+ * @wr_once: write_once register info (addr + mask).
180
+ * @emb_func: embedded function register info (addr + mask).
181
+ * @num_ext_dev: max number of slave devices.
182
+ * @shub_out: sensor hub first output register info.
183
+ * @slv0_addr: slave0 address in secondary page.
184
+ * @dw_slv0_addr: slave0 write register address in secondary page.
185
+ * @batch_en: Enable/disable FIFO batching.
186
+ * @pause: controller pause value.
187
+ */
188
+struct st_lsm6dsx_shub_settings {
189
+ struct st_lsm6dsx_reg page_mux;
190
+ struct {
191
+ bool sec_page;
192
+ u8 addr;
193
+ u8 mask;
194
+ } master_en;
195
+ struct {
196
+ bool sec_page;
197
+ u8 addr;
198
+ u8 mask;
199
+ } pullup_en;
200
+ struct st_lsm6dsx_reg aux_sens;
201
+ struct st_lsm6dsx_reg wr_once;
202
+ struct st_lsm6dsx_reg emb_func;
203
+ u8 num_ext_dev;
204
+ struct {
205
+ bool sec_page;
206
+ u8 addr;
207
+ } shub_out;
208
+ u8 slv0_addr;
209
+ u8 dw_slv0_addr;
210
+ u8 batch_en;
211
+ u8 pause;
212
+};
213
+
214
+struct st_lsm6dsx_event_settings {
215
+ struct st_lsm6dsx_reg enable_reg;
216
+ struct st_lsm6dsx_reg wakeup_reg;
217
+ u8 wakeup_src_reg;
218
+ u8 wakeup_src_status_mask;
219
+ u8 wakeup_src_z_mask;
220
+ u8 wakeup_src_y_mask;
221
+ u8 wakeup_src_x_mask;
222
+};
223
+
224
+enum st_lsm6dsx_ext_sensor_id {
225
+ ST_LSM6DSX_ID_MAGN,
226
+};
227
+
228
+/**
229
+ * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
230
+ * @i2c_addr: I2c slave address list.
231
+ * @wai: Wai address info.
232
+ * @id: external sensor id.
233
+ * @odr_table: Output data rate of the sensor [Hz].
234
+ * @fs_table: Configured sensor sensitivity table depending on full scale.
235
+ * @temp_comp: Temperature compensation register info (addr + mask).
236
+ * @pwr_table: Power on register info (addr + mask).
237
+ * @off_canc: Offset cancellation register info (addr + mask).
238
+ * @bdu: Block data update register info (addr + mask).
239
+ * @out: Output register info.
240
+ */
241
+struct st_lsm6dsx_ext_dev_settings {
242
+ u8 i2c_addr[2];
243
+ struct {
244
+ u8 addr;
245
+ u8 val;
246
+ } wai;
247
+ enum st_lsm6dsx_ext_sensor_id id;
248
+ struct st_lsm6dsx_odr_table_entry odr_table;
249
+ struct st_lsm6dsx_fs_table_entry fs_table;
250
+ struct st_lsm6dsx_reg temp_comp;
251
+ struct {
252
+ struct st_lsm6dsx_reg reg;
253
+ u8 off_val;
254
+ u8 on_val;
255
+ } pwr_table;
256
+ struct st_lsm6dsx_reg off_canc;
257
+ struct st_lsm6dsx_reg bdu;
258
+ struct {
259
+ u8 addr;
260
+ u8 len;
261
+ } out;
74262 };
75263
76264 /**
77265 * struct st_lsm6dsx_settings - ST IMU sensor settings
78266 * @wai: Sensor WhoAmI default value.
267
+ * @reset: register address for reset.
268
+ * @boot: register address for boot.
269
+ * @bdu: register address for Block Data Update.
79270 * @max_fifo_size: Sensor max fifo length in FIFO words.
80
- * @id: List of hw id supported by the driver configuration.
271
+ * @id: List of hw id/device name supported by the driver configuration.
272
+ * @channels: IIO channels supported by the device.
273
+ * @irq_config: interrupts related registers.
274
+ * @drdy_mask: register info for data-ready mask (addr + mask).
275
+ * @odr_table: Hw sensors odr table (Hz + val).
276
+ * @fs_table: Hw sensors gain table (gain + val).
81277 * @decimator: List of decimator register info (addr + mask).
278
+ * @batch: List of FIFO batching register info (addr + mask).
82279 * @fifo_ops: Sensor hw FIFO parameters.
83280 * @ts_settings: Hw timer related settings.
281
+ * @shub_settings: i2c controller related settings.
84282 */
85283 struct st_lsm6dsx_settings {
86284 u8 wai;
285
+ struct st_lsm6dsx_reg reset;
286
+ struct st_lsm6dsx_reg boot;
287
+ struct st_lsm6dsx_reg bdu;
87288 u16 max_fifo_size;
88
- enum st_lsm6dsx_hw_id id[ST_LSM6DSX_MAX_ID];
289
+ struct {
290
+ enum st_lsm6dsx_hw_id hw_id;
291
+ const char *name;
292
+ } id[ST_LSM6DSX_MAX_ID];
293
+ struct {
294
+ const struct iio_chan_spec *chan;
295
+ int len;
296
+ } channels[2];
297
+ struct {
298
+ struct st_lsm6dsx_reg irq1;
299
+ struct st_lsm6dsx_reg irq2;
300
+ struct st_lsm6dsx_reg irq1_func;
301
+ struct st_lsm6dsx_reg irq2_func;
302
+ struct st_lsm6dsx_reg lir;
303
+ struct st_lsm6dsx_reg clear_on_read;
304
+ struct st_lsm6dsx_reg hla;
305
+ struct st_lsm6dsx_reg od;
306
+ } irq_config;
307
+ struct st_lsm6dsx_reg drdy_mask;
308
+ struct st_lsm6dsx_odr_table_entry odr_table[2];
309
+ struct st_lsm6dsx_fs_table_entry fs_table[2];
89310 struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
311
+ struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
90312 struct st_lsm6dsx_fifo_ops fifo_ops;
91313 struct st_lsm6dsx_hw_ts_settings ts_settings;
314
+ struct st_lsm6dsx_shub_settings shub_settings;
315
+ struct st_lsm6dsx_event_settings event_settings;
92316 };
93317
94318 enum st_lsm6dsx_sensor_id {
95
- ST_LSM6DSX_ID_ACC,
96319 ST_LSM6DSX_ID_GYRO,
320
+ ST_LSM6DSX_ID_ACC,
321
+ ST_LSM6DSX_ID_EXT0,
322
+ ST_LSM6DSX_ID_EXT1,
323
+ ST_LSM6DSX_ID_EXT2,
97324 ST_LSM6DSX_ID_MAX,
98325 };
99326
....@@ -110,9 +337,10 @@
110337 * @gain: Configured sensor sensitivity.
111338 * @odr: Output data rate of the sensor [Hz].
112339 * @watermark: Sensor watermark level.
340
+ * @decimator: Sensor decimation factor.
113341 * @sip: Number of samples in a given pattern.
114
- * @decimator: FIFO decimation factor.
115342 * @ts_ref: Sensor timestamp reference for hw one.
343
+ * @ext_info: Sensor settings if it is connected to i2c controller
116344 */
117345 struct st_lsm6dsx_sensor {
118346 char name[32];
....@@ -120,12 +348,18 @@
120348 struct st_lsm6dsx_hw *hw;
121349
122350 u32 gain;
123
- u16 odr;
351
+ u32 odr;
124352
125353 u16 watermark;
126
- u8 sip;
127354 u8 decimator;
355
+ u8 sip;
128356 s64 ts_ref;
357
+
358
+ struct {
359
+ const struct st_lsm6dsx_ext_dev_settings *settings;
360
+ u32 slv_odr;
361
+ u8 addr;
362
+ } ext_info;
129363 };
130364
131365 /**
....@@ -135,13 +369,21 @@
135369 * @irq: Device interrupt line (I2C or SPI).
136370 * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
137371 * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
138
- * @fifo_mode: FIFO operating mode supported by the device.
372
+ * @page_lock: Mutex to prevent concurrent memory page configuration.
373
+ * @suspend_mask: Suspended sensor bitmask.
139374 * @enable_mask: Enabled sensor bitmask.
375
+ * @fifo_mask: Enabled hw FIFO bitmask.
376
+ * @ts_gain: Hw timestamp rate after internal calibration.
140377 * @ts_sip: Total number of timestamp samples in a given pattern.
141378 * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
142379 * @buff: Device read buffer.
380
+ * @irq_routing: pointer to interrupt routing configuration.
381
+ * @event_threshold: wakeup event threshold.
382
+ * @enable_event: enabled event bitmask.
143383 * @iio_devs: Pointers to acc/gyro iio_dev instances.
144384 * @settings: Pointer to the specific sensor settings in use.
385
+ * @orientation: sensor chip orientation relative to main hardware.
386
+ * @scan: Temporary buffers used to align data before iio_push_to_buffers()
145387 */
146388 struct st_lsm6dsx_hw {
147389 struct device *dev;
....@@ -150,30 +392,117 @@
150392
151393 struct mutex fifo_lock;
152394 struct mutex conf_lock;
395
+ struct mutex page_lock;
153396
154
- enum st_lsm6dsx_fifo_mode fifo_mode;
397
+ u8 suspend_mask;
155398 u8 enable_mask;
399
+ u8 fifo_mask;
400
+ s64 ts_gain;
156401 u8 ts_sip;
157402 u8 sip;
403
+
404
+ const struct st_lsm6dsx_reg *irq_routing;
405
+ u8 event_threshold;
406
+ u8 enable_event;
158407
159408 u8 *buff;
160409
161410 struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
162411
163412 const struct st_lsm6dsx_settings *settings;
413
+
414
+ struct iio_mount_matrix orientation;
415
+ /* Ensure natural alignment of buffer elements */
416
+ struct {
417
+ __le16 channels[3];
418
+ s64 ts __aligned(8);
419
+ } scan[3];
420
+};
421
+
422
+static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = {
423
+ .type = IIO_EV_TYPE_THRESH,
424
+ .dir = IIO_EV_DIR_EITHER,
425
+ .mask_separate = BIT(IIO_EV_INFO_VALUE) |
426
+ BIT(IIO_EV_INFO_ENABLE)
427
+};
428
+
429
+static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
430
+ 0x7, 0x0,
164431 };
165432
166433 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
167434
168
-int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
435
+int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
169436 struct regmap *regmap);
170
-int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor);
171
-int st_lsm6dsx_sensor_disable(struct st_lsm6dsx_sensor *sensor);
437
+int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
438
+ bool enable);
172439 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
440
+int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
173441 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
174442 u16 watermark);
443
+int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
175444 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
176
-int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
177
- enum st_lsm6dsx_fifo_mode fifo_mode);
445
+int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw);
446
+int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
447
+int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
448
+int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
449
+int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
450
+int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
451
+int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
452
+
453
+static inline int
454
+st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
455
+ unsigned int mask, unsigned int val)
456
+{
457
+ int err;
458
+
459
+ mutex_lock(&hw->page_lock);
460
+ err = regmap_update_bits(hw->regmap, addr, mask, val);
461
+ mutex_unlock(&hw->page_lock);
462
+
463
+ return err;
464
+}
465
+
466
+static inline int
467
+st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
468
+ void *val, unsigned int len)
469
+{
470
+ int err;
471
+
472
+ mutex_lock(&hw->page_lock);
473
+ err = regmap_bulk_read(hw->regmap, addr, val, len);
474
+ mutex_unlock(&hw->page_lock);
475
+
476
+ return err;
477
+}
478
+
479
+static inline int
480
+st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
481
+ unsigned int val)
482
+{
483
+ int err;
484
+
485
+ mutex_lock(&hw->page_lock);
486
+ err = regmap_write(hw->regmap, addr, val);
487
+ mutex_unlock(&hw->page_lock);
488
+
489
+ return err;
490
+}
491
+
492
+static inline const struct iio_mount_matrix *
493
+st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev,
494
+ const struct iio_chan_spec *chan)
495
+{
496
+ struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
497
+ struct st_lsm6dsx_hw *hw = sensor->hw;
498
+
499
+ return &hw->orientation;
500
+}
501
+
502
+static const
503
+struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_accel_ext_info[] = {
504
+ IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_lsm6dsx_get_mount_matrix),
505
+ { }
506
+};
178507
179508 #endif /* ST_LSM6DSX_H */