xref: /linux/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h (revision 9e56ff53b4115875667760445b028357848b4748)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * STMicroelectronics st_lsm6dsx sensor driver
4  *
5  * Copyright 2016 STMicroelectronics Inc.
6  *
7  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
8  * Denis Ciocca <denis.ciocca@st.com>
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 #define ST_ASM330LHHX_DEV_NAME	"asm330lhhx"
35 #define ST_LSM6DSTX_DEV_NAME	"lsm6dstx"
36 #define ST_LSM6DSV_DEV_NAME	"lsm6dsv"
37 #define ST_LSM6DSV16X_DEV_NAME	"lsm6dsv16x"
38 #define ST_LSM6DSO16IS_DEV_NAME	"lsm6dso16is"
39 #define ST_ISM330IS_DEV_NAME	"ism330is"
40 #define ST_ASM330LHB_DEV_NAME	"asm330lhb"
41 
42 enum st_lsm6dsx_hw_id {
43 	ST_LSM6DS3_ID = 1,
44 	ST_LSM6DS3H_ID,
45 	ST_LSM6DSL_ID,
46 	ST_LSM6DSM_ID,
47 	ST_ISM330DLC_ID,
48 	ST_LSM6DSO_ID,
49 	ST_ASM330LHH_ID,
50 	ST_LSM6DSOX_ID,
51 	ST_LSM6DSR_ID,
52 	ST_LSM6DS3TRC_ID,
53 	ST_ISM330DHCX_ID,
54 	ST_LSM9DS1_ID,
55 	ST_LSM6DS0_ID,
56 	ST_LSM6DSRX_ID,
57 	ST_LSM6DST_ID,
58 	ST_LSM6DSOP_ID,
59 	ST_ASM330LHHX_ID,
60 	ST_LSM6DSTX_ID,
61 	ST_LSM6DSV_ID,
62 	ST_LSM6DSV16X_ID,
63 	ST_LSM6DSO16IS_ID,
64 	ST_ISM330IS_ID,
65 	ST_ASM330LHB_ID,
66 	ST_LSM6DSX_MAX_ID,
67 };
68 
69 #define ST_LSM6DSX_BUFF_SIZE		512
70 #define ST_LSM6DSX_CHAN_SIZE		2
71 #define ST_LSM6DSX_SAMPLE_SIZE		6
72 #define ST_LSM6DSX_TAG_SIZE		1
73 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE	(ST_LSM6DSX_SAMPLE_SIZE + \
74 					 ST_LSM6DSX_TAG_SIZE)
75 #define ST_LSM6DSX_MAX_WORD_LEN		((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
76 					 ST_LSM6DSX_SAMPLE_SIZE)
77 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN	((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
78 					 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
79 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
80 
81 #define ST_LSM6DSX_CHANNEL_ACC(chan_type, addr, mod, scan_idx)		\
82 {									\
83 	.type = chan_type,						\
84 	.address = addr,						\
85 	.modified = 1,							\
86 	.channel2 = mod,						\
87 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
88 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
89 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
90 	.scan_index = scan_idx,						\
91 	.scan_type = {							\
92 		.sign = 's',						\
93 		.realbits = 16,						\
94 		.storagebits = 16,					\
95 		.endianness = IIO_LE,					\
96 	},								\
97 	.event_spec = &st_lsm6dsx_event,				\
98 	.ext_info = st_lsm6dsx_ext_info,				\
99 	.num_event_specs = 1,						\
100 }
101 
102 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx)		\
103 {									\
104 	.type = chan_type,						\
105 	.address = addr,						\
106 	.modified = 1,							\
107 	.channel2 = mod,						\
108 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
109 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
110 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
111 	.scan_index = scan_idx,						\
112 	.scan_type = {							\
113 		.sign = 's',						\
114 		.realbits = 16,						\
115 		.storagebits = 16,					\
116 		.endianness = IIO_LE,					\
117 	},								\
118 	.ext_info = st_lsm6dsx_ext_info,				\
119 }
120 
121 struct st_lsm6dsx_reg {
122 	u8 addr;
123 	u8 mask;
124 };
125 
126 struct st_lsm6dsx_sensor;
127 struct st_lsm6dsx_hw;
128 
129 struct st_lsm6dsx_odr {
130 	u32 milli_hz;
131 	u8 val;
132 };
133 
134 #define ST_LSM6DSX_ODR_LIST_SIZE	8
135 struct st_lsm6dsx_odr_table_entry {
136 	struct st_lsm6dsx_reg reg;
137 
138 	struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
139 	int odr_len;
140 };
141 
142 struct st_lsm6dsx_samples_to_discard {
143 	struct {
144 		u32 milli_hz;
145 		u16 samples;
146 	} val[ST_LSM6DSX_ODR_LIST_SIZE];
147 };
148 
149 struct st_lsm6dsx_fs {
150 	u32 gain;
151 	u8 val;
152 };
153 
154 #define ST_LSM6DSX_FS_LIST_SIZE		4
155 struct st_lsm6dsx_fs_table_entry {
156 	struct st_lsm6dsx_reg reg;
157 
158 	struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
159 	int fs_len;
160 };
161 
162 /**
163  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
164  * @update_fifo: Update FIFO configuration callback.
165  * @read_fifo: Read FIFO callback.
166  * @fifo_th: FIFO threshold register info (addr + mask).
167  * @fifo_diff: FIFO diff status register info (addr + mask).
168  * @max_size: Sensor max fifo length in FIFO words.
169  * @th_wl: FIFO threshold word length.
170  */
171 struct st_lsm6dsx_fifo_ops {
172 	int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
173 	int (*read_fifo)(struct st_lsm6dsx_hw *hw);
174 	struct {
175 		u8 addr;
176 		u16 mask;
177 	} fifo_th;
178 	struct {
179 		u8 addr;
180 		u16 mask;
181 	} fifo_diff;
182 	u16 max_size;
183 	u8 th_wl;
184 };
185 
186 /**
187  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
188  * @timer_en: Hw timer enable register info (addr + mask).
189  * @hr_timer: Hw timer resolution register info (addr + mask).
190  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
191  * @decimator: Hw timer FIFO decimator register info (addr + mask).
192  * @freq_fine: Difference in % of ODR with respect to the typical.
193  */
194 struct st_lsm6dsx_hw_ts_settings {
195 	struct st_lsm6dsx_reg timer_en;
196 	struct st_lsm6dsx_reg hr_timer;
197 	struct st_lsm6dsx_reg fifo_en;
198 	struct st_lsm6dsx_reg decimator;
199 	u8 freq_fine;
200 };
201 
202 /**
203  * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
204  * @page_mux: register page mux info (addr + mask).
205  * @master_en: master config register info (addr + mask).
206  * @pullup_en: i2c controller pull-up register info (addr + mask).
207  * @aux_sens: aux sensor register info (addr + mask).
208  * @wr_once: write_once register info (addr + mask).
209  * @emb_func:  embedded function register info (addr + mask).
210  * @num_ext_dev: max number of slave devices.
211  * @shub_out: sensor hub first output register info.
212  * @slv0_addr: slave0 address in secondary page.
213  * @dw_slv0_addr: slave0 write register address in secondary page.
214  * @batch_en: Enable/disable FIFO batching.
215  * @pause: controller pause value.
216  */
217 struct st_lsm6dsx_shub_settings {
218 	struct st_lsm6dsx_reg page_mux;
219 	struct {
220 		bool sec_page;
221 		u8 addr;
222 		u8 mask;
223 	} master_en;
224 	struct {
225 		bool sec_page;
226 		u8 addr;
227 		u8 mask;
228 	} pullup_en;
229 	struct st_lsm6dsx_reg aux_sens;
230 	struct st_lsm6dsx_reg wr_once;
231 	struct st_lsm6dsx_reg emb_func;
232 	u8 num_ext_dev;
233 	struct {
234 		bool sec_page;
235 		u8 addr;
236 	} shub_out;
237 	u8 slv0_addr;
238 	u8 dw_slv0_addr;
239 	u8 batch_en;
240 	u8 pause;
241 };
242 
243 struct st_lsm6dsx_event_settings {
244 	struct st_lsm6dsx_reg enable_reg;
245 	struct st_lsm6dsx_reg wakeup_reg;
246 	u8 wakeup_src_reg;
247 	u8 wakeup_src_status_mask;
248 	u8 wakeup_src_z_mask;
249 	u8 wakeup_src_y_mask;
250 	u8 wakeup_src_x_mask;
251 };
252 
253 enum st_lsm6dsx_ext_sensor_id {
254 	ST_LSM6DSX_ID_MAGN,
255 };
256 
257 /**
258  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
259  * @i2c_addr: I2c slave address list.
260  * @wai: Wai address info.
261  * @id: external sensor id.
262  * @odr_table: Output data rate of the sensor [Hz].
263  * @fs_table: Configured sensor sensitivity table depending on full scale.
264  * @temp_comp: Temperature compensation register info (addr + mask).
265  * @pwr_table: Power on register info (addr + mask).
266  * @off_canc: Offset cancellation register info (addr + mask).
267  * @bdu: Block data update register info (addr + mask).
268  * @out: Output register info.
269  */
270 struct st_lsm6dsx_ext_dev_settings {
271 	u8 i2c_addr[2];
272 	struct {
273 		u8 addr;
274 		u8 val;
275 	} wai;
276 	enum st_lsm6dsx_ext_sensor_id id;
277 	struct st_lsm6dsx_odr_table_entry odr_table;
278 	struct st_lsm6dsx_fs_table_entry fs_table;
279 	struct st_lsm6dsx_reg temp_comp;
280 	struct {
281 		struct st_lsm6dsx_reg reg;
282 		u8 off_val;
283 		u8 on_val;
284 	} pwr_table;
285 	struct st_lsm6dsx_reg off_canc;
286 	struct st_lsm6dsx_reg bdu;
287 	struct {
288 		u8 addr;
289 		u8 len;
290 	} out;
291 };
292 
293 /**
294  * struct st_lsm6dsx_settings - ST IMU sensor settings
295  * @reset: register address for reset.
296  * @boot: register address for boot.
297  * @bdu: register address for Block Data Update.
298  * @id: List of hw id/device name supported by the driver configuration.
299  * @channels: IIO channels supported by the device.
300  * @irq_config: interrupts related registers.
301  * @drdy_mask: register info for data-ready mask (addr + mask).
302  * @odr_table: Hw sensors odr table (Hz + val).
303  * @samples_to_discard: Number of samples to discard for filters settling time.
304  * @fs_table: Hw sensors gain table (gain + val).
305  * @decimator: List of decimator register info (addr + mask).
306  * @batch: List of FIFO batching register info (addr + mask).
307  * @fifo_ops: Sensor hw FIFO parameters.
308  * @ts_settings: Hw timer related settings.
309  * @shub_settings: i2c controller related settings.
310  */
311 struct st_lsm6dsx_settings {
312 	struct st_lsm6dsx_reg reset;
313 	struct st_lsm6dsx_reg boot;
314 	struct st_lsm6dsx_reg bdu;
315 	struct {
316 		enum st_lsm6dsx_hw_id hw_id;
317 		const char *name;
318 		u8 wai;
319 	} id[ST_LSM6DSX_MAX_ID];
320 	struct {
321 		const struct iio_chan_spec *chan;
322 		int len;
323 	} channels[2];
324 	struct {
325 		struct st_lsm6dsx_reg irq1;
326 		struct st_lsm6dsx_reg irq2;
327 		struct st_lsm6dsx_reg irq1_func;
328 		struct st_lsm6dsx_reg irq2_func;
329 		struct st_lsm6dsx_reg lir;
330 		struct st_lsm6dsx_reg clear_on_read;
331 		struct st_lsm6dsx_reg hla;
332 		struct st_lsm6dsx_reg od;
333 	} irq_config;
334 	struct st_lsm6dsx_reg drdy_mask;
335 	struct st_lsm6dsx_odr_table_entry odr_table[2];
336 	struct st_lsm6dsx_samples_to_discard samples_to_discard[2];
337 	struct st_lsm6dsx_fs_table_entry fs_table[2];
338 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
339 	struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
340 	struct st_lsm6dsx_fifo_ops fifo_ops;
341 	struct st_lsm6dsx_hw_ts_settings ts_settings;
342 	struct st_lsm6dsx_shub_settings shub_settings;
343 	struct st_lsm6dsx_event_settings event_settings;
344 };
345 
346 enum st_lsm6dsx_sensor_id {
347 	ST_LSM6DSX_ID_GYRO,
348 	ST_LSM6DSX_ID_ACC,
349 	ST_LSM6DSX_ID_EXT0,
350 	ST_LSM6DSX_ID_EXT1,
351 	ST_LSM6DSX_ID_EXT2,
352 	ST_LSM6DSX_ID_MAX,
353 };
354 
355 enum st_lsm6dsx_fifo_mode {
356 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
357 	ST_LSM6DSX_FIFO_CONT = 0x6,
358 };
359 
360 /**
361  * struct st_lsm6dsx_sensor - ST IMU sensor instance
362  * @name: Sensor name.
363  * @id: Sensor identifier.
364  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
365  * @gain: Configured sensor sensitivity.
366  * @odr: Output data rate of the sensor [Hz].
367  * @samples_to_discard: Number of samples to discard for filters settling time.
368  * @watermark: Sensor watermark level.
369  * @decimator: Sensor decimation factor.
370  * @sip: Number of samples in a given pattern.
371  * @ts_ref: Sensor timestamp reference for hw one.
372  * @ext_info: Sensor settings if it is connected to i2c controller
373  */
374 struct st_lsm6dsx_sensor {
375 	char name[32];
376 	enum st_lsm6dsx_sensor_id id;
377 	struct st_lsm6dsx_hw *hw;
378 
379 	u32 gain;
380 	u32 odr;
381 
382 	u16 samples_to_discard;
383 	u16 watermark;
384 	u8 decimator;
385 	u8 sip;
386 	s64 ts_ref;
387 
388 	struct {
389 		const struct st_lsm6dsx_ext_dev_settings *settings;
390 		u32 slv_odr;
391 		u8 addr;
392 	} ext_info;
393 };
394 
395 /**
396  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
397  * @dev: Pointer to instance of struct device (I2C or SPI).
398  * @regmap: Register map of the device.
399  * @irq: Device interrupt line (I2C or SPI).
400  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
401  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
402  * @page_lock: Mutex to prevent concurrent memory page configuration.
403  * @suspend_mask: Suspended sensor bitmask.
404  * @enable_mask: Enabled sensor bitmask.
405  * @fifo_mask: Enabled hw FIFO bitmask.
406  * @ts_gain: Hw timestamp rate after internal calibration.
407  * @ts_sip: Total number of timestamp samples in a given pattern.
408  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
409  * @buff: Device read buffer.
410  * @irq_routing: pointer to interrupt routing configuration.
411  * @event_threshold: wakeup event threshold.
412  * @enable_event: enabled event bitmask.
413  * @iio_devs: Pointers to acc/gyro iio_dev instances.
414  * @settings: Pointer to the specific sensor settings in use.
415  * @orientation: sensor chip orientation relative to main hardware.
416  * @scan: Temporary buffers used to align data before iio_push_to_buffers()
417  */
418 struct st_lsm6dsx_hw {
419 	struct device *dev;
420 	struct regmap *regmap;
421 	int irq;
422 
423 	struct mutex fifo_lock;
424 	struct mutex conf_lock;
425 	struct mutex page_lock;
426 
427 	u8 suspend_mask;
428 	u8 enable_mask;
429 	u8 fifo_mask;
430 	s64 ts_gain;
431 	u8 ts_sip;
432 	u8 sip;
433 
434 	const struct st_lsm6dsx_reg *irq_routing;
435 	u8 event_threshold;
436 	u8 enable_event;
437 
438 	u8 *buff;
439 
440 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
441 
442 	const struct st_lsm6dsx_settings *settings;
443 
444 	struct iio_mount_matrix orientation;
445 	/* Ensure natural alignment of buffer elements */
446 	struct {
447 		__le16 channels[3];
448 		s64 ts __aligned(8);
449 	} scan[ST_LSM6DSX_ID_MAX];
450 };
451 
452 static __maybe_unused const struct iio_event_spec st_lsm6dsx_event = {
453 	.type = IIO_EV_TYPE_THRESH,
454 	.dir = IIO_EV_DIR_EITHER,
455 	.mask_separate = BIT(IIO_EV_INFO_VALUE) |
456 			 BIT(IIO_EV_INFO_ENABLE)
457 };
458 
459 static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
460 	0x7, 0x0,
461 };
462 
463 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
464 
465 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
466 		     struct regmap *regmap);
467 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
468 				 bool enable);
469 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
470 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
471 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
472 				u16 watermark);
473 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
474 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
475 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw);
476 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
477 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
478 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
479 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
480 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
481 int st_lsm6dsx_shub_read_output(struct st_lsm6dsx_hw *hw, u8 *data, int len);
482 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
483 
484 static inline int
485 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
486 			      unsigned int mask, unsigned int val)
487 {
488 	int err;
489 
490 	mutex_lock(&hw->page_lock);
491 	err = regmap_update_bits(hw->regmap, addr, mask, val);
492 	mutex_unlock(&hw->page_lock);
493 
494 	return err;
495 }
496 
497 static inline int
498 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
499 		       void *val, unsigned int len)
500 {
501 	int err;
502 
503 	mutex_lock(&hw->page_lock);
504 	err = regmap_bulk_read(hw->regmap, addr, val, len);
505 	mutex_unlock(&hw->page_lock);
506 
507 	return err;
508 }
509 
510 static inline int
511 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
512 			unsigned int val)
513 {
514 	int err;
515 
516 	mutex_lock(&hw->page_lock);
517 	err = regmap_write(hw->regmap, addr, val);
518 	mutex_unlock(&hw->page_lock);
519 
520 	return err;
521 }
522 
523 static inline const struct iio_mount_matrix *
524 st_lsm6dsx_get_mount_matrix(const struct iio_dev *iio_dev,
525 			    const struct iio_chan_spec *chan)
526 {
527 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
528 	struct st_lsm6dsx_hw *hw = sensor->hw;
529 
530 	return &hw->orientation;
531 }
532 
533 static inline int
534 st_lsm6dsx_device_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)
535 {
536 	if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
537 	    sensor->id == ST_LSM6DSX_ID_EXT1 ||
538 	    sensor->id == ST_LSM6DSX_ID_EXT2)
539 		return st_lsm6dsx_shub_set_enable(sensor, enable);
540 
541 	return st_lsm6dsx_sensor_set_enable(sensor, enable);
542 }
543 
544 static const
545 struct iio_chan_spec_ext_info __maybe_unused st_lsm6dsx_ext_info[] = {
546 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_ALL, st_lsm6dsx_get_mount_matrix),
547 	{ }
548 };
549 
550 #endif /* ST_LSM6DSX_H */
551