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