1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2019 Analog Devices Inc.
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
10 #include <linux/debugfs.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/imu/adis.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/irq.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/property.h>
23 #include <linux/spi/spi.h>
25 #define ADIS16475_REG_DIAG_STAT 0x02
26 #define ADIS16475_REG_X_GYRO_L 0x04
27 #define ADIS16475_REG_Y_GYRO_L 0x08
28 #define ADIS16475_REG_Z_GYRO_L 0x0C
29 #define ADIS16475_REG_X_ACCEL_L 0x10
30 #define ADIS16475_REG_Y_ACCEL_L 0x14
31 #define ADIS16475_REG_Z_ACCEL_L 0x18
32 #define ADIS16475_REG_TEMP_OUT 0x1c
33 #define ADIS16475_REG_X_GYRO_BIAS_L 0x40
34 #define ADIS16475_REG_Y_GYRO_BIAS_L 0x44
35 #define ADIS16475_REG_Z_GYRO_BIAS_L 0x48
36 #define ADIS16475_REG_X_ACCEL_BIAS_L 0x4c
37 #define ADIS16475_REG_Y_ACCEL_BIAS_L 0x50
38 #define ADIS16475_REG_Z_ACCEL_BIAS_L 0x54
39 #define ADIS16475_REG_FILT_CTRL 0x5c
40 #define ADIS16475_FILT_CTRL_MASK GENMASK(2, 0)
41 #define ADIS16475_FILT_CTRL(x) FIELD_PREP(ADIS16475_FILT_CTRL_MASK, x)
42 #define ADIS16475_REG_MSG_CTRL 0x60
43 #define ADIS16475_MSG_CTRL_DR_POL_MASK BIT(0)
44 #define ADIS16475_MSG_CTRL_DR_POL(x) \
45 FIELD_PREP(ADIS16475_MSG_CTRL_DR_POL_MASK, x)
46 #define ADIS16475_SYNC_MODE_MASK GENMASK(4, 2)
47 #define ADIS16475_SYNC_MODE(x) FIELD_PREP(ADIS16475_SYNC_MODE_MASK, x)
48 #define ADIS16475_REG_UP_SCALE 0x62
49 #define ADIS16475_REG_DEC_RATE 0x64
50 #define ADIS16475_REG_GLOB_CMD 0x68
51 #define ADIS16475_REG_FIRM_REV 0x6c
52 #define ADIS16475_REG_FIRM_DM 0x6e
53 #define ADIS16475_REG_FIRM_Y 0x70
54 #define ADIS16475_REG_PROD_ID 0x72
55 #define ADIS16475_REG_SERIAL_NUM 0x74
56 #define ADIS16475_REG_FLASH_CNT 0x7c
57 #define ADIS16500_BURST32_MASK BIT(9)
58 #define ADIS16500_BURST32(x) FIELD_PREP(ADIS16500_BURST32_MASK, x)
59 /* number of data elements in burst mode */
60 #define ADIS16475_BURST32_MAX_DATA 32
61 #define ADIS16475_BURST_MAX_DATA 20
62 #define ADIS16475_MAX_SCAN_DATA 20
63 /* spi max speed in brust mode */
64 #define ADIS16475_BURST_MAX_SPEED 1000000
65 #define ADIS16475_LSB_DEC_MASK BIT(0)
66 #define ADIS16475_LSB_FIR_MASK BIT(1)
69 ADIS16475_SYNC_DIRECT
= 1,
70 ADIS16475_SYNC_SCALED
,
71 ADIS16475_SYNC_OUTPUT
,
72 ADIS16475_SYNC_PULSE
= 5,
75 struct adis16475_sync
{
81 struct adis16475_chip_info
{
82 const struct iio_chan_spec
*channels
;
83 const struct adis16475_sync
*sync
;
84 const struct adis_data adis_data
;
99 const struct adis16475_chip_info
*info
;
103 unsigned long lsb_flag
;
104 /* Alignment needed for the timestamp */
105 __be16 data
[ADIS16475_MAX_SCAN_DATA
] __aligned(8);
109 ADIS16475_SCAN_GYRO_X
,
110 ADIS16475_SCAN_GYRO_Y
,
111 ADIS16475_SCAN_GYRO_Z
,
112 ADIS16475_SCAN_ACCEL_X
,
113 ADIS16475_SCAN_ACCEL_Y
,
114 ADIS16475_SCAN_ACCEL_Z
,
116 ADIS16475_SCAN_DIAG_S_FLAGS
,
117 ADIS16475_SCAN_CRC_FAILURE
,
120 #ifdef CONFIG_DEBUG_FS
121 static ssize_t
adis16475_show_firmware_revision(struct file
*file
,
122 char __user
*userbuf
,
123 size_t count
, loff_t
*ppos
)
125 struct adis16475
*st
= file
->private_data
;
131 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_FIRM_REV
, &rev
);
135 len
= scnprintf(buf
, sizeof(buf
), "%x.%x\n", rev
>> 8, rev
& 0xff);
137 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
140 static const struct file_operations adis16475_firmware_revision_fops
= {
142 .read
= adis16475_show_firmware_revision
,
143 .llseek
= default_llseek
,
144 .owner
= THIS_MODULE
,
147 static ssize_t
adis16475_show_firmware_date(struct file
*file
,
148 char __user
*userbuf
,
149 size_t count
, loff_t
*ppos
)
151 struct adis16475
*st
= file
->private_data
;
157 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_FIRM_Y
, &year
);
161 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_FIRM_DM
, &md
);
165 len
= snprintf(buf
, sizeof(buf
), "%.2x-%.2x-%.4x\n", md
>> 8, md
& 0xff,
168 return simple_read_from_buffer(userbuf
, count
, ppos
, buf
, len
);
171 static const struct file_operations adis16475_firmware_date_fops
= {
173 .read
= adis16475_show_firmware_date
,
174 .llseek
= default_llseek
,
175 .owner
= THIS_MODULE
,
178 static int adis16475_show_serial_number(void *arg
, u64
*val
)
180 struct adis16475
*st
= arg
;
184 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_SERIAL_NUM
, &serial
);
192 DEFINE_DEBUGFS_ATTRIBUTE(adis16475_serial_number_fops
,
193 adis16475_show_serial_number
, NULL
, "0x%.4llx\n");
195 static int adis16475_show_product_id(void *arg
, u64
*val
)
197 struct adis16475
*st
= arg
;
201 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_PROD_ID
, &prod_id
);
209 DEFINE_DEBUGFS_ATTRIBUTE(adis16475_product_id_fops
,
210 adis16475_show_product_id
, NULL
, "%llu\n");
212 static int adis16475_show_flash_count(void *arg
, u64
*val
)
214 struct adis16475
*st
= arg
;
218 ret
= adis_read_reg_32(&st
->adis
, ADIS16475_REG_FLASH_CNT
,
227 DEFINE_DEBUGFS_ATTRIBUTE(adis16475_flash_count_fops
,
228 adis16475_show_flash_count
, NULL
, "%lld\n");
230 static void adis16475_debugfs_init(struct iio_dev
*indio_dev
)
232 struct adis16475
*st
= iio_priv(indio_dev
);
233 struct dentry
*d
= iio_get_debugfs_dentry(indio_dev
);
235 debugfs_create_file_unsafe("serial_number", 0400,
236 d
, st
, &adis16475_serial_number_fops
);
237 debugfs_create_file_unsafe("product_id", 0400,
238 d
, st
, &adis16475_product_id_fops
);
239 debugfs_create_file_unsafe("flash_count", 0400,
240 d
, st
, &adis16475_flash_count_fops
);
241 debugfs_create_file("firmware_revision", 0400,
242 d
, st
, &adis16475_firmware_revision_fops
);
243 debugfs_create_file("firmware_date", 0400, d
,
244 st
, &adis16475_firmware_date_fops
);
247 static void adis16475_debugfs_init(struct iio_dev
*indio_dev
)
252 static int adis16475_get_freq(struct adis16475
*st
, u32
*freq
)
257 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_DEC_RATE
, &dec
);
261 *freq
= DIV_ROUND_CLOSEST(st
->clk_freq
, dec
+ 1);
266 static int adis16475_set_freq(struct adis16475
*st
, const u32 freq
)
274 dec
= DIV_ROUND_CLOSEST(st
->clk_freq
, freq
);
279 if (dec
> st
->info
->max_dec
)
280 dec
= st
->info
->max_dec
;
282 ret
= adis_write_reg_16(&st
->adis
, ADIS16475_REG_DEC_RATE
, dec
);
287 * If decimation is used, then gyro and accel data will have meaningful
288 * bits on the LSB registers. This info is used on the trigger handler.
290 assign_bit(ADIS16475_LSB_DEC_MASK
, &st
->lsb_flag
, dec
);
295 /* The values are approximated. */
296 static const u32 adis16475_3db_freqs
[] = {
297 [0] = 720, /* Filter disabled, full BW (~720Hz) */
306 static int adis16475_get_filter(struct adis16475
*st
, u32
*filter
)
310 const int mask
= ADIS16475_FILT_CTRL_MASK
;
312 ret
= adis_read_reg_16(&st
->adis
, ADIS16475_REG_FILT_CTRL
, &filter_sz
);
316 *filter
= adis16475_3db_freqs
[filter_sz
& mask
];
321 static int adis16475_set_filter(struct adis16475
*st
, const u32 filter
)
323 int i
= ARRAY_SIZE(adis16475_3db_freqs
);
327 if (adis16475_3db_freqs
[i
] >= filter
)
331 ret
= adis_write_reg_16(&st
->adis
, ADIS16475_REG_FILT_CTRL
,
332 ADIS16475_FILT_CTRL(i
));
337 * If FIR is used, then gyro and accel data will have meaningful
338 * bits on the LSB registers. This info is used on the trigger handler.
340 assign_bit(ADIS16475_LSB_FIR_MASK
, &st
->lsb_flag
, i
);
345 static const u32 adis16475_calib_regs
[] = {
346 [ADIS16475_SCAN_GYRO_X
] = ADIS16475_REG_X_GYRO_BIAS_L
,
347 [ADIS16475_SCAN_GYRO_Y
] = ADIS16475_REG_Y_GYRO_BIAS_L
,
348 [ADIS16475_SCAN_GYRO_Z
] = ADIS16475_REG_Z_GYRO_BIAS_L
,
349 [ADIS16475_SCAN_ACCEL_X
] = ADIS16475_REG_X_ACCEL_BIAS_L
,
350 [ADIS16475_SCAN_ACCEL_Y
] = ADIS16475_REG_Y_ACCEL_BIAS_L
,
351 [ADIS16475_SCAN_ACCEL_Z
] = ADIS16475_REG_Z_ACCEL_BIAS_L
,
354 static int adis16475_read_raw(struct iio_dev
*indio_dev
,
355 const struct iio_chan_spec
*chan
,
356 int *val
, int *val2
, long info
)
358 struct adis16475
*st
= iio_priv(indio_dev
);
363 case IIO_CHAN_INFO_RAW
:
364 return adis_single_conversion(indio_dev
, chan
, 0, val
);
365 case IIO_CHAN_INFO_SCALE
:
366 switch (chan
->type
) {
368 *val
= st
->info
->gyro_max_val
;
369 *val2
= st
->info
->gyro_max_scale
;
370 return IIO_VAL_FRACTIONAL
;
372 *val
= st
->info
->accel_max_val
;
373 *val2
= st
->info
->accel_max_scale
;
374 return IIO_VAL_FRACTIONAL
;
376 *val
= st
->info
->temp_scale
;
381 case IIO_CHAN_INFO_CALIBBIAS
:
382 ret
= adis_read_reg_32(&st
->adis
,
383 adis16475_calib_regs
[chan
->scan_index
],
389 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
390 ret
= adis16475_get_filter(st
, val
);
395 case IIO_CHAN_INFO_SAMP_FREQ
:
396 ret
= adis16475_get_freq(st
, &tmp
);
401 *val2
= (tmp
% 1000) * 1000;
402 return IIO_VAL_INT_PLUS_MICRO
;
408 static int adis16475_write_raw(struct iio_dev
*indio_dev
,
409 const struct iio_chan_spec
*chan
,
410 int val
, int val2
, long info
)
412 struct adis16475
*st
= iio_priv(indio_dev
);
416 case IIO_CHAN_INFO_SAMP_FREQ
:
417 tmp
= val
* 1000 + val2
/ 1000;
418 return adis16475_set_freq(st
, tmp
);
419 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
420 return adis16475_set_filter(st
, val
);
421 case IIO_CHAN_INFO_CALIBBIAS
:
422 return adis_write_reg_32(&st
->adis
,
423 adis16475_calib_regs
[chan
->scan_index
],
430 #define ADIS16475_MOD_CHAN(_type, _mod, _address, _si, _r_bits, _s_bits) \
434 .channel2 = (_mod), \
435 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
436 BIT(IIO_CHAN_INFO_CALIBBIAS), \
437 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
438 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
439 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
440 .address = (_address), \
441 .scan_index = (_si), \
444 .realbits = (_r_bits), \
445 .storagebits = (_s_bits), \
446 .endianness = IIO_BE, \
450 #define ADIS16475_GYRO_CHANNEL(_mod) \
451 ADIS16475_MOD_CHAN(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
452 ADIS16475_REG_ ## _mod ## _GYRO_L, \
453 ADIS16475_SCAN_GYRO_ ## _mod, 32, 32)
455 #define ADIS16475_ACCEL_CHANNEL(_mod) \
456 ADIS16475_MOD_CHAN(IIO_ACCEL, IIO_MOD_ ## _mod, \
457 ADIS16475_REG_ ## _mod ## _ACCEL_L, \
458 ADIS16475_SCAN_ACCEL_ ## _mod, 32, 32)
460 #define ADIS16475_TEMP_CHANNEL() { \
464 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
465 BIT(IIO_CHAN_INFO_SCALE), \
466 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
467 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
468 .address = ADIS16475_REG_TEMP_OUT, \
469 .scan_index = ADIS16475_SCAN_TEMP, \
474 .endianness = IIO_BE, \
478 static const struct iio_chan_spec adis16475_channels
[] = {
479 ADIS16475_GYRO_CHANNEL(X
),
480 ADIS16475_GYRO_CHANNEL(Y
),
481 ADIS16475_GYRO_CHANNEL(Z
),
482 ADIS16475_ACCEL_CHANNEL(X
),
483 ADIS16475_ACCEL_CHANNEL(Y
),
484 ADIS16475_ACCEL_CHANNEL(Z
),
485 ADIS16475_TEMP_CHANNEL(),
486 IIO_CHAN_SOFT_TIMESTAMP(7)
489 enum adis16475_variant
{
513 ADIS16475_DIAG_STAT_DATA_PATH
= 1,
514 ADIS16475_DIAG_STAT_FLASH_MEM
,
515 ADIS16475_DIAG_STAT_SPI
,
516 ADIS16475_DIAG_STAT_STANDBY
,
517 ADIS16475_DIAG_STAT_SENSOR
,
518 ADIS16475_DIAG_STAT_MEMORY
,
519 ADIS16475_DIAG_STAT_CLK
,
522 static const char * const adis16475_status_error_msgs
[] = {
523 [ADIS16475_DIAG_STAT_DATA_PATH
] = "Data Path Overrun",
524 [ADIS16475_DIAG_STAT_FLASH_MEM
] = "Flash memory update failure",
525 [ADIS16475_DIAG_STAT_SPI
] = "SPI communication error",
526 [ADIS16475_DIAG_STAT_STANDBY
] = "Standby mode",
527 [ADIS16475_DIAG_STAT_SENSOR
] = "Sensor failure",
528 [ADIS16475_DIAG_STAT_MEMORY
] = "Memory failure",
529 [ADIS16475_DIAG_STAT_CLK
] = "Clock error",
532 static int adis16475_enable_irq(struct adis
*adis
, bool enable
)
535 * There is no way to gate the data-ready signal internally inside the
536 * ADIS16475. We can only control it's polarity...
539 enable_irq(adis
->spi
->irq
);
541 disable_irq(adis
->spi
->irq
);
546 #define ADIS16475_DATA(_prod_id, _timeouts) \
548 .msc_ctrl_reg = ADIS16475_REG_MSG_CTRL, \
549 .glob_cmd_reg = ADIS16475_REG_GLOB_CMD, \
550 .diag_stat_reg = ADIS16475_REG_DIAG_STAT, \
551 .prod_id_reg = ADIS16475_REG_PROD_ID, \
552 .prod_id = (_prod_id), \
553 .self_test_mask = BIT(2), \
554 .self_test_reg = ADIS16475_REG_GLOB_CMD, \
555 .cs_change_delay = 16, \
558 .status_error_msgs = adis16475_status_error_msgs, \
559 .status_error_mask = BIT(ADIS16475_DIAG_STAT_DATA_PATH) | \
560 BIT(ADIS16475_DIAG_STAT_FLASH_MEM) | \
561 BIT(ADIS16475_DIAG_STAT_SPI) | \
562 BIT(ADIS16475_DIAG_STAT_STANDBY) | \
563 BIT(ADIS16475_DIAG_STAT_SENSOR) | \
564 BIT(ADIS16475_DIAG_STAT_MEMORY) | \
565 BIT(ADIS16475_DIAG_STAT_CLK), \
566 .enable_irq = adis16475_enable_irq, \
567 .timeouts = (_timeouts), \
568 .burst_reg_cmd = ADIS16475_REG_GLOB_CMD, \
569 .burst_len = ADIS16475_BURST_MAX_DATA, \
570 .burst_max_len = ADIS16475_BURST32_MAX_DATA \
573 static const struct adis16475_sync adis16475_sync_mode
[] = {
574 { ADIS16475_SYNC_OUTPUT
},
575 { ADIS16475_SYNC_DIRECT
, 1900, 2100 },
576 { ADIS16475_SYNC_SCALED
, 1, 128 },
577 { ADIS16475_SYNC_PULSE
, 1000, 2100 },
580 static const struct adis_timeout adis16475_timeouts
= {
586 static const struct adis_timeout adis1650x_timeouts
= {
592 static const struct adis16475_chip_info adis16475_chip_info
[] = {
595 .num_channels
= ARRAY_SIZE(adis16475_channels
),
596 .channels
= adis16475_channels
,
598 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
600 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
604 .sync
= adis16475_sync_mode
,
605 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
606 .adis_data
= ADIS16475_DATA(16470, &adis16475_timeouts
),
609 .name
= "adis16475-1",
610 .num_channels
= ARRAY_SIZE(adis16475_channels
),
611 .channels
= adis16475_channels
,
613 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
615 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
619 .sync
= adis16475_sync_mode
,
620 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
621 .adis_data
= ADIS16475_DATA(16475, &adis16475_timeouts
),
624 .name
= "adis16475-2",
625 .num_channels
= ARRAY_SIZE(adis16475_channels
),
626 .channels
= adis16475_channels
,
628 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
630 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
634 .sync
= adis16475_sync_mode
,
635 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
636 .adis_data
= ADIS16475_DATA(16475, &adis16475_timeouts
),
639 .name
= "adis16475-3",
640 .num_channels
= ARRAY_SIZE(adis16475_channels
),
641 .channels
= adis16475_channels
,
643 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
645 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
649 .sync
= adis16475_sync_mode
,
650 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
651 .adis_data
= ADIS16475_DATA(16475, &adis16475_timeouts
),
654 .name
= "adis16477-1",
655 .num_channels
= ARRAY_SIZE(adis16475_channels
),
656 .channels
= adis16475_channels
,
658 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
660 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
664 .sync
= adis16475_sync_mode
,
665 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
666 .adis_data
= ADIS16475_DATA(16477, &adis16475_timeouts
),
669 .name
= "adis16477-2",
670 .num_channels
= ARRAY_SIZE(adis16475_channels
),
671 .channels
= adis16475_channels
,
673 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
675 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
679 .sync
= adis16475_sync_mode
,
680 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
681 .adis_data
= ADIS16475_DATA(16477, &adis16475_timeouts
),
684 .name
= "adis16477-3",
685 .num_channels
= ARRAY_SIZE(adis16475_channels
),
686 .channels
= adis16475_channels
,
688 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
690 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
694 .sync
= adis16475_sync_mode
,
695 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
696 .adis_data
= ADIS16475_DATA(16477, &adis16475_timeouts
),
699 .name
= "adis16465-1",
700 .num_channels
= ARRAY_SIZE(adis16475_channels
),
701 .channels
= adis16475_channels
,
703 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
705 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
709 .sync
= adis16475_sync_mode
,
710 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
711 .adis_data
= ADIS16475_DATA(16465, &adis16475_timeouts
),
714 .name
= "adis16465-2",
715 .num_channels
= ARRAY_SIZE(adis16475_channels
),
716 .channels
= adis16475_channels
,
718 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
720 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
724 .sync
= adis16475_sync_mode
,
725 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
726 .adis_data
= ADIS16475_DATA(16465, &adis16475_timeouts
),
729 .name
= "adis16465-3",
730 .num_channels
= ARRAY_SIZE(adis16475_channels
),
731 .channels
= adis16475_channels
,
733 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
735 .accel_max_scale
= IIO_M_S_2_TO_G(4000 << 16),
739 .sync
= adis16475_sync_mode
,
740 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
741 .adis_data
= ADIS16475_DATA(16465, &adis16475_timeouts
),
744 .name
= "adis16467-1",
745 .num_channels
= ARRAY_SIZE(adis16475_channels
),
746 .channels
= adis16475_channels
,
748 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
750 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
754 .sync
= adis16475_sync_mode
,
755 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
756 .adis_data
= ADIS16475_DATA(16467, &adis16475_timeouts
),
759 .name
= "adis16467-2",
760 .num_channels
= ARRAY_SIZE(adis16475_channels
),
761 .channels
= adis16475_channels
,
763 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
765 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
769 .sync
= adis16475_sync_mode
,
770 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
771 .adis_data
= ADIS16475_DATA(16467, &adis16475_timeouts
),
774 .name
= "adis16467-3",
775 .num_channels
= ARRAY_SIZE(adis16475_channels
),
776 .channels
= adis16475_channels
,
778 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
780 .accel_max_scale
= IIO_M_S_2_TO_G(800 << 16),
784 .sync
= adis16475_sync_mode
,
785 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
),
786 .adis_data
= ADIS16475_DATA(16467, &adis16475_timeouts
),
790 .num_channels
= ARRAY_SIZE(adis16475_channels
),
791 .channels
= adis16475_channels
,
793 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
794 .accel_max_val
= 392,
795 .accel_max_scale
= 32000 << 16,
799 .sync
= adis16475_sync_mode
,
800 /* pulse sync not supported */
801 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
803 .adis_data
= ADIS16475_DATA(16500, &adis1650x_timeouts
),
806 .name
= "adis16505-1",
807 .num_channels
= ARRAY_SIZE(adis16475_channels
),
808 .channels
= adis16475_channels
,
810 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
812 .accel_max_scale
= 32000 << 16,
816 .sync
= adis16475_sync_mode
,
817 /* pulse sync not supported */
818 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
820 .adis_data
= ADIS16475_DATA(16505, &adis1650x_timeouts
),
823 .name
= "adis16505-2",
824 .num_channels
= ARRAY_SIZE(adis16475_channels
),
825 .channels
= adis16475_channels
,
827 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
829 .accel_max_scale
= 32000 << 16,
833 .sync
= adis16475_sync_mode
,
834 /* pulse sync not supported */
835 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
837 .adis_data
= ADIS16475_DATA(16505, &adis1650x_timeouts
),
840 .name
= "adis16505-3",
841 .num_channels
= ARRAY_SIZE(adis16475_channels
),
842 .channels
= adis16475_channels
,
844 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
846 .accel_max_scale
= 32000 << 16,
850 .sync
= adis16475_sync_mode
,
851 /* pulse sync not supported */
852 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
854 .adis_data
= ADIS16475_DATA(16505, &adis1650x_timeouts
),
857 .name
= "adis16507-1",
858 .num_channels
= ARRAY_SIZE(adis16475_channels
),
859 .channels
= adis16475_channels
,
861 .gyro_max_scale
= IIO_RAD_TO_DEGREE(160 << 16),
862 .accel_max_val
= 392,
863 .accel_max_scale
= 32000 << 16,
867 .sync
= adis16475_sync_mode
,
868 /* pulse sync not supported */
869 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
871 .adis_data
= ADIS16475_DATA(16507, &adis1650x_timeouts
),
874 .name
= "adis16507-2",
875 .num_channels
= ARRAY_SIZE(adis16475_channels
),
876 .channels
= adis16475_channels
,
878 .gyro_max_scale
= IIO_RAD_TO_DEGREE(40 << 16),
879 .accel_max_val
= 392,
880 .accel_max_scale
= 32000 << 16,
884 .sync
= adis16475_sync_mode
,
885 /* pulse sync not supported */
886 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
888 .adis_data
= ADIS16475_DATA(16507, &adis1650x_timeouts
),
891 .name
= "adis16507-3",
892 .num_channels
= ARRAY_SIZE(adis16475_channels
),
893 .channels
= adis16475_channels
,
895 .gyro_max_scale
= IIO_RAD_TO_DEGREE(10 << 16),
896 .accel_max_val
= 392,
897 .accel_max_scale
= 32000 << 16,
901 .sync
= adis16475_sync_mode
,
902 /* pulse sync not supported */
903 .num_sync
= ARRAY_SIZE(adis16475_sync_mode
) - 1,
905 .adis_data
= ADIS16475_DATA(16507, &adis1650x_timeouts
),
909 static const struct iio_info adis16475_info
= {
910 .read_raw
= &adis16475_read_raw
,
911 .write_raw
= &adis16475_write_raw
,
912 .update_scan_mode
= adis_update_scan_mode
,
913 .debugfs_reg_access
= adis_debugfs_reg_access
,
916 static bool adis16475_validate_crc(const u8
*buffer
, u16 crc
,
920 /* extra 6 elements for low gyro and accel */
921 const u16 sz
= burst32
? ADIS16475_BURST32_MAX_DATA
:
922 ADIS16475_BURST_MAX_DATA
;
924 for (i
= 0; i
< sz
- 2; i
++)
930 static void adis16475_burst32_check(struct adis16475
*st
)
933 struct adis
*adis
= &st
->adis
;
935 if (!st
->info
->has_burst32
)
938 if (st
->lsb_flag
&& !st
->burst32
) {
939 const u16 en
= ADIS16500_BURST32(1);
941 ret
= __adis_update_bits(&st
->adis
, ADIS16475_REG_MSG_CTRL
,
942 ADIS16500_BURST32_MASK
, en
);
949 * In 32-bit mode we need extra 2 bytes for all gyro
950 * and accel channels.
952 adis
->burst_extra_len
= 6 * sizeof(u16
);
953 adis
->xfer
[1].len
+= 6 * sizeof(u16
);
954 dev_dbg(&adis
->spi
->dev
, "Enable burst32 mode, xfer:%d",
957 } else if (!st
->lsb_flag
&& st
->burst32
) {
958 const u16 en
= ADIS16500_BURST32(0);
960 ret
= __adis_update_bits(&st
->adis
, ADIS16475_REG_MSG_CTRL
,
961 ADIS16500_BURST32_MASK
, en
);
967 /* Remove the extra bits */
968 adis
->burst_extra_len
= 0;
969 adis
->xfer
[1].len
-= 6 * sizeof(u16
);
970 dev_dbg(&adis
->spi
->dev
, "Disable burst32 mode, xfer:%d\n",
975 static irqreturn_t
adis16475_trigger_handler(int irq
, void *p
)
977 struct iio_poll_func
*pf
= p
;
978 struct iio_dev
*indio_dev
= pf
->indio_dev
;
979 struct adis16475
*st
= iio_priv(indio_dev
);
980 struct adis
*adis
= &st
->adis
;
985 /* offset until the first element after gyro and accel */
986 const u8 offset
= st
->burst32
? 13 : 7;
987 const u32 cached_spi_speed_hz
= adis
->spi
->max_speed_hz
;
989 adis
->spi
->max_speed_hz
= ADIS16475_BURST_MAX_SPEED
;
991 ret
= spi_sync(adis
->spi
, &adis
->msg
);
995 adis
->spi
->max_speed_hz
= cached_spi_speed_hz
;
996 buffer
= adis
->buffer
;
998 crc
= be16_to_cpu(buffer
[offset
+ 2]);
999 valid
= adis16475_validate_crc(adis
->buffer
, crc
, st
->burst32
);
1001 dev_err(&adis
->spi
->dev
, "Invalid crc\n");
1005 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1006 indio_dev
->masklength
) {
1008 * When burst mode is used, system flags is the first data
1009 * channel in the sequence, but the scan index is 7.
1012 case ADIS16475_SCAN_TEMP
:
1013 st
->data
[i
++] = buffer
[offset
];
1015 case ADIS16475_SCAN_GYRO_X
... ADIS16475_SCAN_ACCEL_Z
:
1017 * The first 2 bytes on the received data are the
1018 * DIAG_STAT reg, hence the +1 offset here...
1022 st
->data
[i
++] = buffer
[bit
* 2 + 2];
1024 st
->data
[i
++] = buffer
[bit
* 2 + 1];
1026 st
->data
[i
++] = buffer
[bit
+ 1];
1028 * Don't bother in doing the manual read if the
1029 * device supports burst32. burst32 will be
1030 * enabled in the next call to
1031 * adis16475_burst32_check()...
1033 if (st
->lsb_flag
&& !st
->info
->has_burst32
) {
1035 const u32 reg
= ADIS16475_REG_X_GYRO_L
+
1038 adis_read_reg_16(adis
, reg
, &val
);
1039 st
->data
[i
++] = cpu_to_be16(val
);
1041 /* lower not used */
1049 iio_push_to_buffers_with_timestamp(indio_dev
, st
->data
, pf
->timestamp
);
1052 * We only check the burst mode at the end of the current capture since
1053 * it takes a full data ready cycle for the device to update the burst
1056 adis16475_burst32_check(st
);
1057 iio_trigger_notify_done(indio_dev
->trig
);
1062 static void adis16475_disable_clk(void *data
)
1064 clk_disable_unprepare((struct clk
*)data
);
1067 static int adis16475_config_sync_mode(struct adis16475
*st
)
1070 struct device
*dev
= &st
->adis
.spi
->dev
;
1071 const struct adis16475_sync
*sync
;
1074 /* default to internal clk */
1075 st
->clk_freq
= st
->info
->int_clk
* 1000;
1077 ret
= device_property_read_u32(dev
, "adi,sync-mode", &sync_mode
);
1081 if (sync_mode
>= st
->info
->num_sync
) {
1082 dev_err(dev
, "Invalid sync mode: %u for %s\n", sync_mode
,
1087 sync
= &st
->info
->sync
[sync_mode
];
1089 /* All the other modes require external input signal */
1090 if (sync
->sync_mode
!= ADIS16475_SYNC_OUTPUT
) {
1091 struct clk
*clk
= devm_clk_get(dev
, NULL
);
1094 return PTR_ERR(clk
);
1096 ret
= clk_prepare_enable(clk
);
1100 ret
= devm_add_action_or_reset(dev
, adis16475_disable_clk
, clk
);
1104 st
->clk_freq
= clk_get_rate(clk
);
1105 if (st
->clk_freq
< sync
->min_rate
||
1106 st
->clk_freq
> sync
->max_rate
) {
1108 "Clk rate:%u not in a valid range:[%u %u]\n",
1109 st
->clk_freq
, sync
->min_rate
, sync
->max_rate
);
1113 if (sync
->sync_mode
== ADIS16475_SYNC_SCALED
) {
1115 u32 scaled_out_freq
= 0;
1117 * If we are in scaled mode, we must have an up_scale.
1118 * In scaled mode the allowable input clock range is
1119 * 1 Hz to 128 Hz, and the allowable output range is
1120 * 1900 to 2100 Hz. Hence, a scale must be given to
1121 * get the allowable output.
1123 ret
= device_property_read_u32(dev
,
1124 "adi,scaled-output-hz",
1127 dev_err(dev
, "adi,scaled-output-hz must be given when in scaled sync mode");
1129 } else if (scaled_out_freq
< 1900 ||
1130 scaled_out_freq
> 2100) {
1131 dev_err(dev
, "Invalid value: %u for adi,scaled-output-hz",
1136 up_scale
= DIV_ROUND_CLOSEST(scaled_out_freq
,
1139 ret
= __adis_write_reg_16(&st
->adis
,
1140 ADIS16475_REG_UP_SCALE
,
1145 st
->clk_freq
= scaled_out_freq
;
1148 st
->clk_freq
*= 1000;
1151 * Keep in mind that the mask for the clk modes in adis1650*
1152 * chips is different (1100 instead of 11100). However, we
1153 * are not configuring BIT(4) in these chips and the default
1154 * value is 0, so we are fine in doing the below operations.
1155 * I'm keeping this for simplicity and avoiding extra variables
1158 ret
= __adis_update_bits(&st
->adis
, ADIS16475_REG_MSG_CTRL
,
1159 ADIS16475_SYNC_MODE_MASK
, sync
->sync_mode
);
1163 usleep_range(250, 260);
1168 static int adis16475_config_irq_pin(struct adis16475
*st
)
1171 struct irq_data
*desc
;
1175 struct spi_device
*spi
= st
->adis
.spi
;
1177 desc
= irq_get_irq_data(spi
->irq
);
1179 dev_err(&spi
->dev
, "Could not find IRQ %d\n", spi
->irq
);
1183 * It is possible to configure the data ready polarity. Furthermore, we
1184 * need to update the adis struct if we want data ready as active low.
1186 irq_type
= irqd_get_trigger_type(desc
);
1187 if (irq_type
== IRQ_TYPE_EDGE_RISING
) {
1189 st
->adis
.irq_flag
= IRQF_TRIGGER_RISING
;
1190 } else if (irq_type
== IRQ_TYPE_EDGE_FALLING
) {
1192 st
->adis
.irq_flag
= IRQF_TRIGGER_FALLING
;
1194 dev_err(&spi
->dev
, "Invalid interrupt type 0x%x specified\n",
1199 val
= ADIS16475_MSG_CTRL_DR_POL(polarity
);
1200 ret
= __adis_update_bits(&st
->adis
, ADIS16475_REG_MSG_CTRL
,
1201 ADIS16475_MSG_CTRL_DR_POL_MASK
, val
);
1205 * There is a delay writing to any bits written to the MSC_CTRL
1206 * register. It should not be bigger than 200us, so 250 should be more
1209 usleep_range(250, 260);
1214 static const struct of_device_id adis16475_of_match
[] = {
1215 { .compatible
= "adi,adis16470",
1216 .data
= &adis16475_chip_info
[ADIS16470
] },
1217 { .compatible
= "adi,adis16475-1",
1218 .data
= &adis16475_chip_info
[ADIS16475_1
] },
1219 { .compatible
= "adi,adis16475-2",
1220 .data
= &adis16475_chip_info
[ADIS16475_2
] },
1221 { .compatible
= "adi,adis16475-3",
1222 .data
= &adis16475_chip_info
[ADIS16475_3
] },
1223 { .compatible
= "adi,adis16477-1",
1224 .data
= &adis16475_chip_info
[ADIS16477_1
] },
1225 { .compatible
= "adi,adis16477-2",
1226 .data
= &adis16475_chip_info
[ADIS16477_2
] },
1227 { .compatible
= "adi,adis16477-3",
1228 .data
= &adis16475_chip_info
[ADIS16477_3
] },
1229 { .compatible
= "adi,adis16465-1",
1230 .data
= &adis16475_chip_info
[ADIS16465_1
] },
1231 { .compatible
= "adi,adis16465-2",
1232 .data
= &adis16475_chip_info
[ADIS16465_2
] },
1233 { .compatible
= "adi,adis16465-3",
1234 .data
= &adis16475_chip_info
[ADIS16465_3
] },
1235 { .compatible
= "adi,adis16467-1",
1236 .data
= &adis16475_chip_info
[ADIS16467_1
] },
1237 { .compatible
= "adi,adis16467-2",
1238 .data
= &adis16475_chip_info
[ADIS16467_2
] },
1239 { .compatible
= "adi,adis16467-3",
1240 .data
= &adis16475_chip_info
[ADIS16467_3
] },
1241 { .compatible
= "adi,adis16500",
1242 .data
= &adis16475_chip_info
[ADIS16500
] },
1243 { .compatible
= "adi,adis16505-1",
1244 .data
= &adis16475_chip_info
[ADIS16505_1
] },
1245 { .compatible
= "adi,adis16505-2",
1246 .data
= &adis16475_chip_info
[ADIS16505_2
] },
1247 { .compatible
= "adi,adis16505-3",
1248 .data
= &adis16475_chip_info
[ADIS16505_3
] },
1249 { .compatible
= "adi,adis16507-1",
1250 .data
= &adis16475_chip_info
[ADIS16507_1
] },
1251 { .compatible
= "adi,adis16507-2",
1252 .data
= &adis16475_chip_info
[ADIS16507_2
] },
1253 { .compatible
= "adi,adis16507-3",
1254 .data
= &adis16475_chip_info
[ADIS16507_3
] },
1257 MODULE_DEVICE_TABLE(of
, adis16475_of_match
);
1259 static int adis16475_probe(struct spi_device
*spi
)
1261 struct iio_dev
*indio_dev
;
1262 struct adis16475
*st
;
1265 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
1269 st
= iio_priv(indio_dev
);
1270 spi_set_drvdata(spi
, indio_dev
);
1272 st
->info
= device_get_match_data(&spi
->dev
);
1276 ret
= adis_init(&st
->adis
, indio_dev
, spi
, &st
->info
->adis_data
);
1280 indio_dev
->name
= st
->info
->name
;
1281 indio_dev
->channels
= st
->info
->channels
;
1282 indio_dev
->num_channels
= st
->info
->num_channels
;
1283 indio_dev
->info
= &adis16475_info
;
1284 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1286 ret
= __adis_initial_startup(&st
->adis
);
1290 ret
= adis16475_config_irq_pin(st
);
1294 ret
= adis16475_config_sync_mode(st
);
1298 ret
= devm_adis_setup_buffer_and_trigger(&st
->adis
, indio_dev
,
1299 adis16475_trigger_handler
);
1303 adis16475_enable_irq(&st
->adis
, false);
1305 ret
= devm_iio_device_register(&spi
->dev
, indio_dev
);
1309 adis16475_debugfs_init(indio_dev
);
1314 static struct spi_driver adis16475_driver
= {
1316 .name
= "adis16475",
1317 .of_match_table
= adis16475_of_match
,
1319 .probe
= adis16475_probe
,
1321 module_spi_driver(adis16475_driver
);
1323 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1324 MODULE_DESCRIPTION("Analog Devices ADIS16475 IMU driver");
1325 MODULE_LICENSE("GPL");