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