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