11 #include <linux/device.h>
12 #include <linux/kernel.h>
14 #include <linux/slab.h>
16 #include <linux/module.h>
40 static int adis16201_spi_write_reg_8(
struct iio_dev *indio_dev,
51 ret = spi_write(st->
us, st->tx, 2);
64 static int adis16201_spi_write_reg_16(
struct iio_dev *indio_dev,
86 st->tx[1] = value & 0xFF;
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);
106 static int adis16201_spi_read_reg_16(
struct iio_dev *indio_dev,
107 u8 lower_reg_address,
132 spi_message_init(&
msg);
133 spi_message_add_tail(&xfers[0], &
msg);
134 spi_message_add_tail(&xfers[1], &
msg);
137 dev_err(&st->
us->dev,
"problem when reading 16 bit register 0x%02X",
141 *val = (st->
rx[0] << 8) | st->
rx[1];
148 static int adis16201_reset(
struct iio_dev *indio_dev)
153 ret = adis16201_spi_write_reg_8(indio_dev,
157 dev_err(&st->
us->dev,
"problem resetting device");
184 static int adis16201_check_status(
struct iio_dev *indio_dev)
189 ret = adis16201_spi_read_reg_16(indio_dev,
192 dev_err(&indio_dev->
dev,
"Reading status failed\n");
202 dev_err(&indio_dev->
dev,
"Flash update failed\n");
204 dev_err(&indio_dev->
dev,
"Power supply above 3.625V\n");
206 dev_err(&indio_dev->
dev,
"Power supply below 3.15V\n");
212 static int adis16201_self_test(
struct iio_dev *indio_dev)
215 ret = adis16201_spi_write_reg_16(indio_dev,
219 dev_err(&indio_dev->
dev,
"problem starting self test");
223 ret = adis16201_check_status(indio_dev);
229 static int adis16201_initial_setup(
struct iio_dev *indio_dev)
237 dev_err(dev,
"disable irq failed");
242 ret = adis16201_self_test(indio_dev);
244 dev_err(dev,
"self test failure");
249 ret = adis16201_check_status(indio_dev);
251 adis16201_reset(indio_dev);
252 dev_err(dev,
"device not playing ball -> reset");
254 ret = adis16201_check_status(indio_dev);
265 static u8 adis16201_addresses[7][2] = {
275 static int adis16201_read_raw(
struct iio_dev *indio_dev,
288 addr = adis16201_addresses[chan->
address][0];
289 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
296 ret = adis16201_check_status(indio_dev);
302 val16 = val16 & ((1 << chan->
scan_type.realbits) - 1);
304 val16 = (
s16)(val16 <<
311 switch (chan->
type) {
338 *val = 25000 / -470 - 1278;
341 switch (chan->
type) {
352 addr = adis16201_addresses[chan->
address][1];
353 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
358 val16 &= (1 <<
bits) - 1;
359 val16 = (
s16)(val16 << (16 - bits)) >> (16 - bits);
367 static int adis16201_write_raw(
struct iio_dev *indio_dev,
378 switch (chan->
type) {
388 val16 = val & ((1 <<
bits) - 1);
389 addr = adis16201_addresses[chan->
address][1];
390 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
400 .extend_name =
"supply",
497 static const struct iio_info adis16201_info = {
498 .read_raw = &adis16201_read_raw,
499 .write_raw = &adis16201_write_raw,
511 if (indio_dev ==
NULL) {
515 st = iio_priv(indio_dev);
517 spi_set_drvdata(spi, indio_dev);
522 indio_dev->
name = spi->
dev.driver->name;
523 indio_dev->
dev.parent = &spi->
dev;
524 indio_dev->
info = &adis16201_info;
526 indio_dev->
channels = adis16201_channels;
539 goto error_unreg_ring_funcs;
545 goto error_uninitialize_ring;
549 ret = adis16201_initial_setup(indio_dev);
551 goto error_remove_trigger;
555 goto error_remove_trigger;
558 error_remove_trigger:
560 error_uninitialize_ring:
562 error_unreg_ring_funcs:
572 struct iio_dev *indio_dev = spi_get_drvdata(spi);
588 .probe = adis16201_probe,
594 MODULE_DESCRIPTION(
"Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");