Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / imu / adis16400_core.c
blob46a569005a138cb6a74ffe2d4f5f0910f6625fd2
1 /*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
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.
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.
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>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/sysfs.h>
33 #include <linux/iio/buffer.h>
35 #include "adis16400.h"
37 #ifdef CONFIG_DEBUG_FS
39 static ssize_t adis16400_show_serial_number(struct file *file,
40 char __user *userbuf, size_t count, loff_t *ppos)
42 struct adis16400_state *st = file->private_data;
43 u16 lot1, lot2, serial_number;
44 char buf[16];
45 size_t len;
46 int ret;
48 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
49 if (ret < 0)
50 return ret;
52 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
53 if (ret < 0)
54 return ret;
56 ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
57 &serial_number);
58 if (ret < 0)
59 return ret;
61 len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
62 serial_number);
64 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
67 static const struct file_operations adis16400_serial_number_fops = {
68 .open = simple_open,
69 .read = adis16400_show_serial_number,
70 .llseek = default_llseek,
71 .owner = THIS_MODULE,
74 static int adis16400_show_product_id(void *arg, u64 *val)
76 struct adis16400_state *st = arg;
77 uint16_t prod_id;
78 int ret;
80 ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
81 if (ret < 0)
82 return ret;
84 *val = prod_id;
86 return 0;
88 DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
89 adis16400_show_product_id, NULL, "%lld\n");
91 static int adis16400_show_flash_count(void *arg, u64 *val)
93 struct adis16400_state *st = arg;
94 uint16_t flash_count;
95 int ret;
97 ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
98 if (ret < 0)
99 return ret;
101 *val = flash_count;
103 return 0;
105 DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
106 adis16400_show_flash_count, NULL, "%lld\n");
108 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
110 struct adis16400_state *st = iio_priv(indio_dev);
112 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
113 debugfs_create_file("serial_number", 0400,
114 indio_dev->debugfs_dentry, st,
115 &adis16400_serial_number_fops);
116 if (st->variant->flags & ADIS16400_HAS_PROD_ID)
117 debugfs_create_file("product_id", 0400,
118 indio_dev->debugfs_dentry, st,
119 &adis16400_product_id_fops);
120 debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
121 st, &adis16400_flash_count_fops);
123 return 0;
126 #else
128 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
130 return 0;
133 #endif
135 enum adis16400_chip_variant {
136 ADIS16300,
137 ADIS16334,
138 ADIS16350,
139 ADIS16360,
140 ADIS16362,
141 ADIS16364,
142 ADIS16367,
143 ADIS16400,
144 ADIS16445,
145 ADIS16448,
148 static int adis16334_get_freq(struct adis16400_state *st)
150 int ret;
151 uint16_t t;
153 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
154 if (ret < 0)
155 return ret;
157 t >>= ADIS16334_RATE_DIV_SHIFT;
159 return 819200 >> t;
162 static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
164 unsigned int t;
166 if (freq < 819200)
167 t = ilog2(819200 / freq);
168 else
169 t = 0;
171 if (t > 0x31)
172 t = 0x31;
174 t <<= ADIS16334_RATE_DIV_SHIFT;
175 t |= ADIS16334_RATE_INT_CLK;
177 return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
180 static int adis16400_get_freq(struct adis16400_state *st)
182 int sps, ret;
183 uint16_t t;
185 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
186 if (ret < 0)
187 return ret;
189 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
190 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
192 return sps;
195 static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
197 unsigned int t;
198 uint8_t val = 0;
200 t = 1638404 / freq;
201 if (t >= 128) {
202 val |= ADIS16400_SMPL_PRD_TIME_BASE;
203 t = 52851 / freq;
204 if (t >= 128)
205 t = 127;
206 } else if (t != 0) {
207 t--;
210 val |= t;
212 if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
213 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
214 else
215 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
217 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
220 static const unsigned int adis16400_3db_divisors[] = {
221 [0] = 2, /* Special case */
222 [1] = 6,
223 [2] = 12,
224 [3] = 25,
225 [4] = 50,
226 [5] = 100,
227 [6] = 200,
228 [7] = 200, /* Not a valid setting */
231 static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
233 struct adis16400_state *st = iio_priv(indio_dev);
234 uint16_t val16;
235 int i, ret;
237 for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
238 if (sps / adis16400_3db_divisors[i] >= val)
239 break;
242 ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
243 if (ret < 0)
244 return ret;
246 ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
247 (val16 & ~0x07) | i);
248 return ret;
251 /* Power down the device */
252 static int adis16400_stop_device(struct iio_dev *indio_dev)
254 struct adis16400_state *st = iio_priv(indio_dev);
255 int ret;
257 ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
258 ADIS16400_SLP_CNT_POWER_OFF);
259 if (ret)
260 dev_err(&indio_dev->dev,
261 "problem with turning device off: SLP_CNT");
263 return ret;
266 static int adis16400_initial_setup(struct iio_dev *indio_dev)
268 struct adis16400_state *st = iio_priv(indio_dev);
269 uint16_t prod_id, smp_prd;
270 unsigned int device_id;
271 int ret;
273 /* use low spi speed for init if the device has a slow mode */
274 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
275 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
276 else
277 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
278 st->adis.spi->mode = SPI_MODE_3;
279 spi_setup(st->adis.spi);
281 ret = adis_initial_startup(&st->adis);
282 if (ret)
283 return ret;
285 if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
286 ret = adis_read_reg_16(&st->adis,
287 ADIS16400_PRODUCT_ID, &prod_id);
288 if (ret)
289 goto err_ret;
291 ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
292 if (ret != 1) {
293 ret = -EINVAL;
294 goto err_ret;
297 if (prod_id != device_id)
298 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
299 device_id, prod_id);
301 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
302 indio_dev->name, prod_id,
303 st->adis.spi->chip_select, st->adis.spi->irq);
305 /* use high spi speed if possible */
306 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
307 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
308 if (ret)
309 goto err_ret;
311 if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
312 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
313 spi_setup(st->adis.spi);
317 err_ret:
318 return ret;
321 static const uint8_t adis16400_addresses[] = {
322 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
323 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
324 [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
325 [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
326 [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
327 [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
330 static int adis16400_write_raw(struct iio_dev *indio_dev,
331 struct iio_chan_spec const *chan, int val, int val2, long info)
333 struct adis16400_state *st = iio_priv(indio_dev);
334 int ret, sps;
336 switch (info) {
337 case IIO_CHAN_INFO_CALIBBIAS:
338 mutex_lock(&indio_dev->mlock);
339 ret = adis_write_reg_16(&st->adis,
340 adis16400_addresses[chan->scan_index], val);
341 mutex_unlock(&indio_dev->mlock);
342 return ret;
343 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
345 * Need to cache values so we can update if the frequency
346 * changes.
348 mutex_lock(&indio_dev->mlock);
349 st->filt_int = val;
350 /* Work out update to current value */
351 sps = st->variant->get_freq(st);
352 if (sps < 0) {
353 mutex_unlock(&indio_dev->mlock);
354 return sps;
357 ret = adis16400_set_filter(indio_dev, sps,
358 val * 1000 + val2 / 1000);
359 mutex_unlock(&indio_dev->mlock);
360 return ret;
361 case IIO_CHAN_INFO_SAMP_FREQ:
362 sps = val * 1000 + val2 / 1000;
364 if (sps <= 0)
365 return -EINVAL;
367 mutex_lock(&indio_dev->mlock);
368 ret = st->variant->set_freq(st, sps);
369 mutex_unlock(&indio_dev->mlock);
370 return ret;
371 default:
372 return -EINVAL;
376 static int adis16400_read_raw(struct iio_dev *indio_dev,
377 struct iio_chan_spec const *chan, int *val, int *val2, long info)
379 struct adis16400_state *st = iio_priv(indio_dev);
380 int16_t val16;
381 int ret;
383 switch (info) {
384 case IIO_CHAN_INFO_RAW:
385 return adis_single_conversion(indio_dev, chan, 0, val);
386 case IIO_CHAN_INFO_SCALE:
387 switch (chan->type) {
388 case IIO_ANGL_VEL:
389 *val = 0;
390 *val2 = st->variant->gyro_scale_micro;
391 return IIO_VAL_INT_PLUS_MICRO;
392 case IIO_VOLTAGE:
393 *val = 0;
394 if (chan->channel == 0) {
395 *val = 2;
396 *val2 = 418000; /* 2.418 mV */
397 } else {
398 *val = 0;
399 *val2 = 805800; /* 805.8 uV */
401 return IIO_VAL_INT_PLUS_MICRO;
402 case IIO_ACCEL:
403 *val = 0;
404 *val2 = st->variant->accel_scale_micro;
405 return IIO_VAL_INT_PLUS_MICRO;
406 case IIO_MAGN:
407 *val = 0;
408 *val2 = 500; /* 0.5 mgauss */
409 return IIO_VAL_INT_PLUS_MICRO;
410 case IIO_TEMP:
411 *val = st->variant->temp_scale_nano / 1000000;
412 *val2 = (st->variant->temp_scale_nano % 1000000);
413 return IIO_VAL_INT_PLUS_MICRO;
414 case IIO_PRESSURE:
415 /* 20 uBar = 0.002kPascal */
416 *val = 0;
417 *val2 = 2000;
418 return IIO_VAL_INT_PLUS_MICRO;
419 default:
420 return -EINVAL;
422 case IIO_CHAN_INFO_CALIBBIAS:
423 mutex_lock(&indio_dev->mlock);
424 ret = adis_read_reg_16(&st->adis,
425 adis16400_addresses[chan->scan_index], &val16);
426 mutex_unlock(&indio_dev->mlock);
427 if (ret)
428 return ret;
429 val16 = sign_extend32(val16, 11);
430 *val = val16;
431 return IIO_VAL_INT;
432 case IIO_CHAN_INFO_OFFSET:
433 /* currently only temperature */
434 *val = st->variant->temp_offset;
435 return IIO_VAL_INT;
436 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
437 mutex_lock(&indio_dev->mlock);
438 /* Need both the number of taps and the sampling frequency */
439 ret = adis_read_reg_16(&st->adis,
440 ADIS16400_SENS_AVG,
441 &val16);
442 if (ret < 0) {
443 mutex_unlock(&indio_dev->mlock);
444 return ret;
446 ret = st->variant->get_freq(st);
447 if (ret >= 0) {
448 ret /= adis16400_3db_divisors[val16 & 0x07];
449 *val = ret / 1000;
450 *val2 = (ret % 1000) * 1000;
452 mutex_unlock(&indio_dev->mlock);
453 if (ret < 0)
454 return ret;
455 return IIO_VAL_INT_PLUS_MICRO;
456 case IIO_CHAN_INFO_SAMP_FREQ:
457 ret = st->variant->get_freq(st);
458 if (ret < 0)
459 return ret;
460 *val = ret / 1000;
461 *val2 = (ret % 1000) * 1000;
462 return IIO_VAL_INT_PLUS_MICRO;
463 default:
464 return -EINVAL;
468 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
469 .type = IIO_VOLTAGE, \
470 .indexed = 1, \
471 .channel = chn, \
472 .extend_name = name, \
473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
474 BIT(IIO_CHAN_INFO_SCALE), \
475 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
476 .address = (addr), \
477 .scan_index = (si), \
478 .scan_type = { \
479 .sign = 'u', \
480 .realbits = (bits), \
481 .storagebits = 16, \
482 .shift = 0, \
483 .endianness = IIO_BE, \
484 }, \
487 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
488 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
490 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
491 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
493 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
494 .type = IIO_ANGL_VEL, \
495 .modified = 1, \
496 .channel2 = IIO_MOD_ ## mod, \
497 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
498 BIT(IIO_CHAN_INFO_CALIBBIAS), \
499 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
500 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
501 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
502 .address = addr, \
503 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
504 .scan_type = { \
505 .sign = 's', \
506 .realbits = (bits), \
507 .storagebits = 16, \
508 .shift = 0, \
509 .endianness = IIO_BE, \
510 }, \
513 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
514 .type = IIO_ACCEL, \
515 .modified = 1, \
516 .channel2 = IIO_MOD_ ## mod, \
517 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
518 BIT(IIO_CHAN_INFO_CALIBBIAS), \
519 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
520 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
521 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
522 .address = (addr), \
523 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
524 .scan_type = { \
525 .sign = 's', \
526 .realbits = (bits), \
527 .storagebits = 16, \
528 .shift = 0, \
529 .endianness = IIO_BE, \
530 }, \
533 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
534 .type = IIO_MAGN, \
535 .modified = 1, \
536 .channel2 = IIO_MOD_ ## mod, \
537 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
538 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
539 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
540 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
541 .address = (addr), \
542 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
543 .scan_type = { \
544 .sign = 's', \
545 .realbits = (bits), \
546 .storagebits = 16, \
547 .shift = 0, \
548 .endianness = IIO_BE, \
549 }, \
552 #define ADIS16400_MOD_TEMP_NAME_X "x"
553 #define ADIS16400_MOD_TEMP_NAME_Y "y"
554 #define ADIS16400_MOD_TEMP_NAME_Z "z"
556 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
557 .type = IIO_TEMP, \
558 .indexed = 1, \
559 .channel = 0, \
560 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
561 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
562 BIT(IIO_CHAN_INFO_OFFSET) | \
563 BIT(IIO_CHAN_INFO_SCALE), \
564 .info_mask_shared_by_type = \
565 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
566 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
567 .address = (addr), \
568 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
569 .scan_type = { \
570 .sign = 's', \
571 .realbits = (bits), \
572 .storagebits = 16, \
573 .shift = 0, \
574 .endianness = IIO_BE, \
575 }, \
578 #define ADIS16400_TEMP_CHAN(addr, bits) { \
579 .type = IIO_TEMP, \
580 .indexed = 1, \
581 .channel = 0, \
582 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
583 BIT(IIO_CHAN_INFO_OFFSET) | \
584 BIT(IIO_CHAN_INFO_SCALE), \
585 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
586 .address = (addr), \
587 .scan_index = ADIS16350_SCAN_TEMP_X, \
588 .scan_type = { \
589 .sign = 's', \
590 .realbits = (bits), \
591 .storagebits = 16, \
592 .shift = 0, \
593 .endianness = IIO_BE, \
594 }, \
597 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
598 .type = IIO_INCLI, \
599 .modified = 1, \
600 .channel2 = IIO_MOD_ ## mod, \
601 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
602 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
603 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
604 .address = (addr), \
605 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
606 .scan_type = { \
607 .sign = 's', \
608 .realbits = (bits), \
609 .storagebits = 16, \
610 .shift = 0, \
611 .endianness = IIO_BE, \
612 }, \
615 static const struct iio_chan_spec adis16400_channels[] = {
616 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
617 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
618 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
619 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
620 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
621 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
622 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
623 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
624 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
625 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
626 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
627 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
628 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
631 static const struct iio_chan_spec adis16445_channels[] = {
632 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
633 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
634 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
635 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
636 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
637 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
638 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
639 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
642 static const struct iio_chan_spec adis16448_channels[] = {
643 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
644 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
645 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
646 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
647 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
648 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
649 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
650 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
651 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
653 .type = IIO_PRESSURE,
654 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
655 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
656 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
657 .address = ADIS16448_BARO_OUT,
658 .scan_index = ADIS16400_SCAN_BARO,
659 .scan_type = {
660 .sign = 's',
661 .realbits = 16,
662 .storagebits = 16,
663 .endianness = IIO_BE,
666 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
667 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
670 static const struct iio_chan_spec adis16350_channels[] = {
671 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
672 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
673 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
674 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
675 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
676 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
677 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
678 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
679 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
680 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
681 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
682 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
683 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
684 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
685 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
688 static const struct iio_chan_spec adis16300_channels[] = {
689 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
690 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
691 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
692 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
693 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
694 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
695 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
696 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
697 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
698 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
701 static const struct iio_chan_spec adis16334_channels[] = {
702 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
703 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
704 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
705 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
706 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
707 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
708 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
709 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP),
712 static struct adis16400_chip_info adis16400_chips[] = {
713 [ADIS16300] = {
714 .channels = adis16300_channels,
715 .num_channels = ARRAY_SIZE(adis16300_channels),
716 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
717 ADIS16400_HAS_SERIAL_NUMBER,
718 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
719 .accel_scale_micro = 5884,
720 .temp_scale_nano = 140000000, /* 0.14 C */
721 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
722 .set_freq = adis16400_set_freq,
723 .get_freq = adis16400_get_freq,
725 [ADIS16334] = {
726 .channels = adis16334_channels,
727 .num_channels = ARRAY_SIZE(adis16334_channels),
728 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
729 ADIS16400_HAS_SERIAL_NUMBER,
730 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
731 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
732 .temp_scale_nano = 67850000, /* 0.06785 C */
733 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
734 .set_freq = adis16334_set_freq,
735 .get_freq = adis16334_get_freq,
737 [ADIS16350] = {
738 .channels = adis16350_channels,
739 .num_channels = ARRAY_SIZE(adis16350_channels),
740 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
741 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
742 .temp_scale_nano = 145300000, /* 0.1453 C */
743 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
744 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
745 .set_freq = adis16400_set_freq,
746 .get_freq = adis16400_get_freq,
748 [ADIS16360] = {
749 .channels = adis16350_channels,
750 .num_channels = ARRAY_SIZE(adis16350_channels),
751 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
752 ADIS16400_HAS_SERIAL_NUMBER,
753 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
754 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
755 .temp_scale_nano = 136000000, /* 0.136 C */
756 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
757 .set_freq = adis16400_set_freq,
758 .get_freq = adis16400_get_freq,
760 [ADIS16362] = {
761 .channels = adis16350_channels,
762 .num_channels = ARRAY_SIZE(adis16350_channels),
763 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
764 ADIS16400_HAS_SERIAL_NUMBER,
765 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
766 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
767 .temp_scale_nano = 136000000, /* 0.136 C */
768 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
769 .set_freq = adis16400_set_freq,
770 .get_freq = adis16400_get_freq,
772 [ADIS16364] = {
773 .channels = adis16350_channels,
774 .num_channels = ARRAY_SIZE(adis16350_channels),
775 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
776 ADIS16400_HAS_SERIAL_NUMBER,
777 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
778 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
779 .temp_scale_nano = 136000000, /* 0.136 C */
780 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
781 .set_freq = adis16400_set_freq,
782 .get_freq = adis16400_get_freq,
784 [ADIS16367] = {
785 .channels = adis16350_channels,
786 .num_channels = ARRAY_SIZE(adis16350_channels),
787 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
788 ADIS16400_HAS_SERIAL_NUMBER,
789 .gyro_scale_micro = IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
790 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
791 .temp_scale_nano = 136000000, /* 0.136 C */
792 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
793 .set_freq = adis16400_set_freq,
794 .get_freq = adis16400_get_freq,
796 [ADIS16400] = {
797 .channels = adis16400_channels,
798 .num_channels = ARRAY_SIZE(adis16400_channels),
799 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
800 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
801 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
802 .temp_scale_nano = 140000000, /* 0.14 C */
803 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
804 .set_freq = adis16400_set_freq,
805 .get_freq = adis16400_get_freq,
807 [ADIS16445] = {
808 .channels = adis16445_channels,
809 .num_channels = ARRAY_SIZE(adis16445_channels),
810 .flags = ADIS16400_HAS_PROD_ID |
811 ADIS16400_HAS_SERIAL_NUMBER |
812 ADIS16400_BURST_DIAG_STAT,
813 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
814 .accel_scale_micro = IIO_G_TO_M_S_2(250), /* 1/4000 g */
815 .temp_scale_nano = 73860000, /* 0.07386 C */
816 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
817 .set_freq = adis16334_set_freq,
818 .get_freq = adis16334_get_freq,
820 [ADIS16448] = {
821 .channels = adis16448_channels,
822 .num_channels = ARRAY_SIZE(adis16448_channels),
823 .flags = ADIS16400_HAS_PROD_ID |
824 ADIS16400_HAS_SERIAL_NUMBER |
825 ADIS16400_BURST_DIAG_STAT,
826 .gyro_scale_micro = IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
827 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
828 .temp_scale_nano = 73860000, /* 0.07386 C */
829 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
830 .set_freq = adis16334_set_freq,
831 .get_freq = adis16334_get_freq,
835 static const struct iio_info adis16400_info = {
836 .read_raw = &adis16400_read_raw,
837 .write_raw = &adis16400_write_raw,
838 .update_scan_mode = adis16400_update_scan_mode,
839 .debugfs_reg_access = adis_debugfs_reg_access,
842 static const char * const adis16400_status_error_msgs[] = {
843 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
844 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
845 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
846 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
847 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
848 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
849 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
850 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
851 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
852 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
853 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
854 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
855 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
856 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
857 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
860 static const struct adis_data adis16400_data = {
861 .msc_ctrl_reg = ADIS16400_MSC_CTRL,
862 .glob_cmd_reg = ADIS16400_GLOB_CMD,
863 .diag_stat_reg = ADIS16400_DIAG_STAT,
865 .read_delay = 50,
866 .write_delay = 50,
868 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
869 .startup_delay = ADIS16400_STARTUP_DELAY,
871 .status_error_msgs = adis16400_status_error_msgs,
872 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
873 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
874 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
875 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
876 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
877 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
878 BIT(ADIS16400_DIAG_STAT_ALARM2) |
879 BIT(ADIS16400_DIAG_STAT_ALARM1) |
880 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
881 BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
882 BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
883 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
884 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
885 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
886 BIT(ADIS16400_DIAG_STAT_POWER_LOW),
889 static void adis16400_setup_chan_mask(struct adis16400_state *st)
891 const struct adis16400_chip_info *chip_info = st->variant;
892 unsigned int i;
894 for (i = 0; i < chip_info->num_channels; i++) {
895 const struct iio_chan_spec *ch = &chip_info->channels[i];
897 if (ch->scan_index >= 0 &&
898 ch->scan_index != ADIS16400_SCAN_TIMESTAMP)
899 st->avail_scan_mask[0] |= BIT(ch->scan_index);
903 static int adis16400_probe(struct spi_device *spi)
905 struct adis16400_state *st;
906 struct iio_dev *indio_dev;
907 int ret;
909 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
910 if (indio_dev == NULL)
911 return -ENOMEM;
913 st = iio_priv(indio_dev);
914 /* this is only used for removal purposes */
915 spi_set_drvdata(spi, indio_dev);
917 /* setup the industrialio driver allocated elements */
918 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
919 indio_dev->dev.parent = &spi->dev;
920 indio_dev->name = spi_get_device_id(spi)->name;
921 indio_dev->channels = st->variant->channels;
922 indio_dev->num_channels = st->variant->num_channels;
923 indio_dev->info = &adis16400_info;
924 indio_dev->modes = INDIO_DIRECT_MODE;
926 if (!(st->variant->flags & ADIS16400_NO_BURST)) {
927 adis16400_setup_chan_mask(st);
928 indio_dev->available_scan_masks = st->avail_scan_mask;
931 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
932 if (ret)
933 return ret;
935 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
936 adis16400_trigger_handler);
937 if (ret)
938 return ret;
940 /* Get the device into a sane initial state */
941 ret = adis16400_initial_setup(indio_dev);
942 if (ret)
943 goto error_cleanup_buffer;
944 ret = iio_device_register(indio_dev);
945 if (ret)
946 goto error_cleanup_buffer;
948 adis16400_debugfs_init(indio_dev);
949 return 0;
951 error_cleanup_buffer:
952 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
953 return ret;
956 static int adis16400_remove(struct spi_device *spi)
958 struct iio_dev *indio_dev = spi_get_drvdata(spi);
959 struct adis16400_state *st = iio_priv(indio_dev);
961 iio_device_unregister(indio_dev);
962 adis16400_stop_device(indio_dev);
964 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
966 return 0;
969 static const struct spi_device_id adis16400_id[] = {
970 {"adis16300", ADIS16300},
971 {"adis16305", ADIS16300},
972 {"adis16334", ADIS16334},
973 {"adis16350", ADIS16350},
974 {"adis16354", ADIS16350},
975 {"adis16355", ADIS16350},
976 {"adis16360", ADIS16360},
977 {"adis16362", ADIS16362},
978 {"adis16364", ADIS16364},
979 {"adis16365", ADIS16360},
980 {"adis16367", ADIS16367},
981 {"adis16400", ADIS16400},
982 {"adis16405", ADIS16400},
983 {"adis16445", ADIS16445},
984 {"adis16448", ADIS16448},
987 MODULE_DEVICE_TABLE(spi, adis16400_id);
989 static struct spi_driver adis16400_driver = {
990 .driver = {
991 .name = "adis16400",
993 .id_table = adis16400_id,
994 .probe = adis16400_probe,
995 .remove = adis16400_remove,
997 module_spi_driver(adis16400_driver);
999 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1000 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1001 MODULE_LICENSE("GPL v2");