2 * ADIS16220 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16 #include <linux/module.h>
21 #include "adis16220.h"
23 #define DRIVER_NAME "adis16220"
26 * adis16220_spi_write_reg_8() - write single byte to a register
27 * @indio_dev: iio device associated with child of actual device
28 * @reg_address: the address of the register to be written
29 * @val: the value to write
31 static int adis16220_spi_write_reg_8(struct iio_dev *indio_dev,
36 struct adis16220_state *st = iio_priv(indio_dev);
38 mutex_lock(&st->buf_lock);
39 st->tx[0] = ADIS16220_WRITE_REG(reg_address);
42 ret = spi_write(st->us, st->tx, 2);
43 mutex_unlock(&st->buf_lock);
49 * adis16220_spi_write_reg_16() - write 2 bytes to a pair of registers
50 * @indio_dev: iio device associated with child of actual device
51 * @reg_address: the address of the lower of the two registers. Second register
52 * is assumed to have address one greater.
53 * @val: value to be written
55 static int adis16220_spi_write_reg_16(struct iio_dev *indio_dev,
60 struct spi_message msg;
61 struct adis16220_state *st = iio_priv(indio_dev);
62 struct spi_transfer xfers[] = {
77 mutex_lock(&st->buf_lock);
78 st->tx[0] = ADIS16220_WRITE_REG(lower_reg_address);
79 st->tx[1] = value & 0xFF;
80 st->tx[2] = ADIS16220_WRITE_REG(lower_reg_address + 1);
81 st->tx[3] = (value >> 8) & 0xFF;
83 spi_message_init(&msg);
84 spi_message_add_tail(&xfers[0], &msg);
85 spi_message_add_tail(&xfers[1], &msg);
86 ret = spi_sync(st->us, &msg);
87 mutex_unlock(&st->buf_lock);
93 * adis16220_spi_read_reg_16() - read 2 bytes from a 16-bit register
94 * @indio_dev: iio device associated with child of actual device
95 * @reg_address: the address of the lower of the two registers. Second register
96 * is assumed to have address one greater.
97 * @val: somewhere to pass back the value read
99 static int adis16220_spi_read_reg_16(struct iio_dev *indio_dev,
100 u8 lower_reg_address,
103 struct spi_message msg;
104 struct adis16220_state *st = iio_priv(indio_dev);
106 struct spi_transfer xfers[] = {
122 mutex_lock(&st->buf_lock);
123 st->tx[0] = ADIS16220_READ_REG(lower_reg_address);
126 spi_message_init(&msg);
127 spi_message_add_tail(&xfers[0], &msg);
128 spi_message_add_tail(&xfers[1], &msg);
129 ret = spi_sync(st->us, &msg);
131 dev_err(&st->us->dev,
132 "problem when reading 16 bit register 0x%02X",
136 *val = (st->rx[0] << 8) | st->rx[1];
139 mutex_unlock(&st->buf_lock);
143 static ssize_t adis16220_read_16bit(struct device *dev,
144 struct device_attribute *attr,
147 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
148 struct iio_dev *indio_dev = dev_get_drvdata(dev);
152 /* Take the iio_dev status lock */
153 mutex_lock(&indio_dev->mlock);
154 ret = adis16220_spi_read_reg_16(indio_dev, this_attr->address,
156 mutex_unlock(&indio_dev->mlock);
159 return sprintf(buf, "%d\n", val);
162 static ssize_t adis16220_write_16bit(struct device *dev,
163 struct device_attribute *attr,
167 struct iio_dev *indio_dev = dev_get_drvdata(dev);
168 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
172 ret = kstrtou16(buf, 10, &val);
175 ret = adis16220_spi_write_reg_16(indio_dev, this_attr->address, val);
178 return ret ? ret : len;
181 static int adis16220_capture(struct iio_dev *indio_dev)
184 ret = adis16220_spi_write_reg_16(indio_dev,
186 0xBF08); /* initiates a manual data capture */
188 dev_err(&indio_dev->dev, "problem beginning capture");
190 msleep(10); /* delay for capture to finish */
195 static int adis16220_reset(struct iio_dev *indio_dev)
198 ret = adis16220_spi_write_reg_8(indio_dev,
200 ADIS16220_GLOB_CMD_SW_RESET);
202 dev_err(&indio_dev->dev, "problem resetting device");
207 static ssize_t adis16220_write_reset(struct device *dev,
208 struct device_attribute *attr,
209 const char *buf, size_t len)
211 struct iio_dev *indio_dev = dev_get_drvdata(dev);
215 ret = strtobool(buf, &val);
221 ret = adis16220_reset(indio_dev);
227 static ssize_t adis16220_write_capture(struct device *dev,
228 struct device_attribute *attr,
229 const char *buf, size_t len)
231 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 ret = strtobool(buf, &val);
240 ret = adis16220_capture(indio_dev);
247 static int adis16220_check_status(struct iio_dev *indio_dev)
252 ret = adis16220_spi_read_reg_16(indio_dev, ADIS16220_DIAG_STAT,
256 dev_err(&indio_dev->dev, "Reading status failed\n");
261 if (status & ADIS16220_DIAG_STAT_VIOLATION)
262 dev_err(&indio_dev->dev,
263 "Capture period violation/interruption\n");
264 if (status & ADIS16220_DIAG_STAT_SPI_FAIL)
265 dev_err(&indio_dev->dev, "SPI failure\n");
266 if (status & ADIS16220_DIAG_STAT_FLASH_UPT)
267 dev_err(&indio_dev->dev, "Flash update failed\n");
268 if (status & ADIS16220_DIAG_STAT_POWER_HIGH)
269 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
270 if (status & ADIS16220_DIAG_STAT_POWER_LOW)
271 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
277 static int adis16220_self_test(struct iio_dev *indio_dev)
280 ret = adis16220_spi_write_reg_16(indio_dev,
282 ADIS16220_MSC_CTRL_SELF_TEST_EN);
284 dev_err(&indio_dev->dev, "problem starting self test");
288 adis16220_check_status(indio_dev);
294 static int adis16220_initial_setup(struct iio_dev *indio_dev)
299 ret = adis16220_self_test(indio_dev);
301 dev_err(&indio_dev->dev, "self test failure");
305 /* Read status register to check the result */
306 ret = adis16220_check_status(indio_dev);
308 adis16220_reset(indio_dev);
309 dev_err(&indio_dev->dev, "device not playing ball -> reset");
310 msleep(ADIS16220_STARTUP_DELAY);
311 ret = adis16220_check_status(indio_dev);
313 dev_err(&indio_dev->dev, "giving up");
322 static ssize_t adis16220_capture_buffer_read(struct iio_dev *indio_dev,
328 struct adis16220_state *st = iio_priv(indio_dev);
329 struct spi_message msg;
330 struct spi_transfer xfers[] = {
348 if (unlikely(!count))
351 if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
354 if (off + count > ADIS16220_CAPTURE_SIZE)
355 count = ADIS16220_CAPTURE_SIZE - off;
357 /* write the begin position of capture buffer */
358 ret = adis16220_spi_write_reg_16(indio_dev,
364 /* read count/2 values from capture buffer */
365 mutex_lock(&st->buf_lock);
367 for (i = 0; i < count; i += 2) {
368 st->tx[i] = ADIS16220_READ_REG(addr);
371 xfers[1].len = count;
373 spi_message_init(&msg);
374 spi_message_add_tail(&xfers[0], &msg);
375 spi_message_add_tail(&xfers[1], &msg);
376 ret = spi_sync(st->us, &msg);
379 mutex_unlock(&st->buf_lock);
383 memcpy(buf, st->rx, count);
385 mutex_unlock(&st->buf_lock);
389 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
390 struct bin_attribute *attr,
395 struct device *dev = container_of(kobj, struct device, kobj);
396 struct iio_dev *indio_dev = dev_get_drvdata(dev);
398 return adis16220_capture_buffer_read(indio_dev, buf,
400 ADIS16220_CAPT_BUFA);
403 static struct bin_attribute accel_bin = {
408 .read = adis16220_accel_bin_read,
409 .size = ADIS16220_CAPTURE_SIZE,
412 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
413 struct bin_attribute *attr,
414 char *buf, loff_t off,
417 struct device *dev = container_of(kobj, struct device, kobj);
418 struct iio_dev *indio_dev = dev_get_drvdata(dev);
420 return adis16220_capture_buffer_read(indio_dev, buf,
422 ADIS16220_CAPT_BUF1);
425 static struct bin_attribute adc1_bin = {
430 .read = adis16220_adc1_bin_read,
431 .size = ADIS16220_CAPTURE_SIZE,
434 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
435 struct bin_attribute *attr,
436 char *buf, loff_t off,
439 struct device *dev = container_of(kobj, struct device, kobj);
440 struct iio_dev *indio_dev = dev_get_drvdata(dev);
442 return adis16220_capture_buffer_read(indio_dev, buf,
444 ADIS16220_CAPT_BUF2);
448 static struct bin_attribute adc2_bin = {
453 .read = adis16220_adc2_bin_read,
454 .size = ADIS16220_CAPTURE_SIZE,
457 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
458 adis16220_write_reset, 0);
460 #define IIO_DEV_ATTR_CAPTURE(_store) \
461 IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
463 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
465 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr) \
466 IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
468 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
469 adis16220_read_16bit,
470 adis16220_write_16bit,
471 ADIS16220_CAPT_PNTR);
473 enum adis16220_channel {
474 in_supply, in_1, in_2, accel, temp
477 struct adis16220_address_spec {
483 /* Address / bits / signed */
484 static const struct adis16220_address_spec adis16220_addresses[][3] = {
485 [in_supply] = { { ADIS16220_CAPT_SUPPLY, 12, 0 }, },
486 [in_1] = { { ADIS16220_CAPT_BUF1, 16, 1 },
487 { ADIS16220_AIN1_NULL, 16, 1 },
488 { ADIS16220_CAPT_PEAK1, 16, 1 }, },
489 [in_2] = { { ADIS16220_CAPT_BUF2, 16, 1 },
490 { ADIS16220_AIN2_NULL, 16, 1 },
491 { ADIS16220_CAPT_PEAK2, 16, 1 }, },
492 [accel] = { { ADIS16220_CAPT_BUFA, 16, 1 },
493 { ADIS16220_ACCL_NULL, 16, 1 },
494 { ADIS16220_CAPT_PEAKA, 16, 1 }, },
495 [temp] = { { ADIS16220_CAPT_TEMP, 12, 0 }, }
498 static int adis16220_read_raw(struct iio_dev *indio_dev,
499 struct iio_chan_spec const *chan,
513 case IIO_CHAN_INFO_OFFSET:
514 if (chan->type == IIO_TEMP) {
520 case IIO_CHAN_INFO_PEAK:
523 case IIO_CHAN_INFO_SCALE:
525 switch (chan->type) {
528 return IIO_VAL_INT_PLUS_MICRO;
531 return IIO_VAL_INT_PLUS_MICRO;
533 if (chan->channel == 0)
535 else /* Should really be dependent on VDD */
537 return IIO_VAL_INT_PLUS_MICRO;
544 if (adis16220_addresses[chan->address][addrind].sign) {
545 ret = adis16220_spi_read_reg_16(indio_dev,
546 adis16220_addresses[chan
552 bits = adis16220_addresses[chan->address][addrind].bits;
553 sval &= (1 << bits) - 1;
554 sval = (s16)(sval << (16 - bits)) >> (16 - bits);
558 ret = adis16220_spi_read_reg_16(indio_dev,
559 adis16220_addresses[chan
565 bits = adis16220_addresses[chan->address][addrind].bits;
566 uval &= (1 << bits) - 1;
572 static const struct iio_chan_spec adis16220_channels[] = {
577 .extend_name = "supply",
578 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
579 .address = in_supply,
582 .info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
583 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
584 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
590 .info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
591 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
597 .info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
598 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
608 static struct attribute *adis16220_attributes[] = {
609 &iio_dev_attr_reset.dev_attr.attr,
610 &iio_dev_attr_capture.dev_attr.attr,
611 &iio_dev_attr_capture_count.dev_attr.attr,
615 static const struct attribute_group adis16220_attribute_group = {
616 .attrs = adis16220_attributes,
619 static const struct iio_info adis16220_info = {
620 .attrs = &adis16220_attribute_group,
621 .driver_module = THIS_MODULE,
622 .read_raw = &adis16220_read_raw,
625 static int __devinit adis16220_probe(struct spi_device *spi)
628 struct adis16220_state *st;
629 struct iio_dev *indio_dev;
631 /* setup the industrialio driver allocated elements */
632 indio_dev = iio_allocate_device(sizeof(*st));
633 if (indio_dev == NULL) {
638 st = iio_priv(indio_dev);
639 /* this is only used for removal purposes */
640 spi_set_drvdata(spi, indio_dev);
643 mutex_init(&st->buf_lock);
645 indio_dev->name = spi->dev.driver->name;
646 indio_dev->dev.parent = &spi->dev;
647 indio_dev->info = &adis16220_info;
648 indio_dev->modes = INDIO_DIRECT_MODE;
649 indio_dev->channels = adis16220_channels;
650 indio_dev->num_channels = ARRAY_SIZE(adis16220_channels);
652 ret = iio_device_register(indio_dev);
656 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &accel_bin);
658 goto error_unregister_dev;
660 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc1_bin);
662 goto error_rm_accel_bin;
664 ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc2_bin);
666 goto error_rm_adc1_bin;
668 /* Get the device into a sane initial state */
669 ret = adis16220_initial_setup(indio_dev);
671 goto error_rm_adc2_bin;
675 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
677 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
679 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
680 error_unregister_dev:
681 iio_device_unregister(indio_dev);
683 iio_free_device(indio_dev);
688 static int adis16220_remove(struct spi_device *spi)
690 struct iio_dev *indio_dev = spi_get_drvdata(spi);
692 flush_scheduled_work();
694 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
695 sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
696 sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
697 iio_device_unregister(indio_dev);
698 iio_free_device(indio_dev);
703 static struct spi_driver adis16220_driver = {
706 .owner = THIS_MODULE,
708 .probe = adis16220_probe,
709 .remove = __devexit_p(adis16220_remove),
711 module_spi_driver(adis16220_driver);
713 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
714 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
715 MODULE_LICENSE("GPL v2");
716 MODULE_ALIAS("spi:adis16220");