2 * ADIS16240 Programmable Impact Sensor and Recorder driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/imu/adis.h>
26 #define ADIS16240_STARTUP_DELAY 220 /* ms */
28 /* Flash memory write count */
29 #define ADIS16240_FLASH_CNT 0x00
31 /* Output, power supply */
32 #define ADIS16240_SUPPLY_OUT 0x02
34 /* Output, x-axis accelerometer */
35 #define ADIS16240_XACCL_OUT 0x04
37 /* Output, y-axis accelerometer */
38 #define ADIS16240_YACCL_OUT 0x06
40 /* Output, z-axis accelerometer */
41 #define ADIS16240_ZACCL_OUT 0x08
43 /* Output, auxiliary ADC input */
44 #define ADIS16240_AUX_ADC 0x0A
46 /* Output, temperature */
47 #define ADIS16240_TEMP_OUT 0x0C
49 /* Output, x-axis acceleration peak */
50 #define ADIS16240_XPEAK_OUT 0x0E
52 /* Output, y-axis acceleration peak */
53 #define ADIS16240_YPEAK_OUT 0x10
55 /* Output, z-axis acceleration peak */
56 #define ADIS16240_ZPEAK_OUT 0x12
58 /* Output, sum-of-squares acceleration peak */
59 #define ADIS16240_XYZPEAK_OUT 0x14
61 /* Output, Capture Buffer 1, X and Y acceleration */
62 #define ADIS16240_CAPT_BUF1 0x16
64 /* Output, Capture Buffer 2, Z acceleration */
65 #define ADIS16240_CAPT_BUF2 0x18
67 /* Diagnostic, error flags */
68 #define ADIS16240_DIAG_STAT 0x1A
70 /* Diagnostic, event counter */
71 #define ADIS16240_EVNT_CNTR 0x1C
73 /* Diagnostic, check sum value from firmware test */
74 #define ADIS16240_CHK_SUM 0x1E
76 /* Calibration, x-axis acceleration offset adjustment */
77 #define ADIS16240_XACCL_OFF 0x20
79 /* Calibration, y-axis acceleration offset adjustment */
80 #define ADIS16240_YACCL_OFF 0x22
82 /* Calibration, z-axis acceleration offset adjustment */
83 #define ADIS16240_ZACCL_OFF 0x24
85 /* Clock, hour and minute */
86 #define ADIS16240_CLK_TIME 0x2E
88 /* Clock, month and day */
89 #define ADIS16240_CLK_DATE 0x30
92 #define ADIS16240_CLK_YEAR 0x32
94 /* Wake-up setting, hour and minute */
95 #define ADIS16240_WAKE_TIME 0x34
97 /* Wake-up setting, month and day */
98 #define ADIS16240_WAKE_DATE 0x36
100 /* Alarm 1 amplitude threshold */
101 #define ADIS16240_ALM_MAG1 0x38
103 /* Alarm 2 amplitude threshold */
104 #define ADIS16240_ALM_MAG2 0x3A
107 #define ADIS16240_ALM_CTRL 0x3C
109 /* Capture, external trigger control */
110 #define ADIS16240_XTRIG_CTRL 0x3E
112 /* Capture, address pointer */
113 #define ADIS16240_CAPT_PNTR 0x40
115 /* Capture, configuration and control */
116 #define ADIS16240_CAPT_CTRL 0x42
118 /* General-purpose digital input/output control */
119 #define ADIS16240_GPIO_CTRL 0x44
121 /* Miscellaneous control */
122 #define ADIS16240_MSC_CTRL 0x46
124 /* Internal sample period (rate) control */
125 #define ADIS16240_SMPL_PRD 0x48
128 #define ADIS16240_GLOB_CMD 0x4A
132 /* Enables sum-of-squares output (XYZPEAK_OUT) */
133 #define ADIS16240_MSC_CTRL_XYZPEAK_OUT_EN BIT(15)
135 /* Enables peak tracking output (XPEAK_OUT, YPEAK_OUT, and ZPEAK_OUT) */
136 #define ADIS16240_MSC_CTRL_X_Y_ZPEAK_OUT_EN BIT(14)
138 /* Self-test enable: 1 = apply electrostatic force, 0 = disabled */
139 #define ADIS16240_MSC_CTRL_SELF_TEST_EN BIT(8)
141 /* Data-ready enable: 1 = enabled, 0 = disabled */
142 #define ADIS16240_MSC_CTRL_DATA_RDY_EN BIT(2)
144 /* Data-ready polarity: 1 = active high, 0 = active low */
145 #define ADIS16240_MSC_CTRL_ACTIVE_HIGH BIT(1)
147 /* Data-ready line selection: 1 = DIO2, 0 = DIO1 */
148 #define ADIS16240_MSC_CTRL_DATA_RDY_DIO2 BIT(0)
152 /* Alarm 2 status: 1 = alarm active, 0 = alarm inactive */
153 #define ADIS16240_DIAG_STAT_ALARM2 BIT(9)
155 /* Alarm 1 status: 1 = alarm active, 0 = alarm inactive */
156 #define ADIS16240_DIAG_STAT_ALARM1 BIT(8)
158 /* Capture buffer full: 1 = capture buffer is full */
159 #define ADIS16240_DIAG_STAT_CPT_BUF_FUL BIT(7)
161 /* Flash test, checksum flag: 1 = mismatch, 0 = match */
162 #define ADIS16240_DIAG_STAT_CHKSUM BIT(6)
164 /* Power-on, self-test flag: 1 = failure, 0 = pass */
165 #define ADIS16240_DIAG_STAT_PWRON_FAIL_BIT 5
167 /* Power-on self-test: 1 = in-progress, 0 = complete */
168 #define ADIS16240_DIAG_STAT_PWRON_BUSY BIT(4)
170 /* SPI communications failure */
171 #define ADIS16240_DIAG_STAT_SPI_FAIL_BIT 3
173 /* Flash update failure */
174 #define ADIS16240_DIAG_STAT_FLASH_UPT_BIT 2
176 /* Power supply above 3.625 V */
177 #define ADIS16240_DIAG_STAT_POWER_HIGH_BIT 1
179 /* Power supply below 3.15 V */
180 #define ADIS16240_DIAG_STAT_POWER_LOW_BIT 0
184 #define ADIS16240_GLOB_CMD_RESUME BIT(8)
185 #define ADIS16240_GLOB_CMD_SW_RESET BIT(7)
186 #define ADIS16240_GLOB_CMD_STANDBY BIT(2)
188 #define ADIS16240_ERROR_ACTIVE BIT(14)
190 /* At the moment triggers are only used for ring buffer
191 * filling. This may change!
194 enum adis16240_scan {
195 ADIS16240_SCAN_ACC_X,
196 ADIS16240_SCAN_ACC_Y,
197 ADIS16240_SCAN_ACC_Z,
198 ADIS16240_SCAN_SUPPLY,
199 ADIS16240_SCAN_AUX_ADC,
203 static ssize_t adis16240_spi_read_signed(struct device *dev,
204 struct device_attribute *attr,
208 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
209 struct adis *st = iio_priv(indio_dev);
212 unsigned int shift = 16 - bits;
213 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
215 ret = adis_read_reg_16(st,
216 this_attr->address, (u16 *)&val);
220 if (val & ADIS16240_ERROR_ACTIVE)
221 adis_check_status(st);
223 val = (s16)(val << shift) >> shift;
224 return sprintf(buf, "%d\n", val);
227 static ssize_t adis16240_read_12bit_signed(struct device *dev,
228 struct device_attribute *attr,
231 return adis16240_spi_read_signed(dev, attr, buf, 12);
234 static IIO_DEVICE_ATTR(in_accel_xyz_squared_peak_raw, 0444,
235 adis16240_read_12bit_signed, NULL,
236 ADIS16240_XYZPEAK_OUT);
238 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
240 static const u8 adis16240_addresses[][2] = {
241 [ADIS16240_SCAN_ACC_X] = { ADIS16240_XACCL_OFF, ADIS16240_XPEAK_OUT },
242 [ADIS16240_SCAN_ACC_Y] = { ADIS16240_YACCL_OFF, ADIS16240_YPEAK_OUT },
243 [ADIS16240_SCAN_ACC_Z] = { ADIS16240_ZACCL_OFF, ADIS16240_ZPEAK_OUT },
246 static int adis16240_read_raw(struct iio_dev *indio_dev,
247 struct iio_chan_spec const *chan,
251 struct adis *st = iio_priv(indio_dev);
258 case IIO_CHAN_INFO_RAW:
259 return adis_single_conversion(indio_dev, chan,
260 ADIS16240_ERROR_ACTIVE, val);
261 case IIO_CHAN_INFO_SCALE:
262 switch (chan->type) {
264 if (chan->channel == 0) {
266 *val2 = 880000; /* 4.88 mV */
267 return IIO_VAL_INT_PLUS_MICRO;
271 *val = 244; /* 0.244 C */
273 return IIO_VAL_INT_PLUS_MICRO;
276 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
277 return IIO_VAL_INT_PLUS_MICRO;
282 case IIO_CHAN_INFO_PEAK_SCALE:
284 *val2 = IIO_G_TO_M_S_2(51400); /* 51.4 mg */
285 return IIO_VAL_INT_PLUS_MICRO;
286 case IIO_CHAN_INFO_OFFSET:
287 *val = 25000 / 244 - 0x133; /* 25 C = 0x133 */
289 case IIO_CHAN_INFO_CALIBBIAS:
291 addr = adis16240_addresses[chan->scan_index][0];
292 ret = adis_read_reg_16(st, addr, &val16);
295 val16 &= (1 << bits) - 1;
296 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
299 case IIO_CHAN_INFO_PEAK:
301 addr = adis16240_addresses[chan->scan_index][1];
302 ret = adis_read_reg_16(st, addr, &val16);
305 val16 &= (1 << bits) - 1;
306 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
313 static int adis16240_write_raw(struct iio_dev *indio_dev,
314 struct iio_chan_spec const *chan,
319 struct adis *st = iio_priv(indio_dev);
325 case IIO_CHAN_INFO_CALIBBIAS:
326 val16 = val & ((1 << bits) - 1);
327 addr = adis16240_addresses[chan->scan_index][0];
328 return adis_write_reg_16(st, addr, val16);
333 static const struct iio_chan_spec adis16240_channels[] = {
334 ADIS_SUPPLY_CHAN(ADIS16240_SUPPLY_OUT, ADIS16240_SCAN_SUPPLY, 0, 10),
335 ADIS_AUX_ADC_CHAN(ADIS16240_AUX_ADC, ADIS16240_SCAN_AUX_ADC, 0, 10),
336 ADIS_ACCEL_CHAN(X, ADIS16240_XACCL_OUT, ADIS16240_SCAN_ACC_X,
337 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
339 ADIS_ACCEL_CHAN(Y, ADIS16240_YACCL_OUT, ADIS16240_SCAN_ACC_Y,
340 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
342 ADIS_ACCEL_CHAN(Z, ADIS16240_ZACCL_OUT, ADIS16240_SCAN_ACC_Z,
343 BIT(IIO_CHAN_INFO_CALIBBIAS) | BIT(IIO_CHAN_INFO_PEAK),
345 ADIS_TEMP_CHAN(ADIS16240_TEMP_OUT, ADIS16240_SCAN_TEMP, 0, 10),
346 IIO_CHAN_SOFT_TIMESTAMP(6)
349 static struct attribute *adis16240_attributes[] = {
350 &iio_dev_attr_in_accel_xyz_squared_peak_raw.dev_attr.attr,
351 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
355 static const struct attribute_group adis16240_attribute_group = {
356 .attrs = adis16240_attributes,
359 static const struct iio_info adis16240_info = {
360 .attrs = &adis16240_attribute_group,
361 .read_raw = adis16240_read_raw,
362 .write_raw = adis16240_write_raw,
363 .update_scan_mode = adis_update_scan_mode,
364 .driver_module = THIS_MODULE,
367 static const char * const adis16240_status_error_msgs[] = {
368 [ADIS16240_DIAG_STAT_PWRON_FAIL_BIT] = "Power on, self-test failed",
369 [ADIS16240_DIAG_STAT_SPI_FAIL_BIT] = "SPI failure",
370 [ADIS16240_DIAG_STAT_FLASH_UPT_BIT] = "Flash update failed",
371 [ADIS16240_DIAG_STAT_POWER_HIGH_BIT] = "Power supply above 3.625V",
372 [ADIS16240_DIAG_STAT_POWER_LOW_BIT] = "Power supply below 2.225V",
375 static const struct adis_data adis16240_data = {
378 .msc_ctrl_reg = ADIS16240_MSC_CTRL,
379 .glob_cmd_reg = ADIS16240_GLOB_CMD,
380 .diag_stat_reg = ADIS16240_DIAG_STAT,
382 .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN,
383 .self_test_no_autoclear = true,
384 .startup_delay = ADIS16240_STARTUP_DELAY,
386 .status_error_msgs = adis16240_status_error_msgs,
387 .status_error_mask = BIT(ADIS16240_DIAG_STAT_PWRON_FAIL_BIT) |
388 BIT(ADIS16240_DIAG_STAT_SPI_FAIL_BIT) |
389 BIT(ADIS16240_DIAG_STAT_FLASH_UPT_BIT) |
390 BIT(ADIS16240_DIAG_STAT_POWER_HIGH_BIT) |
391 BIT(ADIS16240_DIAG_STAT_POWER_LOW_BIT),
394 static int adis16240_probe(struct spi_device *spi)
398 struct iio_dev *indio_dev;
400 /* setup the industrialio driver allocated elements */
401 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
404 st = iio_priv(indio_dev);
405 /* this is only used for removal purposes */
406 spi_set_drvdata(spi, indio_dev);
408 indio_dev->name = spi->dev.driver->name;
409 indio_dev->dev.parent = &spi->dev;
410 indio_dev->info = &adis16240_info;
411 indio_dev->channels = adis16240_channels;
412 indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
413 indio_dev->modes = INDIO_DIRECT_MODE;
415 ret = adis_init(st, indio_dev, spi, &adis16240_data);
418 ret = adis_setup_buffer_and_trigger(st, indio_dev, NULL);
422 /* Get the device into a sane initial state */
423 ret = adis_initial_startup(st);
425 goto error_cleanup_buffer_trigger;
426 ret = iio_device_register(indio_dev);
428 goto error_cleanup_buffer_trigger;
431 error_cleanup_buffer_trigger:
432 adis_cleanup_buffer_and_trigger(st, indio_dev);
436 static int adis16240_remove(struct spi_device *spi)
438 struct iio_dev *indio_dev = spi_get_drvdata(spi);
439 struct adis *st = iio_priv(indio_dev);
441 iio_device_unregister(indio_dev);
442 adis_cleanup_buffer_and_trigger(st, indio_dev);
447 static struct spi_driver adis16240_driver = {
451 .probe = adis16240_probe,
452 .remove = adis16240_remove,
454 module_spi_driver(adis16240_driver);
456 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
457 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
458 MODULE_LICENSE("GPL v2");
459 MODULE_ALIAS("spi:adis16240");