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