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
{
146 static int adis16334_get_freq(struct adis16400_state
*st
)
151 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &t
);
155 t
>>= ADIS16334_RATE_DIV_SHIFT
;
160 static int adis16334_set_freq(struct adis16400_state
*st
, unsigned int freq
)
165 t
= ilog2(819200 / freq
);
172 t
<<= ADIS16334_RATE_DIV_SHIFT
;
173 t
|= ADIS16334_RATE_INT_CLK
;
175 return adis_write_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, t
);
178 static int adis16400_get_freq(struct adis16400_state
*st
)
183 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &t
);
187 sps
= (t
& ADIS16400_SMPL_PRD_TIME_BASE
) ? 52851 : 1638404;
188 sps
/= (t
& ADIS16400_SMPL_PRD_DIV_MASK
) + 1;
193 static int adis16400_set_freq(struct adis16400_state
*st
, unsigned int freq
)
200 val
|= ADIS16400_SMPL_PRD_TIME_BASE
;
210 if (t
>= 0x0A || (val
& ADIS16400_SMPL_PRD_TIME_BASE
))
211 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_SLOW
;
213 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_FAST
;
215 return adis_write_reg_8(&st
->adis
, ADIS16400_SMPL_PRD
, val
);
218 static const unsigned adis16400_3db_divisors
[] = {
219 [0] = 2, /* Special case */
226 [7] = 200, /* Not a valid setting */
229 static int adis16400_set_filter(struct iio_dev
*indio_dev
, int sps
, int val
)
231 struct adis16400_state
*st
= iio_priv(indio_dev
);
235 for (i
= ARRAY_SIZE(adis16400_3db_divisors
) - 1; i
>= 1; i
--) {
236 if (sps
/ adis16400_3db_divisors
[i
] >= val
)
240 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SENS_AVG
, &val16
);
244 ret
= adis_write_reg_16(&st
->adis
, ADIS16400_SENS_AVG
,
245 (val16
& ~0x07) | i
);
249 /* Power down the device */
250 static int adis16400_stop_device(struct iio_dev
*indio_dev
)
252 struct adis16400_state
*st
= iio_priv(indio_dev
);
255 ret
= adis_write_reg_16(&st
->adis
, ADIS16400_SLP_CNT
,
256 ADIS16400_SLP_CNT_POWER_OFF
);
258 dev_err(&indio_dev
->dev
,
259 "problem with turning device off: SLP_CNT");
264 static int adis16400_initial_setup(struct iio_dev
*indio_dev
)
266 struct adis16400_state
*st
= iio_priv(indio_dev
);
267 uint16_t prod_id
, smp_prd
;
268 unsigned int device_id
;
271 /* use low spi speed for init if the device has a slow mode */
272 if (st
->variant
->flags
& ADIS16400_HAS_SLOW_MODE
)
273 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_SLOW
;
275 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_FAST
;
276 st
->adis
.spi
->mode
= SPI_MODE_3
;
277 spi_setup(st
->adis
.spi
);
279 ret
= adis_initial_startup(&st
->adis
);
283 if (st
->variant
->flags
& ADIS16400_HAS_PROD_ID
) {
284 ret
= adis_read_reg_16(&st
->adis
,
285 ADIS16400_PRODUCT_ID
, &prod_id
);
289 sscanf(indio_dev
->name
, "adis%u\n", &device_id
);
291 if (prod_id
!= device_id
)
292 dev_warn(&indio_dev
->dev
, "Device ID(%u) and product ID(%u) do not match.",
295 dev_info(&indio_dev
->dev
, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
296 indio_dev
->name
, prod_id
,
297 st
->adis
.spi
->chip_select
, st
->adis
.spi
->irq
);
299 /* use high spi speed if possible */
300 if (st
->variant
->flags
& ADIS16400_HAS_SLOW_MODE
) {
301 ret
= adis_read_reg_16(&st
->adis
, ADIS16400_SMPL_PRD
, &smp_prd
);
305 if ((smp_prd
& ADIS16400_SMPL_PRD_DIV_MASK
) < 0x0A) {
306 st
->adis
.spi
->max_speed_hz
= ADIS16400_SPI_FAST
;
307 spi_setup(st
->adis
.spi
);
315 static const uint8_t adis16400_addresses
[] = {
316 [ADIS16400_SCAN_GYRO_X
] = ADIS16400_XGYRO_OFF
,
317 [ADIS16400_SCAN_GYRO_Y
] = ADIS16400_YGYRO_OFF
,
318 [ADIS16400_SCAN_GYRO_Z
] = ADIS16400_ZGYRO_OFF
,
319 [ADIS16400_SCAN_ACC_X
] = ADIS16400_XACCL_OFF
,
320 [ADIS16400_SCAN_ACC_Y
] = ADIS16400_YACCL_OFF
,
321 [ADIS16400_SCAN_ACC_Z
] = ADIS16400_ZACCL_OFF
,
324 static int adis16400_write_raw(struct iio_dev
*indio_dev
,
325 struct iio_chan_spec
const *chan
, int val
, int val2
, long info
)
327 struct adis16400_state
*st
= iio_priv(indio_dev
);
331 case IIO_CHAN_INFO_CALIBBIAS
:
332 mutex_lock(&indio_dev
->mlock
);
333 ret
= adis_write_reg_16(&st
->adis
,
334 adis16400_addresses
[chan
->scan_index
], val
);
335 mutex_unlock(&indio_dev
->mlock
);
337 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
339 * Need to cache values so we can update if the frequency
342 mutex_lock(&indio_dev
->mlock
);
344 /* Work out update to current value */
345 sps
= st
->variant
->get_freq(st
);
347 mutex_unlock(&indio_dev
->mlock
);
351 ret
= adis16400_set_filter(indio_dev
, sps
,
352 val
* 1000 + val2
/ 1000);
353 mutex_unlock(&indio_dev
->mlock
);
355 case IIO_CHAN_INFO_SAMP_FREQ
:
356 sps
= val
* 1000 + val2
/ 1000;
361 mutex_lock(&indio_dev
->mlock
);
362 ret
= st
->variant
->set_freq(st
, sps
);
363 mutex_unlock(&indio_dev
->mlock
);
370 static int adis16400_read_raw(struct iio_dev
*indio_dev
,
371 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long info
)
373 struct adis16400_state
*st
= iio_priv(indio_dev
);
378 case IIO_CHAN_INFO_RAW
:
379 return adis_single_conversion(indio_dev
, chan
, 0, val
);
380 case IIO_CHAN_INFO_SCALE
:
381 switch (chan
->type
) {
384 *val2
= st
->variant
->gyro_scale_micro
;
385 return IIO_VAL_INT_PLUS_MICRO
;
388 if (chan
->channel
== 0) {
390 *val2
= 418000; /* 2.418 mV */
393 *val2
= 805800; /* 805.8 uV */
395 return IIO_VAL_INT_PLUS_MICRO
;
398 *val2
= st
->variant
->accel_scale_micro
;
399 return IIO_VAL_INT_PLUS_MICRO
;
402 *val2
= 500; /* 0.5 mgauss */
403 return IIO_VAL_INT_PLUS_MICRO
;
405 *val
= st
->variant
->temp_scale_nano
/ 1000000;
406 *val2
= (st
->variant
->temp_scale_nano
% 1000000);
407 return IIO_VAL_INT_PLUS_MICRO
;
411 case IIO_CHAN_INFO_CALIBBIAS
:
412 mutex_lock(&indio_dev
->mlock
);
413 ret
= adis_read_reg_16(&st
->adis
,
414 adis16400_addresses
[chan
->scan_index
], &val16
);
415 mutex_unlock(&indio_dev
->mlock
);
418 val16
= sign_extend32(val16
, 11);
421 case IIO_CHAN_INFO_OFFSET
:
422 /* currently only temperature */
423 *val
= st
->variant
->temp_offset
;
425 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
426 mutex_lock(&indio_dev
->mlock
);
427 /* Need both the number of taps and the sampling frequency */
428 ret
= adis_read_reg_16(&st
->adis
,
432 mutex_unlock(&indio_dev
->mlock
);
435 ret
= st
->variant
->get_freq(st
);
437 ret
/= adis16400_3db_divisors
[val16
& 0x07];
439 *val2
= (ret
% 1000) * 1000;
441 mutex_unlock(&indio_dev
->mlock
);
444 return IIO_VAL_INT_PLUS_MICRO
;
445 case IIO_CHAN_INFO_SAMP_FREQ
:
446 ret
= st
->variant
->get_freq(st
);
450 *val2
= (ret
% 1000) * 1000;
451 return IIO_VAL_INT_PLUS_MICRO
;
457 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si) { \
458 .type = IIO_VOLTAGE, \
461 .extend_name = name, \
462 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
463 BIT(IIO_CHAN_INFO_SCALE), \
464 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
466 .scan_index = (si), \
469 .realbits = (bits), \
472 .endianness = IIO_BE, \
476 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
477 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY)
479 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
480 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC)
482 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
483 .type = IIO_ANGL_VEL, \
485 .channel2 = IIO_MOD_ ## mod, \
486 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
487 BIT(IIO_CHAN_INFO_CALIBBIAS), \
488 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
489 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
490 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
492 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
495 .realbits = (bits), \
498 .endianness = IIO_BE, \
502 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
505 .channel2 = IIO_MOD_ ## mod, \
506 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
507 BIT(IIO_CHAN_INFO_CALIBBIAS), \
508 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
509 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
510 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
512 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
515 .realbits = (bits), \
518 .endianness = IIO_BE, \
522 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
525 .channel2 = IIO_MOD_ ## mod, \
526 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
527 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
528 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
529 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
531 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
534 .realbits = (bits), \
537 .endianness = IIO_BE, \
541 #define ADIS16400_MOD_TEMP_NAME_X "x"
542 #define ADIS16400_MOD_TEMP_NAME_Y "y"
543 #define ADIS16400_MOD_TEMP_NAME_Z "z"
545 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
549 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
550 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
551 BIT(IIO_CHAN_INFO_OFFSET) | \
552 BIT(IIO_CHAN_INFO_SCALE), \
553 .info_mask_shared_by_type = \
554 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
555 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
557 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
560 .realbits = (bits), \
563 .endianness = IIO_BE, \
567 #define ADIS16400_TEMP_CHAN(addr, bits) { \
571 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
572 BIT(IIO_CHAN_INFO_OFFSET) | \
573 BIT(IIO_CHAN_INFO_SCALE), \
574 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
576 .scan_index = ADIS16350_SCAN_TEMP_X, \
579 .realbits = (bits), \
582 .endianness = IIO_BE, \
586 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
589 .channel2 = IIO_MOD_ ## mod, \
590 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
591 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
592 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
594 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
597 .realbits = (bits), \
600 .endianness = IIO_BE, \
604 static const struct iio_chan_spec adis16400_channels
[] = {
605 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 14),
606 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
607 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
608 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
609 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
610 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
611 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
612 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 14),
613 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 14),
614 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 14),
615 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT
, 12),
616 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC
, 12),
617 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
620 static const struct iio_chan_spec adis16448_channels
[] = {
621 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 16),
622 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 16),
623 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 16),
624 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 16),
625 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 16),
626 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 16),
627 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 16),
628 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 16),
629 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 16),
631 .type
= IIO_PRESSURE
,
632 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
633 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
634 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
635 .address
= ADIS16448_BARO_OUT
,
636 .scan_index
= ADIS16400_SCAN_BARO
,
641 .endianness
= IIO_BE
,
644 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT
, 12),
645 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
648 static const struct iio_chan_spec adis16350_channels
[] = {
649 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 12),
650 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
651 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
652 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
653 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
654 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
655 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
656 ADIS16400_MAGN_CHAN(X
, ADIS16400_XMAGN_OUT
, 14),
657 ADIS16400_MAGN_CHAN(Y
, ADIS16400_YMAGN_OUT
, 14),
658 ADIS16400_MAGN_CHAN(Z
, ADIS16400_ZMAGN_OUT
, 14),
659 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC
, 12),
660 ADIS16400_MOD_TEMP_CHAN(X
, ADIS16350_XTEMP_OUT
, 12),
661 ADIS16400_MOD_TEMP_CHAN(Y
, ADIS16350_YTEMP_OUT
, 12),
662 ADIS16400_MOD_TEMP_CHAN(Z
, ADIS16350_ZTEMP_OUT
, 12),
663 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
666 static const struct iio_chan_spec adis16300_channels
[] = {
667 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT
, 12),
668 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
669 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
670 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
671 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
672 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT
, 12),
673 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC
, 12),
674 ADIS16400_INCLI_CHAN(X
, ADIS16300_PITCH_OUT
, 13),
675 ADIS16400_INCLI_CHAN(Y
, ADIS16300_ROLL_OUT
, 13),
676 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
679 static const struct iio_chan_spec adis16334_channels
[] = {
680 ADIS16400_GYRO_CHAN(X
, ADIS16400_XGYRO_OUT
, 14),
681 ADIS16400_GYRO_CHAN(Y
, ADIS16400_YGYRO_OUT
, 14),
682 ADIS16400_GYRO_CHAN(Z
, ADIS16400_ZGYRO_OUT
, 14),
683 ADIS16400_ACCEL_CHAN(X
, ADIS16400_XACCL_OUT
, 14),
684 ADIS16400_ACCEL_CHAN(Y
, ADIS16400_YACCL_OUT
, 14),
685 ADIS16400_ACCEL_CHAN(Z
, ADIS16400_ZACCL_OUT
, 14),
686 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT
, 12),
687 IIO_CHAN_SOFT_TIMESTAMP(ADIS16400_SCAN_TIMESTAMP
),
690 static struct adis16400_chip_info adis16400_chips
[] = {
692 .channels
= adis16300_channels
,
693 .num_channels
= ARRAY_SIZE(adis16300_channels
),
694 .flags
= ADIS16400_HAS_SLOW_MODE
,
695 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
696 .accel_scale_micro
= 5884,
697 .temp_scale_nano
= 140000000, /* 0.14 C */
698 .temp_offset
= 25000000 / 140000, /* 25 C = 0x00 */
699 .set_freq
= adis16400_set_freq
,
700 .get_freq
= adis16400_get_freq
,
703 .channels
= adis16334_channels
,
704 .num_channels
= ARRAY_SIZE(adis16334_channels
),
705 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_NO_BURST
|
706 ADIS16400_HAS_SERIAL_NUMBER
,
707 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
708 .accel_scale_micro
= IIO_G_TO_M_S_2(1000), /* 1 mg */
709 .temp_scale_nano
= 67850000, /* 0.06785 C */
710 .temp_offset
= 25000000 / 67850, /* 25 C = 0x00 */
711 .set_freq
= adis16334_set_freq
,
712 .get_freq
= adis16334_get_freq
,
715 .channels
= adis16350_channels
,
716 .num_channels
= ARRAY_SIZE(adis16350_channels
),
717 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
718 .accel_scale_micro
= IIO_G_TO_M_S_2(2522), /* 0.002522 g */
719 .temp_scale_nano
= 145300000, /* 0.1453 C */
720 .temp_offset
= 25000000 / 145300, /* 25 C = 0x00 */
721 .flags
= ADIS16400_NO_BURST
| ADIS16400_HAS_SLOW_MODE
,
722 .set_freq
= adis16400_set_freq
,
723 .get_freq
= adis16400_get_freq
,
726 .channels
= adis16350_channels
,
727 .num_channels
= ARRAY_SIZE(adis16350_channels
),
728 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
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(3333), /* 3.333 mg */
732 .temp_scale_nano
= 136000000, /* 0.136 C */
733 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
734 .set_freq
= adis16400_set_freq
,
735 .get_freq
= adis16400_get_freq
,
738 .channels
= adis16350_channels
,
739 .num_channels
= ARRAY_SIZE(adis16350_channels
),
740 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
741 ADIS16400_HAS_SERIAL_NUMBER
,
742 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
743 .accel_scale_micro
= IIO_G_TO_M_S_2(333), /* 0.333 mg */
744 .temp_scale_nano
= 136000000, /* 0.136 C */
745 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
746 .set_freq
= adis16400_set_freq
,
747 .get_freq
= adis16400_get_freq
,
750 .channels
= adis16350_channels
,
751 .num_channels
= ARRAY_SIZE(adis16350_channels
),
752 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
|
753 ADIS16400_HAS_SERIAL_NUMBER
,
754 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
755 .accel_scale_micro
= IIO_G_TO_M_S_2(1000), /* 1 mg */
756 .temp_scale_nano
= 136000000, /* 0.136 C */
757 .temp_offset
= 25000000 / 136000, /* 25 C = 0x00 */
758 .set_freq
= adis16400_set_freq
,
759 .get_freq
= adis16400_get_freq
,
762 .channels
= adis16400_channels
,
763 .num_channels
= ARRAY_SIZE(adis16400_channels
),
764 .flags
= ADIS16400_HAS_PROD_ID
| ADIS16400_HAS_SLOW_MODE
,
765 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
766 .accel_scale_micro
= IIO_G_TO_M_S_2(3333), /* 3.333 mg */
767 .temp_scale_nano
= 140000000, /* 0.14 C */
768 .temp_offset
= 25000000 / 140000, /* 25 C = 0x00 */
769 .set_freq
= adis16400_set_freq
,
770 .get_freq
= adis16400_get_freq
,
773 .channels
= adis16448_channels
,
774 .num_channels
= ARRAY_SIZE(adis16448_channels
),
775 .flags
= ADIS16400_HAS_PROD_ID
|
776 ADIS16400_HAS_SERIAL_NUMBER
,
777 .gyro_scale_micro
= IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
778 .accel_scale_micro
= IIO_G_TO_M_S_2(833), /* 1/1200 g */
779 .temp_scale_nano
= 73860000, /* 0.07386 C */
780 .temp_offset
= 31000000 / 73860, /* 31 C = 0x00 */
781 .set_freq
= adis16334_set_freq
,
782 .get_freq
= adis16334_get_freq
,
786 static const struct iio_info adis16400_info
= {
787 .driver_module
= THIS_MODULE
,
788 .read_raw
= &adis16400_read_raw
,
789 .write_raw
= &adis16400_write_raw
,
790 .update_scan_mode
= adis16400_update_scan_mode
,
791 .debugfs_reg_access
= adis_debugfs_reg_access
,
794 static const unsigned long adis16400_burst_scan_mask
[] = {
799 static const char * const adis16400_status_error_msgs
[] = {
800 [ADIS16400_DIAG_STAT_ZACCL_FAIL
] = "Z-axis accelerometer self-test failure",
801 [ADIS16400_DIAG_STAT_YACCL_FAIL
] = "Y-axis accelerometer self-test failure",
802 [ADIS16400_DIAG_STAT_XACCL_FAIL
] = "X-axis accelerometer self-test failure",
803 [ADIS16400_DIAG_STAT_XGYRO_FAIL
] = "X-axis gyroscope self-test failure",
804 [ADIS16400_DIAG_STAT_YGYRO_FAIL
] = "Y-axis gyroscope self-test failure",
805 [ADIS16400_DIAG_STAT_ZGYRO_FAIL
] = "Z-axis gyroscope self-test failure",
806 [ADIS16400_DIAG_STAT_ALARM2
] = "Alarm 2 active",
807 [ADIS16400_DIAG_STAT_ALARM1
] = "Alarm 1 active",
808 [ADIS16400_DIAG_STAT_FLASH_CHK
] = "Flash checksum error",
809 [ADIS16400_DIAG_STAT_SELF_TEST
] = "Self test error",
810 [ADIS16400_DIAG_STAT_OVERFLOW
] = "Sensor overrange",
811 [ADIS16400_DIAG_STAT_SPI_FAIL
] = "SPI failure",
812 [ADIS16400_DIAG_STAT_FLASH_UPT
] = "Flash update failed",
813 [ADIS16400_DIAG_STAT_POWER_HIGH
] = "Power supply above 5.25V",
814 [ADIS16400_DIAG_STAT_POWER_LOW
] = "Power supply below 4.75V",
817 static const struct adis_data adis16400_data
= {
818 .msc_ctrl_reg
= ADIS16400_MSC_CTRL
,
819 .glob_cmd_reg
= ADIS16400_GLOB_CMD
,
820 .diag_stat_reg
= ADIS16400_DIAG_STAT
,
825 .self_test_mask
= ADIS16400_MSC_CTRL_MEM_TEST
,
826 .startup_delay
= ADIS16400_STARTUP_DELAY
,
828 .status_error_msgs
= adis16400_status_error_msgs
,
829 .status_error_mask
= BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL
) |
830 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL
) |
831 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL
) |
832 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL
) |
833 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL
) |
834 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL
) |
835 BIT(ADIS16400_DIAG_STAT_ALARM2
) |
836 BIT(ADIS16400_DIAG_STAT_ALARM1
) |
837 BIT(ADIS16400_DIAG_STAT_FLASH_CHK
) |
838 BIT(ADIS16400_DIAG_STAT_SELF_TEST
) |
839 BIT(ADIS16400_DIAG_STAT_OVERFLOW
) |
840 BIT(ADIS16400_DIAG_STAT_SPI_FAIL
) |
841 BIT(ADIS16400_DIAG_STAT_FLASH_UPT
) |
842 BIT(ADIS16400_DIAG_STAT_POWER_HIGH
) |
843 BIT(ADIS16400_DIAG_STAT_POWER_LOW
),
846 static int adis16400_probe(struct spi_device
*spi
)
848 struct adis16400_state
*st
;
849 struct iio_dev
*indio_dev
;
852 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
853 if (indio_dev
== NULL
)
856 st
= iio_priv(indio_dev
);
857 /* this is only used for removal purposes */
858 spi_set_drvdata(spi
, indio_dev
);
860 /* setup the industrialio driver allocated elements */
861 st
->variant
= &adis16400_chips
[spi_get_device_id(spi
)->driver_data
];
862 indio_dev
->dev
.parent
= &spi
->dev
;
863 indio_dev
->name
= spi_get_device_id(spi
)->name
;
864 indio_dev
->channels
= st
->variant
->channels
;
865 indio_dev
->num_channels
= st
->variant
->num_channels
;
866 indio_dev
->info
= &adis16400_info
;
867 indio_dev
->modes
= INDIO_DIRECT_MODE
;
869 if (!(st
->variant
->flags
& ADIS16400_NO_BURST
))
870 indio_dev
->available_scan_masks
= adis16400_burst_scan_mask
;
872 ret
= adis_init(&st
->adis
, indio_dev
, spi
, &adis16400_data
);
876 ret
= adis_setup_buffer_and_trigger(&st
->adis
, indio_dev
,
877 adis16400_trigger_handler
);
881 /* Get the device into a sane initial state */
882 ret
= adis16400_initial_setup(indio_dev
);
884 goto error_cleanup_buffer
;
885 ret
= iio_device_register(indio_dev
);
887 goto error_cleanup_buffer
;
889 adis16400_debugfs_init(indio_dev
);
892 error_cleanup_buffer
:
893 adis_cleanup_buffer_and_trigger(&st
->adis
, indio_dev
);
897 static int adis16400_remove(struct spi_device
*spi
)
899 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
900 struct adis16400_state
*st
= iio_priv(indio_dev
);
902 iio_device_unregister(indio_dev
);
903 adis16400_stop_device(indio_dev
);
905 adis_cleanup_buffer_and_trigger(&st
->adis
, indio_dev
);
910 static const struct spi_device_id adis16400_id
[] = {
911 {"adis16300", ADIS16300
},
912 {"adis16334", ADIS16334
},
913 {"adis16350", ADIS16350
},
914 {"adis16354", ADIS16350
},
915 {"adis16355", ADIS16350
},
916 {"adis16360", ADIS16360
},
917 {"adis16362", ADIS16362
},
918 {"adis16364", ADIS16364
},
919 {"adis16365", ADIS16360
},
920 {"adis16400", ADIS16400
},
921 {"adis16405", ADIS16400
},
922 {"adis16448", ADIS16448
},
925 MODULE_DEVICE_TABLE(spi
, adis16400_id
);
927 static struct spi_driver adis16400_driver
= {
930 .owner
= THIS_MODULE
,
932 .id_table
= adis16400_id
,
933 .probe
= adis16400_probe
,
934 .remove
= adis16400_remove
,
936 module_spi_driver(adis16400_driver
);
938 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
939 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
940 MODULE_LICENSE("GPL v2");