1 // SPDX-License-Identifier: GPL-2.0-only
3 * ADIS16480 and similar IMUs driver
5 * Copyright 2012 Analog Devices Inc.
9 #include <linux/bitfield.h>
10 #include <linux/of_irq.h>
11 #include <linux/interrupt.h>
12 #include <linux/math.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/module.h>
17 #include <linux/lcm.h>
18 #include <linux/swab.h>
19 #include <linux/crc32.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/imu/adis.h>
24 #include <linux/iio/trigger_consumer.h>
26 #include <linux/debugfs.h>
28 #define ADIS16480_PAGE_SIZE 0x80
30 #define ADIS16480_REG(page, reg) ((page) * ADIS16480_PAGE_SIZE + (reg))
32 #define ADIS16480_REG_PAGE_ID 0x00 /* Same address on each page */
33 #define ADIS16480_REG_SEQ_CNT ADIS16480_REG(0x00, 0x06)
34 #define ADIS16480_REG_SYS_E_FLA ADIS16480_REG(0x00, 0x08)
35 #define ADIS16480_REG_DIAG_STS ADIS16480_REG(0x00, 0x0A)
36 #define ADIS16480_REG_ALM_STS ADIS16480_REG(0x00, 0x0C)
37 #define ADIS16480_REG_TEMP_OUT ADIS16480_REG(0x00, 0x0E)
38 #define ADIS16480_REG_X_GYRO_OUT ADIS16480_REG(0x00, 0x10)
39 #define ADIS16480_REG_Y_GYRO_OUT ADIS16480_REG(0x00, 0x14)
40 #define ADIS16480_REG_Z_GYRO_OUT ADIS16480_REG(0x00, 0x18)
41 #define ADIS16480_REG_X_ACCEL_OUT ADIS16480_REG(0x00, 0x1C)
42 #define ADIS16480_REG_Y_ACCEL_OUT ADIS16480_REG(0x00, 0x20)
43 #define ADIS16480_REG_Z_ACCEL_OUT ADIS16480_REG(0x00, 0x24)
44 #define ADIS16480_REG_X_MAGN_OUT ADIS16480_REG(0x00, 0x28)
45 #define ADIS16480_REG_Y_MAGN_OUT ADIS16480_REG(0x00, 0x2A)
46 #define ADIS16480_REG_Z_MAGN_OUT ADIS16480_REG(0x00, 0x2C)
47 #define ADIS16480_REG_BAROM_OUT ADIS16480_REG(0x00, 0x2E)
48 #define ADIS16480_REG_X_DELTAANG_OUT ADIS16480_REG(0x00, 0x40)
49 #define ADIS16480_REG_Y_DELTAANG_OUT ADIS16480_REG(0x00, 0x44)
50 #define ADIS16480_REG_Z_DELTAANG_OUT ADIS16480_REG(0x00, 0x48)
51 #define ADIS16480_REG_X_DELTAVEL_OUT ADIS16480_REG(0x00, 0x4C)
52 #define ADIS16480_REG_Y_DELTAVEL_OUT ADIS16480_REG(0x00, 0x50)
53 #define ADIS16480_REG_Z_DELTAVEL_OUT ADIS16480_REG(0x00, 0x54)
54 #define ADIS16480_REG_PROD_ID ADIS16480_REG(0x00, 0x7E)
56 #define ADIS16480_REG_X_GYRO_SCALE ADIS16480_REG(0x02, 0x04)
57 #define ADIS16480_REG_Y_GYRO_SCALE ADIS16480_REG(0x02, 0x06)
58 #define ADIS16480_REG_Z_GYRO_SCALE ADIS16480_REG(0x02, 0x08)
59 #define ADIS16480_REG_X_ACCEL_SCALE ADIS16480_REG(0x02, 0x0A)
60 #define ADIS16480_REG_Y_ACCEL_SCALE ADIS16480_REG(0x02, 0x0C)
61 #define ADIS16480_REG_Z_ACCEL_SCALE ADIS16480_REG(0x02, 0x0E)
62 #define ADIS16480_REG_X_GYRO_BIAS ADIS16480_REG(0x02, 0x10)
63 #define ADIS16480_REG_Y_GYRO_BIAS ADIS16480_REG(0x02, 0x14)
64 #define ADIS16480_REG_Z_GYRO_BIAS ADIS16480_REG(0x02, 0x18)
65 #define ADIS16480_REG_X_ACCEL_BIAS ADIS16480_REG(0x02, 0x1C)
66 #define ADIS16480_REG_Y_ACCEL_BIAS ADIS16480_REG(0x02, 0x20)
67 #define ADIS16480_REG_Z_ACCEL_BIAS ADIS16480_REG(0x02, 0x24)
68 #define ADIS16480_REG_X_HARD_IRON ADIS16480_REG(0x02, 0x28)
69 #define ADIS16480_REG_Y_HARD_IRON ADIS16480_REG(0x02, 0x2A)
70 #define ADIS16480_REG_Z_HARD_IRON ADIS16480_REG(0x02, 0x2C)
71 #define ADIS16480_REG_BAROM_BIAS ADIS16480_REG(0x02, 0x40)
72 #define ADIS16480_REG_FLASH_CNT ADIS16480_REG(0x02, 0x7C)
74 #define ADIS16480_REG_GLOB_CMD ADIS16480_REG(0x03, 0x02)
75 #define ADIS16480_REG_FNCTIO_CTRL ADIS16480_REG(0x03, 0x06)
76 #define ADIS16480_REG_GPIO_CTRL ADIS16480_REG(0x03, 0x08)
77 #define ADIS16480_REG_CONFIG ADIS16480_REG(0x03, 0x0A)
78 #define ADIS16480_REG_DEC_RATE ADIS16480_REG(0x03, 0x0C)
79 #define ADIS16480_REG_SLP_CNT ADIS16480_REG(0x03, 0x10)
80 #define ADIS16480_REG_FILTER_BNK0 ADIS16480_REG(0x03, 0x16)
81 #define ADIS16480_REG_FILTER_BNK1 ADIS16480_REG(0x03, 0x18)
82 #define ADIS16480_REG_ALM_CNFG0 ADIS16480_REG(0x03, 0x20)
83 #define ADIS16480_REG_ALM_CNFG1 ADIS16480_REG(0x03, 0x22)
84 #define ADIS16480_REG_ALM_CNFG2 ADIS16480_REG(0x03, 0x24)
85 #define ADIS16480_REG_XG_ALM_MAGN ADIS16480_REG(0x03, 0x28)
86 #define ADIS16480_REG_YG_ALM_MAGN ADIS16480_REG(0x03, 0x2A)
87 #define ADIS16480_REG_ZG_ALM_MAGN ADIS16480_REG(0x03, 0x2C)
88 #define ADIS16480_REG_XA_ALM_MAGN ADIS16480_REG(0x03, 0x2E)
89 #define ADIS16480_REG_YA_ALM_MAGN ADIS16480_REG(0x03, 0x30)
90 #define ADIS16480_REG_ZA_ALM_MAGN ADIS16480_REG(0x03, 0x32)
91 #define ADIS16480_REG_XM_ALM_MAGN ADIS16480_REG(0x03, 0x34)
92 #define ADIS16480_REG_YM_ALM_MAGN ADIS16480_REG(0x03, 0x36)
93 #define ADIS16480_REG_ZM_ALM_MAGN ADIS16480_REG(0x03, 0x38)
94 #define ADIS16480_REG_BR_ALM_MAGN ADIS16480_REG(0x03, 0x3A)
95 #define ADIS16480_REG_FIRM_REV ADIS16480_REG(0x03, 0x78)
96 #define ADIS16480_REG_FIRM_DM ADIS16480_REG(0x03, 0x7A)
97 #define ADIS16480_REG_FIRM_Y ADIS16480_REG(0x03, 0x7C)
100 * External clock scaling in PPS mode.
101 * Available only for ADIS1649x devices
103 #define ADIS16495_REG_SYNC_SCALE ADIS16480_REG(0x03, 0x10)
104 #define ADIS16495_REG_BURST_CMD ADIS16480_REG(0x00, 0x7C)
105 #define ADIS16495_BURST_ID 0xA5A5
106 /* total number of segments in burst */
107 #define ADIS16495_BURST_MAX_DATA 20
108 /* spi max speed in burst mode */
109 #define ADIS16495_BURST_MAX_SPEED 6000000
111 #define ADIS16480_REG_SERIAL_NUM ADIS16480_REG(0x04, 0x20)
113 /* Each filter coefficent bank spans two pages */
114 #define ADIS16480_FIR_COEF(page) (x < 60 ? ADIS16480_REG(page, (x) + 8) : \
115 ADIS16480_REG((page) + 1, (x) - 60 + 8))
116 #define ADIS16480_FIR_COEF_A(x) ADIS16480_FIR_COEF(0x05, (x))
117 #define ADIS16480_FIR_COEF_B(x) ADIS16480_FIR_COEF(0x07, (x))
118 #define ADIS16480_FIR_COEF_C(x) ADIS16480_FIR_COEF(0x09, (x))
119 #define ADIS16480_FIR_COEF_D(x) ADIS16480_FIR_COEF(0x0B, (x))
121 /* ADIS16480_REG_FNCTIO_CTRL */
122 #define ADIS16480_DRDY_SEL_MSK GENMASK(1, 0)
123 #define ADIS16480_DRDY_SEL(x) FIELD_PREP(ADIS16480_DRDY_SEL_MSK, x)
124 #define ADIS16480_DRDY_POL_MSK BIT(2)
125 #define ADIS16480_DRDY_POL(x) FIELD_PREP(ADIS16480_DRDY_POL_MSK, x)
126 #define ADIS16480_DRDY_EN_MSK BIT(3)
127 #define ADIS16480_DRDY_EN(x) FIELD_PREP(ADIS16480_DRDY_EN_MSK, x)
128 #define ADIS16480_SYNC_SEL_MSK GENMASK(5, 4)
129 #define ADIS16480_SYNC_SEL(x) FIELD_PREP(ADIS16480_SYNC_SEL_MSK, x)
130 #define ADIS16480_SYNC_EN_MSK BIT(7)
131 #define ADIS16480_SYNC_EN(x) FIELD_PREP(ADIS16480_SYNC_EN_MSK, x)
132 #define ADIS16480_SYNC_MODE_MSK BIT(8)
133 #define ADIS16480_SYNC_MODE(x) FIELD_PREP(ADIS16480_SYNC_MODE_MSK, x)
135 struct adis16480_chip_info {
136 unsigned int num_channels;
137 const struct iio_chan_spec *channels;
138 unsigned int gyro_max_val;
139 unsigned int gyro_max_scale;
140 unsigned int accel_max_val;
141 unsigned int accel_max_scale;
142 unsigned int temp_scale;
143 unsigned int int_clk;
144 unsigned int max_dec_rate;
145 const unsigned int *filter_freqs;
146 bool has_pps_clk_mode;
148 const struct adis_data adis_data;
151 enum adis16480_int_pin {
158 enum adis16480_clock_mode {
165 const struct adis16480_chip_info *chip_info;
169 enum adis16480_clock_mode clk_mode;
170 unsigned int clk_freq;
171 /* Alignment needed for the timestamp */
172 __be16 data[ADIS16495_BURST_MAX_DATA] __aligned(8);
175 static const char * const adis16480_int_pin_names[4] = {
176 [ADIS16480_PIN_DIO1] = "DIO1",
177 [ADIS16480_PIN_DIO2] = "DIO2",
178 [ADIS16480_PIN_DIO3] = "DIO3",
179 [ADIS16480_PIN_DIO4] = "DIO4",
182 static bool low_rate_allow;
183 module_param(low_rate_allow, bool, 0444);
184 MODULE_PARM_DESC(low_rate_allow,
185 "Allow IMU rates below the minimum advisable when external clk is used in PPS mode (default: N)");
187 #ifdef CONFIG_DEBUG_FS
189 static ssize_t adis16480_show_firmware_revision(struct file *file,
190 char __user *userbuf, size_t count, loff_t *ppos)
192 struct adis16480 *adis16480 = file->private_data;
198 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_REV, &rev);
202 len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
204 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
207 static const struct file_operations adis16480_firmware_revision_fops = {
209 .read = adis16480_show_firmware_revision,
210 .llseek = default_llseek,
211 .owner = THIS_MODULE,
214 static ssize_t adis16480_show_firmware_date(struct file *file,
215 char __user *userbuf, size_t count, loff_t *ppos)
217 struct adis16480 *adis16480 = file->private_data;
223 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_Y, &year);
227 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_DM, &md);
231 len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n",
232 md >> 8, md & 0xff, year);
234 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
237 static const struct file_operations adis16480_firmware_date_fops = {
239 .read = adis16480_show_firmware_date,
240 .llseek = default_llseek,
241 .owner = THIS_MODULE,
244 static int adis16480_show_serial_number(void *arg, u64 *val)
246 struct adis16480 *adis16480 = arg;
250 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_SERIAL_NUM,
259 DEFINE_DEBUGFS_ATTRIBUTE(adis16480_serial_number_fops,
260 adis16480_show_serial_number, NULL, "0x%.4llx\n");
262 static int adis16480_show_product_id(void *arg, u64 *val)
264 struct adis16480 *adis16480 = arg;
268 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_PROD_ID,
277 DEFINE_DEBUGFS_ATTRIBUTE(adis16480_product_id_fops,
278 adis16480_show_product_id, NULL, "%llu\n");
280 static int adis16480_show_flash_count(void *arg, u64 *val)
282 struct adis16480 *adis16480 = arg;
286 ret = adis_read_reg_32(&adis16480->adis, ADIS16480_REG_FLASH_CNT,
295 DEFINE_DEBUGFS_ATTRIBUTE(adis16480_flash_count_fops,
296 adis16480_show_flash_count, NULL, "%lld\n");
298 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
300 struct adis16480 *adis16480 = iio_priv(indio_dev);
301 struct dentry *d = iio_get_debugfs_dentry(indio_dev);
303 debugfs_create_file_unsafe("firmware_revision", 0400,
304 d, adis16480, &adis16480_firmware_revision_fops);
305 debugfs_create_file_unsafe("firmware_date", 0400,
306 d, adis16480, &adis16480_firmware_date_fops);
307 debugfs_create_file_unsafe("serial_number", 0400,
308 d, adis16480, &adis16480_serial_number_fops);
309 debugfs_create_file_unsafe("product_id", 0400,
310 d, adis16480, &adis16480_product_id_fops);
311 debugfs_create_file_unsafe("flash_count", 0400,
312 d, adis16480, &adis16480_flash_count_fops);
319 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
326 static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
328 struct adis16480 *st = iio_priv(indio_dev);
329 unsigned int t, sample_rate = st->clk_freq;
332 if (val < 0 || val2 < 0)
335 t = val * 1000 + val2 / 1000;
339 adis_dev_lock(&st->adis);
341 * When using PPS mode, the input clock needs to be scaled so that we have an IMU
342 * sample rate between (optimally) 4000 and 4250. After this, we can use the
343 * decimation filter to lower the sampling rate in order to get what the user wants.
344 * Optimally, the user sample rate is a multiple of both the IMU sample rate and
345 * the input clock. Hence, calculating the sync_scale dynamically gives us better
346 * chances of achieving a perfect/integer value for DEC_RATE. The math here is:
347 * 1. lcm of the input clock and the desired output rate.
348 * 2. get the highest multiple of the previous result lower than the adis max rate.
349 * 3. The last result becomes the IMU sample rate. Use that to calculate SYNC_SCALE
350 * and DEC_RATE (to get the user output rate)
352 if (st->clk_mode == ADIS16480_CLK_PPS) {
353 unsigned long scaled_rate = lcm(st->clk_freq, t);
357 * If lcm is bigger than the IMU maximum sampling rate there's no perfect
358 * solution. In this case, we get the highest multiple of the input clock
359 * lower than the IMU max sample rate.
361 if (scaled_rate > st->chip_info->int_clk)
362 scaled_rate = st->chip_info->int_clk / st->clk_freq * st->clk_freq;
364 scaled_rate = st->chip_info->int_clk / scaled_rate * scaled_rate;
367 * This is not an hard requirement but it's not advised to run the IMU
368 * with a sample rate lower than 4000Hz due to possible undersampling
369 * issues. However, there are users that might really want to take the risk.
370 * Hence, we provide a module parameter for them. If set, we allow sample
371 * rates lower than 4KHz. By default, we won't allow this and we just roundup
372 * the rate to the next multiple of the input clock bigger than 4KHz. This
373 * is done like this as in some cases (when DEC_RATE is 0) might give
374 * us the closest value to the one desired by the user...
376 if (scaled_rate < 4000000 && !low_rate_allow)
377 scaled_rate = roundup(4000000, st->clk_freq);
379 sync_scale = scaled_rate / st->clk_freq;
380 ret = __adis_write_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, sync_scale);
384 sample_rate = scaled_rate;
387 t = DIV_ROUND_CLOSEST(sample_rate, t);
391 if (t > st->chip_info->max_dec_rate)
392 t = st->chip_info->max_dec_rate;
394 ret = __adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
396 adis_dev_unlock(&st->adis);
400 static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
402 struct adis16480 *st = iio_priv(indio_dev);
405 unsigned int freq, sample_rate = st->clk_freq;
407 adis_dev_lock(&st->adis);
409 if (st->clk_mode == ADIS16480_CLK_PPS) {
412 ret = __adis_read_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, &sync_scale);
416 sample_rate = st->clk_freq * sync_scale;
419 ret = __adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
423 adis_dev_unlock(&st->adis);
425 freq = DIV_ROUND_CLOSEST(sample_rate, (t + 1));
428 *val2 = (freq % 1000) * 1000;
430 return IIO_VAL_INT_PLUS_MICRO;
432 adis_dev_unlock(&st->adis);
437 ADIS16480_SCAN_GYRO_X,
438 ADIS16480_SCAN_GYRO_Y,
439 ADIS16480_SCAN_GYRO_Z,
440 ADIS16480_SCAN_ACCEL_X,
441 ADIS16480_SCAN_ACCEL_Y,
442 ADIS16480_SCAN_ACCEL_Z,
443 ADIS16480_SCAN_MAGN_X,
444 ADIS16480_SCAN_MAGN_Y,
445 ADIS16480_SCAN_MAGN_Z,
450 static const unsigned int adis16480_calibbias_regs[] = {
451 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_BIAS,
452 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_BIAS,
453 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_BIAS,
454 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_BIAS,
455 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_BIAS,
456 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_BIAS,
457 [ADIS16480_SCAN_MAGN_X] = ADIS16480_REG_X_HARD_IRON,
458 [ADIS16480_SCAN_MAGN_Y] = ADIS16480_REG_Y_HARD_IRON,
459 [ADIS16480_SCAN_MAGN_Z] = ADIS16480_REG_Z_HARD_IRON,
460 [ADIS16480_SCAN_BARO] = ADIS16480_REG_BAROM_BIAS,
463 static const unsigned int adis16480_calibscale_regs[] = {
464 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_SCALE,
465 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_SCALE,
466 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_SCALE,
467 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_SCALE,
468 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_SCALE,
469 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_SCALE,
472 static int adis16480_set_calibbias(struct iio_dev *indio_dev,
473 const struct iio_chan_spec *chan, int bias)
475 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
476 struct adis16480 *st = iio_priv(indio_dev);
478 switch (chan->type) {
481 if (bias < -0x8000 || bias >= 0x8000)
483 return adis_write_reg_16(&st->adis, reg, bias);
486 return adis_write_reg_32(&st->adis, reg, bias);
494 static int adis16480_get_calibbias(struct iio_dev *indio_dev,
495 const struct iio_chan_spec *chan, int *bias)
497 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
498 struct adis16480 *st = iio_priv(indio_dev);
503 switch (chan->type) {
506 ret = adis_read_reg_16(&st->adis, reg, &val16);
508 *bias = sign_extend32(val16, 15);
512 ret = adis_read_reg_32(&st->adis, reg, &val32);
514 *bias = sign_extend32(val32, 31);
526 static int adis16480_set_calibscale(struct iio_dev *indio_dev,
527 const struct iio_chan_spec *chan, int scale)
529 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
530 struct adis16480 *st = iio_priv(indio_dev);
532 if (scale < -0x8000 || scale >= 0x8000)
535 return adis_write_reg_16(&st->adis, reg, scale);
538 static int adis16480_get_calibscale(struct iio_dev *indio_dev,
539 const struct iio_chan_spec *chan, int *scale)
541 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
542 struct adis16480 *st = iio_priv(indio_dev);
546 ret = adis_read_reg_16(&st->adis, reg, &val16);
550 *scale = sign_extend32(val16, 15);
554 static const unsigned int adis16480_def_filter_freqs[] = {
561 static const unsigned int adis16495_def_filter_freqs[] = {
568 static const unsigned int ad16480_filter_data[][2] = {
569 [ADIS16480_SCAN_GYRO_X] = { ADIS16480_REG_FILTER_BNK0, 0 },
570 [ADIS16480_SCAN_GYRO_Y] = { ADIS16480_REG_FILTER_BNK0, 3 },
571 [ADIS16480_SCAN_GYRO_Z] = { ADIS16480_REG_FILTER_BNK0, 6 },
572 [ADIS16480_SCAN_ACCEL_X] = { ADIS16480_REG_FILTER_BNK0, 9 },
573 [ADIS16480_SCAN_ACCEL_Y] = { ADIS16480_REG_FILTER_BNK0, 12 },
574 [ADIS16480_SCAN_ACCEL_Z] = { ADIS16480_REG_FILTER_BNK1, 0 },
575 [ADIS16480_SCAN_MAGN_X] = { ADIS16480_REG_FILTER_BNK1, 3 },
576 [ADIS16480_SCAN_MAGN_Y] = { ADIS16480_REG_FILTER_BNK1, 6 },
577 [ADIS16480_SCAN_MAGN_Z] = { ADIS16480_REG_FILTER_BNK1, 9 },
580 static int adis16480_get_filter_freq(struct iio_dev *indio_dev,
581 const struct iio_chan_spec *chan, int *freq)
583 struct adis16480 *st = iio_priv(indio_dev);
584 unsigned int enable_mask, offset, reg;
588 reg = ad16480_filter_data[chan->scan_index][0];
589 offset = ad16480_filter_data[chan->scan_index][1];
590 enable_mask = BIT(offset + 2);
592 ret = adis_read_reg_16(&st->adis, reg, &val);
596 if (!(val & enable_mask))
599 *freq = st->chip_info->filter_freqs[(val >> offset) & 0x3];
604 static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
605 const struct iio_chan_spec *chan, unsigned int freq)
607 struct adis16480 *st = iio_priv(indio_dev);
608 unsigned int enable_mask, offset, reg;
609 unsigned int diff, best_diff;
610 unsigned int i, best_freq;
614 reg = ad16480_filter_data[chan->scan_index][0];
615 offset = ad16480_filter_data[chan->scan_index][1];
616 enable_mask = BIT(offset + 2);
618 adis_dev_lock(&st->adis);
620 ret = __adis_read_reg_16(&st->adis, reg, &val);
628 best_diff = st->chip_info->filter_freqs[0];
629 for (i = 0; i < ARRAY_SIZE(adis16480_def_filter_freqs); i++) {
630 if (st->chip_info->filter_freqs[i] >= freq) {
631 diff = st->chip_info->filter_freqs[i] - freq;
632 if (diff < best_diff) {
639 val &= ~(0x3 << offset);
640 val |= best_freq << offset;
644 ret = __adis_write_reg_16(&st->adis, reg, val);
646 adis_dev_unlock(&st->adis);
651 static int adis16480_read_raw(struct iio_dev *indio_dev,
652 const struct iio_chan_spec *chan, int *val, int *val2, long info)
654 struct adis16480 *st = iio_priv(indio_dev);
658 case IIO_CHAN_INFO_RAW:
659 return adis_single_conversion(indio_dev, chan, 0, val);
660 case IIO_CHAN_INFO_SCALE:
661 switch (chan->type) {
663 *val = st->chip_info->gyro_max_scale;
664 *val2 = st->chip_info->gyro_max_val;
665 return IIO_VAL_FRACTIONAL;
667 *val = st->chip_info->accel_max_scale;
668 *val2 = st->chip_info->accel_max_val;
669 return IIO_VAL_FRACTIONAL;
672 *val2 = 100; /* 0.0001 gauss */
673 return IIO_VAL_INT_PLUS_MICRO;
676 * +85 degrees Celsius = temp_max_scale
677 * +25 degrees Celsius = 0
678 * LSB, 25 degrees Celsius = 60 / temp_max_scale
680 *val = st->chip_info->temp_scale / 1000;
681 *val2 = (st->chip_info->temp_scale % 1000) * 1000;
682 return IIO_VAL_INT_PLUS_MICRO;
685 * max scale is 1310 mbar
686 * max raw value is 32767 shifted for 32bits
688 *val = 131; /* 1310mbar = 131 kPa */
690 return IIO_VAL_FRACTIONAL;
694 case IIO_CHAN_INFO_OFFSET:
695 /* Only the temperature channel has a offset */
696 temp = 25 * 1000000LL; /* 25 degree Celsius = 0x0000 */
697 *val = DIV_ROUND_CLOSEST_ULL(temp, st->chip_info->temp_scale);
699 case IIO_CHAN_INFO_CALIBBIAS:
700 return adis16480_get_calibbias(indio_dev, chan, val);
701 case IIO_CHAN_INFO_CALIBSCALE:
702 return adis16480_get_calibscale(indio_dev, chan, val);
703 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
704 return adis16480_get_filter_freq(indio_dev, chan, val);
705 case IIO_CHAN_INFO_SAMP_FREQ:
706 return adis16480_get_freq(indio_dev, val, val2);
712 static int adis16480_write_raw(struct iio_dev *indio_dev,
713 const struct iio_chan_spec *chan, int val, int val2, long info)
716 case IIO_CHAN_INFO_CALIBBIAS:
717 return adis16480_set_calibbias(indio_dev, chan, val);
718 case IIO_CHAN_INFO_CALIBSCALE:
719 return adis16480_set_calibscale(indio_dev, chan, val);
720 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
721 return adis16480_set_filter_freq(indio_dev, chan, val);
722 case IIO_CHAN_INFO_SAMP_FREQ:
723 return adis16480_set_freq(indio_dev, val, val2);
730 #define ADIS16480_MOD_CHANNEL(_type, _mod, _address, _si, _info_sep, _bits) \
734 .channel2 = (_mod), \
735 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
736 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
738 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
739 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
740 .address = (_address), \
741 .scan_index = (_si), \
744 .realbits = (_bits), \
745 .storagebits = (_bits), \
746 .endianness = IIO_BE, \
750 #define ADIS16480_GYRO_CHANNEL(_mod) \
751 ADIS16480_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
752 ADIS16480_REG_ ## _mod ## _GYRO_OUT, ADIS16480_SCAN_GYRO_ ## _mod, \
753 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
754 BIT(IIO_CHAN_INFO_CALIBSCALE), \
757 #define ADIS16480_ACCEL_CHANNEL(_mod) \
758 ADIS16480_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
759 ADIS16480_REG_ ## _mod ## _ACCEL_OUT, ADIS16480_SCAN_ACCEL_ ## _mod, \
760 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
761 BIT(IIO_CHAN_INFO_CALIBSCALE), \
764 #define ADIS16480_MAGN_CHANNEL(_mod) \
765 ADIS16480_MOD_CHANNEL(IIO_MAGN, IIO_MOD_ ## _mod, \
766 ADIS16480_REG_ ## _mod ## _MAGN_OUT, ADIS16480_SCAN_MAGN_ ## _mod, \
767 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
770 #define ADIS16480_PRESSURE_CHANNEL() \
772 .type = IIO_PRESSURE, \
775 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
776 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
777 BIT(IIO_CHAN_INFO_SCALE), \
778 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
779 .address = ADIS16480_REG_BAROM_OUT, \
780 .scan_index = ADIS16480_SCAN_BARO, \
785 .endianness = IIO_BE, \
789 #define ADIS16480_TEMP_CHANNEL() { \
793 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
794 BIT(IIO_CHAN_INFO_SCALE) | \
795 BIT(IIO_CHAN_INFO_OFFSET), \
796 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
797 .address = ADIS16480_REG_TEMP_OUT, \
798 .scan_index = ADIS16480_SCAN_TEMP, \
803 .endianness = IIO_BE, \
807 static const struct iio_chan_spec adis16480_channels[] = {
808 ADIS16480_GYRO_CHANNEL(X),
809 ADIS16480_GYRO_CHANNEL(Y),
810 ADIS16480_GYRO_CHANNEL(Z),
811 ADIS16480_ACCEL_CHANNEL(X),
812 ADIS16480_ACCEL_CHANNEL(Y),
813 ADIS16480_ACCEL_CHANNEL(Z),
814 ADIS16480_MAGN_CHANNEL(X),
815 ADIS16480_MAGN_CHANNEL(Y),
816 ADIS16480_MAGN_CHANNEL(Z),
817 ADIS16480_PRESSURE_CHANNEL(),
818 ADIS16480_TEMP_CHANNEL(),
819 IIO_CHAN_SOFT_TIMESTAMP(11)
822 static const struct iio_chan_spec adis16485_channels[] = {
823 ADIS16480_GYRO_CHANNEL(X),
824 ADIS16480_GYRO_CHANNEL(Y),
825 ADIS16480_GYRO_CHANNEL(Z),
826 ADIS16480_ACCEL_CHANNEL(X),
827 ADIS16480_ACCEL_CHANNEL(Y),
828 ADIS16480_ACCEL_CHANNEL(Z),
829 ADIS16480_TEMP_CHANNEL(),
830 IIO_CHAN_SOFT_TIMESTAMP(7)
833 enum adis16480_variant {
847 #define ADIS16480_DIAG_STAT_XGYRO_FAIL 0
848 #define ADIS16480_DIAG_STAT_YGYRO_FAIL 1
849 #define ADIS16480_DIAG_STAT_ZGYRO_FAIL 2
850 #define ADIS16480_DIAG_STAT_XACCL_FAIL 3
851 #define ADIS16480_DIAG_STAT_YACCL_FAIL 4
852 #define ADIS16480_DIAG_STAT_ZACCL_FAIL 5
853 #define ADIS16480_DIAG_STAT_XMAGN_FAIL 8
854 #define ADIS16480_DIAG_STAT_YMAGN_FAIL 9
855 #define ADIS16480_DIAG_STAT_ZMAGN_FAIL 10
856 #define ADIS16480_DIAG_STAT_BARO_FAIL 11
858 static const char * const adis16480_status_error_msgs[] = {
859 [ADIS16480_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
860 [ADIS16480_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
861 [ADIS16480_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
862 [ADIS16480_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
863 [ADIS16480_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
864 [ADIS16480_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
865 [ADIS16480_DIAG_STAT_XMAGN_FAIL] = "X-axis magnetometer self-test failure",
866 [ADIS16480_DIAG_STAT_YMAGN_FAIL] = "Y-axis magnetometer self-test failure",
867 [ADIS16480_DIAG_STAT_ZMAGN_FAIL] = "Z-axis magnetometer self-test failure",
868 [ADIS16480_DIAG_STAT_BARO_FAIL] = "Barometer self-test failure",
871 static int adis16480_enable_irq(struct adis *adis, bool enable);
873 #define ADIS16480_DATA(_prod_id, _timeouts, _burst_len) \
875 .diag_stat_reg = ADIS16480_REG_DIAG_STS, \
876 .glob_cmd_reg = ADIS16480_REG_GLOB_CMD, \
877 .prod_id_reg = ADIS16480_REG_PROD_ID, \
878 .prod_id = (_prod_id), \
879 .has_paging = true, \
882 .self_test_mask = BIT(1), \
883 .self_test_reg = ADIS16480_REG_GLOB_CMD, \
884 .status_error_msgs = adis16480_status_error_msgs, \
885 .status_error_mask = BIT(ADIS16480_DIAG_STAT_XGYRO_FAIL) | \
886 BIT(ADIS16480_DIAG_STAT_YGYRO_FAIL) | \
887 BIT(ADIS16480_DIAG_STAT_ZGYRO_FAIL) | \
888 BIT(ADIS16480_DIAG_STAT_XACCL_FAIL) | \
889 BIT(ADIS16480_DIAG_STAT_YACCL_FAIL) | \
890 BIT(ADIS16480_DIAG_STAT_ZACCL_FAIL) | \
891 BIT(ADIS16480_DIAG_STAT_XMAGN_FAIL) | \
892 BIT(ADIS16480_DIAG_STAT_YMAGN_FAIL) | \
893 BIT(ADIS16480_DIAG_STAT_ZMAGN_FAIL) | \
894 BIT(ADIS16480_DIAG_STAT_BARO_FAIL), \
895 .enable_irq = adis16480_enable_irq, \
896 .timeouts = (_timeouts), \
897 .burst_reg_cmd = ADIS16495_REG_BURST_CMD, \
898 .burst_len = (_burst_len), \
899 .burst_max_speed_hz = ADIS16495_BURST_MAX_SPEED \
902 static const struct adis_timeout adis16485_timeouts = {
908 static const struct adis_timeout adis16480_timeouts = {
914 static const struct adis_timeout adis16495_timeouts = {
920 static const struct adis_timeout adis16495_1_timeouts = {
926 static const struct adis16480_chip_info adis16480_chip_info[] = {
928 .channels = adis16485_channels,
929 .num_channels = ARRAY_SIZE(adis16485_channels),
931 * Typically we do IIO_RAD_TO_DEGREE in the denominator, which
932 * is exactly the same as IIO_DEGREE_TO_RAD in numerator, since
933 * it gives better approximation. However, in this case we
934 * cannot do it since it would not fit in a 32bit variable.
936 .gyro_max_val = 22887 << 16,
937 .gyro_max_scale = IIO_DEGREE_TO_RAD(300),
938 .accel_max_val = IIO_M_S_2_TO_G(21973 << 16),
939 .accel_max_scale = 18,
940 .temp_scale = 5650, /* 5.65 milli degree Celsius */
942 .max_dec_rate = 2048,
943 .has_sleep_cnt = true,
944 .filter_freqs = adis16480_def_filter_freqs,
945 .adis_data = ADIS16480_DATA(16375, &adis16485_timeouts, 0),
948 .channels = adis16480_channels,
949 .num_channels = ARRAY_SIZE(adis16480_channels),
950 .gyro_max_val = 22500 << 16,
951 .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
952 .accel_max_val = IIO_M_S_2_TO_G(12500 << 16),
953 .accel_max_scale = 10,
954 .temp_scale = 5650, /* 5.65 milli degree Celsius */
956 .max_dec_rate = 2048,
957 .has_sleep_cnt = true,
958 .filter_freqs = adis16480_def_filter_freqs,
959 .adis_data = ADIS16480_DATA(16480, &adis16480_timeouts, 0),
962 .channels = adis16485_channels,
963 .num_channels = ARRAY_SIZE(adis16485_channels),
964 .gyro_max_val = 22500 << 16,
965 .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
966 .accel_max_val = IIO_M_S_2_TO_G(20000 << 16),
967 .accel_max_scale = 5,
968 .temp_scale = 5650, /* 5.65 milli degree Celsius */
970 .max_dec_rate = 2048,
971 .has_sleep_cnt = true,
972 .filter_freqs = adis16480_def_filter_freqs,
973 .adis_data = ADIS16480_DATA(16485, &adis16485_timeouts, 0),
976 .channels = adis16480_channels,
977 .num_channels = ARRAY_SIZE(adis16480_channels),
978 .gyro_max_val = 22500 << 16,
979 .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
980 .accel_max_val = IIO_M_S_2_TO_G(22500 << 16),
981 .accel_max_scale = 18,
982 .temp_scale = 5650, /* 5.65 milli degree Celsius */
984 .max_dec_rate = 2048,
985 .has_sleep_cnt = true,
986 .filter_freqs = adis16480_def_filter_freqs,
987 .adis_data = ADIS16480_DATA(16488, &adis16485_timeouts, 0),
990 .channels = adis16485_channels,
991 .num_channels = ARRAY_SIZE(adis16485_channels),
992 .gyro_max_val = 20000 << 16,
993 .gyro_max_scale = IIO_DEGREE_TO_RAD(100),
994 .accel_max_val = IIO_M_S_2_TO_G(16000 << 16),
995 .accel_max_scale = 8,
996 .temp_scale = 14285, /* 14.285 milli degree Celsius */
998 .max_dec_rate = 4250,
999 .filter_freqs = adis16495_def_filter_freqs,
1000 .has_pps_clk_mode = true,
1001 .adis_data = ADIS16480_DATA(16490, &adis16495_timeouts, 0),
1004 .channels = adis16485_channels,
1005 .num_channels = ARRAY_SIZE(adis16485_channels),
1006 .gyro_max_val = 20000 << 16,
1007 .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
1008 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1009 .accel_max_scale = 8,
1010 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1012 .max_dec_rate = 4250,
1013 .filter_freqs = adis16495_def_filter_freqs,
1014 .has_pps_clk_mode = true,
1015 /* 20 elements of 16bits */
1016 .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
1017 ADIS16495_BURST_MAX_DATA * 2),
1020 .channels = adis16485_channels,
1021 .num_channels = ARRAY_SIZE(adis16485_channels),
1022 .gyro_max_val = 18000 << 16,
1023 .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
1024 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1025 .accel_max_scale = 8,
1026 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1028 .max_dec_rate = 4250,
1029 .filter_freqs = adis16495_def_filter_freqs,
1030 .has_pps_clk_mode = true,
1031 /* 20 elements of 16bits */
1032 .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
1033 ADIS16495_BURST_MAX_DATA * 2),
1036 .channels = adis16485_channels,
1037 .num_channels = ARRAY_SIZE(adis16485_channels),
1038 .gyro_max_val = 20000 << 16,
1039 .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
1040 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1041 .accel_max_scale = 8,
1042 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1044 .max_dec_rate = 4250,
1045 .filter_freqs = adis16495_def_filter_freqs,
1046 .has_pps_clk_mode = true,
1047 /* 20 elements of 16bits */
1048 .adis_data = ADIS16480_DATA(16495, &adis16495_1_timeouts,
1049 ADIS16495_BURST_MAX_DATA * 2),
1052 .channels = adis16485_channels,
1053 .num_channels = ARRAY_SIZE(adis16485_channels),
1054 .gyro_max_val = 20000 << 16,
1055 .gyro_max_scale = IIO_DEGREE_TO_RAD(125),
1056 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1057 .accel_max_scale = 40,
1058 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1060 .max_dec_rate = 4250,
1061 .filter_freqs = adis16495_def_filter_freqs,
1062 .has_pps_clk_mode = true,
1063 /* 20 elements of 16bits */
1064 .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
1065 ADIS16495_BURST_MAX_DATA * 2),
1068 .channels = adis16485_channels,
1069 .num_channels = ARRAY_SIZE(adis16485_channels),
1070 .gyro_max_val = 18000 << 16,
1071 .gyro_max_scale = IIO_DEGREE_TO_RAD(450),
1072 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1073 .accel_max_scale = 40,
1074 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1076 .max_dec_rate = 4250,
1077 .filter_freqs = adis16495_def_filter_freqs,
1078 .has_pps_clk_mode = true,
1079 /* 20 elements of 16bits */
1080 .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
1081 ADIS16495_BURST_MAX_DATA * 2),
1084 .channels = adis16485_channels,
1085 .num_channels = ARRAY_SIZE(adis16485_channels),
1086 .gyro_max_val = 20000 << 16,
1087 .gyro_max_scale = IIO_DEGREE_TO_RAD(2000),
1088 .accel_max_val = IIO_M_S_2_TO_G(32000 << 16),
1089 .accel_max_scale = 40,
1090 .temp_scale = 12500, /* 12.5 milli degree Celsius */
1092 .max_dec_rate = 4250,
1093 .filter_freqs = adis16495_def_filter_freqs,
1094 .has_pps_clk_mode = true,
1095 /* 20 elements of 16bits */
1096 .adis_data = ADIS16480_DATA(16497, &adis16495_1_timeouts,
1097 ADIS16495_BURST_MAX_DATA * 2),
1101 static bool adis16480_validate_crc(const u16 *buf, const u8 n_elem, const u32 crc)
1107 for (j = 0; j < n_elem; j++)
1108 crc_buf[j] = swab16(buf[j]);
1110 crc_calc = crc32(~0, crc_buf, n_elem * 2);
1113 return (crc == crc_calc);
1116 static irqreturn_t adis16480_trigger_handler(int irq, void *p)
1118 struct iio_poll_func *pf = p;
1119 struct iio_dev *indio_dev = pf->indio_dev;
1120 struct adis16480 *st = iio_priv(indio_dev);
1121 struct adis *adis = &st->adis;
1122 int ret, bit, offset, i = 0;
1127 adis_dev_lock(adis);
1128 if (adis->current_page != 0) {
1129 adis->tx[0] = ADIS_WRITE_REG(ADIS_REG_PAGE_ID);
1131 ret = spi_write(adis->spi, adis->tx, 2);
1133 dev_err(&adis->spi->dev, "Failed to change device page: %d\n", ret);
1134 adis_dev_unlock(adis);
1138 adis->current_page = 0;
1141 ret = spi_sync(adis->spi, &adis->msg);
1143 dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret);
1144 adis_dev_unlock(adis);
1148 adis_dev_unlock(adis);
1151 * After making the burst request, the response can have one or two
1152 * 16-bit responses containing the BURST_ID depending on the sclk. If
1153 * clk > 3.6MHz, then we will have two BURST_ID in a row. If clk < 3MHZ,
1154 * we have only one. To manage that variation, we use the transition from the
1155 * BURST_ID to the SYS_E_FLAG register, which will not be equal to 0xA5A5. If
1156 * we not find this variation in the first 4 segments, then the data should
1159 buffer = adis->buffer;
1160 for (offset = 0; offset < 4; offset++) {
1161 u16 curr = be16_to_cpu(buffer[offset]);
1162 u16 next = be16_to_cpu(buffer[offset + 1]);
1164 if (curr == ADIS16495_BURST_ID && next != ADIS16495_BURST_ID) {
1171 dev_err(&adis->spi->dev, "Invalid burst data\n");
1175 crc = be16_to_cpu(buffer[offset + 16]) << 16 | be16_to_cpu(buffer[offset + 15]);
1176 valid = adis16480_validate_crc((u16 *)&buffer[offset], 15, crc);
1178 dev_err(&adis->spi->dev, "Invalid crc\n");
1182 for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) {
1184 * When burst mode is used, temperature is the first data
1185 * channel in the sequence, but the temperature scan index
1189 case ADIS16480_SCAN_TEMP:
1190 st->data[i++] = buffer[offset + 1];
1192 case ADIS16480_SCAN_GYRO_X ... ADIS16480_SCAN_ACCEL_Z:
1193 /* The lower register data is sequenced first */
1194 st->data[i++] = buffer[2 * bit + offset + 3];
1195 st->data[i++] = buffer[2 * bit + offset + 2];
1200 iio_push_to_buffers_with_timestamp(indio_dev, st->data, pf->timestamp);
1202 iio_trigger_notify_done(indio_dev->trig);
1207 static const struct iio_info adis16480_info = {
1208 .read_raw = &adis16480_read_raw,
1209 .write_raw = &adis16480_write_raw,
1210 .update_scan_mode = adis_update_scan_mode,
1211 .debugfs_reg_access = adis_debugfs_reg_access,
1214 static int adis16480_stop_device(struct iio_dev *indio_dev)
1216 struct adis16480 *st = iio_priv(indio_dev);
1219 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_SLP_CNT, BIT(9));
1221 dev_err(&indio_dev->dev,
1222 "Could not power down device: %d\n", ret);
1227 static int adis16480_enable_irq(struct adis *adis, bool enable)
1232 ret = __adis_read_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL, &val);
1236 val &= ~ADIS16480_DRDY_EN_MSK;
1237 val |= ADIS16480_DRDY_EN(enable);
1239 return __adis_write_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL, val);
1242 static int adis16480_config_irq_pin(struct device_node *of_node,
1243 struct adis16480 *st)
1245 struct irq_data *desc;
1246 enum adis16480_int_pin pin;
1247 unsigned int irq_type;
1251 desc = irq_get_irq_data(st->adis.spi->irq);
1253 dev_err(&st->adis.spi->dev, "Could not find IRQ %d\n", irq);
1257 /* Disable data ready since the default after reset is on */
1258 val = ADIS16480_DRDY_EN(0);
1261 * Get the interrupt from the devicetre by reading the interrupt-names
1262 * property. If it is not specified, use DIO1 pin as default.
1263 * According to the datasheet, the factory default assigns DIO2 as data
1264 * ready signal. However, in the previous versions of the driver, DIO1
1265 * pin was used. So, we should leave it as is since some devices might
1266 * be expecting the interrupt on the wrong physical pin.
1268 pin = ADIS16480_PIN_DIO1;
1269 for (i = 0; i < ARRAY_SIZE(adis16480_int_pin_names); i++) {
1270 irq = of_irq_get_byname(of_node, adis16480_int_pin_names[i]);
1277 val |= ADIS16480_DRDY_SEL(pin);
1280 * Get the interrupt line behaviour. The data ready polarity can be
1281 * configured as positive or negative, corresponding to
1282 * IRQ_TYPE_EDGE_RISING or IRQ_TYPE_EDGE_FALLING respectively.
1284 irq_type = irqd_get_trigger_type(desc);
1285 if (irq_type == IRQ_TYPE_EDGE_RISING) { /* Default */
1286 val |= ADIS16480_DRDY_POL(1);
1287 } else if (irq_type == IRQ_TYPE_EDGE_FALLING) {
1288 val |= ADIS16480_DRDY_POL(0);
1290 dev_err(&st->adis.spi->dev,
1291 "Invalid interrupt type 0x%x specified\n", irq_type);
1294 /* Write the data ready configuration to the FNCTIO_CTRL register */
1295 return adis_write_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, val);
1298 static int adis16480_of_get_ext_clk_pin(struct adis16480 *st,
1299 struct device_node *of_node)
1301 const char *ext_clk_pin;
1302 enum adis16480_int_pin pin;
1305 pin = ADIS16480_PIN_DIO2;
1306 if (of_property_read_string(of_node, "adi,ext-clk-pin", &ext_clk_pin))
1307 goto clk_input_not_found;
1309 for (i = 0; i < ARRAY_SIZE(adis16480_int_pin_names); i++) {
1310 if (strcasecmp(ext_clk_pin, adis16480_int_pin_names[i]) == 0)
1314 clk_input_not_found:
1315 dev_info(&st->adis.spi->dev,
1316 "clk input line not specified, using DIO2\n");
1320 static int adis16480_ext_clk_config(struct adis16480 *st,
1321 struct device_node *of_node,
1324 unsigned int mode, mask;
1325 enum adis16480_int_pin pin;
1329 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, &val);
1333 pin = adis16480_of_get_ext_clk_pin(st, of_node);
1335 * Each DIOx pin supports only one function at a time. When a single pin
1336 * has two assignments, the enable bit for a lower priority function
1337 * automatically resets to zero (disabling the lower priority function).
1339 if (pin == ADIS16480_DRDY_SEL(val))
1340 dev_warn(&st->adis.spi->dev,
1341 "DIO%x pin supports only one function at a time\n",
1344 mode = ADIS16480_SYNC_EN(enable) | ADIS16480_SYNC_SEL(pin);
1345 mask = ADIS16480_SYNC_EN_MSK | ADIS16480_SYNC_SEL_MSK;
1346 /* Only ADIS1649x devices support pps ext clock mode */
1347 if (st->chip_info->has_pps_clk_mode) {
1348 mode |= ADIS16480_SYNC_MODE(st->clk_mode);
1349 mask |= ADIS16480_SYNC_MODE_MSK;
1355 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_FNCTIO_CTRL, val);
1359 return clk_prepare_enable(st->ext_clk);
1362 static int adis16480_get_ext_clocks(struct adis16480 *st)
1364 st->clk_mode = ADIS16480_CLK_INT;
1365 st->ext_clk = devm_clk_get(&st->adis.spi->dev, "sync");
1366 if (!IS_ERR_OR_NULL(st->ext_clk)) {
1367 st->clk_mode = ADIS16480_CLK_SYNC;
1371 if (PTR_ERR(st->ext_clk) != -ENOENT) {
1372 dev_err(&st->adis.spi->dev, "failed to get ext clk\n");
1373 return PTR_ERR(st->ext_clk);
1376 if (st->chip_info->has_pps_clk_mode) {
1377 st->ext_clk = devm_clk_get(&st->adis.spi->dev, "pps");
1378 if (!IS_ERR_OR_NULL(st->ext_clk)) {
1379 st->clk_mode = ADIS16480_CLK_PPS;
1383 if (PTR_ERR(st->ext_clk) != -ENOENT) {
1384 dev_err(&st->adis.spi->dev, "failed to get ext clk\n");
1385 return PTR_ERR(st->ext_clk);
1392 static void adis16480_stop(void *data)
1394 adis16480_stop_device(data);
1397 static void adis16480_clk_disable(void *data)
1399 clk_disable_unprepare(data);
1402 static int adis16480_probe(struct spi_device *spi)
1404 const struct spi_device_id *id = spi_get_device_id(spi);
1405 const struct adis_data *adis16480_data;
1406 irq_handler_t trigger_handler = NULL;
1407 struct iio_dev *indio_dev;
1408 struct adis16480 *st;
1411 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1412 if (indio_dev == NULL)
1415 st = iio_priv(indio_dev);
1417 st->chip_info = &adis16480_chip_info[id->driver_data];
1418 indio_dev->name = spi_get_device_id(spi)->name;
1419 indio_dev->channels = st->chip_info->channels;
1420 indio_dev->num_channels = st->chip_info->num_channels;
1421 indio_dev->info = &adis16480_info;
1422 indio_dev->modes = INDIO_DIRECT_MODE;
1424 adis16480_data = &st->chip_info->adis_data;
1426 ret = adis_init(&st->adis, indio_dev, spi, adis16480_data);
1430 ret = __adis_initial_startup(&st->adis);
1434 if (st->chip_info->has_sleep_cnt) {
1435 ret = devm_add_action_or_reset(&spi->dev, adis16480_stop,
1441 ret = adis16480_config_irq_pin(spi->dev.of_node, st);
1445 ret = adis16480_get_ext_clocks(st);
1449 if (!IS_ERR_OR_NULL(st->ext_clk)) {
1450 ret = adis16480_ext_clk_config(st, spi->dev.of_node, true);
1454 ret = devm_add_action_or_reset(&spi->dev, adis16480_clk_disable, st->ext_clk);
1458 st->clk_freq = clk_get_rate(st->ext_clk);
1459 st->clk_freq *= 1000; /* micro */
1460 if (st->clk_mode == ADIS16480_CLK_PPS) {
1464 * In PPS mode, the IMU sample rate is the clk_freq * sync_scale. Hence,
1465 * default the IMU sample rate to the highest multiple of the input clock
1466 * lower than the IMU max sample rate. The internal sample rate is the
1469 sync_scale = st->chip_info->int_clk / st->clk_freq;
1470 ret = __adis_write_reg_16(&st->adis, ADIS16495_REG_SYNC_SCALE, sync_scale);
1475 st->clk_freq = st->chip_info->int_clk;
1478 /* Only use our trigger handler if burst mode is supported */
1479 if (adis16480_data->burst_len)
1480 trigger_handler = adis16480_trigger_handler;
1482 ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev,
1487 ret = devm_iio_device_register(&spi->dev, indio_dev);
1491 adis16480_debugfs_init(indio_dev);
1496 static const struct spi_device_id adis16480_ids[] = {
1497 { "adis16375", ADIS16375 },
1498 { "adis16480", ADIS16480 },
1499 { "adis16485", ADIS16485 },
1500 { "adis16488", ADIS16488 },
1501 { "adis16490", ADIS16490 },
1502 { "adis16495-1", ADIS16495_1 },
1503 { "adis16495-2", ADIS16495_2 },
1504 { "adis16495-3", ADIS16495_3 },
1505 { "adis16497-1", ADIS16497_1 },
1506 { "adis16497-2", ADIS16497_2 },
1507 { "adis16497-3", ADIS16497_3 },
1510 MODULE_DEVICE_TABLE(spi, adis16480_ids);
1512 static const struct of_device_id adis16480_of_match[] = {
1513 { .compatible = "adi,adis16375" },
1514 { .compatible = "adi,adis16480" },
1515 { .compatible = "adi,adis16485" },
1516 { .compatible = "adi,adis16488" },
1517 { .compatible = "adi,adis16490" },
1518 { .compatible = "adi,adis16495-1" },
1519 { .compatible = "adi,adis16495-2" },
1520 { .compatible = "adi,adis16495-3" },
1521 { .compatible = "adi,adis16497-1" },
1522 { .compatible = "adi,adis16497-2" },
1523 { .compatible = "adi,adis16497-3" },
1526 MODULE_DEVICE_TABLE(of, adis16480_of_match);
1528 static struct spi_driver adis16480_driver = {
1530 .name = "adis16480",
1531 .of_match_table = adis16480_of_match,
1533 .id_table = adis16480_ids,
1534 .probe = adis16480_probe,
1536 module_spi_driver(adis16480_driver);
1538 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1539 MODULE_DESCRIPTION("Analog Devices ADIS16480 IMU driver");
1540 MODULE_LICENSE("GPL v2");