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>
20 #include "inclinometer.h"
21 #include "../ring_generic.h"
22 #include "../adc/adc.h"
24 #include "adis16203.h"
26 #define DRIVER_NAME "adis16203"
29 * adis16203_spi_write_reg_8() - write single byte to a register
30 * @indio_dev: iio device associated with child of actual device
31 * @reg_address: the address of the register to be written
32 * @val: the value to write
34 static int adis16203_spi_write_reg_8(struct iio_dev *indio_dev,
39 struct adis16203_state *st = iio_priv(indio_dev);
41 mutex_lock(&st->buf_lock);
42 st->tx[0] = ADIS16203_WRITE_REG(reg_address);
45 ret = spi_write(st->us, st->tx, 2);
46 mutex_unlock(&st->buf_lock);
52 * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
53 * @indio_dev: iio device associated with child of actual device
54 * @reg_address: the address of the lower of the two registers. Second register
55 * is assumed to have address one greater.
56 * @val: value to be written
58 static int adis16203_spi_write_reg_16(struct iio_dev *indio_dev,
63 struct spi_message msg;
64 struct adis16203_state *st = iio_priv(indio_dev);
65 struct spi_transfer xfers[] = {
78 mutex_lock(&st->buf_lock);
79 st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
80 st->tx[1] = value & 0xFF;
81 st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
82 st->tx[3] = (value >> 8) & 0xFF;
84 spi_message_init(&msg);
85 spi_message_add_tail(&xfers[0], &msg);
86 spi_message_add_tail(&xfers[1], &msg);
87 ret = spi_sync(st->us, &msg);
88 mutex_unlock(&st->buf_lock);
94 * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
95 * @indio_dev: iio device associated with child of actual device
96 * @reg_address: the address of the lower of the two registers. Second register
97 * is assumed to have address one greater.
98 * @val: somewhere to pass back the value read
100 static int adis16203_spi_read_reg_16(struct iio_dev *indio_dev,
101 u8 lower_reg_address,
104 struct spi_message msg;
105 struct adis16203_state *st = iio_priv(indio_dev);
107 struct spi_transfer xfers[] = {
122 mutex_lock(&st->buf_lock);
123 st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
126 spi_message_init(&msg);
127 spi_message_add_tail(&xfers[0], &msg);
128 spi_message_add_tail(&xfers[1], &msg);
129 ret = spi_sync(st->us, &msg);
131 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
135 *val = (st->rx[0] << 8) | st->rx[1];
138 mutex_unlock(&st->buf_lock);
142 static int adis16203_check_status(struct iio_dev *indio_dev)
147 ret = adis16203_spi_read_reg_16(indio_dev,
151 dev_err(&indio_dev->dev, "Reading status failed\n");
156 if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
157 dev_err(&indio_dev->dev, "Self test failure\n");
158 if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
159 dev_err(&indio_dev->dev, "SPI failure\n");
160 if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
161 dev_err(&indio_dev->dev, "Flash update failed\n");
162 if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
163 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
164 if (status & ADIS16203_DIAG_STAT_POWER_LOW)
165 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
171 static int adis16203_reset(struct iio_dev *indio_dev)
174 ret = adis16203_spi_write_reg_8(indio_dev,
176 ADIS16203_GLOB_CMD_SW_RESET);
178 dev_err(&indio_dev->dev, "problem resetting device");
183 static ssize_t adis16203_write_reset(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t len)
187 struct iio_dev *indio_dev = dev_get_drvdata(dev);
194 return adis16203_reset(indio_dev);
199 int adis16203_set_irq(struct iio_dev *indio_dev, bool enable)
204 ret = adis16203_spi_read_reg_16(indio_dev, ADIS16203_MSC_CTRL, &msc);
208 msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
209 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
211 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
213 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
215 ret = adis16203_spi_write_reg_16(indio_dev, ADIS16203_MSC_CTRL, msc);
221 static int adis16203_self_test(struct iio_dev *indio_dev)
224 ret = adis16203_spi_write_reg_16(indio_dev,
226 ADIS16203_MSC_CTRL_SELF_TEST_EN);
228 dev_err(&indio_dev->dev, "problem starting self test");
232 adis16203_check_status(indio_dev);
238 static int adis16203_initial_setup(struct iio_dev *indio_dev)
243 ret = adis16203_set_irq(indio_dev, false);
245 dev_err(&indio_dev->dev, "disable irq failed");
250 ret = adis16203_self_test(indio_dev);
252 dev_err(&indio_dev->dev, "self test failure");
256 /* Read status register to check the result */
257 ret = adis16203_check_status(indio_dev);
259 adis16203_reset(indio_dev);
260 dev_err(&indio_dev->dev, "device not playing ball -> reset");
261 msleep(ADIS16203_STARTUP_DELAY);
262 ret = adis16203_check_status(indio_dev);
264 dev_err(&indio_dev->dev, "giving up");
273 enum adis16203_chan {
281 static u8 adis16203_addresses[5][2] = {
282 [in_supply] = { ADIS16203_SUPPLY_OUT },
283 [in_aux] = { ADIS16203_AUX_ADC },
284 [incli_x] = { ADIS16203_XINCL_OUT, ADIS16203_INCL_NULL},
285 [incli_y] = { ADIS16203_YINCL_OUT },
286 [temp] = { ADIS16203_TEMP_OUT }
289 static int adis16203_write_raw(struct iio_dev *indio_dev,
290 struct iio_chan_spec const *chan,
295 /* currently only one writable parameter which keeps this simple */
296 u8 addr = adis16203_addresses[chan->address][1];
297 return adis16203_spi_write_reg_16(indio_dev, addr, val & 0x3FFF);
300 static int adis16203_read_raw(struct iio_dev *indio_dev,
301 struct iio_chan_spec const *chan,
311 mutex_lock(&indio_dev->mlock);
312 addr = adis16203_addresses[chan->address][0];
313 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
315 mutex_unlock(&indio_dev->mlock);
319 if (val16 & ADIS16203_ERROR_ACTIVE) {
320 ret = adis16203_check_status(indio_dev);
322 mutex_unlock(&indio_dev->mlock);
326 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
327 if (chan->scan_type.sign == 's')
328 val16 = (s16)(val16 <<
329 (16 - chan->scan_type.realbits)) >>
330 (16 - chan->scan_type.realbits);
332 mutex_unlock(&indio_dev->mlock);
334 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
335 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
336 switch (chan->type) {
339 if (chan->channel == 0)
343 return IIO_VAL_INT_PLUS_MICRO;
347 return IIO_VAL_INT_PLUS_MICRO;
351 return IIO_VAL_INT_PLUS_MICRO;
355 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
358 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
360 mutex_lock(&indio_dev->mlock);
361 addr = adis16203_addresses[chan->address][1];
362 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
364 mutex_unlock(&indio_dev->mlock);
367 val16 &= (1 << bits) - 1;
368 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
370 mutex_unlock(&indio_dev->mlock);
377 static struct iio_chan_spec adis16203_channels[] = {
378 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
379 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
380 in_supply, ADIS16203_SCAN_SUPPLY,
381 IIO_ST('u', 12, 16, 0), 0),
382 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
383 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
384 in_aux, ADIS16203_SCAN_AUX_ADC,
385 IIO_ST('u', 12, 16, 0), 0),
386 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
387 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
388 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
389 incli_x, ADIS16203_SCAN_INCLI_X,
390 IIO_ST('s', 14, 16, 0), 0),
391 /* Fixme: Not what it appears to be - see data sheet */
392 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
393 (1 << IIO_CHAN_INFO_SCALE_SHARED),
394 incli_y, ADIS16203_SCAN_INCLI_Y,
395 IIO_ST('s', 14, 16, 0), 0),
396 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
397 (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
398 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
399 temp, ADIS16203_SCAN_TEMP,
400 IIO_ST('u', 12, 16, 0), 0),
401 IIO_CHAN_SOFT_TIMESTAMP(5),
404 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16203_write_reset, 0);
406 static struct attribute *adis16203_attributes[] = {
407 &iio_dev_attr_reset.dev_attr.attr,
411 static const struct attribute_group adis16203_attribute_group = {
412 .attrs = adis16203_attributes,
415 static const struct iio_info adis16203_info = {
416 .attrs = &adis16203_attribute_group,
417 .read_raw = &adis16203_read_raw,
418 .write_raw = &adis16203_write_raw,
419 .driver_module = THIS_MODULE,
422 static int __devinit adis16203_probe(struct spi_device *spi)
424 int ret, regdone = 0;
425 struct iio_dev *indio_dev;
426 struct adis16203_state *st;
428 /* setup the industrialio driver allocated elements */
429 indio_dev = iio_allocate_device(sizeof(*st));
430 if (indio_dev == NULL) {
434 st = iio_priv(indio_dev);
435 /* this is only used for removal purposes */
436 spi_set_drvdata(spi, indio_dev);
438 mutex_init(&st->buf_lock);
440 indio_dev->name = spi->dev.driver->name;
441 indio_dev->dev.parent = &spi->dev;
442 indio_dev->channels = adis16203_channels;
443 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
444 indio_dev->info = &adis16203_info;
445 indio_dev->modes = INDIO_DIRECT_MODE;
447 ret = adis16203_configure_ring(indio_dev);
451 ret = iio_device_register(indio_dev);
453 goto error_unreg_ring_funcs;
456 ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
458 ARRAY_SIZE(adis16203_channels));
460 printk(KERN_ERR "failed to initialize the ring\n");
461 goto error_unreg_ring_funcs;
465 ret = adis16203_probe_trigger(indio_dev);
467 goto error_uninitialize_ring;
470 /* Get the device into a sane initial state */
471 ret = adis16203_initial_setup(indio_dev);
473 goto error_remove_trigger;
476 error_remove_trigger:
477 adis16203_remove_trigger(indio_dev);
478 error_uninitialize_ring:
479 iio_ring_buffer_unregister(indio_dev->ring);
480 error_unreg_ring_funcs:
481 adis16203_unconfigure_ring(indio_dev);
484 iio_device_unregister(indio_dev);
486 iio_free_device(indio_dev);
491 static int adis16203_remove(struct spi_device *spi)
493 struct iio_dev *indio_dev = spi_get_drvdata(spi);
495 adis16203_remove_trigger(indio_dev);
496 iio_ring_buffer_unregister(indio_dev->ring);
497 iio_device_unregister(indio_dev);
498 adis16203_unconfigure_ring(indio_dev);
503 static struct spi_driver adis16203_driver = {
506 .owner = THIS_MODULE,
508 .probe = adis16203_probe,
509 .remove = __devexit_p(adis16203_remove),
512 static __init int adis16203_init(void)
514 return spi_register_driver(&adis16203_driver);
516 module_init(adis16203_init);
518 static __exit void adis16203_exit(void)
520 spi_unregister_driver(&adis16203_driver);
522 module_exit(adis16203_exit);
524 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
525 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
526 MODULE_LICENSE("GPL v2");