fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / iio / imu / adis16400_core.c
blob368660dfe135a51c3cac05dd90d7dcb75dacb7c2
1 /*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@kernel.org>
9 * Copyright (c) 2011 Analog Devices Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/delay.h>
20 #include <linux/mutex.h>
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/spi/spi.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/debugfs.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/buffer.h>
34 #include "adis16400.h"
36 #ifdef CONFIG_DEBUG_FS
38 static ssize_t adis16400_show_serial_number(struct file *file,
39 char __user *userbuf, size_t count, loff_t *ppos)
41 struct adis16400_state *st = file->private_data;
42 u16 lot1, lot2, serial_number;
43 char buf[16];
44 size_t len;
45 int ret;
47 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID1, &lot1);
48 if (ret < 0)
49 return ret;
51 ret = adis_read_reg_16(&st->adis, ADIS16334_LOT_ID2, &lot2);
52 if (ret < 0)
53 return ret;
55 ret = adis_read_reg_16(&st->adis, ADIS16334_SERIAL_NUMBER,
56 &serial_number);
57 if (ret < 0)
58 return ret;
60 len = snprintf(buf, sizeof(buf), "%.4x-%.4x-%.4x\n", lot1, lot2,
61 serial_number);
63 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
66 static const struct file_operations adis16400_serial_number_fops = {
67 .open = simple_open,
68 .read = adis16400_show_serial_number,
69 .llseek = default_llseek,
70 .owner = THIS_MODULE,
73 static int adis16400_show_product_id(void *arg, u64 *val)
75 struct adis16400_state *st = arg;
76 uint16_t prod_id;
77 int ret;
79 ret = adis_read_reg_16(&st->adis, ADIS16400_PRODUCT_ID, &prod_id);
80 if (ret < 0)
81 return ret;
83 *val = prod_id;
85 return 0;
87 DEFINE_SIMPLE_ATTRIBUTE(adis16400_product_id_fops,
88 adis16400_show_product_id, NULL, "%lld\n");
90 static int adis16400_show_flash_count(void *arg, u64 *val)
92 struct adis16400_state *st = arg;
93 uint16_t flash_count;
94 int ret;
96 ret = adis_read_reg_16(&st->adis, ADIS16400_FLASH_CNT, &flash_count);
97 if (ret < 0)
98 return ret;
100 *val = flash_count;
102 return 0;
104 DEFINE_SIMPLE_ATTRIBUTE(adis16400_flash_count_fops,
105 adis16400_show_flash_count, NULL, "%lld\n");
107 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
109 struct adis16400_state *st = iio_priv(indio_dev);
111 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER)
112 debugfs_create_file("serial_number", 0400,
113 indio_dev->debugfs_dentry, st,
114 &adis16400_serial_number_fops);
115 if (st->variant->flags & ADIS16400_HAS_PROD_ID)
116 debugfs_create_file("product_id", 0400,
117 indio_dev->debugfs_dentry, st,
118 &adis16400_product_id_fops);
119 debugfs_create_file("flash_count", 0400, indio_dev->debugfs_dentry,
120 st, &adis16400_flash_count_fops);
122 return 0;
125 #else
127 static int adis16400_debugfs_init(struct iio_dev *indio_dev)
129 return 0;
132 #endif
134 enum adis16400_chip_variant {
135 ADIS16300,
136 ADIS16334,
137 ADIS16350,
138 ADIS16360,
139 ADIS16362,
140 ADIS16364,
141 ADIS16400,
142 ADIS16448,
145 static int adis16334_get_freq(struct adis16400_state *st)
147 int ret;
148 uint16_t t;
150 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
151 if (ret < 0)
152 return ret;
154 t >>= ADIS16334_RATE_DIV_SHIFT;
156 return 819200 >> t;
159 static int adis16334_set_freq(struct adis16400_state *st, unsigned int freq)
161 unsigned int t;
163 if (freq < 819200)
164 t = ilog2(819200 / freq);
165 else
166 t = 0;
168 if (t > 0x31)
169 t = 0x31;
171 t <<= ADIS16334_RATE_DIV_SHIFT;
172 t |= ADIS16334_RATE_INT_CLK;
174 return adis_write_reg_16(&st->adis, ADIS16400_SMPL_PRD, t);
177 static int adis16400_get_freq(struct adis16400_state *st)
179 int sps, ret;
180 uint16_t t;
182 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &t);
183 if (ret < 0)
184 return ret;
186 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 52851 : 1638404;
187 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
189 return sps;
192 static int adis16400_set_freq(struct adis16400_state *st, unsigned int freq)
194 unsigned int t;
195 uint8_t val = 0;
197 t = 1638404 / freq;
198 if (t >= 128) {
199 val |= ADIS16400_SMPL_PRD_TIME_BASE;
200 t = 52851 / freq;
201 if (t >= 128)
202 t = 127;
203 } else if (t != 0) {
204 t--;
207 val |= t;
209 if (t >= 0x0A || (val & ADIS16400_SMPL_PRD_TIME_BASE))
210 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
211 else
212 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
214 return adis_write_reg_8(&st->adis, ADIS16400_SMPL_PRD, val);
217 static ssize_t adis16400_read_frequency(struct device *dev,
218 struct device_attribute *attr,
219 char *buf)
221 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
222 struct adis16400_state *st = iio_priv(indio_dev);
223 int ret;
225 ret = st->variant->get_freq(st);
226 if (ret < 0)
227 return ret;
229 return sprintf(buf, "%d.%.3d\n", ret / 1000, ret % 1000);
232 static const unsigned adis16400_3db_divisors[] = {
233 [0] = 2, /* Special case */
234 [1] = 6,
235 [2] = 12,
236 [3] = 25,
237 [4] = 50,
238 [5] = 100,
239 [6] = 200,
240 [7] = 200, /* Not a valid setting */
243 static int adis16400_set_filter(struct iio_dev *indio_dev, int sps, int val)
245 struct adis16400_state *st = iio_priv(indio_dev);
246 uint16_t val16;
247 int i, ret;
249 for (i = ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 1; i--) {
250 if (sps / adis16400_3db_divisors[i] >= val)
251 break;
254 ret = adis_read_reg_16(&st->adis, ADIS16400_SENS_AVG, &val16);
255 if (ret < 0)
256 return ret;
258 ret = adis_write_reg_16(&st->adis, ADIS16400_SENS_AVG,
259 (val16 & ~0x07) | i);
260 return ret;
263 static ssize_t adis16400_write_frequency(struct device *dev,
264 struct device_attribute *attr, const char *buf, size_t len)
266 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
267 struct adis16400_state *st = iio_priv(indio_dev);
268 int i, f, val;
269 int ret;
271 ret = iio_str_to_fixpoint(buf, 100, &i, &f);
272 if (ret)
273 return ret;
275 val = i * 1000 + f;
277 if (val <= 0)
278 return -EINVAL;
280 mutex_lock(&indio_dev->mlock);
281 st->variant->set_freq(st, val);
282 mutex_unlock(&indio_dev->mlock);
284 return ret ? ret : len;
287 /* Power down the device */
288 static int adis16400_stop_device(struct iio_dev *indio_dev)
290 struct adis16400_state *st = iio_priv(indio_dev);
291 int ret;
293 ret = adis_write_reg_16(&st->adis, ADIS16400_SLP_CNT,
294 ADIS16400_SLP_CNT_POWER_OFF);
295 if (ret)
296 dev_err(&indio_dev->dev,
297 "problem with turning device off: SLP_CNT");
299 return ret;
302 static int adis16400_initial_setup(struct iio_dev *indio_dev)
304 struct adis16400_state *st = iio_priv(indio_dev);
305 uint16_t prod_id, smp_prd;
306 unsigned int device_id;
307 int ret;
309 /* use low spi speed for init if the device has a slow mode */
310 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE)
311 st->adis.spi->max_speed_hz = ADIS16400_SPI_SLOW;
312 else
313 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
314 st->adis.spi->mode = SPI_MODE_3;
315 spi_setup(st->adis.spi);
317 ret = adis_initial_startup(&st->adis);
318 if (ret)
319 return ret;
321 if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
322 ret = adis_read_reg_16(&st->adis,
323 ADIS16400_PRODUCT_ID, &prod_id);
324 if (ret)
325 goto err_ret;
327 sscanf(indio_dev->name, "adis%u\n", &device_id);
329 if (prod_id != device_id)
330 dev_warn(&indio_dev->dev, "Device ID(%u) and product ID(%u) do not match.",
331 device_id, prod_id);
333 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
334 indio_dev->name, prod_id,
335 st->adis.spi->chip_select, st->adis.spi->irq);
337 /* use high spi speed if possible */
338 if (st->variant->flags & ADIS16400_HAS_SLOW_MODE) {
339 ret = adis_read_reg_16(&st->adis, ADIS16400_SMPL_PRD, &smp_prd);
340 if (ret)
341 goto err_ret;
343 if ((smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
344 st->adis.spi->max_speed_hz = ADIS16400_SPI_FAST;
345 spi_setup(st->adis.spi);
349 err_ret:
350 return ret;
353 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
354 adis16400_read_frequency,
355 adis16400_write_frequency);
357 static const uint8_t adis16400_addresses[] = {
358 [ADIS16400_SCAN_GYRO_X] = ADIS16400_XGYRO_OFF,
359 [ADIS16400_SCAN_GYRO_Y] = ADIS16400_YGYRO_OFF,
360 [ADIS16400_SCAN_GYRO_Z] = ADIS16400_ZGYRO_OFF,
361 [ADIS16400_SCAN_ACC_X] = ADIS16400_XACCL_OFF,
362 [ADIS16400_SCAN_ACC_Y] = ADIS16400_YACCL_OFF,
363 [ADIS16400_SCAN_ACC_Z] = ADIS16400_ZACCL_OFF,
366 static int adis16400_write_raw(struct iio_dev *indio_dev,
367 struct iio_chan_spec const *chan, int val, int val2, long info)
369 struct adis16400_state *st = iio_priv(indio_dev);
370 int ret, sps;
372 switch (info) {
373 case IIO_CHAN_INFO_CALIBBIAS:
374 mutex_lock(&indio_dev->mlock);
375 ret = adis_write_reg_16(&st->adis,
376 adis16400_addresses[chan->scan_index], val);
377 mutex_unlock(&indio_dev->mlock);
378 return ret;
379 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
381 * Need to cache values so we can update if the frequency
382 * changes.
384 mutex_lock(&indio_dev->mlock);
385 st->filt_int = val;
386 /* Work out update to current value */
387 sps = st->variant->get_freq(st);
388 if (sps < 0) {
389 mutex_unlock(&indio_dev->mlock);
390 return sps;
393 ret = adis16400_set_filter(indio_dev, sps,
394 val * 1000 + val2 / 1000);
395 mutex_unlock(&indio_dev->mlock);
396 return ret;
397 default:
398 return -EINVAL;
402 static int adis16400_read_raw(struct iio_dev *indio_dev,
403 struct iio_chan_spec const *chan, int *val, int *val2, long info)
405 struct adis16400_state *st = iio_priv(indio_dev);
406 int16_t val16;
407 int ret;
409 switch (info) {
410 case IIO_CHAN_INFO_RAW:
411 return adis_single_conversion(indio_dev, chan, 0, val);
412 case IIO_CHAN_INFO_SCALE:
413 switch (chan->type) {
414 case IIO_ANGL_VEL:
415 *val = 0;
416 *val2 = st->variant->gyro_scale_micro;
417 return IIO_VAL_INT_PLUS_MICRO;
418 case IIO_VOLTAGE:
419 *val = 0;
420 if (chan->channel == 0) {
421 *val = 2;
422 *val2 = 418000; /* 2.418 mV */
423 } else {
424 *val = 0;
425 *val2 = 805800; /* 805.8 uV */
427 return IIO_VAL_INT_PLUS_MICRO;
428 case IIO_ACCEL:
429 *val = 0;
430 *val2 = st->variant->accel_scale_micro;
431 return IIO_VAL_INT_PLUS_MICRO;
432 case IIO_MAGN:
433 *val = 0;
434 *val2 = 500; /* 0.5 mgauss */
435 return IIO_VAL_INT_PLUS_MICRO;
436 case IIO_TEMP:
437 *val = st->variant->temp_scale_nano / 1000000;
438 *val2 = (st->variant->temp_scale_nano % 1000000);
439 return IIO_VAL_INT_PLUS_MICRO;
440 default:
441 return -EINVAL;
443 case IIO_CHAN_INFO_CALIBBIAS:
444 mutex_lock(&indio_dev->mlock);
445 ret = adis_read_reg_16(&st->adis,
446 adis16400_addresses[chan->scan_index], &val16);
447 mutex_unlock(&indio_dev->mlock);
448 if (ret)
449 return ret;
450 val16 = ((val16 & 0xFFF) << 4) >> 4;
451 *val = val16;
452 return IIO_VAL_INT;
453 case IIO_CHAN_INFO_OFFSET:
454 /* currently only temperature */
455 *val = st->variant->temp_offset;
456 return IIO_VAL_INT;
457 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
458 mutex_lock(&indio_dev->mlock);
459 /* Need both the number of taps and the sampling frequency */
460 ret = adis_read_reg_16(&st->adis,
461 ADIS16400_SENS_AVG,
462 &val16);
463 if (ret < 0) {
464 mutex_unlock(&indio_dev->mlock);
465 return ret;
467 ret = st->variant->get_freq(st);
468 if (ret >= 0) {
469 ret /= adis16400_3db_divisors[val16 & 0x07];
470 *val = ret / 1000;
471 *val2 = (ret % 1000) * 1000;
473 mutex_unlock(&indio_dev->mlock);
474 if (ret < 0)
475 return ret;
476 return IIO_VAL_INT_PLUS_MICRO;
477 default:
478 return -EINVAL;
482 #define ADIS16400_VOLTAGE_CHAN(addr, bits, name, si) { \
483 .type = IIO_VOLTAGE, \
484 .indexed = 1, \
485 .channel = 0, \
486 .extend_name = name, \
487 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
488 BIT(IIO_CHAN_INFO_SCALE), \
489 .address = (addr), \
490 .scan_index = (si), \
491 .scan_type = { \
492 .sign = 'u', \
493 .realbits = (bits), \
494 .storagebits = 16, \
495 .shift = 0, \
496 .endianness = IIO_BE, \
497 }, \
500 #define ADIS16400_SUPPLY_CHAN(addr, bits) \
501 ADIS16400_VOLTAGE_CHAN(addr, bits, "supply", ADIS16400_SCAN_SUPPLY)
503 #define ADIS16400_AUX_ADC_CHAN(addr, bits) \
504 ADIS16400_VOLTAGE_CHAN(addr, bits, NULL, ADIS16400_SCAN_ADC)
506 #define ADIS16400_GYRO_CHAN(mod, addr, bits) { \
507 .type = IIO_ANGL_VEL, \
508 .modified = 1, \
509 .channel2 = IIO_MOD_ ## mod, \
510 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
511 BIT(IIO_CHAN_INFO_CALIBBIAS), \
512 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
513 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
514 .address = addr, \
515 .scan_index = ADIS16400_SCAN_GYRO_ ## mod, \
516 .scan_type = { \
517 .sign = 's', \
518 .realbits = (bits), \
519 .storagebits = 16, \
520 .shift = 0, \
521 .endianness = IIO_BE, \
522 }, \
525 #define ADIS16400_ACCEL_CHAN(mod, addr, bits) { \
526 .type = IIO_ACCEL, \
527 .modified = 1, \
528 .channel2 = IIO_MOD_ ## mod, \
529 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
530 BIT(IIO_CHAN_INFO_CALIBBIAS), \
531 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
532 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
533 .address = (addr), \
534 .scan_index = ADIS16400_SCAN_ACC_ ## mod, \
535 .scan_type = { \
536 .sign = 's', \
537 .realbits = (bits), \
538 .storagebits = 16, \
539 .shift = 0, \
540 .endianness = IIO_BE, \
541 }, \
544 #define ADIS16400_MAGN_CHAN(mod, addr, bits) { \
545 .type = IIO_MAGN, \
546 .modified = 1, \
547 .channel2 = IIO_MOD_ ## mod, \
548 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
549 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
550 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
551 .address = (addr), \
552 .scan_index = ADIS16400_SCAN_MAGN_ ## mod, \
553 .scan_type = { \
554 .sign = 's', \
555 .realbits = (bits), \
556 .storagebits = 16, \
557 .shift = 0, \
558 .endianness = IIO_BE, \
559 }, \
562 #define ADIS16400_MOD_TEMP_NAME_X "x"
563 #define ADIS16400_MOD_TEMP_NAME_Y "y"
564 #define ADIS16400_MOD_TEMP_NAME_Z "z"
566 #define ADIS16400_MOD_TEMP_CHAN(mod, addr, bits) { \
567 .type = IIO_TEMP, \
568 .indexed = 1, \
569 .channel = 0, \
570 .extend_name = ADIS16400_MOD_TEMP_NAME_ ## mod, \
571 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
572 BIT(IIO_CHAN_INFO_OFFSET) | \
573 BIT(IIO_CHAN_INFO_SCALE), \
574 .info_mask_shared_by_type = \
575 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
576 .address = (addr), \
577 .scan_index = ADIS16350_SCAN_TEMP_ ## mod, \
578 .scan_type = { \
579 .sign = 's', \
580 .realbits = (bits), \
581 .storagebits = 16, \
582 .shift = 0, \
583 .endianness = IIO_BE, \
584 }, \
587 #define ADIS16400_TEMP_CHAN(addr, bits) { \
588 .type = IIO_TEMP, \
589 .indexed = 1, \
590 .channel = 0, \
591 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
592 BIT(IIO_CHAN_INFO_OFFSET) | \
593 BIT(IIO_CHAN_INFO_SCALE), \
594 .address = (addr), \
595 .scan_index = ADIS16350_SCAN_TEMP_X, \
596 .scan_type = { \
597 .sign = 's', \
598 .realbits = (bits), \
599 .storagebits = 16, \
600 .shift = 0, \
601 .endianness = IIO_BE, \
602 }, \
605 #define ADIS16400_INCLI_CHAN(mod, addr, bits) { \
606 .type = IIO_INCLI, \
607 .modified = 1, \
608 .channel2 = IIO_MOD_ ## mod, \
609 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
610 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
611 .address = (addr), \
612 .scan_index = ADIS16300_SCAN_INCLI_ ## mod, \
613 .scan_type = { \
614 .sign = 's', \
615 .realbits = (bits), \
616 .storagebits = 16, \
617 .shift = 0, \
618 .endianness = IIO_BE, \
619 }, \
622 static const struct iio_chan_spec adis16400_channels[] = {
623 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 14),
624 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
625 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
626 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
627 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
628 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
629 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
630 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
631 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
632 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
633 ADIS16400_TEMP_CHAN(ADIS16400_TEMP_OUT, 12),
634 ADIS16400_AUX_ADC_CHAN(ADIS16400_AUX_ADC, 12),
635 IIO_CHAN_SOFT_TIMESTAMP(12)
638 static const struct iio_chan_spec adis16448_channels[] = {
639 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 16),
640 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 16),
641 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 16),
642 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 16),
643 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 16),
644 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 16),
645 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 16),
646 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 16),
647 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 16),
649 .type = IIO_PRESSURE,
650 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
651 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
652 .address = ADIS16448_BARO_OUT,
653 .scan_index = ADIS16400_SCAN_BARO,
654 .scan_type = {
655 .sign = 's',
656 .realbits = 16,
657 .storagebits = 16,
658 .endianness = IIO_BE,
661 ADIS16400_TEMP_CHAN(ADIS16448_TEMP_OUT, 12),
662 IIO_CHAN_SOFT_TIMESTAMP(11)
665 static const struct iio_chan_spec adis16350_channels[] = {
666 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
667 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
668 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
669 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
670 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
671 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
672 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
673 ADIS16400_MAGN_CHAN(X, ADIS16400_XMAGN_OUT, 14),
674 ADIS16400_MAGN_CHAN(Y, ADIS16400_YMAGN_OUT, 14),
675 ADIS16400_MAGN_CHAN(Z, ADIS16400_ZMAGN_OUT, 14),
676 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
677 ADIS16400_MOD_TEMP_CHAN(X, ADIS16350_XTEMP_OUT, 12),
678 ADIS16400_MOD_TEMP_CHAN(Y, ADIS16350_YTEMP_OUT, 12),
679 ADIS16400_MOD_TEMP_CHAN(Z, ADIS16350_ZTEMP_OUT, 12),
680 IIO_CHAN_SOFT_TIMESTAMP(11)
683 static const struct iio_chan_spec adis16300_channels[] = {
684 ADIS16400_SUPPLY_CHAN(ADIS16400_SUPPLY_OUT, 12),
685 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
686 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
687 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
688 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
689 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
690 ADIS16400_AUX_ADC_CHAN(ADIS16300_AUX_ADC, 12),
691 ADIS16400_INCLI_CHAN(X, ADIS16300_PITCH_OUT, 13),
692 ADIS16400_INCLI_CHAN(Y, ADIS16300_ROLL_OUT, 13),
693 IIO_CHAN_SOFT_TIMESTAMP(14)
696 static const struct iio_chan_spec adis16334_channels[] = {
697 ADIS16400_GYRO_CHAN(X, ADIS16400_XGYRO_OUT, 14),
698 ADIS16400_GYRO_CHAN(Y, ADIS16400_YGYRO_OUT, 14),
699 ADIS16400_GYRO_CHAN(Z, ADIS16400_ZGYRO_OUT, 14),
700 ADIS16400_ACCEL_CHAN(X, ADIS16400_XACCL_OUT, 14),
701 ADIS16400_ACCEL_CHAN(Y, ADIS16400_YACCL_OUT, 14),
702 ADIS16400_ACCEL_CHAN(Z, ADIS16400_ZACCL_OUT, 14),
703 ADIS16400_TEMP_CHAN(ADIS16350_XTEMP_OUT, 12),
704 IIO_CHAN_SOFT_TIMESTAMP(8)
707 static struct attribute *adis16400_attributes[] = {
708 &iio_dev_attr_sampling_frequency.dev_attr.attr,
709 NULL
712 static const struct attribute_group adis16400_attribute_group = {
713 .attrs = adis16400_attributes,
716 static struct adis16400_chip_info adis16400_chips[] = {
717 [ADIS16300] = {
718 .channels = adis16300_channels,
719 .num_channels = ARRAY_SIZE(adis16300_channels),
720 .flags = ADIS16400_HAS_SLOW_MODE,
721 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
722 .accel_scale_micro = 5884,
723 .temp_scale_nano = 140000000, /* 0.14 C */
724 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
725 .set_freq = adis16400_set_freq,
726 .get_freq = adis16400_get_freq,
728 [ADIS16334] = {
729 .channels = adis16334_channels,
730 .num_channels = ARRAY_SIZE(adis16334_channels),
731 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_NO_BURST |
732 ADIS16400_HAS_SERIAL_NUMBER,
733 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
734 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
735 .temp_scale_nano = 67850000, /* 0.06785 C */
736 .temp_offset = 25000000 / 67850, /* 25 C = 0x00 */
737 .set_freq = adis16334_set_freq,
738 .get_freq = adis16334_get_freq,
740 [ADIS16350] = {
741 .channels = adis16350_channels,
742 .num_channels = ARRAY_SIZE(adis16350_channels),
743 .gyro_scale_micro = IIO_DEGREE_TO_RAD(73260), /* 0.07326 deg/s */
744 .accel_scale_micro = IIO_G_TO_M_S_2(2522), /* 0.002522 g */
745 .temp_scale_nano = 145300000, /* 0.1453 C */
746 .temp_offset = 25000000 / 145300, /* 25 C = 0x00 */
747 .flags = ADIS16400_NO_BURST | ADIS16400_HAS_SLOW_MODE,
748 .set_freq = adis16400_set_freq,
749 .get_freq = adis16400_get_freq,
751 [ADIS16360] = {
752 .channels = adis16350_channels,
753 .num_channels = ARRAY_SIZE(adis16350_channels),
754 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
755 ADIS16400_HAS_SERIAL_NUMBER,
756 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
757 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
758 .temp_scale_nano = 136000000, /* 0.136 C */
759 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
760 .set_freq = adis16400_set_freq,
761 .get_freq = adis16400_get_freq,
763 [ADIS16362] = {
764 .channels = adis16350_channels,
765 .num_channels = ARRAY_SIZE(adis16350_channels),
766 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
767 ADIS16400_HAS_SERIAL_NUMBER,
768 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
769 .accel_scale_micro = IIO_G_TO_M_S_2(333), /* 0.333 mg */
770 .temp_scale_nano = 136000000, /* 0.136 C */
771 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
772 .set_freq = adis16400_set_freq,
773 .get_freq = adis16400_get_freq,
775 [ADIS16364] = {
776 .channels = adis16350_channels,
777 .num_channels = ARRAY_SIZE(adis16350_channels),
778 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE |
779 ADIS16400_HAS_SERIAL_NUMBER,
780 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
781 .accel_scale_micro = IIO_G_TO_M_S_2(1000), /* 1 mg */
782 .temp_scale_nano = 136000000, /* 0.136 C */
783 .temp_offset = 25000000 / 136000, /* 25 C = 0x00 */
784 .set_freq = adis16400_set_freq,
785 .get_freq = adis16400_get_freq,
787 [ADIS16400] = {
788 .channels = adis16400_channels,
789 .num_channels = ARRAY_SIZE(adis16400_channels),
790 .flags = ADIS16400_HAS_PROD_ID | ADIS16400_HAS_SLOW_MODE,
791 .gyro_scale_micro = IIO_DEGREE_TO_RAD(50000), /* 0.05 deg/s */
792 .accel_scale_micro = IIO_G_TO_M_S_2(3333), /* 3.333 mg */
793 .temp_scale_nano = 140000000, /* 0.14 C */
794 .temp_offset = 25000000 / 140000, /* 25 C = 0x00 */
795 .set_freq = adis16400_set_freq,
796 .get_freq = adis16400_get_freq,
798 [ADIS16448] = {
799 .channels = adis16448_channels,
800 .num_channels = ARRAY_SIZE(adis16448_channels),
801 .flags = ADIS16400_HAS_PROD_ID |
802 ADIS16400_HAS_SERIAL_NUMBER,
803 .gyro_scale_micro = IIO_DEGREE_TO_RAD(10000), /* 0.01 deg/s */
804 .accel_scale_micro = IIO_G_TO_M_S_2(833), /* 1/1200 g */
805 .temp_scale_nano = 73860000, /* 0.07386 C */
806 .temp_offset = 31000000 / 73860, /* 31 C = 0x00 */
807 .set_freq = adis16334_set_freq,
808 .get_freq = adis16334_get_freq,
812 static const struct iio_info adis16400_info = {
813 .driver_module = THIS_MODULE,
814 .read_raw = &adis16400_read_raw,
815 .write_raw = &adis16400_write_raw,
816 .attrs = &adis16400_attribute_group,
817 .update_scan_mode = adis16400_update_scan_mode,
818 .debugfs_reg_access = adis_debugfs_reg_access,
821 static const unsigned long adis16400_burst_scan_mask[] = {
822 ~0UL,
826 static const char * const adis16400_status_error_msgs[] = {
827 [ADIS16400_DIAG_STAT_ZACCL_FAIL] = "Z-axis accelerometer self-test failure",
828 [ADIS16400_DIAG_STAT_YACCL_FAIL] = "Y-axis accelerometer self-test failure",
829 [ADIS16400_DIAG_STAT_XACCL_FAIL] = "X-axis accelerometer self-test failure",
830 [ADIS16400_DIAG_STAT_XGYRO_FAIL] = "X-axis gyroscope self-test failure",
831 [ADIS16400_DIAG_STAT_YGYRO_FAIL] = "Y-axis gyroscope self-test failure",
832 [ADIS16400_DIAG_STAT_ZGYRO_FAIL] = "Z-axis gyroscope self-test failure",
833 [ADIS16400_DIAG_STAT_ALARM2] = "Alarm 2 active",
834 [ADIS16400_DIAG_STAT_ALARM1] = "Alarm 1 active",
835 [ADIS16400_DIAG_STAT_FLASH_CHK] = "Flash checksum error",
836 [ADIS16400_DIAG_STAT_SELF_TEST] = "Self test error",
837 [ADIS16400_DIAG_STAT_OVERFLOW] = "Sensor overrange",
838 [ADIS16400_DIAG_STAT_SPI_FAIL] = "SPI failure",
839 [ADIS16400_DIAG_STAT_FLASH_UPT] = "Flash update failed",
840 [ADIS16400_DIAG_STAT_POWER_HIGH] = "Power supply above 5.25V",
841 [ADIS16400_DIAG_STAT_POWER_LOW] = "Power supply below 4.75V",
844 static const struct adis_data adis16400_data = {
845 .msc_ctrl_reg = ADIS16400_MSC_CTRL,
846 .glob_cmd_reg = ADIS16400_GLOB_CMD,
847 .diag_stat_reg = ADIS16400_DIAG_STAT,
849 .read_delay = 50,
850 .write_delay = 50,
852 .self_test_mask = ADIS16400_MSC_CTRL_MEM_TEST,
853 .startup_delay = ADIS16400_STARTUP_DELAY,
855 .status_error_msgs = adis16400_status_error_msgs,
856 .status_error_mask = BIT(ADIS16400_DIAG_STAT_ZACCL_FAIL) |
857 BIT(ADIS16400_DIAG_STAT_YACCL_FAIL) |
858 BIT(ADIS16400_DIAG_STAT_XACCL_FAIL) |
859 BIT(ADIS16400_DIAG_STAT_XGYRO_FAIL) |
860 BIT(ADIS16400_DIAG_STAT_YGYRO_FAIL) |
861 BIT(ADIS16400_DIAG_STAT_ZGYRO_FAIL) |
862 BIT(ADIS16400_DIAG_STAT_ALARM2) |
863 BIT(ADIS16400_DIAG_STAT_ALARM1) |
864 BIT(ADIS16400_DIAG_STAT_FLASH_CHK) |
865 BIT(ADIS16400_DIAG_STAT_SELF_TEST) |
866 BIT(ADIS16400_DIAG_STAT_OVERFLOW) |
867 BIT(ADIS16400_DIAG_STAT_SPI_FAIL) |
868 BIT(ADIS16400_DIAG_STAT_FLASH_UPT) |
869 BIT(ADIS16400_DIAG_STAT_POWER_HIGH) |
870 BIT(ADIS16400_DIAG_STAT_POWER_LOW),
873 static int adis16400_probe(struct spi_device *spi)
875 struct adis16400_state *st;
876 struct iio_dev *indio_dev;
877 int ret;
879 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
880 if (indio_dev == NULL)
881 return -ENOMEM;
883 st = iio_priv(indio_dev);
884 /* this is only used for removal purposes */
885 spi_set_drvdata(spi, indio_dev);
887 /* setup the industrialio driver allocated elements */
888 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
889 indio_dev->dev.parent = &spi->dev;
890 indio_dev->name = spi_get_device_id(spi)->name;
891 indio_dev->channels = st->variant->channels;
892 indio_dev->num_channels = st->variant->num_channels;
893 indio_dev->info = &adis16400_info;
894 indio_dev->modes = INDIO_DIRECT_MODE;
896 if (!(st->variant->flags & ADIS16400_NO_BURST))
897 indio_dev->available_scan_masks = adis16400_burst_scan_mask;
899 ret = adis_init(&st->adis, indio_dev, spi, &adis16400_data);
900 if (ret)
901 return ret;
903 ret = adis_setup_buffer_and_trigger(&st->adis, indio_dev,
904 adis16400_trigger_handler);
905 if (ret)
906 return ret;
908 /* Get the device into a sane initial state */
909 ret = adis16400_initial_setup(indio_dev);
910 if (ret)
911 goto error_cleanup_buffer;
912 ret = iio_device_register(indio_dev);
913 if (ret)
914 goto error_cleanup_buffer;
916 adis16400_debugfs_init(indio_dev);
917 return 0;
919 error_cleanup_buffer:
920 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
921 return ret;
924 static int adis16400_remove(struct spi_device *spi)
926 struct iio_dev *indio_dev = spi_get_drvdata(spi);
927 struct adis16400_state *st = iio_priv(indio_dev);
929 iio_device_unregister(indio_dev);
930 adis16400_stop_device(indio_dev);
932 adis_cleanup_buffer_and_trigger(&st->adis, indio_dev);
934 return 0;
937 static const struct spi_device_id adis16400_id[] = {
938 {"adis16300", ADIS16300},
939 {"adis16334", ADIS16334},
940 {"adis16350", ADIS16350},
941 {"adis16354", ADIS16350},
942 {"adis16355", ADIS16350},
943 {"adis16360", ADIS16360},
944 {"adis16362", ADIS16362},
945 {"adis16364", ADIS16364},
946 {"adis16365", ADIS16360},
947 {"adis16400", ADIS16400},
948 {"adis16405", ADIS16400},
949 {"adis16448", ADIS16448},
952 MODULE_DEVICE_TABLE(spi, adis16400_id);
954 static struct spi_driver adis16400_driver = {
955 .driver = {
956 .name = "adis16400",
957 .owner = THIS_MODULE,
959 .id_table = adis16400_id,
960 .probe = adis16400_probe,
961 .remove = adis16400_remove,
963 module_spi_driver(adis16400_driver);
965 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
966 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
967 MODULE_LICENSE("GPL v2");