2 * ADIS16203 Programmable Digital Vibration Sensor driver
4 * Copyright 2030 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>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/imu/adis.h>
23 #include "adis16203.h"
25 #define DRIVER_NAME "adis16203"
27 static const u8 adis16203_addresses[] = {
28 [ADIS16203_SCAN_INCLI_X] = ADIS16203_INCL_NULL,
31 static int adis16203_write_raw(struct iio_dev *indio_dev,
32 struct iio_chan_spec const *chan,
37 struct adis *st = iio_priv(indio_dev);
38 /* currently only one writable parameter which keeps this simple */
39 u8 addr = adis16203_addresses[chan->scan_index];
41 return adis_write_reg_16(st, addr, val & 0x3FFF);
44 static int adis16203_read_raw(struct iio_dev *indio_dev,
45 struct iio_chan_spec const *chan,
49 struct adis *st = iio_priv(indio_dev);
56 case IIO_CHAN_INFO_RAW:
57 return adis_single_conversion(indio_dev, chan,
58 ADIS16203_ERROR_ACTIVE, val);
59 case IIO_CHAN_INFO_SCALE:
62 if (chan->channel == 0) {
64 *val2 = 220000; /* 1.22 mV */
67 *val2 = 610000; /* 0.61 mV */
69 return IIO_VAL_INT_PLUS_MICRO;
71 *val = -470; /* -0.47 C */
73 return IIO_VAL_INT_PLUS_MICRO;
76 *val2 = 25000; /* 0.025 degree */
77 return IIO_VAL_INT_PLUS_MICRO;
81 case IIO_CHAN_INFO_OFFSET:
82 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
84 case IIO_CHAN_INFO_CALIBBIAS:
86 mutex_lock(&indio_dev->mlock);
87 addr = adis16203_addresses[chan->scan_index];
88 ret = adis_read_reg_16(st, addr, &val16);
90 mutex_unlock(&indio_dev->mlock);
93 val16 &= (1 << bits) - 1;
94 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
96 mutex_unlock(&indio_dev->mlock);
103 static const struct iio_chan_spec adis16203_channels[] = {
104 ADIS_SUPPLY_CHAN(ADIS16203_SUPPLY_OUT, ADIS16203_SCAN_SUPPLY, 0, 12),
105 ADIS_AUX_ADC_CHAN(ADIS16203_AUX_ADC, ADIS16203_SCAN_AUX_ADC, 0, 12),
106 ADIS_INCLI_CHAN(X, ADIS16203_XINCL_OUT, ADIS16203_SCAN_INCLI_X,
107 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
108 /* Fixme: Not what it appears to be - see data sheet */
109 ADIS_INCLI_CHAN(Y, ADIS16203_YINCL_OUT, ADIS16203_SCAN_INCLI_Y,
111 ADIS_TEMP_CHAN(ADIS16203_TEMP_OUT, ADIS16203_SCAN_TEMP, 0, 12),
112 IIO_CHAN_SOFT_TIMESTAMP(5),
115 static const struct iio_info adis16203_info = {
116 .read_raw = &adis16203_read_raw,
117 .write_raw = &adis16203_write_raw,
118 .update_scan_mode = adis_update_scan_mode,
119 .driver_module = THIS_MODULE,
122 static const char * const adis16203_status_error_msgs[] = {
123 [ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
124 [ADIS16203_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
125 [ADIS16203_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
126 [ADIS16203_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
127 [ADIS16203_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
130 static const struct adis_data adis16203_data = {
132 .msc_ctrl_reg = ADIS16203_MSC_CTRL,
133 .glob_cmd_reg = ADIS16203_GLOB_CMD,
134 .diag_stat_reg = ADIS16203_DIAG_STAT,
136 .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN,
137 .startup_delay = ADIS16203_STARTUP_DELAY,
139 .status_error_msgs = adis16203_status_error_msgs,
140 .status_error_mask = BIT(ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT) |
141 BIT(ADIS16203_DIAG_STAT_SPI_FAIL_BIT) |
142 BIT(ADIS16203_DIAG_STAT_FLASH_UPT_BIT) |
143 BIT(ADIS16203_DIAG_STAT_POWER_HIGH_BIT) |
144 BIT(ADIS16203_DIAG_STAT_POWER_LOW_BIT),
147 static int adis16203_probe(struct spi_device *spi)
150 struct iio_dev *indio_dev;
153 /* setup the industrialio driver allocated elements */
154 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
157 st = iio_priv(indio_dev);
158 /* this is only used for removal purposes */
159 spi_set_drvdata(spi, indio_dev);
161 indio_dev->name = spi->dev.driver->name;
162 indio_dev->dev.parent = &spi->dev;
163 indio_dev->channels = adis16203_channels;
164 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
165 indio_dev->info = &adis16203_info;
166 indio_dev->modes = INDIO_DIRECT_MODE;
168 ret = adis_init(st, indio_dev, spi, &adis16203_data);
172 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
176 /* Get the device into a sane initial state */
177 ret = adis_initial_startup(st);
179 goto error_cleanup_buffer_trigger;
181 ret = iio_device_register(indio_dev);
183 goto error_cleanup_buffer_trigger;
187 error_cleanup_buffer_trigger:
188 adis_cleanup_buffer_and_trigger(st, indio_dev);
192 static int adis16203_remove(struct spi_device *spi)
194 struct iio_dev *indio_dev = spi_get_drvdata(spi);
195 struct adis *st = iio_priv(indio_dev);
197 iio_device_unregister(indio_dev);
198 adis_cleanup_buffer_and_trigger(st, indio_dev);
203 static struct spi_driver adis16203_driver = {
207 .probe = adis16203_probe,
208 .remove = adis16203_remove,
210 module_spi_driver(adis16203_driver);
212 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
213 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
214 MODULE_LICENSE("GPL v2");
215 MODULE_ALIAS("spi:adis16203");