1 /*
2  * ADIS16201 Programmable Digital Vibration Sensor driver
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/mutex.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/module.h>
17 
18 #include "../iio.h"
19 #include "../sysfs.h"
20 #include "../buffer.h"
21 
22 #include "adis16201.h"
23 
24 enum adis16201_chan {
25 	in_supply,
26 	temp,
27 	accel_x,
28 	accel_y,
29 	incli_x,
30 	incli_y,
31 	in_aux,
32 };
33 
34 /**
35  * adis16201_spi_write_reg_8() - write single byte to a register
36  * @dev: device associated with child of actual device (iio_dev or iio_trig)
37  * @reg_address: the address of the register to be written
38  * @val: the value to write
39  **/
adis16201_spi_write_reg_8(struct iio_dev * indio_dev,u8 reg_address,u8 val)40 static int adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
41 		u8 reg_address,
42 		u8 val)
43 {
44 	int ret;
45 	struct adis16201_state *st = iio_priv(indio_dev);
46 
47 	mutex_lock(&st->buf_lock);
48 	st->tx[0] = ADIS16201_WRITE_REG(reg_address);
49 	st->tx[1] = val;
50 
51 	ret = spi_write(st->us, st->tx, 2);
52 	mutex_unlock(&st->buf_lock);
53 
54 	return ret;
55 }
56 
57 /**
58  * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
59  * @indio_dev: iio device associated with child of actual device
60  * @reg_address: the address of the lower of the two registers. Second register
61  *               is assumed to have address one greater.
62  * @val: value to be written
63  **/
adis16201_spi_write_reg_16(struct iio_dev * indio_dev,u8 lower_reg_address,u16 value)64 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
65 				      u8 lower_reg_address,
66 				      u16 value)
67 {
68 	int ret;
69 	struct spi_message msg;
70 	struct adis16201_state *st = iio_priv(indio_dev);
71 	struct spi_transfer xfers[] = {
72 		{
73 			.tx_buf = st->tx,
74 			.bits_per_word = 8,
75 			.len = 2,
76 			.cs_change = 1,
77 		}, {
78 			.tx_buf = st->tx + 2,
79 			.bits_per_word = 8,
80 			.len = 2,
81 		},
82 	};
83 
84 	mutex_lock(&st->buf_lock);
85 	st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
86 	st->tx[1] = value & 0xFF;
87 	st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1);
88 	st->tx[3] = (value >> 8) & 0xFF;
89 
90 	spi_message_init(&msg);
91 	spi_message_add_tail(&xfers[0], &msg);
92 	spi_message_add_tail(&xfers[1], &msg);
93 	ret = spi_sync(st->us, &msg);
94 	mutex_unlock(&st->buf_lock);
95 
96 	return ret;
97 }
98 
99 /**
100  * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
101  * @indio_dev: iio device associated with child of actual device
102  * @reg_address: the address of the lower of the two registers. Second register
103  *               is assumed to have address one greater.
104  * @val: somewhere to pass back the value read
105  **/
adis16201_spi_read_reg_16(struct iio_dev * indio_dev,u8 lower_reg_address,u16 * val)106 static int adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
107 		u8 lower_reg_address,
108 		u16 *val)
109 {
110 	struct spi_message msg;
111 	struct adis16201_state *st = iio_priv(indio_dev);
112 	int ret;
113 	struct spi_transfer xfers[] = {
114 		{
115 			.tx_buf = st->tx,
116 			.bits_per_word = 8,
117 			.len = 2,
118 			.cs_change = 1,
119 			.delay_usecs = 20,
120 		}, {
121 			.rx_buf = st->rx,
122 			.bits_per_word = 8,
123 			.len = 2,
124 			.delay_usecs = 20,
125 		},
126 	};
127 
128 	mutex_lock(&st->buf_lock);
129 	st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
130 	st->tx[1] = 0;
131 
132 	spi_message_init(&msg);
133 	spi_message_add_tail(&xfers[0], &msg);
134 	spi_message_add_tail(&xfers[1], &msg);
135 	ret = spi_sync(st->us, &msg);
136 	if (ret) {
137 		dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
138 				lower_reg_address);
139 		goto error_ret;
140 	}
141 	*val = (st->rx[0] << 8) | st->rx[1];
142 
143 error_ret:
144 	mutex_unlock(&st->buf_lock);
145 	return ret;
146 }
147 
adis16201_reset(struct iio_dev * indio_dev)148 static int adis16201_reset(struct iio_dev *indio_dev)
149 {
150 	int ret;
151 	struct adis16201_state *st = iio_priv(indio_dev);
152 
153 	ret = adis16201_spi_write_reg_8(indio_dev,
154 			ADIS16201_GLOB_CMD,
155 			ADIS16201_GLOB_CMD_SW_RESET);
156 	if (ret)
157 		dev_err(&st->us->dev, "problem resetting device");
158 
159 	return ret;
160 }
161 
adis16201_write_reset(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)162 static ssize_t adis16201_write_reset(struct device *dev,
163 		struct device_attribute *attr,
164 		const char *buf, size_t len)
165 {
166 	int ret;
167 	bool res;
168 
169 	if (len < 1)
170 		return -EINVAL;
171 	ret = strtobool(buf, &res);
172 	if (ret || !res)
173 		return ret;
174 	return adis16201_reset(dev_get_drvdata(dev));
175 }
176 
adis16201_set_irq(struct iio_dev * indio_dev,bool enable)177 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
178 {
179 	int ret = 0;
180 	u16 msc;
181 
182 	ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
183 	if (ret)
184 		goto error_ret;
185 
186 	msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
187 	msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
188 	if (enable)
189 		msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
190 	else
191 		msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
192 
193 	ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
194 
195 error_ret:
196 	return ret;
197 }
198 
adis16201_check_status(struct iio_dev * indio_dev)199 static int adis16201_check_status(struct iio_dev *indio_dev)
200 {
201 	u16 status;
202 	int ret;
203 
204 	ret = adis16201_spi_read_reg_16(indio_dev,
205 					ADIS16201_DIAG_STAT, &status);
206 	if (ret < 0) {
207 		dev_err(&indio_dev->dev, "Reading status failed\n");
208 		goto error_ret;
209 	}
210 	ret = status & 0xF;
211 	if (ret)
212 		ret = -EFAULT;
213 
214 	if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
215 		dev_err(&indio_dev->dev, "SPI failure\n");
216 	if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
217 		dev_err(&indio_dev->dev, "Flash update failed\n");
218 	if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
219 		dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
220 	if (status & ADIS16201_DIAG_STAT_POWER_LOW)
221 		dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
222 
223 error_ret:
224 	return ret;
225 }
226 
adis16201_self_test(struct iio_dev * indio_dev)227 static int adis16201_self_test(struct iio_dev *indio_dev)
228 {
229 	int ret;
230 	ret = adis16201_spi_write_reg_16(indio_dev,
231 			ADIS16201_MSC_CTRL,
232 			ADIS16201_MSC_CTRL_SELF_TEST_EN);
233 	if (ret) {
234 		dev_err(&indio_dev->dev, "problem starting self test");
235 		goto err_ret;
236 	}
237 
238 	ret = adis16201_check_status(indio_dev);
239 
240 err_ret:
241 	return ret;
242 }
243 
adis16201_initial_setup(struct iio_dev * indio_dev)244 static int adis16201_initial_setup(struct iio_dev *indio_dev)
245 {
246 	int ret;
247 	struct device *dev = &indio_dev->dev;
248 
249 	/* Disable IRQ */
250 	ret = adis16201_set_irq(indio_dev, false);
251 	if (ret) {
252 		dev_err(dev, "disable irq failed");
253 		goto err_ret;
254 	}
255 
256 	/* Do self test */
257 	ret = adis16201_self_test(indio_dev);
258 	if (ret) {
259 		dev_err(dev, "self test failure");
260 		goto err_ret;
261 	}
262 
263 	/* Read status register to check the result */
264 	ret = adis16201_check_status(indio_dev);
265 	if (ret) {
266 		adis16201_reset(indio_dev);
267 		dev_err(dev, "device not playing ball -> reset");
268 		msleep(ADIS16201_STARTUP_DELAY);
269 		ret = adis16201_check_status(indio_dev);
270 		if (ret) {
271 			dev_err(dev, "giving up");
272 			goto err_ret;
273 		}
274 	}
275 
276 err_ret:
277 	return ret;
278 }
279 
280 static u8 adis16201_addresses[7][2] = {
281 	[in_supply] = { ADIS16201_SUPPLY_OUT, },
282 	[temp] = { ADIS16201_TEMP_OUT },
283 	[accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
284 	[accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
285 	[in_aux] = { ADIS16201_AUX_ADC },
286 	[incli_x] = { ADIS16201_XINCL_OUT },
287 	[incli_y] = { ADIS16201_YINCL_OUT },
288 };
289 
adis16201_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)290 static int adis16201_read_raw(struct iio_dev *indio_dev,
291 			      struct iio_chan_spec const *chan,
292 			      int *val, int *val2,
293 			      long mask)
294 {
295 	int ret;
296 	int bits;
297 	u8 addr;
298 	s16 val16;
299 
300 	switch (mask) {
301 	case 0:
302 		mutex_lock(&indio_dev->mlock);
303 		addr = adis16201_addresses[chan->address][0];
304 		ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
305 		if (ret) {
306 			mutex_unlock(&indio_dev->mlock);
307 			return ret;
308 		}
309 
310 		if (val16 & ADIS16201_ERROR_ACTIVE) {
311 			ret = adis16201_check_status(indio_dev);
312 			if (ret) {
313 				mutex_unlock(&indio_dev->mlock);
314 				return ret;
315 			}
316 		}
317 		val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
318 		if (chan->scan_type.sign == 's')
319 			val16 = (s16)(val16 <<
320 				      (16 - chan->scan_type.realbits)) >>
321 				(16 - chan->scan_type.realbits);
322 		*val = val16;
323 		mutex_unlock(&indio_dev->mlock);
324 		return IIO_VAL_INT;
325 	case IIO_CHAN_INFO_SCALE:
326 		switch (chan->type) {
327 		case IIO_VOLTAGE:
328 			*val = 0;
329 			if (chan->channel == 0)
330 				*val2 = 1220;
331 			else
332 				*val2 = 610;
333 			return IIO_VAL_INT_PLUS_MICRO;
334 		case IIO_TEMP:
335 			*val = 0;
336 			*val2 = -470000;
337 			return IIO_VAL_INT_PLUS_MICRO;
338 		case IIO_ACCEL:
339 			*val = 0;
340 			*val2 = 462500;
341 			return IIO_VAL_INT_PLUS_MICRO;
342 		case IIO_INCLI:
343 			*val = 0;
344 			*val2 = 100000;
345 			return IIO_VAL_INT_PLUS_MICRO;
346 		default:
347 			return -EINVAL;
348 		}
349 		break;
350 	case IIO_CHAN_INFO_OFFSET:
351 		*val = 25;
352 		return IIO_VAL_INT;
353 	case IIO_CHAN_INFO_CALIBBIAS:
354 		switch (chan->type) {
355 		case IIO_ACCEL:
356 			bits = 12;
357 			break;
358 		case IIO_INCLI:
359 			bits = 9;
360 			break;
361 		default:
362 			return -EINVAL;
363 		};
364 		mutex_lock(&indio_dev->mlock);
365 		addr = adis16201_addresses[chan->address][1];
366 		ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
367 		if (ret) {
368 			mutex_unlock(&indio_dev->mlock);
369 			return ret;
370 		}
371 		val16 &= (1 << bits) - 1;
372 		val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
373 		*val = val16;
374 		mutex_unlock(&indio_dev->mlock);
375 		return IIO_VAL_INT;
376 	}
377 	return -EINVAL;
378 }
379 
adis16201_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)380 static int adis16201_write_raw(struct iio_dev *indio_dev,
381 			       struct iio_chan_spec const *chan,
382 			       int val,
383 			       int val2,
384 			       long mask)
385 {
386 	int bits;
387 	s16 val16;
388 	u8 addr;
389 	switch (mask) {
390 	case IIO_CHAN_INFO_CALIBBIAS:
391 		switch (chan->type) {
392 		case IIO_ACCEL:
393 			bits = 12;
394 			break;
395 		case IIO_INCLI:
396 			bits = 9;
397 			break;
398 		default:
399 			return -EINVAL;
400 		};
401 		val16 = val & ((1 << bits) - 1);
402 		addr = adis16201_addresses[chan->address][1];
403 		return adis16201_spi_write_reg_16(indio_dev, addr, val16);
404 	}
405 	return -EINVAL;
406 }
407 
408 static struct iio_chan_spec adis16201_channels[] = {
409 	IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, "supply", 0, 0,
410 		 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
411 		 in_supply, ADIS16201_SCAN_SUPPLY,
412 		 IIO_ST('u', 12, 16, 0), 0),
413 	IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
414 		 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
415 		 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
416 		 temp, ADIS16201_SCAN_TEMP,
417 		 IIO_ST('u', 12, 16, 0), 0),
418 	IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
419 		 IIO_CHAN_INFO_SCALE_SHARED_BIT |
420 		 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
421 		 accel_x, ADIS16201_SCAN_ACC_X,
422 		 IIO_ST('s', 14, 16, 0), 0),
423 	IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
424 		 IIO_CHAN_INFO_SCALE_SHARED_BIT |
425 		 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
426 		 accel_y, ADIS16201_SCAN_ACC_Y,
427 		 IIO_ST('s', 14, 16, 0), 0),
428 	IIO_CHAN(IIO_VOLTAGE, 0, 1, 0, NULL, 1, 0,
429 		 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
430 		 in_aux, ADIS16201_SCAN_AUX_ADC,
431 		 IIO_ST('u', 12, 16, 0), 0),
432 	IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
433 		 IIO_CHAN_INFO_SCALE_SHARED_BIT |
434 		 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
435 		 incli_x, ADIS16201_SCAN_INCLI_X,
436 		 IIO_ST('s', 14, 16, 0), 0),
437 	IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
438 		 IIO_CHAN_INFO_SCALE_SHARED_BIT |
439 		 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
440 		 incli_y, ADIS16201_SCAN_INCLI_Y,
441 		 IIO_ST('s', 14, 16, 0), 0),
442 	IIO_CHAN_SOFT_TIMESTAMP(7)
443 };
444 
445 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0);
446 
447 static struct attribute *adis16201_attributes[] = {
448 	&iio_dev_attr_reset.dev_attr.attr,
449 	NULL
450 };
451 
452 static const struct attribute_group adis16201_attribute_group = {
453 	.attrs = adis16201_attributes,
454 };
455 
456 static const struct iio_info adis16201_info = {
457 	.attrs = &adis16201_attribute_group,
458 	.read_raw = &adis16201_read_raw,
459 	.write_raw = &adis16201_write_raw,
460 	.driver_module = THIS_MODULE,
461 };
462 
adis16201_probe(struct spi_device * spi)463 static int __devinit adis16201_probe(struct spi_device *spi)
464 {
465 	int ret;
466 	struct adis16201_state *st;
467 	struct iio_dev *indio_dev;
468 
469 	/* setup the industrialio driver allocated elements */
470 	indio_dev = iio_allocate_device(sizeof(*st));
471 	if (indio_dev == NULL) {
472 		ret = -ENOMEM;
473 		goto error_ret;
474 	}
475 	st = iio_priv(indio_dev);
476 	/* this is only used for removal purposes */
477 	spi_set_drvdata(spi, indio_dev);
478 
479 	st->us = spi;
480 	mutex_init(&st->buf_lock);
481 
482 	indio_dev->name = spi->dev.driver->name;
483 	indio_dev->dev.parent = &spi->dev;
484 	indio_dev->info = &adis16201_info;
485 
486 	indio_dev->channels = adis16201_channels;
487 	indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
488 	indio_dev->modes = INDIO_DIRECT_MODE;
489 
490 	ret = adis16201_configure_ring(indio_dev);
491 	if (ret)
492 		goto error_free_dev;
493 
494 	ret = iio_buffer_register(indio_dev,
495 				  adis16201_channels,
496 				  ARRAY_SIZE(adis16201_channels));
497 	if (ret) {
498 		printk(KERN_ERR "failed to initialize the ring\n");
499 		goto error_unreg_ring_funcs;
500 	}
501 
502 	if (spi->irq) {
503 		ret = adis16201_probe_trigger(indio_dev);
504 		if (ret)
505 			goto error_uninitialize_ring;
506 	}
507 
508 	/* Get the device into a sane initial state */
509 	ret = adis16201_initial_setup(indio_dev);
510 	if (ret)
511 		goto error_remove_trigger;
512 
513 	ret = iio_device_register(indio_dev);
514 	if (ret < 0)
515 		goto error_remove_trigger;
516 	return 0;
517 
518 error_remove_trigger:
519 	adis16201_remove_trigger(indio_dev);
520 error_uninitialize_ring:
521 	iio_buffer_unregister(indio_dev);
522 error_unreg_ring_funcs:
523 	adis16201_unconfigure_ring(indio_dev);
524 error_free_dev:
525 	iio_free_device(indio_dev);
526 error_ret:
527 	return ret;
528 }
529 
adis16201_remove(struct spi_device * spi)530 static int adis16201_remove(struct spi_device *spi)
531 {
532 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
533 
534 	iio_device_unregister(indio_dev);
535 	adis16201_remove_trigger(indio_dev);
536 	iio_buffer_unregister(indio_dev);
537 	adis16201_unconfigure_ring(indio_dev);
538 	iio_free_device(indio_dev);
539 
540 	return 0;
541 }
542 
543 static struct spi_driver adis16201_driver = {
544 	.driver = {
545 		.name = "adis16201",
546 		.owner = THIS_MODULE,
547 	},
548 	.probe = adis16201_probe,
549 	.remove = __devexit_p(adis16201_remove),
550 };
551 module_spi_driver(adis16201_driver);
552 
553 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
554 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
555 MODULE_LICENSE("GPL v2");
556 MODULE_ALIAS("spi:adis16201");
557