21 #include <linux/device.h>
22 #include <linux/kernel.h>
24 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
51 static int adis16400_spi_write_reg_8(
struct iio_dev *indio_dev,
62 ret = spi_write(st->
us, st->tx, 2);
78 static int adis16400_spi_write_reg_16(
struct iio_dev *indio_dev,
100 st->tx[1] = value & 0xFF;
102 st->tx[3] = (value >> 8) & 0xFF;
104 spi_message_init(&
msg);
105 spi_message_add_tail(&xfers[0], &
msg);
106 spi_message_add_tail(&xfers[1], &
msg);
123 static int adis16400_spi_read_reg_16(
struct iio_dev *indio_dev,
124 u8 lower_reg_address,
147 spi_message_init(&
msg);
148 spi_message_add_tail(&xfers[0], &
msg);
149 spi_message_add_tail(&xfers[1], &
msg);
153 "problem when reading 16 bit register 0x%02X",
157 *val = (st->rx[0] << 8) | st->rx[1];
164 static int adis16400_get_freq(
struct iio_dev *indio_dev)
182 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
184 ret = adis16400_get_freq(indio_dev);
187 len =
sprintf(buf,
"%d SPS\n", ret);
191 static const unsigned adis16400_3db_divisors[] = {
199 static int adis16400_set_filter(
struct iio_dev *indio_dev,
int sps,
int val)
203 for (i =
ARRAY_SIZE(adis16400_3db_divisors) - 1; i >= 0; i--)
204 if (sps/adis16400_3db_divisors[i] > val)
209 ret = adis16400_spi_read_reg_16(indio_dev,
215 ret = adis16400_spi_write_reg_16(indio_dev,
217 (val16 & ~0x03) | i);
228 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
251 ret = adis16400_spi_write_reg_8(indio_dev,
258 return ret ? ret : len;
261 static int adis16400_reset(
struct iio_dev *indio_dev)
264 ret = adis16400_spi_write_reg_8(indio_dev,
268 dev_err(&indio_dev->
dev,
"problem resetting device");
297 static int adis16400_stop_device(
struct iio_dev *indio_dev)
305 "problem with turning device off: SLP_CNT");
310 static int adis16400_check_status(
struct iio_dev *indio_dev)
316 ret = adis16400_spi_read_reg_16(indio_dev,
320 dev_err(dev,
"Reading status failed\n");
325 dev_err(dev,
"Z-axis accelerometer self-test failure\n");
327 dev_err(dev,
"Y-axis accelerometer self-test failure\n");
329 dev_err(dev,
"X-axis accelerometer self-test failure\n");
331 dev_err(dev,
"X-axis gyroscope self-test failure\n");
333 dev_err(dev,
"Y-axis gyroscope self-test failure\n");
335 dev_err(dev,
"Z-axis gyroscope self-test failure\n");
337 dev_err(dev,
"Alarm 2 active\n");
339 dev_err(dev,
"Alarm 1 active\n");
341 dev_err(dev,
"Flash checksum error\n");
343 dev_err(dev,
"Self test error\n");
345 dev_err(dev,
"Sensor overrange\n");
349 dev_err(dev,
"Flash update failed\n");
351 dev_err(dev,
"Power supply above 5.25V\n");
353 dev_err(dev,
"Power supply below 4.75V\n");
359 static int adis16400_self_test(
struct iio_dev *indio_dev)
362 ret = adis16400_spi_write_reg_16(indio_dev,
366 dev_err(&indio_dev->
dev,
"problem starting self test");
371 adis16400_check_status(indio_dev);
377 static int adis16400_initial_setup(
struct iio_dev *indio_dev)
380 u16 prod_id, smp_prd;
390 dev_err(&indio_dev->
dev,
"disable irq failed");
394 ret = adis16400_self_test(indio_dev);
396 dev_err(&indio_dev->
dev,
"self test failure");
400 ret = adis16400_check_status(indio_dev);
402 adis16400_reset(indio_dev);
403 dev_err(&indio_dev->
dev,
"device not playing ball -> reset");
405 ret = adis16400_check_status(indio_dev);
412 ret = adis16400_spi_read_reg_16(indio_dev,
417 if ((prod_id & 0xF000) != st->
variant->product_id)
420 dev_info(&indio_dev->
dev,
"%s: prod_id 0x%04x at CS%d (irq %d)\n",
421 indio_dev->
name, prod_id,
422 st->
us->chip_select, st->
us->irq);
425 ret = adis16400_spi_read_reg_16(indio_dev,
427 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
437 adis16400_read_frequency,
438 adis16400_write_frequency);
461 static u8 adis16400_addresses[18][2] = {
483 static int adis16400_write_raw(
struct iio_dev *indio_dev,
495 ret = adis16400_spi_write_reg_16(indio_dev,
496 adis16400_addresses[chan->
address][1],
506 sps = adis16400_get_freq(indio_dev);
512 ret = adis16400_set_filter(indio_dev, sps, val);
520 static int adis16400_read_raw(
struct iio_dev *indio_dev,
533 ret = adis16400_spi_read_reg_16(indio_dev,
534 adis16400_addresses[chan->
address][0],
540 val16 &= (1 << chan->
scan_type.realbits) - 1;
543 val16 = (
s16)(val16 << shift) >> shift;
549 switch (chan->
type) {
552 *val2 = st->
variant->gyro_scale_micro;
566 *val2 = st->
variant->accel_scale_micro;
573 *val = st->
variant->temp_scale_nano / 1000000;
574 *val2 = (st->
variant->temp_scale_nano % 1000000);
581 ret = adis16400_spi_read_reg_16(indio_dev,
582 adis16400_addresses[chan->
address][1],
587 val16 = ((val16 & 0xFFF) << 4) >> 4;
592 *val = st->
variant->temp_offset;
597 ret = adis16400_spi_read_reg_16(indio_dev,
604 ret = adis16400_get_freq(indio_dev);
606 *val = ret/adis16400_3db_divisors[val16 & 0x03];
622 .extend_name =
"supply",
627 .scan_type =
IIO_ST(
'u', 14, 16, 0)
638 .scan_type =
IIO_ST(
's', 14, 16, 0)
649 .scan_type =
IIO_ST(
's', 14, 16, 0),
660 .scan_type =
IIO_ST(
's', 14, 16, 0),
671 .scan_type =
IIO_ST(
's', 14, 16, 0),
682 .scan_type =
IIO_ST(
's', 14, 16, 0),
693 .scan_type =
IIO_ST(
's', 14, 16, 0),
703 .scan_type =
IIO_ST(
's', 14, 16, 0),
713 .scan_type =
IIO_ST(
's', 14, 16, 0),
723 .scan_type =
IIO_ST(
's', 14, 16, 0),
733 .scan_type =
IIO_ST(
's', 12, 16, 0),
742 .scan_type =
IIO_ST(
's', 12, 16, 0),
752 .extend_name =
"supply",
757 .scan_type =
IIO_ST(
'u', 12, 16, 0)
768 .scan_type =
IIO_ST(
's', 14, 16, 0)
779 .scan_type =
IIO_ST(
's', 14, 16, 0),
790 .scan_type =
IIO_ST(
's', 14, 16, 0),
801 .scan_type =
IIO_ST(
's', 14, 16, 0),
812 .scan_type =
IIO_ST(
's', 14, 16, 0),
823 .scan_type =
IIO_ST(
's', 14, 16, 0),
835 .scan_type =
IIO_ST(
's', 12, 16, 0),
847 .scan_type =
IIO_ST(
's', 12, 16, 0),
858 .scan_type =
IIO_ST(
's', 12, 16, 0),
867 .scan_type =
IIO_ST(
's', 12, 16, 0),
877 .extend_name =
"supply",
882 .scan_type =
IIO_ST(
'u', 12, 16, 0)
893 .scan_type =
IIO_ST(
's', 14, 16, 0),
904 .scan_type =
IIO_ST(
's', 14, 16, 0),
915 .scan_type =
IIO_ST(
's', 14, 16, 0),
926 .scan_type =
IIO_ST(
's', 14, 16, 0),
936 .scan_type =
IIO_ST(
's', 12, 16, 0),
945 .scan_type =
IIO_ST(
's', 12, 16, 0),
954 .scan_type =
IIO_ST(
's', 13, 16, 0),
963 .scan_type =
IIO_ST(
's', 13, 16, 0),
979 .scan_type =
IIO_ST(
's', 14, 16, 0),
990 .scan_type =
IIO_ST(
's', 14, 16, 0),
1001 .scan_type =
IIO_ST(
's', 14, 16, 0),
1012 .scan_type =
IIO_ST(
's', 14, 16, 0),
1023 .scan_type =
IIO_ST(
's', 14, 16, 0),
1034 .scan_type =
IIO_ST(
's', 14, 16, 0),
1044 .scan_type =
IIO_ST(
's', 14, 16, 0),
1049 static struct attribute *adis16400_attributes[] = {
1050 &iio_dev_attr_sampling_frequency.dev_attr.attr,
1051 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1056 .attrs = adis16400_attributes,
1061 .channels = adis16300_channels,
1062 .num_channels =
ARRAY_SIZE(adis16300_channels),
1064 .accel_scale_micro = 5884,
1065 .temp_scale_nano = 140000000,
1066 .temp_offset = 25000000 / 140000,
1075 .channels = adis16334_channels,
1076 .num_channels =
ARRAY_SIZE(adis16334_channels),
1079 .temp_scale_nano = 67850000,
1080 .temp_offset = 25000000 / 67850,
1087 .channels = adis16350_channels,
1088 .num_channels =
ARRAY_SIZE(adis16350_channels),
1091 .temp_scale_nano = 145300000,
1092 .temp_offset = 25000000 / 145300,
1093 .default_scan_mask = 0x7FF,
1097 .channels = adis16350_channels,
1098 .num_channels =
ARRAY_SIZE(adis16350_channels),
1100 .product_id = 0x3FE8,
1103 .temp_scale_nano = 136000000,
1104 .temp_offset = 25000000 / 136000,
1105 .default_scan_mask = 0x7FF,
1108 .channels = adis16350_channels,
1109 .num_channels =
ARRAY_SIZE(adis16350_channels),
1111 .product_id = 0x3FEA,
1114 .temp_scale_nano = 136000000,
1115 .temp_offset = 25000000 / 136000,
1116 .default_scan_mask = 0x7FF,
1119 .channels = adis16350_channels,
1120 .num_channels =
ARRAY_SIZE(adis16350_channels),
1122 .product_id = 0x3FEC,
1125 .temp_scale_nano = 136000000,
1126 .temp_offset = 25000000 / 136000,
1127 .default_scan_mask = 0x7FF,
1130 .channels = adis16350_channels,
1131 .num_channels =
ARRAY_SIZE(adis16350_channels),
1133 .product_id = 0x3FED,
1136 .temp_scale_nano = 136000000,
1137 .temp_offset = 25000000 / 136000,
1138 .default_scan_mask = 0x7FF,
1141 .channels = adis16400_channels,
1142 .num_channels =
ARRAY_SIZE(adis16400_channels),
1144 .product_id = 0x4015,
1147 .default_scan_mask = 0xFFF,
1148 .temp_scale_nano = 140000000,
1149 .temp_offset = 25000000 / 140000,
1153 static const struct iio_info adis16400_info = {
1155 .read_raw = &adis16400_read_raw,
1156 .write_raw = &adis16400_write_raw,
1157 .attrs = &adis16400_attribute_group,
1165 if (indio_dev ==
NULL) {
1169 st = iio_priv(indio_dev);
1171 spi_set_drvdata(spi, indio_dev);
1178 indio_dev->
dev.parent = &spi->
dev;
1182 indio_dev->
info = &adis16400_info;
1187 goto error_free_dev;
1193 dev_err(&spi->
dev,
"failed to initialize the ring\n");
1194 goto error_unreg_ring_funcs;
1200 goto error_uninitialize_ring;
1204 ret = adis16400_initial_setup(indio_dev);
1206 goto error_remove_trigger;
1209 goto error_remove_trigger;
1213 error_remove_trigger:
1216 error_uninitialize_ring:
1218 error_unreg_ring_funcs:
1229 struct iio_dev *indio_dev = spi_get_drvdata(spi);
1232 adis16400_stop_device(indio_dev);
1258 static struct spi_driver adis16400_driver = {
1260 .name =
"adis16400",
1263 .id_table = adis16400_id,
1264 .probe = adis16400_probe,