2 * ADIS16260/ADIS16265 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>
23 #include "../ring_generic.h"
24 #include "../adc/adc.h"
27 #include "adis16260.h"
29 #define DRIVER_NAME "adis16260"
31 static int adis16260_check_status(struct iio_dev *indio_dev);
34 * adis16260_spi_write_reg_8() - write single byte to a register
35 * @indio_dev: iio_dev for the device
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
39 static int adis16260_spi_write_reg_8(struct iio_dev *indio_dev,
44 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
46 mutex_lock(&st->buf_lock);
47 st->tx[0] = ADIS16260_WRITE_REG(reg_address);
50 ret = spi_write(st->us, st->tx, 2);
51 mutex_unlock(&st->buf_lock);
57 * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
58 * @indio_dev: iio_dev for the device
59 * @reg_address: the address of the lower of the two registers. Second register
60 * is assumed to have address one greater.
61 * @val: value to be written
63 static int adis16260_spi_write_reg_16(struct iio_dev *indio_dev,
68 struct spi_message msg;
69 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
70 struct spi_transfer xfers[] = {
85 mutex_lock(&st->buf_lock);
86 st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address);
87 st->tx[1] = value & 0xFF;
88 st->tx[2] = ADIS16260_WRITE_REG(lower_reg_address + 1);
89 st->tx[3] = (value >> 8) & 0xFF;
91 spi_message_init(&msg);
92 spi_message_add_tail(&xfers[0], &msg);
93 spi_message_add_tail(&xfers[1], &msg);
94 ret = spi_sync(st->us, &msg);
95 mutex_unlock(&st->buf_lock);
101 * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @indio_dev: iio_dev for the device
103 * @reg_address: the address of the lower of the two registers. Second register
104 * is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
107 static int adis16260_spi_read_reg_16(struct iio_dev *indio_dev,
108 u8 lower_reg_address,
111 struct spi_message msg;
112 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
114 struct spi_transfer xfers[] = {
129 mutex_lock(&st->buf_lock);
130 st->tx[0] = ADIS16260_READ_REG(lower_reg_address);
133 spi_message_init(&msg);
134 spi_message_add_tail(&xfers[0], &msg);
135 spi_message_add_tail(&xfers[1], &msg);
136 ret = spi_sync(st->us, &msg);
138 dev_err(&st->us->dev,
139 "problem when reading 16 bit register 0x%02X",
143 *val = (st->rx[0] << 8) | st->rx[1];
146 mutex_unlock(&st->buf_lock);
150 static ssize_t adis16260_read_frequency_available(struct device *dev,
151 struct device_attribute *attr,
154 struct iio_dev *indio_dev = dev_get_drvdata(dev);
155 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
156 if (spi_get_device_id(st->us)->driver_data)
157 return sprintf(buf, "%s\n", "0.129 ~ 256");
159 return sprintf(buf, "%s\n", "256 2048");
162 static ssize_t adis16260_read_frequency(struct device *dev,
163 struct device_attribute *attr,
166 struct iio_dev *indio_dev = dev_get_drvdata(dev);
167 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
171 ret = adis16260_spi_read_reg_16(indio_dev,
177 if (spi_get_device_id(st->us)->driver_data) /* If an adis16251 */
178 sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256;
180 sps = (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
181 sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
182 len = sprintf(buf, "%d SPS\n", sps);
186 static ssize_t adis16260_write_frequency(struct device *dev,
187 struct device_attribute *attr,
191 struct iio_dev *indio_dev = dev_get_drvdata(dev);
192 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
197 ret = strict_strtol(buf, 10, &val);
201 mutex_lock(&indio_dev->mlock);
202 if (spi_get_device_id(st->us)) {
206 t &= ADIS16260_SMPL_PRD_DIV_MASK;
211 t &= ADIS16260_SMPL_PRD_DIV_MASK;
213 if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
214 st->us->max_speed_hz = ADIS16260_SPI_SLOW;
216 st->us->max_speed_hz = ADIS16260_SPI_FAST;
217 ret = adis16260_spi_write_reg_8(indio_dev,
221 mutex_unlock(&indio_dev->mlock);
223 return ret ? ret : len;
226 static int adis16260_reset(struct iio_dev *indio_dev)
229 ret = adis16260_spi_write_reg_8(indio_dev,
231 ADIS16260_GLOB_CMD_SW_RESET);
233 dev_err(&indio_dev->dev, "problem resetting device");
238 static ssize_t adis16260_write_reset(struct device *dev,
239 struct device_attribute *attr,
240 const char *buf, size_t len)
242 struct iio_dev *indio_dev = dev_get_drvdata(dev);
249 return adis16260_reset(indio_dev);
254 int adis16260_set_irq(struct iio_dev *indio_dev, bool enable)
258 ret = adis16260_spi_read_reg_16(indio_dev, ADIS16260_MSC_CTRL, &msc);
262 msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
264 msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
266 msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
268 ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_MSC_CTRL, msc);
276 /* Power down the device */
277 static int adis16260_stop_device(struct iio_dev *indio_dev)
280 u16 val = ADIS16260_SLP_CNT_POWER_OFF;
282 ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_SLP_CNT, val);
284 dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
289 static int adis16260_self_test(struct iio_dev *indio_dev)
292 ret = adis16260_spi_write_reg_16(indio_dev,
294 ADIS16260_MSC_CTRL_MEM_TEST);
296 dev_err(&indio_dev->dev, "problem starting self test");
300 adis16260_check_status(indio_dev);
306 static int adis16260_check_status(struct iio_dev *indio_dev)
310 struct device *dev = &indio_dev->dev;
312 ret = adis16260_spi_read_reg_16(indio_dev,
317 dev_err(dev, "Reading status failed\n");
321 if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
322 dev_err(dev, "Flash checksum error\n");
323 if (status & ADIS16260_DIAG_STAT_SELF_TEST)
324 dev_err(dev, "Self test error\n");
325 if (status & ADIS16260_DIAG_STAT_OVERFLOW)
326 dev_err(dev, "Sensor overrange\n");
327 if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
328 dev_err(dev, "SPI failure\n");
329 if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
330 dev_err(dev, "Flash update failed\n");
331 if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
332 dev_err(dev, "Power supply above 5.25V\n");
333 if (status & ADIS16260_DIAG_STAT_POWER_LOW)
334 dev_err(dev, "Power supply below 4.75V\n");
340 static int adis16260_initial_setup(struct iio_dev *indio_dev)
343 struct device *dev = &indio_dev->dev;
346 ret = adis16260_set_irq(indio_dev, false);
348 dev_err(dev, "disable irq failed");
353 ret = adis16260_self_test(indio_dev);
355 dev_err(dev, "self test failure");
359 /* Read status register to check the result */
360 ret = adis16260_check_status(indio_dev);
362 adis16260_reset(indio_dev);
363 dev_err(dev, "device not playing ball -> reset");
364 msleep(ADIS16260_STARTUP_DELAY);
365 ret = adis16260_check_status(indio_dev);
367 dev_err(dev, "giving up");
376 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
377 adis16260_read_frequency,
378 adis16260_write_frequency);
380 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0);
382 static IIO_DEVICE_ATTR(sampling_frequency_available,
383 S_IRUGO, adis16260_read_frequency_available, NULL, 0);
385 enum adis16260_channel {
392 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
393 struct iio_chan_spec adis16260_channels_##axis[] = { \
394 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, mod, \
395 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) | \
396 (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) | \
397 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
398 gyro, ADIS16260_SCAN_GYRO, \
399 IIO_ST('s', 14, 16, 0), 0), \
400 IIO_CHAN(IIO_ANGL, 1, 0, 0, NULL, 0, mod, \
402 angle, ADIS16260_SCAN_ANGL, \
403 IIO_ST('u', 14, 16, 0), 0), \
404 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0, \
405 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) | \
406 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
407 temp, ADIS16260_SCAN_TEMP, \
408 IIO_ST('u', 12, 16, 0), 0), \
409 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0, \
410 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
411 in_supply, ADIS16260_SCAN_SUPPLY, \
412 IIO_ST('u', 12, 16, 0), 0), \
413 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0, \
414 (1 << IIO_CHAN_INFO_SCALE_SEPARATE), \
415 in_aux, ADIS16260_SCAN_AUX_ADC, \
416 IIO_ST('u', 12, 16, 0), 0), \
417 IIO_CHAN_SOFT_TIMESTAMP(5) \
420 static const ADIS16260_GYRO_CHANNEL_SET(x, IIO_MOD_X);
421 static const ADIS16260_GYRO_CHANNEL_SET(y, IIO_MOD_Y);
422 static const ADIS16260_GYRO_CHANNEL_SET(z, IIO_MOD_Z);
424 static const u8 adis16260_addresses[5][3] = {
425 [gyro] = { ADIS16260_GYRO_OUT,
427 ADIS16260_GYRO_SCALE },
428 [angle] = { ADIS16260_ANGL_OUT },
429 [in_supply] = { ADIS16260_SUPPLY_OUT },
430 [in_aux] = { ADIS16260_AUX_ADC },
431 [temp] = { ADIS16260_TEMP_OUT },
433 static int adis16260_read_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
438 struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
446 mutex_lock(&indio_dev->mlock);
447 addr = adis16260_addresses[chan->address][0];
448 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
452 if (val16 & ADIS16260_ERROR_ACTIVE) {
453 ret = adis16260_check_status(indio_dev);
457 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
458 if (chan->scan_type.sign == 's')
459 val16 = (s16)(val16 <<
460 (16 - chan->scan_type.realbits)) >>
461 (16 - chan->scan_type.realbits);
463 mutex_unlock(&indio_dev->mlock);
465 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
466 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
467 switch (chan->type) {
470 if (spi_get_device_id(st->us)->driver_data)
474 return IIO_VAL_INT_PLUS_MICRO;
477 if (chan->channel == 0)
481 return IIO_VAL_INT_PLUS_MICRO;
485 return IIO_VAL_INT_PLUS_MICRO;
490 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
493 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
494 switch (chan->type) {
501 mutex_lock(&indio_dev->mlock);
502 addr = adis16260_addresses[chan->address][1];
503 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
505 mutex_unlock(&indio_dev->mlock);
508 val16 &= (1 << bits) - 1;
509 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
511 mutex_unlock(&indio_dev->mlock);
513 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
514 switch (chan->type) {
521 mutex_lock(&indio_dev->mlock);
522 addr = adis16260_addresses[chan->address][2];
523 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
525 mutex_unlock(&indio_dev->mlock);
528 *val = (1 << bits) - 1;
529 mutex_unlock(&indio_dev->mlock);
535 static int adis16260_write_raw(struct iio_dev *indio_dev,
536 struct iio_chan_spec const *chan,
545 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
546 val16 = val & ((1 << bits) - 1);
547 addr = adis16260_addresses[chan->address][1];
548 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
549 case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
550 val16 = val & ((1 << bits) - 1);
551 addr = adis16260_addresses[chan->address][2];
552 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
557 static struct attribute *adis16260_attributes[] = {
558 &iio_dev_attr_sampling_frequency.dev_attr.attr,
559 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
560 &iio_dev_attr_reset.dev_attr.attr,
564 static const struct attribute_group adis16260_attribute_group = {
565 .attrs = adis16260_attributes,
568 static const struct iio_info adis16260_info = {
569 .attrs = &adis16260_attribute_group,
570 .read_raw = &adis16260_read_raw,
571 .write_raw = &adis16260_write_raw,
572 .driver_module = THIS_MODULE,
575 static int __devinit adis16260_probe(struct spi_device *spi)
577 int ret, regdone = 0;
578 struct adis16260_platform_data *pd = spi->dev.platform_data;
579 struct adis16260_state *st = kzalloc(sizeof *st, GFP_KERNEL);
585 st->negate = pd->negate;
586 /* this is only used for removal purposes */
587 spi_set_drvdata(spi, st);
589 /* Allocate the comms buffers */
590 st->rx = kzalloc(sizeof(*st->rx)*ADIS16260_MAX_RX, GFP_KERNEL);
591 if (st->rx == NULL) {
595 st->tx = kzalloc(sizeof(*st->tx)*ADIS16260_MAX_TX, GFP_KERNEL);
596 if (st->tx == NULL) {
601 mutex_init(&st->buf_lock);
602 /* setup the industrialio driver allocated elements */
603 st->indio_dev = iio_allocate_device(0);
604 if (st->indio_dev == NULL) {
609 st->indio_dev->name = spi_get_device_id(st->us)->name;
610 st->indio_dev->dev.parent = &spi->dev;
611 st->indio_dev->info = &adis16260_info;
612 st->indio_dev->num_channels
613 = ARRAY_SIZE(adis16260_channels_x);
614 if (pd && pd->direction)
615 switch (pd->direction) {
617 st->indio_dev->channels = adis16260_channels_x;
620 st->indio_dev->channels = adis16260_channels_y;
623 st->indio_dev->channels = adis16260_channels_z;
629 st->indio_dev->channels = adis16260_channels_x;
631 st->indio_dev->dev_data = (void *)(st);
632 st->indio_dev->modes = INDIO_DIRECT_MODE;
634 ret = adis16260_configure_ring(st->indio_dev);
638 ret = iio_device_register(st->indio_dev);
640 goto error_unreg_ring_funcs;
642 ret = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
643 st->indio_dev->channels,
644 ARRAY_SIZE(adis16260_channels_x));
646 printk(KERN_ERR "failed to initialize the ring\n");
647 goto error_unreg_ring_funcs;
651 ret = adis16260_probe_trigger(st->indio_dev);
653 goto error_uninitialize_ring;
656 /* Get the device into a sane initial state */
657 ret = adis16260_initial_setup(st->indio_dev);
659 goto error_remove_trigger;
662 error_remove_trigger:
663 adis16260_remove_trigger(st->indio_dev);
664 error_uninitialize_ring:
665 iio_ring_buffer_unregister(st->indio_dev->ring);
666 error_unreg_ring_funcs:
667 adis16260_unconfigure_ring(st->indio_dev);
670 iio_device_unregister(st->indio_dev);
672 iio_free_device(st->indio_dev);
683 static int adis16260_remove(struct spi_device *spi)
686 struct adis16260_state *st = spi_get_drvdata(spi);
687 struct iio_dev *indio_dev = st->indio_dev;
689 ret = adis16260_stop_device(indio_dev);
693 flush_scheduled_work();
695 adis16260_remove_trigger(indio_dev);
697 iio_ring_buffer_unregister(st->indio_dev->ring);
698 iio_device_unregister(indio_dev);
699 adis16260_unconfigure_ring(indio_dev);
709 * These parts do not need to be differentiated until someone adds
710 * support for the on chip filtering.
712 static const struct spi_device_id adis16260_id[] = {
721 static struct spi_driver adis16260_driver = {
724 .owner = THIS_MODULE,
726 .probe = adis16260_probe,
727 .remove = __devexit_p(adis16260_remove),
728 .id_table = adis16260_id,
731 static __init int adis16260_init(void)
733 return spi_register_driver(&adis16260_driver);
735 module_init(adis16260_init);
737 static __exit void adis16260_exit(void)
739 spi_unregister_driver(&adis16260_driver);
741 module_exit(adis16260_exit);
743 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
744 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
745 MODULE_LICENSE("GPL v2");