xref: /openbmc/linux/drivers/iio/imu/adis16400.c (revision d9fd5a71)
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 		ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
466 		if (ret != 1) {
467 			ret = -EINVAL;
468 			goto err_ret;
469 		}
470 
471 		if (prod_id != device_id)
472 			dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
473 					device_id, prod_id);
474 
475 		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
476 			indio_dev->name, prod_id,
477 			st->adis.spi->chip_select, st->adis.spi->irq);
478 	}
479 	/* use high spi speed if possible */
480 	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
481 		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
482 		if (ret)
483 			goto err_ret;
484 
485 		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
486 			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
487 			spi_setup(st->adis.spi);
488 		}
489 	}
490 
491 err_ret:
492 	return ret;
493 }
494 
495 static const uint8_t adis16400_addresses[] = {
496 	[ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
497 	[ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
498 	[ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
499 	[ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
500 	[ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
501 	[ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
502 };
503 
504 static int adis16400_write_raw(struct iio_dev *indio_dev,
505 	struct iio_chan_spec const *chan, int val, int val2, long info)
506 {
507 	struct adis16400_state *st = iio_priv(indio_dev);
508 	struct mutex *slock = &st->adis.state_lock;
509 	int ret, sps;
510 
511 	switch (info) {
512 	case IIO_CHAN_INFO_CALIBBIAS:
513 		ret = adis_write_reg_16(&st->adis,
514 				adis16400_addresses[chan->scan_index], val);
515 		return ret;
516 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
517 		/*
518 		 * Need to cache values so we can update if the frequency
519 		 * changes.
520 		 */
521 		mutex_lock(slock);
522 		st->filt_int = val;
523 		/* Work out update to current value */
524 		sps = st->variant->get_freq(st);
525 		if (sps < 0) {
526 			mutex_unlock(slock);
527 			return sps;
528 		}
529 
530 		ret = __adis16400_set_filter(indio_dev, sps,
531 			val * 1000 + val2 / 1000);
532 		mutex_unlock(slock);
533 		return ret;
534 	case IIO_CHAN_INFO_SAMP_FREQ:
535 		sps = val * 1000 + val2 / 1000;
536 
537 		if (sps <= 0)
538 			return -EINVAL;
539 
540 		mutex_lock(slock);
541 		ret = st->variant->set_freq(st, sps);
542 		mutex_unlock(slock);
543 		return ret;
544 	default:
545 		return -EINVAL;
546 	}
547 }
548 
549 static int adis16400_read_raw(struct iio_dev *indio_dev,
550 	struct iio_chan_spec const *chan, int *val, int *val2, long info)
551 {
552 	struct adis16400_state *st = iio_priv(indio_dev);
553 	struct mutex *slock = &st->adis.state_lock;
554 	int16_t val16;
555 	int ret;
556 
557 	switch (info) {
558 	case IIO_CHAN_INFO_RAW:
559 		return adis_single_conversion(indio_dev, chan, 0, val);
560 	case IIO_CHAN_INFO_SCALE:
561 		switch (chan->type) {
562 		case IIO_ANGL_VEL:
563 			*val = 0;
564 			*val2 = st->variant->gyro_scale_micro;
565 			return IIO_VAL_INT_PLUS_MICRO;
566 		case IIO_VOLTAGE:
567 			*val = 0;
568 			if (chan->channel == 0) {
569 				*val = 2;
570 				*val2 = 418000; /* 2.418 mV */
571 			} else {
572 				*val = 0;
573 				*val2 = 805800; /* 805.8 uV */
574 			}
575 			return IIO_VAL_INT_PLUS_MICRO;
576 		case IIO_ACCEL:
577 			*val = 0;
578 			*val2 = st->variant->accel_scale_micro;
579 			return IIO_VAL_INT_PLUS_MICRO;
580 		case IIO_MAGN:
581 			*val = 0;
582 			*val2 = 500; /* 0.5 mgauss */
583 			return IIO_VAL_INT_PLUS_MICRO;
584 		case IIO_TEMP:
585 			*val = st->variant->temp_scale_nano / 1000000;
586 			*val2 = (st->variant->temp_scale_nano % 1000000);
587 			return IIO_VAL_INT_PLUS_MICRO;
588 		case IIO_PRESSURE:
589 			/* 20 uBar = 0.002kPascal */
590 			*val = 0;
591 			*val2 = 2000;
592 			return IIO_VAL_INT_PLUS_MICRO;
593 		default:
594 			return -EINVAL;
595 		}
596 	case IIO_CHAN_INFO_CALIBBIAS:
597 		ret = adis_read_reg_16(&st->adis,
598 				adis16400_addresses[chan->scan_index], &val16);
599 		if (ret)
600 			return ret;
601 		val16 = sign_extend32(val16, 11);
602 		*val = val16;
603 		return IIO_VAL_INT;
604 	case IIO_CHAN_INFO_OFFSET:
605 		/* currently only temperature */
606 		*val = st->variant->temp_offset;
607 		return IIO_VAL_INT;
608 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
609 		mutex_lock(slock);
610 		/* Need both the number of taps and the sampling frequency */
611 		ret = __adis_read_reg_16(&st->adis,
612 						ADIS16400_SENS_AVG,
613 						&val16);
614 		if (ret) {
615 			mutex_unlock(slock);
616 			return ret;
617 		}
618 		ret = st->variant->get_freq(st);
619 		mutex_unlock(slock);
620 		if (ret)
621 			return ret;
622 		ret /= adis16400_3db_divisors[val16 & 0x07];
623 		*val = ret / 1000;
624 		*val2 = (ret % 1000) * 1000;
625 		return IIO_VAL_INT_PLUS_MICRO;
626 	case IIO_CHAN_INFO_SAMP_FREQ:
627 		mutex_lock(slock);
628 		ret = st->variant->get_freq(st);
629 		mutex_unlock(slock);
630 		if (ret)
631 			return ret;
632 		*val = ret / 1000;
633 		*val2 = (ret % 1000) * 1000;
634 		return IIO_VAL_INT_PLUS_MICRO;
635 	default:
636 		return -EINVAL;
637 	}
638 }
639 
640 #if IS_ENABLED(CONFIG_IIO_BUFFER)
641 static irqreturn_t adis16400_trigger_handler(int irq, void *p)
642 {
643 	struct iio_poll_func *pf = p;
644 	struct iio_dev *indio_dev = pf->indio_dev;
645 	struct adis16400_state *st = iio_priv(indio_dev);
646 	struct adis *adis = &st->adis;
647 	u32 old_speed_hz = st->adis.spi->max_speed_hz;
648 	void *buffer;
649 	int ret;
650 
651 	if (!adis->buffer)
652 		return -ENOMEM;
653 
654 	if (!(st->variant->flags & ADIS16400_NO_BURST) &&
655 		st->adis.spi->max_speed_hz > ADIS16400_SPI_BURST) {
656 		st->adis.spi->max_speed_hz = ADIS16400_SPI_BURST;
657 		spi_setup(st->adis.spi);
658 	}
659 
660 	ret = spi_sync(adis->spi, &adis->msg);
661 	if (ret)
662 		dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
663 
664 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
665 		st->adis.spi->max_speed_hz = old_speed_hz;
666 		spi_setup(st->adis.spi);
667 	}
668 
669 	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
670 		buffer = adis->buffer + sizeof(u16);
671 	else
672 		buffer = adis->buffer;
673 
674 	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
675 		pf->timestamp);
676 
677 	iio_trigger_notify_done(indio_dev->trig);
678 
679 	return IRQ_HANDLED;
680 }
681 #else
682 #define adis16400_trigger_handler	NULL
683 #endif /* IS_ENABLED(CONFIG_IIO_BUFFER) */
684 
685 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
686 	.type = IIO_VOLTAGE, \
687 	.indexed = 1, \
688 	.channel = chn, \
689 	.extend_name = name, \
690 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
691 		BIT(IIO_CHAN_INFO_SCALE), \
692 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
693 	.address = (addr), \
694 	.scan_index = (si), \
695 	.scan_type = { \
696 		.sign = 'u', \
697 		.realbits = (bits), \
698 		.storagebits = 16, \
699 		.shift = 0, \
700 		.endianness = IIO_BE, \
701 	}, \
702 }
703 
704 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
705 	ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
706 
707 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
708 	ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
709 
710 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
711 	.type = IIO_ANGL_VEL, \
712 	.modified = 1, \
713 	.channel2 = IIO_MOD_ ## mod, \
714 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
715 		BIT(IIO_CHAN_INFO_CALIBBIAS),		  \
716 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
717 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
718 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
719 	.address = addr, \
720 	.scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
721 	.scan_type = { \
722 		.sign = 's', \
723 		.realbits = (bits), \
724 		.storagebits = 16, \
725 		.shift = 0, \
726 		.endianness = IIO_BE, \
727 	}, \
728 }
729 
730 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
731 	.type = IIO_ACCEL, \
732 	.modified = 1, \
733 	.channel2 = IIO_MOD_ ## mod, \
734 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
735 		BIT(IIO_CHAN_INFO_CALIBBIAS), \
736 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
737 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
738 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
739 	.address = (addr), \
740 	.scan_index = ADIS16400_SCAN_ACC_ ## mod, \
741 	.scan_type = { \
742 		.sign = 's', \
743 		.realbits = (bits), \
744 		.storagebits = 16, \
745 		.shift = 0, \
746 		.endianness = IIO_BE, \
747 	}, \
748 }
749 
750 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
751 	.type = IIO_MAGN, \
752 	.modified = 1, \
753 	.channel2 = IIO_MOD_ ## mod, \
754 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
755 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
756 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
757 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
758 	.address = (addr), \
759 	.scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
760 	.scan_type = { \
761 		.sign = 's', \
762 		.realbits = (bits), \
763 		.storagebits = 16, \
764 		.shift = 0, \
765 		.endianness = IIO_BE, \
766 	}, \
767 }
768 
769 #define ADIS16400_MOD_TEMP_NAME_X "x"
770 #define ADIS16400_MOD_TEMP_NAME_Y "y"
771 #define ADIS16400_MOD_TEMP_NAME_Z "z"
772 
773 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
774 	.type = IIO_TEMP, \
775 	.indexed = 1, \
776 	.channel = 0, \
777 	.extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
778 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
779 		BIT(IIO_CHAN_INFO_OFFSET) | \
780 		BIT(IIO_CHAN_INFO_SCALE), \
781 	.info_mask_shared_by_type = \
782 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
783 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
784 	.address = (addr), \
785 	.scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
786 	.scan_type = { \
787 		.sign = 's', \
788 		.realbits = (bits), \
789 		.storagebits = 16, \
790 		.shift = 0, \
791 		.endianness = IIO_BE, \
792 	}, \
793 }
794 
795 #define ADIS16400_TEMP_CHAN(addr, bits) { \
796 	.type = IIO_TEMP, \
797 	.indexed = 1, \
798 	.channel = 0, \
799 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
800 		BIT(IIO_CHAN_INFO_OFFSET) | \
801 		BIT(IIO_CHAN_INFO_SCALE), \
802 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
803 	.address = (addr), \
804 	.scan_index = ADIS16350_SCAN_TEMP_X, \
805 	.scan_type = { \
806 		.sign = 's', \
807 		.realbits = (bits), \
808 		.storagebits = 16, \
809 		.shift = 0, \
810 		.endianness = IIO_BE, \
811 	}, \
812 }
813 
814 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
815 	.type = IIO_INCLI, \
816 	.modified = 1, \
817 	.channel2 = IIO_MOD_ ## mod, \
818 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
819 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
820 	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
821 	.address = (addr), \
822 	.scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
823 	.scan_type = { \
824 		.sign = 's', \
825 		.realbits = (bits), \
826 		.storagebits = 16, \
827 		.shift = 0, \
828 		.endianness = IIO_BE, \
829 	}, \
830 }
831 
832 static const struct iio_chan_spec adis16400_channels[] = {
833 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
834 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
835 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
836 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
837 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
838 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
839 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
840 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
841 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
842 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
843 	ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
844 	ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
845 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
846 };
847 
848 static const struct iio_chan_spec adis16445_channels[] = {
849 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
850 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
851 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
852 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
853 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
854 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
855 	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
856 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
857 };
858 
859 static const struct iio_chan_spec adis16448_channels[] = {
860 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
861 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
862 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
863 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
864 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
865 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
866 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
867 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
868 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
869 	{
870 		.type = IIO_PRESSURE,
871 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
872 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
873 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
874 		.address = ADIS16448_BARO_OUT,
875 		.scan_index = ADIS16400_SCAN_BARO,
876 		.scan_type = {
877 			.sign = 's',
878 			.realbits = 16,
879 			.storagebits = 16,
880 			.endianness = IIO_BE,
881 		},
882 	},
883 	ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
884 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
885 };
886 
887 static const struct iio_chan_spec adis16350_channels[] = {
888 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
889 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
890 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
891 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
892 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
893 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
894 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
895 	ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
896 	ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
897 	ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
898 	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
899 	ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
900 	ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
901 	ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
902 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
903 };
904 
905 static const struct iio_chan_spec adis16300_channels[] = {
906 	ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
907 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
908 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
909 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
910 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
911 	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
912 	ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
913 	ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
914 	ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
915 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
916 };
917 
918 static const struct iio_chan_spec adis16334_channels[] = {
919 	ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
920 	ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
921 	ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
922 	ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
923 	ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
924 	ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
925 	ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
926 	IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
927 };
928 
929 static const char * const adis16400_status_error_msgs[] = {
930 	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
931 	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
932 	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
933 	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
934 	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
935 	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
936 	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
937 	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
938 	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
939 	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
940 	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
941 	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
942 	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
943 	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
944 	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
945 };
946 
947 #define ADIS16400_DATA(_timeouts, _burst_len)				\
948 {									\
949 	.msc_ctrl_reg = ADIS16400_MSC_CTRL,				\
950 	.glob_cmd_reg = ADIS16400_GLOB_CMD,				\
951 	.diag_stat_reg = ADIS16400_DIAG_STAT,				\
952 	.read_delay = 50,						\
953 	.write_delay = 50,						\
954 	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,			\
955 	.self_test_reg = ADIS16400_MSC_CTRL,				\
956 	.status_error_msgs = adis16400_status_error_msgs,		\
957 	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |	\
958 		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |			\
959 		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |			\
960 		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |			\
961 		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |			\
962 		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |			\
963 		BIT(ADIS16400_DIAG_STAT_ALARM2) |			\
964 		BIT(ADIS16400_DIAG_STAT_ALARM1) |			\
965 		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |			\
966 		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |			\
967 		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |			\
968 		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |			\
969 		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |			\
970 		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |			\
971 		BIT(ADIS16400_DIAG_STAT_POWER_LOW),			\
972 	.timeouts = (_timeouts),					\
973 	.burst_reg_cmd = ADIS16400_GLOB_CMD,				\
974 	.burst_len = (_burst_len)					\
975 }
976 
977 static const struct adis_timeout adis16300_timeouts = {
978 	.reset_ms = ADIS16400_STARTUP_DELAY,
979 	.sw_reset_ms = ADIS16400_STARTUP_DELAY,
980 	.self_test_ms = ADIS16400_STARTUP_DELAY,
981 };
982 
983 static const struct adis_timeout adis16334_timeouts = {
984 	.reset_ms = 60,
985 	.sw_reset_ms = 60,
986 	.self_test_ms = 14,
987 };
988 
989 static const struct adis_timeout adis16362_timeouts = {
990 	.reset_ms = 130,
991 	.sw_reset_ms = 130,
992 	.self_test_ms = 12,
993 };
994 
995 static const struct adis_timeout adis16400_timeouts = {
996 	.reset_ms = 170,
997 	.sw_reset_ms = 170,
998 	.self_test_ms = 12,
999 };
1000 
1001 static const struct adis_timeout adis16445_timeouts = {
1002 	.reset_ms = 55,
1003 	.sw_reset_ms = 55,
1004 	.self_test_ms = 16,
1005 };
1006 
1007 static const struct adis_timeout adis16448_timeouts = {
1008 	.reset_ms = 90,
1009 	.sw_reset_ms = 90,
1010 	.self_test_ms = 45,
1011 };
1012 
1013 static struct adis16400_chip_info adis16400_chips[] = {
1014 	[ADIS16300] = {
1015 		.channels = adis16300_channels,
1016 		.num_channels = ARRAY_SIZE(adis16300_channels),
1017 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1018 				ADIS16400_HAS_SERIAL_NUMBER,
1019 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1020 		.accel_scale_micro = 5884,
1021 		.temp_scale_nano = 140000000, /* 0.14 C */
1022 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
1023 		.set_freq = adis16400_set_freq,
1024 		.get_freq = adis16400_get_freq,
1025 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 18),
1026 	},
1027 	[ADIS16334] = {
1028 		.channels = adis16334_channels,
1029 		.num_channels = ARRAY_SIZE(adis16334_channels),
1030 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
1031 				ADIS16400_HAS_SERIAL_NUMBER,
1032 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1033 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1034 		.temp_scale_nano = 67850000, /* 0.06785 C */
1035 		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
1036 		.set_freq = adis16334_set_freq,
1037 		.get_freq = adis16334_get_freq,
1038 		.adis_data = ADIS16400_DATA(&adis16334_timeouts, 0),
1039 	},
1040 	[ADIS16350] = {
1041 		.channels = adis16350_channels,
1042 		.num_channels = ARRAY_SIZE(adis16350_channels),
1043 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
1044 		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
1045 		.temp_scale_nano = 145300000, /* 0.1453 C */
1046 		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
1047 		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
1048 		.set_freq = adis16400_set_freq,
1049 		.get_freq = adis16400_get_freq,
1050 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 0),
1051 	},
1052 	[ADIS16360] = {
1053 		.channels = adis16350_channels,
1054 		.num_channels = ARRAY_SIZE(adis16350_channels),
1055 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1056 				ADIS16400_HAS_SERIAL_NUMBER,
1057 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1058 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1059 		.temp_scale_nano = 136000000, /* 0.136 C */
1060 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1061 		.set_freq = adis16400_set_freq,
1062 		.get_freq = adis16400_get_freq,
1063 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 28),
1064 	},
1065 	[ADIS16362] = {
1066 		.channels = adis16350_channels,
1067 		.num_channels = ARRAY_SIZE(adis16350_channels),
1068 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1069 				ADIS16400_HAS_SERIAL_NUMBER,
1070 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1071 		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
1072 		.temp_scale_nano = 136000000, /* 0.136 C */
1073 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1074 		.set_freq = adis16400_set_freq,
1075 		.get_freq = adis16400_get_freq,
1076 		.adis_data = ADIS16400_DATA(&adis16362_timeouts, 28),
1077 	},
1078 	[ADIS16364] = {
1079 		.channels = adis16350_channels,
1080 		.num_channels = ARRAY_SIZE(adis16350_channels),
1081 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1082 				ADIS16400_HAS_SERIAL_NUMBER,
1083 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1084 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1085 		.temp_scale_nano = 136000000, /* 0.136 C */
1086 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1087 		.set_freq = adis16400_set_freq,
1088 		.get_freq = adis16400_get_freq,
1089 		.adis_data = ADIS16400_DATA(&adis16362_timeouts, 28),
1090 	},
1091 	[ADIS16367] = {
1092 		.channels = adis16350_channels,
1093 		.num_channels = ARRAY_SIZE(adis16350_channels),
1094 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1095 				ADIS16400_HAS_SERIAL_NUMBER,
1096 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
1097 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1098 		.temp_scale_nano = 136000000, /* 0.136 C */
1099 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1100 		.set_freq = adis16400_set_freq,
1101 		.get_freq = adis16400_get_freq,
1102 		.adis_data = ADIS16400_DATA(&adis16300_timeouts, 28),
1103 	},
1104 	[ADIS16400] = {
1105 		.channels = adis16400_channels,
1106 		.num_channels = ARRAY_SIZE(adis16400_channels),
1107 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
1108 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1109 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1110 		.temp_scale_nano = 140000000, /* 0.14 C */
1111 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
1112 		.set_freq = adis16400_set_freq,
1113 		.get_freq = adis16400_get_freq,
1114 		.adis_data = ADIS16400_DATA(&adis16400_timeouts, 24),
1115 	},
1116 	[ADIS16445] = {
1117 		.channels = adis16445_channels,
1118 		.num_channels = ARRAY_SIZE(adis16445_channels),
1119 		.flags = ADIS16400_HAS_PROD_ID |
1120 				ADIS16400_HAS_SERIAL_NUMBER |
1121 				ADIS16400_BURST_DIAG_STAT,
1122 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
1123 		.accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
1124 		.temp_scale_nano = 73860000, /* 0.07386 C */
1125 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1126 		.set_freq = adis16334_set_freq,
1127 		.get_freq = adis16334_get_freq,
1128 		.adis_data = ADIS16400_DATA(&adis16445_timeouts, 16),
1129 	},
1130 	[ADIS16448] = {
1131 		.channels = adis16448_channels,
1132 		.num_channels = ARRAY_SIZE(adis16448_channels),
1133 		.flags = ADIS16400_HAS_PROD_ID |
1134 				ADIS16400_HAS_SERIAL_NUMBER |
1135 				ADIS16400_BURST_DIAG_STAT,
1136 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
1137 		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
1138 		.temp_scale_nano = 73860000, /* 0.07386 C */
1139 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1140 		.set_freq = adis16334_set_freq,
1141 		.get_freq = adis16334_get_freq,
1142 		.adis_data = ADIS16400_DATA(&adis16448_timeouts, 24),
1143 	}
1144 };
1145 
1146 static const struct iio_info adis16400_info = {
1147 	.read_raw = &adis16400_read_raw,
1148 	.write_raw = &adis16400_write_raw,
1149 	.update_scan_mode = adis_update_scan_mode,
1150 	.debugfs_reg_access = adis_debugfs_reg_access,
1151 };
1152 
1153 static void adis16400_setup_chan_mask(struct adis16400_state *st)
1154 {
1155 	const struct adis16400_chip_info *chip_info = st->variant;
1156 	unsigned int i;
1157 
1158 	for (i = 0; i < chip_info->num_channels; i++) {
1159 		const struct iio_chan_spec *ch = &chip_info->channels[i];
1160 
1161 		if (ch->scan_index >= 0 &&
1162 		    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
1163 			st->avail_scan_mask[0] |= BIT(ch->scan_index);
1164 	}
1165 }
1166 
1167 static void adis16400_stop(void *data)
1168 {
1169 	adis16400_stop_device(data);
1170 }
1171 
1172 static int adis16400_probe(struct spi_device *spi)
1173 {
1174 	struct adis16400_state *st;
1175 	struct iio_dev *indio_dev;
1176 	int ret;
1177 	const struct adis_data *adis16400_data;
1178 
1179 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1180 	if (indio_dev == NULL)
1181 		return -ENOMEM;
1182 
1183 	st = iio_priv(indio_dev);
1184 	/* this is only used for removal purposes */
1185 	spi_set_drvdata(spi, indio_dev);
1186 
1187 	/* setup the industrialio driver allocated elements */
1188 	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1189 	indio_dev->name = spi_get_device_id(spi)->name;
1190 	indio_dev->channels = st->variant->channels;
1191 	indio_dev->num_channels = st->variant->num_channels;
1192 	indio_dev->info = &adis16400_info;
1193 	indio_dev->modes = INDIO_DIRECT_MODE;
1194 
1195 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
1196 		adis16400_setup_chan_mask(st);
1197 		indio_dev->available_scan_masks = st->avail_scan_mask;
1198 	}
1199 
1200 	adis16400_data = &st->variant->adis_data;
1201 
1202 	ret = adis_init(&st->adis, indio_dev, spi, adis16400_data);
1203 	if (ret)
1204 		return ret;
1205 
1206 	ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, adis16400_trigger_handler);
1207 	if (ret)
1208 		return ret;
1209 
1210 	/* Get the device into a sane initial state */
1211 	ret = adis16400_initial_setup(indio_dev);
1212 	if (ret)
1213 		return ret;
1214 
1215 	ret = devm_add_action_or_reset(&spi->dev, adis16400_stop, indio_dev);
1216 	if (ret)
1217 		return ret;
1218 
1219 	ret = devm_iio_device_register(&spi->dev, indio_dev);
1220 	if (ret)
1221 		return ret;
1222 
1223 	adis16400_debugfs_init(indio_dev);
1224 	return 0;
1225 }
1226 
1227 static const struct spi_device_id adis16400_id[] = {
1228 	{"adis16300", ADIS16300},
1229 	{"adis16305", ADIS16300},
1230 	{"adis16334", ADIS16334},
1231 	{"adis16350", ADIS16350},
1232 	{"adis16354", ADIS16350},
1233 	{"adis16355", ADIS16350},
1234 	{"adis16360", ADIS16360},
1235 	{"adis16362", ADIS16362},
1236 	{"adis16364", ADIS16364},
1237 	{"adis16365", ADIS16360},
1238 	{"adis16367", ADIS16367},
1239 	{"adis16400", ADIS16400},
1240 	{"adis16405", ADIS16400},
1241 	{"adis16445", ADIS16445},
1242 	{"adis16448", ADIS16448},
1243 	{}
1244 };
1245 MODULE_DEVICE_TABLE(spi, adis16400_id);
1246 
1247 static struct spi_driver adis16400_driver = {
1248 	.driver = {
1249 		.name = "adis16400",
1250 	},
1251 	.id_table = adis16400_id,
1252 	.probe = adis16400_probe,
1253 };
1254 module_spi_driver(adis16400_driver);
1255 
1256 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1257 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1258 MODULE_LICENSE("GPL v2");
1259