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