14 #include <linux/device.h>
15 #include <linux/kernel.h>
17 #include <linux/slab.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
28 #define DRIVER_NAME "adis16240"
30 static int adis16240_check_status(
struct iio_dev *indio_dev);
38 static int adis16240_spi_write_reg_8(
struct iio_dev *indio_dev,
49 ret = spi_write(st->
us, st->tx, 2);
62 static int adis16240_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 adis16240_spi_read_reg_16(
struct iio_dev *indio_dev,
107 u8 lower_reg_address,
135 spi_message_init(&
msg);
136 spi_message_add_tail(&xfers[0], &
msg);
137 spi_message_add_tail(&xfers[1], &
msg);
141 "problem when reading 16 bit register 0x%02X",
145 *val = (st->
rx[0] << 8) | st->
rx[1];
157 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
160 unsigned shift = 16 -
bits;
163 ret = adis16240_spi_read_reg_16(indio_dev,
169 adis16240_check_status(indio_dev);
171 val = ((
s16)(val << shift) >> shift);
172 return sprintf(buf,
"%d\n", val);
175 static ssize_t adis16240_read_12bit_signed(
struct device *dev,
180 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
184 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
190 static int adis16240_reset(
struct iio_dev *indio_dev)
193 ret = adis16240_spi_write_reg_8(indio_dev,
197 dev_err(&indio_dev->
dev,
"problem resetting device");
207 ret = adis16240_spi_read_reg_16(indio_dev,
219 ret = adis16240_spi_write_reg_16(indio_dev,
226 static int adis16240_self_test(
struct iio_dev *indio_dev)
229 ret = adis16240_spi_write_reg_16(indio_dev,
233 dev_err(&indio_dev->
dev,
"problem starting self test");
239 adis16240_check_status(indio_dev);
245 static int adis16240_check_status(
struct iio_dev *indio_dev)
251 ret = adis16240_spi_read_reg_16(indio_dev,
255 dev_err(dev,
"Reading status failed\n");
261 dev_err(dev,
"Power-on, self-test fail\n");
265 dev_err(dev,
"Flash update failed\n");
267 dev_err(dev,
"Power supply above 3.625V\n");
269 dev_err(dev,
"Power supply below 2.225V\n");
275 static int adis16240_initial_setup(
struct iio_dev *indio_dev)
283 dev_err(dev,
"disable irq failed");
288 ret = adis16240_self_test(indio_dev);
290 dev_err(dev,
"self test failure");
295 ret = adis16240_check_status(indio_dev);
297 adis16240_reset(indio_dev);
298 dev_err(dev,
"device not playing ball -> reset");
300 ret = adis16240_check_status(indio_dev);
312 adis16240_read_12bit_signed,
NULL,
326 static const u8 adis16240_addresses[6][3] = {
338 static int adis16240_read_raw(
struct iio_dev *indio_dev,
351 addr = adis16240_addresses[chan->
address][0];
352 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
358 if (val16 & ADIS16240_ERROR_ACTIVE) {
359 ret = adis16240_check_status(indio_dev);
365 val16 = val16 & ((1 << chan->
scan_type.realbits) - 1);
367 val16 = (
s16)(val16 <<
374 switch (chan->
type) {
400 *val = 25000 / 244 - 0x133;
405 addr = adis16240_addresses[chan->
address][1];
406 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
411 val16 &= (1 <<
bits) - 1;
412 val16 = (
s16)(val16 << (16 - bits)) >> (16 - bits);
419 addr = adis16240_addresses[chan->
address][2];
420 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
425 val16 &= (1 <<
bits) - 1;
426 val16 = (
s16)(val16 << (16 - bits)) >> (16 - bits);
434 static int adis16240_write_raw(
struct iio_dev *indio_dev,
445 val16 = val & ((1 <<
bits) - 1);
446 addr = adis16240_addresses[chan->
address][1];
447 return adis16240_spi_write_reg_16(indio_dev, addr, val16);
457 .extend_name =
"supply",
541 static struct attribute *adis16240_attributes[] = {
542 &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
543 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
548 .attrs = adis16240_attributes,
551 static const struct iio_info adis16240_info = {
552 .attrs = &adis16240_attribute_group,
553 .read_raw = &adis16240_read_raw,
554 .write_raw = &adis16240_write_raw,
566 if (indio_dev ==
NULL) {
570 st = iio_priv(indio_dev);
572 spi_set_drvdata(spi, indio_dev);
577 indio_dev->
name = spi->
dev.driver->name;
578 indio_dev->
dev.parent = &spi->
dev;
579 indio_dev->
info = &adis16240_info;
580 indio_dev->
channels = adis16240_channels;
593 goto error_unreg_ring_funcs;
599 goto error_uninitialize_ring;
603 ret = adis16240_initial_setup(indio_dev);
605 goto error_remove_trigger;
608 goto error_remove_trigger;
611 error_remove_trigger:
613 error_uninitialize_ring:
615 error_unreg_ring_funcs:
626 struct iio_dev *indio_dev = spi_get_drvdata(spi);
642 .probe = adis16240_probe,