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 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 sscanf(indio_dev
->name
, "adis%u\n", &device_id
);
293 if (prod_id
!= device_id
)
294 dev_warn(&indio_dev
->dev
, "Device ID(%u) and product ID(%u) do not match.",
297 dev_info(&indio_dev
->dev
, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
298 indio_dev
->name
, prod_id
,
299 st
->adis
.spi
->chip_select
, st
->adis
.spi
->irq
);
301 /* use high spi speed if possible */
302 if (st
->variant
->flags
& ADIS16400_HAS_SLOW_MODE
) {
303 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &smp_prd
);
307 if ((smp_prd
& ADIS16400_SMPL_PRD_DIV_MASK
) < 0x0A) {
308 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_FAST
;
309 spi_setup(st
->adis
.spi
);
317 static const uint8_t adis16400_addresses
[] = {
318 [ADIS16400_SCAN_GYRO_X
] = ADIS16400_XGYRO_OFF
,
319 [ADIS16400_SCAN_GYRO_Y
] = ADIS16400_YGYRO_OFF
,
320 [ADIS16400_SCAN_GYRO_Z
] = ADIS16400_ZGYRO_OFF
,
321 [ADIS16400_SCAN_ACC_X
] = ADIS16400_XACCL_OFF
,
322 [ADIS16400_SCAN_ACC_Y
] = ADIS16400_YACCL_OFF
,
323 [ADIS16400_SCAN_ACC_Z
] = ADIS16400_ZACCL_OFF
,
326 static int adis16400_write_raw(struct iio_dev
*indio_dev
,
327 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
329 struct adis16400_state
*st
= iio_priv(indio_dev
);
333 case IIO_CHAN_INFO_CALIBBIAS
:
334 mutex_lock(&indio_dev
->mlock
);
335 ret
= adis_write_reg_16(&st
->adis
,
336 adis16400_addresses
[chan
->scan_index
], val
);
337 mutex_unlock(&indio_dev
->mlock
);
339 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
341 * Need to cache values so we can update if the frequency
344 mutex_lock(&indio_dev
->mlock
);
346 /* Work out update to current value */
347 sps
= st
->variant
->get_freq(st
);
349 mutex_unlock(&indio_dev
->mlock
);
353 ret
= adis16400_set_filter(indio_dev
, sps
,
354 val
* 1000 + val2
/ 1000);
355 mutex_unlock(&indio_dev
->mlock
);
357 case IIO_CHAN_INFO_SAMP_FREQ
:
358 sps
= val
* 1000 + val2
/ 1000;
363 mutex_lock(&indio_dev
->mlock
);
364 ret
= st
->variant
->set_freq(st
, sps
);
365 mutex_unlock(&indio_dev
->mlock
);
372 static int adis16400_read_raw(struct iio_dev
*indio_dev
,
373 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
375 struct adis16400_state
*st
= iio_priv(indio_dev
);
380 case IIO_CHAN_INFO_RAW
:
381 return adis_single_conversion(indio_dev
, chan
, 0, val
);
382 case IIO_CHAN_INFO_SCALE
:
383 switch (chan
->type
) {
386 *val2
= st
->variant
->gyro_scale_micro
;
387 return IIO_VAL_INT_PLUS_MICRO
;
390 if (chan
->channel
== 0) {
392 *val2
= 418000; /* 2.418 mV */
395 *val2
= 805800; /* 805.8 uV */
397 return IIO_VAL_INT_PLUS_MICRO
;
400 *val2
= st
->variant
->accel_scale_micro
;
401 return IIO_VAL_INT_PLUS_MICRO
;
404 *val2
= 500; /* 0.5 mgauss */
405 return IIO_VAL_INT_PLUS_MICRO
;
407 *val
= st
->variant
->temp_scale_nano
/ 1000000;
408 *val2
= (st
->variant
->temp_scale_nano
% 1000000);
409 return IIO_VAL_INT_PLUS_MICRO
;
411 /* 20 uBar = 0.002kPascal */
414 return IIO_VAL_INT_PLUS_MICRO
;
418 case IIO_CHAN_INFO_CALIBBIAS
:
419 mutex_lock(&indio_dev
->mlock
);
420 ret
= adis_read_reg_16(&st
->adis
,
421 adis16400_addresses
[chan
->scan_index
], &val16
);
422 mutex_unlock(&indio_dev
->mlock
);
425 val16
= sign_extend32(val16
, 11);
428 case IIO_CHAN_INFO_OFFSET
:
429 /* currently only temperature */
430 *val
= st
->variant
->temp_offset
;
432 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
433 mutex_lock(&indio_dev
->mlock
);
434 /* Need both the number of taps and the sampling frequency */
435 ret
= adis_read_reg_16(&st
->adis
,
439 mutex_unlock(&indio_dev
->mlock
);
442 ret
= st
->variant
->get_freq(st
);
444 ret
/= adis16400_3db_divisors
[val16
& 0x07];
446 *val2
= (ret
% 1000) * 1000;
448 mutex_unlock(&indio_dev
->mlock
);
451 return IIO_VAL_INT_PLUS_MICRO
;
452 case IIO_CHAN_INFO_SAMP_FREQ
:
453 ret
= st
->variant
->get_freq(st
);
457 *val2
= (ret
% 1000) * 1000;
458 return IIO_VAL_INT_PLUS_MICRO
;
464 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si, chn) { \
465 .type = IIO_VOLTAGE, \
468 .extend_name = name, \
469 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
470 BIT(IIO_CHAN_INFO_SCALE), \
471 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
473 .scan_index = (si), \
476 .realbits = (bits), \
479 .endianness = IIO_BE, \
483 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
484 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY, 0)
486 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
487 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC, 1)
489 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
490 .type = IIO_ANGL_VEL, \
492 .channel2 = IIO_MOD_ ## mod, \
493 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
494 BIT(IIO_CHAN_INFO_CALIBBIAS), \
495 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
496 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
497 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
499 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
502 .realbits = (bits), \
505 .endianness = IIO_BE, \
509 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
512 .channel2 = IIO_MOD_ ## mod, \
513 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
514 BIT(IIO_CHAN_INFO_CALIBBIAS), \
515 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
516 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
517 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
519 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
522 .realbits = (bits), \
525 .endianness = IIO_BE, \
529 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
532 .channel2 = IIO_MOD_ ## mod, \
533 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
534 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
535 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
536 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
538 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
541 .realbits = (bits), \
544 .endianness = IIO_BE, \
548 #define ADIS16400_MOD_TEMP_NAME_X "x"
549 #define ADIS16400_MOD_TEMP_NAME_Y "y"
550 #define ADIS16400_MOD_TEMP_NAME_Z "z"
552 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
556 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
557 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
558 BIT(IIO_CHAN_INFO_OFFSET) | \
559 BIT(IIO_CHAN_INFO_SCALE), \
560 .info_mask_shared_by_type = \
561 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
562 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
564 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
567 .realbits = (bits), \
570 .endianness = IIO_BE, \
574 #define ADIS16400_TEMP_CHAN(addr, bits) { \
578 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
579 BIT(IIO_CHAN_INFO_OFFSET) | \
580 BIT(IIO_CHAN_INFO_SCALE), \
581 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
583 .scan_index = ADIS16350_SCAN_TEMP_X, \
586 .realbits = (bits), \
589 .endianness = IIO_BE, \
593 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
596 .channel2 = IIO_MOD_ ## mod, \
597 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
598 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
599 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
601 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
604 .realbits = (bits), \
607 .endianness = IIO_BE, \
611 static const struct iio_chan_spec adis16400_channels
[] = {
612 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 14),
613 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
614 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
615 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
616 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
617 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
618 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
619 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 14),
620 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 14),
621 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 14),
622 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT
, 12),
623 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC
, 12),
624 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
627 static const struct iio_chan_spec adis16445_channels
[] = {
628 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 16),
629 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 16),
630 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 16),
631 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 16),
632 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 16),
633 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 16),
634 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT
, 12),
635 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
638 static const struct iio_chan_spec adis16448_channels
[] = {
639 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 16),
640 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 16),
641 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 16),
642 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 16),
643 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 16),
644 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 16),
645 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 16),
646 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 16),
647 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 16),
649 .type
= IIO_PRESSURE
,
650 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
651 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
652 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
653 .address
= ADIS16448_BARO_OUT
,
654 .scan_index
= ADIS16400_SCAN_BARO
,
659 .endianness
= IIO_BE
,
662 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT
, 12),
663 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
666 static const struct iio_chan_spec adis16350_channels
[] = {
667 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 12),
668 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
669 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
670 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
671 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
672 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
673 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
674 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 14),
675 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 14),
676 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 14),
677 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC
, 12),
678 ADIS16400_MOD_TEMP_CHAN(X
, ADIS16350_XTEMP_OUT
, 12),
679 ADIS16400_MOD_TEMP_CHAN(Y
, ADIS16350_YTEMP_OUT
, 12),
680 ADIS16400_MOD_TEMP_CHAN(Z
, ADIS16350_ZTEMP_OUT
, 12),
681 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
684 static const struct iio_chan_spec adis16300_channels
[] = {
685 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 12),
686 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
687 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
688 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
689 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
690 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT
, 12),
691 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC
, 12),
692 ADIS16400_INCLI_CHAN(X
, ADIS16300_PITCH_OUT
, 13),
693 ADIS16400_INCLI_CHAN(Y
, ADIS16300_ROLL_OUT
, 13),
694 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
697 static const struct iio_chan_spec adis16334_channels
[] = {
698 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
699 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
700 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
701 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
702 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
703 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
704 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT
, 12),
705 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
708 static struct adis16400_chip_info adis16400_chips
[] = {
710 .channels
= adis16300_channels
,
711 .num_channels
= ARRAY_SIZE(adis16300_channels
),
712 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
713 ADIS16400_HAS_SERIAL_NUMBER
,
714 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
715 .accel_scale_micro
= 5884,
716 .temp_scale_nano
= 140000000, /* 0.14 C */
717 .temp_offset
= 25000000 / 140000, /* 25 C = 0x00 */
718 .set_freq
= adis16400_set_freq
,
719 .get_freq
= adis16400_get_freq
,
722 .channels
= adis16334_channels
,
723 .num_channels
= ARRAY_SIZE(adis16334_channels
),
724 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_NO_BURST
|
725 ADIS16400_HAS_SERIAL_NUMBER
,
726 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
727 .accel_scale_micro
= IIO_G_TO_M_S_2(1000), /* 1 mg */
728 .temp_scale_nano
= 67850000, /* 0.06785 C */
729 .temp_offset
= 25000000 / 67850, /* 25 C = 0x00 */
730 .set_freq
= adis16334_set_freq
,
731 .get_freq
= adis16334_get_freq
,
734 .channels
= adis16350_channels
,
735 .num_channels
= ARRAY_SIZE(adis16350_channels
),
736 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
737 .accel_scale_micro
= IIO_G_TO_M_S_2(2522), /* 0.002522 g */
738 .temp_scale_nano
= 145300000, /* 0.1453 C */
739 .temp_offset
= 25000000 / 145300, /* 25 C = 0x00 */
740 .flags
= ADIS16400_NO_BURST
| ADIS16400_HAS_SLOW_MODE
,
741 .set_freq
= adis16400_set_freq
,
742 .get_freq
= adis16400_get_freq
,
745 .channels
= adis16350_channels
,
746 .num_channels
= ARRAY_SIZE(adis16350_channels
),
747 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
748 ADIS16400_HAS_SERIAL_NUMBER
,
749 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
750 .accel_scale_micro
= IIO_G_TO_M_S_2(3333), /* 3.333 mg */
751 .temp_scale_nano
= 136000000, /* 0.136 C */
752 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
753 .set_freq
= adis16400_set_freq
,
754 .get_freq
= adis16400_get_freq
,
757 .channels
= adis16350_channels
,
758 .num_channels
= ARRAY_SIZE(adis16350_channels
),
759 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
760 ADIS16400_HAS_SERIAL_NUMBER
,
761 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
762 .accel_scale_micro
= IIO_G_TO_M_S_2(333), /* 0.333 mg */
763 .temp_scale_nano
= 136000000, /* 0.136 C */
764 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
765 .set_freq
= adis16400_set_freq
,
766 .get_freq
= adis16400_get_freq
,
769 .channels
= adis16350_channels
,
770 .num_channels
= ARRAY_SIZE(adis16350_channels
),
771 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
772 ADIS16400_HAS_SERIAL_NUMBER
,
773 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
774 .accel_scale_micro
= IIO_G_TO_M_S_2(1000), /* 1 mg */
775 .temp_scale_nano
= 136000000, /* 0.136 C */
776 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
777 .set_freq
= adis16400_set_freq
,
778 .get_freq
= adis16400_get_freq
,
781 .channels
= adis16350_channels
,
782 .num_channels
= ARRAY_SIZE(adis16350_channels
),
783 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
784 ADIS16400_HAS_SERIAL_NUMBER
,
785 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(2000), /* 0.2 deg/s */
786 .accel_scale_micro
= IIO_G_TO_M_S_2(3333), /* 3.333 mg */
787 .temp_scale_nano
= 136000000, /* 0.136 C */
788 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
789 .set_freq
= adis16400_set_freq
,
790 .get_freq
= adis16400_get_freq
,
793 .channels
= adis16400_channels
,
794 .num_channels
= ARRAY_SIZE(adis16400_channels
),
795 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
,
796 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
797 .accel_scale_micro
= IIO_G_TO_M_S_2(3333), /* 3.333 mg */
798 .temp_scale_nano
= 140000000, /* 0.14 C */
799 .temp_offset
= 25000000 / 140000, /* 25 C = 0x00 */
800 .set_freq
= adis16400_set_freq
,
801 .get_freq
= adis16400_get_freq
,
804 .channels
= adis16445_channels
,
805 .num_channels
= ARRAY_SIZE(adis16445_channels
),
806 .flags
= ADIS16400_HAS_PROD_ID
|
807 ADIS16400_HAS_SERIAL_NUMBER
|
808 ADIS16400_BURST_DIAG_STAT
,
809 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
810 .accel_scale_micro
= IIO_G_TO_M_S_2(250), /* 1/4000 g */
811 .temp_scale_nano
= 73860000, /* 0.07386 C */
812 .temp_offset
= 31000000 / 73860, /* 31 C = 0x00 */
813 .set_freq
= adis16334_set_freq
,
814 .get_freq
= adis16334_get_freq
,
817 .channels
= adis16448_channels
,
818 .num_channels
= ARRAY_SIZE(adis16448_channels
),
819 .flags
= ADIS16400_HAS_PROD_ID
|
820 ADIS16400_HAS_SERIAL_NUMBER
|
821 ADIS16400_BURST_DIAG_STAT
,
822 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(40000), /* 0.04 deg/s */
823 .accel_scale_micro
= IIO_G_TO_M_S_2(833), /* 1/1200 g */
824 .temp_scale_nano
= 73860000, /* 0.07386 C */
825 .temp_offset
= 31000000 / 73860, /* 31 C = 0x00 */
826 .set_freq
= adis16334_set_freq
,
827 .get_freq
= adis16334_get_freq
,
831 static const struct iio_info adis16400_info
= {
832 .driver_module
= THIS_MODULE
,
833 .read_raw
= &adis16400_read_raw
,
834 .write_raw
= &adis16400_write_raw
,
835 .update_scan_mode
= adis16400_update_scan_mode
,
836 .debugfs_reg_access
= adis_debugfs_reg_access
,
839 static const char * const adis16400_status_error_msgs
[] = {
840 [ADIS16400_DIAG_STAT_ZACCL_FAIL
] = "Z-axis accelerometer self-test failure",
841 [ADIS16400_DIAG_STAT_YACCL_FAIL
] = "Y-axis accelerometer self-test failure",
842 [ADIS16400_DIAG_STAT_XACCL_FAIL
] = "X-axis accelerometer self-test failure",
843 [ADIS16400_DIAG_STAT_XGYRO_FAIL
] = "X-axis gyroscope self-test failure",
844 [ADIS16400_DIAG_STAT_YGYRO_FAIL
] = "Y-axis gyroscope self-test failure",
845 [ADIS16400_DIAG_STAT_ZGYRO_FAIL
] = "Z-axis gyroscope self-test failure",
846 [ADIS16400_DIAG_STAT_ALARM2
] = "Alarm 2 active",
847 [ADIS16400_DIAG_STAT_ALARM1
] = "Alarm 1 active",
848 [ADIS16400_DIAG_STAT_FLASH_CHK
] = "Flash checksum error",
849 [ADIS16400_DIAG_STAT_SELF_TEST
] = "Self test error",
850 [ADIS16400_DIAG_STAT_OVERFLOW
] = "Sensor overrange",
851 [ADIS16400_DIAG_STAT_SPI_FAIL
] = "SPI failure",
852 [ADIS16400_DIAG_STAT_FLASH_UPT
] = "Flash update failed",
853 [ADIS16400_DIAG_STAT_POWER_HIGH
] = "Power supply above 5.25V",
854 [ADIS16400_DIAG_STAT_POWER_LOW
] = "Power supply below 4.75V",
857 static const struct adis_data adis16400_data
= {
858 .msc_ctrl_reg
= ADIS16400_MSC_CTRL
,
859 .glob_cmd_reg
= ADIS16400_GLOB_CMD
,
860 .diag_stat_reg
= ADIS16400_DIAG_STAT
,
865 .self_test_mask
= ADIS16400_MSC_CTRL_MEM_TEST
,
866 .startup_delay
= ADIS16400_STARTUP_DELAY
,
868 .status_error_msgs
= adis16400_status_error_msgs
,
869 .status_error_mask
= BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL
) |
870 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL
) |
871 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL
) |
872 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL
) |
873 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL
) |
874 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL
) |
875 BIT(ADIS16400_DIAG_STAT_ALARM2
) |
876 BIT(ADIS16400_DIAG_STAT_ALARM1
) |
877 BIT(ADIS16400_DIAG_STAT_FLASH_CHK
) |
878 BIT(ADIS16400_DIAG_STAT_SELF_TEST
) |
879 BIT(ADIS16400_DIAG_STAT_OVERFLOW
) |
880 BIT(ADIS16400_DIAG_STAT_SPI_FAIL
) |
881 BIT(ADIS16400_DIAG_STAT_FLASH_UPT
) |
882 BIT(ADIS16400_DIAG_STAT_POWER_HIGH
) |
883 BIT(ADIS16400_DIAG_STAT_POWER_LOW
),
886 static void adis16400_setup_chan_mask(struct adis16400_state
*st
)
888 const struct adis16400_chip_info
*chip_info
= st
->variant
;
891 for (i
= 0; i
< chip_info
->num_channels
; i
++) {
892 const struct iio_chan_spec
*ch
= &chip_info
->channels
[i
];
894 if (ch
->scan_index
>= 0 &&
895 ch
->scan_index
!= ADIS16400_SCAN_TIMESTAMP
)
896 st
->avail_scan_mask
[0] |= BIT(ch
->scan_index
);
900 static int adis16400_probe(struct spi_device
*spi
)
902 struct adis16400_state
*st
;
903 struct iio_dev
*indio_dev
;
906 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
907 if (indio_dev
== NULL
)
910 st
= iio_priv(indio_dev
);
911 /* this is only used for removal purposes */
912 spi_set_drvdata(spi
, indio_dev
);
914 /* setup the industrialio driver allocated elements */
915 st
->variant
= &adis16400_chips
[spi_get_device_id(spi
)->driver_data
];
916 indio_dev
->dev
.parent
= &spi
->dev
;
917 indio_dev
->name
= spi_get_device_id(spi
)->name
;
918 indio_dev
->channels
= st
->variant
->channels
;
919 indio_dev
->num_channels
= st
->variant
->num_channels
;
920 indio_dev
->info
= &adis16400_info
;
921 indio_dev
->modes
= INDIO_DIRECT_MODE
;
923 if (!(st
->variant
->flags
& ADIS16400_NO_BURST
)) {
924 adis16400_setup_chan_mask(st
);
925 indio_dev
->available_scan_masks
= st
->avail_scan_mask
;
928 ret
= adis_init(&st
->adis
, indio_dev
, spi
, &adis16400_data
);
932 ret
= adis_setup_buffer_and_trigger(&st
->adis
, indio_dev
,
933 adis16400_trigger_handler
);
937 /* Get the device into a sane initial state */
938 ret
= adis16400_initial_setup(indio_dev
);
940 goto error_cleanup_buffer
;
941 ret
= iio_device_register(indio_dev
);
943 goto error_cleanup_buffer
;
945 adis16400_debugfs_init(indio_dev
);
948 error_cleanup_buffer
:
949 adis_cleanup_buffer_and_trigger(&st
->adis
, indio_dev
);
953 static int adis16400_remove(struct spi_device
*spi
)
955 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
956 struct adis16400_state
*st
= iio_priv(indio_dev
);
958 iio_device_unregister(indio_dev
);
959 adis16400_stop_device(indio_dev
);
961 adis_cleanup_buffer_and_trigger(&st
->adis
, indio_dev
);
966 static const struct spi_device_id adis16400_id
[] = {
967 {"adis16300", ADIS16300
},
968 {"adis16305", ADIS16300
},
969 {"adis16334", ADIS16334
},
970 {"adis16350", ADIS16350
},
971 {"adis16354", ADIS16350
},
972 {"adis16355", ADIS16350
},
973 {"adis16360", ADIS16360
},
974 {"adis16362", ADIS16362
},
975 {"adis16364", ADIS16364
},
976 {"adis16365", ADIS16360
},
977 {"adis16367", ADIS16367
},
978 {"adis16400", ADIS16400
},
979 {"adis16405", ADIS16400
},
980 {"adis16445", ADIS16445
},
981 {"adis16448", ADIS16448
},
984 MODULE_DEVICE_TABLE(spi
, adis16400_id
);
986 static struct spi_driver adis16400_driver
= {
989 .owner
= THIS_MODULE
,
991 .id_table
= adis16400_id
,
992 .probe
= adis16400_probe
,
993 .remove
= adis16400_remove
,
995 module_spi_driver(adis16400_driver
);
997 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
998 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
999 MODULE_LICENSE("GPL v2");