xref: /linux/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h (revision 52338415)
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 
16 #define ST_LSM6DS3_DEV_NAME	"lsm6ds3"
17 #define ST_LSM6DS3H_DEV_NAME	"lsm6ds3h"
18 #define ST_LSM6DSL_DEV_NAME	"lsm6dsl"
19 #define ST_LSM6DSM_DEV_NAME	"lsm6dsm"
20 #define ST_ISM330DLC_DEV_NAME	"ism330dlc"
21 #define ST_LSM6DSO_DEV_NAME	"lsm6dso"
22 #define ST_ASM330LHH_DEV_NAME	"asm330lhh"
23 #define ST_LSM6DSOX_DEV_NAME	"lsm6dsox"
24 #define ST_LSM6DSR_DEV_NAME	"lsm6dsr"
25 #define ST_LSM6DS3TRC_DEV_NAME	"lsm6ds3tr-c"
26 #define ST_ISM330DHCX_DEV_NAME	"ism330dhcx"
27 #define ST_LSM9DS1_DEV_NAME	"lsm9ds1-imu"
28 
29 enum st_lsm6dsx_hw_id {
30 	ST_LSM6DS3_ID,
31 	ST_LSM6DS3H_ID,
32 	ST_LSM6DSL_ID,
33 	ST_LSM6DSM_ID,
34 	ST_ISM330DLC_ID,
35 	ST_LSM6DSO_ID,
36 	ST_ASM330LHH_ID,
37 	ST_LSM6DSOX_ID,
38 	ST_LSM6DSR_ID,
39 	ST_LSM6DS3TRC_ID,
40 	ST_ISM330DHCX_ID,
41 	ST_LSM9DS1_ID,
42 	ST_LSM6DSX_MAX_ID,
43 };
44 
45 #define ST_LSM6DSX_BUFF_SIZE		512
46 #define ST_LSM6DSX_CHAN_SIZE		2
47 #define ST_LSM6DSX_SAMPLE_SIZE		6
48 #define ST_LSM6DSX_TAG_SIZE		1
49 #define ST_LSM6DSX_TAGGED_SAMPLE_SIZE	(ST_LSM6DSX_SAMPLE_SIZE + \
50 					 ST_LSM6DSX_TAG_SIZE)
51 #define ST_LSM6DSX_MAX_WORD_LEN		((32 / ST_LSM6DSX_SAMPLE_SIZE) * \
52 					 ST_LSM6DSX_SAMPLE_SIZE)
53 #define ST_LSM6DSX_MAX_TAGGED_WORD_LEN	((32 / ST_LSM6DSX_TAGGED_SAMPLE_SIZE) \
54 					 * ST_LSM6DSX_TAGGED_SAMPLE_SIZE)
55 #define ST_LSM6DSX_SHIFT_VAL(val, mask)	(((val) << __ffs(mask)) & (mask))
56 
57 #define ST_LSM6DSX_CHANNEL(chan_type, addr, mod, scan_idx)		\
58 {									\
59 	.type = chan_type,						\
60 	.address = addr,						\
61 	.modified = 1,							\
62 	.channel2 = mod,						\
63 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
64 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
65 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
66 	.scan_index = scan_idx,						\
67 	.scan_type = {							\
68 		.sign = 's',						\
69 		.realbits = 16,						\
70 		.storagebits = 16,					\
71 		.endianness = IIO_LE,					\
72 	},								\
73 }
74 
75 struct st_lsm6dsx_reg {
76 	u8 addr;
77 	u8 mask;
78 };
79 
80 struct st_lsm6dsx_sensor;
81 struct st_lsm6dsx_hw;
82 
83 struct st_lsm6dsx_odr {
84 	u16 hz;
85 	u8 val;
86 };
87 
88 #define ST_LSM6DSX_ODR_LIST_SIZE	6
89 struct st_lsm6dsx_odr_table_entry {
90 	struct st_lsm6dsx_reg reg;
91 	struct st_lsm6dsx_odr odr_avl[ST_LSM6DSX_ODR_LIST_SIZE];
92 };
93 
94 struct st_lsm6dsx_fs {
95 	u32 gain;
96 	u8 val;
97 };
98 
99 #define ST_LSM6DSX_FS_LIST_SIZE		4
100 struct st_lsm6dsx_fs_table_entry {
101 	struct st_lsm6dsx_reg reg;
102 
103 	struct st_lsm6dsx_fs fs_avl[ST_LSM6DSX_FS_LIST_SIZE];
104 	int fs_len;
105 };
106 
107 /**
108  * struct st_lsm6dsx_fifo_ops - ST IMU FIFO settings
109  * @update_fifo: Update FIFO configuration callback.
110  * @read_fifo: Read FIFO callback.
111  * @fifo_th: FIFO threshold register info (addr + mask).
112  * @fifo_diff: FIFO diff status register info (addr + mask).
113  * @th_wl: FIFO threshold word length.
114  */
115 struct st_lsm6dsx_fifo_ops {
116 	int (*update_fifo)(struct st_lsm6dsx_sensor *sensor, bool enable);
117 	int (*read_fifo)(struct st_lsm6dsx_hw *hw);
118 	struct {
119 		u8 addr;
120 		u16 mask;
121 	} fifo_th;
122 	struct {
123 		u8 addr;
124 		u16 mask;
125 	} fifo_diff;
126 	u8 th_wl;
127 };
128 
129 /**
130  * struct st_lsm6dsx_hw_ts_settings - ST IMU hw timer settings
131  * @timer_en: Hw timer enable register info (addr + mask).
132  * @hr_timer: Hw timer resolution register info (addr + mask).
133  * @fifo_en: Hw timer FIFO enable register info (addr + mask).
134  * @decimator: Hw timer FIFO decimator register info (addr + mask).
135  */
136 struct st_lsm6dsx_hw_ts_settings {
137 	struct st_lsm6dsx_reg timer_en;
138 	struct st_lsm6dsx_reg hr_timer;
139 	struct st_lsm6dsx_reg fifo_en;
140 	struct st_lsm6dsx_reg decimator;
141 };
142 
143 /**
144  * struct st_lsm6dsx_shub_settings - ST IMU hw i2c controller settings
145  * @page_mux: register page mux info (addr + mask).
146  * @master_en: master config register info (addr + mask).
147  * @pullup_en: i2c controller pull-up register info (addr + mask).
148  * @aux_sens: aux sensor register info (addr + mask).
149  * @wr_once: write_once register info (addr + mask).
150  * @shub_out: sensor hub first output register info.
151  * @slv0_addr: slave0 address in secondary page.
152  * @dw_slv0_addr: slave0 write register address in secondary page.
153  * @batch_en: Enable/disable FIFO batching.
154  */
155 struct st_lsm6dsx_shub_settings {
156 	struct st_lsm6dsx_reg page_mux;
157 	struct st_lsm6dsx_reg master_en;
158 	struct st_lsm6dsx_reg pullup_en;
159 	struct st_lsm6dsx_reg aux_sens;
160 	struct st_lsm6dsx_reg wr_once;
161 	u8 shub_out;
162 	u8 slv0_addr;
163 	u8 dw_slv0_addr;
164 	u8 batch_en;
165 };
166 
167 enum st_lsm6dsx_ext_sensor_id {
168 	ST_LSM6DSX_ID_MAGN,
169 };
170 
171 /**
172  * struct st_lsm6dsx_ext_dev_settings - i2c controller slave settings
173  * @i2c_addr: I2c slave address list.
174  * @wai: Wai address info.
175  * @id: external sensor id.
176  * @odr: Output data rate of the sensor [Hz].
177  * @gain: Configured sensor sensitivity.
178  * @temp_comp: Temperature compensation register info (addr + mask).
179  * @pwr_table: Power on register info (addr + mask).
180  * @off_canc: Offset cancellation register info (addr + mask).
181  * @bdu: Block data update register info (addr + mask).
182  * @out: Output register info.
183  */
184 struct st_lsm6dsx_ext_dev_settings {
185 	u8 i2c_addr[2];
186 	struct {
187 		u8 addr;
188 		u8 val;
189 	} wai;
190 	enum st_lsm6dsx_ext_sensor_id id;
191 	struct st_lsm6dsx_odr_table_entry odr_table;
192 	struct st_lsm6dsx_fs_table_entry fs_table;
193 	struct st_lsm6dsx_reg temp_comp;
194 	struct {
195 		struct st_lsm6dsx_reg reg;
196 		u8 off_val;
197 		u8 on_val;
198 	} pwr_table;
199 	struct st_lsm6dsx_reg off_canc;
200 	struct st_lsm6dsx_reg bdu;
201 	struct {
202 		u8 addr;
203 		u8 len;
204 	} out;
205 };
206 
207 /**
208  * struct st_lsm6dsx_settings - ST IMU sensor settings
209  * @wai: Sensor WhoAmI default value.
210  * @int1_addr: Control Register address for INT1
211  * @int2_addr: Control Register address for INT2
212  * @reset_addr: register address for reset/reboot
213  * @max_fifo_size: Sensor max fifo length in FIFO words.
214  * @id: List of hw id/device name supported by the driver configuration.
215  * @channels: IIO channels supported by the device.
216  * @odr_table: Hw sensors odr table (Hz + val).
217  * @fs_table: Hw sensors gain table (gain + val).
218  * @decimator: List of decimator register info (addr + mask).
219  * @batch: List of FIFO batching register info (addr + mask).
220  * @fifo_ops: Sensor hw FIFO parameters.
221  * @ts_settings: Hw timer related settings.
222  * @shub_settings: i2c controller related settings.
223  */
224 struct st_lsm6dsx_settings {
225 	u8 wai;
226 	u8 int1_addr;
227 	u8 int2_addr;
228 	u8 reset_addr;
229 	u16 max_fifo_size;
230 	struct {
231 		enum st_lsm6dsx_hw_id hw_id;
232 		const char *name;
233 	} id[ST_LSM6DSX_MAX_ID];
234 	struct {
235 		const struct iio_chan_spec *chan;
236 		int len;
237 	} channels[2];
238 	struct st_lsm6dsx_odr_table_entry odr_table[2];
239 	struct st_lsm6dsx_fs_table_entry fs_table[2];
240 	struct st_lsm6dsx_reg decimator[ST_LSM6DSX_MAX_ID];
241 	struct st_lsm6dsx_reg batch[ST_LSM6DSX_MAX_ID];
242 	struct st_lsm6dsx_fifo_ops fifo_ops;
243 	struct st_lsm6dsx_hw_ts_settings ts_settings;
244 	struct st_lsm6dsx_shub_settings shub_settings;
245 };
246 
247 enum st_lsm6dsx_sensor_id {
248 	ST_LSM6DSX_ID_GYRO,
249 	ST_LSM6DSX_ID_ACC,
250 	ST_LSM6DSX_ID_EXT0,
251 	ST_LSM6DSX_ID_EXT1,
252 	ST_LSM6DSX_ID_EXT2,
253 	ST_LSM6DSX_ID_MAX,
254 };
255 
256 enum st_lsm6dsx_fifo_mode {
257 	ST_LSM6DSX_FIFO_BYPASS = 0x0,
258 	ST_LSM6DSX_FIFO_CONT = 0x6,
259 };
260 
261 /**
262  * struct st_lsm6dsx_sensor - ST IMU sensor instance
263  * @name: Sensor name.
264  * @id: Sensor identifier.
265  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
266  * @gain: Configured sensor sensitivity.
267  * @odr: Output data rate of the sensor [Hz].
268  * @watermark: Sensor watermark level.
269  * @sip: Number of samples in a given pattern.
270  * @decimator: FIFO decimation factor.
271  * @ts_ref: Sensor timestamp reference for hw one.
272  * @ext_info: Sensor settings if it is connected to i2c controller
273  */
274 struct st_lsm6dsx_sensor {
275 	char name[32];
276 	enum st_lsm6dsx_sensor_id id;
277 	struct st_lsm6dsx_hw *hw;
278 
279 	u32 gain;
280 	u16 odr;
281 
282 	u16 watermark;
283 	u8 sip;
284 	u8 decimator;
285 	s64 ts_ref;
286 
287 	struct {
288 		const struct st_lsm6dsx_ext_dev_settings *settings;
289 		u8 addr;
290 	} ext_info;
291 };
292 
293 /**
294  * struct st_lsm6dsx_hw - ST IMU MEMS hw instance
295  * @dev: Pointer to instance of struct device (I2C or SPI).
296  * @regmap: Register map of the device.
297  * @irq: Device interrupt line (I2C or SPI).
298  * @fifo_lock: Mutex to prevent concurrent access to the hw FIFO.
299  * @conf_lock: Mutex to prevent concurrent FIFO configuration update.
300  * @page_lock: Mutex to prevent concurrent memory page configuration.
301  * @fifo_mode: FIFO operating mode supported by the device.
302  * @suspend_mask: Suspended sensor bitmask.
303  * @enable_mask: Enabled sensor bitmask.
304  * @ts_sip: Total number of timestamp samples in a given pattern.
305  * @sip: Total number of samples (acc/gyro/ts) in a given pattern.
306  * @buff: Device read buffer.
307  * @iio_devs: Pointers to acc/gyro iio_dev instances.
308  * @settings: Pointer to the specific sensor settings in use.
309  */
310 struct st_lsm6dsx_hw {
311 	struct device *dev;
312 	struct regmap *regmap;
313 	int irq;
314 
315 	struct mutex fifo_lock;
316 	struct mutex conf_lock;
317 	struct mutex page_lock;
318 
319 	enum st_lsm6dsx_fifo_mode fifo_mode;
320 	u8 suspend_mask;
321 	u8 enable_mask;
322 	u8 ts_sip;
323 	u8 sip;
324 
325 	u8 *buff;
326 
327 	struct iio_dev *iio_devs[ST_LSM6DSX_ID_MAX];
328 
329 	const struct st_lsm6dsx_settings *settings;
330 };
331 
332 static const unsigned long st_lsm6dsx_available_scan_masks[] = {0x7, 0x0};
333 extern const struct dev_pm_ops st_lsm6dsx_pm_ops;
334 
335 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
336 		     struct regmap *regmap);
337 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
338 				 bool enable);
339 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw);
340 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val);
341 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor,
342 				u16 watermark);
343 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable);
344 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw);
345 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
346 			     enum st_lsm6dsx_fifo_mode fifo_mode);
347 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw);
348 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw);
349 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val);
350 int st_lsm6dsx_shub_probe(struct st_lsm6dsx_hw *hw, const char *name);
351 int st_lsm6dsx_shub_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable);
352 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable);
353 
354 static inline int
355 st_lsm6dsx_update_bits_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
356 			      unsigned int mask, unsigned int val)
357 {
358 	int err;
359 
360 	mutex_lock(&hw->page_lock);
361 	err = regmap_update_bits(hw->regmap, addr, mask, val);
362 	mutex_unlock(&hw->page_lock);
363 
364 	return err;
365 }
366 
367 static inline int
368 st_lsm6dsx_read_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
369 		       void *val, unsigned int len)
370 {
371 	int err;
372 
373 	mutex_lock(&hw->page_lock);
374 	err = regmap_bulk_read(hw->regmap, addr, val, len);
375 	mutex_unlock(&hw->page_lock);
376 
377 	return err;
378 }
379 
380 static inline int
381 st_lsm6dsx_write_locked(struct st_lsm6dsx_hw *hw, unsigned int addr,
382 			unsigned int val)
383 {
384 	int err;
385 
386 	mutex_lock(&hw->page_lock);
387 	err = regmap_write(hw->regmap, addr, val);
388 	mutex_unlock(&hw->page_lock);
389 
390 	return err;
391 }
392 
393 #endif /* ST_LSM6DSX_H */
394