1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ADIS16240 Programmable Impact Sensor and Recorder driver
4 *
5 * Copyright 2010 Analog Devices Inc.
6 */
7
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/spi/spi.h>
11 #include <linux/sysfs.h>
12 #include <linux/module.h>
13
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/imu/adis.h>
17
18 #define ADIS16240_STARTUP_DELAY 220 /* ms */
19
20 /* Flash memory write count */
21 #define ADIS16240_FLASH_CNT 0x00
22
23 /* Output, power supply */
24 #define ADIS16240_SUPPLY_OUT 0x02
25
26 /* Output, x-axis accelerometer */
27 #define ADIS16240_XACCL_OUT 0x04
28
29 /* Output, y-axis accelerometer */
30 #define ADIS16240_YACCL_OUT 0x06
31
32 /* Output, z-axis accelerometer */
33 #define ADIS16240_ZACCL_OUT 0x08
34
35 /* Output, auxiliary ADC input */
36 #define ADIS16240_AUX_ADC 0x0A
37
38 /* Output, temperature */
39 #define ADIS16240_TEMP_OUT 0x0C
40
41 /* Output, x-axis acceleration peak */
42 #define ADIS16240_XPEAK_OUT 0x0E
43
44 /* Output, y-axis acceleration peak */
45 #define ADIS16240_YPEAK_OUT 0x10
46
47 /* Output, z-axis acceleration peak */
48 #define ADIS16240_ZPEAK_OUT 0x12
49
50 /* Output, sum-of-squares acceleration peak */
51 #define ADIS16240_XYZPEAK_OUT 0x14
52
53 /* Output, Capture Buffer 1, X and Y acceleration */
54 #define ADIS16240_CAPT_BUF1 0x16
55
56 /* Output, Capture Buffer 2, Z acceleration */
57 #define ADIS16240_CAPT_BUF2 0x18
58
59 /* Diagnostic, error flags */
60 #define ADIS16240_DIAG_STAT 0x1A
61
62 /* Diagnostic, event counter */
63 #define ADIS16240_EVNT_CNTR 0x1C
64
65 /* Diagnostic, check sum value from firmware test */
66 #define ADIS16240_CHK_SUM 0x1E
67
68 /* Calibration, x-axis acceleration offset adjustment */
69 #define ADIS16240_XACCL_OFF 0x20
70
71 /* Calibration, y-axis acceleration offset adjustment */
72 #define ADIS16240_YACCL_OFF 0x22
73
74 /* Calibration, z-axis acceleration offset adjustment */
75 #define ADIS16240_ZACCL_OFF 0x24
76
77 /* Clock, hour and minute */
78 #define ADIS16240_CLK_TIME 0x2E
79
80 /* Clock, month and day */
81 #define ADIS16240_CLK_DATE 0x30
82
83 /* Clock, year */
84 #define ADIS16240_CLK_YEAR 0x32
85
86 /* Wake-up setting, hour and minute */
87 #define ADIS16240_WAKE_TIME 0x34
88
89 /* Wake-up setting, month and day */
90 #define ADIS16240_WAKE_DATE 0x36
91
92 /* Alarm 1 amplitude threshold */
93 #define ADIS16240_ALM_MAG1 0x38
94
95 /* Alarm 2 amplitude threshold */
96 #define ADIS16240_ALM_MAG2 0x3A
97
98 /* Alarm control */
99 #define ADIS16240_ALM_CTRL 0x3C
100
101 /* Capture, external trigger control */
102 #define ADIS16240_XTRIG_CTRL 0x3E
103
104 /* Capture, address pointer */
105 #define ADIS16240_CAPT_PNTR 0x40
106
107 /* Capture, configuration and control */
108 #define ADIS16240_CAPT_CTRL 0x42
109
110 /* General-purpose digital input/output control */
111 #define ADIS16240_GPIO_CTRL 0x44
112
113 /* Miscellaneous control */
114 #define ADIS16240_MSC_CTRL 0x46
115
116 /* Internal sample period (rate) control */
117 #define ADIS16240_SMPL_PRD 0x48
118
119 /* System command */
120 #define ADIS16240_GLOB_CMD 0x4A
121
122 /* MSC_CTRL */
123
124 /* Enables sum-of-squares output (XYZPEAK_OUT) */
125 #define ADIS16240_MSC_CTRL_XYZPEAK_OUT_EN BIT(15)
126
127 /* Enables peak tracking output (XPEAK_OUT, YPEAK_OUT, and ZPEAK_OUT) */
128 #define ADIS16240_MSC_CTRL_X_Y_ZPEAK_OUT_EN BIT(14)
129
130 /* Self-test enable: 1 = apply electrostatic force, 0 = disabled */
131 #define ADIS16240_MSC_CTRL_SELF_TEST_EN BIT(8)
132
133 /* Data-ready enable: 1 = enabled, 0 = disabled */
134 #define ADIS16240_MSC_CTRL_DATA_RDY_EN BIT(2)
135
136 /* Data-ready polarity: 1 = active high, 0 = active low */
137 #define ADIS16240_MSC_CTRL_ACTIVE_HIGH BIT(1)
138
139 /* Data-ready line selection: 1 = DIO2, 0 = DIO1 */
140 #define ADIS16240_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
141
142 /* DIAG_STAT */
143
144 /* Alarm 2 status: 1 = alarm active, 0 = alarm inactive */
145 #define ADIS16240_DIAG_STAT_ALARM2 BIT(9)
146
147 /* Alarm 1 status: 1 = alarm active, 0 = alarm inactive */
148 #define ADIS16240_DIAG_STAT_ALARM1 BIT(8)
149
150 /* Capture buffer full: 1 = capture buffer is full */
151 #define ADIS16240_DIAG_STAT_CPT_BUF_FUL BIT(7)
152
153 /* Flash test, checksum flag: 1 = mismatch, 0 = match */
154 #define ADIS16240_DIAG_STAT_CHKSUM BIT(6)
155
156 /* Power-on, self-test flag: 1 = failure, 0 = pass */
157 #define ADIS16240_DIAG_STAT_PWRON_FAIL_BIT 5
158
159 /* Power-on self-test: 1 = in-progress, 0 = complete */
160 #define ADIS16240_DIAG_STAT_PWRON_BUSY BIT(4)
161
162 /* SPI communications failure */
163 #define ADIS16240_DIAG_STAT_SPI_FAIL_BIT 3
164
165 /* Flash update failure */
166 #define ADIS16240_DIAG_STAT_FLASH_UPT_BIT 2
167
168 /* Power supply above 3.625 V */
169 #define ADIS16240_DIAG_STAT_POWER_HIGH_BIT 1
170
171 /* Power supply below 2.225 V */
172 #define ADIS16240_DIAG_STAT_POWER_LOW_BIT 0
173
174 /* GLOB_CMD */
175
176 #define ADIS16240_GLOB_CMD_RESUME BIT(8)
177 #define ADIS16240_GLOB_CMD_SW_RESET BIT(7)
178 #define ADIS16240_GLOB_CMD_STANDBY BIT(2)
179
180 #define ADIS16240_ERROR_ACTIVE BIT(14)
181
182 /* At the moment triggers are only used for ring buffer
183 * filling. This may change!
184 */
185
186 enum adis16240_scan {
187 ADIS16240_SCAN_ACC_X,
188 ADIS16240_SCAN_ACC_Y,
189 ADIS16240_SCAN_ACC_Z,
190 ADIS16240_SCAN_SUPPLY,
191 ADIS16240_SCAN_AUX_ADC,
192 ADIS16240_SCAN_TEMP,
193 };
194
adis16240_spi_read_signed(struct device * dev,struct device_attribute * attr,char * buf,unsigned int bits)195 static ssize_t adis16240_spi_read_signed(struct device *dev,
196 struct device_attribute *attr,
197 char *buf,
198 unsigned int bits)
199 {
200 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
201 struct adis *st = iio_priv(indio_dev);
202 int ret;
203 s16 val = 0;
204 unsigned int shift = 16 - bits;
205 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
206
207 ret = adis_read_reg_16(st,
208 this_attr->address, (u16 *)&val);
209 if (ret)
210 return ret;
211
212 if (val & ADIS16240_ERROR_ACTIVE)
213 adis_check_status(st);
214
215 val = (s16)(val << shift) >> shift;
216 return sprintf(buf, "%d\n", val);
217 }
218
adis16240_read_12bit_signed(struct device * dev,struct device_attribute * attr,char * buf)219 static ssize_t adis16240_read_12bit_signed(struct device *dev,
220 struct device_attribute *attr,
221 char *buf)
222 {
223 return adis16240_spi_read_signed(dev, attr, buf, 12);
224 }
225
226 static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, 0444,
227 adis16240_read_12bit_signed, NULL,
228 ADIS16240_XYZPEAK_OUT);
229
230 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
231
232 static const u8 adis16240_addresses[][2] = {
233 [ADIS16240_SCAN_ACC_X] = { ADIS16240_XACCL_OFF, ADIS16240_XPEAK_OUT },
234 [ADIS16240_SCAN_ACC_Y] = { ADIS16240_YACCL_OFF, ADIS16240_YPEAK_OUT },
235 [ADIS16240_SCAN_ACC_Z] = { ADIS16240_ZACCL_OFF, ADIS16240_ZPEAK_OUT },
236 };
237
adis16240_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)238 static int adis16240_read_raw(struct iio_dev *indio_dev,
239 struct iio_chan_spec const *chan,
240 int *val, int *val2,
241 long mask)
242 {
243 struct adis *st = iio_priv(indio_dev);
244 int ret;
245 u8 addr;
246 s16 val16;
247
248 switch (mask) {
249 case IIO_CHAN_INFO_RAW:
250 return adis_single_conversion(indio_dev, chan,
251 ADIS16240_ERROR_ACTIVE, val);
252 case IIO_CHAN_INFO_SCALE:
253 switch (chan->type) {
254 case IIO_VOLTAGE:
255 if (chan->channel == 0) {
256 *val = 4;
257 *val2 = 880000; /* 4.88 mV */
258 return IIO_VAL_INT_PLUS_MICRO;
259 }
260 return -EINVAL;
261 case IIO_TEMP:
262 *val = 244; /* 0.244 C */
263 *val2 = 0;
264 return IIO_VAL_INT_PLUS_MICRO;
265 case IIO_ACCEL:
266 *val = 0;
267 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
268 return IIO_VAL_INT_PLUS_MICRO;
269 default:
270 return -EINVAL;
271 }
272 break;
273 case IIO_CHAN_INFO_PEAK_SCALE:
274 *val = 0;
275 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
276 return IIO_VAL_INT_PLUS_MICRO;
277 case IIO_CHAN_INFO_OFFSET:
278 *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */
279 return IIO_VAL_INT;
280 case IIO_CHAN_INFO_CALIBBIAS:
281 addr = adis16240_addresses[chan->scan_index][0];
282 ret = adis_read_reg_16(st, addr, &val16);
283 if (ret)
284 return ret;
285 *val = sign_extend32(val16, 9);
286 return IIO_VAL_INT;
287 case IIO_CHAN_INFO_PEAK:
288 addr = adis16240_addresses[chan->scan_index][1];
289 ret = adis_read_reg_16(st, addr, &val16);
290 if (ret)
291 return ret;
292 *val = sign_extend32(val16, 9);
293 return IIO_VAL_INT;
294 }
295 return -EINVAL;
296 }
297
adis16240_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)298 static int adis16240_write_raw(struct iio_dev *indio_dev,
299 struct iio_chan_spec const *chan,
300 int val,
301 int val2,
302 long mask)
303 {
304 struct adis *st = iio_priv(indio_dev);
305 u8 addr;
306
307 switch (mask) {
308 case IIO_CHAN_INFO_CALIBBIAS:
309 addr = adis16240_addresses[chan->scan_index][0];
310 return adis_write_reg_16(st, addr, val & GENMASK(9, 0));
311 }
312 return -EINVAL;
313 }
314
315 static const struct iio_chan_spec adis16240_channels[] = {
316 ADIS_SUPPLY_CHAN(ADIS16240_SUPPLY_OUT, ADIS16240_SCAN_SUPPLY, 0, 10),
317 ADIS_AUX_ADC_CHAN(ADIS16240_AUX_ADC, ADIS16240_SCAN_AUX_ADC, 0, 10),
318 ADIS_ACCEL_CHAN(X, ADIS16240_XACCL_OUT, ADIS16240_SCAN_ACC_X,
319 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
320 0, 10),
321 ADIS_ACCEL_CHAN(Y, ADIS16240_YACCL_OUT, ADIS16240_SCAN_ACC_Y,
322 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
323 0, 10),
324 ADIS_ACCEL_CHAN(Z, ADIS16240_ZACCL_OUT, ADIS16240_SCAN_ACC_Z,
325 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
326 0, 10),
327 ADIS_TEMP_CHAN(ADIS16240_TEMP_OUT, ADIS16240_SCAN_TEMP, 0, 10),
328 IIO_CHAN_SOFT_TIMESTAMP(6)
329 };
330
331 static struct attribute *adis16240_attributes[] = {
332 &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
333 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
334 NULL
335 };
336
337 static const struct attribute_group adis16240_attribute_group = {
338 .attrs = adis16240_attributes,
339 };
340
341 static const struct iio_info adis16240_info = {
342 .attrs = &adis16240_attribute_group,
343 .read_raw = adis16240_read_raw,
344 .write_raw = adis16240_write_raw,
345 .update_scan_mode = adis_update_scan_mode,
346 };
347
348 static const char * const adis16240_status_error_msgs[] = {
349 [ADIS16240_DIAG_STAT_PWRON_FAIL_BIT] = "Power on, self-test failed",
350 [ADIS16240_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
351 [ADIS16240_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
352 [ADIS16240_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
353 [ADIS16240_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.225V",
354 };
355
356 static const struct adis_timeout adis16240_timeouts = {
357 .reset_ms = ADIS16240_STARTUP_DELAY,
358 .sw_reset_ms = ADIS16240_STARTUP_DELAY,
359 .self_test_ms = ADIS16240_STARTUP_DELAY,
360 };
361
362 static const struct adis_data adis16240_data = {
363 .write_delay = 35,
364 .read_delay = 35,
365 .msc_ctrl_reg = ADIS16240_MSC_CTRL,
366 .glob_cmd_reg = ADIS16240_GLOB_CMD,
367 .diag_stat_reg = ADIS16240_DIAG_STAT,
368
369 .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN,
370 .self_test_reg = ADIS16240_MSC_CTRL,
371 .self_test_no_autoclear = true,
372 .timeouts = &adis16240_timeouts,
373
374 .status_error_msgs = adis16240_status_error_msgs,
375 .status_error_mask = BIT(ADIS16240_DIAG_STAT_PWRON_FAIL_BIT) |
376 BIT(ADIS16240_DIAG_STAT_SPI_FAIL_BIT) |
377 BIT(ADIS16240_DIAG_STAT_FLASH_UPT_BIT) |
378 BIT(ADIS16240_DIAG_STAT_POWER_HIGH_BIT) |
379 BIT(ADIS16240_DIAG_STAT_POWER_LOW_BIT),
380 };
381
adis16240_probe(struct spi_device * spi)382 static int adis16240_probe(struct spi_device *spi)
383 {
384 int ret;
385 struct adis *st;
386 struct iio_dev *indio_dev;
387
388 /* setup the industrialio driver allocated elements */
389 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
390 if (!indio_dev)
391 return -ENOMEM;
392 st = iio_priv(indio_dev);
393 /* this is only used for removal purposes */
394 spi_set_drvdata(spi, indio_dev);
395
396 indio_dev->name = spi->dev.driver->name;
397 indio_dev->info = &adis16240_info;
398 indio_dev->channels = adis16240_channels;
399 indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
400 indio_dev->modes = INDIO_DIRECT_MODE;
401
402 spi->mode = SPI_MODE_3;
403 ret = spi_setup(spi);
404 if (ret) {
405 dev_err(&spi->dev, "spi_setup failed!\n");
406 return ret;
407 }
408
409 ret = adis_init(st, indio_dev, spi, &adis16240_data);
410 if (ret)
411 return ret;
412 ret = devm_adis_setup_buffer_and_trigger(st, indio_dev, NULL);
413 if (ret)
414 return ret;
415
416 /* Get the device into a sane initial state */
417 ret = __adis_initial_startup(st);
418 if (ret)
419 return ret;
420
421 return devm_iio_device_register(&spi->dev, indio_dev);
422 }
423
424 static const struct of_device_id adis16240_of_match[] = {
425 { .compatible = "adi,adis16240" },
426 { },
427 };
428 MODULE_DEVICE_TABLE(of, adis16240_of_match);
429
430 static struct spi_driver adis16240_driver = {
431 .driver = {
432 .name = "adis16240",
433 .of_match_table = adis16240_of_match,
434 },
435 .probe = adis16240_probe,
436 };
437 module_spi_driver(adis16240_driver);
438
439 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
440 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
441 MODULE_LICENSE("GPL v2");
442 MODULE_ALIAS("spi:adis16240");
443 MODULE_IMPORT_NS(IIO_ADISLIB);
444