xref: /openbmc/linux/drivers/iio/imu/adis16400.c (revision 7f877908)
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_timeout *timeouts;
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 struct adis_timeout adis16300_timeouts = {
934 	.reset_ms = ADIS16400_STARTUP_DELAY,
935 	.sw_reset_ms = ADIS16400_STARTUP_DELAY,
936 	.self_test_ms = ADIS16400_STARTUP_DELAY,
937 };
938 
939 static const struct adis_timeout adis16362_timeouts = {
940 	.reset_ms = 130,
941 	.sw_reset_ms = 130,
942 	.self_test_ms = 12,
943 };
944 
945 static const struct adis_timeout adis16400_timeouts = {
946 	.reset_ms = 170,
947 	.sw_reset_ms = 170,
948 	.self_test_ms = 12,
949 };
950 
951 static const struct adis_timeout adis16445_timeouts = {
952 	.reset_ms = 55,
953 	.sw_reset_ms = 55,
954 	.self_test_ms = 16,
955 };
956 
957 static const struct adis_timeout adis16448_timeouts = {
958 	.reset_ms = 90,
959 	.sw_reset_ms = 90,
960 	.self_test_ms = 45,
961 };
962 
963 static struct adis16400_chip_info adis16400_chips[] = {
964 	[ADIS16300] = {
965 		.channels = adis16300_channels,
966 		.num_channels = ARRAY_SIZE(adis16300_channels),
967 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
968 				ADIS16400_HAS_SERIAL_NUMBER,
969 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
970 		.accel_scale_micro = 5884,
971 		.temp_scale_nano = 140000000, /* 0.14 C */
972 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
973 		.set_freq = adis16400_set_freq,
974 		.get_freq = adis16400_get_freq,
975 		.timeouts = &adis16300_timeouts,
976 	},
977 	[ADIS16334] = {
978 		.channels = adis16334_channels,
979 		.num_channels = ARRAY_SIZE(adis16334_channels),
980 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
981 				ADIS16400_HAS_SERIAL_NUMBER,
982 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
983 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
984 		.temp_scale_nano = 67850000, /* 0.06785 C */
985 		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
986 		.set_freq = adis16334_set_freq,
987 		.get_freq = adis16334_get_freq,
988 	},
989 	[ADIS16350] = {
990 		.channels = adis16350_channels,
991 		.num_channels = ARRAY_SIZE(adis16350_channels),
992 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
993 		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
994 		.temp_scale_nano = 145300000, /* 0.1453 C */
995 		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
996 		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
997 		.set_freq = adis16400_set_freq,
998 		.get_freq = adis16400_get_freq,
999 		.timeouts = &adis16300_timeouts,
1000 	},
1001 	[ADIS16360] = {
1002 		.channels = adis16350_channels,
1003 		.num_channels = ARRAY_SIZE(adis16350_channels),
1004 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1005 				ADIS16400_HAS_SERIAL_NUMBER,
1006 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1007 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1008 		.temp_scale_nano = 136000000, /* 0.136 C */
1009 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1010 		.set_freq = adis16400_set_freq,
1011 		.get_freq = adis16400_get_freq,
1012 		.timeouts = &adis16300_timeouts,
1013 	},
1014 	[ADIS16362] = {
1015 		.channels = adis16350_channels,
1016 		.num_channels = ARRAY_SIZE(adis16350_channels),
1017 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1018 				ADIS16400_HAS_SERIAL_NUMBER,
1019 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1020 		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
1021 		.temp_scale_nano = 136000000, /* 0.136 C */
1022 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1023 		.set_freq = adis16400_set_freq,
1024 		.get_freq = adis16400_get_freq,
1025 		.timeouts = &adis16362_timeouts,
1026 	},
1027 	[ADIS16364] = {
1028 		.channels = adis16350_channels,
1029 		.num_channels = ARRAY_SIZE(adis16350_channels),
1030 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1031 				ADIS16400_HAS_SERIAL_NUMBER,
1032 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1033 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1034 		.temp_scale_nano = 136000000, /* 0.136 C */
1035 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1036 		.set_freq = adis16400_set_freq,
1037 		.get_freq = adis16400_get_freq,
1038 		.timeouts = &adis16362_timeouts,
1039 	},
1040 	[ADIS16367] = {
1041 		.channels = adis16350_channels,
1042 		.num_channels = ARRAY_SIZE(adis16350_channels),
1043 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1044 				ADIS16400_HAS_SERIAL_NUMBER,
1045 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
1046 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1047 		.temp_scale_nano = 136000000, /* 0.136 C */
1048 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1049 		.set_freq = adis16400_set_freq,
1050 		.get_freq = adis16400_get_freq,
1051 		.timeouts = &adis16300_timeouts,
1052 	},
1053 	[ADIS16400] = {
1054 		.channels = adis16400_channels,
1055 		.num_channels = ARRAY_SIZE(adis16400_channels),
1056 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
1057 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1058 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1059 		.temp_scale_nano = 140000000, /* 0.14 C */
1060 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
1061 		.set_freq = adis16400_set_freq,
1062 		.get_freq = adis16400_get_freq,
1063 		.timeouts = &adis16400_timeouts,
1064 	},
1065 	[ADIS16445] = {
1066 		.channels = adis16445_channels,
1067 		.num_channels = ARRAY_SIZE(adis16445_channels),
1068 		.flags = ADIS16400_HAS_PROD_ID |
1069 				ADIS16400_HAS_SERIAL_NUMBER |
1070 				ADIS16400_BURST_DIAG_STAT,
1071 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
1072 		.accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
1073 		.temp_scale_nano = 73860000, /* 0.07386 C */
1074 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1075 		.set_freq = adis16334_set_freq,
1076 		.get_freq = adis16334_get_freq,
1077 		.timeouts = &adis16445_timeouts,
1078 	},
1079 	[ADIS16448] = {
1080 		.channels = adis16448_channels,
1081 		.num_channels = ARRAY_SIZE(adis16448_channels),
1082 		.flags = ADIS16400_HAS_PROD_ID |
1083 				ADIS16400_HAS_SERIAL_NUMBER |
1084 				ADIS16400_BURST_DIAG_STAT,
1085 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
1086 		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
1087 		.temp_scale_nano = 73860000, /* 0.07386 C */
1088 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1089 		.set_freq = adis16334_set_freq,
1090 		.get_freq = adis16334_get_freq,
1091 		.timeouts = &adis16448_timeouts,
1092 	}
1093 };
1094 
1095 static const struct iio_info adis16400_info = {
1096 	.read_raw = &adis16400_read_raw,
1097 	.write_raw = &adis16400_write_raw,
1098 	.update_scan_mode = adis_update_scan_mode,
1099 	.debugfs_reg_access = adis_debugfs_reg_access,
1100 };
1101 
1102 static const char * const adis16400_status_error_msgs[] = {
1103 	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
1104 	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
1105 	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
1106 	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
1107 	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
1108 	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
1109 	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
1110 	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
1111 	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
1112 	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
1113 	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
1114 	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
1115 	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
1116 	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
1117 	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
1118 };
1119 
1120 static const struct adis_data adis16400_data = {
1121 	.msc_ctrl_reg = ADIS16400_MSC_CTRL,
1122 	.glob_cmd_reg = ADIS16400_GLOB_CMD,
1123 	.diag_stat_reg = ADIS16400_DIAG_STAT,
1124 
1125 	.read_delay = 50,
1126 	.write_delay = 50,
1127 
1128 	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
1129 
1130 	.status_error_msgs = adis16400_status_error_msgs,
1131 	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
1132 		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
1133 		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
1134 		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
1135 		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
1136 		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
1137 		BIT(ADIS16400_DIAG_STAT_ALARM2) |
1138 		BIT(ADIS16400_DIAG_STAT_ALARM1) |
1139 		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
1140 		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
1141 		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
1142 		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
1143 		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
1144 		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
1145 		BIT(ADIS16400_DIAG_STAT_POWER_LOW),
1146 };
1147 
1148 static void adis16400_setup_chan_mask(struct adis16400_state *st)
1149 {
1150 	const struct adis16400_chip_info *chip_info = st->variant;
1151 	unsigned int i;
1152 
1153 	for (i = 0; i < chip_info->num_channels; i++) {
1154 		const struct iio_chan_spec *ch = &chip_info->channels[i];
1155 
1156 		if (ch->scan_index >= 0 &&
1157 		    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
1158 			st->avail_scan_mask[0] |= BIT(ch->scan_index);
1159 	}
1160 }
1161 
1162 static struct adis_data *adis16400_adis_data_alloc(struct adis16400_state *st,
1163 						   struct device *dev)
1164 {
1165 	struct adis_data *data;
1166 
1167 	data = devm_kmalloc(dev, sizeof(struct adis_data), GFP_KERNEL);
1168 	if (!data)
1169 		return ERR_PTR(-ENOMEM);
1170 
1171 	memcpy(data, &adis16400_data, sizeof(*data));
1172 
1173 	data->timeouts = st->variant->timeouts;
1174 
1175 	return data;
1176 }
1177 
1178 static int adis16400_probe(struct spi_device *spi)
1179 {
1180 	struct adis16400_state *st;
1181 	struct iio_dev *indio_dev;
1182 	int ret;
1183 	const struct adis_data *adis16400_data;
1184 
1185 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1186 	if (indio_dev == NULL)
1187 		return -ENOMEM;
1188 
1189 	st = iio_priv(indio_dev);
1190 	/* this is only used for removal purposes */
1191 	spi_set_drvdata(spi, indio_dev);
1192 
1193 	/* setup the industrialio driver allocated elements */
1194 	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1195 	indio_dev->dev.parent = &spi->dev;
1196 	indio_dev->name = spi_get_device_id(spi)->name;
1197 	indio_dev->channels = st->variant->channels;
1198 	indio_dev->num_channels = st->variant->num_channels;
1199 	indio_dev->info = &adis16400_info;
1200 	indio_dev->modes = INDIO_DIRECT_MODE;
1201 
1202 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
1203 		adis16400_setup_chan_mask(st);
1204 		indio_dev->available_scan_masks = st->avail_scan_mask;
1205 		st->adis.burst = &adis16400_burst;
1206 		if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
1207 			st->adis.burst->extra_len = sizeof(u16);
1208 	}
1209 
1210 	adis16400_data = adis16400_adis_data_alloc(st, &spi->dev);
1211 	if (IS_ERR(adis16400_data))
1212 		return PTR_ERR(adis16400_data);
1213 
1214 	ret = adis_init(&st->adis, indio_dev, spi, adis16400_data);
1215 	if (ret)
1216 		return ret;
1217 
1218 	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
1219 			adis16400_trigger_handler);
1220 	if (ret)
1221 		return ret;
1222 
1223 	/* Get the device into a sane initial state */
1224 	ret = adis16400_initial_setup(indio_dev);
1225 	if (ret)
1226 		goto error_cleanup_buffer;
1227 	ret = iio_device_register(indio_dev);
1228 	if (ret)
1229 		goto error_cleanup_buffer;
1230 
1231 	adis16400_debugfs_init(indio_dev);
1232 	return 0;
1233 
1234 error_cleanup_buffer:
1235 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
1236 	return ret;
1237 }
1238 
1239 static int adis16400_remove(struct spi_device *spi)
1240 {
1241 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
1242 	struct adis16400_state *st = iio_priv(indio_dev);
1243 
1244 	iio_device_unregister(indio_dev);
1245 	adis16400_stop_device(indio_dev);
1246 
1247 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
1248 
1249 	return 0;
1250 }
1251 
1252 static const struct spi_device_id adis16400_id[] = {
1253 	{"adis16300", ADIS16300},
1254 	{"adis16305", ADIS16300},
1255 	{"adis16334", ADIS16334},
1256 	{"adis16350", ADIS16350},
1257 	{"adis16354", ADIS16350},
1258 	{"adis16355", ADIS16350},
1259 	{"adis16360", ADIS16360},
1260 	{"adis16362", ADIS16362},
1261 	{"adis16364", ADIS16364},
1262 	{"adis16365", ADIS16360},
1263 	{"adis16367", ADIS16367},
1264 	{"adis16400", ADIS16400},
1265 	{"adis16405", ADIS16400},
1266 	{"adis16445", ADIS16445},
1267 	{"adis16448", ADIS16448},
1268 	{}
1269 };
1270 MODULE_DEVICE_TABLE(spi, adis16400_id);
1271 
1272 static struct spi_driver adis16400_driver = {
1273 	.driver = {
1274 		.name = "adis16400",
1275 	},
1276 	.id_table = adis16400_id,
1277 	.probe = adis16400_probe,
1278 	.remove = adis16400_remove,
1279 };
1280 module_spi_driver(adis16400_driver);
1281 
1282 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1283 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1284 MODULE_LICENSE("GPL v2");
1285