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/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
25 #include "adis16204.h"
27 #define DRIVER_NAME "adis16204"
30 * adis16204_spi_write_reg_8() - write single byte to a register
31 * @dev: device associated with child of actual device (iio_dev or iio_trig)
32 * @reg_address: the address of the register to be written
33 * @val: the value to write
35 static int adis16204_spi_write_reg_8(struct iio_dev *indio_dev,
40 struct adis16204_state *st = iio_priv(indio_dev);
42 mutex_lock(&st->buf_lock);
43 st->tx[0] = ADIS16204_WRITE_REG(reg_address);
46 ret = spi_write(st->us, st->tx, 2);
47 mutex_unlock(&st->buf_lock);
53 * adis16204_spi_write_reg_16() - write 2 bytes to a pair of registers
54 * @indio_dev: iio device associated with child of actual device
55 * @reg_address: the address of the lower of the two registers. Second register
56 * is assumed to have address one greater.
57 * @val: value to be written
59 static int adis16204_spi_write_reg_16(struct iio_dev *indio_dev,
64 struct spi_message msg;
65 struct adis16204_state *st = iio_priv(indio_dev);
66 struct spi_transfer xfers[] = {
80 mutex_lock(&st->buf_lock);
81 st->tx[0] = ADIS16204_WRITE_REG(lower_reg_address);
82 st->tx[1] = value & 0xFF;
83 st->tx[2] = ADIS16204_WRITE_REG(lower_reg_address + 1);
84 st->tx[3] = (value >> 8) & 0xFF;
86 spi_message_init(&msg);
87 spi_message_add_tail(&xfers[0], &msg);
88 spi_message_add_tail(&xfers[1], &msg);
89 ret = spi_sync(st->us, &msg);
90 mutex_unlock(&st->buf_lock);
96 * adis16204_spi_read_reg_16() - read 2 bytes from a 16-bit register
97 * @indio_dev: iio device associated with child of actual device
98 * @reg_address: the address of the lower of the two registers. Second register
99 * is assumed to have address one greater.
100 * @val: somewhere to pass back the value read
102 static int adis16204_spi_read_reg_16(struct iio_dev *indio_dev,
103 u8 lower_reg_address,
106 struct spi_message msg;
107 struct adis16204_state *st = iio_priv(indio_dev);
109 struct spi_transfer xfers[] = {
124 mutex_lock(&st->buf_lock);
125 st->tx[0] = ADIS16204_READ_REG(lower_reg_address);
128 spi_message_init(&msg);
129 spi_message_add_tail(&xfers[0], &msg);
130 spi_message_add_tail(&xfers[1], &msg);
131 ret = spi_sync(st->us, &msg);
133 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
137 *val = (st->rx[0] << 8) | st->rx[1];
140 mutex_unlock(&st->buf_lock);
144 static int adis16204_check_status(struct iio_dev *indio_dev)
149 ret = adis16204_spi_read_reg_16(indio_dev,
150 ADIS16204_DIAG_STAT, &status);
152 dev_err(&indio_dev->dev, "Reading status failed\n");
157 if (status & ADIS16204_DIAG_STAT_SELFTEST_FAIL)
158 dev_err(&indio_dev->dev, "Self test failure\n");
159 if (status & ADIS16204_DIAG_STAT_SPI_FAIL)
160 dev_err(&indio_dev->dev, "SPI failure\n");
161 if (status & ADIS16204_DIAG_STAT_FLASH_UPT)
162 dev_err(&indio_dev->dev, "Flash update failed\n");
163 if (status & ADIS16204_DIAG_STAT_POWER_HIGH)
164 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
165 if (status & ADIS16204_DIAG_STAT_POWER_LOW)
166 dev_err(&indio_dev->dev, "Power supply below 2.975V\n");
172 static int adis16204_reset(struct iio_dev *indio_dev)
175 ret = adis16204_spi_write_reg_8(indio_dev,
177 ADIS16204_GLOB_CMD_SW_RESET);
179 dev_err(&indio_dev->dev, "problem resetting device");
184 int adis16204_set_irq(struct iio_dev *indio_dev, bool enable)
189 ret = adis16204_spi_read_reg_16(indio_dev, ADIS16204_MSC_CTRL, &msc);
193 msc |= ADIS16204_MSC_CTRL_ACTIVE_HIGH;
194 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_DIO2;
196 msc |= ADIS16204_MSC_CTRL_DATA_RDY_EN;
198 msc &= ~ADIS16204_MSC_CTRL_DATA_RDY_EN;
200 ret = adis16204_spi_write_reg_16(indio_dev, ADIS16204_MSC_CTRL, msc);
206 static int adis16204_self_test(struct iio_dev *indio_dev)
209 ret = adis16204_spi_write_reg_16(indio_dev,
211 ADIS16204_MSC_CTRL_SELF_TEST_EN);
213 dev_err(&indio_dev->dev, "problem starting self test");
217 adis16204_check_status(indio_dev);
223 static int adis16204_initial_setup(struct iio_dev *indio_dev)
228 ret = adis16204_set_irq(indio_dev, false);
230 dev_err(&indio_dev->dev, "disable irq failed");
235 ret = adis16204_self_test(indio_dev);
237 dev_err(&indio_dev->dev, "self test failure");
241 /* Read status register to check the result */
242 ret = adis16204_check_status(indio_dev);
244 adis16204_reset(indio_dev);
245 dev_err(&indio_dev->dev, "device not playing ball -> reset");
246 msleep(ADIS16204_STARTUP_DELAY);
247 ret = adis16204_check_status(indio_dev);
249 dev_err(&indio_dev->dev, "giving up");
258 /* Unique to this driver currently */
260 enum adis16204_channel {
269 static u8 adis16204_addresses[6][3] = {
270 [in_supply] = { ADIS16204_SUPPLY_OUT },
271 [in_aux] = { ADIS16204_AUX_ADC },
272 [temp] = { ADIS16204_TEMP_OUT },
273 [accel_x] = { ADIS16204_XACCL_OUT, ADIS16204_XACCL_NULL,
274 ADIS16204_X_PEAK_OUT },
275 [accel_y] = { ADIS16204_XACCL_OUT, ADIS16204_YACCL_NULL,
276 ADIS16204_Y_PEAK_OUT },
277 [accel_xy] = { ADIS16204_XY_RSS_OUT, 0,
278 ADIS16204_XY_PEAK_OUT },
281 static int adis16204_read_raw(struct iio_dev *indio_dev,
282 struct iio_chan_spec const *chan,
293 case IIO_CHAN_INFO_RAW:
294 mutex_lock(&indio_dev->mlock);
295 addr = adis16204_addresses[chan->address][0];
296 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
298 mutex_unlock(&indio_dev->mlock);
302 if (val16 & ADIS16204_ERROR_ACTIVE) {
303 ret = adis16204_check_status(indio_dev);
305 mutex_unlock(&indio_dev->mlock);
309 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
310 if (chan->scan_type.sign == 's')
311 val16 = (s16)(val16 <<
312 (16 - chan->scan_type.realbits)) >>
313 (16 - chan->scan_type.realbits);
315 mutex_unlock(&indio_dev->mlock);
317 case IIO_CHAN_INFO_SCALE:
318 switch (chan->type) {
321 if (chan->channel == 0)
325 return IIO_VAL_INT_PLUS_MICRO;
329 return IIO_VAL_INT_PLUS_MICRO;
332 switch (chan->channel2) {
334 case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
342 return IIO_VAL_INT_PLUS_MICRO;
347 case IIO_CHAN_INFO_OFFSET:
350 case IIO_CHAN_INFO_CALIBBIAS:
351 case IIO_CHAN_INFO_PEAK:
352 if (mask == IIO_CHAN_INFO_CALIBBIAS) {
355 } else { /* PEAK_SEPARATE */
359 mutex_lock(&indio_dev->mlock);
360 addr = adis16204_addresses[chan->address][addrind];
361 ret = adis16204_spi_read_reg_16(indio_dev, addr, &val16);
363 mutex_unlock(&indio_dev->mlock);
366 val16 &= (1 << bits) - 1;
367 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
369 mutex_unlock(&indio_dev->mlock);
375 static int adis16204_write_raw(struct iio_dev *indio_dev,
376 struct iio_chan_spec const *chan,
385 case IIO_CHAN_INFO_CALIBBIAS:
386 switch (chan->type) {
393 val16 = val & ((1 << bits) - 1);
394 addr = adis16204_addresses[chan->address][1];
395 return adis16204_spi_write_reg_16(indio_dev, addr, val16);
400 static const struct iio_chan_spec adis16204_channels[] = {
403 .indexed = 1, /* Note was not previously indexed */
405 .extend_name = "supply",
406 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
407 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
408 .address = in_supply,
409 .scan_index = ADIS16204_SCAN_SUPPLY,
419 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
420 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
422 .scan_index = ADIS16204_SCAN_AUX_ADC,
432 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
433 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
434 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
436 .scan_index = ADIS16204_SCAN_TEMP,
445 .channel2 = IIO_MOD_X,
446 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
447 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
448 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
449 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
451 .scan_index = ADIS16204_SCAN_ACC_X,
460 .channel2 = IIO_MOD_Y,
461 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
462 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
463 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |
464 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
466 .scan_index = ADIS16204_SCAN_ACC_Y,
473 IIO_CHAN_SOFT_TIMESTAMP(5),
477 .channel2 = IIO_MOD_ROOT_SUM_SQUARED_X_Y,
478 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
479 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
480 IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
490 static const struct iio_info adis16204_info = {
491 .read_raw = &adis16204_read_raw,
492 .write_raw = &adis16204_write_raw,
493 .driver_module = THIS_MODULE,
496 static int __devinit adis16204_probe(struct spi_device *spi)
499 struct adis16204_state *st;
500 struct iio_dev *indio_dev;
502 /* setup the industrialio driver allocated elements */
503 indio_dev = iio_device_alloc(sizeof(*st));
504 if (indio_dev == NULL) {
508 st = iio_priv(indio_dev);
509 /* this is only used for removal purposes */
510 spi_set_drvdata(spi, indio_dev);
512 mutex_init(&st->buf_lock);
514 indio_dev->name = spi->dev.driver->name;
515 indio_dev->dev.parent = &spi->dev;
516 indio_dev->info = &adis16204_info;
517 indio_dev->channels = adis16204_channels;
518 indio_dev->num_channels = ARRAY_SIZE(adis16204_channels);
519 indio_dev->modes = INDIO_DIRECT_MODE;
521 ret = adis16204_configure_ring(indio_dev);
525 ret = iio_buffer_register(indio_dev,
529 printk(KERN_ERR "failed to initialize the ring\n");
530 goto error_unreg_ring_funcs;
534 ret = adis16204_probe_trigger(indio_dev);
536 goto error_uninitialize_ring;
539 /* Get the device into a sane initial state */
540 ret = adis16204_initial_setup(indio_dev);
542 goto error_remove_trigger;
543 ret = iio_device_register(indio_dev);
545 goto error_remove_trigger;
549 error_remove_trigger:
550 adis16204_remove_trigger(indio_dev);
551 error_uninitialize_ring:
552 iio_buffer_unregister(indio_dev);
553 error_unreg_ring_funcs:
554 adis16204_unconfigure_ring(indio_dev);
556 iio_device_free(indio_dev);
561 static int __devexit adis16204_remove(struct spi_device *spi)
563 struct iio_dev *indio_dev = spi_get_drvdata(spi);
565 iio_device_unregister(indio_dev);
566 adis16204_remove_trigger(indio_dev);
567 iio_buffer_unregister(indio_dev);
568 adis16204_unconfigure_ring(indio_dev);
569 iio_device_free(indio_dev);
574 static struct spi_driver adis16204_driver = {
577 .owner = THIS_MODULE,
579 .probe = adis16204_probe,
580 .remove = __devexit_p(adis16204_remove),
582 module_spi_driver(adis16204_driver);
584 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
585 MODULE_DESCRIPTION("ADIS16204 High-g Digital Impact Sensor and Recorder");
586 MODULE_LICENSE("GPL v2");
587 MODULE_ALIAS("spi:adis16204");