xref: /openbmc/linux/drivers/iio/imu/adis16400.c (revision 0edabdfe89581669609eaac5f6a8d0ae6fe95e7f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * adis16400.c	support Analog Devices ADIS16400/5
4  *		3d 2g Linear Accelerometers,
5  *		3d Gyroscopes,
6  *		3d Magnetometers via SPI
7  *
8  * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
9  * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
10  * Copyright (c) 2011 Analog Devices Inc.
11  */
12 
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/delay.h>
16 #include <linux/mutex.h>
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/spi/spi.h>
20 #include <linux/slab.h>
21 #include <linux/sysfs.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/debugfs.h>
25 #include <linux/bitops.h>
26 
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/imu/adis.h>
32 
33 #define ADIS16400_STARTUP_DELAY	290 /* ms */
34 #define ADIS16400_MTEST_DELAY 90 /* ms */
35 
36 #define ADIS16400_FLASH_CNT  0x00 /* Flash memory write count */
37 #define ADIS16400_SUPPLY_OUT 0x02 /* Power supply measurement */
38 #define ADIS16400_XGYRO_OUT 0x04 /* X-axis gyroscope output */
39 #define ADIS16400_YGYRO_OUT 0x06 /* Y-axis gyroscope output */
40 #define ADIS16400_ZGYRO_OUT 0x08 /* Z-axis gyroscope output */
41 #define ADIS16400_XACCL_OUT 0x0A /* X-axis accelerometer output */
42 #define ADIS16400_YACCL_OUT 0x0C /* Y-axis accelerometer output */
43 #define ADIS16400_ZACCL_OUT 0x0E /* Z-axis accelerometer output */
44 #define ADIS16400_XMAGN_OUT 0x10 /* X-axis magnetometer measurement */
45 #define ADIS16400_YMAGN_OUT 0x12 /* Y-axis magnetometer measurement */
46 #define ADIS16400_ZMAGN_OUT 0x14 /* Z-axis magnetometer measurement */
47 #define ADIS16400_TEMP_OUT  0x16 /* Temperature output */
48 #define ADIS16400_AUX_ADC   0x18 /* Auxiliary ADC measurement */
49 
50 #define ADIS16350_XTEMP_OUT 0x10 /* X-axis gyroscope temperature measurement */
51 #define ADIS16350_YTEMP_OUT 0x12 /* Y-axis gyroscope temperature measurement */
52 #define ADIS16350_ZTEMP_OUT 0x14 /* Z-axis gyroscope temperature measurement */
53 
54 #define ADIS16300_PITCH_OUT 0x12 /* X axis inclinometer output measurement */
55 #define ADIS16300_ROLL_OUT  0x14 /* Y axis inclinometer output measurement */
56 #define ADIS16300_AUX_ADC   0x16 /* Auxiliary ADC measurement */
57 
58 #define ADIS16448_BARO_OUT	0x16 /* Barometric pressure output */
59 #define ADIS16448_TEMP_OUT  0x18 /* Temperature output */
60 
61 /* Calibration parameters */
62 #define ADIS16400_XGYRO_OFF 0x1A /* X-axis gyroscope bias offset factor */
63 #define ADIS16400_YGYRO_OFF 0x1C /* Y-axis gyroscope bias offset factor */
64 #define ADIS16400_ZGYRO_OFF 0x1E /* Z-axis gyroscope bias offset factor */
65 #define ADIS16400_XACCL_OFF 0x20 /* X-axis acceleration bias offset factor */
66 #define ADIS16400_YACCL_OFF 0x22 /* Y-axis acceleration bias offset factor */
67 #define ADIS16400_ZACCL_OFF 0x24 /* Z-axis acceleration bias offset factor */
68 #define ADIS16400_XMAGN_HIF 0x26 /* X-axis magnetometer, hard-iron factor */
69 #define ADIS16400_YMAGN_HIF 0x28 /* Y-axis magnetometer, hard-iron factor */
70 #define ADIS16400_ZMAGN_HIF 0x2A /* Z-axis magnetometer, hard-iron factor */
71 #define ADIS16400_XMAGN_SIF 0x2C /* X-axis magnetometer, soft-iron factor */
72 #define ADIS16400_YMAGN_SIF 0x2E /* Y-axis magnetometer, soft-iron factor */
73 #define ADIS16400_ZMAGN_SIF 0x30 /* Z-axis magnetometer, soft-iron factor */
74 
75 #define ADIS16400_GPIO_CTRL 0x32 /* Auxiliary digital input/output control */
76 #define ADIS16400_MSC_CTRL  0x34 /* Miscellaneous control */
77 #define ADIS16400_SMPL_PRD  0x36 /* Internal sample period (rate) control */
78 #define ADIS16400_SENS_AVG  0x38 /* Dynamic range and digital filter control */
79 #define ADIS16400_SLP_CNT   0x3A /* Sleep mode control */
80 #define ADIS16400_DIAG_STAT 0x3C /* System status */
81 
82 /* Alarm functions */
83 #define ADIS16400_GLOB_CMD  0x3E /* System command */
84 #define ADIS16400_ALM_MAG1  0x40 /* Alarm 1 amplitude threshold */
85 #define ADIS16400_ALM_MAG2  0x42 /* Alarm 2 amplitude threshold */
86 #define ADIS16400_ALM_SMPL1 0x44 /* Alarm 1 sample size */
87 #define ADIS16400_ALM_SMPL2 0x46 /* Alarm 2 sample size */
88 #define ADIS16400_ALM_CTRL  0x48 /* Alarm control */
89 #define ADIS16400_AUX_DAC   0x4A /* Auxiliary DAC data */
90 
91 #define ADIS16334_LOT_ID1   0x52 /* Lot identification code 1 */
92 #define ADIS16334_LOT_ID2   0x54 /* Lot identification code 2 */
93 #define ADIS16400_PRODUCT_ID 0x56 /* Product identifier */
94 #define ADIS16334_SERIAL_NUMBER 0x58 /* Serial number, lot specific */
95 
96 #define ADIS16400_ERROR_ACTIVE			(1<<14)
97 #define ADIS16400_NEW_DATA			(1<<14)
98 
99 /* MSC_CTRL */
100 #define ADIS16400_MSC_CTRL_MEM_TEST		(1<<11)
101 #define ADIS16400_MSC_CTRL_INT_SELF_TEST	(1<<10)
102 #define ADIS16400_MSC_CTRL_NEG_SELF_TEST	(1<<9)
103 #define ADIS16400_MSC_CTRL_POS_SELF_TEST	(1<<8)
104 #define ADIS16400_MSC_CTRL_GYRO_BIAS		(1<<7)
105 #define ADIS16400_MSC_CTRL_ACCL_ALIGN		(1<<6)
106 #define ADIS16400_MSC_CTRL_DATA_RDY_EN		(1<<2)
107 #define ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH	(1<<1)
108 #define ADIS16400_MSC_CTRL_DATA_RDY_DIO2	(1<<0)
109 
110 /* SMPL_PRD */
111 #define ADIS16400_SMPL_PRD_TIME_BASE	(1<<7)
112 #define ADIS16400_SMPL_PRD_DIV_MASK	0x7F
113 
114 /* DIAG_STAT */
115 #define ADIS16400_DIAG_STAT_ZACCL_FAIL	15
116 #define ADIS16400_DIAG_STAT_YACCL_FAIL	14
117 #define ADIS16400_DIAG_STAT_XACCL_FAIL	13
118 #define ADIS16400_DIAG_STAT_XGYRO_FAIL	12
119 #define ADIS16400_DIAG_STAT_YGYRO_FAIL	11
120 #define ADIS16400_DIAG_STAT_ZGYRO_FAIL	10
121 #define ADIS16400_DIAG_STAT_ALARM2	9
122 #define ADIS16400_DIAG_STAT_ALARM1	8
123 #define ADIS16400_DIAG_STAT_FLASH_CHK	6
124 #define ADIS16400_DIAG_STAT_SELF_TEST	5
125 #define ADIS16400_DIAG_STAT_OVERFLOW	4
126 #define ADIS16400_DIAG_STAT_SPI_FAIL	3
127 #define ADIS16400_DIAG_STAT_FLASH_UPT	2
128 #define ADIS16400_DIAG_STAT_POWER_HIGH	1
129 #define ADIS16400_DIAG_STAT_POWER_LOW	0
130 
131 /* GLOB_CMD */
132 #define ADIS16400_GLOB_CMD_SW_RESET	(1<<7)
133 #define ADIS16400_GLOB_CMD_P_AUTO_NULL	(1<<4)
134 #define ADIS16400_GLOB_CMD_FLASH_UPD	(1<<3)
135 #define ADIS16400_GLOB_CMD_DAC_LATCH	(1<<2)
136 #define ADIS16400_GLOB_CMD_FAC_CALIB	(1<<1)
137 #define ADIS16400_GLOB_CMD_AUTO_NULL	(1<<0)
138 
139 /* SLP_CNT */
140 #define ADIS16400_SLP_CNT_POWER_OFF	(1<<8)
141 
142 #define ADIS16334_RATE_DIV_SHIFT 8
143 #define ADIS16334_RATE_INT_CLK BIT(0)
144 
145 #define ADIS16400_SPI_SLOW	(u32)(300 * 1000)
146 #define ADIS16400_SPI_BURST	(u32)(1000 * 1000)
147 #define ADIS16400_SPI_FAST	(u32)(2000 * 1000)
148 
149 #define ADIS16400_HAS_PROD_ID		BIT(0)
150 #define ADIS16400_NO_BURST		BIT(1)
151 #define ADIS16400_HAS_SLOW_MODE		BIT(2)
152 #define ADIS16400_HAS_SERIAL_NUMBER	BIT(3)
153 #define ADIS16400_BURST_DIAG_STAT	BIT(4)
154 
155 struct adis16400_state;
156 
157 struct adis16400_chip_info {
158 	const struct iio_chan_spec *channels;
159 	const struct adis_data adis_data;
160 	const int num_channels;
161 	const long flags;
162 	unsigned int gyro_scale_micro;
163 	unsigned int accel_scale_micro;
164 	int temp_scale_nano;
165 	int temp_offset;
166 	/* set_freq() & get_freq() need to avoid using ADIS lib's state lock */
167 	int (*set_freq)(struct adis16400_state *st, unsigned int freq);
168 	int (*get_freq)(struct adis16400_state *st);
169 };
170 
171 /**
172  * struct adis16400_state - device instance specific data
173  * @variant:	chip variant info
174  * @filt_int:	integer part of requested filter frequency
175  * @adis:	adis device
176  * @avail_scan_mask:	NULL terminated array of bitmaps of channels
177  *			that must be enabled together
178  **/
179 struct adis16400_state {
180 	struct adis16400_chip_info	*variant;
181 	int				filt_int;
182 
183 	struct adis adis;
184 	unsigned long avail_scan_mask[2];
185 };
186 
187 /* At the moment triggers are only used for ring buffer
188  * filling. This may change!
189  */
190 
191 enum {
192 	ADIS16400_SCAN_SUPPLY,
193 	ADIS16400_SCAN_GYRO_X,
194 	ADIS16400_SCAN_GYRO_Y,
195 	ADIS16400_SCAN_GYRO_Z,
196 	ADIS16400_SCAN_ACC_X,
197 	ADIS16400_SCAN_ACC_Y,
198 	ADIS16400_SCAN_ACC_Z,
199 	ADIS16400_SCAN_MAGN_X,
200 	ADIS16400_SCAN_MAGN_Y,
201 	ADIS16400_SCAN_MAGN_Z,
202 	ADIS16400_SCAN_BARO,
203 	ADIS16350_SCAN_TEMP_X,
204 	ADIS16350_SCAN_TEMP_Y,
205 	ADIS16350_SCAN_TEMP_Z,
206 	ADIS16300_SCAN_INCLI_X,
207 	ADIS16300_SCAN_INCLI_Y,
208 	ADIS16400_SCAN_ADC,
209 	ADIS16400_SCAN_TIMESTAMP,
210 };
211 
212 #ifdef CONFIG_DEBUG_FS
213 
214 static ssize_t adis16400_show_serial_number(struct file *file,
215 		char __user *userbuf, size_t count, loff_t *ppos)
216 {
217 	struct adis16400_state *st = file->private_data;
218 	u16 lot1, lot2, serial_number;
219 	char buf[16];
220 	size_t len;
221 	int ret;
222 
223 	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
224 	if (ret)
225 		return ret;
226 
227 	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
228 	if (ret)
229 		return ret;
230 
231 	ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
232 			&serial_number);
233 	if (ret)
234 		return ret;
235 
236 	len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
237 			serial_number);
238 
239 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
240 }
241 
242 static const struct file_operations adis16400_serial_number_fops = {
243 	.open = simple_open,
244 	.read = adis16400_show_serial_number,
245 	.llseek = default_llseek,
246 	.owner = THIS_MODULE,
247 };
248 
249 static int adis16400_show_product_id(void *arg, u64 *val)
250 {
251 	struct adis16400_state *st = arg;
252 	uint16_t prod_id;
253 	int ret;
254 
255 	ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
256 	if (ret)
257 		return ret;
258 
259 	*val = prod_id;
260 
261 	return 0;
262 }
263 DEFINE_DEBUGFS_ATTRIBUTE(adis16400_product_id_fops,
264 	adis16400_show_product_id, NULL, "%lld\n");
265 
266 static int adis16400_show_flash_count(void *arg, u64 *val)
267 {
268 	struct adis16400_state *st = arg;
269 	uint16_t flash_count;
270 	int ret;
271 
272 	ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
273 	if (ret)
274 		return ret;
275 
276 	*val = flash_count;
277 
278 	return 0;
279 }
280 DEFINE_DEBUGFS_ATTRIBUTE(adis16400_flash_count_fops,
281 	adis16400_show_flash_count, NULL, "%lld\n");
282 
283 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
284 {
285 	struct adis16400_state *st = iio_priv(indio_dev);
286 	struct dentry *d = iio_get_debugfs_dentry(indio_dev);
287 
288 	if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
289 		debugfs_create_file_unsafe("serial_number", 0400,
290 				d, st, &adis16400_serial_number_fops);
291 	if (st->variant->flags & ADIS16400_HAS_PROD_ID)
292 		debugfs_create_file_unsafe("product_id", 0400,
293 				d, st, &adis16400_product_id_fops);
294 	debugfs_create_file_unsafe("flash_count", 0400,
295 			d, st, &adis16400_flash_count_fops);
296 
297 	return 0;
298 }
299 
300 #else
301 
302 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
303 {
304 	return 0;
305 }
306 
307 #endif
308 
309 enum adis16400_chip_variant {
310 	ADIS16300,
311 	ADIS16334,
312 	ADIS16350,
313 	ADIS16360,
314 	ADIS16362,
315 	ADIS16364,
316 	ADIS16367,
317 	ADIS16400,
318 	ADIS16445,
319 	ADIS16448,
320 };
321 
322 static int adis16334_get_freq(struct adis16400_state *st)
323 {
324 	int ret;
325 	uint16_t t;
326 
327 	ret = __adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
328 	if (ret)
329 		return ret;
330 
331 	t >>= ADIS16334_RATE_DIV_SHIFT;
332 
333 	return 819200 >> t;
334 }
335 
336 static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
337 {
338 	unsigned int t;
339 
340 	if (freq < 819200)
341 		t = ilog2(819200 / freq);
342 	else
343 		t = 0;
344 
345 	if (t > 0x31)
346 		t = 0x31;
347 
348 	t <<= ADIS16334_RATE_DIV_SHIFT;
349 	t |= ADIS16334_RATE_INT_CLK;
350 
351 	return __adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
352 }
353 
354 static int adis16400_get_freq(struct adis16400_state *st)
355 {
356 	int sps, ret;
357 	uint16_t t;
358 
359 	ret = __adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
360 	if (ret)
361 		return ret;
362 
363 	sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
364 	sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
365 
366 	return sps;
367 }
368 
369 static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
370 {
371 	unsigned int t;
372 	uint8_t val = 0;
373 
374 	t = 1638404 / freq;
375 	if (t >= 128) {
376 		val |= ADIS16400_SMPL_PRD_TIME_BASE;
377 		t = 52851 / freq;
378 		if (t >= 128)
379 			t = 127;
380 	} else if (t != 0) {
381 		t--;
382 	}
383 
384 	val |= t;
385 
386 	if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
387 		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
388 	else
389 		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
390 
391 	return __adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
392 }
393 
394 static const unsigned int adis16400_3db_divisors[] = {
395 	[0] = 2, /* Special case */
396 	[1] = 6,
397 	[2] = 12,
398 	[3] = 25,
399 	[4] = 50,
400 	[5] = 100,
401 	[6] = 200,
402 	[7] = 200, /* Not a valid setting */
403 };
404 
405 static int __adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
406 {
407 	struct adis16400_state *st = iio_priv(indio_dev);
408 	uint16_t val16;
409 	int i, ret;
410 
411 	for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
412 		if (sps / adis16400_3db_divisors[i] >= val)
413 			break;
414 	}
415 
416 	ret = __adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
417 	if (ret)
418 		return ret;
419 
420 	ret = __adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
421 					 (val16 & ~0x07) | i);
422 	return ret;
423 }
424 
425 /* Power down the device */
426 static int adis16400_stop_device(struct iio_dev *indio_dev)
427 {
428 	struct adis16400_state *st = iio_priv(indio_dev);
429 	int ret;
430 
431 	ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
432 			ADIS16400_SLP_CNT_POWER_OFF);
433 	if (ret)
434 		dev_err(&indio_dev->dev,
435 			"problem with turning device off: SLP_CNT");
436 
437 	return ret;
438 }
439 
440 static int adis16400_initial_setup(struct iio_dev *indio_dev)
441 {
442 	struct adis16400_state *st = iio_priv(indio_dev);
443 	uint16_t prod_id, smp_prd;
444 	unsigned int device_id;
445 	int ret;
446 
447 	/* use low spi speed for init if the device has a slow mode */
448 	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
449 		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
450 	else
451 		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
452 	st->adis.spi->mode = SPI_MODE_3;
453 	spi_setup(st->adis.spi);
454 
455 	ret = adis_initial_startup(&st->adis);
456 	if (ret)
457 		return ret;
458 
459 	if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
460 		ret = adis_read_reg_16(&st->adis,
461 						ADIS16400_PRODUCT_ID, &prod_id);
462 		if (ret)
463 			goto err_ret;
464 
465 		if (sscanf(indio_dev->name, "adis%u\n", &device_id) != 1) {
466 			ret = -EINVAL;
467 			goto err_ret;
468 		}
469 
470 		if (prod_id != device_id)
471 			dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
472 					device_id, prod_id);
473 
474 		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
475 			indio_dev->name, prod_id,
476 			st->adis.spi->chip_select, st->adis.spi->irq);
477 	}
478 	/* use high spi speed if possible */
479 	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
480 		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
481 		if (ret)
482 			goto err_ret;
483 
484 		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
485 			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
486 			spi_setup(st->adis.spi);
487 		}
488 	}
489 
490 err_ret:
491 	return ret;
492 }
493 
494 static const uint8_t adis16400_addresses[] = {
495 	[ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
496 	[ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
497 	[ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
498 	[ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
499 	[ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
500 	[ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
501 };
502 
503 static int adis16400_write_raw(struct iio_dev *indio_dev,
504 	struct iio_chan_spec const *chan, int val, int val2, long info)
505 {
506 	struct adis16400_state *st = iio_priv(indio_dev);
507 	struct mutex *slock = &st->adis.state_lock;
508 	int ret, sps;
509 
510 	switch (info) {
511 	case IIO_CHAN_INFO_CALIBBIAS:
512 		ret = adis_write_reg_16(&st->adis,
513 				adis16400_addresses[chan->scan_index], val);
514 		return ret;
515 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
516 		/*
517 		 * Need to cache values so we can update if the frequency
518 		 * changes.
519 		 */
520 		mutex_lock(slock);
521 		st->filt_int = val;
522 		/* Work out update to current value */
523 		sps = st->variant->get_freq(st);
524 		if (sps < 0) {
525 			mutex_unlock(slock);
526 			return sps;
527 		}
528 
529 		ret = __adis16400_set_filter(indio_dev, sps,
530 			val * 1000 + val2 / 1000);
531 		mutex_unlock(slock);
532 		return ret;
533 	case IIO_CHAN_INFO_SAMP_FREQ:
534 		sps = val * 1000 + val2 / 1000;
535 
536 		if (sps <= 0)
537 			return -EINVAL;
538 
539 		mutex_lock(slock);
540 		ret = st->variant->set_freq(st, sps);
541 		mutex_unlock(slock);
542 		return ret;
543 	default:
544 		return -EINVAL;
545 	}
546 }
547 
548 static int adis16400_read_raw(struct iio_dev *indio_dev,
549 	struct iio_chan_spec const *chan, int *val, int *val2, long info)
550 {
551 	struct adis16400_state *st = iio_priv(indio_dev);
552 	struct mutex *slock = &st->adis.state_lock;
553 	int16_t val16;
554 	int ret;
555 
556 	switch (info) {
557 	case IIO_CHAN_INFO_RAW:
558 		return adis_single_conversion(indio_dev, chan, 0, val);
559 	case IIO_CHAN_INFO_SCALE:
560 		switch (chan->type) {
561 		case IIO_ANGL_VEL:
562 			*val = 0;
563 			*val2 = st->variant->gyro_scale_micro;
564 			return IIO_VAL_INT_PLUS_MICRO;
565 		case IIO_VOLTAGE:
566 			*val = 0;
567 			if (chan->channel == 0) {
568 				*val = 2;
569 				*val2 = 418000; /* 2.418 mV */
570 			} else {
571 				*val = 0;
572 				*val2 = 805800; /* 805.8 uV */
573 			}
574 			return IIO_VAL_INT_PLUS_MICRO;
575 		case IIO_ACCEL:
576 			*val = 0;
577 			*val2 = st->variant->accel_scale_micro;
578 			return IIO_VAL_INT_PLUS_MICRO;
579 		case IIO_MAGN:
580 			*val = 0;
581 			*val2 = 500; /* 0.5 mgauss */
582 			return IIO_VAL_INT_PLUS_MICRO;
583 		case IIO_TEMP:
584 			*val = st->variant->temp_scale_nano / 1000000;
585 			*val2 = (st->variant->temp_scale_nano % 1000000);
586 			return IIO_VAL_INT_PLUS_MICRO;
587 		case IIO_PRESSURE:
588 			/* 20 uBar = 0.002kPascal */
589 			*val = 0;
590 			*val2 = 2000;
591 			return IIO_VAL_INT_PLUS_MICRO;
592 		default:
593 			return -EINVAL;
594 		}
595 	case IIO_CHAN_INFO_CALIBBIAS:
596 		ret = adis_read_reg_16(&st->adis,
597 				adis16400_addresses[chan->scan_index], &val16);
598 		if (ret)
599 			return ret;
600 		val16 = sign_extend32(val16, 11);
601 		*val = val16;
602 		return IIO_VAL_INT;
603 	case IIO_CHAN_INFO_OFFSET:
604 		/* currently only temperature */
605 		*val = st->variant->temp_offset;
606 		return IIO_VAL_INT;
607 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
608 		mutex_lock(slock);
609 		/* Need both the number of taps and the sampling frequency */
610 		ret = __adis_read_reg_16(&st->adis,
611 						ADIS16400_SENS_AVG,
612 						&val16);
613 		if (ret) {
614 			mutex_unlock(slock);
615 			return ret;
616 		}
617 		ret = st->variant->get_freq(st);
618 		mutex_unlock(slock);
619 		if (ret)
620 			return ret;
621 		ret /= adis16400_3db_divisors[val16 & 0x07];
622 		*val = ret / 1000;
623 		*val2 = (ret % 1000) * 1000;
624 		return IIO_VAL_INT_PLUS_MICRO;
625 	case IIO_CHAN_INFO_SAMP_FREQ:
626 		mutex_lock(slock);
627 		ret = st->variant->get_freq(st);
628 		mutex_unlock(slock);
629 		if (ret)
630 			return ret;
631 		*val = ret / 1000;
632 		*val2 = (ret % 1000) * 1000;
633 		return IIO_VAL_INT_PLUS_MICRO;
634 	default:
635 		return -EINVAL;
636 	}
637 }
638 
639 #if IS_ENABLED(CONFIG_IIO_BUFFER)
640 static irqreturn_t adis16400_trigger_handler(int irq, void *p)
641 {
642 	struct iio_poll_func *pf = p;
643 	struct iio_dev *indio_dev = pf->indio_dev;
644 	struct adis16400_state *st = iio_priv(indio_dev);
645 	struct adis *adis = &st->adis;
646 	u32 old_speed_hz = st->adis.spi->max_speed_hz;
647 	void *buffer;
648 	int ret;
649 
650 	if (!adis->buffer)
651 		return -ENOMEM;
652 
653 	if (!(st->variant->flags & ADIS16400_NO_BURST) &&
654 		st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
655 		st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
656 		spi_setup(st->adis.spi);
657 	}
658 
659 	ret = spi_sync(adis->spi, &adis->msg);
660 	if (ret)
661 		dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
662 
663 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
664 		st->adis.spi->max_speed_hz = old_speed_hz;
665 		spi_setup(st->adis.spi);
666 	}
667 
668 	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
669 		buffer = adis->buffer + sizeof(u16);
670 	else
671 		buffer = adis->buffer;
672 
673 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
674 		pf->timestamp);
675 
676 	iio_trigger_notify_done(indio_dev->trig);
677 
678 	return IRQ_HANDLED;
679 }
680 #else
681 #define adis16400_trigger_handler	NULL
682 #endif /* IS_ENABLED(CONFIG_IIO_BUFFER) */
683 
684 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
685 	.type = IIO_VOLTAGE, \
686 	.indexed = 1, \
687 	.channel = chn, \
688 	.extend_name = name, \
689 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
690 		BIT(IIO_CHAN_INFO_SCALE), \
691 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
692 	.address = (addr), \
693 	.scan_index = (si), \
694 	.scan_type = { \
695 		.sign = 'u', \
696 		.realbits = (bits), \
697 		.storagebits = 16, \
698 		.shift = 0, \
699 		.endianness = IIO_BE, \
700 	}, \
701 }
702 
703 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
704 	ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
705 
706 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
707 	ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
708 
709 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
710 	.type = IIO_ANGL_VEL, \
711 	.modified = 1, \
712 	.channel2 = IIO_MOD_ ## mod, \
713 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
714 		BIT(IIO_CHAN_INFO_CALIBBIAS),		  \
715 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
716 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
717 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
718 	.address = addr, \
719 	.scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
720 	.scan_type = { \
721 		.sign = 's', \
722 		.realbits = (bits), \
723 		.storagebits = 16, \
724 		.shift = 0, \
725 		.endianness = IIO_BE, \
726 	}, \
727 }
728 
729 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
730 	.type = IIO_ACCEL, \
731 	.modified = 1, \
732 	.channel2 = IIO_MOD_ ## mod, \
733 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
734 		BIT(IIO_CHAN_INFO_CALIBBIAS), \
735 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
736 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
737 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
738 	.address = (addr), \
739 	.scan_index = ADIS16400_SCAN_ACC_ ## mod, \
740 	.scan_type = { \
741 		.sign = 's', \
742 		.realbits = (bits), \
743 		.storagebits = 16, \
744 		.shift = 0, \
745 		.endianness = IIO_BE, \
746 	}, \
747 }
748 
749 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
750 	.type = IIO_MAGN, \
751 	.modified = 1, \
752 	.channel2 = IIO_MOD_ ## mod, \
753 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
754 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
755 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
756 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
757 	.address = (addr), \
758 	.scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
759 	.scan_type = { \
760 		.sign = 's', \
761 		.realbits = (bits), \
762 		.storagebits = 16, \
763 		.shift = 0, \
764 		.endianness = IIO_BE, \
765 	}, \
766 }
767 
768 #define ADIS16400_MOD_TEMP_NAME_X "x"
769 #define ADIS16400_MOD_TEMP_NAME_Y "y"
770 #define ADIS16400_MOD_TEMP_NAME_Z "z"
771 
772 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
773 	.type = IIO_TEMP, \
774 	.indexed = 1, \
775 	.channel = 0, \
776 	.extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
777 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
778 		BIT(IIO_CHAN_INFO_OFFSET) | \
779 		BIT(IIO_CHAN_INFO_SCALE), \
780 	.info_mask_shared_by_type = \
781 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
782 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
783 	.address = (addr), \
784 	.scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
785 	.scan_type = { \
786 		.sign = 's', \
787 		.realbits = (bits), \
788 		.storagebits = 16, \
789 		.shift = 0, \
790 		.endianness = IIO_BE, \
791 	}, \
792 }
793 
794 #define ADIS16400_TEMP_CHAN(addr, bits) { \
795 	.type = IIO_TEMP, \
796 	.indexed = 1, \
797 	.channel = 0, \
798 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
799 		BIT(IIO_CHAN_INFO_OFFSET) | \
800 		BIT(IIO_CHAN_INFO_SCALE), \
801 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
802 	.address = (addr), \
803 	.scan_index = ADIS16350_SCAN_TEMP_X, \
804 	.scan_type = { \
805 		.sign = 's', \
806 		.realbits = (bits), \
807 		.storagebits = 16, \
808 		.shift = 0, \
809 		.endianness = IIO_BE, \
810 	}, \
811 }
812 
813 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
814 	.type = IIO_INCLI, \
815 	.modified = 1, \
816 	.channel2 = IIO_MOD_ ## mod, \
817 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
818 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
819 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
820 	.address = (addr), \
821 	.scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
822 	.scan_type = { \
823 		.sign = 's', \
824 		.realbits = (bits), \
825 		.storagebits = 16, \
826 		.shift = 0, \
827 		.endianness = IIO_BE, \
828 	}, \
829 }
830 
831 static const struct iio_chan_spec adis16400_channels[] = {
832 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
833 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
834 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
835 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
836 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
837 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
838 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
839 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
840 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
841 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
842 	ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
843 	ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
844 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
845 };
846 
847 static const struct iio_chan_spec adis16445_channels[] = {
848 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
849 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
850 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
851 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
852 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
853 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
854 	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
855 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
856 };
857 
858 static const struct iio_chan_spec adis16448_channels[] = {
859 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
860 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
861 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
862 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
863 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
864 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
865 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
866 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
867 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
868 	{
869 		.type = IIO_PRESSURE,
870 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
871 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
872 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
873 		.address = ADIS16448_BARO_OUT,
874 		.scan_index = ADIS16400_SCAN_BARO,
875 		.scan_type = {
876 			.sign = 's',
877 			.realbits = 16,
878 			.storagebits = 16,
879 			.endianness = IIO_BE,
880 		},
881 	},
882 	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
883 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
884 };
885 
886 static const struct iio_chan_spec adis16350_channels[] = {
887 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
888 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
889 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
890 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
891 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
892 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
893 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
894 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
895 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
896 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
897 	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
898 	ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
899 	ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
900 	ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
901 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
902 };
903 
904 static const struct iio_chan_spec adis16300_channels[] = {
905 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
906 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
907 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
908 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
909 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
910 	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
911 	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
912 	ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
913 	ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
914 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
915 };
916 
917 static const struct iio_chan_spec adis16334_channels[] = {
918 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
919 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
920 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
921 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
922 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
923 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
924 	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
925 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
926 };
927 
928 static const char * const adis16400_status_error_msgs[] = {
929 	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
930 	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
931 	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
932 	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
933 	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
934 	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
935 	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
936 	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
937 	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
938 	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
939 	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
940 	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
941 	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
942 	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
943 	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
944 };
945 
946 #define ADIS16400_DATA(_timeouts, _burst_len)				\
947 {									\
948 	.msc_ctrl_reg = ADIS16400_MSC_CTRL,				\
949 	.glob_cmd_reg = ADIS16400_GLOB_CMD,				\
950 	.diag_stat_reg = ADIS16400_DIAG_STAT,				\
951 	.read_delay = 50,						\
952 	.write_delay = 50,						\
953 	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,			\
954 	.self_test_reg = ADIS16400_MSC_CTRL,				\
955 	.status_error_msgs = adis16400_status_error_msgs,		\
956 	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |	\
957 		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |			\
958 		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |			\
959 		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |			\
960 		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |			\
961 		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |			\
962 		BIT(ADIS16400_DIAG_STAT_ALARM2) |			\
963 		BIT(ADIS16400_DIAG_STAT_ALARM1) |			\
964 		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |			\
965 		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |			\
966 		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |			\
967 		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |			\
968 		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |			\
969 		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |			\
970 		BIT(ADIS16400_DIAG_STAT_POWER_LOW),			\
971 	.timeouts = (_timeouts),					\
972 	.burst_reg_cmd = ADIS16400_GLOB_CMD,				\
973 	.burst_len = (_burst_len)					\
974 }
975 
976 static const struct adis_timeout adis16300_timeouts = {
977 	.reset_ms = ADIS16400_STARTUP_DELAY,
978 	.sw_reset_ms = ADIS16400_STARTUP_DELAY,
979 	.self_test_ms = ADIS16400_STARTUP_DELAY,
980 };
981 
982 static const struct adis_timeout adis16334_timeouts = {
983 	.reset_ms = 60,
984 	.sw_reset_ms = 60,
985 	.self_test_ms = 14,
986 };
987 
988 static const struct adis_timeout adis16362_timeouts = {
989 	.reset_ms = 130,
990 	.sw_reset_ms = 130,
991 	.self_test_ms = 12,
992 };
993 
994 static const struct adis_timeout adis16400_timeouts = {
995 	.reset_ms = 170,
996 	.sw_reset_ms = 170,
997 	.self_test_ms = 12,
998 };
999 
1000 static const struct adis_timeout adis16445_timeouts = {
1001 	.reset_ms = 55,
1002 	.sw_reset_ms = 55,
1003 	.self_test_ms = 16,
1004 };
1005 
1006 static const struct adis_timeout adis16448_timeouts = {
1007 	.reset_ms = 90,
1008 	.sw_reset_ms = 90,
1009 	.self_test_ms = 45,
1010 };
1011 
1012 static struct adis16400_chip_info adis16400_chips[] = {
1013 	[ADIS16300] = {
1014 		.channels = adis16300_channels,
1015 		.num_channels = ARRAY_SIZE(adis16300_channels),
1016 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1017 				ADIS16400_HAS_SERIAL_NUMBER,
1018 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1019 		.accel_scale_micro = 5884,
1020 		.temp_scale_nano = 140000000, /* 0.14 C */
1021 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
1022 		.set_freq = adis16400_set_freq,
1023 		.get_freq = adis16400_get_freq,
1024 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 18),
1025 	},
1026 	[ADIS16334] = {
1027 		.channels = adis16334_channels,
1028 		.num_channels = ARRAY_SIZE(adis16334_channels),
1029 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
1030 				ADIS16400_HAS_SERIAL_NUMBER,
1031 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1032 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1033 		.temp_scale_nano = 67850000, /* 0.06785 C */
1034 		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
1035 		.set_freq = adis16334_set_freq,
1036 		.get_freq = adis16334_get_freq,
1037 		.adis_data = ADIS16400_DATA(&adis16334_timeouts, 0),
1038 	},
1039 	[ADIS16350] = {
1040 		.channels = adis16350_channels,
1041 		.num_channels = ARRAY_SIZE(adis16350_channels),
1042 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
1043 		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
1044 		.temp_scale_nano = 145300000, /* 0.1453 C */
1045 		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
1046 		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
1047 		.set_freq = adis16400_set_freq,
1048 		.get_freq = adis16400_get_freq,
1049 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 0),
1050 	},
1051 	[ADIS16360] = {
1052 		.channels = adis16350_channels,
1053 		.num_channels = ARRAY_SIZE(adis16350_channels),
1054 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1055 				ADIS16400_HAS_SERIAL_NUMBER,
1056 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1057 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1058 		.temp_scale_nano = 136000000, /* 0.136 C */
1059 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1060 		.set_freq = adis16400_set_freq,
1061 		.get_freq = adis16400_get_freq,
1062 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 28),
1063 	},
1064 	[ADIS16362] = {
1065 		.channels = adis16350_channels,
1066 		.num_channels = ARRAY_SIZE(adis16350_channels),
1067 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1068 				ADIS16400_HAS_SERIAL_NUMBER,
1069 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1070 		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
1071 		.temp_scale_nano = 136000000, /* 0.136 C */
1072 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1073 		.set_freq = adis16400_set_freq,
1074 		.get_freq = adis16400_get_freq,
1075 		.adis_data = ADIS16400_DATA(&adis16362_timeouts, 28),
1076 	},
1077 	[ADIS16364] = {
1078 		.channels = adis16350_channels,
1079 		.num_channels = ARRAY_SIZE(adis16350_channels),
1080 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1081 				ADIS16400_HAS_SERIAL_NUMBER,
1082 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1083 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1084 		.temp_scale_nano = 136000000, /* 0.136 C */
1085 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1086 		.set_freq = adis16400_set_freq,
1087 		.get_freq = adis16400_get_freq,
1088 		.adis_data = ADIS16400_DATA(&adis16362_timeouts, 28),
1089 	},
1090 	[ADIS16367] = {
1091 		.channels = adis16350_channels,
1092 		.num_channels = ARRAY_SIZE(adis16350_channels),
1093 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1094 				ADIS16400_HAS_SERIAL_NUMBER,
1095 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
1096 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1097 		.temp_scale_nano = 136000000, /* 0.136 C */
1098 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1099 		.set_freq = adis16400_set_freq,
1100 		.get_freq = adis16400_get_freq,
1101 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 28),
1102 	},
1103 	[ADIS16400] = {
1104 		.channels = adis16400_channels,
1105 		.num_channels = ARRAY_SIZE(adis16400_channels),
1106 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
1107 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1108 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1109 		.temp_scale_nano = 140000000, /* 0.14 C */
1110 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
1111 		.set_freq = adis16400_set_freq,
1112 		.get_freq = adis16400_get_freq,
1113 		.adis_data = ADIS16400_DATA(&adis16400_timeouts, 24),
1114 	},
1115 	[ADIS16445] = {
1116 		.channels = adis16445_channels,
1117 		.num_channels = ARRAY_SIZE(adis16445_channels),
1118 		.flags = ADIS16400_HAS_PROD_ID |
1119 				ADIS16400_HAS_SERIAL_NUMBER |
1120 				ADIS16400_BURST_DIAG_STAT,
1121 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
1122 		.accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
1123 		.temp_scale_nano = 73860000, /* 0.07386 C */
1124 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1125 		.set_freq = adis16334_set_freq,
1126 		.get_freq = adis16334_get_freq,
1127 		.adis_data = ADIS16400_DATA(&adis16445_timeouts, 16),
1128 	},
1129 	[ADIS16448] = {
1130 		.channels = adis16448_channels,
1131 		.num_channels = ARRAY_SIZE(adis16448_channels),
1132 		.flags = ADIS16400_HAS_PROD_ID |
1133 				ADIS16400_HAS_SERIAL_NUMBER |
1134 				ADIS16400_BURST_DIAG_STAT,
1135 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
1136 		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
1137 		.temp_scale_nano = 73860000, /* 0.07386 C */
1138 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1139 		.set_freq = adis16334_set_freq,
1140 		.get_freq = adis16334_get_freq,
1141 		.adis_data = ADIS16400_DATA(&adis16448_timeouts, 24),
1142 	}
1143 };
1144 
1145 static const struct iio_info adis16400_info = {
1146 	.read_raw = &adis16400_read_raw,
1147 	.write_raw = &adis16400_write_raw,
1148 	.update_scan_mode = adis_update_scan_mode,
1149 	.debugfs_reg_access = adis_debugfs_reg_access,
1150 };
1151 
1152 static void adis16400_setup_chan_mask(struct adis16400_state *st)
1153 {
1154 	const struct adis16400_chip_info *chip_info = st->variant;
1155 	unsigned int i;
1156 
1157 	for (i = 0; i < chip_info->num_channels; i++) {
1158 		const struct iio_chan_spec *ch = &chip_info->channels[i];
1159 
1160 		if (ch->scan_index >= 0 &&
1161 		    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
1162 			st->avail_scan_mask[0] |= BIT(ch->scan_index);
1163 	}
1164 }
1165 
1166 static void adis16400_stop(void *data)
1167 {
1168 	adis16400_stop_device(data);
1169 }
1170 
1171 static int adis16400_probe(struct spi_device *spi)
1172 {
1173 	struct adis16400_state *st;
1174 	struct iio_dev *indio_dev;
1175 	int ret;
1176 	const struct adis_data *adis16400_data;
1177 
1178 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1179 	if (indio_dev == NULL)
1180 		return -ENOMEM;
1181 
1182 	st = iio_priv(indio_dev);
1183 	/* this is only used for removal purposes */
1184 	spi_set_drvdata(spi, indio_dev);
1185 
1186 	/* setup the industrialio driver allocated elements */
1187 	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1188 	indio_dev->name = spi_get_device_id(spi)->name;
1189 	indio_dev->channels = st->variant->channels;
1190 	indio_dev->num_channels = st->variant->num_channels;
1191 	indio_dev->info = &adis16400_info;
1192 	indio_dev->modes = INDIO_DIRECT_MODE;
1193 
1194 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
1195 		adis16400_setup_chan_mask(st);
1196 		indio_dev->available_scan_masks = st->avail_scan_mask;
1197 	}
1198 
1199 	adis16400_data = &st->variant->adis_data;
1200 
1201 	ret = adis_init(&st->adis, indio_dev, spi, adis16400_data);
1202 	if (ret)
1203 		return ret;
1204 
1205 	ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, adis16400_trigger_handler);
1206 	if (ret)
1207 		return ret;
1208 
1209 	/* Get the device into a sane initial state */
1210 	ret = adis16400_initial_setup(indio_dev);
1211 	if (ret)
1212 		return ret;
1213 
1214 	ret = devm_add_action_or_reset(&spi->dev, adis16400_stop, indio_dev);
1215 	if (ret)
1216 		return ret;
1217 
1218 	ret = devm_iio_device_register(&spi->dev, indio_dev);
1219 	if (ret)
1220 		return ret;
1221 
1222 	adis16400_debugfs_init(indio_dev);
1223 	return 0;
1224 }
1225 
1226 static const struct spi_device_id adis16400_id[] = {
1227 	{"adis16300", ADIS16300},
1228 	{"adis16305", ADIS16300},
1229 	{"adis16334", ADIS16334},
1230 	{"adis16350", ADIS16350},
1231 	{"adis16354", ADIS16350},
1232 	{"adis16355", ADIS16350},
1233 	{"adis16360", ADIS16360},
1234 	{"adis16362", ADIS16362},
1235 	{"adis16364", ADIS16364},
1236 	{"adis16365", ADIS16360},
1237 	{"adis16367", ADIS16367},
1238 	{"adis16400", ADIS16400},
1239 	{"adis16405", ADIS16400},
1240 	{"adis16445", ADIS16445},
1241 	{"adis16448", ADIS16448},
1242 	{}
1243 };
1244 MODULE_DEVICE_TABLE(spi, adis16400_id);
1245 
1246 static struct spi_driver adis16400_driver = {
1247 	.driver = {
1248 		.name = "adis16400",
1249 	},
1250 	.id_table = adis16400_id,
1251 	.probe = adis16400_probe,
1252 };
1253 module_spi_driver(adis16400_driver);
1254 
1255 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1256 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1257 MODULE_LICENSE("GPL v2");
1258