2 * ADIS16251 Programmable Digital Gyroscope Sensor Driver
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>
24 #include "../adc/adc.h"
26 #include "adis16251.h"
28 #define DRIVER_NAME "adis16251"
30 /* At the moment the spi framework doesn't allow global setting of cs_change.
31 * It's in the likely to be added comment at the top of spi.h.
32 * This means that use cannot be made of spi_write etc.
36 * adis16251_spi_write_reg_8() - write single byte to a register
37 * @dev: device associated with child of actual device (iio_dev or iio_trig)
38 * @reg_address: the address of the register to be written
39 * @val: the value to write
41 int adis16251_spi_write_reg_8(struct device *dev,
46 struct iio_dev *indio_dev = dev_get_drvdata(dev);
47 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
49 mutex_lock(&st->buf_lock);
50 st->tx[0] = ADIS16251_WRITE_REG(reg_address);
53 ret = spi_write(st->us, st->tx, 2);
54 mutex_unlock(&st->buf_lock);
60 * adis16251_spi_write_reg_16() - write 2 bytes to a pair of registers
61 * @dev: device associated with child of actual device (iio_dev or iio_trig)
62 * @reg_address: the address of the lower of the two registers. Second register
63 * is assumed to have address one greater.
64 * @val: value to be written
66 static int adis16251_spi_write_reg_16(struct device *dev,
71 struct spi_message msg;
72 struct iio_dev *indio_dev = dev_get_drvdata(dev);
73 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
74 struct spi_transfer xfers[] = {
88 mutex_lock(&st->buf_lock);
89 st->tx[0] = ADIS16251_WRITE_REG(lower_reg_address);
90 st->tx[1] = value & 0xFF;
91 st->tx[2] = ADIS16251_WRITE_REG(lower_reg_address + 1);
92 st->tx[3] = (value >> 8) & 0xFF;
94 spi_message_init(&msg);
95 spi_message_add_tail(&xfers[0], &msg);
96 spi_message_add_tail(&xfers[1], &msg);
97 ret = spi_sync(st->us, &msg);
98 mutex_unlock(&st->buf_lock);
104 * adis16251_spi_read_reg_16() - read 2 bytes from a 16-bit register
105 * @dev: device associated with child of actual device (iio_dev or iio_trig)
106 * @reg_address: the address of the lower of the two registers. Second register
107 * is assumed to have address one greater.
108 * @val: somewhere to pass back the value read
110 static int adis16251_spi_read_reg_16(struct device *dev,
111 u8 lower_reg_address,
114 struct spi_message msg;
115 struct iio_dev *indio_dev = dev_get_drvdata(dev);
116 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
118 struct spi_transfer xfers[] = {
132 mutex_lock(&st->buf_lock);
133 st->tx[0] = ADIS16251_READ_REG(lower_reg_address);
138 spi_message_init(&msg);
139 spi_message_add_tail(&xfers[0], &msg);
140 spi_message_add_tail(&xfers[1], &msg);
141 ret = spi_sync(st->us, &msg);
143 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
147 *val = (st->rx[0] << 8) | st->rx[1];
150 mutex_unlock(&st->buf_lock);
155 * adis16251_spi_read_burst() - read all data registers
156 * @dev: device associated with child of actual device (iio_dev or iio_trig)
157 * @rx: somewhere to pass back the value read (min size is 24 bytes)
159 int adis16251_spi_read_burst(struct device *dev, u8 *rx)
161 struct spi_message msg;
162 struct iio_dev *indio_dev = dev_get_drvdata(dev);
163 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
164 u32 old_speed_hz = st->us->max_speed_hz;
167 struct spi_transfer xfers[] = {
181 mutex_lock(&st->buf_lock);
182 st->tx[0] = ADIS16251_READ_REG(ADIS16251_GLOB_CMD);
185 spi_message_init(&msg);
186 spi_message_add_tail(&xfers[0], &msg);
187 spi_message_add_tail(&xfers[1], &msg);
189 st->us->max_speed_hz = min(ADIS16251_SPI_BURST, old_speed_hz);
192 ret = spi_sync(st->us, &msg);
194 dev_err(&st->us->dev, "problem when burst reading");
196 st->us->max_speed_hz = old_speed_hz;
198 mutex_unlock(&st->buf_lock);
203 * adis16251_spi_read_sequence() - read a sequence of 16-bit registers
204 * @dev: device associated with child of actual device (iio_dev or iio_trig)
205 * @tx: register addresses in bytes 0,2,4,6... (min size is 2*num bytes)
206 * @rx: somewhere to pass back the value read (min size is 2*num bytes)
208 int adis16251_spi_read_sequence(struct device *dev,
209 u8 *tx, u8 *rx, int num)
211 struct spi_message msg;
212 struct spi_transfer *xfers;
213 struct iio_dev *indio_dev = dev_get_drvdata(dev);
214 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
217 xfers = kzalloc(num + 1, GFP_KERNEL);
219 dev_err(&st->us->dev, "memory alloc failed");
224 /* tx: |add1|addr2|addr3|...|addrN |zero|
225 * rx: |zero|res1 |res2 |...|resN-1|resN| */
226 spi_message_init(&msg);
227 for (i = 0; i < num + 1; i++) {
229 xfers[i].rx_buf = st->rx + 2*(i - 1);
231 xfers[i].tx_buf = st->tx + 2*i;
232 xfers[i].bits_per_word = 8;
234 xfers[i].cs_change = 1;
235 spi_message_add_tail(&xfers[i], &msg);
238 mutex_lock(&st->buf_lock);
240 ret = spi_sync(st->us, &msg);
242 dev_err(&st->us->dev, "problem when reading sequence");
244 mutex_unlock(&st->buf_lock);
251 static ssize_t adis16251_spi_read_signed(struct device *dev,
252 struct device_attribute *attr,
258 unsigned shift = 16 - bits;
259 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
261 ret = adis16251_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
265 if (val & ADIS16251_ERROR_ACTIVE)
266 adis16251_check_status(dev);
267 val = ((s16)(val << shift) >> shift);
268 return sprintf(buf, "%d\n", val);
271 static ssize_t adis16251_read_12bit_unsigned(struct device *dev,
272 struct device_attribute *attr,
277 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
279 ret = adis16251_spi_read_reg_16(dev, this_attr->address, &val);
283 if (val & ADIS16251_ERROR_ACTIVE)
284 adis16251_check_status(dev);
286 return sprintf(buf, "%u\n", val & 0x0FFF);
289 static ssize_t adis16251_read_14bit_signed(struct device *dev,
290 struct device_attribute *attr,
293 struct iio_dev *indio_dev = dev_get_drvdata(dev);
296 /* Take the iio_dev status lock */
297 mutex_lock(&indio_dev->mlock);
298 ret = adis16251_spi_read_signed(dev, attr, buf, 14);
299 mutex_unlock(&indio_dev->mlock);
304 static ssize_t adis16251_read_12bit_signed(struct device *dev,
305 struct device_attribute *attr,
308 struct iio_dev *indio_dev = dev_get_drvdata(dev);
311 /* Take the iio_dev status lock */
312 mutex_lock(&indio_dev->mlock);
313 ret = adis16251_spi_read_signed(dev, attr, buf, 12);
314 mutex_unlock(&indio_dev->mlock);
319 static ssize_t adis16251_write_16bit(struct device *dev,
320 struct device_attribute *attr,
324 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
328 ret = strict_strtol(buf, 10, &val);
331 ret = adis16251_spi_write_reg_16(dev, this_attr->address, val);
334 return ret ? ret : len;
337 static ssize_t adis16251_read_frequency(struct device *dev,
338 struct device_attribute *attr,
344 ret = adis16251_spi_read_reg_16(dev,
349 sps = (t & ADIS16251_SMPL_PRD_TIME_BASE) ? 8 : 256;
350 sps /= (t & ADIS16251_SMPL_PRD_DIV_MASK) + 1;
351 len = sprintf(buf, "%d SPS\n", sps);
355 static ssize_t adis16251_write_frequency(struct device *dev,
356 struct device_attribute *attr,
360 struct iio_dev *indio_dev = dev_get_drvdata(dev);
361 struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
366 ret = strict_strtol(buf, 10, &val);
370 mutex_lock(&indio_dev->mlock);
375 t &= ADIS16251_SMPL_PRD_DIV_MASK;
376 if ((t & ADIS16251_SMPL_PRD_DIV_MASK) >= 0x0A)
377 st->us->max_speed_hz = ADIS16251_SPI_SLOW;
379 st->us->max_speed_hz = ADIS16251_SPI_FAST;
381 ret = adis16251_spi_write_reg_8(dev,
385 mutex_unlock(&indio_dev->mlock);
387 return ret ? ret : len;
390 static ssize_t adis16251_write_reset(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t len)
400 return adis16251_reset(dev);
407 int adis16251_set_irq(struct device *dev, bool enable)
411 ret = adis16251_spi_read_reg_16(dev, ADIS16251_MSC_CTRL, &msc);
415 msc |= ADIS16251_MSC_CTRL_DATA_RDY_POL_HIGH;
417 msc |= ADIS16251_MSC_CTRL_DATA_RDY_EN;
419 msc &= ~ADIS16251_MSC_CTRL_DATA_RDY_EN;
421 ret = adis16251_spi_write_reg_16(dev, ADIS16251_MSC_CTRL, msc);
429 int adis16251_reset(struct device *dev)
432 ret = adis16251_spi_write_reg_8(dev,
434 ADIS16251_GLOB_CMD_SW_RESET);
436 dev_err(dev, "problem resetting device");
441 /* Power down the device */
442 int adis16251_stop_device(struct device *dev)
445 u16 val = ADIS16251_SLP_CNT_POWER_OFF;
447 ret = adis16251_spi_write_reg_16(dev, ADIS16251_SLP_CNT, val);
449 dev_err(dev, "problem with turning device off: SLP_CNT");
454 static int adis16251_self_test(struct device *dev)
458 ret = adis16251_spi_write_reg_16(dev,
460 ADIS16251_MSC_CTRL_INT_SELF_TEST);
462 dev_err(dev, "problem starting self test");
466 adis16251_check_status(dev);
472 int adis16251_check_status(struct device *dev)
477 ret = adis16251_spi_read_reg_16(dev, ADIS16251_DIAG_STAT, &status);
480 dev_err(dev, "Reading status failed\n");
484 if (!(status & ADIS16251_DIAG_STAT_ERR_MASK)) {
491 if (status & ADIS16251_DIAG_STAT_ALARM2)
492 dev_err(dev, "Alarm 2 active\n");
493 if (status & ADIS16251_DIAG_STAT_ALARM1)
494 dev_err(dev, "Alarm 1 active\n");
495 if (status & ADIS16251_DIAG_STAT_SELF_TEST)
496 dev_err(dev, "Self test error\n");
497 if (status & ADIS16251_DIAG_STAT_OVERFLOW)
498 dev_err(dev, "Sensor overrange\n");
499 if (status & ADIS16251_DIAG_STAT_SPI_FAIL)
500 dev_err(dev, "SPI failure\n");
501 if (status & ADIS16251_DIAG_STAT_FLASH_UPT)
502 dev_err(dev, "Flash update failed\n");
503 if (status & ADIS16251_DIAG_STAT_POWER_HIGH)
504 dev_err(dev, "Power supply above 5.25V\n");
505 if (status & ADIS16251_DIAG_STAT_POWER_LOW)
506 dev_err(dev, "Power supply below 4.75V\n");
512 static int adis16251_initial_setup(struct adis16251_state *st)
516 struct device *dev = &st->indio_dev->dev;
518 /* use low spi speed for init */
519 st->us->max_speed_hz = ADIS16251_SPI_SLOW;
520 st->us->mode = SPI_MODE_3;
524 ret = adis16251_set_irq(dev, false);
526 dev_err(dev, "disable irq failed");
532 /* Read status register to check the result */
533 ret = adis16251_check_status(dev);
535 adis16251_reset(dev);
536 dev_err(dev, "device not playing ball -> reset");
537 msleep(ADIS16251_STARTUP_DELAY);
538 ret = adis16251_check_status(dev);
540 dev_err(dev, "giving up");
545 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
546 st->us->chip_select, st->us->irq);
548 /* use high spi speed if possible */
549 ret = adis16251_spi_read_reg_16(dev, ADIS16251_SMPL_PRD, &smp_prd);
550 if (!ret && (smp_prd & ADIS16251_SMPL_PRD_DIV_MASK) < 0x0A) {
551 st->us->max_speed_hz = ADIS16251_SPI_SLOW;
559 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16251_read_12bit_signed,
560 ADIS16251_SUPPLY_OUT);
561 static IIO_CONST_ATTR(in0_supply_scale, "0.0018315");
563 static IIO_DEV_ATTR_GYRO(adis16251_read_14bit_signed,
565 static IIO_DEV_ATTR_GYRO_SCALE(S_IWUSR | S_IRUGO,
566 adis16251_read_12bit_signed,
567 adis16251_write_16bit,
568 ADIS16251_GYRO_SCALE);
569 static IIO_DEV_ATTR_GYRO_OFFSET(S_IWUSR | S_IRUGO,
570 adis16251_read_12bit_signed,
571 adis16251_write_16bit,
574 static IIO_DEV_ATTR_TEMP_RAW(adis16251_read_12bit_signed);
575 static IIO_CONST_ATTR(temp_offset, "25 K");
576 static IIO_CONST_ATTR(temp_scale, "0.1453 K");
578 static IIO_DEV_ATTR_IN_NAMED_RAW(1, aux, adis16251_read_12bit_unsigned,
580 static IIO_CONST_ATTR(in1_aux_scale, "0.0006105");
582 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
583 adis16251_read_frequency,
584 adis16251_write_frequency);
585 static IIO_DEV_ATTR_ANGL(adis16251_read_14bit_signed,
588 static IIO_DEV_ATTR_RESET(adis16251_write_reset);
590 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.129 ~ 256");
592 static IIO_CONST_ATTR(name, "adis16251");
594 static struct attribute *adis16251_event_attributes[] = {
598 static struct attribute_group adis16251_event_attribute_group = {
599 .attrs = adis16251_event_attributes,
602 static struct attribute *adis16251_attributes[] = {
603 &iio_dev_attr_in0_supply_raw.dev_attr.attr,
604 &iio_const_attr_in0_supply_scale.dev_attr.attr,
605 &iio_dev_attr_gyro_raw.dev_attr.attr,
606 &iio_dev_attr_gyro_scale.dev_attr.attr,
607 &iio_dev_attr_gyro_offset.dev_attr.attr,
608 &iio_dev_attr_angl_raw.dev_attr.attr,
609 &iio_dev_attr_temp_raw.dev_attr.attr,
610 &iio_const_attr_temp_offset.dev_attr.attr,
611 &iio_const_attr_temp_scale.dev_attr.attr,
612 &iio_dev_attr_in1_aux_raw.dev_attr.attr,
613 &iio_const_attr_in1_aux_scale.dev_attr.attr,
614 &iio_dev_attr_sampling_frequency.dev_attr.attr,
615 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
616 &iio_dev_attr_reset.dev_attr.attr,
617 &iio_const_attr_name.dev_attr.attr,
621 static const struct attribute_group adis16251_attribute_group = {
622 .attrs = adis16251_attributes,
625 static int __devinit adis16251_probe(struct spi_device *spi)
627 int ret, regdone = 0;
628 struct adis16251_state *st = kzalloc(sizeof *st, GFP_KERNEL);
633 /* this is only used for removal purposes */
634 spi_set_drvdata(spi, st);
636 /* Allocate the comms buffers */
637 st->rx = kzalloc(sizeof(*st->rx)*ADIS16251_MAX_RX, GFP_KERNEL);
638 if (st->rx == NULL) {
642 st->tx = kzalloc(sizeof(*st->tx)*ADIS16251_MAX_TX, GFP_KERNEL);
643 if (st->tx == NULL) {
648 mutex_init(&st->buf_lock);
649 /* setup the industrialio driver allocated elements */
650 st->indio_dev = iio_allocate_device();
651 if (st->indio_dev == NULL) {
656 st->indio_dev->dev.parent = &spi->dev;
657 st->indio_dev->num_interrupt_lines = 1;
658 st->indio_dev->event_attrs = &adis16251_event_attribute_group;
659 st->indio_dev->attrs = &adis16251_attribute_group;
660 st->indio_dev->dev_data = (void *)(st);
661 st->indio_dev->driver_module = THIS_MODULE;
662 st->indio_dev->modes = INDIO_DIRECT_MODE;
664 ret = adis16251_configure_ring(st->indio_dev);
668 ret = iio_device_register(st->indio_dev);
670 goto error_unreg_ring_funcs;
673 ret = adis16251_initialize_ring(st->indio_dev->ring);
675 printk(KERN_ERR "failed to initialize the ring\n");
676 goto error_unreg_ring_funcs;
679 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
680 ret = iio_register_interrupt_line(spi->irq,
686 goto error_uninitialize_ring;
688 ret = adis16251_probe_trigger(st->indio_dev);
690 goto error_unregister_line;
693 /* Get the device into a sane initial state */
694 ret = adis16251_initial_setup(st);
696 goto error_remove_trigger;
699 error_remove_trigger:
700 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
701 adis16251_remove_trigger(st->indio_dev);
702 error_unregister_line:
703 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
704 iio_unregister_interrupt_line(st->indio_dev, 0);
705 error_uninitialize_ring:
706 adis16251_uninitialize_ring(st->indio_dev->ring);
707 error_unreg_ring_funcs:
708 adis16251_unconfigure_ring(st->indio_dev);
711 iio_device_unregister(st->indio_dev);
713 iio_free_device(st->indio_dev);
724 /* fixme, confirm ordering in this function */
725 static int adis16251_remove(struct spi_device *spi)
728 struct adis16251_state *st = spi_get_drvdata(spi);
729 struct iio_dev *indio_dev = st->indio_dev;
731 ret = adis16251_stop_device(&(indio_dev->dev));
735 flush_scheduled_work();
737 adis16251_remove_trigger(indio_dev);
738 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
739 iio_unregister_interrupt_line(indio_dev, 0);
741 adis16251_uninitialize_ring(indio_dev->ring);
742 adis16251_unconfigure_ring(indio_dev);
743 iio_device_unregister(indio_dev);
754 static struct spi_driver adis16251_driver = {
757 .owner = THIS_MODULE,
759 .probe = adis16251_probe,
760 .remove = __devexit_p(adis16251_remove),
763 static __init int adis16251_init(void)
765 return spi_register_driver(&adis16251_driver);
767 module_init(adis16251_init);
769 static __exit void adis16251_exit(void)
771 spi_unregister_driver(&adis16251_driver);
773 module_exit(adis16251_exit);
775 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
776 MODULE_DESCRIPTION("Analog Devices ADIS16251 Digital Gyroscope Sensor SPI driver");
777 MODULE_LICENSE("GPL v2");