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