1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer
4 *
5 * Copyright 2010 Analog Devices Inc.
6 */
7
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 #include <linux/sysfs.h>
15
16 #include <linux/iio/iio.h>
17 #include <linux/iio/imu/adis.h>
18
19 #define ADIS16201_STARTUP_DELAY_MS 220
20 #define ADIS16201_FLASH_CNT 0x00
21
22 /* Data Output Register Information */
23 #define ADIS16201_SUPPLY_OUT_REG 0x02
24 #define ADIS16201_XACCL_OUT_REG 0x04
25 #define ADIS16201_YACCL_OUT_REG 0x06
26 #define ADIS16201_AUX_ADC_REG 0x08
27 #define ADIS16201_TEMP_OUT_REG 0x0A
28 #define ADIS16201_XINCL_OUT_REG 0x0C
29 #define ADIS16201_YINCL_OUT_REG 0x0E
30
31 /* Calibration Register Definition */
32 #define ADIS16201_XACCL_OFFS_REG 0x10
33 #define ADIS16201_YACCL_OFFS_REG 0x12
34 #define ADIS16201_XACCL_SCALE_REG 0x14
35 #define ADIS16201_YACCL_SCALE_REG 0x16
36 #define ADIS16201_XINCL_OFFS_REG 0x18
37 #define ADIS16201_YINCL_OFFS_REG 0x1A
38 #define ADIS16201_XINCL_SCALE_REG 0x1C
39 #define ADIS16201_YINCL_SCALE_REG 0x1E
40
41 /* Alarm Register Definition */
42 #define ADIS16201_ALM_MAG1_REG 0x20
43 #define ADIS16201_ALM_MAG2_REG 0x22
44 #define ADIS16201_ALM_SMPL1_REG 0x24
45 #define ADIS16201_ALM_SMPL2_REG 0x26
46 #define ADIS16201_ALM_CTRL_REG 0x28
47
48 #define ADIS16201_AUX_DAC_REG 0x30
49 #define ADIS16201_GPIO_CTRL_REG 0x32
50 #define ADIS16201_SMPL_PRD_REG 0x36
51 /* Operation, filter configuration */
52 #define ADIS16201_AVG_CNT_REG 0x38
53 #define ADIS16201_SLP_CNT_REG 0x3A
54
55 /* Miscellaneous Control Register Definition */
56 #define ADIS16201_MSC_CTRL_REG 0x34
57 #define ADIS16201_MSC_CTRL_SELF_TEST_EN BIT(8)
58 /* Data-ready enable: 1 = enabled, 0 = disabled */
59 #define ADIS16201_MSC_CTRL_DATA_RDY_EN BIT(2)
60 /* Data-ready polarity: 1 = active high, 0 = active low */
61 #define ADIS16201_MSC_CTRL_ACTIVE_DATA_RDY_HIGH BIT(1)
62 /* Data-ready line selection: 1 = DIO1, 0 = DIO0 */
63 #define ADIS16201_MSC_CTRL_DATA_RDY_DIO1 BIT(0)
64
65 /* Diagnostics System Status Register Definition */
66 #define ADIS16201_DIAG_STAT_REG 0x3C
67 #define ADIS16201_DIAG_STAT_ALARM2 BIT(9)
68 #define ADIS16201_DIAG_STAT_ALARM1 BIT(8)
69 #define ADIS16201_DIAG_STAT_SPI_FAIL_BIT 3
70 #define ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT 2
71 /* Power supply above 3.625 V */
72 #define ADIS16201_DIAG_STAT_POWER_HIGH_BIT 1
73 /* Power supply below 2.975 V */
74 #define ADIS16201_DIAG_STAT_POWER_LOW_BIT 0
75
76 /* System Command Register Definition */
77 #define ADIS16201_GLOB_CMD_REG 0x3E
78 #define ADIS16201_GLOB_CMD_SW_RESET BIT(7)
79 #define ADIS16201_GLOB_CMD_FACTORY_RESET BIT(1)
80
81 #define ADIS16201_ERROR_ACTIVE BIT(14)
82
83 enum adis16201_scan {
84 ADIS16201_SCAN_ACC_X,
85 ADIS16201_SCAN_ACC_Y,
86 ADIS16201_SCAN_INCLI_X,
87 ADIS16201_SCAN_INCLI_Y,
88 ADIS16201_SCAN_SUPPLY,
89 ADIS16201_SCAN_AUX_ADC,
90 ADIS16201_SCAN_TEMP,
91 };
92
93 static const u8 adis16201_addresses[] = {
94 [ADIS16201_SCAN_ACC_X] = ADIS16201_XACCL_OFFS_REG,
95 [ADIS16201_SCAN_ACC_Y] = ADIS16201_YACCL_OFFS_REG,
96 [ADIS16201_SCAN_INCLI_X] = ADIS16201_XINCL_OFFS_REG,
97 [ADIS16201_SCAN_INCLI_Y] = ADIS16201_YINCL_OFFS_REG,
98 };
99
adis16201_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)100 static int adis16201_read_raw(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan,
102 int *val, int *val2,
103 long mask)
104 {
105 struct adis *st = iio_priv(indio_dev);
106 int ret;
107 int bits;
108 u8 addr;
109 s16 val16;
110
111 switch (mask) {
112 case IIO_CHAN_INFO_RAW:
113 return adis_single_conversion(indio_dev, chan,
114 ADIS16201_ERROR_ACTIVE, val);
115 case IIO_CHAN_INFO_SCALE:
116 switch (chan->type) {
117 case IIO_VOLTAGE:
118 if (chan->channel == 0) {
119 /* Voltage base units are mV hence 1.22 mV */
120 *val = 1;
121 *val2 = 220000;
122 } else {
123 /* Voltage base units are mV hence 0.61 mV */
124 *val = 0;
125 *val2 = 610000;
126 }
127 return IIO_VAL_INT_PLUS_MICRO;
128 case IIO_TEMP:
129 *val = -470;
130 *val2 = 0;
131 return IIO_VAL_INT_PLUS_MICRO;
132 case IIO_ACCEL:
133 /*
134 * IIO base unit for sensitivity of accelerometer
135 * is milli g.
136 * 1 LSB represents 0.244 mg.
137 */
138 *val = 0;
139 *val2 = IIO_G_TO_M_S_2(462400);
140 return IIO_VAL_INT_PLUS_NANO;
141 case IIO_INCLI:
142 *val = 0;
143 *val2 = 100000;
144 return IIO_VAL_INT_PLUS_MICRO;
145 default:
146 return -EINVAL;
147 }
148 break;
149 case IIO_CHAN_INFO_OFFSET:
150 /*
151 * The raw ADC value is 1278 when the temperature
152 * is 25 degrees and the scale factor per milli
153 * degree celcius is -470.
154 */
155 *val = 25000 / -470 - 1278;
156 return IIO_VAL_INT;
157 case IIO_CHAN_INFO_CALIBBIAS:
158 switch (chan->type) {
159 case IIO_ACCEL:
160 bits = 12;
161 break;
162 case IIO_INCLI:
163 bits = 9;
164 break;
165 default:
166 return -EINVAL;
167 }
168 addr = adis16201_addresses[chan->scan_index];
169 ret = adis_read_reg_16(st, addr, &val16);
170 if (ret)
171 return ret;
172
173 *val = sign_extend32(val16, bits - 1);
174 return IIO_VAL_INT;
175 }
176
177 return -EINVAL;
178 }
179
adis16201_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)180 static int adis16201_write_raw(struct iio_dev *indio_dev,
181 struct iio_chan_spec const *chan,
182 int val,
183 int val2,
184 long mask)
185 {
186 struct adis *st = iio_priv(indio_dev);
187 int m;
188
189 if (mask != IIO_CHAN_INFO_CALIBBIAS)
190 return -EINVAL;
191
192 switch (chan->type) {
193 case IIO_ACCEL:
194 m = GENMASK(11, 0);
195 break;
196 case IIO_INCLI:
197 m = GENMASK(8, 0);
198 break;
199 default:
200 return -EINVAL;
201 }
202
203 return adis_write_reg_16(st, adis16201_addresses[chan->scan_index],
204 val & m);
205 }
206
207 static const struct iio_chan_spec adis16201_channels[] = {
208 ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT_REG, ADIS16201_SCAN_SUPPLY, 0,
209 12),
210 ADIS_TEMP_CHAN(ADIS16201_TEMP_OUT_REG, ADIS16201_SCAN_TEMP, 0, 12),
211 ADIS_ACCEL_CHAN(X, ADIS16201_XACCL_OUT_REG, ADIS16201_SCAN_ACC_X,
212 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
213 ADIS_ACCEL_CHAN(Y, ADIS16201_YACCL_OUT_REG, ADIS16201_SCAN_ACC_Y,
214 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
215 ADIS_AUX_ADC_CHAN(ADIS16201_AUX_ADC_REG, ADIS16201_SCAN_AUX_ADC, 0, 12),
216 ADIS_INCLI_CHAN(X, ADIS16201_XINCL_OUT_REG, ADIS16201_SCAN_INCLI_X,
217 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
218 ADIS_INCLI_CHAN(Y, ADIS16201_YINCL_OUT_REG, ADIS16201_SCAN_INCLI_Y,
219 BIT(IIO_CHAN_INFO_CALIBBIAS), 0, 14),
220 IIO_CHAN_SOFT_TIMESTAMP(7)
221 };
222
223 static const struct iio_info adis16201_info = {
224 .read_raw = adis16201_read_raw,
225 .write_raw = adis16201_write_raw,
226 .update_scan_mode = adis_update_scan_mode,
227 };
228
229 static const char * const adis16201_status_error_msgs[] = {
230 [ADIS16201_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
231 [ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT] = "Flash update failed",
232 [ADIS16201_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
233 [ADIS16201_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.975V",
234 };
235
236 static const struct adis_timeout adis16201_timeouts = {
237 .reset_ms = ADIS16201_STARTUP_DELAY_MS,
238 .sw_reset_ms = ADIS16201_STARTUP_DELAY_MS,
239 .self_test_ms = ADIS16201_STARTUP_DELAY_MS,
240 };
241
242 static const struct adis_data adis16201_data = {
243 .read_delay = 20,
244 .msc_ctrl_reg = ADIS16201_MSC_CTRL_REG,
245 .glob_cmd_reg = ADIS16201_GLOB_CMD_REG,
246 .diag_stat_reg = ADIS16201_DIAG_STAT_REG,
247
248 .self_test_mask = ADIS16201_MSC_CTRL_SELF_TEST_EN,
249 .self_test_reg = ADIS16201_MSC_CTRL_REG,
250 .self_test_no_autoclear = true,
251 .timeouts = &adis16201_timeouts,
252
253 .status_error_msgs = adis16201_status_error_msgs,
254 .status_error_mask = BIT(ADIS16201_DIAG_STAT_SPI_FAIL_BIT) |
255 BIT(ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT) |
256 BIT(ADIS16201_DIAG_STAT_POWER_HIGH_BIT) |
257 BIT(ADIS16201_DIAG_STAT_POWER_LOW_BIT),
258 };
259
adis16201_probe(struct spi_device * spi)260 static int adis16201_probe(struct spi_device *spi)
261 {
262 struct iio_dev *indio_dev;
263 struct adis *st;
264 int ret;
265
266 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
267 if (!indio_dev)
268 return -ENOMEM;
269
270 st = iio_priv(indio_dev);
271 spi_set_drvdata(spi, indio_dev);
272
273 indio_dev->name = spi->dev.driver->name;
274 indio_dev->info = &adis16201_info;
275
276 indio_dev->channels = adis16201_channels;
277 indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
278 indio_dev->modes = INDIO_DIRECT_MODE;
279
280 ret = adis_init(st, indio_dev, spi, &adis16201_data);
281 if (ret)
282 return ret;
283
284 ret = devm_adis_setup_buffer_and_trigger(st, indio_dev, NULL);
285 if (ret)
286 return ret;
287
288 ret = adis_initial_startup(st);
289 if (ret)
290 return ret;
291
292 return devm_iio_device_register(&spi->dev, indio_dev);
293 }
294
295 static struct spi_driver adis16201_driver = {
296 .driver = {
297 .name = "adis16201",
298 },
299 .probe = adis16201_probe,
300 };
301 module_spi_driver(adis16201_driver);
302
303 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
304 MODULE_DESCRIPTION("Analog Devices ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer");
305 MODULE_LICENSE("GPL v2");
306 MODULE_ALIAS("spi:adis16201");
307 MODULE_IMPORT_NS(IIO_ADISLIB);
308