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 
35 enum 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 
106 struct st_lsm6dsx_reg {
107 	u8 addr;
108 	u8 mask;
109 };
110 
111 struct st_lsm6dsx_sensor;
112 struct st_lsm6dsx_hw;
113 
114 struct st_lsm6dsx_odr {
115 	u32 milli_hz;
116 	u8 val;
117 };
118 
119 #define ST_LSM6DSX_ODR_LIST_SIZE	8
120 struct 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 
127 struct st_lsm6dsx_fs {
128 	u32 gain;
129 	u8 val;
130 };
131 
132 #define ST_LSM6DSX_FS_LIST_SIZE		4
133 struct 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  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
142  * @update_fifo: Update FIFO configuration callback.
143  * @read_fifo: Read FIFO callback.
144  * @fifo_th: FIFO threshold register info (addr + mask).
145  * @fifo_diff: FIFO diff status register info (addr + mask).
146  * @max_size: Sensor max fifo length in FIFO words.
147  * @th_wl: FIFO threshold word length.
148  */
149 struct st_lsm6dsx_fifo_ops {
150 	int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
151 	int (*read_fifo)(struct st_lsm6dsx_hw *hw);
152 	struct {
153 		u8 addr;
154 		u16 mask;
155 	} fifo_th;
156 	struct {
157 		u8 addr;
158 		u16 mask;
159 	} fifo_diff;
160 	u16 max_size;
161 	u8 th_wl;
162 };
163 
164 /**
165  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
166  * @timer_en: Hw timer enable register info (addr + mask).
167  * @hr_timer: Hw timer resolution register info (addr + mask).
168  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
169  * @decimator: Hw timer FIFO decimator register info (addr + mask).
170  * @freq_fine: Difference in % of ODR with respect to the typical.
171  */
172 struct st_lsm6dsx_hw_ts_settings {
173 	struct st_lsm6dsx_reg timer_en;
174 	struct st_lsm6dsx_reg hr_timer;
175 	struct st_lsm6dsx_reg fifo_en;
176 	struct st_lsm6dsx_reg decimator;
177 	u8 freq_fine;
178 };
179 
180 /**
181  * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
182  * @page_mux: register page mux info (addr + mask).
183  * @master_en: master config register info (addr + mask).
184  * @pullup_en: i2c controller pull-up register info (addr + mask).
185  * @aux_sens: aux sensor register info (addr + mask).
186  * @wr_once: write_once register info (addr + mask).
187  * @emb_func:  embedded function register info (addr + mask).
188  * @num_ext_dev: max number of slave devices.
189  * @shub_out: sensor hub first output register info.
190  * @slv0_addr: slave0 address in secondary page.
191  * @dw_slv0_addr: slave0 write register address in secondary page.
192  * @batch_en: Enable/disable FIFO batching.
193  * @pause: controller pause value.
194  */
195 struct st_lsm6dsx_shub_settings {
196 	struct st_lsm6dsx_reg page_mux;
197 	struct {
198 		bool sec_page;
199 		u8 addr;
200 		u8 mask;
201 	} master_en;
202 	struct {
203 		bool sec_page;
204 		u8 addr;
205 		u8 mask;
206 	} pullup_en;
207 	struct st_lsm6dsx_reg aux_sens;
208 	struct st_lsm6dsx_reg wr_once;
209 	struct st_lsm6dsx_reg emb_func;
210 	u8 num_ext_dev;
211 	struct {
212 		bool sec_page;
213 		u8 addr;
214 	} shub_out;
215 	u8 slv0_addr;
216 	u8 dw_slv0_addr;
217 	u8 batch_en;
218 	u8 pause;
219 };
220 
221 struct st_lsm6dsx_event_settings {
222 	struct st_lsm6dsx_reg enable_reg;
223 	struct st_lsm6dsx_reg wakeup_reg;
224 	u8 wakeup_src_reg;
225 	u8 wakeup_src_status_mask;
226 	u8 wakeup_src_z_mask;
227 	u8 wakeup_src_y_mask;
228 	u8 wakeup_src_x_mask;
229 };
230 
231 enum st_lsm6dsx_ext_sensor_id {
232 	ST_LSM6DSX_ID_MAGN,
233 };
234 
235 /**
236  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
237  * @i2c_addr: I2c slave address list.
238  * @wai: Wai address info.
239  * @id: external sensor id.
240  * @odr_table: Output data rate of the sensor [Hz].
241  * @fs_table: Configured sensor sensitivity table depending on full scale.
242  * @temp_comp: Temperature compensation register info (addr + mask).
243  * @pwr_table: Power on register info (addr + mask).
244  * @off_canc: Offset cancellation register info (addr + mask).
245  * @bdu: Block data update register info (addr + mask).
246  * @out: Output register info.
247  */
248 struct st_lsm6dsx_ext_dev_settings {
249 	u8 i2c_addr[2];
250 	struct {
251 		u8 addr;
252 		u8 val;
253 	} wai;
254 	enum st_lsm6dsx_ext_sensor_id id;
255 	struct st_lsm6dsx_odr_table_entry odr_table;
256 	struct st_lsm6dsx_fs_table_entry fs_table;
257 	struct st_lsm6dsx_reg temp_comp;
258 	struct {
259 		struct st_lsm6dsx_reg reg;
260 		u8 off_val;
261 		u8 on_val;
262 	} pwr_table;
263 	struct st_lsm6dsx_reg off_canc;
264 	struct st_lsm6dsx_reg bdu;
265 	struct {
266 		u8 addr;
267 		u8 len;
268 	} out;
269 };
270 
271 /**
272  * struct st_lsm6dsx_settings - ST IMU sensor settings
273  * @reset: register address for reset.
274  * @boot: register address for boot.
275  * @bdu: register address for Block Data Update.
276  * @id: List of hw id/device name supported by the driver configuration.
277  * @channels: IIO channels supported by the device.
278  * @irq_config: interrupts related registers.
279  * @drdy_mask: register info for data-ready mask (addr + mask).
280  * @odr_table: Hw sensors odr table (Hz + val).
281  * @fs_table: Hw sensors gain table (gain + val).
282  * @decimator: List of decimator register info (addr + mask).
283  * @batch: List of FIFO batching register info (addr + mask).
284  * @fifo_ops: Sensor hw FIFO parameters.
285  * @ts_settings: Hw timer related settings.
286  * @shub_settings: i2c controller related settings.
287  */
288 struct st_lsm6dsx_settings {
289 	struct st_lsm6dsx_reg reset;
290 	struct st_lsm6dsx_reg boot;
291 	struct st_lsm6dsx_reg bdu;
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 
322 enum 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 
331 enum st_lsm6dsx_fifo_mode {
332 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
333 	ST_LSM6DSX_FIFO_CONT = 0x6,
334 };
335 
336 /**
337  * struct st_lsm6dsx_sensor - ST IMU sensor instance
338  * @name: Sensor name.
339  * @id: Sensor identifier.
340  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
341  * @gain: Configured sensor sensitivity.
342  * @odr: Output data rate of the sensor [Hz].
343  * @watermark: Sensor watermark level.
344  * @decimator: Sensor decimation factor.
345  * @sip: Number of samples in a given pattern.
346  * @ts_ref: Sensor timestamp reference for hw one.
347  * @ext_info: Sensor settings if it is connected to i2c controller
348  */
349 struct 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  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
371  * @dev: Pointer to instance of struct device (I2C or SPI).
372  * @regmap: Register map of the device.
373  * @regulators: VDD/VDDIO voltage regulators.
374  * @irq: Device interrupt line (I2C or SPI).
375  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
376  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
377  * @page_lock: Mutex to prevent concurrent memory page configuration.
378  * @suspend_mask: Suspended sensor bitmask.
379  * @enable_mask: Enabled sensor bitmask.
380  * @fifo_mask: Enabled hw FIFO bitmask.
381  * @ts_gain: Hw timestamp rate after internal calibration.
382  * @ts_sip: Total number of timestamp samples in a given pattern.
383  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
384  * @buff: Device read buffer.
385  * @irq_routing: pointer to interrupt routing configuration.
386  * @event_threshold: wakeup event threshold.
387  * @enable_event: enabled event bitmask.
388  * @iio_devs: Pointers to acc/gyro iio_dev instances.
389  * @settings: Pointer to the specific sensor settings in use.
390  * @orientation: sensor chip orientation relative to main hardware.
391  * @scan: Temporary buffers used to align data before iio_push_to_buffers()
392  */
393 struct 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 	/* Ensure natural alignment of buffer elements */
422 	struct {
423 		__le16 channels[3];
424 		s64 ts __aligned(8);
425 	} scan[3];
426 };
427 
428 static __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 
435 static __maybe_unused const unsigned long st_lsm6dsx_available_scan_masks[] = {
436 	0x7, 0x0,
437 };
438 
439 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
440 
441 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
442 		     struct regmap *regmap);
443 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
444 				 bool enable);
445 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
446 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
447 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
448 				u16 watermark);
449 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
450 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
451 int st_lsm6dsx_resume_fifo(struct st_lsm6dsx_hw *hw);
452 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
453 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
454 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val);
455 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
456 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
457 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
458 
459 static inline int
st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw * hw,unsigned int addr,unsigned int mask,unsigned int val)460 st_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 
472 static inline int
st_lsm6dsx_read_locked(struct st_lsm6dsx_hw * hw,unsigned int addr,void * val,unsigned int len)473 st_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 
485 static inline int
st_lsm6dsx_write_locked(struct st_lsm6dsx_hw * hw,unsigned int addr,unsigned int val)486 st_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 
498 static inline const struct iio_mount_matrix *
st_lsm6dsx_get_mount_matrix(const struct iio_dev * iio_dev,const struct iio_chan_spec * chan)499 st_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 
508 static const
509 struct 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 /* ST_LSM6DSX_H */
515