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