2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
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
;
48 ret
= adis_read_reg_16(&st
->adis
, ADIS16334_LOT_ID1
, &lot1
);
52 ret
= adis_read_reg_16(&st
->adis
, ADIS16334_LOT_ID2
, &lot2
);
56 ret
= adis_read_reg_16(&st
->adis
, ADIS16334_SERIAL_NUMBER
,
61 len
= snprintf(buf
, sizeof(buf
), "%.4x-%.4x-%.4x\n", lot1
, lot2
,
64 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
67 static const struct file_operations adis16400_serial_number_fops
= {
69 .read
= adis16400_show_serial_number
,
70 .llseek
= default_llseek
,
74 static int adis16400_show_product_id(void *arg
, u64
*val
)
76 struct adis16400_state
*st
= arg
;
80 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_PRODUCT_ID
, &prod_id
);
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
;
97 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_FLASH_CNT
, &flash_count
);
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
);
128 static int adis16400_debugfs_init(struct iio_dev
*indio_dev
)
135 enum adis16400_chip_variant
{
148 static int adis16334_get_freq(struct adis16400_state
*st
)
153 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &t
);
157 t
>>= ADIS16334_RATE_DIV_SHIFT
;
162 static int adis16334_set_freq(struct adis16400_state
*st
, unsigned int freq
)
167 t
= ilog2(819200 / freq
);
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
)
185 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &t
);
189 sps
= (t
& ADIS16400_SMPL_PRD_TIME_BASE
) ? 52851 : 1638404;
190 sps
/= (t
& ADIS16400_SMPL_PRD_DIV_MASK
) + 1;
195 static int adis16400_set_freq(struct adis16400_state
*st
, unsigned int freq
)
202 val
|= ADIS16400_SMPL_PRD_TIME_BASE
;
212 if (t
>= 0x0A || (val
& ADIS16400_SMPL_PRD_TIME_BASE
))
213 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_SLOW
;
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 */
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
);
237 for (i
= ARRAY_SIZE(adis16400_3db_divisors
) - 1; i
>= 1; i
--) {
238 if (sps
/ adis16400_3db_divisors
[i
] >= val
)
242 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SENS_AVG
, &val16
);
246 ret
= adis_write_reg_16(&st
->adis
, ADIS16400_SENS_AVG
,
247 (val16
& ~0x07) | i
);
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
);
257 ret
= adis_write_reg_16(&st
->adis
, ADIS16400_SLP_CNT
,
258 ADIS16400_SLP_CNT_POWER_OFF
);
260 dev_err(&indio_dev
->dev
,
261 "problem with turning device off: SLP_CNT");
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
;
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
;
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
);
285 if (st
->variant
->flags
& ADIS16400_HAS_PROD_ID
) {
286 ret
= adis_read_reg_16(&st
->adis
,
287 ADIS16400_PRODUCT_ID
, &prod_id
);
291 ret
= sscanf(indio_dev
->name
, "adis%u\n", &device_id
);
297 if (prod_id
!= device_id
)
298 dev_warn(&indio_dev
->dev
, "Device ID(%u) and product ID(%u) do not match.",
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
);
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
);
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
);
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
);
343 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
345 * Need to cache values so we can update if the frequency
348 mutex_lock(&indio_dev
->mlock
);
350 /* Work out update to current value */
351 sps
= st
->variant
->get_freq(st
);
353 mutex_unlock(&indio_dev
->mlock
);
357 ret
= adis16400_set_filter(indio_dev
, sps
,
358 val
* 1000 + val2
/ 1000);
359 mutex_unlock(&indio_dev
->mlock
);
361 case IIO_CHAN_INFO_SAMP_FREQ
:
362 sps
= val
* 1000 + val2
/ 1000;
367 mutex_lock(&indio_dev
->mlock
);
368 ret
= st
->variant
->set_freq(st
, sps
);
369 mutex_unlock(&indio_dev
->mlock
);
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
);
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
) {
390 *val2
= st
->variant
->gyro_scale_micro
;
391 return IIO_VAL_INT_PLUS_MICRO
;
394 if (chan
->channel
== 0) {
396 *val2
= 418000; /* 2.418 mV */
399 *val2
= 805800; /* 805.8 uV */
401 return IIO_VAL_INT_PLUS_MICRO
;
404 *val2
= st
->variant
->accel_scale_micro
;
405 return IIO_VAL_INT_PLUS_MICRO
;
408 *val2
= 500; /* 0.5 mgauss */
409 return IIO_VAL_INT_PLUS_MICRO
;
411 *val
= st
->variant
->temp_scale_nano
/ 1000000;
412 *val2
= (st
->variant
->temp_scale_nano
% 1000000);
413 return IIO_VAL_INT_PLUS_MICRO
;
415 /* 20 uBar = 0.002kPascal */
418 return IIO_VAL_INT_PLUS_MICRO
;
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
);
429 val16
= sign_extend32(val16
, 11);
432 case IIO_CHAN_INFO_OFFSET
:
433 /* currently only temperature */
434 *val
= st
->variant
->temp_offset
;
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
,
443 mutex_unlock(&indio_dev
->mlock
);
446 ret
= st
->variant
->get_freq(st
);
448 ret
/= adis16400_3db_divisors
[val16
& 0x07];
450 *val2
= (ret
% 1000) * 1000;
452 mutex_unlock(&indio_dev
->mlock
);
455 return IIO_VAL_INT_PLUS_MICRO
;
456 case IIO_CHAN_INFO_SAMP_FREQ
:
457 ret
= st
->variant
->get_freq(st
);
461 *val2
= (ret
% 1000) * 1000;
462 return IIO_VAL_INT_PLUS_MICRO
;
468 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
469 .type = IIO_VOLTAGE, \
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), \
477 .scan_index = (si), \
480 .realbits = (bits), \
483 .endianness = IIO_BE, \
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, \
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), \
503 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
506 .realbits = (bits), \
509 .endianness = IIO_BE, \
513 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
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), \
523 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
526 .realbits = (bits), \
529 .endianness = IIO_BE, \
533 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
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), \
542 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
545 .realbits = (bits), \
548 .endianness = IIO_BE, \
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) { \
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), \
568 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
571 .realbits = (bits), \
574 .endianness = IIO_BE, \
578 #define ADIS16400_TEMP_CHAN(addr, bits) { \
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), \
587 .scan_index = ADIS16350_SCAN_TEMP_X, \
590 .realbits = (bits), \
593 .endianness = IIO_BE, \
597 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
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), \
605 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
608 .realbits = (bits), \
611 .endianness = IIO_BE, \
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
,
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
[] = {
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
,
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
;
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
;
909 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
910 if (indio_dev
== NULL
)
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
);
935 ret
= adis_setup_buffer_and_trigger(&st
->adis
, indio_dev
,
936 adis16400_trigger_handler
);
940 /* Get the device into a sane initial state */
941 ret
= adis16400_initial_setup(indio_dev
);
943 goto error_cleanup_buffer
;
944 ret
= iio_device_register(indio_dev
);
946 goto error_cleanup_buffer
;
948 adis16400_debugfs_init(indio_dev
);
951 error_cleanup_buffer
:
952 adis_cleanup_buffer_and_trigger(&st
->adis
, indio_dev
);
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
);
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
= {
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");