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