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