1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer
5 * Copyright 2010 Analog Devices Inc.
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>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/imu/adis.h>
19 #define ADIS16201_STARTUP_DELAY_MS 220
20 #define ADIS16201_FLASH_CNT 0x00
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
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
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
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
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)
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
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)
81 #define ADIS16201_ERROR_ACTIVE BIT(14)
86 ADIS16201_SCAN_INCLI_X
,
87 ADIS16201_SCAN_INCLI_Y
,
88 ADIS16201_SCAN_SUPPLY
,
89 ADIS16201_SCAN_AUX_ADC
,
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
,
100 static int adis16201_read_raw(struct iio_dev
*indio_dev
,
101 struct iio_chan_spec
const *chan
,
105 struct adis
*st
= iio_priv(indio_dev
);
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
) {
118 if (chan
->channel
== 0) {
119 /* Voltage base units are mV hence 1.22 mV */
123 /* Voltage base units are mV hence 0.61 mV */
127 return IIO_VAL_INT_PLUS_MICRO
;
131 return IIO_VAL_INT_PLUS_MICRO
;
134 * IIO base unit for sensitivity of accelerometer
136 * 1 LSB represents 0.244 mg.
139 *val2
= IIO_G_TO_M_S_2(462400);
140 return IIO_VAL_INT_PLUS_NANO
;
144 return IIO_VAL_INT_PLUS_MICRO
;
149 case IIO_CHAN_INFO_OFFSET
:
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.
155 *val
= 25000 / -470 - 1278;
157 case IIO_CHAN_INFO_CALIBBIAS
:
158 switch (chan
->type
) {
168 addr
= adis16201_addresses
[chan
->scan_index
];
169 ret
= adis_read_reg_16(st
, addr
, &val16
);
173 *val
= sign_extend32(val16
, bits
- 1);
180 static int adis16201_write_raw(struct iio_dev
*indio_dev
,
181 struct iio_chan_spec
const *chan
,
186 struct adis
*st
= iio_priv(indio_dev
);
189 if (mask
!= IIO_CHAN_INFO_CALIBBIAS
)
192 switch (chan
->type
) {
203 return adis_write_reg_16(st
, adis16201_addresses
[chan
->scan_index
],
207 static const struct iio_chan_spec adis16201_channels
[] = {
208 ADIS_SUPPLY_CHAN(ADIS16201_SUPPLY_OUT_REG
, ADIS16201_SCAN_SUPPLY
, 0,
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(X
, ADIS16201_YINCL_OUT_REG
, ADIS16201_SCAN_INCLI_Y
,
219 BIT(IIO_CHAN_INFO_CALIBBIAS
), 0, 14),
220 IIO_CHAN_SOFT_TIMESTAMP(7)
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
,
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",
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
,
242 static const struct adis_data adis16201_data
= {
244 .msc_ctrl_reg
= ADIS16201_MSC_CTRL_REG
,
245 .glob_cmd_reg
= ADIS16201_GLOB_CMD_REG
,
246 .diag_stat_reg
= ADIS16201_DIAG_STAT_REG
,
248 .self_test_mask
= ADIS16201_MSC_CTRL_SELF_TEST_EN
,
249 .self_test_no_autoclear
= true,
250 .timeouts
= &adis16201_timeouts
,
252 .status_error_msgs
= adis16201_status_error_msgs
,
253 .status_error_mask
= BIT(ADIS16201_DIAG_STAT_SPI_FAIL_BIT
) |
254 BIT(ADIS16201_DIAG_STAT_FLASH_UPT_FAIL_BIT
) |
255 BIT(ADIS16201_DIAG_STAT_POWER_HIGH_BIT
) |
256 BIT(ADIS16201_DIAG_STAT_POWER_LOW_BIT
),
259 static int adis16201_probe(struct spi_device
*spi
)
261 struct iio_dev
*indio_dev
;
265 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
269 st
= iio_priv(indio_dev
);
270 spi_set_drvdata(spi
, indio_dev
);
272 indio_dev
->name
= spi
->dev
.driver
->name
;
273 indio_dev
->dev
.parent
= &spi
->dev
;
274 indio_dev
->info
= &adis16201_info
;
276 indio_dev
->channels
= adis16201_channels
;
277 indio_dev
->num_channels
= ARRAY_SIZE(adis16201_channels
);
278 indio_dev
->modes
= INDIO_DIRECT_MODE
;
280 ret
= adis_init(st
, indio_dev
, spi
, &adis16201_data
);
284 ret
= adis_setup_buffer_and_trigger(st
, indio_dev
, NULL
);
288 ret
= adis_initial_startup(st
);
290 goto error_cleanup_buffer_trigger
;
292 ret
= iio_device_register(indio_dev
);
294 goto error_cleanup_buffer_trigger
;
298 error_cleanup_buffer_trigger
:
299 adis_cleanup_buffer_and_trigger(st
, indio_dev
);
303 static int adis16201_remove(struct spi_device
*spi
)
305 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
306 struct adis
*st
= iio_priv(indio_dev
);
308 iio_device_unregister(indio_dev
);
309 adis_cleanup_buffer_and_trigger(st
, indio_dev
);
314 static struct spi_driver adis16201_driver
= {
318 .probe
= adis16201_probe
,
319 .remove
= adis16201_remove
,
321 module_spi_driver(adis16201_driver
);
323 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
324 MODULE_DESCRIPTION("Analog Devices ADIS16201 Dual-Axis Digital Inclinometer and Accelerometer");
325 MODULE_LICENSE("GPL v2");
326 MODULE_ALIAS("spi:adis16201");