2 * ADIS16209 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/list.h>
17 #include <linux/module.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
23 #include "adis16209.h"
25 #define DRIVER_NAME "adis16209"
28 * adis16209_spi_write_reg_8() - write single byte to a register
29 * @indio_dev: iio device associated with actual device
30 * @reg_address: the address of the register to be written
31 * @val: the value to write
33 static int adis16209_spi_write_reg_8(struct iio_dev *indio_dev,
38 struct adis16209_state *st = iio_priv(indio_dev);
40 mutex_lock(&st->buf_lock);
41 st->tx[0] = ADIS16209_WRITE_REG(reg_address);
44 ret = spi_write(st->us, st->tx, 2);
45 mutex_unlock(&st->buf_lock);
51 * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
52 * @indio_dev: iio device associated actual device
53 * @reg_address: the address of the lower of the two registers. Second register
54 * is assumed to have address one greater.
55 * @val: value to be written
57 static int adis16209_spi_write_reg_16(struct iio_dev *indio_dev,
62 struct spi_message msg;
63 struct adis16209_state *st = iio_priv(indio_dev);
64 struct spi_transfer xfers[] = {
79 mutex_lock(&st->buf_lock);
80 st->tx[0] = ADIS16209_WRITE_REG(lower_reg_address);
81 st->tx[1] = value & 0xFF;
82 st->tx[2] = ADIS16209_WRITE_REG(lower_reg_address + 1);
83 st->tx[3] = (value >> 8) & 0xFF;
85 spi_message_init(&msg);
86 spi_message_add_tail(&xfers[0], &msg);
87 spi_message_add_tail(&xfers[1], &msg);
88 ret = spi_sync(st->us, &msg);
89 mutex_unlock(&st->buf_lock);
95 * adis16209_spi_read_reg_16() - read 2 bytes from a 16-bit register
96 * @indio_dev: iio device associated with device
97 * @reg_address: the address of the lower of the two registers. Second register
98 * is assumed to have address one greater.
99 * @val: somewhere to pass back the value read
101 static int adis16209_spi_read_reg_16(struct iio_dev *indio_dev,
102 u8 lower_reg_address,
105 struct spi_message msg;
106 struct adis16209_state *st = iio_priv(indio_dev);
108 struct spi_transfer xfers[] = {
123 mutex_lock(&st->buf_lock);
124 st->tx[0] = ADIS16209_READ_REG(lower_reg_address);
127 spi_message_init(&msg);
128 spi_message_add_tail(&xfers[0], &msg);
129 spi_message_add_tail(&xfers[1], &msg);
130 ret = spi_sync(st->us, &msg);
132 dev_err(&st->us->dev,
133 "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 adis16209_reset(struct iio_dev *indio_dev)
147 ret = adis16209_spi_write_reg_8(indio_dev,
149 ADIS16209_GLOB_CMD_SW_RESET);
151 dev_err(&indio_dev->dev, "problem resetting device");
156 int adis16209_set_irq(struct iio_dev *indio_dev, bool enable)
161 ret = adis16209_spi_read_reg_16(indio_dev, ADIS16209_MSC_CTRL, &msc);
165 msc |= ADIS16209_MSC_CTRL_ACTIVE_HIGH;
166 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2;
168 msc |= ADIS16209_MSC_CTRL_DATA_RDY_EN;
170 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_EN;
172 ret = adis16209_spi_write_reg_16(indio_dev, ADIS16209_MSC_CTRL, msc);
178 static int adis16209_check_status(struct iio_dev *indio_dev)
183 ret = adis16209_spi_read_reg_16(indio_dev,
184 ADIS16209_DIAG_STAT, &status);
186 dev_err(&indio_dev->dev, "Reading status failed\n");
191 if (status & ADIS16209_DIAG_STAT_SELFTEST_FAIL)
192 dev_err(&indio_dev->dev, "Self test failure\n");
193 if (status & ADIS16209_DIAG_STAT_SPI_FAIL)
194 dev_err(&indio_dev->dev, "SPI failure\n");
195 if (status & ADIS16209_DIAG_STAT_FLASH_UPT)
196 dev_err(&indio_dev->dev, "Flash update failed\n");
197 if (status & ADIS16209_DIAG_STAT_POWER_HIGH)
198 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
199 if (status & ADIS16209_DIAG_STAT_POWER_LOW)
200 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
206 static int adis16209_self_test(struct iio_dev *indio_dev)
209 ret = adis16209_spi_write_reg_16(indio_dev,
211 ADIS16209_MSC_CTRL_SELF_TEST_EN);
213 dev_err(&indio_dev->dev, "problem starting self test");
217 adis16209_check_status(indio_dev);
223 static int adis16209_initial_setup(struct iio_dev *indio_dev)
228 ret = adis16209_set_irq(indio_dev, false);
230 dev_err(&indio_dev->dev, "disable irq failed");
235 ret = adis16209_self_test(indio_dev);
237 dev_err(&indio_dev->dev, "self test failure");
241 /* Read status register to check the result */
242 ret = adis16209_check_status(indio_dev);
244 adis16209_reset(indio_dev);
245 dev_err(&indio_dev->dev, "device not playing ball -> reset");
246 msleep(ADIS16209_STARTUP_DELAY);
247 ret = adis16209_check_status(indio_dev);
249 dev_err(&indio_dev->dev, "giving up");
258 enum adis16209_chan {
269 static const u8 adis16209_addresses[8][2] = {
270 [in_supply] = { ADIS16209_SUPPLY_OUT },
271 [in_aux] = { ADIS16209_AUX_ADC },
272 [accel_x] = { ADIS16209_XACCL_OUT, ADIS16209_XACCL_NULL },
273 [accel_y] = { ADIS16209_YACCL_OUT, ADIS16209_YACCL_NULL },
274 [incli_x] = { ADIS16209_XINCL_OUT, ADIS16209_XINCL_NULL },
275 [incli_y] = { ADIS16209_YINCL_OUT, ADIS16209_YINCL_NULL },
276 [rot] = { ADIS16209_ROT_OUT },
277 [temp] = { ADIS16209_TEMP_OUT },
280 static int adis16209_write_raw(struct iio_dev *indio_dev,
281 struct iio_chan_spec const *chan,
290 case IIO_CHAN_INFO_CALIBBIAS:
291 switch (chan->type) {
299 val16 = val & ((1 << bits) - 1);
300 addr = adis16209_addresses[chan->address][1];
301 return adis16209_spi_write_reg_16(indio_dev, addr, val16);
306 static int adis16209_read_raw(struct iio_dev *indio_dev,
307 struct iio_chan_spec const *chan,
317 case IIO_CHAN_INFO_RAW:
318 mutex_lock(&indio_dev->mlock);
319 addr = adis16209_addresses[chan->address][0];
320 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
322 mutex_unlock(&indio_dev->mlock);
326 if (val16 & ADIS16209_ERROR_ACTIVE) {
327 ret = adis16209_check_status(indio_dev);
329 mutex_unlock(&indio_dev->mlock);
333 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
334 if (chan->scan_type.sign == 's')
335 val16 = (s16)(val16 <<
336 (16 - chan->scan_type.realbits)) >>
337 (16 - chan->scan_type.realbits);
339 mutex_unlock(&indio_dev->mlock);
341 case IIO_CHAN_INFO_SCALE:
342 switch (chan->type) {
345 if (chan->channel == 0)
349 return IIO_VAL_INT_PLUS_MICRO;
353 return IIO_VAL_INT_PLUS_MICRO;
357 return IIO_VAL_INT_PLUS_MICRO;
361 return IIO_VAL_INT_PLUS_MICRO;
366 case IIO_CHAN_INFO_OFFSET:
369 case IIO_CHAN_INFO_CALIBBIAS:
370 switch (chan->type) {
377 mutex_lock(&indio_dev->mlock);
378 addr = adis16209_addresses[chan->address][1];
379 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
381 mutex_unlock(&indio_dev->mlock);
384 val16 &= (1 << bits) - 1;
385 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
387 mutex_unlock(&indio_dev->mlock);
393 static const struct iio_chan_spec adis16209_channels[] = {
398 .extend_name = "supply",
399 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
400 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
401 .address = in_supply,
402 .scan_index = ADIS16209_SCAN_SUPPLY,
412 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
413 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
414 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
416 .scan_index = ADIS16209_SCAN_TEMP,
425 .channel2 = IIO_MOD_X,
426 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
427 IIO_CHAN_INFO_SCALE_SHARED_BIT |
428 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
430 .scan_index = ADIS16209_SCAN_ACC_X,
439 .channel2 = IIO_MOD_Y,
440 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
441 IIO_CHAN_INFO_SCALE_SHARED_BIT |
442 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
444 .scan_index = ADIS16209_SCAN_ACC_Y,
454 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
455 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
457 .scan_index = ADIS16209_SCAN_AUX_ADC,
466 .channel2 = IIO_MOD_X,
467 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
468 IIO_CHAN_INFO_SCALE_SHARED_BIT,
470 .scan_index = ADIS16209_SCAN_INCLI_X,
479 .channel2 = IIO_MOD_Y,
480 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
481 IIO_CHAN_INFO_SCALE_SHARED_BIT,
483 .scan_index = ADIS16209_SCAN_INCLI_Y,
492 .channel2 = IIO_MOD_X,
493 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
495 .scan_index = ADIS16209_SCAN_ROT,
502 IIO_CHAN_SOFT_TIMESTAMP(8)
505 static const struct iio_info adis16209_info = {
506 .read_raw = &adis16209_read_raw,
507 .write_raw = &adis16209_write_raw,
508 .driver_module = THIS_MODULE,
511 static int __devinit adis16209_probe(struct spi_device *spi)
514 struct adis16209_state *st;
515 struct iio_dev *indio_dev;
517 /* setup the industrialio driver allocated elements */
518 indio_dev = iio_device_alloc(sizeof(*st));
519 if (indio_dev == NULL) {
523 st = iio_priv(indio_dev);
524 /* this is only used for removal purposes */
525 spi_set_drvdata(spi, indio_dev);
527 mutex_init(&st->buf_lock);
529 indio_dev->name = spi->dev.driver->name;
530 indio_dev->dev.parent = &spi->dev;
531 indio_dev->info = &adis16209_info;
532 indio_dev->channels = adis16209_channels;
533 indio_dev->num_channels = ARRAY_SIZE(adis16209_channels);
534 indio_dev->modes = INDIO_DIRECT_MODE;
536 ret = adis16209_configure_ring(indio_dev);
540 ret = iio_buffer_register(indio_dev,
542 ARRAY_SIZE(adis16209_channels));
544 printk(KERN_ERR "failed to initialize the ring\n");
545 goto error_unreg_ring_funcs;
549 ret = adis16209_probe_trigger(indio_dev);
551 goto error_uninitialize_ring;
554 /* Get the device into a sane initial state */
555 ret = adis16209_initial_setup(indio_dev);
557 goto error_remove_trigger;
558 ret = iio_device_register(indio_dev);
560 goto error_remove_trigger;
564 error_remove_trigger:
565 adis16209_remove_trigger(indio_dev);
566 error_uninitialize_ring:
567 iio_buffer_unregister(indio_dev);
568 error_unreg_ring_funcs:
569 adis16209_unconfigure_ring(indio_dev);
571 iio_device_free(indio_dev);
576 static int __devexit adis16209_remove(struct spi_device *spi)
578 struct iio_dev *indio_dev = spi_get_drvdata(spi);
580 iio_device_unregister(indio_dev);
581 adis16209_remove_trigger(indio_dev);
582 iio_buffer_unregister(indio_dev);
583 adis16209_unconfigure_ring(indio_dev);
584 iio_device_free(indio_dev);
589 static struct spi_driver adis16209_driver = {
592 .owner = THIS_MODULE,
594 .probe = adis16209_probe,
595 .remove = __devexit_p(adis16209_remove),
597 module_spi_driver(adis16209_driver);
599 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
600 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
601 MODULE_LICENSE("GPL v2");
602 MODULE_ALIAS("spi:adis16209");