2 * ADIS16203 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>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
22 #include "adis16203.h"
24 #define DRIVER_NAME "adis16203"
27 * adis16203_spi_write_reg_8() - write single byte to a register
28 * @indio_dev: iio device associated with child of actual device
29 * @reg_address: the address of the register to be written
30 * @val: the value to write
32 static int adis16203_spi_write_reg_8(struct iio_dev *indio_dev,
37 struct adis16203_state *st = iio_priv(indio_dev);
39 mutex_lock(&st->buf_lock);
40 st->tx[0] = ADIS16203_WRITE_REG(reg_address);
43 ret = spi_write(st->us, st->tx, 2);
44 mutex_unlock(&st->buf_lock);
50 * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
51 * @indio_dev: iio device associated with child of actual device
52 * @reg_address: the address of the lower of the two registers. Second register
53 * is assumed to have address one greater.
54 * @val: value to be written
56 static int adis16203_spi_write_reg_16(struct iio_dev *indio_dev,
61 struct spi_message msg;
62 struct adis16203_state *st = iio_priv(indio_dev);
63 struct spi_transfer xfers[] = {
76 mutex_lock(&st->buf_lock);
77 st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
78 st->tx[1] = value & 0xFF;
79 st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
80 st->tx[3] = (value >> 8) & 0xFF;
82 spi_message_init(&msg);
83 spi_message_add_tail(&xfers[0], &msg);
84 spi_message_add_tail(&xfers[1], &msg);
85 ret = spi_sync(st->us, &msg);
86 mutex_unlock(&st->buf_lock);
92 * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
93 * @indio_dev: iio device associated with child of actual device
94 * @reg_address: the address of the lower of the two registers. Second register
95 * is assumed to have address one greater.
96 * @val: somewhere to pass back the value read
98 static int adis16203_spi_read_reg_16(struct iio_dev *indio_dev,
102 struct spi_message msg;
103 struct adis16203_state *st = iio_priv(indio_dev);
105 struct spi_transfer xfers[] = {
120 mutex_lock(&st->buf_lock);
121 st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
124 spi_message_init(&msg);
125 spi_message_add_tail(&xfers[0], &msg);
126 spi_message_add_tail(&xfers[1], &msg);
127 ret = spi_sync(st->us, &msg);
129 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
133 *val = (st->rx[0] << 8) | st->rx[1];
136 mutex_unlock(&st->buf_lock);
140 static int adis16203_check_status(struct iio_dev *indio_dev)
145 ret = adis16203_spi_read_reg_16(indio_dev,
149 dev_err(&indio_dev->dev, "Reading status failed\n");
154 if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
155 dev_err(&indio_dev->dev, "Self test failure\n");
156 if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
157 dev_err(&indio_dev->dev, "SPI failure\n");
158 if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
159 dev_err(&indio_dev->dev, "Flash update failed\n");
160 if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
161 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
162 if (status & ADIS16203_DIAG_STAT_POWER_LOW)
163 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
169 static int adis16203_reset(struct iio_dev *indio_dev)
172 ret = adis16203_spi_write_reg_8(indio_dev,
174 ADIS16203_GLOB_CMD_SW_RESET);
176 dev_err(&indio_dev->dev, "problem resetting device");
181 int adis16203_set_irq(struct iio_dev *indio_dev, bool enable)
186 ret = adis16203_spi_read_reg_16(indio_dev, ADIS16203_MSC_CTRL, &msc);
190 msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
191 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
193 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
195 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
197 ret = adis16203_spi_write_reg_16(indio_dev, ADIS16203_MSC_CTRL, msc);
203 static int adis16203_self_test(struct iio_dev *indio_dev)
206 ret = adis16203_spi_write_reg_16(indio_dev,
208 ADIS16203_MSC_CTRL_SELF_TEST_EN);
210 dev_err(&indio_dev->dev, "problem starting self test");
214 adis16203_check_status(indio_dev);
220 static int adis16203_initial_setup(struct iio_dev *indio_dev)
225 ret = adis16203_set_irq(indio_dev, false);
227 dev_err(&indio_dev->dev, "disable irq failed");
232 ret = adis16203_self_test(indio_dev);
234 dev_err(&indio_dev->dev, "self test failure");
238 /* Read status register to check the result */
239 ret = adis16203_check_status(indio_dev);
241 adis16203_reset(indio_dev);
242 dev_err(&indio_dev->dev, "device not playing ball -> reset");
243 msleep(ADIS16203_STARTUP_DELAY);
244 ret = adis16203_check_status(indio_dev);
246 dev_err(&indio_dev->dev, "giving up");
255 enum adis16203_chan {
263 static u8 adis16203_addresses[5][2] = {
264 [in_supply] = { ADIS16203_SUPPLY_OUT },
265 [in_aux] = { ADIS16203_AUX_ADC },
266 [incli_x] = { ADIS16203_XINCL_OUT, ADIS16203_INCL_NULL},
267 [incli_y] = { ADIS16203_YINCL_OUT },
268 [temp] = { ADIS16203_TEMP_OUT }
271 static int adis16203_write_raw(struct iio_dev *indio_dev,
272 struct iio_chan_spec const *chan,
277 /* currently only one writable parameter which keeps this simple */
278 u8 addr = adis16203_addresses[chan->address][1];
279 return adis16203_spi_write_reg_16(indio_dev, addr, val & 0x3FFF);
282 static int adis16203_read_raw(struct iio_dev *indio_dev,
283 struct iio_chan_spec const *chan,
292 case IIO_CHAN_INFO_RAW:
293 mutex_lock(&indio_dev->mlock);
294 addr = adis16203_addresses[chan->address][0];
295 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
297 mutex_unlock(&indio_dev->mlock);
301 if (val16 & ADIS16203_ERROR_ACTIVE) {
302 ret = adis16203_check_status(indio_dev);
304 mutex_unlock(&indio_dev->mlock);
308 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
309 if (chan->scan_type.sign == 's')
310 val16 = (s16)(val16 <<
311 (16 - chan->scan_type.realbits)) >>
312 (16 - chan->scan_type.realbits);
314 mutex_unlock(&indio_dev->mlock);
316 case IIO_CHAN_INFO_SCALE:
317 switch (chan->type) {
319 if (chan->channel == 0) {
321 *val2 = 220000; /* 1.22 mV */
324 *val2 = 610000; /* 0.61 mV */
326 return IIO_VAL_INT_PLUS_MICRO;
328 *val = -470; /* -0.47 C */
330 return IIO_VAL_INT_PLUS_MICRO;
333 *val2 = 25000; /* 0.025 degree */
334 return IIO_VAL_INT_PLUS_MICRO;
338 case IIO_CHAN_INFO_OFFSET:
339 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
341 case IIO_CHAN_INFO_CALIBBIAS:
343 mutex_lock(&indio_dev->mlock);
344 addr = adis16203_addresses[chan->address][1];
345 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
347 mutex_unlock(&indio_dev->mlock);
350 val16 &= (1 << bits) - 1;
351 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
353 mutex_unlock(&indio_dev->mlock);
360 static const struct iio_chan_spec adis16203_channels[] = {
365 .extend_name = "supply",
366 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
367 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
368 .address = in_supply,
369 .scan_index = ADIS16203_SCAN_SUPPLY,
379 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
380 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
382 .scan_index = ADIS16203_SCAN_AUX_ADC,
391 .channel2 = IIO_MOD_X,
392 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
393 IIO_CHAN_INFO_SCALE_SHARED_BIT |
394 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
396 .scan_index = ADIS16203_SCAN_INCLI_X,
402 }, { /* Fixme: Not what it appears to be - see data sheet */
405 .channel2 = IIO_MOD_Y,
406 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
407 IIO_CHAN_INFO_SCALE_SHARED_BIT,
409 .scan_index = ADIS16203_SCAN_INCLI_Y,
419 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
420 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
421 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
423 .scan_index = ADIS16203_SCAN_TEMP,
430 IIO_CHAN_SOFT_TIMESTAMP(5),
433 static const struct iio_info adis16203_info = {
434 .read_raw = &adis16203_read_raw,
435 .write_raw = &adis16203_write_raw,
436 .driver_module = THIS_MODULE,
439 static int __devinit adis16203_probe(struct spi_device *spi)
442 struct iio_dev *indio_dev;
443 struct adis16203_state *st;
445 /* setup the industrialio driver allocated elements */
446 indio_dev = iio_device_alloc(sizeof(*st));
447 if (indio_dev == NULL) {
451 st = iio_priv(indio_dev);
452 /* this is only used for removal purposes */
453 spi_set_drvdata(spi, indio_dev);
455 mutex_init(&st->buf_lock);
457 indio_dev->name = spi->dev.driver->name;
458 indio_dev->dev.parent = &spi->dev;
459 indio_dev->channels = adis16203_channels;
460 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
461 indio_dev->info = &adis16203_info;
462 indio_dev->modes = INDIO_DIRECT_MODE;
464 ret = adis16203_configure_ring(indio_dev);
468 ret = iio_buffer_register(indio_dev,
470 ARRAY_SIZE(adis16203_channels));
472 printk(KERN_ERR "failed to initialize the ring\n");
473 goto error_unreg_ring_funcs;
477 ret = adis16203_probe_trigger(indio_dev);
479 goto error_uninitialize_ring;
482 /* Get the device into a sane initial state */
483 ret = adis16203_initial_setup(indio_dev);
485 goto error_remove_trigger;
487 ret = iio_device_register(indio_dev);
489 goto error_remove_trigger;
493 error_remove_trigger:
494 adis16203_remove_trigger(indio_dev);
495 error_uninitialize_ring:
496 iio_buffer_unregister(indio_dev);
497 error_unreg_ring_funcs:
498 adis16203_unconfigure_ring(indio_dev);
500 iio_device_free(indio_dev);
505 static int __devexit adis16203_remove(struct spi_device *spi)
507 struct iio_dev *indio_dev = spi_get_drvdata(spi);
509 iio_device_unregister(indio_dev);
510 adis16203_remove_trigger(indio_dev);
511 iio_buffer_unregister(indio_dev);
512 adis16203_unconfigure_ring(indio_dev);
513 iio_device_free(indio_dev);
518 static struct spi_driver adis16203_driver = {
521 .owner = THIS_MODULE,
523 .probe = adis16203_probe,
524 .remove = __devexit_p(adis16203_remove),
526 module_spi_driver(adis16203_driver);
528 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
529 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
530 MODULE_LICENSE("GPL v2");
531 MODULE_ALIAS("spi:adis16203");