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