1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2020 Invensense, Inc.
4  */
5 
6 #ifndef INV_ICM42600_H_
7 #define INV_ICM42600_H_
8 
9 #include <linux/bits.h>
10 #include <linux/bitfield.h>
11 #include <linux/regmap.h>
12 #include <linux/mutex.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/pm.h>
15 #include <linux/iio/iio.h>
16 
17 #include "inv_icm42600_buffer.h"
18 
19 enum inv_icm42600_chip {
20 	INV_CHIP_ICM42600,
21 	INV_CHIP_ICM42602,
22 	INV_CHIP_ICM42605,
23 	INV_CHIP_ICM42622,
24 	INV_CHIP_NB,
25 };
26 
27 /* serial bus slew rates */
28 enum inv_icm42600_slew_rate {
29 	INV_ICM42600_SLEW_RATE_20_60NS,
30 	INV_ICM42600_SLEW_RATE_12_36NS,
31 	INV_ICM42600_SLEW_RATE_6_18NS,
32 	INV_ICM42600_SLEW_RATE_4_12NS,
33 	INV_ICM42600_SLEW_RATE_2_6NS,
34 	INV_ICM42600_SLEW_RATE_INF_2NS,
35 };
36 
37 enum inv_icm42600_sensor_mode {
38 	INV_ICM42600_SENSOR_MODE_OFF,
39 	INV_ICM42600_SENSOR_MODE_STANDBY,
40 	INV_ICM42600_SENSOR_MODE_LOW_POWER,
41 	INV_ICM42600_SENSOR_MODE_LOW_NOISE,
42 	INV_ICM42600_SENSOR_MODE_NB,
43 };
44 
45 /* gyroscope fullscale values */
46 enum inv_icm42600_gyro_fs {
47 	INV_ICM42600_GYRO_FS_2000DPS,
48 	INV_ICM42600_GYRO_FS_1000DPS,
49 	INV_ICM42600_GYRO_FS_500DPS,
50 	INV_ICM42600_GYRO_FS_250DPS,
51 	INV_ICM42600_GYRO_FS_125DPS,
52 	INV_ICM42600_GYRO_FS_62_5DPS,
53 	INV_ICM42600_GYRO_FS_31_25DPS,
54 	INV_ICM42600_GYRO_FS_15_625DPS,
55 	INV_ICM42600_GYRO_FS_NB,
56 };
57 
58 /* accelerometer fullscale values */
59 enum inv_icm42600_accel_fs {
60 	INV_ICM42600_ACCEL_FS_16G,
61 	INV_ICM42600_ACCEL_FS_8G,
62 	INV_ICM42600_ACCEL_FS_4G,
63 	INV_ICM42600_ACCEL_FS_2G,
64 	INV_ICM42600_ACCEL_FS_NB,
65 };
66 
67 /* ODR suffixed by LN or LP are Low-Noise or Low-Power mode only */
68 enum inv_icm42600_odr {
69 	INV_ICM42600_ODR_8KHZ_LN = 3,
70 	INV_ICM42600_ODR_4KHZ_LN,
71 	INV_ICM42600_ODR_2KHZ_LN,
72 	INV_ICM42600_ODR_1KHZ_LN,
73 	INV_ICM42600_ODR_200HZ,
74 	INV_ICM42600_ODR_100HZ,
75 	INV_ICM42600_ODR_50HZ,
76 	INV_ICM42600_ODR_25HZ,
77 	INV_ICM42600_ODR_12_5HZ,
78 	INV_ICM42600_ODR_6_25HZ_LP,
79 	INV_ICM42600_ODR_3_125HZ_LP,
80 	INV_ICM42600_ODR_1_5625HZ_LP,
81 	INV_ICM42600_ODR_500HZ,
82 	INV_ICM42600_ODR_NB,
83 };
84 
85 enum inv_icm42600_filter {
86 	/* Low-Noise mode sensor data filter (3rd order filter by default) */
87 	INV_ICM42600_FILTER_BW_ODR_DIV_2,
88 
89 	/* Low-Power mode sensor data filter (averaging) */
90 	INV_ICM42600_FILTER_AVG_1X = 1,
91 	INV_ICM42600_FILTER_AVG_16X = 6,
92 };
93 
94 struct inv_icm42600_sensor_conf {
95 	int mode;
96 	int fs;
97 	int odr;
98 	int filter;
99 };
100 #define INV_ICM42600_SENSOR_CONF_INIT		{-1, -1, -1, -1}
101 
102 struct inv_icm42600_conf {
103 	struct inv_icm42600_sensor_conf gyro;
104 	struct inv_icm42600_sensor_conf accel;
105 	bool temp_en;
106 };
107 
108 struct inv_icm42600_suspended {
109 	enum inv_icm42600_sensor_mode gyro;
110 	enum inv_icm42600_sensor_mode accel;
111 	bool temp;
112 };
113 
114 /**
115  *  struct inv_icm42600_state - driver state variables
116  *  @lock:		lock for serializing multiple registers access.
117  *  @chip:		chip identifier.
118  *  @name:		chip name.
119  *  @map:		regmap pointer.
120  *  @vdd_supply:	VDD voltage regulator for the chip.
121  *  @vddio_supply:	I/O voltage regulator for the chip.
122  *  @orientation:	sensor chip orientation relative to main hardware.
123  *  @conf:		chip sensors configurations.
124  *  @suspended:		suspended sensors configuration.
125  *  @indio_gyro:	gyroscope IIO device.
126  *  @indio_accel:	accelerometer IIO device.
127  *  @buffer:		data transfer buffer aligned for DMA.
128  *  @fifo:		FIFO management structure.
129  */
130 struct inv_icm42600_state {
131 	struct mutex lock;
132 	enum inv_icm42600_chip chip;
133 	const char *name;
134 	struct regmap *map;
135 	struct regulator *vdd_supply;
136 	struct regulator *vddio_supply;
137 	struct iio_mount_matrix orientation;
138 	struct inv_icm42600_conf conf;
139 	struct inv_icm42600_suspended suspended;
140 	struct iio_dev *indio_gyro;
141 	struct iio_dev *indio_accel;
142 	uint8_t buffer[2] ____cacheline_aligned;
143 	struct inv_icm42600_fifo fifo;
144 };
145 
146 /* Virtual register addresses: @bank on MSB (4 upper bits), @address on LSB */
147 
148 /* Bank selection register, available in all banks */
149 #define INV_ICM42600_REG_BANK_SEL			0x76
150 #define INV_ICM42600_BANK_SEL_MASK			GENMASK(2, 0)
151 
152 /* User bank 0 (MSB 0x00) */
153 #define INV_ICM42600_REG_DEVICE_CONFIG			0x0011
154 #define INV_ICM42600_DEVICE_CONFIG_SOFT_RESET		BIT(0)
155 
156 #define INV_ICM42600_REG_DRIVE_CONFIG			0x0013
157 #define INV_ICM42600_DRIVE_CONFIG_I2C_MASK		GENMASK(5, 3)
158 #define INV_ICM42600_DRIVE_CONFIG_I2C(_rate)		\
159 		FIELD_PREP(INV_ICM42600_DRIVE_CONFIG_I2C_MASK, (_rate))
160 #define INV_ICM42600_DRIVE_CONFIG_SPI_MASK		GENMASK(2, 0)
161 #define INV_ICM42600_DRIVE_CONFIG_SPI(_rate)		\
162 		FIELD_PREP(INV_ICM42600_DRIVE_CONFIG_SPI_MASK, (_rate))
163 
164 #define INV_ICM42600_REG_INT_CONFIG			0x0014
165 #define INV_ICM42600_INT_CONFIG_INT2_LATCHED		BIT(5)
166 #define INV_ICM42600_INT_CONFIG_INT2_PUSH_PULL		BIT(4)
167 #define INV_ICM42600_INT_CONFIG_INT2_ACTIVE_HIGH	BIT(3)
168 #define INV_ICM42600_INT_CONFIG_INT2_ACTIVE_LOW		0x00
169 #define INV_ICM42600_INT_CONFIG_INT1_LATCHED		BIT(2)
170 #define INV_ICM42600_INT_CONFIG_INT1_PUSH_PULL		BIT(1)
171 #define INV_ICM42600_INT_CONFIG_INT1_ACTIVE_HIGH	BIT(0)
172 #define INV_ICM42600_INT_CONFIG_INT1_ACTIVE_LOW		0x00
173 
174 #define INV_ICM42600_REG_FIFO_CONFIG			0x0016
175 #define INV_ICM42600_FIFO_CONFIG_MASK			GENMASK(7, 6)
176 #define INV_ICM42600_FIFO_CONFIG_BYPASS			\
177 		FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 0)
178 #define INV_ICM42600_FIFO_CONFIG_STREAM			\
179 		FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 1)
180 #define INV_ICM42600_FIFO_CONFIG_STOP_ON_FULL		\
181 		FIELD_PREP(INV_ICM42600_FIFO_CONFIG_MASK, 2)
182 
183 /* all sensor data are 16 bits (2 registers wide) in big-endian */
184 #define INV_ICM42600_REG_TEMP_DATA			0x001D
185 #define INV_ICM42600_REG_ACCEL_DATA_X			0x001F
186 #define INV_ICM42600_REG_ACCEL_DATA_Y			0x0021
187 #define INV_ICM42600_REG_ACCEL_DATA_Z			0x0023
188 #define INV_ICM42600_REG_GYRO_DATA_X			0x0025
189 #define INV_ICM42600_REG_GYRO_DATA_Y			0x0027
190 #define INV_ICM42600_REG_GYRO_DATA_Z			0x0029
191 #define INV_ICM42600_DATA_INVALID			-32768
192 
193 #define INV_ICM42600_REG_INT_STATUS			0x002D
194 #define INV_ICM42600_INT_STATUS_UI_FSYNC		BIT(6)
195 #define INV_ICM42600_INT_STATUS_PLL_RDY			BIT(5)
196 #define INV_ICM42600_INT_STATUS_RESET_DONE		BIT(4)
197 #define INV_ICM42600_INT_STATUS_DATA_RDY		BIT(3)
198 #define INV_ICM42600_INT_STATUS_FIFO_THS		BIT(2)
199 #define INV_ICM42600_INT_STATUS_FIFO_FULL		BIT(1)
200 #define INV_ICM42600_INT_STATUS_AGC_RDY			BIT(0)
201 
202 /*
203  * FIFO access registers
204  * FIFO count is 16 bits (2 registers) big-endian
205  * FIFO data is a continuous read register to read FIFO content
206  */
207 #define INV_ICM42600_REG_FIFO_COUNT			0x002E
208 #define INV_ICM42600_REG_FIFO_DATA			0x0030
209 
210 #define INV_ICM42600_REG_SIGNAL_PATH_RESET		0x004B
211 #define INV_ICM42600_SIGNAL_PATH_RESET_DMP_INIT_EN	BIT(6)
212 #define INV_ICM42600_SIGNAL_PATH_RESET_DMP_MEM_RESET	BIT(5)
213 #define INV_ICM42600_SIGNAL_PATH_RESET_RESET		BIT(3)
214 #define INV_ICM42600_SIGNAL_PATH_RESET_TMST_STROBE	BIT(2)
215 #define INV_ICM42600_SIGNAL_PATH_RESET_FIFO_FLUSH	BIT(1)
216 
217 /* default configuration: all data big-endian and fifo count in bytes */
218 #define INV_ICM42600_REG_INTF_CONFIG0			0x004C
219 #define INV_ICM42600_INTF_CONFIG0_FIFO_HOLD_LAST_DATA	BIT(7)
220 #define INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_REC	BIT(6)
221 #define INV_ICM42600_INTF_CONFIG0_FIFO_COUNT_ENDIAN	BIT(5)
222 #define INV_ICM42600_INTF_CONFIG0_SENSOR_DATA_ENDIAN	BIT(4)
223 #define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK	GENMASK(1, 0)
224 #define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_SPI_DIS	\
225 		FIELD_PREP(INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK, 2)
226 #define INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_I2C_DIS	\
227 		FIELD_PREP(INV_ICM42600_INTF_CONFIG0_UI_SIFS_CFG_MASK, 3)
228 
229 #define INV_ICM42600_REG_INTF_CONFIG1			0x004D
230 #define INV_ICM42600_INTF_CONFIG1_ACCEL_LP_CLK_RC	BIT(3)
231 
232 #define INV_ICM42600_REG_PWR_MGMT0			0x004E
233 #define INV_ICM42600_PWR_MGMT0_TEMP_DIS			BIT(5)
234 #define INV_ICM42600_PWR_MGMT0_IDLE			BIT(4)
235 #define INV_ICM42600_PWR_MGMT0_GYRO(_mode)		\
236 		FIELD_PREP(GENMASK(3, 2), (_mode))
237 #define INV_ICM42600_PWR_MGMT0_ACCEL(_mode)		\
238 		FIELD_PREP(GENMASK(1, 0), (_mode))
239 
240 #define INV_ICM42600_REG_GYRO_CONFIG0			0x004F
241 #define INV_ICM42600_GYRO_CONFIG0_FS(_fs)		\
242 		FIELD_PREP(GENMASK(7, 5), (_fs))
243 #define INV_ICM42600_GYRO_CONFIG0_ODR(_odr)		\
244 		FIELD_PREP(GENMASK(3, 0), (_odr))
245 
246 #define INV_ICM42600_REG_ACCEL_CONFIG0			0x0050
247 #define INV_ICM42600_ACCEL_CONFIG0_FS(_fs)		\
248 		FIELD_PREP(GENMASK(7, 5), (_fs))
249 #define INV_ICM42600_ACCEL_CONFIG0_ODR(_odr)		\
250 		FIELD_PREP(GENMASK(3, 0), (_odr))
251 
252 #define INV_ICM42600_REG_GYRO_ACCEL_CONFIG0		0x0052
253 #define INV_ICM42600_GYRO_ACCEL_CONFIG0_ACCEL_FILT(_f)	\
254 		FIELD_PREP(GENMASK(7, 4), (_f))
255 #define INV_ICM42600_GYRO_ACCEL_CONFIG0_GYRO_FILT(_f)	\
256 		FIELD_PREP(GENMASK(3, 0), (_f))
257 
258 #define INV_ICM42600_REG_TMST_CONFIG			0x0054
259 #define INV_ICM42600_TMST_CONFIG_MASK			GENMASK(4, 0)
260 #define INV_ICM42600_TMST_CONFIG_TMST_TO_REGS_EN	BIT(4)
261 #define INV_ICM42600_TMST_CONFIG_TMST_RES_16US		BIT(3)
262 #define INV_ICM42600_TMST_CONFIG_TMST_DELTA_EN		BIT(2)
263 #define INV_ICM42600_TMST_CONFIG_TMST_FSYNC_EN		BIT(1)
264 #define INV_ICM42600_TMST_CONFIG_TMST_EN		BIT(0)
265 
266 #define INV_ICM42600_REG_FIFO_CONFIG1			0x005F
267 #define INV_ICM42600_FIFO_CONFIG1_RESUME_PARTIAL_RD	BIT(6)
268 #define INV_ICM42600_FIFO_CONFIG1_WM_GT_TH		BIT(5)
269 #define INV_ICM42600_FIFO_CONFIG1_TMST_FSYNC_EN		BIT(3)
270 #define INV_ICM42600_FIFO_CONFIG1_TEMP_EN		BIT(2)
271 #define INV_ICM42600_FIFO_CONFIG1_GYRO_EN		BIT(1)
272 #define INV_ICM42600_FIFO_CONFIG1_ACCEL_EN		BIT(0)
273 
274 /* FIFO watermark is 16 bits (2 registers wide) in little-endian */
275 #define INV_ICM42600_REG_FIFO_WATERMARK			0x0060
276 #define INV_ICM42600_FIFO_WATERMARK_VAL(_wm)		\
277 		cpu_to_le16((_wm) & GENMASK(11, 0))
278 /* FIFO is 2048 bytes, let 12 samples for reading latency */
279 #define INV_ICM42600_FIFO_WATERMARK_MAX			(2048 - 12 * 16)
280 
281 #define INV_ICM42600_REG_INT_CONFIG1			0x0064
282 #define INV_ICM42600_INT_CONFIG1_TPULSE_DURATION	BIT(6)
283 #define INV_ICM42600_INT_CONFIG1_TDEASSERT_DISABLE	BIT(5)
284 #define INV_ICM42600_INT_CONFIG1_ASYNC_RESET		BIT(4)
285 
286 #define INV_ICM42600_REG_INT_SOURCE0			0x0065
287 #define INV_ICM42600_INT_SOURCE0_UI_FSYNC_INT1_EN	BIT(6)
288 #define INV_ICM42600_INT_SOURCE0_PLL_RDY_INT1_EN	BIT(5)
289 #define INV_ICM42600_INT_SOURCE0_RESET_DONE_INT1_EN	BIT(4)
290 #define INV_ICM42600_INT_SOURCE0_UI_DRDY_INT1_EN	BIT(3)
291 #define INV_ICM42600_INT_SOURCE0_FIFO_THS_INT1_EN	BIT(2)
292 #define INV_ICM42600_INT_SOURCE0_FIFO_FULL_INT1_EN	BIT(1)
293 #define INV_ICM42600_INT_SOURCE0_UI_AGC_RDY_INT1_EN	BIT(0)
294 
295 #define INV_ICM42600_REG_WHOAMI				0x0075
296 #define INV_ICM42600_WHOAMI_ICM42600			0x40
297 #define INV_ICM42600_WHOAMI_ICM42602			0x41
298 #define INV_ICM42600_WHOAMI_ICM42605			0x42
299 #define INV_ICM42600_WHOAMI_ICM42622			0x46
300 
301 /* User bank 1 (MSB 0x10) */
302 #define INV_ICM42600_REG_SENSOR_CONFIG0			0x1003
303 #define INV_ICM42600_SENSOR_CONFIG0_ZG_DISABLE		BIT(5)
304 #define INV_ICM42600_SENSOR_CONFIG0_YG_DISABLE		BIT(4)
305 #define INV_ICM42600_SENSOR_CONFIG0_XG_DISABLE		BIT(3)
306 #define INV_ICM42600_SENSOR_CONFIG0_ZA_DISABLE		BIT(2)
307 #define INV_ICM42600_SENSOR_CONFIG0_YA_DISABLE		BIT(1)
308 #define INV_ICM42600_SENSOR_CONFIG0_XA_DISABLE		BIT(0)
309 
310 /* Timestamp value is 20 bits (3 registers) in little-endian */
311 #define INV_ICM42600_REG_TMSTVAL			0x1062
312 #define INV_ICM42600_TMSTVAL_MASK			GENMASK(19, 0)
313 
314 #define INV_ICM42600_REG_INTF_CONFIG4			0x107A
315 #define INV_ICM42600_INTF_CONFIG4_I3C_BUS_ONLY		BIT(6)
316 #define INV_ICM42600_INTF_CONFIG4_SPI_AP_4WIRE		BIT(1)
317 
318 #define INV_ICM42600_REG_INTF_CONFIG6			0x107C
319 #define INV_ICM42600_INTF_CONFIG6_MASK			GENMASK(4, 0)
320 #define INV_ICM42600_INTF_CONFIG6_I3C_EN		BIT(4)
321 #define INV_ICM42600_INTF_CONFIG6_I3C_IBI_BYTE_EN	BIT(3)
322 #define INV_ICM42600_INTF_CONFIG6_I3C_IBI_EN		BIT(2)
323 #define INV_ICM42600_INTF_CONFIG6_I3C_DDR_EN		BIT(1)
324 #define INV_ICM42600_INTF_CONFIG6_I3C_SDR_EN		BIT(0)
325 
326 /* User bank 4 (MSB 0x40) */
327 #define INV_ICM42600_REG_INT_SOURCE8			0x404F
328 #define INV_ICM42600_INT_SOURCE8_FSYNC_IBI_EN		BIT(5)
329 #define INV_ICM42600_INT_SOURCE8_PLL_RDY_IBI_EN		BIT(4)
330 #define INV_ICM42600_INT_SOURCE8_UI_DRDY_IBI_EN		BIT(3)
331 #define INV_ICM42600_INT_SOURCE8_FIFO_THS_IBI_EN	BIT(2)
332 #define INV_ICM42600_INT_SOURCE8_FIFO_FULL_IBI_EN	BIT(1)
333 #define INV_ICM42600_INT_SOURCE8_AGC_RDY_IBI_EN		BIT(0)
334 
335 #define INV_ICM42600_REG_OFFSET_USER0			0x4077
336 #define INV_ICM42600_REG_OFFSET_USER1			0x4078
337 #define INV_ICM42600_REG_OFFSET_USER2			0x4079
338 #define INV_ICM42600_REG_OFFSET_USER3			0x407A
339 #define INV_ICM42600_REG_OFFSET_USER4			0x407B
340 #define INV_ICM42600_REG_OFFSET_USER5			0x407C
341 #define INV_ICM42600_REG_OFFSET_USER6			0x407D
342 #define INV_ICM42600_REG_OFFSET_USER7			0x407E
343 #define INV_ICM42600_REG_OFFSET_USER8			0x407F
344 
345 /* Sleep times required by the driver */
346 #define INV_ICM42600_POWER_UP_TIME_MS		100
347 #define INV_ICM42600_RESET_TIME_MS		1
348 #define INV_ICM42600_ACCEL_STARTUP_TIME_MS	20
349 #define INV_ICM42600_GYRO_STARTUP_TIME_MS	60
350 #define INV_ICM42600_GYRO_STOP_TIME_MS		150
351 #define INV_ICM42600_TEMP_STARTUP_TIME_MS	14
352 #define INV_ICM42600_SUSPEND_DELAY_MS		2000
353 
354 typedef int (*inv_icm42600_bus_setup)(struct inv_icm42600_state *);
355 
356 extern const struct regmap_config inv_icm42600_regmap_config;
357 extern const struct dev_pm_ops inv_icm42600_pm_ops;
358 
359 const struct iio_mount_matrix *
360 inv_icm42600_get_mount_matrix(const struct iio_dev *indio_dev,
361 			      const struct iio_chan_spec *chan);
362 
363 uint32_t inv_icm42600_odr_to_period(enum inv_icm42600_odr odr);
364 
365 int inv_icm42600_set_accel_conf(struct inv_icm42600_state *st,
366 				struct inv_icm42600_sensor_conf *conf,
367 				unsigned int *sleep_ms);
368 
369 int inv_icm42600_set_gyro_conf(struct inv_icm42600_state *st,
370 			       struct inv_icm42600_sensor_conf *conf,
371 			       unsigned int *sleep_ms);
372 
373 int inv_icm42600_set_temp_conf(struct inv_icm42600_state *st, bool enable,
374 			       unsigned int *sleep_ms);
375 
376 int inv_icm42600_debugfs_reg(struct iio_dev *indio_dev, unsigned int reg,
377 			     unsigned int writeval, unsigned int *readval);
378 
379 int inv_icm42600_core_probe(struct regmap *regmap, int chip, int irq,
380 			    inv_icm42600_bus_setup bus_setup);
381 
382 struct iio_dev *inv_icm42600_gyro_init(struct inv_icm42600_state *st);
383 
384 int inv_icm42600_gyro_parse_fifo(struct iio_dev *indio_dev);
385 
386 struct iio_dev *inv_icm42600_accel_init(struct inv_icm42600_state *st);
387 
388 int inv_icm42600_accel_parse_fifo(struct iio_dev *indio_dev);
389 
390 #endif
391