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