2 * ADIS16240 Programmable Impact Sensor and Recorder 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"
25 #include "../adc/adc.h"
27 #include "adis16240.h"
29 #define DRIVER_NAME "adis16240"
31 static int adis16240_check_status(struct device *dev);
34 * adis16240_spi_write_reg_8() - write single byte to a register
35 * @dev: device associated with child of actual device (iio_dev or iio_trig)
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
39 static int adis16240_spi_write_reg_8(struct device *dev,
44 struct iio_dev *indio_dev = dev_get_drvdata(dev);
45 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
47 mutex_lock(&st->buf_lock);
48 st->tx[0] = ADIS16240_WRITE_REG(reg_address);
51 ret = spi_write(st->us, st->tx, 2);
52 mutex_unlock(&st->buf_lock);
58 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
59 * @dev: device associated with child of actual device (iio_dev or iio_trig)
60 * @reg_address: the address of the lower of the two registers. Second register
61 * is assumed to have address one greater.
62 * @val: value to be written
64 static int adis16240_spi_write_reg_16(struct device *dev,
69 struct spi_message msg;
70 struct iio_dev *indio_dev = dev_get_drvdata(dev);
71 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
72 struct spi_transfer xfers[] = {
88 mutex_lock(&st->buf_lock);
89 st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
90 st->tx[1] = value & 0xFF;
91 st->tx[2] = ADIS16240_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 * adis16240_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 adis16240_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 adis16240_state *st = iio_dev_get_devdata(indio_dev);
118 struct spi_transfer xfers[] = {
134 mutex_lock(&st->buf_lock);
135 st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
140 spi_message_init(&msg);
141 spi_message_add_tail(&xfers[0], &msg);
142 spi_message_add_tail(&xfers[1], &msg);
143 ret = spi_sync(st->us, &msg);
145 dev_err(&st->us->dev,
146 "problem when reading 16 bit register 0x%02X",
150 *val = (st->rx[0] << 8) | st->rx[1];
153 mutex_unlock(&st->buf_lock);
157 static ssize_t adis16240_spi_read_signed(struct device *dev,
158 struct device_attribute *attr,
164 unsigned shift = 16 - bits;
165 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
167 ret = adis16240_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
171 if (val & ADIS16240_ERROR_ACTIVE)
172 adis16240_check_status(dev);
174 val = ((s16)(val << shift) >> shift);
175 return sprintf(buf, "%d\n", val);
178 static ssize_t adis16240_read_10bit_unsigned(struct device *dev,
179 struct device_attribute *attr,
184 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
186 ret = adis16240_spi_read_reg_16(dev, this_attr->address, &val);
190 if (val & ADIS16240_ERROR_ACTIVE)
191 adis16240_check_status(dev);
193 return sprintf(buf, "%u\n", val & 0x03FF);
196 static ssize_t adis16240_read_10bit_signed(struct device *dev,
197 struct device_attribute *attr,
200 struct iio_dev *indio_dev = dev_get_drvdata(dev);
203 /* Take the iio_dev status lock */
204 mutex_lock(&indio_dev->mlock);
205 ret = adis16240_spi_read_signed(dev, attr, buf, 10);
206 mutex_unlock(&indio_dev->mlock);
211 static ssize_t adis16240_read_12bit_signed(struct device *dev,
212 struct device_attribute *attr,
215 struct iio_dev *indio_dev = dev_get_drvdata(dev);
218 /* Take the iio_dev status lock */
219 mutex_lock(&indio_dev->mlock);
220 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
221 mutex_unlock(&indio_dev->mlock);
226 static ssize_t adis16240_write_16bit(struct device *dev,
227 struct device_attribute *attr,
231 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
235 ret = strict_strtol(buf, 10, &val);
238 ret = adis16240_spi_write_reg_16(dev, this_attr->address, val);
241 return ret ? ret : len;
244 static int adis16240_reset(struct device *dev)
247 ret = adis16240_spi_write_reg_8(dev,
249 ADIS16240_GLOB_CMD_SW_RESET);
251 dev_err(dev, "problem resetting device");
256 static ssize_t adis16240_write_reset(struct device *dev,
257 struct device_attribute *attr,
258 const char *buf, size_t len)
266 return adis16240_reset(dev);
271 int adis16240_set_irq(struct device *dev, bool enable)
276 ret = adis16240_spi_read_reg_16(dev, ADIS16240_MSC_CTRL, &msc);
280 msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
281 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
283 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
285 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
287 ret = adis16240_spi_write_reg_16(dev, ADIS16240_MSC_CTRL, msc);
293 static int adis16240_self_test(struct device *dev)
296 ret = adis16240_spi_write_reg_16(dev,
298 ADIS16240_MSC_CTRL_SELF_TEST_EN);
300 dev_err(dev, "problem starting self test");
304 msleep(ADIS16240_STARTUP_DELAY);
306 adis16240_check_status(dev);
312 static int adis16240_check_status(struct device *dev)
317 ret = adis16240_spi_read_reg_16(dev, ADIS16240_DIAG_STAT, &status);
320 dev_err(dev, "Reading status failed\n");
325 if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
326 dev_err(dev, "Power-on, self-test fail\n");
327 if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
328 dev_err(dev, "SPI failure\n");
329 if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
330 dev_err(dev, "Flash update failed\n");
331 if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
332 dev_err(dev, "Power supply above 3.625V\n");
333 if (status & ADIS16240_DIAG_STAT_POWER_LOW)
334 dev_err(dev, "Power supply below 2.225V\n");
340 static int adis16240_initial_setup(struct adis16240_state *st)
343 struct device *dev = &st->indio_dev->dev;
346 ret = adis16240_set_irq(dev, false);
348 dev_err(dev, "disable irq failed");
353 ret = adis16240_self_test(dev);
355 dev_err(dev, "self test failure");
359 /* Read status register to check the result */
360 ret = adis16240_check_status(dev);
362 adis16240_reset(dev);
363 dev_err(dev, "device not playing ball -> reset");
364 msleep(ADIS16240_STARTUP_DELAY);
365 ret = adis16240_check_status(dev);
367 dev_err(dev, "giving up");
372 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
373 st->us->chip_select, st->us->irq);
379 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16240_read_10bit_unsigned,
380 ADIS16240_SUPPLY_OUT);
381 static IIO_DEV_ATTR_IN_RAW(0, adis16240_read_10bit_signed,
383 static IIO_CONST_ATTR_IN_NAMED_SCALE(supply, "0.00488");
385 static IIO_CONST_ATTR_ACCEL_SCALE("0.50406181");
386 static IIO_CONST_ATTR(accel_peak_scale, "6.6292954");
387 static IIO_DEV_ATTR_ACCEL_X(adis16240_read_10bit_signed,
388 ADIS16240_XACCL_OUT);
389 static IIO_DEVICE_ATTR(accel_x_peak_raw, S_IRUGO,
390 adis16240_read_10bit_signed, NULL,
391 ADIS16240_XPEAK_OUT);
392 static IIO_DEV_ATTR_ACCEL_Y(adis16240_read_10bit_signed,
393 ADIS16240_YACCL_OUT);
394 static IIO_DEVICE_ATTR(accel_y_peak_raw, S_IRUGO,
395 adis16240_read_10bit_signed, NULL,
396 ADIS16240_YPEAK_OUT);
397 static IIO_DEV_ATTR_ACCEL_Z(adis16240_read_10bit_signed,
398 ADIS16240_ZACCL_OUT);
399 static IIO_DEVICE_ATTR(accel_z_peak_raw, S_IRUGO,
400 adis16240_read_10bit_signed, NULL,
401 ADIS16240_ZPEAK_OUT);
403 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
404 adis16240_read_12bit_signed, NULL,
405 ADIS16240_XYZPEAK_OUT);
406 static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR | S_IRUGO,
407 adis16240_read_10bit_signed,
408 adis16240_write_16bit,
409 ADIS16240_XACCL_OFF);
410 static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
411 adis16240_read_10bit_signed,
412 adis16240_write_16bit,
413 ADIS16240_YACCL_OFF);
414 static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
415 adis16240_read_10bit_signed,
416 adis16240_write_16bit,
417 ADIS16240_ZACCL_OFF);
418 static IIO_DEV_ATTR_TEMP_RAW(adis16240_read_10bit_unsigned);
419 static IIO_CONST_ATTR_TEMP_SCALE("0.244");
421 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
423 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
425 static IIO_CONST_ATTR_NAME("adis16240");
427 static struct attribute *adis16240_event_attributes[] = {
431 static struct attribute_group adis16240_event_attribute_group = {
432 .attrs = adis16240_event_attributes,
435 static struct attribute *adis16240_attributes[] = {
436 &iio_dev_attr_in_supply_raw.dev_attr.attr,
437 &iio_const_attr_in_supply_scale.dev_attr.attr,
438 &iio_dev_attr_in0_raw.dev_attr.attr,
439 &iio_const_attr_accel_scale.dev_attr.attr,
440 &iio_const_attr_accel_peak_scale.dev_attr.attr,
441 &iio_dev_attr_accel_x_raw.dev_attr.attr,
442 &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
443 &iio_dev_attr_accel_x_peak_raw.dev_attr.attr,
444 &iio_dev_attr_accel_y_raw.dev_attr.attr,
445 &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
446 &iio_dev_attr_accel_y_peak_raw.dev_attr.attr,
447 &iio_dev_attr_accel_z_raw.dev_attr.attr,
448 &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
449 &iio_dev_attr_accel_z_peak_raw.dev_attr.attr,
450 &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
451 &iio_dev_attr_temp_raw.dev_attr.attr,
452 &iio_const_attr_temp_scale.dev_attr.attr,
453 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
454 &iio_dev_attr_reset.dev_attr.attr,
455 &iio_const_attr_name.dev_attr.attr,
459 static const struct attribute_group adis16240_attribute_group = {
460 .attrs = adis16240_attributes,
463 static int __devinit adis16240_probe(struct spi_device *spi)
465 int ret, regdone = 0;
466 struct adis16240_state *st = kzalloc(sizeof *st, GFP_KERNEL);
471 /* this is only used for removal purposes */
472 spi_set_drvdata(spi, st);
474 /* Allocate the comms buffers */
475 st->rx = kzalloc(sizeof(*st->rx)*ADIS16240_MAX_RX, GFP_KERNEL);
476 if (st->rx == NULL) {
480 st->tx = kzalloc(sizeof(*st->tx)*ADIS16240_MAX_TX, GFP_KERNEL);
481 if (st->tx == NULL) {
486 mutex_init(&st->buf_lock);
487 /* setup the industrialio driver allocated elements */
488 st->indio_dev = iio_allocate_device();
489 if (st->indio_dev == NULL) {
494 st->indio_dev->dev.parent = &spi->dev;
495 st->indio_dev->num_interrupt_lines = 1;
496 st->indio_dev->event_attrs = &adis16240_event_attribute_group;
497 st->indio_dev->attrs = &adis16240_attribute_group;
498 st->indio_dev->dev_data = (void *)(st);
499 st->indio_dev->driver_module = THIS_MODULE;
500 st->indio_dev->modes = INDIO_DIRECT_MODE;
502 ret = adis16240_configure_ring(st->indio_dev);
506 ret = iio_device_register(st->indio_dev);
508 goto error_unreg_ring_funcs;
511 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
513 printk(KERN_ERR "failed to initialize the ring\n");
514 goto error_unreg_ring_funcs;
518 ret = iio_register_interrupt_line(spi->irq,
524 goto error_uninitialize_ring;
526 ret = adis16240_probe_trigger(st->indio_dev);
528 goto error_unregister_line;
531 /* Get the device into a sane initial state */
532 ret = adis16240_initial_setup(st);
534 goto error_remove_trigger;
537 error_remove_trigger:
538 adis16240_remove_trigger(st->indio_dev);
539 error_unregister_line:
541 iio_unregister_interrupt_line(st->indio_dev, 0);
542 error_uninitialize_ring:
543 iio_ring_buffer_unregister(st->indio_dev->ring);
544 error_unreg_ring_funcs:
545 adis16240_unconfigure_ring(st->indio_dev);
548 iio_device_unregister(st->indio_dev);
550 iio_free_device(st->indio_dev);
561 static int adis16240_remove(struct spi_device *spi)
563 struct adis16240_state *st = spi_get_drvdata(spi);
564 struct iio_dev *indio_dev = st->indio_dev;
566 flush_scheduled_work();
568 adis16240_remove_trigger(indio_dev);
570 iio_unregister_interrupt_line(indio_dev, 0);
572 iio_ring_buffer_unregister(indio_dev->ring);
573 iio_device_unregister(indio_dev);
574 adis16240_unconfigure_ring(indio_dev);
582 static struct spi_driver adis16240_driver = {
585 .owner = THIS_MODULE,
587 .probe = adis16240_probe,
588 .remove = __devexit_p(adis16240_remove),
591 static __init int adis16240_init(void)
593 return spi_register_driver(&adis16240_driver);
595 module_init(adis16240_init);
597 static __exit void adis16240_exit(void)
599 spi_unregister_driver(&adis16240_driver);
601 module_exit(adis16240_exit);
603 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
604 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
605 MODULE_LICENSE("GPL v2");