2 * ADIS16204 Programmable High-g Digital Impact Sensor and Recorder
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>
20 #include <linux/module.h>
24 #include "../ring_generic.h"
26 #include "../adc/adc.h"
28 #include "adis16204.h"
30 #define DRIVER_NAME "adis16204"
33 * adis16204_spi_write_reg_8() - write single byte to a register
34 * @dev: device associated with child of actual device (iio_dev or iio_trig)
35 * @reg_address: the address of the register to be written
36 * @val: the value to write
38 static int adis16204_spi_write_reg_8(struct iio_dev *indio_dev,
43 struct adis16204_state *st = iio_priv(indio_dev);
45 mutex_lock(&st->buf_lock);
46 st->tx[0] = ADIS16204_WRITE_REG(reg_address);
49 ret = spi_write(st->us, st->tx, 2);
50 mutex_unlock(&st->buf_lock);
56 * adis16204_spi_write_reg_16() - write 2 bytes to a pair of registers
57 * @indio_dev: iio device associated with child of actual device
58 * @reg_address: the address of the lower of the two registers. Second register
59 * is assumed to have address one greater.
60 * @val: value to be written
62 static int adis16204_spi_write_reg_16(struct iio_dev *indio_dev,
67 struct spi_message msg;
68 struct adis16204_state *st = iio_priv(indio_dev);
69 struct spi_transfer xfers[] = {
83 mutex_lock(&st->buf_lock);
84 st->tx[0] = ADIS16204_WRITE_REG(lower_reg_address);
85 st->tx[1] = value & 0xFF;
86 st->tx[2] = ADIS16204_WRITE_REG(lower_reg_address + 1);
87 st->tx[3] = (value >> 8) & 0xFF;
89 spi_message_init(&msg);
90 spi_message_add_tail(&xfers[0], &msg);
91 spi_message_add_tail(&xfers[1], &msg);
92 ret = spi_sync(st->us, &msg);
93 mutex_unlock(&st->buf_lock);
99 * adis16204_spi_read_reg_16() - read 2 bytes from a 16-bit register
100 * @indio_dev: iio device associated with child of actual device
101 * @reg_address: the address of the lower of the two registers. Second register
102 * is assumed to have address one greater.
103 * @val: somewhere to pass back the value read
105 static int adis16204_spi_read_reg_16(struct iio_dev *indio_dev,
106 u8 lower_reg_address,
109 struct spi_message msg;
110 struct adis16204_state *st = iio_priv(indio_dev);
112 struct spi_transfer xfers[] = {
127 mutex_lock(&st->buf_lock);
128 st->tx[0] = ADIS16204_READ_REG(lower_reg_address);
131 spi_message_init(&msg);
132 spi_message_add_tail(&xfers[0], &msg);
133 spi_message_add_tail(&xfers[1], &msg);
134 ret = spi_sync(st->us, &msg);
136 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
140 *val = (st->rx[0] << 8) | st->rx[1];
143 mutex_unlock(&st->buf_lock);
147 static int adis16204_check_status(struct iio_dev *indio_dev)
152 ret = adis16204_spi_read_reg_16(indio_dev,
153 ADIS16204_DIAG_STAT, &status);
155 dev_err(&indio_dev->dev, "Reading status failed\n");
160 if (status & ADIS16204_DIAG_STAT_SELFTEST_FAIL)
161 dev_err(&indio_dev->dev, "Self test failure\n");
162 if (status & ADIS16204_DIAG_STAT_SPI_FAIL)
163 dev_err(&indio_dev->dev, "SPI failure\n");
164 if (status & ADIS16204_DIAG_STAT_FLASH_UPT)
165 dev_err(&indio_dev->dev, "Flash update failed\n");
166 if (status & ADIS16204_DIAG_STAT_POWER_HIGH)
167 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
168 if (status & ADIS16204_DIAG_STAT_POWER_LOW)
169 dev_err(&indio_dev->dev, "Power supply below 2.975V\n");
175 static ssize_t adis16204_read_14bit_signed(struct device *dev,
176 struct device_attribute *attr,
179 struct iio_dev *indio_dev = dev_get_drvdata(dev);
180 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
184 mutex_lock(&indio_dev->mlock);
186 ret = adis16204_spi_read_reg_16(indio_dev,
187 this_attr->address, (u16 *)&val);
189 if (val & ADIS16204_ERROR_ACTIVE)
190 adis16204_check_status(indio_dev);
192 val = ((s16)(val << 2) >> 2);
193 ret = sprintf(buf, "%d\n", val);
196 mutex_unlock(&indio_dev->mlock);
201 static int adis16204_reset(struct iio_dev *indio_dev)
204 ret = adis16204_spi_write_reg_8(indio_dev,
206 ADIS16204_GLOB_CMD_SW_RESET);
208 dev_err(&indio_dev->dev, "problem resetting device");
213 static ssize_t adis16204_write_reset(struct device *dev,
214 struct device_attribute *attr,
215 const char *buf, size_t len)
217 struct iio_dev *indio_dev = dev_get_drvdata(dev);
225 return adis16204_reset(indio_dev);
230 int adis16204_set_irq(struct iio_dev *indio_dev, bool enable)
235 ret = adis16204_spi_read_reg_16(indio_dev, ADIS16204_MSC_CTRL, &msc);
239 msc |= ADIS16204_MSC_CTRL_ACTIVE_HIGH;
240 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_DIO2;
242 msc |= ADIS16204_MSC_CTRL_DATA_RDY_EN;
244 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_EN;
246 ret = adis16204_spi_write_reg_16(indio_dev, ADIS16204_MSC_CTRL, msc);
252 static int adis16204_self_test(struct iio_dev *indio_dev)
255 ret = adis16204_spi_write_reg_16(indio_dev,
257 ADIS16204_MSC_CTRL_SELF_TEST_EN);
259 dev_err(&indio_dev->dev, "problem starting self test");
263 adis16204_check_status(indio_dev);
269 static int adis16204_initial_setup(struct iio_dev *indio_dev)
274 ret = adis16204_set_irq(indio_dev, false);
276 dev_err(&indio_dev->dev, "disable irq failed");
281 ret = adis16204_self_test(indio_dev);
283 dev_err(&indio_dev->dev, "self test failure");
287 /* Read status register to check the result */
288 ret = adis16204_check_status(indio_dev);
290 adis16204_reset(indio_dev);
291 dev_err(&indio_dev->dev, "device not playing ball -> reset");
292 msleep(ADIS16204_STARTUP_DELAY);
293 ret = adis16204_check_status(indio_dev);
295 dev_err(&indio_dev->dev, "giving up");
303 static IIO_DEV_ATTR_ACCEL_XY(adis16204_read_14bit_signed,
304 ADIS16204_XY_RSS_OUT);
305 static IIO_DEV_ATTR_ACCEL_XPEAK(adis16204_read_14bit_signed,
306 ADIS16204_X_PEAK_OUT);
307 static IIO_DEV_ATTR_ACCEL_YPEAK(adis16204_read_14bit_signed,
308 ADIS16204_Y_PEAK_OUT);
309 static IIO_DEV_ATTR_ACCEL_XYPEAK(adis16204_read_14bit_signed,
310 ADIS16204_XY_PEAK_OUT);
311 static IIO_CONST_ATTR(accel_xy_scale, "0.017125");
313 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16204_write_reset, 0);
315 enum adis16204_channel {
323 static u8 adis16204_addresses[5][2] = {
324 [in_supply] = { ADIS16204_SUPPLY_OUT },
325 [in_aux] = { ADIS16204_AUX_ADC },
326 [temp] = { ADIS16204_TEMP_OUT },
327 [accel_x] = { ADIS16204_XACCL_OUT, ADIS16204_XACCL_NULL },
328 [accel_y] = { ADIS16204_XACCL_OUT, ADIS16204_YACCL_NULL },
330 static int adis16204_read_raw(struct iio_dev *indio_dev,
331 struct iio_chan_spec const *chan,
342 mutex_lock(&indio_dev->mlock);
343 addr = adis16204_addresses[chan->address][0];
344 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
346 mutex_unlock(&indio_dev->mlock);
350 if (val16 & ADIS16204_ERROR_ACTIVE) {
351 ret = adis16204_check_status(indio_dev);
353 mutex_unlock(&indio_dev->mlock);
357 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
358 if (chan->scan_type.sign == 's')
359 val16 = (s16)(val16 <<
360 (16 - chan->scan_type.realbits)) >>
361 (16 - chan->scan_type.realbits);
363 mutex_unlock(&indio_dev->mlock);
365 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
366 switch (chan->type) {
369 if (chan->channel == 0)
373 return IIO_VAL_INT_PLUS_MICRO;
377 return IIO_VAL_INT_PLUS_MICRO;
380 if (chan->channel == 'x')
384 return IIO_VAL_INT_PLUS_MICRO;
389 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
392 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
393 switch (chan->type) {
400 mutex_lock(&indio_dev->mlock);
401 addr = adis16204_addresses[chan->address][1];
402 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
404 mutex_unlock(&indio_dev->mlock);
407 val16 &= (1 << bits) - 1;
408 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
410 mutex_unlock(&indio_dev->mlock);
416 static int adis16204_write_raw(struct iio_dev *indio_dev,
417 struct iio_chan_spec const *chan,
426 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
427 switch (chan->type) {
434 val16 = val & ((1 << bits) - 1);
435 addr = adis16204_addresses[chan->address][1];
436 return adis16204_spi_write_reg_16(indio_dev, addr, val16);
441 static struct iio_chan_spec adis16204_channels[] = {
442 IIO_CHAN(IIO_IN, 0, 0, 0, "supply", 0, 0,
443 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
444 in_supply, ADIS16204_SCAN_SUPPLY,
445 IIO_ST('u', 12, 16, 0), 0),
446 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
447 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
448 in_aux, ADIS16204_SCAN_AUX_ADC,
449 IIO_ST('u', 12, 16, 0), 0),
450 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
451 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
452 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
453 temp, ADIS16204_SCAN_TEMP,
454 IIO_ST('u', 12, 16, 0), 0),
455 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
456 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
457 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
458 accel_x, ADIS16204_SCAN_ACC_X,
459 IIO_ST('s', 14, 16, 0), 0),
460 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
461 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
462 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
463 accel_y, ADIS16204_SCAN_ACC_Y,
464 IIO_ST('s', 14, 16, 0), 0),
465 IIO_CHAN_SOFT_TIMESTAMP(5),
467 static struct attribute *adis16204_attributes[] = {
468 &iio_dev_attr_reset.dev_attr.attr,
469 &iio_dev_attr_accel_xy.dev_attr.attr,
470 &iio_dev_attr_accel_xpeak.dev_attr.attr,
471 &iio_dev_attr_accel_ypeak.dev_attr.attr,
472 &iio_dev_attr_accel_xypeak.dev_attr.attr,
473 &iio_const_attr_accel_xy_scale.dev_attr.attr,
477 static const struct attribute_group adis16204_attribute_group = {
478 .attrs = adis16204_attributes,
481 static const struct iio_info adis16204_info = {
482 .attrs = &adis16204_attribute_group,
483 .read_raw = &adis16204_read_raw,
484 .write_raw = &adis16204_write_raw,
485 .driver_module = THIS_MODULE,
488 static int __devinit adis16204_probe(struct spi_device *spi)
490 int ret, regdone = 0;
491 struct adis16204_state *st;
492 struct iio_dev *indio_dev;
494 /* setup the industrialio driver allocated elements */
495 indio_dev = iio_allocate_device(sizeof(*st));
496 if (indio_dev == NULL) {
500 st = iio_priv(indio_dev);
501 /* this is only used for removal purposes */
502 spi_set_drvdata(spi, indio_dev);
504 mutex_init(&st->buf_lock);
506 indio_dev->name = spi->dev.driver->name;
507 indio_dev->dev.parent = &spi->dev;
508 indio_dev->info = &adis16204_info;
509 indio_dev->channels = adis16204_channels;
510 indio_dev->num_channels = ARRAY_SIZE(adis16204_channels);
511 indio_dev->modes = INDIO_DIRECT_MODE;
513 ret = adis16204_configure_ring(indio_dev);
517 ret = iio_device_register(indio_dev);
519 goto error_unreg_ring_funcs;
522 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
524 ARRAY_SIZE(adis16204_channels));
526 printk(KERN_ERR "failed to initialize the ring\n");
527 goto error_unreg_ring_funcs;
531 ret = adis16204_probe_trigger(indio_dev);
533 goto error_uninitialize_ring;
536 /* Get the device into a sane initial state */
537 ret = adis16204_initial_setup(indio_dev);
539 goto error_remove_trigger;
542 error_remove_trigger:
543 adis16204_remove_trigger(indio_dev);
544 error_uninitialize_ring:
545 iio_ring_buffer_unregister(indio_dev->ring);
546 error_unreg_ring_funcs:
547 adis16204_unconfigure_ring(indio_dev);
550 iio_device_unregister(indio_dev);
552 iio_free_device(indio_dev);
557 static int adis16204_remove(struct spi_device *spi)
559 struct iio_dev *indio_dev = spi_get_drvdata(spi);
561 adis16204_remove_trigger(indio_dev);
562 iio_ring_buffer_unregister(indio_dev->ring);
563 iio_device_unregister(indio_dev);
564 adis16204_unconfigure_ring(indio_dev);
569 static struct spi_driver adis16204_driver = {
572 .owner = THIS_MODULE,
574 .probe = adis16204_probe,
575 .remove = __devexit_p(adis16204_remove),
578 static __init int adis16204_init(void)
580 return spi_register_driver(&adis16204_driver);
582 module_init(adis16204_init);
584 static __exit void adis16204_exit(void)
586 spi_unregister_driver(&adis16204_driver);
588 module_exit(adis16204_exit);
590 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
591 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
592 MODULE_LICENSE("GPL v2");