Linux 4.19.133
[linux/fpc-iii.git] / drivers / iio / imu / adis16480.c
blob186f71927006a991628aded3b6a35a39823a8b6f
1 /*
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;
119 struct adis16480 {
120 const struct adis16480_chip_info *chip_info;
122 struct adis adis;
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;
131 char buf[7];
132 size_t len;
133 u16 rev;
134 int ret;
136 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_REV, &rev);
137 if (ret < 0)
138 return ret;
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 = {
146 .open = simple_open,
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;
156 u16 md, year;
157 char buf[12];
158 size_t len;
159 int ret;
161 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_Y, &year);
162 if (ret < 0)
163 return ret;
165 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_FIRM_DM, &md);
166 if (ret < 0)
167 return ret;
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 = {
176 .open = simple_open,
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;
185 u16 serial;
186 int ret;
188 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_SERIAL_NUM,
189 &serial);
190 if (ret < 0)
191 return ret;
193 *val = serial;
195 return 0;
197 DEFINE_DEBUGFS_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;
203 u16 prod_id;
204 int ret;
206 ret = adis_read_reg_16(&adis16480->adis, ADIS16480_REG_PROD_ID,
207 &prod_id);
208 if (ret < 0)
209 return ret;
211 *val = prod_id;
213 return 0;
215 DEFINE_DEBUGFS_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;
221 u32 flash_count;
222 int ret;
224 ret = adis_read_reg_32(&adis16480->adis, ADIS16480_REG_FLASH_CNT,
225 &flash_count);
226 if (ret < 0)
227 return ret;
229 *val = flash_count;
231 return 0;
233 DEFINE_DEBUGFS_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_unsafe("firmware_revision", 0400,
241 indio_dev->debugfs_dentry, adis16480,
242 &adis16480_firmware_revision_fops);
243 debugfs_create_file_unsafe("firmware_date", 0400,
244 indio_dev->debugfs_dentry, adis16480,
245 &adis16480_firmware_date_fops);
246 debugfs_create_file_unsafe("serial_number", 0400,
247 indio_dev->debugfs_dentry, adis16480,
248 &adis16480_serial_number_fops);
249 debugfs_create_file_unsafe("product_id", 0400,
250 indio_dev->debugfs_dentry, adis16480,
251 &adis16480_product_id_fops);
252 debugfs_create_file_unsafe("flash_count", 0400,
253 indio_dev->debugfs_dentry, adis16480,
254 &adis16480_flash_count_fops);
256 return 0;
259 #else
261 static int adis16480_debugfs_init(struct iio_dev *indio_dev)
263 return 0;
266 #endif
268 static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2)
270 struct adis16480 *st = iio_priv(indio_dev);
271 unsigned int t;
273 if (val < 0 || val2 < 0)
274 return -EINVAL;
276 t = val * 1000 + val2 / 1000;
277 if (t == 0)
278 return -EINVAL;
280 t = 2460000 / t;
281 if (t > 2048)
282 t = 2048;
284 if (t != 0)
285 t--;
287 return adis_write_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, t);
290 static int adis16480_get_freq(struct iio_dev *indio_dev, int *val, int *val2)
292 struct adis16480 *st = iio_priv(indio_dev);
293 uint16_t t;
294 int ret;
295 unsigned freq;
297 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_DEC_RATE, &t);
298 if (ret < 0)
299 return ret;
301 freq = 2460000 / (t + 1);
302 *val = freq / 1000;
303 *val2 = (freq % 1000) * 1000;
305 return IIO_VAL_INT_PLUS_MICRO;
308 enum {
309 ADIS16480_SCAN_GYRO_X,
310 ADIS16480_SCAN_GYRO_Y,
311 ADIS16480_SCAN_GYRO_Z,
312 ADIS16480_SCAN_ACCEL_X,
313 ADIS16480_SCAN_ACCEL_Y,
314 ADIS16480_SCAN_ACCEL_Z,
315 ADIS16480_SCAN_MAGN_X,
316 ADIS16480_SCAN_MAGN_Y,
317 ADIS16480_SCAN_MAGN_Z,
318 ADIS16480_SCAN_BARO,
319 ADIS16480_SCAN_TEMP,
322 static const unsigned int adis16480_calibbias_regs[] = {
323 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_BIAS,
324 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_BIAS,
325 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_BIAS,
326 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_BIAS,
327 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_BIAS,
328 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_BIAS,
329 [ADIS16480_SCAN_MAGN_X] = ADIS16480_REG_X_HARD_IRON,
330 [ADIS16480_SCAN_MAGN_Y] = ADIS16480_REG_Y_HARD_IRON,
331 [ADIS16480_SCAN_MAGN_Z] = ADIS16480_REG_Z_HARD_IRON,
332 [ADIS16480_SCAN_BARO] = ADIS16480_REG_BAROM_BIAS,
335 static const unsigned int adis16480_calibscale_regs[] = {
336 [ADIS16480_SCAN_GYRO_X] = ADIS16480_REG_X_GYRO_SCALE,
337 [ADIS16480_SCAN_GYRO_Y] = ADIS16480_REG_Y_GYRO_SCALE,
338 [ADIS16480_SCAN_GYRO_Z] = ADIS16480_REG_Z_GYRO_SCALE,
339 [ADIS16480_SCAN_ACCEL_X] = ADIS16480_REG_X_ACCEL_SCALE,
340 [ADIS16480_SCAN_ACCEL_Y] = ADIS16480_REG_Y_ACCEL_SCALE,
341 [ADIS16480_SCAN_ACCEL_Z] = ADIS16480_REG_Z_ACCEL_SCALE,
344 static int adis16480_set_calibbias(struct iio_dev *indio_dev,
345 const struct iio_chan_spec *chan, int bias)
347 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
348 struct adis16480 *st = iio_priv(indio_dev);
350 switch (chan->type) {
351 case IIO_MAGN:
352 case IIO_PRESSURE:
353 if (bias < -0x8000 || bias >= 0x8000)
354 return -EINVAL;
355 return adis_write_reg_16(&st->adis, reg, bias);
356 case IIO_ANGL_VEL:
357 case IIO_ACCEL:
358 return adis_write_reg_32(&st->adis, reg, bias);
359 default:
360 break;
363 return -EINVAL;
366 static int adis16480_get_calibbias(struct iio_dev *indio_dev,
367 const struct iio_chan_spec *chan, int *bias)
369 unsigned int reg = adis16480_calibbias_regs[chan->scan_index];
370 struct adis16480 *st = iio_priv(indio_dev);
371 uint16_t val16;
372 uint32_t val32;
373 int ret;
375 switch (chan->type) {
376 case IIO_MAGN:
377 case IIO_PRESSURE:
378 ret = adis_read_reg_16(&st->adis, reg, &val16);
379 if (ret == 0)
380 *bias = sign_extend32(val16, 15);
381 break;
382 case IIO_ANGL_VEL:
383 case IIO_ACCEL:
384 ret = adis_read_reg_32(&st->adis, reg, &val32);
385 if (ret == 0)
386 *bias = sign_extend32(val32, 31);
387 break;
388 default:
389 ret = -EINVAL;
392 if (ret < 0)
393 return ret;
395 return IIO_VAL_INT;
398 static int adis16480_set_calibscale(struct iio_dev *indio_dev,
399 const struct iio_chan_spec *chan, int scale)
401 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
402 struct adis16480 *st = iio_priv(indio_dev);
404 if (scale < -0x8000 || scale >= 0x8000)
405 return -EINVAL;
407 return adis_write_reg_16(&st->adis, reg, scale);
410 static int adis16480_get_calibscale(struct iio_dev *indio_dev,
411 const struct iio_chan_spec *chan, int *scale)
413 unsigned int reg = adis16480_calibscale_regs[chan->scan_index];
414 struct adis16480 *st = iio_priv(indio_dev);
415 uint16_t val16;
416 int ret;
418 ret = adis_read_reg_16(&st->adis, reg, &val16);
419 if (ret < 0)
420 return ret;
422 *scale = sign_extend32(val16, 15);
423 return IIO_VAL_INT;
426 static const unsigned int adis16480_def_filter_freqs[] = {
427 310,
429 275,
433 static const unsigned int ad16480_filter_data[][2] = {
434 [ADIS16480_SCAN_GYRO_X] = { ADIS16480_REG_FILTER_BNK0, 0 },
435 [ADIS16480_SCAN_GYRO_Y] = { ADIS16480_REG_FILTER_BNK0, 3 },
436 [ADIS16480_SCAN_GYRO_Z] = { ADIS16480_REG_FILTER_BNK0, 6 },
437 [ADIS16480_SCAN_ACCEL_X] = { ADIS16480_REG_FILTER_BNK0, 9 },
438 [ADIS16480_SCAN_ACCEL_Y] = { ADIS16480_REG_FILTER_BNK0, 12 },
439 [ADIS16480_SCAN_ACCEL_Z] = { ADIS16480_REG_FILTER_BNK1, 0 },
440 [ADIS16480_SCAN_MAGN_X] = { ADIS16480_REG_FILTER_BNK1, 3 },
441 [ADIS16480_SCAN_MAGN_Y] = { ADIS16480_REG_FILTER_BNK1, 6 },
442 [ADIS16480_SCAN_MAGN_Z] = { ADIS16480_REG_FILTER_BNK1, 9 },
445 static int adis16480_get_filter_freq(struct iio_dev *indio_dev,
446 const struct iio_chan_spec *chan, int *freq)
448 struct adis16480 *st = iio_priv(indio_dev);
449 unsigned int enable_mask, offset, reg;
450 uint16_t val;
451 int ret;
453 reg = ad16480_filter_data[chan->scan_index][0];
454 offset = ad16480_filter_data[chan->scan_index][1];
455 enable_mask = BIT(offset + 2);
457 ret = adis_read_reg_16(&st->adis, reg, &val);
458 if (ret < 0)
459 return ret;
461 if (!(val & enable_mask))
462 *freq = 0;
463 else
464 *freq = adis16480_def_filter_freqs[(val >> offset) & 0x3];
466 return IIO_VAL_INT;
469 static int adis16480_set_filter_freq(struct iio_dev *indio_dev,
470 const struct iio_chan_spec *chan, unsigned int freq)
472 struct adis16480 *st = iio_priv(indio_dev);
473 unsigned int enable_mask, offset, reg;
474 unsigned int diff, best_diff;
475 unsigned int i, best_freq;
476 uint16_t val;
477 int ret;
479 reg = ad16480_filter_data[chan->scan_index][0];
480 offset = ad16480_filter_data[chan->scan_index][1];
481 enable_mask = BIT(offset + 2);
483 ret = adis_read_reg_16(&st->adis, reg, &val);
484 if (ret < 0)
485 return ret;
487 if (freq == 0) {
488 val &= ~enable_mask;
489 } else {
490 best_freq = 0;
491 best_diff = 310;
492 for (i = 0; i < ARRAY_SIZE(adis16480_def_filter_freqs); i++) {
493 if (adis16480_def_filter_freqs[i] >= freq) {
494 diff = adis16480_def_filter_freqs[i] - freq;
495 if (diff < best_diff) {
496 best_diff = diff;
497 best_freq = i;
502 val &= ~(0x3 << offset);
503 val |= best_freq << offset;
504 val |= enable_mask;
507 return adis_write_reg_16(&st->adis, reg, val);
510 static int adis16480_read_raw(struct iio_dev *indio_dev,
511 const struct iio_chan_spec *chan, int *val, int *val2, long info)
513 struct adis16480 *st = iio_priv(indio_dev);
515 switch (info) {
516 case IIO_CHAN_INFO_RAW:
517 return adis_single_conversion(indio_dev, chan, 0, val);
518 case IIO_CHAN_INFO_SCALE:
519 switch (chan->type) {
520 case IIO_ANGL_VEL:
521 *val = st->chip_info->gyro_max_scale;
522 *val2 = st->chip_info->gyro_max_val;
523 return IIO_VAL_FRACTIONAL;
524 case IIO_ACCEL:
525 *val = st->chip_info->accel_max_scale;
526 *val2 = st->chip_info->accel_max_val;
527 return IIO_VAL_FRACTIONAL;
528 case IIO_MAGN:
529 *val = 0;
530 *val2 = 100; /* 0.0001 gauss */
531 return IIO_VAL_INT_PLUS_MICRO;
532 case IIO_TEMP:
533 *val = 5;
534 *val2 = 650000; /* 5.65 milli degree Celsius */
535 return IIO_VAL_INT_PLUS_MICRO;
536 case IIO_PRESSURE:
537 *val = 0;
538 *val2 = 4000; /* 40ubar = 0.004 kPa */
539 return IIO_VAL_INT_PLUS_MICRO;
540 default:
541 return -EINVAL;
543 case IIO_CHAN_INFO_OFFSET:
544 /* Only the temperature channel has a offset */
545 *val = 4425; /* 25 degree Celsius = 0x0000 */
546 return IIO_VAL_INT;
547 case IIO_CHAN_INFO_CALIBBIAS:
548 return adis16480_get_calibbias(indio_dev, chan, val);
549 case IIO_CHAN_INFO_CALIBSCALE:
550 return adis16480_get_calibscale(indio_dev, chan, val);
551 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
552 return adis16480_get_filter_freq(indio_dev, chan, val);
553 case IIO_CHAN_INFO_SAMP_FREQ:
554 return adis16480_get_freq(indio_dev, val, val2);
555 default:
556 return -EINVAL;
560 static int adis16480_write_raw(struct iio_dev *indio_dev,
561 const struct iio_chan_spec *chan, int val, int val2, long info)
563 switch (info) {
564 case IIO_CHAN_INFO_CALIBBIAS:
565 return adis16480_set_calibbias(indio_dev, chan, val);
566 case IIO_CHAN_INFO_CALIBSCALE:
567 return adis16480_set_calibscale(indio_dev, chan, val);
568 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
569 return adis16480_set_filter_freq(indio_dev, chan, val);
570 case IIO_CHAN_INFO_SAMP_FREQ:
571 return adis16480_set_freq(indio_dev, val, val2);
573 default:
574 return -EINVAL;
578 #define ADIS16480_MOD_CHANNEL(_type, _mod, _address, _si, _info_sep, _bits) \
580 .type = (_type), \
581 .modified = 1, \
582 .channel2 = (_mod), \
583 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
584 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
585 _info_sep, \
586 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
587 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
588 .address = (_address), \
589 .scan_index = (_si), \
590 .scan_type = { \
591 .sign = 's', \
592 .realbits = (_bits), \
593 .storagebits = (_bits), \
594 .endianness = IIO_BE, \
595 }, \
598 #define ADIS16480_GYRO_CHANNEL(_mod) \
599 ADIS16480_MOD_CHANNEL(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \
600 ADIS16480_REG_ ## _mod ## _GYRO_OUT, ADIS16480_SCAN_GYRO_ ## _mod, \
601 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
602 BIT(IIO_CHAN_INFO_CALIBSCALE), \
605 #define ADIS16480_ACCEL_CHANNEL(_mod) \
606 ADIS16480_MOD_CHANNEL(IIO_ACCEL, IIO_MOD_ ## _mod, \
607 ADIS16480_REG_ ## _mod ## _ACCEL_OUT, ADIS16480_SCAN_ACCEL_ ## _mod, \
608 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
609 BIT(IIO_CHAN_INFO_CALIBSCALE), \
612 #define ADIS16480_MAGN_CHANNEL(_mod) \
613 ADIS16480_MOD_CHANNEL(IIO_MAGN, IIO_MOD_ ## _mod, \
614 ADIS16480_REG_ ## _mod ## _MAGN_OUT, ADIS16480_SCAN_MAGN_ ## _mod, \
615 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
618 #define ADIS16480_PRESSURE_CHANNEL() \
620 .type = IIO_PRESSURE, \
621 .indexed = 1, \
622 .channel = 0, \
623 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
624 BIT(IIO_CHAN_INFO_CALIBBIAS) | \
625 BIT(IIO_CHAN_INFO_SCALE), \
626 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
627 .address = ADIS16480_REG_BAROM_OUT, \
628 .scan_index = ADIS16480_SCAN_BARO, \
629 .scan_type = { \
630 .sign = 's', \
631 .realbits = 32, \
632 .storagebits = 32, \
633 .endianness = IIO_BE, \
634 }, \
637 #define ADIS16480_TEMP_CHANNEL() { \
638 .type = IIO_TEMP, \
639 .indexed = 1, \
640 .channel = 0, \
641 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
642 BIT(IIO_CHAN_INFO_SCALE) | \
643 BIT(IIO_CHAN_INFO_OFFSET), \
644 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
645 .address = ADIS16480_REG_TEMP_OUT, \
646 .scan_index = ADIS16480_SCAN_TEMP, \
647 .scan_type = { \
648 .sign = 's', \
649 .realbits = 16, \
650 .storagebits = 16, \
651 .endianness = IIO_BE, \
652 }, \
655 static const struct iio_chan_spec adis16480_channels[] = {
656 ADIS16480_GYRO_CHANNEL(X),
657 ADIS16480_GYRO_CHANNEL(Y),
658 ADIS16480_GYRO_CHANNEL(Z),
659 ADIS16480_ACCEL_CHANNEL(X),
660 ADIS16480_ACCEL_CHANNEL(Y),
661 ADIS16480_ACCEL_CHANNEL(Z),
662 ADIS16480_MAGN_CHANNEL(X),
663 ADIS16480_MAGN_CHANNEL(Y),
664 ADIS16480_MAGN_CHANNEL(Z),
665 ADIS16480_PRESSURE_CHANNEL(),
666 ADIS16480_TEMP_CHANNEL(),
667 IIO_CHAN_SOFT_TIMESTAMP(11)
670 static const struct iio_chan_spec adis16485_channels[] = {
671 ADIS16480_GYRO_CHANNEL(X),
672 ADIS16480_GYRO_CHANNEL(Y),
673 ADIS16480_GYRO_CHANNEL(Z),
674 ADIS16480_ACCEL_CHANNEL(X),
675 ADIS16480_ACCEL_CHANNEL(Y),
676 ADIS16480_ACCEL_CHANNEL(Z),
677 ADIS16480_TEMP_CHANNEL(),
678 IIO_CHAN_SOFT_TIMESTAMP(7)
681 enum adis16480_variant {
682 ADIS16375,
683 ADIS16480,
684 ADIS16485,
685 ADIS16488,
688 static const struct adis16480_chip_info adis16480_chip_info[] = {
689 [ADIS16375] = {
690 .channels = adis16485_channels,
691 .num_channels = ARRAY_SIZE(adis16485_channels),
693 * storing the value in rad/degree and the scale in degree
694 * gives us the result in rad and better precession than
695 * storing the scale directly in rad.
697 .gyro_max_val = IIO_RAD_TO_DEGREE(22887),
698 .gyro_max_scale = 300,
699 .accel_max_val = IIO_M_S_2_TO_G(21973),
700 .accel_max_scale = 18,
702 [ADIS16480] = {
703 .channels = adis16480_channels,
704 .num_channels = ARRAY_SIZE(adis16480_channels),
705 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
706 .gyro_max_scale = 450,
707 .accel_max_val = IIO_M_S_2_TO_G(12500),
708 .accel_max_scale = 10,
710 [ADIS16485] = {
711 .channels = adis16485_channels,
712 .num_channels = ARRAY_SIZE(adis16485_channels),
713 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
714 .gyro_max_scale = 450,
715 .accel_max_val = IIO_M_S_2_TO_G(20000),
716 .accel_max_scale = 5,
718 [ADIS16488] = {
719 .channels = adis16480_channels,
720 .num_channels = ARRAY_SIZE(adis16480_channels),
721 .gyro_max_val = IIO_RAD_TO_DEGREE(22500),
722 .gyro_max_scale = 450,
723 .accel_max_val = IIO_M_S_2_TO_G(22500),
724 .accel_max_scale = 18,
728 static const struct iio_info adis16480_info = {
729 .read_raw = &adis16480_read_raw,
730 .write_raw = &adis16480_write_raw,
731 .update_scan_mode = adis_update_scan_mode,
732 .debugfs_reg_access = adis_debugfs_reg_access,
735 static int adis16480_stop_device(struct iio_dev *indio_dev)
737 struct adis16480 *st = iio_priv(indio_dev);
738 int ret;
740 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_SLP_CNT, BIT(9));
741 if (ret)
742 dev_err(&indio_dev->dev,
743 "Could not power down device: %d\n", ret);
745 return ret;
748 static int adis16480_enable_irq(struct adis *adis, bool enable)
750 return adis_write_reg_16(adis, ADIS16480_REG_FNCTIO_CTRL,
751 enable ? BIT(3) : 0);
754 static int adis16480_initial_setup(struct iio_dev *indio_dev)
756 struct adis16480 *st = iio_priv(indio_dev);
757 uint16_t prod_id;
758 unsigned int device_id;
759 int ret;
761 adis_reset(&st->adis);
762 msleep(70);
764 ret = adis_write_reg_16(&st->adis, ADIS16480_REG_GLOB_CMD, BIT(1));
765 if (ret)
766 return ret;
767 msleep(30);
769 ret = adis_check_status(&st->adis);
770 if (ret)
771 return ret;
773 ret = adis_read_reg_16(&st->adis, ADIS16480_REG_PROD_ID, &prod_id);
774 if (ret)
775 return ret;
777 ret = sscanf(indio_dev->name, "adis%u\n", &device_id);
778 if (ret != 1)
779 return -EINVAL;
781 if (prod_id != device_id)
782 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
783 device_id, prod_id);
785 return 0;
788 #define ADIS16480_DIAG_STAT_XGYRO_FAIL 0
789 #define ADIS16480_DIAG_STAT_YGYRO_FAIL 1
790 #define ADIS16480_DIAG_STAT_ZGYRO_FAIL 2
791 #define ADIS16480_DIAG_STAT_XACCL_FAIL 3
792 #define ADIS16480_DIAG_STAT_YACCL_FAIL 4
793 #define ADIS16480_DIAG_STAT_ZACCL_FAIL 5
794 #define ADIS16480_DIAG_STAT_XMAGN_FAIL 8
795 #define ADIS16480_DIAG_STAT_YMAGN_FAIL 9
796 #define ADIS16480_DIAG_STAT_ZMAGN_FAIL 10
797 #define ADIS16480_DIAG_STAT_BARO_FAIL 11
799 static const char * const adis16480_status_error_msgs[] = {
800 [ADIS16480_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
801 [ADIS16480_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
802 [ADIS16480_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
803 [ADIS16480_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
804 [ADIS16480_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
805 [ADIS16480_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
806 [ADIS16480_DIAG_STAT_XMAGN_FAIL] = "X-axis magnetometer self-test failure",
807 [ADIS16480_DIAG_STAT_YMAGN_FAIL] = "Y-axis magnetometer self-test failure",
808 [ADIS16480_DIAG_STAT_ZMAGN_FAIL] = "Z-axis magnetometer self-test failure",
809 [ADIS16480_DIAG_STAT_BARO_FAIL] = "Barometer self-test failure",
812 static const struct adis_data adis16480_data = {
813 .diag_stat_reg = ADIS16480_REG_DIAG_STS,
814 .glob_cmd_reg = ADIS16480_REG_GLOB_CMD,
815 .has_paging = true,
817 .read_delay = 5,
818 .write_delay = 5,
820 .status_error_msgs = adis16480_status_error_msgs,
821 .status_error_mask = BIT(ADIS16480_DIAG_STAT_XGYRO_FAIL) |
822 BIT(ADIS16480_DIAG_STAT_YGYRO_FAIL) |
823 BIT(ADIS16480_DIAG_STAT_ZGYRO_FAIL) |
824 BIT(ADIS16480_DIAG_STAT_XACCL_FAIL) |
825 BIT(ADIS16480_DIAG_STAT_YACCL_FAIL) |
826 BIT(ADIS16480_DIAG_STAT_ZACCL_FAIL) |
827 BIT(ADIS16480_DIAG_STAT_XMAGN_FAIL) |
828 BIT(ADIS16480_DIAG_STAT_YMAGN_FAIL) |
829 BIT(ADIS16480_DIAG_STAT_ZMAGN_FAIL) |
830 BIT(ADIS16480_DIAG_STAT_BARO_FAIL),
832 .enable_irq = adis16480_enable_irq,
835 static int adis16480_probe(struct spi_device *spi)
837 const struct spi_device_id *id = spi_get_device_id(spi);
838 struct iio_dev *indio_dev;
839 struct adis16480 *st;
840 int ret;
842 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
843 if (indio_dev == NULL)
844 return -ENOMEM;
846 spi_set_drvdata(spi, indio_dev);
848 st = iio_priv(indio_dev);
850 st->chip_info = &adis16480_chip_info[id->driver_data];
851 indio_dev->dev.parent = &spi->dev;
852 indio_dev->name = spi_get_device_id(spi)->name;
853 indio_dev->channels = st->chip_info->channels;
854 indio_dev->num_channels = st->chip_info->num_channels;
855 indio_dev->info = &adis16480_info;
856 indio_dev->modes = INDIO_DIRECT_MODE;
858 ret = adis_init(&st->adis, indio_dev, spi, &adis16480_data);
859 if (ret)
860 return ret;
862 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev, NULL);
863 if (ret)
864 return ret;
866 ret = adis16480_initial_setup(indio_dev);
867 if (ret)
868 goto error_cleanup_buffer;
870 ret = iio_device_register(indio_dev);
871 if (ret)
872 goto error_stop_device;
874 adis16480_debugfs_init(indio_dev);
876 return 0;
878 error_stop_device:
879 adis16480_stop_device(indio_dev);
880 error_cleanup_buffer:
881 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
882 return ret;
885 static int adis16480_remove(struct spi_device *spi)
887 struct iio_dev *indio_dev = spi_get_drvdata(spi);
888 struct adis16480 *st = iio_priv(indio_dev);
890 iio_device_unregister(indio_dev);
891 adis16480_stop_device(indio_dev);
893 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
895 return 0;
898 static const struct spi_device_id adis16480_ids[] = {
899 { "adis16375", ADIS16375 },
900 { "adis16480", ADIS16480 },
901 { "adis16485", ADIS16485 },
902 { "adis16488", ADIS16488 },
905 MODULE_DEVICE_TABLE(spi, adis16480_ids);
907 static struct spi_driver adis16480_driver = {
908 .driver = {
909 .name = "adis16480",
911 .id_table = adis16480_ids,
912 .probe = adis16480_probe,
913 .remove = adis16480_remove,
915 module_spi_driver(adis16480_driver);
917 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
918 MODULE_DESCRIPTION("Analog Devices ADIS16480 IMU driver");
919 MODULE_LICENSE("GPL v2");