2 * ADIS16350/54/55/60/62/64/65 high precision tri-axis inertial sensor
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
23 #include "../ring_generic.h"
24 #include "../accel/accel.h"
25 #include "../adc/adc.h"
26 #include "../gyro/gyro.h"
28 #include "adis16350.h"
30 #define DRIVER_NAME "adis16350"
32 static int adis16350_check_status(struct device *dev);
35 * adis16350_spi_write_reg_8() - write single byte to a register
36 * @dev: device associated with child of actual device (iio_dev or iio_trig)
37 * @reg_address: the address of the register to be written
38 * @val: the value to write
40 static int adis16350_spi_write_reg_8(struct device *dev,
45 struct iio_dev *indio_dev = dev_get_drvdata(dev);
46 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
48 mutex_lock(&st->buf_lock);
49 st->tx[0] = ADIS16350_WRITE_REG(reg_address);
52 ret = spi_write(st->us, st->tx, 2);
53 mutex_unlock(&st->buf_lock);
59 * adis16350_spi_write_reg_16() - write 2 bytes to a pair of registers
60 * @dev: device associated with child of actual device (iio_dev or iio_trig)
61 * @reg_address: the address of the lower of the two registers. Second register
62 * is assumed to have address one greater.
63 * @val: value to be written
65 static int adis16350_spi_write_reg_16(struct device *dev,
70 struct spi_message msg;
71 struct iio_dev *indio_dev = dev_get_drvdata(dev);
72 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
73 struct spi_transfer xfers[] = {
89 mutex_lock(&st->buf_lock);
90 st->tx[0] = ADIS16350_WRITE_REG(lower_reg_address);
91 st->tx[1] = value & 0xFF;
92 st->tx[2] = ADIS16350_WRITE_REG(lower_reg_address + 1);
93 st->tx[3] = (value >> 8) & 0xFF;
95 spi_message_init(&msg);
96 spi_message_add_tail(&xfers[0], &msg);
97 spi_message_add_tail(&xfers[1], &msg);
98 ret = spi_sync(st->us, &msg);
99 mutex_unlock(&st->buf_lock);
105 * adis16350_spi_read_reg_16() - read 2 bytes from a 16-bit register
106 * @dev: device associated with child of actual device (iio_dev or iio_trig)
107 * @reg_address: the address of the lower of the two registers. Second register
108 * is assumed to have address one greater.
109 * @val: somewhere to pass back the value read
111 static int adis16350_spi_read_reg_16(struct device *dev,
112 u8 lower_reg_address,
115 struct spi_message msg;
116 struct iio_dev *indio_dev = dev_get_drvdata(dev);
117 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
119 struct spi_transfer xfers[] = {
135 mutex_lock(&st->buf_lock);
136 st->tx[0] = ADIS16350_READ_REG(lower_reg_address);
141 spi_message_init(&msg);
142 spi_message_add_tail(&xfers[0], &msg);
143 spi_message_add_tail(&xfers[1], &msg);
144 ret = spi_sync(st->us, &msg);
146 dev_err(&st->us->dev,
147 "problem when reading 16 bit register 0x%02X",
151 *val = (st->rx[0] << 8) | st->rx[1];
154 mutex_unlock(&st->buf_lock);
159 static ssize_t adis16350_spi_read_signed(struct device *dev,
160 struct device_attribute *attr,
166 unsigned shift = 16 - bits;
167 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
169 ret = adis16350_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
173 if (val & ADIS16350_ERROR_ACTIVE)
174 adis16350_check_status(dev);
175 val = ((s16)(val << shift) >> shift);
176 return sprintf(buf, "%d\n", val);
179 static ssize_t adis16350_read_12bit_unsigned(struct device *dev,
180 struct device_attribute *attr,
185 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
187 ret = adis16350_spi_read_reg_16(dev, this_attr->address, &val);
191 if (val & ADIS16350_ERROR_ACTIVE)
192 adis16350_check_status(dev);
194 return sprintf(buf, "%u\n", val & 0x0FFF);
197 static ssize_t adis16350_read_14bit_signed(struct device *dev,
198 struct device_attribute *attr,
201 struct iio_dev *indio_dev = dev_get_drvdata(dev);
204 /* Take the iio_dev status lock */
205 mutex_lock(&indio_dev->mlock);
206 ret = adis16350_spi_read_signed(dev, attr, buf, 14);
207 mutex_unlock(&indio_dev->mlock);
212 static ssize_t adis16350_read_12bit_signed(struct device *dev,
213 struct device_attribute *attr,
216 struct iio_dev *indio_dev = dev_get_drvdata(dev);
219 /* Take the iio_dev status lock */
220 mutex_lock(&indio_dev->mlock);
221 ret = adis16350_spi_read_signed(dev, attr, buf, 12);
222 mutex_unlock(&indio_dev->mlock);
227 static ssize_t adis16350_write_16bit(struct device *dev,
228 struct device_attribute *attr,
232 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
236 ret = strict_strtol(buf, 10, &val);
239 ret = adis16350_spi_write_reg_16(dev, this_attr->address, val);
242 return ret ? ret : len;
245 static ssize_t adis16350_read_frequency(struct device *dev,
246 struct device_attribute *attr,
252 ret = adis16350_spi_read_reg_16(dev,
257 sps = (t & ADIS16350_SMPL_PRD_TIME_BASE) ? 53 : 1638;
258 sps /= (t & ADIS16350_SMPL_PRD_DIV_MASK) + 1;
259 len = sprintf(buf, "%d SPS\n", sps);
263 static ssize_t adis16350_write_frequency(struct device *dev,
264 struct device_attribute *attr,
268 struct iio_dev *indio_dev = dev_get_drvdata(dev);
269 struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
274 ret = strict_strtol(buf, 10, &val);
278 mutex_lock(&indio_dev->mlock);
283 t &= ADIS16350_SMPL_PRD_DIV_MASK;
284 if ((t & ADIS16350_SMPL_PRD_DIV_MASK) >= 0x0A)
285 st->us->max_speed_hz = ADIS16350_SPI_SLOW;
287 st->us->max_speed_hz = ADIS16350_SPI_FAST;
289 ret = adis16350_spi_write_reg_8(dev,
293 mutex_unlock(&indio_dev->mlock);
295 return ret ? ret : len;
298 static int adis16350_reset(struct device *dev)
301 ret = adis16350_spi_write_reg_8(dev,
303 ADIS16350_GLOB_CMD_SW_RESET);
305 dev_err(dev, "problem resetting device");
310 static ssize_t adis16350_write_reset(struct device *dev,
311 struct device_attribute *attr,
312 const char *buf, size_t len)
320 return adis16350_reset(dev);
325 int adis16350_set_irq(struct device *dev, bool enable)
329 ret = adis16350_spi_read_reg_16(dev, ADIS16350_MSC_CTRL, &msc);
333 msc |= ADIS16350_MSC_CTRL_DATA_RDY_POL_HIGH;
334 msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_DIO2;
337 msc |= ADIS16350_MSC_CTRL_DATA_RDY_EN;
339 msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_EN;
341 ret = adis16350_spi_write_reg_16(dev, ADIS16350_MSC_CTRL, msc);
349 /* Power down the device */
350 static int adis16350_stop_device(struct device *dev)
353 u16 val = ADIS16350_SLP_CNT_POWER_OFF;
355 ret = adis16350_spi_write_reg_16(dev, ADIS16350_SLP_CNT, val);
357 dev_err(dev, "problem with turning device off: SLP_CNT");
362 static int adis16350_self_test(struct device *dev)
365 ret = adis16350_spi_write_reg_16(dev,
367 ADIS16350_MSC_CTRL_MEM_TEST);
369 dev_err(dev, "problem starting self test");
373 adis16350_check_status(dev);
379 static int adis16350_check_status(struct device *dev)
384 ret = adis16350_spi_read_reg_16(dev, ADIS16350_DIAG_STAT, &status);
387 dev_err(dev, "Reading status failed\n");
391 if (status & ADIS16350_DIAG_STAT_ZACCL_FAIL)
392 dev_err(dev, "Z-axis accelerometer self-test failure\n");
393 if (status & ADIS16350_DIAG_STAT_YACCL_FAIL)
394 dev_err(dev, "Y-axis accelerometer self-test failure\n");
395 if (status & ADIS16350_DIAG_STAT_XACCL_FAIL)
396 dev_err(dev, "X-axis accelerometer self-test failure\n");
397 if (status & ADIS16350_DIAG_STAT_XGYRO_FAIL)
398 dev_err(dev, "X-axis gyroscope self-test failure\n");
399 if (status & ADIS16350_DIAG_STAT_YGYRO_FAIL)
400 dev_err(dev, "Y-axis gyroscope self-test failure\n");
401 if (status & ADIS16350_DIAG_STAT_ZGYRO_FAIL)
402 dev_err(dev, "Z-axis gyroscope self-test failure\n");
403 if (status & ADIS16350_DIAG_STAT_ALARM2)
404 dev_err(dev, "Alarm 2 active\n");
405 if (status & ADIS16350_DIAG_STAT_ALARM1)
406 dev_err(dev, "Alarm 1 active\n");
407 if (status & ADIS16350_DIAG_STAT_FLASH_CHK)
408 dev_err(dev, "Flash checksum error\n");
409 if (status & ADIS16350_DIAG_STAT_SELF_TEST)
410 dev_err(dev, "Self test error\n");
411 if (status & ADIS16350_DIAG_STAT_OVERFLOW)
412 dev_err(dev, "Sensor overrange\n");
413 if (status & ADIS16350_DIAG_STAT_SPI_FAIL)
414 dev_err(dev, "SPI failure\n");
415 if (status & ADIS16350_DIAG_STAT_FLASH_UPT)
416 dev_err(dev, "Flash update failed\n");
417 if (status & ADIS16350_DIAG_STAT_POWER_HIGH)
418 dev_err(dev, "Power supply above 5.25V\n");
419 if (status & ADIS16350_DIAG_STAT_POWER_LOW)
420 dev_err(dev, "Power supply below 4.75V\n");
426 static int adis16350_initial_setup(struct adis16350_state *st)
430 struct device *dev = &st->indio_dev->dev;
432 /* use low spi speed for init */
433 st->us->max_speed_hz = ADIS16350_SPI_SLOW;
434 st->us->mode = SPI_MODE_3;
438 ret = adis16350_set_irq(dev, false);
440 dev_err(dev, "disable irq failed");
445 ret = adis16350_self_test(dev);
447 dev_err(dev, "self test failure");
451 /* Read status register to check the result */
452 ret = adis16350_check_status(dev);
454 adis16350_reset(dev);
455 dev_err(dev, "device not playing ball -> reset");
456 msleep(ADIS16350_STARTUP_DELAY);
457 ret = adis16350_check_status(dev);
459 dev_err(dev, "giving up");
464 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
465 st->us->chip_select, st->us->irq);
467 /* use high spi speed if possible */
468 ret = adis16350_spi_read_reg_16(dev, ADIS16350_SMPL_PRD, &smp_prd);
469 if (!ret && (smp_prd & ADIS16350_SMPL_PRD_DIV_MASK) < 0x0A) {
470 st->us->max_speed_hz = ADIS16350_SPI_SLOW;
478 static IIO_DEV_ATTR_GYRO_X_CALIBBIAS(S_IWUSR | S_IRUGO,
479 adis16350_read_12bit_signed,
480 adis16350_write_16bit,
481 ADIS16350_XGYRO_OFF);
483 static IIO_DEV_ATTR_GYRO_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
484 adis16350_read_12bit_signed,
485 adis16350_write_16bit,
486 ADIS16350_YGYRO_OFF);
488 static IIO_DEV_ATTR_GYRO_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
489 adis16350_read_12bit_signed,
490 adis16350_write_16bit,
491 ADIS16350_ZGYRO_OFF);
493 static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR | S_IRUGO,
494 adis16350_read_12bit_signed,
495 adis16350_write_16bit,
496 ADIS16350_XACCL_OFF);
498 static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
499 adis16350_read_12bit_signed,
500 adis16350_write_16bit,
501 ADIS16350_YACCL_OFF);
503 static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
504 adis16350_read_12bit_signed,
505 adis16350_write_16bit,
506 ADIS16350_ZACCL_OFF);
508 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16350_read_12bit_unsigned,
509 ADIS16350_SUPPLY_OUT);
510 static IIO_CONST_ATTR_IN_NAMED_SCALE(supply, "0.002418");
512 static IIO_DEV_ATTR_GYRO_X(adis16350_read_14bit_signed,
513 ADIS16350_XGYRO_OUT);
514 static IIO_DEV_ATTR_GYRO_Y(adis16350_read_14bit_signed,
515 ADIS16350_YGYRO_OUT);
516 static IIO_DEV_ATTR_GYRO_Z(adis16350_read_14bit_signed,
517 ADIS16350_ZGYRO_OUT);
518 static IIO_CONST_ATTR_GYRO_SCALE("0.00127862821");
520 static IIO_DEV_ATTR_ACCEL_X(adis16350_read_14bit_signed,
521 ADIS16350_XACCL_OUT);
522 static IIO_DEV_ATTR_ACCEL_Y(adis16350_read_14bit_signed,
523 ADIS16350_YACCL_OUT);
524 static IIO_DEV_ATTR_ACCEL_Z(adis16350_read_14bit_signed,
525 ADIS16350_ZACCL_OUT);
526 static IIO_CONST_ATTR_ACCEL_SCALE("0.0247323713");
528 static IIO_DEVICE_ATTR(temp_x_raw, S_IRUGO, adis16350_read_12bit_signed,
529 NULL, ADIS16350_XTEMP_OUT);
530 static IIO_DEVICE_ATTR(temp_y_raw, S_IRUGO, adis16350_read_12bit_signed,
531 NULL, ADIS16350_YTEMP_OUT);
532 static IIO_DEVICE_ATTR(temp_z_raw, S_IRUGO, adis16350_read_12bit_signed,
533 NULL, ADIS16350_ZTEMP_OUT);
534 static IIO_CONST_ATTR_TEMP_SCALE("0.14534");
535 static IIO_CONST_ATTR_TEMP_OFFSET("198.16");
537 static IIO_DEV_ATTR_IN_RAW(0, adis16350_read_12bit_unsigned,
539 static IIO_CONST_ATTR(in0_scale, "0.000806");
541 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
542 adis16350_read_frequency,
543 adis16350_write_frequency);
545 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
546 adis16350_write_reset, 0);
548 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
550 static IIO_CONST_ATTR_NAME("adis16350");
552 static struct attribute *adis16350_attributes[] = {
553 &iio_dev_attr_gyro_x_calibbias.dev_attr.attr,
554 &iio_dev_attr_gyro_y_calibbias.dev_attr.attr,
555 &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
556 &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
557 &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
558 &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
559 &iio_dev_attr_in_supply_raw.dev_attr.attr,
560 &iio_const_attr_in_supply_scale.dev_attr.attr,
561 &iio_dev_attr_gyro_x_raw.dev_attr.attr,
562 &iio_dev_attr_gyro_y_raw.dev_attr.attr,
563 &iio_dev_attr_gyro_z_raw.dev_attr.attr,
564 &iio_const_attr_gyro_scale.dev_attr.attr,
565 &iio_dev_attr_accel_x_raw.dev_attr.attr,
566 &iio_dev_attr_accel_y_raw.dev_attr.attr,
567 &iio_dev_attr_accel_z_raw.dev_attr.attr,
568 &iio_const_attr_accel_scale.dev_attr.attr,
569 &iio_dev_attr_temp_x_raw.dev_attr.attr,
570 &iio_dev_attr_temp_y_raw.dev_attr.attr,
571 &iio_dev_attr_temp_z_raw.dev_attr.attr,
572 &iio_const_attr_temp_scale.dev_attr.attr,
573 &iio_dev_attr_in0_raw.dev_attr.attr,
574 &iio_const_attr_in0_scale.dev_attr.attr,
575 &iio_dev_attr_sampling_frequency.dev_attr.attr,
576 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
577 &iio_dev_attr_reset.dev_attr.attr,
578 &iio_const_attr_name.dev_attr.attr,
582 static const struct attribute_group adis16350_attribute_group = {
583 .attrs = adis16350_attributes,
586 static struct attribute *adis16350_event_attributes[] = {
590 static struct attribute_group adis16350_event_attribute_group = {
591 .attrs = adis16350_event_attributes,
594 static int __devinit adis16350_probe(struct spi_device *spi)
596 int ret, regdone = 0;
597 struct adis16350_state *st = kzalloc(sizeof *st, GFP_KERNEL);
602 /* this is only used for removal purposes */
603 spi_set_drvdata(spi, st);
605 /* Allocate the comms buffers */
606 st->rx = kzalloc(sizeof(*st->rx)*ADIS16350_MAX_RX, GFP_KERNEL);
607 if (st->rx == NULL) {
611 st->tx = kzalloc(sizeof(*st->tx)*ADIS16350_MAX_TX, GFP_KERNEL);
612 if (st->tx == NULL) {
617 mutex_init(&st->buf_lock);
618 /* setup the industrialio driver allocated elements */
619 st->indio_dev = iio_allocate_device();
620 if (st->indio_dev == NULL) {
625 st->indio_dev->dev.parent = &spi->dev;
626 st->indio_dev->num_interrupt_lines = 1;
627 st->indio_dev->event_attrs = &adis16350_event_attribute_group;
628 st->indio_dev->attrs = &adis16350_attribute_group;
629 st->indio_dev->dev_data = (void *)(st);
630 st->indio_dev->driver_module = THIS_MODULE;
631 st->indio_dev->modes = INDIO_DIRECT_MODE;
633 ret = adis16350_configure_ring(st->indio_dev);
637 ret = iio_device_register(st->indio_dev);
639 goto error_unreg_ring_funcs;
642 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
644 printk(KERN_ERR "failed to initialize the ring\n");
645 goto error_unreg_ring_funcs;
649 ret = iio_register_interrupt_line(spi->irq,
655 goto error_uninitialize_ring;
657 ret = adis16350_probe_trigger(st->indio_dev);
659 goto error_unregister_line;
662 /* Get the device into a sane initial state */
663 ret = adis16350_initial_setup(st);
665 goto error_remove_trigger;
668 error_remove_trigger:
669 adis16350_remove_trigger(st->indio_dev);
670 error_unregister_line:
672 iio_unregister_interrupt_line(st->indio_dev, 0);
673 error_uninitialize_ring:
674 iio_ring_buffer_unregister(st->indio_dev->ring);
675 error_unreg_ring_funcs:
676 adis16350_unconfigure_ring(st->indio_dev);
679 iio_device_unregister(st->indio_dev);
681 iio_free_device(st->indio_dev);
692 static int adis16350_remove(struct spi_device *spi)
695 struct adis16350_state *st = spi_get_drvdata(spi);
696 struct iio_dev *indio_dev = st->indio_dev;
698 ret = adis16350_stop_device(&(indio_dev->dev));
702 flush_scheduled_work();
704 adis16350_remove_trigger(indio_dev);
706 iio_unregister_interrupt_line(indio_dev, 0);
708 iio_ring_buffer_unregister(indio_dev->ring);
709 iio_device_unregister(indio_dev);
710 adis16350_unconfigure_ring(indio_dev);
721 static const struct spi_device_id adis16350_id[] = {
732 static struct spi_driver adis16350_driver = {
735 .owner = THIS_MODULE,
737 .probe = adis16350_probe,
738 .remove = __devexit_p(adis16350_remove),
739 .id_table = adis16350_id,
742 static __init int adis16350_init(void)
744 return spi_register_driver(&adis16350_driver);
746 module_init(adis16350_init);
748 static __exit void adis16350_exit(void)
750 spi_unregister_driver(&adis16350_driver);
752 module_exit(adis16350_exit);
754 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
755 MODULE_DESCRIPTION("Analog Devices ADIS16350/54/55/60/62/64/65 IMU SPI driver");
756 MODULE_LICENSE("GPL v2");