xref: /openbmc/linux/drivers/iio/imu/adis16400.c (revision e7253313)
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 int num_channels;
160 	const long flags;
161 	unsigned int gyro_scale_micro;
162 	unsigned int accel_scale_micro;
163 	int temp_scale_nano;
164 	int temp_offset;
165 	int (*set_freq)(struct adis16400_state *st, unsigned int freq);
166 	int (*get_freq)(struct adis16400_state *st);
167 };
168 
169 /**
170  * struct adis16400_state - device instance specific data
171  * @variant:	chip variant info
172  * @filt_int:	integer part of requested filter frequency
173  * @adis:	adis device
174  **/
175 struct adis16400_state {
176 	struct adis16400_chip_info	*variant;
177 	int				filt_int;
178 
179 	struct adis adis;
180 	unsigned long avail_scan_mask[2];
181 };
182 
183 /* At the moment triggers are only used for ring buffer
184  * filling. This may change!
185  */
186 
187 enum {
188 	ADIS16400_SCAN_SUPPLY,
189 	ADIS16400_SCAN_GYRO_X,
190 	ADIS16400_SCAN_GYRO_Y,
191 	ADIS16400_SCAN_GYRO_Z,
192 	ADIS16400_SCAN_ACC_X,
193 	ADIS16400_SCAN_ACC_Y,
194 	ADIS16400_SCAN_ACC_Z,
195 	ADIS16400_SCAN_MAGN_X,
196 	ADIS16400_SCAN_MAGN_Y,
197 	ADIS16400_SCAN_MAGN_Z,
198 	ADIS16400_SCAN_BARO,
199 	ADIS16350_SCAN_TEMP_X,
200 	ADIS16350_SCAN_TEMP_Y,
201 	ADIS16350_SCAN_TEMP_Z,
202 	ADIS16300_SCAN_INCLI_X,
203 	ADIS16300_SCAN_INCLI_Y,
204 	ADIS16400_SCAN_ADC,
205 	ADIS16400_SCAN_TIMESTAMP,
206 };
207 
208 #ifdef CONFIG_DEBUG_FS
209 
210 static ssize_t adis16400_show_serial_number(struct file *file,
211 		char __user *userbuf, size_t count, loff_t *ppos)
212 {
213 	struct adis16400_state *st = file->private_data;
214 	u16 lot1, lot2, serial_number;
215 	char buf[16];
216 	size_t len;
217 	int ret;
218 
219 	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
220 	if (ret)
221 		return ret;
222 
223 	ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
224 	if (ret)
225 		return ret;
226 
227 	ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
228 			&serial_number);
229 	if (ret)
230 		return ret;
231 
232 	len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
233 			serial_number);
234 
235 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
236 }
237 
238 static const struct file_operations adis16400_serial_number_fops = {
239 	.open = simple_open,
240 	.read = adis16400_show_serial_number,
241 	.llseek = default_llseek,
242 	.owner = THIS_MODULE,
243 };
244 
245 static int adis16400_show_product_id(void *arg, u64 *val)
246 {
247 	struct adis16400_state *st = arg;
248 	uint16_t prod_id;
249 	int ret;
250 
251 	ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
252 	if (ret)
253 		return ret;
254 
255 	*val = prod_id;
256 
257 	return 0;
258 }
259 DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
260 	adis16400_show_product_id, NULL, "%lld\n");
261 
262 static int adis16400_show_flash_count(void *arg, u64 *val)
263 {
264 	struct adis16400_state *st = arg;
265 	uint16_t flash_count;
266 	int ret;
267 
268 	ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
269 	if (ret)
270 		return ret;
271 
272 	*val = flash_count;
273 
274 	return 0;
275 }
276 DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
277 	adis16400_show_flash_count, NULL, "%lld\n");
278 
279 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
280 {
281 	struct adis16400_state *st = iio_priv(indio_dev);
282 
283 	if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
284 		debugfs_create_file("serial_number", 0400,
285 			indio_dev->debugfs_dentry, st,
286 			&adis16400_serial_number_fops);
287 	if (st->variant->flags & ADIS16400_HAS_PROD_ID)
288 		debugfs_create_file("product_id", 0400,
289 			indio_dev->debugfs_dentry, st,
290 			&adis16400_product_id_fops);
291 	debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
292 		st, &adis16400_flash_count_fops);
293 
294 	return 0;
295 }
296 
297 #else
298 
299 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
300 {
301 	return 0;
302 }
303 
304 #endif
305 
306 enum adis16400_chip_variant {
307 	ADIS16300,
308 	ADIS16334,
309 	ADIS16350,
310 	ADIS16360,
311 	ADIS16362,
312 	ADIS16364,
313 	ADIS16367,
314 	ADIS16400,
315 	ADIS16445,
316 	ADIS16448,
317 };
318 
319 static struct adis_burst adis16400_burst = {
320 	.en = true,
321 	.reg_cmd = ADIS16400_GLOB_CMD,
322 };
323 
324 static int adis16334_get_freq(struct adis16400_state *st)
325 {
326 	int ret;
327 	uint16_t t;
328 
329 	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
330 	if (ret)
331 		return ret;
332 
333 	t >>= ADIS16334_RATE_DIV_SHIFT;
334 
335 	return 819200 >> t;
336 }
337 
338 static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
339 {
340 	unsigned int t;
341 
342 	if (freq < 819200)
343 		t = ilog2(819200 / freq);
344 	else
345 		t = 0;
346 
347 	if (t > 0x31)
348 		t = 0x31;
349 
350 	t <<= ADIS16334_RATE_DIV_SHIFT;
351 	t |= ADIS16334_RATE_INT_CLK;
352 
353 	return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
354 }
355 
356 static int adis16400_get_freq(struct adis16400_state *st)
357 {
358 	int sps, ret;
359 	uint16_t t;
360 
361 	ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
362 	if (ret)
363 		return ret;
364 
365 	sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
366 	sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
367 
368 	return sps;
369 }
370 
371 static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
372 {
373 	unsigned int t;
374 	uint8_t val = 0;
375 
376 	t = 1638404 / freq;
377 	if (t >= 128) {
378 		val |= ADIS16400_SMPL_PRD_TIME_BASE;
379 		t = 52851 / freq;
380 		if (t >= 128)
381 			t = 127;
382 	} else if (t != 0) {
383 		t--;
384 	}
385 
386 	val |= t;
387 
388 	if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
389 		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
390 	else
391 		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
392 
393 	return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
394 }
395 
396 static const unsigned int adis16400_3db_divisors[] = {
397 	[0] = 2, /* Special case */
398 	[1] = 6,
399 	[2] = 12,
400 	[3] = 25,
401 	[4] = 50,
402 	[5] = 100,
403 	[6] = 200,
404 	[7] = 200, /* Not a valid setting */
405 };
406 
407 static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
408 {
409 	struct adis16400_state *st = iio_priv(indio_dev);
410 	uint16_t val16;
411 	int i, ret;
412 
413 	for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
414 		if (sps / adis16400_3db_divisors[i] >= val)
415 			break;
416 	}
417 
418 	ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
419 	if (ret)
420 		return ret;
421 
422 	ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
423 					 (val16 & ~0x07) | i);
424 	return ret;
425 }
426 
427 /* Power down the device */
428 static int adis16400_stop_device(struct iio_dev *indio_dev)
429 {
430 	struct adis16400_state *st = iio_priv(indio_dev);
431 	int ret;
432 
433 	ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
434 			ADIS16400_SLP_CNT_POWER_OFF);
435 	if (ret)
436 		dev_err(&indio_dev->dev,
437 			"problem with turning device off: SLP_CNT");
438 
439 	return ret;
440 }
441 
442 static int adis16400_initial_setup(struct iio_dev *indio_dev)
443 {
444 	struct adis16400_state *st = iio_priv(indio_dev);
445 	uint16_t prod_id, smp_prd;
446 	unsigned int device_id;
447 	int ret;
448 
449 	/* use low spi speed for init if the device has a slow mode */
450 	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
451 		st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
452 	else
453 		st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
454 	st->adis.spi->mode = SPI_MODE_3;
455 	spi_setup(st->adis.spi);
456 
457 	ret = adis_initial_startup(&st->adis);
458 	if (ret)
459 		return ret;
460 
461 	if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
462 		ret = adis_read_reg_16(&st->adis,
463 						ADIS16400_PRODUCT_ID, &prod_id);
464 		if (ret)
465 			goto err_ret;
466 
467 		ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
468 		if (ret != 1) {
469 			ret = -EINVAL;
470 			goto err_ret;
471 		}
472 
473 		if (prod_id != device_id)
474 			dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
475 					device_id, prod_id);
476 
477 		dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
478 			indio_dev->name, prod_id,
479 			st->adis.spi->chip_select, st->adis.spi->irq);
480 	}
481 	/* use high spi speed if possible */
482 	if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
483 		ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
484 		if (ret)
485 			goto err_ret;
486 
487 		if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
488 			st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
489 			spi_setup(st->adis.spi);
490 		}
491 	}
492 
493 err_ret:
494 	return ret;
495 }
496 
497 static const uint8_t adis16400_addresses[] = {
498 	[ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
499 	[ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
500 	[ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
501 	[ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
502 	[ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
503 	[ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
504 };
505 
506 static int adis16400_write_raw(struct iio_dev *indio_dev,
507 	struct iio_chan_spec const *chan, int val, int val2, long info)
508 {
509 	struct adis16400_state *st = iio_priv(indio_dev);
510 	int ret, sps;
511 
512 	switch (info) {
513 	case IIO_CHAN_INFO_CALIBBIAS:
514 		mutex_lock(&indio_dev->mlock);
515 		ret = adis_write_reg_16(&st->adis,
516 				adis16400_addresses[chan->scan_index], val);
517 		mutex_unlock(&indio_dev->mlock);
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(&indio_dev->mlock);
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(&indio_dev->mlock);
530 			return sps;
531 		}
532 
533 		ret = adis16400_set_filter(indio_dev, sps,
534 			val * 1000 + val2 / 1000);
535 		mutex_unlock(&indio_dev->mlock);
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(&indio_dev->mlock);
544 		ret = st->variant->set_freq(st, sps);
545 		mutex_unlock(&indio_dev->mlock);
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 	int16_t val16;
557 	int ret;
558 
559 	switch (info) {
560 	case IIO_CHAN_INFO_RAW:
561 		return adis_single_conversion(indio_dev, chan, 0, val);
562 	case IIO_CHAN_INFO_SCALE:
563 		switch (chan->type) {
564 		case IIO_ANGL_VEL:
565 			*val = 0;
566 			*val2 = st->variant->gyro_scale_micro;
567 			return IIO_VAL_INT_PLUS_MICRO;
568 		case IIO_VOLTAGE:
569 			*val = 0;
570 			if (chan->channel == 0) {
571 				*val = 2;
572 				*val2 = 418000; /* 2.418 mV */
573 			} else {
574 				*val = 0;
575 				*val2 = 805800; /* 805.8 uV */
576 			}
577 			return IIO_VAL_INT_PLUS_MICRO;
578 		case IIO_ACCEL:
579 			*val = 0;
580 			*val2 = st->variant->accel_scale_micro;
581 			return IIO_VAL_INT_PLUS_MICRO;
582 		case IIO_MAGN:
583 			*val = 0;
584 			*val2 = 500; /* 0.5 mgauss */
585 			return IIO_VAL_INT_PLUS_MICRO;
586 		case IIO_TEMP:
587 			*val = st->variant->temp_scale_nano / 1000000;
588 			*val2 = (st->variant->temp_scale_nano % 1000000);
589 			return IIO_VAL_INT_PLUS_MICRO;
590 		case IIO_PRESSURE:
591 			/* 20 uBar = 0.002kPascal */
592 			*val = 0;
593 			*val2 = 2000;
594 			return IIO_VAL_INT_PLUS_MICRO;
595 		default:
596 			return -EINVAL;
597 		}
598 	case IIO_CHAN_INFO_CALIBBIAS:
599 		mutex_lock(&indio_dev->mlock);
600 		ret = adis_read_reg_16(&st->adis,
601 				adis16400_addresses[chan->scan_index], &val16);
602 		mutex_unlock(&indio_dev->mlock);
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(&indio_dev->mlock);
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(&indio_dev->mlock);
620 			return ret;
621 		}
622 		ret = st->variant->get_freq(st);
623 		if (ret >= 0) {
624 			ret /= adis16400_3db_divisors[val16 & 0x07];
625 			*val = ret / 1000;
626 			*val2 = (ret % 1000) * 1000;
627 		}
628 		mutex_unlock(&indio_dev->mlock);
629 		if (ret)
630 			return ret;
631 		return IIO_VAL_INT_PLUS_MICRO;
632 	case IIO_CHAN_INFO_SAMP_FREQ:
633 		ret = st->variant->get_freq(st);
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 struct adis16400_chip_info adis16400_chips[] = {
934 	[ADIS16300] = {
935 		.channels = adis16300_channels,
936 		.num_channels = ARRAY_SIZE(adis16300_channels),
937 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
938 				ADIS16400_HAS_SERIAL_NUMBER,
939 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
940 		.accel_scale_micro = 5884,
941 		.temp_scale_nano = 140000000, /* 0.14 C */
942 		.temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
943 		.set_freq = adis16400_set_freq,
944 		.get_freq = adis16400_get_freq,
945 	},
946 	[ADIS16334] = {
947 		.channels = adis16334_channels,
948 		.num_channels = ARRAY_SIZE(adis16334_channels),
949 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
950 				ADIS16400_HAS_SERIAL_NUMBER,
951 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
952 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
953 		.temp_scale_nano = 67850000, /* 0.06785 C */
954 		.temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
955 		.set_freq = adis16334_set_freq,
956 		.get_freq = adis16334_get_freq,
957 	},
958 	[ADIS16350] = {
959 		.channels = adis16350_channels,
960 		.num_channels = ARRAY_SIZE(adis16350_channels),
961 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
962 		.accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
963 		.temp_scale_nano = 145300000, /* 0.1453 C */
964 		.temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
965 		.flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
966 		.set_freq = adis16400_set_freq,
967 		.get_freq = adis16400_get_freq,
968 	},
969 	[ADIS16360] = {
970 		.channels = adis16350_channels,
971 		.num_channels = ARRAY_SIZE(adis16350_channels),
972 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
973 				ADIS16400_HAS_SERIAL_NUMBER,
974 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
975 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
976 		.temp_scale_nano = 136000000, /* 0.136 C */
977 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
978 		.set_freq = adis16400_set_freq,
979 		.get_freq = adis16400_get_freq,
980 	},
981 	[ADIS16362] = {
982 		.channels = adis16350_channels,
983 		.num_channels = ARRAY_SIZE(adis16350_channels),
984 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
985 				ADIS16400_HAS_SERIAL_NUMBER,
986 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
987 		.accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
988 		.temp_scale_nano = 136000000, /* 0.136 C */
989 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
990 		.set_freq = adis16400_set_freq,
991 		.get_freq = adis16400_get_freq,
992 	},
993 	[ADIS16364] = {
994 		.channels = adis16350_channels,
995 		.num_channels = ARRAY_SIZE(adis16350_channels),
996 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
997 				ADIS16400_HAS_SERIAL_NUMBER,
998 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
999 		.accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
1000 		.temp_scale_nano = 136000000, /* 0.136 C */
1001 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1002 		.set_freq = adis16400_set_freq,
1003 		.get_freq = adis16400_get_freq,
1004 	},
1005 	[ADIS16367] = {
1006 		.channels = adis16350_channels,
1007 		.num_channels = ARRAY_SIZE(adis16350_channels),
1008 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
1009 				ADIS16400_HAS_SERIAL_NUMBER,
1010 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
1011 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
1012 		.temp_scale_nano = 136000000, /* 0.136 C */
1013 		.temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
1014 		.set_freq = adis16400_set_freq,
1015 		.get_freq = adis16400_get_freq,
1016 	},
1017 	[ADIS16400] = {
1018 		.channels = adis16400_channels,
1019 		.num_channels = ARRAY_SIZE(adis16400_channels),
1020 		.flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
1021 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
1022 		.accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
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 	},
1028 	[ADIS16445] = {
1029 		.channels = adis16445_channels,
1030 		.num_channels = ARRAY_SIZE(adis16445_channels),
1031 		.flags = ADIS16400_HAS_PROD_ID |
1032 				ADIS16400_HAS_SERIAL_NUMBER |
1033 				ADIS16400_BURST_DIAG_STAT,
1034 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
1035 		.accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
1036 		.temp_scale_nano = 73860000, /* 0.07386 C */
1037 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1038 		.set_freq = adis16334_set_freq,
1039 		.get_freq = adis16334_get_freq,
1040 	},
1041 	[ADIS16448] = {
1042 		.channels = adis16448_channels,
1043 		.num_channels = ARRAY_SIZE(adis16448_channels),
1044 		.flags = ADIS16400_HAS_PROD_ID |
1045 				ADIS16400_HAS_SERIAL_NUMBER |
1046 				ADIS16400_BURST_DIAG_STAT,
1047 		.gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
1048 		.accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
1049 		.temp_scale_nano = 73860000, /* 0.07386 C */
1050 		.temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
1051 		.set_freq = adis16334_set_freq,
1052 		.get_freq = adis16334_get_freq,
1053 	}
1054 };
1055 
1056 static const struct iio_info adis16400_info = {
1057 	.read_raw = &adis16400_read_raw,
1058 	.write_raw = &adis16400_write_raw,
1059 	.update_scan_mode = adis_update_scan_mode,
1060 	.debugfs_reg_access = adis_debugfs_reg_access,
1061 };
1062 
1063 static const char * const adis16400_status_error_msgs[] = {
1064 	[ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
1065 	[ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
1066 	[ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
1067 	[ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
1068 	[ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
1069 	[ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
1070 	[ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
1071 	[ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
1072 	[ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
1073 	[ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
1074 	[ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
1075 	[ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
1076 	[ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
1077 	[ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
1078 	[ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
1079 };
1080 
1081 static const struct adis_data adis16400_data = {
1082 	.msc_ctrl_reg = ADIS16400_MSC_CTRL,
1083 	.glob_cmd_reg = ADIS16400_GLOB_CMD,
1084 	.diag_stat_reg = ADIS16400_DIAG_STAT,
1085 
1086 	.read_delay = 50,
1087 	.write_delay = 50,
1088 
1089 	.self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
1090 	.startup_delay = ADIS16400_STARTUP_DELAY,
1091 
1092 	.status_error_msgs = adis16400_status_error_msgs,
1093 	.status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
1094 		BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
1095 		BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
1096 		BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
1097 		BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
1098 		BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
1099 		BIT(ADIS16400_DIAG_STAT_ALARM2) |
1100 		BIT(ADIS16400_DIAG_STAT_ALARM1) |
1101 		BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
1102 		BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
1103 		BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
1104 		BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
1105 		BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
1106 		BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
1107 		BIT(ADIS16400_DIAG_STAT_POWER_LOW),
1108 };
1109 
1110 static void adis16400_setup_chan_mask(struct adis16400_state *st)
1111 {
1112 	const struct adis16400_chip_info *chip_info = st->variant;
1113 	unsigned int i;
1114 
1115 	for (i = 0; i < chip_info->num_channels; i++) {
1116 		const struct iio_chan_spec *ch = &chip_info->channels[i];
1117 
1118 		if (ch->scan_index >= 0 &&
1119 		    ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
1120 			st->avail_scan_mask[0] |= BIT(ch->scan_index);
1121 	}
1122 }
1123 
1124 static int adis16400_probe(struct spi_device *spi)
1125 {
1126 	struct adis16400_state *st;
1127 	struct iio_dev *indio_dev;
1128 	int ret;
1129 
1130 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1131 	if (indio_dev == NULL)
1132 		return -ENOMEM;
1133 
1134 	st = iio_priv(indio_dev);
1135 	/* this is only used for removal purposes */
1136 	spi_set_drvdata(spi, indio_dev);
1137 
1138 	/* setup the industrialio driver allocated elements */
1139 	st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1140 	indio_dev->dev.parent = &spi->dev;
1141 	indio_dev->name = spi_get_device_id(spi)->name;
1142 	indio_dev->channels = st->variant->channels;
1143 	indio_dev->num_channels = st->variant->num_channels;
1144 	indio_dev->info = &adis16400_info;
1145 	indio_dev->modes = INDIO_DIRECT_MODE;
1146 
1147 	if (!(st->variant->flags & ADIS16400_NO_BURST)) {
1148 		adis16400_setup_chan_mask(st);
1149 		indio_dev->available_scan_masks = st->avail_scan_mask;
1150 		st->adis.burst = &adis16400_burst;
1151 		if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
1152 			st->adis.burst->extra_len = sizeof(u16);
1153 	}
1154 
1155 	ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
1156 	if (ret)
1157 		return ret;
1158 
1159 	ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
1160 			adis16400_trigger_handler);
1161 	if (ret)
1162 		return ret;
1163 
1164 	/* Get the device into a sane initial state */
1165 	ret = adis16400_initial_setup(indio_dev);
1166 	if (ret)
1167 		goto error_cleanup_buffer;
1168 	ret = iio_device_register(indio_dev);
1169 	if (ret)
1170 		goto error_cleanup_buffer;
1171 
1172 	adis16400_debugfs_init(indio_dev);
1173 	return 0;
1174 
1175 error_cleanup_buffer:
1176 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
1177 	return ret;
1178 }
1179 
1180 static int adis16400_remove(struct spi_device *spi)
1181 {
1182 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
1183 	struct adis16400_state *st = iio_priv(indio_dev);
1184 
1185 	iio_device_unregister(indio_dev);
1186 	adis16400_stop_device(indio_dev);
1187 
1188 	adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
1189 
1190 	return 0;
1191 }
1192 
1193 static const struct spi_device_id adis16400_id[] = {
1194 	{"adis16300", ADIS16300},
1195 	{"adis16305", ADIS16300},
1196 	{"adis16334", ADIS16334},
1197 	{"adis16350", ADIS16350},
1198 	{"adis16354", ADIS16350},
1199 	{"adis16355", ADIS16350},
1200 	{"adis16360", ADIS16360},
1201 	{"adis16362", ADIS16362},
1202 	{"adis16364", ADIS16364},
1203 	{"adis16365", ADIS16360},
1204 	{"adis16367", ADIS16367},
1205 	{"adis16400", ADIS16400},
1206 	{"adis16405", ADIS16400},
1207 	{"adis16445", ADIS16445},
1208 	{"adis16448", ADIS16448},
1209 	{}
1210 };
1211 MODULE_DEVICE_TABLE(spi, adis16400_id);
1212 
1213 static struct spi_driver adis16400_driver = {
1214 	.driver = {
1215 		.name = "adis16400",
1216 	},
1217 	.id_table = adis16400_id,
1218 	.probe = adis16400_probe,
1219 	.remove = adis16400_remove,
1220 };
1221 module_spi_driver(adis16400_driver);
1222 
1223 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1224 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1225 MODULE_LICENSE("GPL v2");
1226