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