2 * ADIS16201 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>
20 #include "../buffer.h"
22 #include "adis16201.h"
35 * adis16201_spi_write_reg_8() - write single byte to a register
36 * @dev: device associated with child of actual device (iio_dev or iio_trig)
37 * @reg_address: the address of the register to be written
38 * @val: the value to write
40 static int adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
45 struct adis16201_state *st = iio_priv(indio_dev);
47 mutex_lock(&st->buf_lock);
48 st->tx[0] = ADIS16201_WRITE_REG(reg_address);
51 ret = spi_write(st->us, st->tx, 2);
52 mutex_unlock(&st->buf_lock);
58 * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
59 * @indio_dev: iio device associated with child of actual device
60 * @reg_address: the address of the lower of the two registers. Second register
61 * is assumed to have address one greater.
62 * @val: value to be written
64 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
69 struct spi_message msg;
70 struct adis16201_state *st = iio_priv(indio_dev);
71 struct spi_transfer xfers[] = {
84 mutex_lock(&st->buf_lock);
85 st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
86 st->tx[1] = value & 0xFF;
87 st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1);
88 st->tx[3] = (value >> 8) & 0xFF;
90 spi_message_init(&msg);
91 spi_message_add_tail(&xfers[0], &msg);
92 spi_message_add_tail(&xfers[1], &msg);
93 ret = spi_sync(st->us, &msg);
94 mutex_unlock(&st->buf_lock);
100 * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
101 * @indio_dev: iio device associated with child of actual device
102 * @reg_address: the address of the lower of the two registers. Second register
103 * is assumed to have address one greater.
104 * @val: somewhere to pass back the value read
106 static int adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
107 u8 lower_reg_address,
110 struct spi_message msg;
111 struct adis16201_state *st = iio_priv(indio_dev);
113 struct spi_transfer xfers[] = {
128 mutex_lock(&st->buf_lock);
129 st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
132 spi_message_init(&msg);
133 spi_message_add_tail(&xfers[0], &msg);
134 spi_message_add_tail(&xfers[1], &msg);
135 ret = spi_sync(st->us, &msg);
137 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
141 *val = (st->rx[0] << 8) | st->rx[1];
144 mutex_unlock(&st->buf_lock);
148 static int adis16201_reset(struct iio_dev *indio_dev)
151 struct adis16201_state *st = iio_priv(indio_dev);
153 ret = adis16201_spi_write_reg_8(indio_dev,
155 ADIS16201_GLOB_CMD_SW_RESET);
157 dev_err(&st->us->dev, "problem resetting device");
162 static ssize_t adis16201_write_reset(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t len)
171 ret = strtobool(buf, &res);
174 return adis16201_reset(dev_get_drvdata(dev));
177 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
182 ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
186 msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
187 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
189 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
191 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
193 ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
199 static int adis16201_check_status(struct iio_dev *indio_dev)
204 ret = adis16201_spi_read_reg_16(indio_dev,
205 ADIS16201_DIAG_STAT, &status);
207 dev_err(&indio_dev->dev, "Reading status failed\n");
214 if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
215 dev_err(&indio_dev->dev, "SPI failure\n");
216 if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
217 dev_err(&indio_dev->dev, "Flash update failed\n");
218 if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
219 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
220 if (status & ADIS16201_DIAG_STAT_POWER_LOW)
221 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
227 static int adis16201_self_test(struct iio_dev *indio_dev)
230 ret = adis16201_spi_write_reg_16(indio_dev,
232 ADIS16201_MSC_CTRL_SELF_TEST_EN);
234 dev_err(&indio_dev->dev, "problem starting self test");
238 ret = adis16201_check_status(indio_dev);
244 static int adis16201_initial_setup(struct iio_dev *indio_dev)
247 struct device *dev = &indio_dev->dev;
250 ret = adis16201_set_irq(indio_dev, false);
252 dev_err(dev, "disable irq failed");
257 ret = adis16201_self_test(indio_dev);
259 dev_err(dev, "self test failure");
263 /* Read status register to check the result */
264 ret = adis16201_check_status(indio_dev);
266 adis16201_reset(indio_dev);
267 dev_err(dev, "device not playing ball -> reset");
268 msleep(ADIS16201_STARTUP_DELAY);
269 ret = adis16201_check_status(indio_dev);
271 dev_err(dev, "giving up");
280 static u8 adis16201_addresses[7][2] = {
281 [in_supply] = { ADIS16201_SUPPLY_OUT, },
282 [temp] = { ADIS16201_TEMP_OUT },
283 [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
284 [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
285 [in_aux] = { ADIS16201_AUX_ADC },
286 [incli_x] = { ADIS16201_XINCL_OUT },
287 [incli_y] = { ADIS16201_YINCL_OUT },
290 static int adis16201_read_raw(struct iio_dev *indio_dev,
291 struct iio_chan_spec const *chan,
302 mutex_lock(&indio_dev->mlock);
303 addr = adis16201_addresses[chan->address][0];
304 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
306 mutex_unlock(&indio_dev->mlock);
310 if (val16 & ADIS16201_ERROR_ACTIVE) {
311 ret = adis16201_check_status(indio_dev);
313 mutex_unlock(&indio_dev->mlock);
317 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
318 if (chan->scan_type.sign == 's')
319 val16 = (s16)(val16 <<
320 (16 - chan->scan_type.realbits)) >>
321 (16 - chan->scan_type.realbits);
323 mutex_unlock(&indio_dev->mlock);
325 case IIO_CHAN_INFO_SCALE:
326 switch (chan->type) {
329 if (chan->channel == 0)
333 return IIO_VAL_INT_PLUS_MICRO;
337 return IIO_VAL_INT_PLUS_MICRO;
341 return IIO_VAL_INT_PLUS_MICRO;
345 return IIO_VAL_INT_PLUS_MICRO;
350 case IIO_CHAN_INFO_OFFSET:
353 case IIO_CHAN_INFO_CALIBBIAS:
354 switch (chan->type) {
364 mutex_lock(&indio_dev->mlock);
365 addr = adis16201_addresses[chan->address][1];
366 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
368 mutex_unlock(&indio_dev->mlock);
371 val16 &= (1 << bits) - 1;
372 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
374 mutex_unlock(&indio_dev->mlock);
380 static int adis16201_write_raw(struct iio_dev *indio_dev,
381 struct iio_chan_spec const *chan,
390 case IIO_CHAN_INFO_CALIBBIAS:
391 switch (chan->type) {
401 val16 = val & ((1 << bits) - 1);
402 addr = adis16201_addresses[chan->address][1];
403 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
408 static struct iio_chan_spec adis16201_channels[] = {
409 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, "supply", 0, 0,
410 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
411 in_supply, ADIS16201_SCAN_SUPPLY,
412 IIO_ST('u', 12, 16, 0), 0),
413 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
414 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
415 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
416 temp, ADIS16201_SCAN_TEMP,
417 IIO_ST('u', 12, 16, 0), 0),
418 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
419 IIO_CHAN_INFO_SCALE_SHARED_BIT |
420 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
421 accel_x, ADIS16201_SCAN_ACC_X,
422 IIO_ST('s', 14, 16, 0), 0),
423 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
424 IIO_CHAN_INFO_SCALE_SHARED_BIT |
425 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
426 accel_y, ADIS16201_SCAN_ACC_Y,
427 IIO_ST('s', 14, 16, 0), 0),
428 IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 1, 0,
429 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
430 in_aux, ADIS16201_SCAN_AUX_ADC,
431 IIO_ST('u', 12, 16, 0), 0),
432 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
433 IIO_CHAN_INFO_SCALE_SHARED_BIT |
434 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
435 incli_x, ADIS16201_SCAN_INCLI_X,
436 IIO_ST('s', 14, 16, 0), 0),
437 IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
438 IIO_CHAN_INFO_SCALE_SHARED_BIT |
439 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
440 incli_y, ADIS16201_SCAN_INCLI_Y,
441 IIO_ST('s', 14, 16, 0), 0),
442 IIO_CHAN_SOFT_TIMESTAMP(7)
445 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0);
447 static struct attribute *adis16201_attributes[] = {
448 &iio_dev_attr_reset.dev_attr.attr,
452 static const struct attribute_group adis16201_attribute_group = {
453 .attrs = adis16201_attributes,
456 static const struct iio_info adis16201_info = {
457 .attrs = &adis16201_attribute_group,
458 .read_raw = &adis16201_read_raw,
459 .write_raw = &adis16201_write_raw,
460 .driver_module = THIS_MODULE,
463 static int __devinit adis16201_probe(struct spi_device *spi)
466 struct adis16201_state *st;
467 struct iio_dev *indio_dev;
469 /* setup the industrialio driver allocated elements */
470 indio_dev = iio_allocate_device(sizeof(*st));
471 if (indio_dev == NULL) {
475 st = iio_priv(indio_dev);
476 /* this is only used for removal purposes */
477 spi_set_drvdata(spi, indio_dev);
480 mutex_init(&st->buf_lock);
482 indio_dev->name = spi->dev.driver->name;
483 indio_dev->dev.parent = &spi->dev;
484 indio_dev->info = &adis16201_info;
486 indio_dev->channels = adis16201_channels;
487 indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
488 indio_dev->modes = INDIO_DIRECT_MODE;
490 ret = adis16201_configure_ring(indio_dev);
494 ret = iio_buffer_register(indio_dev,
496 ARRAY_SIZE(adis16201_channels));
498 printk(KERN_ERR "failed to initialize the ring\n");
499 goto error_unreg_ring_funcs;
503 ret = adis16201_probe_trigger(indio_dev);
505 goto error_uninitialize_ring;
508 /* Get the device into a sane initial state */
509 ret = adis16201_initial_setup(indio_dev);
511 goto error_remove_trigger;
513 ret = iio_device_register(indio_dev);
515 goto error_remove_trigger;
518 error_remove_trigger:
519 adis16201_remove_trigger(indio_dev);
520 error_uninitialize_ring:
521 iio_buffer_unregister(indio_dev);
522 error_unreg_ring_funcs:
523 adis16201_unconfigure_ring(indio_dev);
525 iio_free_device(indio_dev);
530 static int adis16201_remove(struct spi_device *spi)
532 struct iio_dev *indio_dev = spi_get_drvdata(spi);
534 iio_device_unregister(indio_dev);
535 adis16201_remove_trigger(indio_dev);
536 iio_buffer_unregister(indio_dev);
537 adis16201_unconfigure_ring(indio_dev);
538 iio_free_device(indio_dev);
543 static struct spi_driver adis16201_driver = {
546 .owner = THIS_MODULE,
548 .probe = adis16201_probe,
549 .remove = __devexit_p(adis16201_remove),
551 module_spi_driver(adis16201_driver);
553 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
554 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
555 MODULE_LICENSE("GPL v2");
556 MODULE_ALIAS("spi:adis16201");