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