2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
5 * 3d Magnetometers via SPI
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk>
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>
31 #include "../buffer_generic.h"
32 #include "adis16400.h"
34 enum adis16400_chip_variant {
46 * adis16400_spi_write_reg_8() - write single byte to a register
47 * @dev: device associated with child of actual device (iio_dev or iio_trig)
48 * @reg_address: the address of the register to be written
49 * @val: the value to write
51 static int adis16400_spi_write_reg_8(struct iio_dev *indio_dev,
56 struct adis16400_state *st = iio_priv(indio_dev);
58 mutex_lock(&st->buf_lock);
59 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
62 ret = spi_write(st->us, st->tx, 2);
63 mutex_unlock(&st->buf_lock);
69 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
70 * @dev: device associated with child of actual device (iio_dev or iio_trig)
71 * @reg_address: the address of the lower of the two registers. Second register
72 * is assumed to have address one greater.
73 * @val: value to be written
75 * At the moment the spi framework doesn't allow global setting of cs_change.
76 * This means that use cannot be made of spi_write.
78 static int adis16400_spi_write_reg_16(struct iio_dev *indio_dev,
83 struct spi_message msg;
84 struct adis16400_state *st = iio_priv(indio_dev);
85 struct spi_transfer xfers[] = {
98 mutex_lock(&st->buf_lock);
99 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
100 st->tx[1] = value & 0xFF;
101 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
102 st->tx[3] = (value >> 8) & 0xFF;
104 spi_message_init(&msg);
105 spi_message_add_tail(&xfers[0], &msg);
106 spi_message_add_tail(&xfers[1], &msg);
107 ret = spi_sync(st->us, &msg);
108 mutex_unlock(&st->buf_lock);
114 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
115 * @indio_dev: iio device
116 * @reg_address: the address of the lower of the two registers. Second register
117 * is assumed to have address one greater.
118 * @val: somewhere to pass back the value read
120 * At the moment the spi framework doesn't allow global setting of cs_change.
121 * This means that use cannot be made of spi_read.
123 static int adis16400_spi_read_reg_16(struct iio_dev *indio_dev,
124 u8 lower_reg_address,
127 struct spi_message msg;
128 struct adis16400_state *st = iio_priv(indio_dev);
130 struct spi_transfer xfers[] = {
143 mutex_lock(&st->buf_lock);
144 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
147 spi_message_init(&msg);
148 spi_message_add_tail(&xfers[0], &msg);
149 spi_message_add_tail(&xfers[1], &msg);
150 ret = spi_sync(st->us, &msg);
152 dev_err(&st->us->dev,
153 "problem when reading 16 bit register 0x%02X",
157 *val = (st->rx[0] << 8) | st->rx[1];
160 mutex_unlock(&st->buf_lock);
164 static ssize_t adis16400_read_frequency(struct device *dev,
165 struct device_attribute *attr,
168 struct iio_dev *indio_dev = dev_get_drvdata(dev);
172 ret = adis16400_spi_read_reg_16(indio_dev,
177 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
178 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
179 len = sprintf(buf, "%d SPS\n", sps);
183 static ssize_t adis16400_write_frequency(struct device *dev,
184 struct device_attribute *attr,
188 struct iio_dev *indio_dev = dev_get_drvdata(dev);
189 struct adis16400_state *st = iio_priv(indio_dev);
194 ret = strict_strtol(buf, 10, &val);
198 mutex_lock(&indio_dev->mlock);
203 t &= ADIS16400_SMPL_PRD_DIV_MASK;
204 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
205 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
207 st->us->max_speed_hz = ADIS16400_SPI_FAST;
209 ret = adis16400_spi_write_reg_8(indio_dev,
213 mutex_unlock(&indio_dev->mlock);
215 return ret ? ret : len;
218 static int adis16400_reset(struct iio_dev *indio_dev)
221 ret = adis16400_spi_write_reg_8(indio_dev,
223 ADIS16400_GLOB_CMD_SW_RESET);
225 dev_err(&indio_dev->dev, "problem resetting device");
230 static ssize_t adis16400_write_reset(struct device *dev,
231 struct device_attribute *attr,
232 const char *buf, size_t len)
237 ret = strtobool(buf, &val);
241 ret = adis16400_reset(dev_get_drvdata(dev));
249 int adis16400_set_irq(struct iio_dev *indio_dev, bool enable)
254 ret = adis16400_spi_read_reg_16(indio_dev, ADIS16400_MSC_CTRL, &msc);
258 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
260 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
262 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
264 ret = adis16400_spi_write_reg_16(indio_dev, ADIS16400_MSC_CTRL, msc);
272 /* Power down the device */
273 static int adis16400_stop_device(struct iio_dev *indio_dev)
276 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
278 ret = adis16400_spi_write_reg_16(indio_dev, ADIS16400_SLP_CNT, val);
280 dev_err(&indio_dev->dev,
281 "problem with turning device off: SLP_CNT");
286 static int adis16400_check_status(struct iio_dev *indio_dev)
290 struct device *dev = &indio_dev->dev;
292 ret = adis16400_spi_read_reg_16(indio_dev,
293 ADIS16400_DIAG_STAT, &status);
296 dev_err(dev, "Reading status failed\n");
300 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
301 dev_err(dev, "Z-axis accelerometer self-test failure\n");
302 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
303 dev_err(dev, "Y-axis accelerometer self-test failure\n");
304 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
305 dev_err(dev, "X-axis accelerometer self-test failure\n");
306 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
307 dev_err(dev, "X-axis gyroscope self-test failure\n");
308 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
309 dev_err(dev, "Y-axis gyroscope self-test failure\n");
310 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
311 dev_err(dev, "Z-axis gyroscope self-test failure\n");
312 if (status & ADIS16400_DIAG_STAT_ALARM2)
313 dev_err(dev, "Alarm 2 active\n");
314 if (status & ADIS16400_DIAG_STAT_ALARM1)
315 dev_err(dev, "Alarm 1 active\n");
316 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
317 dev_err(dev, "Flash checksum error\n");
318 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
319 dev_err(dev, "Self test error\n");
320 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
321 dev_err(dev, "Sensor overrange\n");
322 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
323 dev_err(dev, "SPI failure\n");
324 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
325 dev_err(dev, "Flash update failed\n");
326 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
327 dev_err(dev, "Power supply above 5.25V\n");
328 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
329 dev_err(dev, "Power supply below 4.75V\n");
335 static int adis16400_self_test(struct iio_dev *indio_dev)
338 ret = adis16400_spi_write_reg_16(indio_dev,
340 ADIS16400_MSC_CTRL_MEM_TEST);
342 dev_err(&indio_dev->dev, "problem starting self test");
346 msleep(ADIS16400_MTEST_DELAY);
347 adis16400_check_status(indio_dev);
353 static int adis16400_initial_setup(struct iio_dev *indio_dev)
356 u16 prod_id, smp_prd;
357 struct adis16400_state *st = iio_priv(indio_dev);
359 /* use low spi speed for init */
360 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
361 st->us->mode = SPI_MODE_3;
364 ret = adis16400_set_irq(indio_dev, false);
366 dev_err(&indio_dev->dev, "disable irq failed");
370 ret = adis16400_self_test(indio_dev);
372 dev_err(&indio_dev->dev, "self test failure");
376 ret = adis16400_check_status(indio_dev);
378 adis16400_reset(indio_dev);
379 dev_err(&indio_dev->dev, "device not playing ball -> reset");
380 msleep(ADIS16400_STARTUP_DELAY);
381 ret = adis16400_check_status(indio_dev);
383 dev_err(&indio_dev->dev, "giving up");
387 if (st->variant->flags & ADIS16400_HAS_PROD_ID) {
388 ret = adis16400_spi_read_reg_16(indio_dev,
389 ADIS16400_PRODUCT_ID, &prod_id);
393 if ((prod_id & 0xF000) != st->variant->product_id)
394 dev_warn(&indio_dev->dev, "incorrect id");
396 dev_info(&indio_dev->dev, "%s: prod_id 0x%04x at CS%d (irq %d)\n",
397 indio_dev->name, prod_id,
398 st->us->chip_select, st->us->irq);
400 /* use high spi speed if possible */
401 ret = adis16400_spi_read_reg_16(indio_dev,
402 ADIS16400_SMPL_PRD, &smp_prd);
403 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
404 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
412 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
413 adis16400_read_frequency,
414 adis16400_write_frequency);
416 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
418 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
420 enum adis16400_chan {
438 static u8 adis16400_addresses[17][2] = {
439 [in_supply] = { ADIS16400_SUPPLY_OUT },
440 [gyro_x] = { ADIS16400_XGYRO_OUT, ADIS16400_XGYRO_OFF },
441 [gyro_y] = { ADIS16400_YGYRO_OUT, ADIS16400_YGYRO_OFF },
442 [gyro_z] = { ADIS16400_ZGYRO_OUT, ADIS16400_ZGYRO_OFF },
443 [accel_x] = { ADIS16400_XACCL_OUT, ADIS16400_XACCL_OFF },
444 [accel_y] = { ADIS16400_YACCL_OUT, ADIS16400_YACCL_OFF },
445 [accel_z] = { ADIS16400_ZACCL_OUT, ADIS16400_ZACCL_OFF },
446 [magn_x] = { ADIS16400_XMAGN_OUT },
447 [magn_y] = { ADIS16400_YMAGN_OUT },
448 [magn_z] = { ADIS16400_ZMAGN_OUT },
449 [temp] = { ADIS16400_TEMP_OUT },
450 [temp0] = { ADIS16350_XTEMP_OUT },
451 [temp1] = { ADIS16350_YTEMP_OUT },
452 [temp2] = { ADIS16350_ZTEMP_OUT },
453 [in1] = { ADIS16400_AUX_ADC },
454 [incli_x] = { ADIS16300_PITCH_OUT },
455 [incli_y] = { ADIS16300_ROLL_OUT }
458 static int adis16400_write_raw(struct iio_dev *indio_dev,
459 struct iio_chan_spec const *chan,
467 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
468 mutex_lock(&indio_dev->mlock);
469 ret = adis16400_spi_write_reg_16(indio_dev,
470 adis16400_addresses[chan->address][1],
472 mutex_unlock(&indio_dev->mlock);
479 static int adis16400_read_raw(struct iio_dev *indio_dev,
480 struct iio_chan_spec const *chan,
485 struct adis16400_state *st = iio_priv(indio_dev);
491 mutex_lock(&indio_dev->mlock);
492 ret = adis16400_spi_read_reg_16(indio_dev,
493 adis16400_addresses[chan->address][0],
496 mutex_unlock(&indio_dev->mlock);
499 val16 &= (1 << chan->scan_type.realbits) - 1;
500 if (chan->scan_type.sign == 's') {
501 shift = 16 - chan->scan_type.realbits;
502 val16 = (s16)(val16 << shift) >> shift;
505 mutex_unlock(&indio_dev->mlock);
507 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
508 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
509 switch (chan->type) {
512 *val2 = st->variant->gyro_scale_micro;
513 return IIO_VAL_INT_PLUS_MICRO;
516 if (chan->channel == 0)
520 return IIO_VAL_INT_PLUS_MICRO;
523 *val2 = st->variant->accel_scale_micro;
524 return IIO_VAL_INT_PLUS_MICRO;
528 return IIO_VAL_INT_PLUS_MICRO;
532 return IIO_VAL_INT_PLUS_MICRO;
536 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
537 mutex_lock(&indio_dev->mlock);
538 ret = adis16400_spi_read_reg_16(indio_dev,
539 adis16400_addresses[chan->address][1],
541 mutex_unlock(&indio_dev->mlock);
544 val16 = ((val16 & 0xFFF) << 4) >> 4;
547 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
548 /* currently only temperature */
551 return IIO_VAL_INT_PLUS_MICRO;
557 static struct iio_chan_spec adis16400_channels[] = {
562 .extend_name = "supply",
563 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
564 .address = in_supply,
565 .scan_index = ADIS16400_SCAN_SUPPLY,
566 .scan_type = IIO_ST('u', 14, 16, 0)
568 .type = IIO_ANGL_VEL,
570 .channel2 = IIO_MOD_X,
571 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
572 (1 << IIO_CHAN_INFO_SCALE_SHARED),
574 .scan_index = ADIS16400_SCAN_GYRO_X,
575 .scan_type = IIO_ST('s', 14, 16, 0)
577 .type = IIO_ANGL_VEL,
579 .channel2 = IIO_MOD_Y,
580 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
581 (1 << IIO_CHAN_INFO_SCALE_SHARED),
583 .scan_index = ADIS16400_SCAN_GYRO_Y,
584 .scan_type = IIO_ST('s', 14, 16, 0),
586 .type = IIO_ANGL_VEL,
588 .channel2 = IIO_MOD_Z,
589 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
590 (1 << IIO_CHAN_INFO_SCALE_SHARED),
592 .scan_index = ADIS16400_SCAN_GYRO_Z,
593 .scan_type = IIO_ST('s', 14, 16, 0),
597 .channel2 = IIO_MOD_X,
598 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
599 (1 << IIO_CHAN_INFO_SCALE_SHARED),
601 .scan_index = ADIS16400_SCAN_ACC_X,
602 .scan_type = IIO_ST('s', 14, 16, 0),
606 .channel2 = IIO_MOD_Y,
607 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
608 (1 << IIO_CHAN_INFO_SCALE_SHARED),
610 .scan_index = ADIS16400_SCAN_ACC_Y,
611 .scan_type = IIO_ST('s', 14, 16, 0),
615 .channel2 = IIO_MOD_Z,
616 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
617 (1 << IIO_CHAN_INFO_SCALE_SHARED),
619 .scan_index = ADIS16400_SCAN_ACC_Z,
620 .scan_type = IIO_ST('s', 14, 16, 0),
624 .channel2 = IIO_MOD_X,
625 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
627 .scan_index = ADIS16400_SCAN_MAGN_X,
628 .scan_type = IIO_ST('s', 14, 16, 0),
632 .channel2 = IIO_MOD_Y,
633 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
635 .scan_index = ADIS16400_SCAN_MAGN_Y,
636 .scan_type = IIO_ST('s', 14, 16, 0),
640 .channel2 = IIO_MOD_Z,
641 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
643 .scan_index = ADIS16400_SCAN_MAGN_Z,
644 .scan_type = IIO_ST('s', 14, 16, 0),
649 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
650 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
652 .scan_index = ADIS16400_SCAN_TEMP,
653 .scan_type = IIO_ST('s', 12, 16, 0),
658 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
660 .scan_index = ADIS16400_SCAN_ADC_0,
661 .scan_type = IIO_ST('s', 12, 16, 0),
663 IIO_CHAN_SOFT_TIMESTAMP(12)
666 static struct iio_chan_spec adis16350_channels[] = {
671 .extend_name = "supply",
672 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
673 .address = in_supply,
674 .scan_index = ADIS16400_SCAN_SUPPLY,
675 .scan_type = IIO_ST('u', 12, 16, 0)
677 .type = IIO_ANGL_VEL,
679 .channel2 = IIO_MOD_X,
680 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
681 (1 << IIO_CHAN_INFO_SCALE_SHARED),
683 .scan_index = ADIS16400_SCAN_GYRO_X,
684 .scan_type = IIO_ST('s', 14, 16, 0)
686 .type = IIO_ANGL_VEL,
688 .channel2 = IIO_MOD_Y,
689 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
690 (1 << IIO_CHAN_INFO_SCALE_SHARED),
692 .scan_index = ADIS16400_SCAN_GYRO_Y,
693 .scan_type = IIO_ST('s', 14, 16, 0),
695 .type = IIO_ANGL_VEL,
697 .channel2 = IIO_MOD_Z,
698 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
699 (1 << IIO_CHAN_INFO_SCALE_SHARED),
701 .scan_index = ADIS16400_SCAN_GYRO_Z,
702 .scan_type = IIO_ST('s', 14, 16, 0),
706 .channel2 = IIO_MOD_X,
707 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
708 (1 << IIO_CHAN_INFO_SCALE_SHARED),
710 .scan_index = ADIS16400_SCAN_ACC_X,
711 .scan_type = IIO_ST('s', 14, 16, 0),
715 .channel2 = IIO_MOD_Y,
716 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
717 (1 << IIO_CHAN_INFO_SCALE_SHARED),
719 .scan_index = ADIS16400_SCAN_ACC_Y,
720 .scan_type = IIO_ST('s', 14, 16, 0),
724 .channel2 = IIO_MOD_Z,
725 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
726 (1 << IIO_CHAN_INFO_SCALE_SHARED),
728 .scan_index = ADIS16400_SCAN_ACC_Z,
729 .scan_type = IIO_ST('s', 14, 16, 0),
735 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
736 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
738 .scan_index = ADIS16350_SCAN_TEMP_X,
739 .scan_type = IIO_ST('s', 12, 16, 0),
745 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
746 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
748 .scan_index = ADIS16350_SCAN_TEMP_Y,
749 .scan_type = IIO_ST('s', 12, 16, 0),
755 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
756 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
758 .scan_index = ADIS16350_SCAN_TEMP_Z,
759 .scan_type = IIO_ST('s', 12, 16, 0),
764 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
766 .scan_index = ADIS16350_SCAN_ADC_0,
767 .scan_type = IIO_ST('s', 12, 16, 0),
769 IIO_CHAN_SOFT_TIMESTAMP(11)
772 static struct iio_chan_spec adis16300_channels[] = {
777 .extend_name = "supply",
778 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
779 .address = in_supply,
780 .scan_index = ADIS16400_SCAN_SUPPLY,
781 .scan_type = IIO_ST('u', 12, 16, 0)
783 .type = IIO_ANGL_VEL,
785 .channel2 = IIO_MOD_X,
786 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
787 (1 << IIO_CHAN_INFO_SCALE_SHARED),
789 .scan_index = ADIS16400_SCAN_GYRO_X,
790 .scan_type = IIO_ST('s', 14, 16, 0),
794 .channel2 = IIO_MOD_X,
795 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
796 (1 << IIO_CHAN_INFO_SCALE_SHARED),
798 .scan_index = ADIS16400_SCAN_ACC_X,
799 .scan_type = IIO_ST('s', 14, 16, 0),
803 .channel2 = IIO_MOD_Y,
804 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
805 (1 << IIO_CHAN_INFO_SCALE_SHARED),
807 .scan_index = ADIS16400_SCAN_ACC_Y,
808 .scan_type = IIO_ST('s', 14, 16, 0),
812 .channel2 = IIO_MOD_Z,
813 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
814 (1 << IIO_CHAN_INFO_SCALE_SHARED),
816 .scan_index = ADIS16400_SCAN_ACC_Z,
817 .scan_type = IIO_ST('s', 14, 16, 0),
822 .info_mask = (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
823 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
825 .scan_index = ADIS16400_SCAN_TEMP,
826 .scan_type = IIO_ST('s', 12, 16, 0),
831 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
833 .scan_index = ADIS16350_SCAN_ADC_0,
834 .scan_type = IIO_ST('s', 12, 16, 0),
838 .channel2 = IIO_MOD_X,
839 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
841 .scan_index = ADIS16300_SCAN_INCLI_X,
842 .scan_type = IIO_ST('s', 13, 16, 0),
846 .channel2 = IIO_MOD_Y,
847 .info_mask = (1 << IIO_CHAN_INFO_SCALE_SHARED),
849 .scan_index = ADIS16300_SCAN_INCLI_Y,
850 .scan_type = IIO_ST('s', 13, 16, 0),
852 IIO_CHAN_SOFT_TIMESTAMP(14)
855 static const struct iio_chan_spec adis16334_channels[] = {
857 .type = IIO_ANGL_VEL,
859 .channel2 = IIO_MOD_X,
860 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
861 (1 << IIO_CHAN_INFO_SCALE_SHARED),
863 .scan_index = ADIS16400_SCAN_GYRO_X,
864 .scan_type = IIO_ST('s', 14, 16, 0),
866 .type = IIO_ANGL_VEL,
868 .channel2 = IIO_MOD_Y,
869 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
870 (1 << IIO_CHAN_INFO_SCALE_SHARED),
872 .scan_index = ADIS16400_SCAN_GYRO_Y,
873 .scan_type = IIO_ST('s', 14, 16, 0),
875 .type = IIO_ANGL_VEL,
877 .channel2 = IIO_MOD_Z,
878 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
879 (1 << IIO_CHAN_INFO_SCALE_SHARED),
881 .scan_index = ADIS16400_SCAN_GYRO_Z,
882 .scan_type = IIO_ST('s', 14, 16, 0),
886 .channel2 = IIO_MOD_X,
887 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
888 (1 << IIO_CHAN_INFO_SCALE_SHARED),
890 .scan_index = ADIS16400_SCAN_ACC_X,
891 .scan_type = IIO_ST('s', 14, 16, 0),
895 .channel2 = IIO_MOD_Y,
896 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
897 (1 << IIO_CHAN_INFO_SCALE_SHARED),
899 .scan_index = ADIS16400_SCAN_ACC_Y,
900 .scan_type = IIO_ST('s', 14, 16, 0),
904 .channel2 = IIO_MOD_Z,
905 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
906 (1 << IIO_CHAN_INFO_SCALE_SHARED),
908 .scan_index = ADIS16400_SCAN_ACC_Z,
909 .scan_type = IIO_ST('s', 14, 16, 0),
914 .info_mask = (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
915 (1 << IIO_CHAN_INFO_SCALE_SHARED),
917 .scan_index = ADIS16400_SCAN_ACC_Z,
918 .scan_type = IIO_ST('s', 14, 16, 0),
920 IIO_CHAN_SOFT_TIMESTAMP(12)
923 static struct attribute *adis16400_attributes[] = {
924 &iio_dev_attr_sampling_frequency.dev_attr.attr,
925 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
926 &iio_dev_attr_reset.dev_attr.attr,
930 static const struct attribute_group adis16400_attribute_group = {
931 .attrs = adis16400_attributes,
934 static struct adis16400_chip_info adis16400_chips[] = {
936 .channels = adis16300_channels,
937 .num_channels = ARRAY_SIZE(adis16300_channels),
938 .gyro_scale_micro = 873,
939 .accel_scale_micro = 5884,
940 .default_scan_mask = (1 << ADIS16400_SCAN_SUPPLY) |
941 (1 << ADIS16400_SCAN_GYRO_X) | (1 << ADIS16400_SCAN_ACC_X) |
942 (1 << ADIS16400_SCAN_ACC_Y) | (1 << ADIS16400_SCAN_ACC_Z) |
943 (1 << ADIS16400_SCAN_TEMP) | (1 << ADIS16400_SCAN_ADC_0) |
944 (1 << ADIS16300_SCAN_INCLI_X) | (1 << ADIS16300_SCAN_INCLI_Y) |
948 .channels = adis16334_channels,
949 .num_channels = ARRAY_SIZE(adis16334_channels),
950 .gyro_scale_micro = 873,
951 .accel_scale_micro = 981,
952 .default_scan_mask = (1 << ADIS16400_SCAN_GYRO_X) |
953 (1 << ADIS16400_SCAN_GYRO_Y) | (1 << ADIS16400_SCAN_GYRO_Z) |
954 (1 << ADIS16400_SCAN_ACC_X) | (1 << ADIS16400_SCAN_ACC_Y) |
955 (1 << ADIS16400_SCAN_ACC_Z),
958 .channels = adis16350_channels,
959 .num_channels = ARRAY_SIZE(adis16350_channels),
960 .gyro_scale_micro = 872664,
961 .accel_scale_micro = 24732,
962 .default_scan_mask = 0x7FF,
963 .flags = ADIS16400_NO_BURST,
966 .channels = adis16350_channels,
967 .num_channels = ARRAY_SIZE(adis16350_channels),
968 .flags = ADIS16400_HAS_PROD_ID,
969 .product_id = 0x3FE8,
970 .gyro_scale_micro = 1279,
971 .accel_scale_micro = 24732,
972 .default_scan_mask = 0x7FF,
975 .channels = adis16350_channels,
976 .num_channels = ARRAY_SIZE(adis16350_channels),
977 .flags = ADIS16400_HAS_PROD_ID,
978 .product_id = 0x3FEA,
979 .gyro_scale_micro = 1279,
980 .accel_scale_micro = 24732,
981 .default_scan_mask = 0x7FF,
984 .channels = adis16350_channels,
985 .num_channels = ARRAY_SIZE(adis16350_channels),
986 .flags = ADIS16400_HAS_PROD_ID,
987 .product_id = 0x3FEC,
988 .gyro_scale_micro = 1279,
989 .accel_scale_micro = 24732,
990 .default_scan_mask = 0x7FF,
993 .channels = adis16350_channels,
994 .num_channels = ARRAY_SIZE(adis16350_channels),
995 .flags = ADIS16400_HAS_PROD_ID,
996 .product_id = 0x3FED,
997 .gyro_scale_micro = 1279,
998 .accel_scale_micro = 24732,
999 .default_scan_mask = 0x7FF,
1002 .channels = adis16400_channels,
1003 .num_channels = ARRAY_SIZE(adis16400_channels),
1004 .flags = ADIS16400_HAS_PROD_ID,
1005 .product_id = 0x4015,
1006 .gyro_scale_micro = 873,
1007 .accel_scale_micro = 32656,
1008 .default_scan_mask = 0xFFF,
1012 static const struct iio_info adis16400_info = {
1013 .driver_module = THIS_MODULE,
1014 .read_raw = &adis16400_read_raw,
1015 .write_raw = &adis16400_write_raw,
1016 .attrs = &adis16400_attribute_group,
1019 static int __devinit adis16400_probe(struct spi_device *spi)
1022 struct adis16400_state *st;
1023 struct iio_dev *indio_dev = iio_allocate_device(sizeof(*st));
1024 if (indio_dev == NULL) {
1028 st = iio_priv(indio_dev);
1029 /* this is only used for removal purposes */
1030 spi_set_drvdata(spi, indio_dev);
1033 mutex_init(&st->buf_lock);
1035 /* setup the industrialio driver allocated elements */
1036 st->variant = &adis16400_chips[spi_get_device_id(spi)->driver_data];
1037 indio_dev->dev.parent = &spi->dev;
1038 indio_dev->name = spi_get_device_id(spi)->name;
1039 indio_dev->channels = st->variant->channels;
1040 indio_dev->num_channels = st->variant->num_channels;
1041 indio_dev->info = &adis16400_info;
1042 indio_dev->modes = INDIO_DIRECT_MODE;
1044 ret = adis16400_configure_ring(indio_dev);
1046 goto error_free_dev;
1048 ret = iio_buffer_register(indio_dev,
1049 st->variant->channels,
1050 st->variant->num_channels);
1052 dev_err(&spi->dev, "failed to initialize the ring\n");
1053 goto error_unreg_ring_funcs;
1057 ret = adis16400_probe_trigger(indio_dev);
1059 goto error_uninitialize_ring;
1062 /* Get the device into a sane initial state */
1063 ret = adis16400_initial_setup(indio_dev);
1065 goto error_remove_trigger;
1066 ret = iio_device_register(indio_dev);
1068 goto error_remove_trigger;
1072 error_remove_trigger:
1073 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1074 adis16400_remove_trigger(indio_dev);
1075 error_uninitialize_ring:
1076 iio_buffer_unregister(indio_dev);
1077 error_unreg_ring_funcs:
1078 adis16400_unconfigure_ring(indio_dev);
1080 iio_free_device(indio_dev);
1085 /* fixme, confirm ordering in this function */
1086 static int adis16400_remove(struct spi_device *spi)
1089 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1091 iio_device_unregister(indio_dev);
1092 ret = adis16400_stop_device(indio_dev);
1096 adis16400_remove_trigger(indio_dev);
1097 iio_buffer_unregister(indio_dev);
1098 adis16400_unconfigure_ring(indio_dev);
1099 iio_free_device(indio_dev);
1107 static const struct spi_device_id adis16400_id[] = {
1108 {"adis16300", ADIS16300},
1109 {"adis16334", ADIS16334},
1110 {"adis16350", ADIS16350},
1111 {"adis16354", ADIS16350},
1112 {"adis16355", ADIS16350},
1113 {"adis16360", ADIS16360},
1114 {"adis16362", ADIS16362},
1115 {"adis16364", ADIS16364},
1116 {"adis16365", ADIS16365},
1117 {"adis16400", ADIS16400},
1118 {"adis16405", ADIS16400},
1122 static struct spi_driver adis16400_driver = {
1124 .name = "adis16400",
1125 .owner = THIS_MODULE,
1127 .id_table = adis16400_id,
1128 .probe = adis16400_probe,
1129 .remove = __devexit_p(adis16400_remove),
1132 static __init int adis16400_init(void)
1134 return spi_register_driver(&adis16400_driver);
1136 module_init(adis16400_init);
1138 static __exit void adis16400_exit(void)
1140 spi_unregister_driver(&adis16400_driver);
1142 module_exit(adis16400_exit);
1144 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
1145 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
1146 MODULE_LICENSE("GPL v2");