1
2
3
4
5
6
7
8
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>
17
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>
22
23#include "adis16203.h"
24
25#define DRIVER_NAME "adis16203"
26
27static const u8 adis16203_addresses[] = {
28 [ADIS16203_SCAN_INCLI_X] = ADIS16203_INCL_NULL,
29};
30
31static int adis16203_write_raw(struct iio_dev *indio_dev,
32 struct iio_chan_spec const *chan,
33 int val,
34 int val2,
35 long mask)
36{
37 struct adis *st = iio_priv(indio_dev);
38
39 u8 addr = adis16203_addresses[chan->scan_index];
40 return adis_write_reg_16(st, addr, val & 0x3FFF);
41}
42
43static int adis16203_read_raw(struct iio_dev *indio_dev,
44 struct iio_chan_spec const *chan,
45 int *val, int *val2,
46 long mask)
47{
48 struct adis *st = iio_priv(indio_dev);
49 int ret;
50 int bits;
51 u8 addr;
52 s16 val16;
53 switch (mask) {
54 case IIO_CHAN_INFO_RAW:
55 return adis_single_conversion(indio_dev, chan,
56 ADIS16203_ERROR_ACTIVE, val);
57 case IIO_CHAN_INFO_SCALE:
58 switch (chan->type) {
59 case IIO_VOLTAGE:
60 if (chan->channel == 0) {
61 *val = 1;
62 *val2 = 220000;
63 } else {
64 *val = 0;
65 *val2 = 610000;
66 }
67 return IIO_VAL_INT_PLUS_MICRO;
68 case IIO_TEMP:
69 *val = -470;
70 *val2 = 0;
71 return IIO_VAL_INT_PLUS_MICRO;
72 case IIO_INCLI:
73 *val = 0;
74 *val2 = 25000;
75 return IIO_VAL_INT_PLUS_MICRO;
76 default:
77 return -EINVAL;
78 }
79 case IIO_CHAN_INFO_OFFSET:
80 *val = 25000 / -470 - 1278;
81 return IIO_VAL_INT;
82 case IIO_CHAN_INFO_CALIBBIAS:
83 bits = 14;
84 mutex_lock(&indio_dev->mlock);
85 addr = adis16203_addresses[chan->scan_index];
86 ret = adis_read_reg_16(st, addr, &val16);
87 if (ret) {
88 mutex_unlock(&indio_dev->mlock);
89 return ret;
90 }
91 val16 &= (1 << bits) - 1;
92 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
93 *val = val16;
94 mutex_unlock(&indio_dev->mlock);
95 return IIO_VAL_INT;
96 default:
97 return -EINVAL;
98 }
99}
100
101static const struct iio_chan_spec adis16203_channels[] = {
102 ADIS_SUPPLY_CHAN(ADIS16203_SUPPLY_OUT, ADIS16203_SCAN_SUPPLY, 12),
103 ADIS_AUX_ADC_CHAN(ADIS16203_AUX_ADC, ADIS16203_SCAN_AUX_ADC, 12),
104 ADIS_INCLI_CHAN(X, ADIS16203_XINCL_OUT, ADIS16203_SCAN_INCLI_X,
105 BIT(IIO_CHAN_INFO_CALIBBIAS), 14),
106
107 ADIS_INCLI_CHAN(Y, ADIS16203_YINCL_OUT, ADIS16203_SCAN_INCLI_Y, 0, 14),
108 ADIS_TEMP_CHAN(ADIS16203_TEMP_OUT, ADIS16203_SCAN_TEMP, 12),
109 IIO_CHAN_SOFT_TIMESTAMP(5),
110};
111
112static const struct iio_info adis16203_info = {
113 .read_raw = &adis16203_read_raw,
114 .write_raw = &adis16203_write_raw,
115 .update_scan_mode = adis_update_scan_mode,
116 .driver_module = THIS_MODULE,
117};
118
119static const char * const adis16203_status_error_msgs[] = {
120 [ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT] = "Self test failure",
121 [ADIS16203_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
122 [ADIS16203_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
123 [ADIS16203_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
124 [ADIS16203_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 3.15V",
125};
126
127static const struct adis_data adis16203_data = {
128 .read_delay = 20,
129 .msc_ctrl_reg = ADIS16203_MSC_CTRL,
130 .glob_cmd_reg = ADIS16203_GLOB_CMD,
131 .diag_stat_reg = ADIS16203_DIAG_STAT,
132
133 .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN,
134 .startup_delay = ADIS16203_STARTUP_DELAY,
135
136 .status_error_msgs = adis16203_status_error_msgs,
137 .status_error_mask = BIT(ADIS16203_DIAG_STAT_SELFTEST_FAIL_BIT) |
138 BIT(ADIS16203_DIAG_STAT_SPI_FAIL_BIT) |
139 BIT(ADIS16203_DIAG_STAT_FLASH_UPT_BIT) |
140 BIT(ADIS16203_DIAG_STAT_POWER_HIGH_BIT) |
141 BIT(ADIS16203_DIAG_STAT_POWER_LOW_BIT),
142};
143
144static int adis16203_probe(struct spi_device *spi)
145{
146 int ret;
147 struct iio_dev *indio_dev;
148 struct adis *st;
149
150
151 indio_dev = iio_device_alloc(sizeof(*st));
152 if (indio_dev == NULL) {
153 ret = -ENOMEM;
154 goto error_ret;
155 }
156 st = iio_priv(indio_dev);
157
158 spi_set_drvdata(spi, indio_dev);
159
160 indio_dev->name = spi->dev.driver->name;
161 indio_dev->dev.parent = &spi->dev;
162 indio_dev->channels = adis16203_channels;
163 indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
164 indio_dev->info = &adis16203_info;
165 indio_dev->modes = INDIO_DIRECT_MODE;
166
167 ret = adis_init(st, indio_dev, spi, &adis16203_data);
168 if (ret)
169 goto error_free_dev;
170
171 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
172 if (ret)
173 goto error_free_dev;
174
175
176 ret = adis_initial_startup(st);
177 if (ret)
178 goto error_cleanup_buffer_trigger;
179
180 ret = iio_device_register(indio_dev);
181 if (ret)
182 goto error_cleanup_buffer_trigger;
183
184 return 0;
185
186error_cleanup_buffer_trigger:
187 adis_cleanup_buffer_and_trigger(st, indio_dev);
188error_free_dev:
189 iio_device_free(indio_dev);
190error_ret:
191 return ret;
192}
193
194static int adis16203_remove(struct spi_device *spi)
195{
196 struct iio_dev *indio_dev = spi_get_drvdata(spi);
197 struct adis *st = iio_priv(indio_dev);
198
199 iio_device_unregister(indio_dev);
200 adis_cleanup_buffer_and_trigger(st, indio_dev);
201 iio_device_free(indio_dev);
202
203 return 0;
204}
205
206static struct spi_driver adis16203_driver = {
207 .driver = {
208 .name = "adis16203",
209 .owner = THIS_MODULE,
210 },
211 .probe = adis16203_probe,
212 .remove = adis16203_remove,
213};
214module_spi_driver(adis16203_driver);
215
216MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
217MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
218MODULE_LICENSE("GPL v2");
219MODULE_ALIAS("spi:adis16203");
220