2 * ADIS16480 and similar IMUs driver
4 * Copyright 2012 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/mutex.h>
15 #include <linux/device.h>
16 #include <linux/kernel.h>
17 #include <linux/spi/spi.h>
18 #include <linux/slab.h>
19 #include <linux/sysfs.h>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/imu/adis.h>
27 #include <linux/debugfs.h>
29 #define ADIS16480_PAGE_SIZE 0x80
31 #define ADIS16480_REG(page, reg) ((page) * ADIS16480_PAGE_SIZE + (reg))
33 #define ADIS16480_REG_PAGE_ID 0x00 /* Same address on each page */
34 #define ADIS16480_REG_SEQ_CNT ADIS16480_REG(0x00, 0x06)
35 #define ADIS16480_REG_SYS_E_FLA ADIS16480_REG(0x00, 0x08)
36 #define ADIS16480_REG_DIAG_STS ADIS16480_REG(0x00, 0x0A)
37 #define ADIS16480_REG_ALM_STS ADIS16480_REG(0x00, 0x0C)
38 #define ADIS16480_REG_TEMP_OUT ADIS16480_REG(0x00, 0x0E)
39 #define ADIS16480_REG_X_GYRO_OUT ADIS16480_REG(0x00, 0x10)
40 #define ADIS16480_REG_Y_GYRO_OUT ADIS16480_REG(0x00, 0x14)
41 #define ADIS16480_REG_Z_GYRO_OUT ADIS16480_REG(0x00, 0x18)
42 #define ADIS16480_REG_X_ACCEL_OUT ADIS16480_REG(0x00, 0x1C)
43 #define ADIS16480_REG_Y_ACCEL_OUT ADIS16480_REG(0x00, 0x20)
44 #define ADIS16480_REG_Z_ACCEL_OUT ADIS16480_REG(0x00, 0x24)
45 #define ADIS16480_REG_X_MAGN_OUT ADIS16480_REG(0x00, 0x28)
46 #define ADIS16480_REG_Y_MAGN_OUT ADIS16480_REG(0x00, 0x2A)
47 #define ADIS16480_REG_Z_MAGN_OUT ADIS16480_REG(0x00, 0x2C)
48 #define ADIS16480_REG_BAROM_OUT ADIS16480_REG(0x00, 0x2E)
49 #define ADIS16480_REG_X_DELTAANG_OUT ADIS16480_REG(0x00, 0x40)
50 #define ADIS16480_REG_Y_DELTAANG_OUT ADIS16480_REG(0x00, 0x44)
51 #define ADIS16480_REG_Z_DELTAANG_OUT ADIS16480_REG(0x00, 0x48)
52 #define ADIS16480_REG_X_DELTAVEL_OUT ADIS16480_REG(0x00, 0x4C)
53 #define ADIS16480_REG_Y_DELTAVEL_OUT ADIS16480_REG(0x00, 0x50)
54 #define ADIS16480_REG_Z_DELTAVEL_OUT ADIS16480_REG(0x00, 0x54)
55 #define ADIS16480_REG_PROD_ID ADIS16480_REG(0x00, 0x7E)
57 #define ADIS16480_REG_X_GYRO_SCALE ADIS16480_REG(0x02, 0x04)
58 #define ADIS16480_REG_Y_GYRO_SCALE ADIS16480_REG(0x02, 0x06)
59 #define ADIS16480_REG_Z_GYRO_SCALE ADIS16480_REG(0x02, 0x08)
60 #define ADIS16480_REG_X_ACCEL_SCALE ADIS16480_REG(0x02, 0x0A)
61 #define ADIS16480_REG_Y_ACCEL_SCALE ADIS16480_REG(0x02, 0x0C)
62 #define ADIS16480_REG_Z_ACCEL_SCALE ADIS16480_REG(0x02, 0x0E)
63 #define ADIS16480_REG_X_GYRO_BIAS ADIS16480_REG(0x02, 0x10)
64 #define ADIS16480_REG_Y_GYRO_BIAS ADIS16480_REG(0x02, 0x14)
65 #define ADIS16480_REG_Z_GYRO_BIAS ADIS16480_REG(0x02, 0x18)
66 #define ADIS16480_REG_X_ACCEL_BIAS ADIS16480_REG(0x02, 0x1C)
67 #define ADIS16480_REG_Y_ACCEL_BIAS ADIS16480_REG(0x02, 0x20)
68 #define ADIS16480_REG_Z_ACCEL_BIAS ADIS16480_REG(0x02, 0x24)
69 #define ADIS16480_REG_X_HARD_IRON ADIS16480_REG(0x02, 0x28)
70 #define ADIS16480_REG_Y_HARD_IRON ADIS16480_REG(0x02, 0x2A)
71 #define ADIS16480_REG_Z_HARD_IRON ADIS16480_REG(0x02, 0x2C)
72 #define ADIS16480_REG_BAROM_BIAS ADIS16480_REG(0x02, 0x40)
73 #define ADIS16480_REG_FLASH_CNT ADIS16480_REG(0x02, 0x7C)
75 #define ADIS16480_REG_GLOB_CMD ADIS16480_REG(0x03, 0x02)
76 #define ADIS16480_REG_FNCTIO_CTRL ADIS16480_REG(0x03, 0x06)
77 #define ADIS16480_REG_GPIO_CTRL ADIS16480_REG(0x03, 0x08)
78 #define ADIS16480_REG_CONFIG ADIS16480_REG(0x03, 0x0A)
79 #define ADIS16480_REG_DEC_RATE ADIS16480_REG(0x03, 0x0C)
80 #define ADIS16480_REG_SLP_CNT ADIS16480_REG(0x03, 0x10)
81 #define ADIS16480_REG_FILTER_BNK0 ADIS16480_REG(0x03, 0x16)
82 #define ADIS16480_REG_FILTER_BNK1 ADIS16480_REG(0x03, 0x18)
83 #define ADIS16480_REG_ALM_CNFG0 ADIS16480_REG(0x03, 0x20)
84 #define ADIS16480_REG_ALM_CNFG1 ADIS16480_REG(0x03, 0x22)
85 #define ADIS16480_REG_ALM_CNFG2 ADIS16480_REG(0x03, 0x24)
86 #define ADIS16480_REG_XG_ALM_MAGN ADIS16480_REG(0x03, 0x28)
87 #define ADIS16480_REG_YG_ALM_MAGN ADIS16480_REG(0x03, 0x2A)
88 #define ADIS16480_REG_ZG_ALM_MAGN ADIS16480_REG(0x03, 0x2C)
89 #define ADIS16480_REG_XA_ALM_MAGN ADIS16480_REG(0x03, 0x2E)
90 #define ADIS16480_REG_YA_ALM_MAGN ADIS16480_REG(0x03, 0x30)
91 #define ADIS16480_REG_ZA_ALM_MAGN ADIS16480_REG(0x03, 0x32)
92 #define ADIS16480_REG_XM_ALM_MAGN ADIS16480_REG(0x03, 0x34)
93 #define ADIS16480_REG_YM_ALM_MAGN ADIS16480_REG(0x03, 0x36)
94 #define ADIS16480_REG_ZM_ALM_MAGN ADIS16480_REG(0x03, 0x38)
95 #define ADIS16480_REG_BR_ALM_MAGN ADIS16480_REG(0x03, 0x3A)
96 #define ADIS16480_REG_FIRM_REV ADIS16480_REG(0x03, 0x78)
97 #define ADIS16480_REG_FIRM_DM ADIS16480_REG(0x03, 0x7A)
98 #define ADIS16480_REG_FIRM_Y ADIS16480_REG(0x03, 0x7C)
100 #define ADIS16480_REG_SERIAL_NUM ADIS16480_REG(0x04, 0x20)
102 /* Each filter coefficent bank spans two pages */
103 #define ADIS16480_FIR_COEF(page) (x < 60 ? ADIS16480_REG(page, (x) + 8) : \
104 ADIS16480_REG((page) + 1, (x) - 60 + 8))
105 #define ADIS16480_FIR_COEF_A(x) ADIS16480_FIR_COEF(0x05, (x))
106 #define ADIS16480_FIR_COEF_B(x) ADIS16480_FIR_COEF(0x07, (x))
107 #define ADIS16480_FIR_COEF_C(x) ADIS16480_FIR_COEF(0x09, (x))
108 #define ADIS16480_FIR_COEF_D(x) ADIS16480_FIR_COEF(0x0B, (x))
110 struct adis16480_chip_info {
111 unsigned int num_channels;
112 const struct iio_chan_spec *channels;
113 unsigned int gyro_max_val;
114 unsigned int gyro_max_scale;
115 unsigned int accel_max_val;
116 unsigned int accel_max_scale;
120 const struct adis16480_chip_info *chip_info;
125 #ifdef CONFIG_DEBUG_FS
127 static ssize_t adis16480_show_firmware_revision(struct file *file,
128 char __user *userbuf, size_t count, loff_t *ppos)
130 struct adis16480 *adis16480 = file->private_data;
136 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_REV, &rev);
140 len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff);
142 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
145 static const struct file_operations adis16480_firmware_revision_fops = {
147 .read = adis16480_show_firmware_revision,
148 .llseek = default_llseek,
149 .owner = THIS_MODULE,
152 static ssize_t adis16480_show_firmware_date(struct file *file,
153 char __user *userbuf, size_t count, loff_t *ppos)
155 struct adis16480 *adis16480 = file->private_data;
161 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_Y, &year);
165 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_DM, &md);
169 len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n",
170 md >> 8, md & 0xff, year);
172 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
175 static const struct file_operations adis16480_firmware_date_fops = {
177 .read = adis16480_show_firmware_date,
178 .llseek = default_llseek,
179 .owner = THIS_MODULE,
182 static int adis16480_show_serial_number(void *arg, u64 *val)
184 struct adis16480 *adis16480 = arg;
188 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_SERIAL_NUM,
197 DEFINE_SIMPLE_ATTRIBUTE(adis16480_serial_number_fops,
198 adis16480_show_serial_number, NULL, "0x%.4llx\n");
200 static int adis16480_show_product_id(void *arg, u64 *val)
202 struct adis16480 *adis16480 = arg;
206 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_PROD_ID,
215 DEFINE_SIMPLE_ATTRIBUTE(adis16480_product_id_fops,
216 adis16480_show_product_id, NULL, "%llu\n");
218 static int adis16480_show_flash_count(void *arg, u64 *val)
220 struct adis16480 *adis16480 = arg;
224 ret = adis_read_reg_32(&adis16480->adis, ADIS16480_REG_FLASH_CNT,
233 DEFINE_SIMPLE_ATTRIBUTE(adis16480_flash_count_fops,
234 adis16480_show_flash_count, NULL, "%lld\n");
236 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
238 struct adis16480 *adis16480 = iio_priv(indio_dev);
240 debugfs_create_file("firmware_revision", 0400,
241 indio_dev->debugfs_dentry, adis16480,
242 &adis16480_firmware_revision_fops);
243 debugfs_create_file("firmware_date", 0400, indio_dev->debugfs_dentry,
244 adis16480, &adis16480_firmware_date_fops);
245 debugfs_create_file("serial_number", 0400, indio_dev->debugfs_dentry,
246 adis16480, &adis16480_serial_number_fops);
247 debugfs_create_file("product_id", 0400, indio_dev->debugfs_dentry,
248 adis16480, &adis16480_product_id_fops);
249 debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
250 adis16480, &adis16480_flash_count_fops);
257 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
264 static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
266 struct adis16480 *st = iio_priv(indio_dev);
269 if (val < 0 || val2 < 0)
272 t = val * 1000 + val2 / 1000;
283 return adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
286 static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
288 struct adis16480 *st = iio_priv(indio_dev);
293 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
297 freq = 2460000 / (t + 1);
299 *val2 = (freq % 1000) * 1000;
301 return IIO_VAL_INT_PLUS_MICRO;
305 ADIS16480_SCAN_GYRO_X,
306 ADIS16480_SCAN_GYRO_Y,
307 ADIS16480_SCAN_GYRO_Z,
308 ADIS16480_SCAN_ACCEL_X,
309 ADIS16480_SCAN_ACCEL_Y,
310 ADIS16480_SCAN_ACCEL_Z,
311 ADIS16480_SCAN_MAGN_X,
312 ADIS16480_SCAN_MAGN_Y,
313 ADIS16480_SCAN_MAGN_Z,
318 static const unsigned int adis16480_calibbias_regs[] = {
319 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_BIAS,
320 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_BIAS,
321 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_BIAS,
322 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_BIAS,
323 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_BIAS,
324 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_BIAS,
325 [ADIS16480_SCAN_MAGN_X] = ADIS16480_REG_X_HARD_IRON,
326 [ADIS16480_SCAN_MAGN_Y] = ADIS16480_REG_Y_HARD_IRON,
327 [ADIS16480_SCAN_MAGN_Z] = ADIS16480_REG_Z_HARD_IRON,
328 [ADIS16480_SCAN_BARO] = ADIS16480_REG_BAROM_BIAS,
331 static const unsigned int adis16480_calibscale_regs[] = {
332 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_SCALE,
333 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_SCALE,
334 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_SCALE,
335 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_SCALE,
336 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_SCALE,
337 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_SCALE,
340 static int adis16480_set_calibbias(struct iio_dev *indio_dev,
341 const struct iio_chan_spec *chan, int bias)
343 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
344 struct adis16480 *st = iio_priv(indio_dev);
346 switch (chan->type) {
349 if (bias < -0x8000 || bias >= 0x8000)
351 return adis_write_reg_16(&st->adis, reg, bias);
354 return adis_write_reg_32(&st->adis, reg, bias);
362 static int adis16480_get_calibbias(struct iio_dev *indio_dev,
363 const struct iio_chan_spec *chan, int *bias)
365 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
366 struct adis16480 *st = iio_priv(indio_dev);
371 switch (chan->type) {
374 ret = adis_read_reg_16(&st->adis, reg, &val16);
376 *bias = sign_extend32(val16, 15);
380 ret = adis_read_reg_32(&st->adis, reg, &val32);
382 *bias = sign_extend32(val32, 31);
394 static int adis16480_set_calibscale(struct iio_dev *indio_dev,
395 const struct iio_chan_spec *chan, int scale)
397 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
398 struct adis16480 *st = iio_priv(indio_dev);
400 if (scale < -0x8000 || scale >= 0x8000)
403 return adis_write_reg_16(&st->adis, reg, scale);
406 static int adis16480_get_calibscale(struct iio_dev *indio_dev,
407 const struct iio_chan_spec *chan, int *scale)
409 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
410 struct adis16480 *st = iio_priv(indio_dev);
414 ret = adis_read_reg_16(&st->adis, reg, &val16);
418 *scale = sign_extend32(val16, 15);
422 static const unsigned int adis16480_def_filter_freqs[] = {
429 static const unsigned int ad16480_filter_data[][2] = {
430 [ADIS16480_SCAN_GYRO_X] = { ADIS16480_REG_FILTER_BNK0, 0 },
431 [ADIS16480_SCAN_GYRO_Y] = { ADIS16480_REG_FILTER_BNK0, 3 },
432 [ADIS16480_SCAN_GYRO_Z] = { ADIS16480_REG_FILTER_BNK0, 6 },
433 [ADIS16480_SCAN_ACCEL_X] = { ADIS16480_REG_FILTER_BNK0, 9 },
434 [ADIS16480_SCAN_ACCEL_Y] = { ADIS16480_REG_FILTER_BNK0, 12 },
435 [ADIS16480_SCAN_ACCEL_Z] = { ADIS16480_REG_FILTER_BNK1, 0 },
436 [ADIS16480_SCAN_MAGN_X] = { ADIS16480_REG_FILTER_BNK1, 3 },
437 [ADIS16480_SCAN_MAGN_Y] = { ADIS16480_REG_FILTER_BNK1, 6 },
438 [ADIS16480_SCAN_MAGN_Z] = { ADIS16480_REG_FILTER_BNK1, 9 },
441 static int adis16480_get_filter_freq(struct iio_dev *indio_dev,
442 const struct iio_chan_spec *chan, int *freq)
444 struct adis16480 *st = iio_priv(indio_dev);
445 unsigned int enable_mask, offset, reg;
449 reg = ad16480_filter_data[chan->scan_index][0];
450 offset = ad16480_filter_data[chan->scan_index][1];
451 enable_mask = BIT(offset + 2);
453 ret = adis_read_reg_16(&st->adis, reg, &val);
457 if (!(val & enable_mask))
460 *freq = adis16480_def_filter_freqs[(val >> offset) & 0x3];
465 static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
466 const struct iio_chan_spec *chan, unsigned int freq)
468 struct adis16480 *st = iio_priv(indio_dev);
469 unsigned int enable_mask, offset, reg;
470 unsigned int diff, best_diff;
471 unsigned int i, best_freq;
475 reg = ad16480_filter_data[chan->scan_index][0];
476 offset = ad16480_filter_data[chan->scan_index][1];
477 enable_mask = BIT(offset + 2);
479 ret = adis_read_reg_16(&st->adis, reg, &val);
488 for (i = 0; i < ARRAY_SIZE(adis16480_def_filter_freqs); i++) {
489 if (adis16480_def_filter_freqs[i] >= freq) {
490 diff = adis16480_def_filter_freqs[i] - freq;
491 if (diff < best_diff) {
498 val &= ~(0x3 << offset);
499 val |= best_freq << offset;
503 return adis_write_reg_16(&st->adis, reg, val);
506 static int adis16480_read_raw(struct iio_dev *indio_dev,
507 const struct iio_chan_spec *chan, int *val, int *val2, long info)
509 struct adis16480 *st = iio_priv(indio_dev);
512 case IIO_CHAN_INFO_RAW:
513 return adis_single_conversion(indio_dev, chan, 0, val);
514 case IIO_CHAN_INFO_SCALE:
515 switch (chan->type) {
517 *val = st->chip_info->gyro_max_scale;
518 *val2 = st->chip_info->gyro_max_val;
519 return IIO_VAL_FRACTIONAL;
521 *val = st->chip_info->accel_max_scale;
522 *val2 = st->chip_info->accel_max_val;
523 return IIO_VAL_FRACTIONAL;
526 *val2 = 100; /* 0.0001 gauss */
527 return IIO_VAL_INT_PLUS_MICRO;
530 *val2 = 650000; /* 5.65 milli degree Celsius */
531 return IIO_VAL_INT_PLUS_MICRO;
534 *val2 = 4000; /* 40ubar = 0.004 kPa */
535 return IIO_VAL_INT_PLUS_MICRO;
539 case IIO_CHAN_INFO_OFFSET:
540 /* Only the temperature channel has a offset */
541 *val = 4425; /* 25 degree Celsius = 0x0000 */
543 case IIO_CHAN_INFO_CALIBBIAS:
544 return adis16480_get_calibbias(indio_dev, chan, val);
545 case IIO_CHAN_INFO_CALIBSCALE:
546 return adis16480_get_calibscale(indio_dev, chan, val);
547 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
548 return adis16480_get_filter_freq(indio_dev, chan, val);
549 case IIO_CHAN_INFO_SAMP_FREQ:
550 return adis16480_get_freq(indio_dev, val, val2);
556 static int adis16480_write_raw(struct iio_dev *indio_dev,
557 const struct iio_chan_spec *chan, int val, int val2, long info)
560 case IIO_CHAN_INFO_CALIBBIAS:
561 return adis16480_set_calibbias(indio_dev, chan, val);
562 case IIO_CHAN_INFO_CALIBSCALE:
563 return adis16480_set_calibscale(indio_dev, chan, val);
564 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
565 return adis16480_set_filter_freq(indio_dev, chan, val);
566 case IIO_CHAN_INFO_SAMP_FREQ:
567 return adis16480_set_freq(indio_dev, val, val2);
574 #define ADIS16480_MOD_CHANNEL(_type, _mod, _address, _si, _info_sep, _bits) \
578 .channel2 = (_mod), \
579 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
580 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
582 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
583 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
584 .address = (_address), \
585 .scan_index = (_si), \
588 .realbits = (_bits), \
589 .storagebits = (_bits), \
590 .endianness = IIO_BE, \
594 #define ADIS16480_GYRO_CHANNEL(_mod) \
595 ADIS16480_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
596 ADIS16480_REG_ ## _mod ## _GYRO_OUT, ADIS16480_SCAN_GYRO_ ## _mod, \
597 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
598 BIT(IIO_CHAN_INFO_CALIBSCALE), \
601 #define ADIS16480_ACCEL_CHANNEL(_mod) \
602 ADIS16480_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
603 ADIS16480_REG_ ## _mod ## _ACCEL_OUT, ADIS16480_SCAN_ACCEL_ ## _mod, \
604 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
605 BIT(IIO_CHAN_INFO_CALIBSCALE), \
608 #define ADIS16480_MAGN_CHANNEL(_mod) \
609 ADIS16480_MOD_CHANNEL(IIO_MAGN, IIO_MOD_ ## _mod, \
610 ADIS16480_REG_ ## _mod ## _MAGN_OUT, ADIS16480_SCAN_MAGN_ ## _mod, \
611 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
614 #define ADIS16480_PRESSURE_CHANNEL() \
616 .type = IIO_PRESSURE, \
619 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
620 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
621 BIT(IIO_CHAN_INFO_SCALE), \
622 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
623 .address = ADIS16480_REG_BAROM_OUT, \
624 .scan_index = ADIS16480_SCAN_BARO, \
629 .endianness = IIO_BE, \
633 #define ADIS16480_TEMP_CHANNEL() { \
637 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
638 BIT(IIO_CHAN_INFO_SCALE) | \
639 BIT(IIO_CHAN_INFO_OFFSET), \
640 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
641 .address = ADIS16480_REG_TEMP_OUT, \
642 .scan_index = ADIS16480_SCAN_TEMP, \
647 .endianness = IIO_BE, \
651 static const struct iio_chan_spec adis16480_channels[] = {
652 ADIS16480_GYRO_CHANNEL(X),
653 ADIS16480_GYRO_CHANNEL(Y),
654 ADIS16480_GYRO_CHANNEL(Z),
655 ADIS16480_ACCEL_CHANNEL(X),
656 ADIS16480_ACCEL_CHANNEL(Y),
657 ADIS16480_ACCEL_CHANNEL(Z),
658 ADIS16480_MAGN_CHANNEL(X),
659 ADIS16480_MAGN_CHANNEL(Y),
660 ADIS16480_MAGN_CHANNEL(Z),
661 ADIS16480_PRESSURE_CHANNEL(),
662 ADIS16480_TEMP_CHANNEL(),
663 IIO_CHAN_SOFT_TIMESTAMP(11)
666 static const struct iio_chan_spec adis16485_channels[] = {
667 ADIS16480_GYRO_CHANNEL(X),
668 ADIS16480_GYRO_CHANNEL(Y),
669 ADIS16480_GYRO_CHANNEL(Z),
670 ADIS16480_ACCEL_CHANNEL(X),
671 ADIS16480_ACCEL_CHANNEL(Y),
672 ADIS16480_ACCEL_CHANNEL(Z),
673 ADIS16480_TEMP_CHANNEL(),
674 IIO_CHAN_SOFT_TIMESTAMP(7)
677 enum adis16480_variant {
684 static const struct adis16480_chip_info adis16480_chip_info[] = {
686 .channels = adis16485_channels,
687 .num_channels = ARRAY_SIZE(adis16485_channels),
689 * storing the value in rad/degree and the scale in degree
690 * gives us the result in rad and better precession than
691 * storing the scale directly in rad.
693 .gyro_max_val = IIO_RAD_TO_DEGREE(22887),
694 .gyro_max_scale = 300,
695 .accel_max_val = IIO_M_S_2_TO_G(21973),
696 .accel_max_scale = 18,
699 .channels = adis16480_channels,
700 .num_channels = ARRAY_SIZE(adis16480_channels),
701 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
702 .gyro_max_scale = 450,
703 .accel_max_val = IIO_M_S_2_TO_G(12500),
704 .accel_max_scale = 10,
707 .channels = adis16485_channels,
708 .num_channels = ARRAY_SIZE(adis16485_channels),
709 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
710 .gyro_max_scale = 450,
711 .accel_max_val = IIO_M_S_2_TO_G(20000),
712 .accel_max_scale = 5,
715 .channels = adis16480_channels,
716 .num_channels = ARRAY_SIZE(adis16480_channels),
717 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
718 .gyro_max_scale = 450,
719 .accel_max_val = IIO_M_S_2_TO_G(22500),
720 .accel_max_scale = 18,
724 static const struct iio_info adis16480_info = {
725 .read_raw = &adis16480_read_raw,
726 .write_raw = &adis16480_write_raw,
727 .update_scan_mode = adis_update_scan_mode,
728 .driver_module = THIS_MODULE,
729 .debugfs_reg_access = adis_debugfs_reg_access,
732 static int adis16480_stop_device(struct iio_dev *indio_dev)
734 struct adis16480 *st = iio_priv(indio_dev);
737 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_SLP_CNT, BIT(9));
739 dev_err(&indio_dev->dev,
740 "Could not power down device: %d\n", ret);
745 static int adis16480_enable_irq(struct adis *adis, bool enable)
747 return adis_write_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL,
748 enable ? BIT(3) : 0);
751 static int adis16480_initial_setup(struct iio_dev *indio_dev)
753 struct adis16480 *st = iio_priv(indio_dev);
755 unsigned int device_id;
758 adis_reset(&st->adis);
761 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_GLOB_CMD, BIT(1));
766 ret = adis_check_status(&st->adis);
770 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_PROD_ID, &prod_id);
774 ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
778 if (prod_id != device_id)
779 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
785 #define ADIS16480_DIAG_STAT_XGYRO_FAIL 0
786 #define ADIS16480_DIAG_STAT_YGYRO_FAIL 1
787 #define ADIS16480_DIAG_STAT_ZGYRO_FAIL 2
788 #define ADIS16480_DIAG_STAT_XACCL_FAIL 3
789 #define ADIS16480_DIAG_STAT_YACCL_FAIL 4
790 #define ADIS16480_DIAG_STAT_ZACCL_FAIL 5
791 #define ADIS16480_DIAG_STAT_XMAGN_FAIL 8
792 #define ADIS16480_DIAG_STAT_YMAGN_FAIL 9
793 #define ADIS16480_DIAG_STAT_ZMAGN_FAIL 10
794 #define ADIS16480_DIAG_STAT_BARO_FAIL 11
796 static const char * const adis16480_status_error_msgs[] = {
797 [ADIS16480_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
798 [ADIS16480_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
799 [ADIS16480_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
800 [ADIS16480_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
801 [ADIS16480_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
802 [ADIS16480_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
803 [ADIS16480_DIAG_STAT_XMAGN_FAIL] = "X-axis magnetometer self-test failure",
804 [ADIS16480_DIAG_STAT_YMAGN_FAIL] = "Y-axis magnetometer self-test failure",
805 [ADIS16480_DIAG_STAT_ZMAGN_FAIL] = "Z-axis magnetometer self-test failure",
806 [ADIS16480_DIAG_STAT_BARO_FAIL] = "Barometer self-test failure",
809 static const struct adis_data adis16480_data = {
810 .diag_stat_reg = ADIS16480_REG_DIAG_STS,
811 .glob_cmd_reg = ADIS16480_REG_GLOB_CMD,
817 .status_error_msgs = adis16480_status_error_msgs,
818 .status_error_mask = BIT(ADIS16480_DIAG_STAT_XGYRO_FAIL) |
819 BIT(ADIS16480_DIAG_STAT_YGYRO_FAIL) |
820 BIT(ADIS16480_DIAG_STAT_ZGYRO_FAIL) |
821 BIT(ADIS16480_DIAG_STAT_XACCL_FAIL) |
822 BIT(ADIS16480_DIAG_STAT_YACCL_FAIL) |
823 BIT(ADIS16480_DIAG_STAT_ZACCL_FAIL) |
824 BIT(ADIS16480_DIAG_STAT_XMAGN_FAIL) |
825 BIT(ADIS16480_DIAG_STAT_YMAGN_FAIL) |
826 BIT(ADIS16480_DIAG_STAT_ZMAGN_FAIL) |
827 BIT(ADIS16480_DIAG_STAT_BARO_FAIL),
829 .enable_irq = adis16480_enable_irq,
832 static int adis16480_probe(struct spi_device *spi)
834 const struct spi_device_id *id = spi_get_device_id(spi);
835 struct iio_dev *indio_dev;
836 struct adis16480 *st;
839 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
840 if (indio_dev == NULL)
843 spi_set_drvdata(spi, indio_dev);
845 st = iio_priv(indio_dev);
847 st->chip_info = &adis16480_chip_info[id->driver_data];
848 indio_dev->dev.parent = &spi->dev;
849 indio_dev->name = spi_get_device_id(spi)->name;
850 indio_dev->channels = st->chip_info->channels;
851 indio_dev->num_channels = st->chip_info->num_channels;
852 indio_dev->info = &adis16480_info;
853 indio_dev->modes = INDIO_DIRECT_MODE;
855 ret = adis_init(&st->adis, indio_dev, spi, &adis16480_data);
859 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL);
863 ret = adis16480_initial_setup(indio_dev);
865 goto error_cleanup_buffer;
867 ret = iio_device_register(indio_dev);
869 goto error_stop_device;
871 adis16480_debugfs_init(indio_dev);
876 adis16480_stop_device(indio_dev);
877 error_cleanup_buffer:
878 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
882 static int adis16480_remove(struct spi_device *spi)
884 struct iio_dev *indio_dev = spi_get_drvdata(spi);
885 struct adis16480 *st = iio_priv(indio_dev);
887 iio_device_unregister(indio_dev);
888 adis16480_stop_device(indio_dev);
890 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
895 static const struct spi_device_id adis16480_ids[] = {
896 { "adis16375", ADIS16375 },
897 { "adis16480", ADIS16480 },
898 { "adis16485", ADIS16485 },
899 { "adis16488", ADIS16488 },
902 MODULE_DEVICE_TABLE(spi, adis16480_ids);
904 static struct spi_driver adis16480_driver = {
908 .id_table = adis16480_ids,
909 .probe = adis16480_probe,
910 .remove = adis16480_remove,
912 module_spi_driver(adis16480_driver);
914 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
915 MODULE_DESCRIPTION("Analog Devices ADIS16480 IMU driver");
916 MODULE_LICENSE("GPL v2");