1 /*
2  * ADIS16220 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 
21 #include "adis16220.h"
22 
23 #define DRIVER_NAME		"adis16220"
24 
25 /**
26  * adis16220_spi_write_reg_8() - write single byte to a register
27  * @indio_dev: iio device associated with child of actual device
28  * @reg_address: the address of the register to be written
29  * @val: the value to write
30  **/
adis16220_spi_write_reg_8(struct iio_dev * indio_dev,u8 reg_address,u8 val)31 static int adis16220_spi_write_reg_8(struct iio_dev *indio_dev,
32 		u8 reg_address,
33 		u8 val)
34 {
35 	int ret;
36 	struct adis16220_state *st = iio_priv(indio_dev);
37 
38 	mutex_lock(&st->buf_lock);
39 	st->tx[0] = ADIS16220_WRITE_REG(reg_address);
40 	st->tx[1] = val;
41 
42 	ret = spi_write(st->us, st->tx, 2);
43 	mutex_unlock(&st->buf_lock);
44 
45 	return ret;
46 }
47 
48 /**
49  * adis16220_spi_write_reg_16() - write 2 bytes to a pair of registers
50  * @indio_dev:  iio device associated with child of actual device
51  * @reg_address: the address of the lower of the two registers. Second register
52  *               is assumed to have address one greater.
53  * @val: value to be written
54  **/
adis16220_spi_write_reg_16(struct iio_dev * indio_dev,u8 lower_reg_address,u16 value)55 static int adis16220_spi_write_reg_16(struct iio_dev *indio_dev,
56 		u8 lower_reg_address,
57 		u16 value)
58 {
59 	int ret;
60 	struct spi_message msg;
61 	struct adis16220_state *st = iio_priv(indio_dev);
62 	struct spi_transfer xfers[] = {
63 		{
64 			.tx_buf = st->tx,
65 			.bits_per_word = 8,
66 			.len = 2,
67 			.cs_change = 1,
68 			.delay_usecs = 35,
69 		}, {
70 			.tx_buf = st->tx + 2,
71 			.bits_per_word = 8,
72 			.len = 2,
73 			.delay_usecs = 35,
74 		},
75 	};
76 
77 	mutex_lock(&st->buf_lock);
78 	st->tx[0] = ADIS16220_WRITE_REG(lower_reg_address);
79 	st->tx[1] = value & 0xFF;
80 	st->tx[2] = ADIS16220_WRITE_REG(lower_reg_address + 1);
81 	st->tx[3] = (value >> 8) & 0xFF;
82 
83 	spi_message_init(&msg);
84 	spi_message_add_tail(&xfers[0], &msg);
85 	spi_message_add_tail(&xfers[1], &msg);
86 	ret = spi_sync(st->us, &msg);
87 	mutex_unlock(&st->buf_lock);
88 
89 	return ret;
90 }
91 
92 /**
93  * adis16220_spi_read_reg_16() - read 2 bytes from a 16-bit register
94  * @indio_dev: iio device associated with child of actual device
95  * @reg_address: the address of the lower of the two registers. Second register
96  *               is assumed to have address one greater.
97  * @val: somewhere to pass back the value read
98  **/
adis16220_spi_read_reg_16(struct iio_dev * indio_dev,u8 lower_reg_address,u16 * val)99 static int adis16220_spi_read_reg_16(struct iio_dev *indio_dev,
100 				     u8 lower_reg_address,
101 				     u16 *val)
102 {
103 	struct spi_message msg;
104 	struct adis16220_state *st = iio_priv(indio_dev);
105 	int ret;
106 	struct spi_transfer xfers[] = {
107 		{
108 			.tx_buf = st->tx,
109 			.bits_per_word = 8,
110 			.len = 2,
111 			.cs_change = 1,
112 			.delay_usecs = 35,
113 		}, {
114 			.rx_buf = st->rx,
115 			.bits_per_word = 8,
116 			.len = 2,
117 			.cs_change = 1,
118 			.delay_usecs = 35,
119 		},
120 	};
121 
122 	mutex_lock(&st->buf_lock);
123 	st->tx[0] = ADIS16220_READ_REG(lower_reg_address);
124 	st->tx[1] = 0;
125 
126 	spi_message_init(&msg);
127 	spi_message_add_tail(&xfers[0], &msg);
128 	spi_message_add_tail(&xfers[1], &msg);
129 	ret = spi_sync(st->us, &msg);
130 	if (ret) {
131 		dev_err(&st->us->dev,
132 			"problem when reading 16 bit register 0x%02X",
133 			lower_reg_address);
134 		goto error_ret;
135 	}
136 	*val = (st->rx[0] << 8) | st->rx[1];
137 
138 error_ret:
139 	mutex_unlock(&st->buf_lock);
140 	return ret;
141 }
142 
adis16220_read_16bit(struct device * dev,struct device_attribute * attr,char * buf)143 static ssize_t adis16220_read_16bit(struct device *dev,
144 		struct device_attribute *attr,
145 		char *buf)
146 {
147 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
148 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
149 	ssize_t ret;
150 	s16 val = 0;
151 
152 	/* Take the iio_dev status lock */
153 	mutex_lock(&indio_dev->mlock);
154 	ret = adis16220_spi_read_reg_16(indio_dev, this_attr->address,
155 					(u16 *)&val);
156 	mutex_unlock(&indio_dev->mlock);
157 	if (ret)
158 		return ret;
159 	return sprintf(buf, "%d\n", val);
160 }
161 
adis16220_write_16bit(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)162 static ssize_t adis16220_write_16bit(struct device *dev,
163 		struct device_attribute *attr,
164 		const char *buf,
165 		size_t len)
166 {
167 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
168 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
169 	int ret;
170 	u16 val;
171 
172 	ret = kstrtou16(buf, 10, &val);
173 	if (ret)
174 		goto error_ret;
175 	ret = adis16220_spi_write_reg_16(indio_dev, this_attr->address, val);
176 
177 error_ret:
178 	return ret ? ret : len;
179 }
180 
adis16220_capture(struct iio_dev * indio_dev)181 static int adis16220_capture(struct iio_dev *indio_dev)
182 {
183 	int ret;
184 	ret = adis16220_spi_write_reg_16(indio_dev,
185 			ADIS16220_GLOB_CMD,
186 			0xBF08); /* initiates a manual data capture */
187 	if (ret)
188 		dev_err(&indio_dev->dev, "problem beginning capture");
189 
190 	msleep(10); /* delay for capture to finish */
191 
192 	return ret;
193 }
194 
adis16220_reset(struct iio_dev * indio_dev)195 static int adis16220_reset(struct iio_dev *indio_dev)
196 {
197 	int ret;
198 	ret = adis16220_spi_write_reg_8(indio_dev,
199 			ADIS16220_GLOB_CMD,
200 			ADIS16220_GLOB_CMD_SW_RESET);
201 	if (ret)
202 		dev_err(&indio_dev->dev, "problem resetting device");
203 
204 	return ret;
205 }
206 
adis16220_write_reset(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)207 static ssize_t adis16220_write_reset(struct device *dev,
208 		struct device_attribute *attr,
209 		const char *buf, size_t len)
210 {
211 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
212 	bool val;
213 	int ret;
214 
215 	ret = strtobool(buf, &val);
216 	if (ret)
217 		return ret;
218 	if (!val)
219 		return -EINVAL;
220 
221 	ret = adis16220_reset(indio_dev);
222 	if (ret)
223 		return ret;
224 	return len;
225 }
226 
adis16220_write_capture(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)227 static ssize_t adis16220_write_capture(struct device *dev,
228 		struct device_attribute *attr,
229 		const char *buf, size_t len)
230 {
231 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
232 	bool val;
233 	int ret;
234 
235 	ret = strtobool(buf, &val);
236 	if (ret)
237 		return ret;
238 	if (!val)
239 		return -EINVAL;
240 	ret = adis16220_capture(indio_dev);
241 	if (ret)
242 		return ret;
243 
244 	return len;
245 }
246 
adis16220_check_status(struct iio_dev * indio_dev)247 static int adis16220_check_status(struct iio_dev *indio_dev)
248 {
249 	u16 status;
250 	int ret;
251 
252 	ret = adis16220_spi_read_reg_16(indio_dev, ADIS16220_DIAG_STAT,
253 					&status);
254 
255 	if (ret < 0) {
256 		dev_err(&indio_dev->dev, "Reading status failed\n");
257 		goto error_ret;
258 	}
259 	ret = status & 0x7F;
260 
261 	if (status & ADIS16220_DIAG_STAT_VIOLATION)
262 		dev_err(&indio_dev->dev,
263 			"Capture period violation/interruption\n");
264 	if (status & ADIS16220_DIAG_STAT_SPI_FAIL)
265 		dev_err(&indio_dev->dev, "SPI failure\n");
266 	if (status & ADIS16220_DIAG_STAT_FLASH_UPT)
267 		dev_err(&indio_dev->dev, "Flash update failed\n");
268 	if (status & ADIS16220_DIAG_STAT_POWER_HIGH)
269 		dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
270 	if (status & ADIS16220_DIAG_STAT_POWER_LOW)
271 		dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
272 
273 error_ret:
274 	return ret;
275 }
276 
adis16220_self_test(struct iio_dev * indio_dev)277 static int adis16220_self_test(struct iio_dev *indio_dev)
278 {
279 	int ret;
280 	ret = adis16220_spi_write_reg_16(indio_dev,
281 			ADIS16220_MSC_CTRL,
282 			ADIS16220_MSC_CTRL_SELF_TEST_EN);
283 	if (ret) {
284 		dev_err(&indio_dev->dev, "problem starting self test");
285 		goto err_ret;
286 	}
287 
288 	adis16220_check_status(indio_dev);
289 
290 err_ret:
291 	return ret;
292 }
293 
adis16220_initial_setup(struct iio_dev * indio_dev)294 static int adis16220_initial_setup(struct iio_dev *indio_dev)
295 {
296 	int ret;
297 
298 	/* Do self test */
299 	ret = adis16220_self_test(indio_dev);
300 	if (ret) {
301 		dev_err(&indio_dev->dev, "self test failure");
302 		goto err_ret;
303 	}
304 
305 	/* Read status register to check the result */
306 	ret = adis16220_check_status(indio_dev);
307 	if (ret) {
308 		adis16220_reset(indio_dev);
309 		dev_err(&indio_dev->dev, "device not playing ball -> reset");
310 		msleep(ADIS16220_STARTUP_DELAY);
311 		ret = adis16220_check_status(indio_dev);
312 		if (ret) {
313 			dev_err(&indio_dev->dev, "giving up");
314 			goto err_ret;
315 		}
316 	}
317 
318 err_ret:
319 	return ret;
320 }
321 
adis16220_capture_buffer_read(struct iio_dev * indio_dev,char * buf,loff_t off,size_t count,int addr)322 static ssize_t adis16220_capture_buffer_read(struct iio_dev *indio_dev,
323 					char *buf,
324 					loff_t off,
325 					size_t count,
326 					int addr)
327 {
328 	struct adis16220_state *st = iio_priv(indio_dev);
329 	struct spi_message msg;
330 	struct spi_transfer xfers[] = {
331 		{
332 			.tx_buf = st->tx,
333 			.bits_per_word = 8,
334 			.len = 2,
335 			.cs_change = 1,
336 			.delay_usecs = 25,
337 		}, {
338 			.tx_buf = st->tx,
339 			.rx_buf = st->rx,
340 			.bits_per_word = 8,
341 			.cs_change = 1,
342 			.delay_usecs = 25,
343 		},
344 	};
345 	int ret;
346 	int i;
347 
348 	if (unlikely(!count))
349 		return count;
350 
351 	if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
352 		return -EINVAL;
353 
354 	if (off + count > ADIS16220_CAPTURE_SIZE)
355 		count = ADIS16220_CAPTURE_SIZE - off;
356 
357 	/* write the begin position of capture buffer */
358 	ret = adis16220_spi_write_reg_16(indio_dev,
359 					ADIS16220_CAPT_PNTR,
360 					off > 1);
361 	if (ret)
362 		return -EIO;
363 
364 	/* read count/2 values from capture buffer */
365 	mutex_lock(&st->buf_lock);
366 
367 	for (i = 0; i < count; i += 2) {
368 		st->tx[i] = ADIS16220_READ_REG(addr);
369 		st->tx[i + 1] = 0;
370 	}
371 	xfers[1].len = count;
372 
373 	spi_message_init(&msg);
374 	spi_message_add_tail(&xfers[0], &msg);
375 	spi_message_add_tail(&xfers[1], &msg);
376 	ret = spi_sync(st->us, &msg);
377 	if (ret) {
378 
379 		mutex_unlock(&st->buf_lock);
380 		return -EIO;
381 	}
382 
383 	memcpy(buf, st->rx, count);
384 
385 	mutex_unlock(&st->buf_lock);
386 	return count;
387 }
388 
adis16220_accel_bin_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)389 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
390 					struct bin_attribute *attr,
391 					char *buf,
392 					loff_t off,
393 					size_t count)
394 {
395 	struct device *dev = container_of(kobj, struct device, kobj);
396 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
397 
398 	return adis16220_capture_buffer_read(indio_dev, buf,
399 					off, count,
400 					ADIS16220_CAPT_BUFA);
401 }
402 
403 static struct bin_attribute accel_bin = {
404 	.attr = {
405 		.name = "accel_bin",
406 		.mode = S_IRUGO,
407 	},
408 	.read = adis16220_accel_bin_read,
409 	.size = ADIS16220_CAPTURE_SIZE,
410 };
411 
adis16220_adc1_bin_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)412 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
413 				struct bin_attribute *attr,
414 				char *buf, loff_t off,
415 				size_t count)
416 {
417 	struct device *dev = container_of(kobj, struct device, kobj);
418 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
419 
420 	return adis16220_capture_buffer_read(indio_dev, buf,
421 					off, count,
422 					ADIS16220_CAPT_BUF1);
423 }
424 
425 static struct bin_attribute adc1_bin = {
426 	.attr = {
427 		.name = "in0_bin",
428 		.mode = S_IRUGO,
429 	},
430 	.read =  adis16220_adc1_bin_read,
431 	.size = ADIS16220_CAPTURE_SIZE,
432 };
433 
adis16220_adc2_bin_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)434 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
435 				struct bin_attribute *attr,
436 				char *buf, loff_t off,
437 				size_t count)
438 {
439 	struct device *dev = container_of(kobj, struct device, kobj);
440 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
441 
442 	return adis16220_capture_buffer_read(indio_dev, buf,
443 					off, count,
444 					ADIS16220_CAPT_BUF2);
445 }
446 
447 
448 static struct bin_attribute adc2_bin = {
449 	.attr = {
450 		.name = "in1_bin",
451 		.mode = S_IRUGO,
452 	},
453 	.read =  adis16220_adc2_bin_read,
454 	.size = ADIS16220_CAPTURE_SIZE,
455 };
456 
457 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
458 		adis16220_write_reset, 0);
459 
460 #define IIO_DEV_ATTR_CAPTURE(_store)				\
461 	IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
462 
463 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
464 
465 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr)		\
466 	IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
467 
468 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
469 		adis16220_read_16bit,
470 		adis16220_write_16bit,
471 		ADIS16220_CAPT_PNTR);
472 
473 enum adis16220_channel {
474 	in_supply, in_1, in_2, accel, temp
475 };
476 
477 struct adis16220_address_spec {
478 	u8 addr;
479 	u8 bits;
480 	bool sign;
481 };
482 
483 /* Address / bits / signed */
484 static const struct adis16220_address_spec adis16220_addresses[][3] = {
485 	[in_supply] =	{ { ADIS16220_CAPT_SUPPLY,	12, 0 }, },
486 	[in_1] =	{ { ADIS16220_CAPT_BUF1,	16, 1 },
487 			  { ADIS16220_AIN1_NULL,	16, 1 },
488 			  { ADIS16220_CAPT_PEAK1,	16, 1 }, },
489 	[in_2] =	{ { ADIS16220_CAPT_BUF2,	16, 1 },
490 			  { ADIS16220_AIN2_NULL,	16, 1 },
491 			  { ADIS16220_CAPT_PEAK2,	16, 1 }, },
492 	[accel] =	{ { ADIS16220_CAPT_BUFA,	16, 1 },
493 			  { ADIS16220_ACCL_NULL,	16, 1 },
494 			  { ADIS16220_CAPT_PEAKA,	16, 1 }, },
495 	[temp] =	{ { ADIS16220_CAPT_TEMP,	12, 0 }, }
496 };
497 
adis16220_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)498 static int adis16220_read_raw(struct iio_dev *indio_dev,
499 			      struct iio_chan_spec const *chan,
500 			      int *val, int *val2,
501 			      long mask)
502 {
503 	int ret = -EINVAL;
504 	int addrind = 0;
505 	u16 uval;
506 	s16 sval;
507 	u8 bits;
508 
509 	switch (mask) {
510 	case 0:
511 		addrind = 0;
512 		break;
513 	case IIO_CHAN_INFO_OFFSET:
514 		if (chan->type == IIO_TEMP) {
515 			*val = 25;
516 			return IIO_VAL_INT;
517 		}
518 		addrind = 1;
519 		break;
520 	case IIO_CHAN_INFO_PEAK:
521 		addrind = 2;
522 		break;
523 	case IIO_CHAN_INFO_SCALE:
524 		*val = 0;
525 		switch (chan->type) {
526 		case IIO_TEMP:
527 			*val2 = -470000;
528 			return IIO_VAL_INT_PLUS_MICRO;
529 		case IIO_ACCEL:
530 			*val2 = 1887042;
531 			return IIO_VAL_INT_PLUS_MICRO;
532 		case IIO_VOLTAGE:
533 			if (chan->channel == 0)
534 				*val2 = 0012221;
535 			else /* Should really be dependent on VDD */
536 				*val2 = 305;
537 			return IIO_VAL_INT_PLUS_MICRO;
538 		default:
539 			return -EINVAL;
540 		}
541 	default:
542 		return -EINVAL;
543 	}
544 	if (adis16220_addresses[chan->address][addrind].sign) {
545 		ret = adis16220_spi_read_reg_16(indio_dev,
546 						adis16220_addresses[chan
547 								    ->address]
548 						[addrind].addr,
549 						&sval);
550 		if (ret)
551 			return ret;
552 		bits = adis16220_addresses[chan->address][addrind].bits;
553 		sval &= (1 << bits) - 1;
554 		sval = (s16)(sval << (16 - bits)) >> (16 - bits);
555 		*val = sval;
556 		return IIO_VAL_INT;
557 	} else {
558 		ret = adis16220_spi_read_reg_16(indio_dev,
559 						adis16220_addresses[chan
560 								    ->address]
561 						[addrind].addr,
562 						&uval);
563 		if (ret)
564 			return ret;
565 		bits = adis16220_addresses[chan->address][addrind].bits;
566 		uval &= (1 << bits) - 1;
567 		*val = uval;
568 		return IIO_VAL_INT;
569 	}
570 }
571 
572 static const struct iio_chan_spec adis16220_channels[] = {
573 	{
574 		.type = IIO_VOLTAGE,
575 		.indexed = 1,
576 		.channel = 0,
577 		.extend_name = "supply",
578 		.info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
579 		.address = in_supply,
580 	}, {
581 		.type = IIO_ACCEL,
582 		.info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
583 			     IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
584 			     IIO_CHAN_INFO_PEAK_SEPARATE_BIT,
585 		.address = accel,
586 	}, {
587 		.type = IIO_TEMP,
588 		.indexed = 1,
589 		.channel = 0,
590 		.info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
591 			     IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
592 		.address = temp,
593 	}, {
594 		.type = IIO_VOLTAGE,
595 		.indexed = 1,
596 		.channel = 1,
597 		.info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |
598 			     IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
599 		.address = in_1,
600 	}, {
601 		.type = IIO_VOLTAGE,
602 		.indexed = 1,
603 		.channel = 2,
604 		.address = in_2,
605 	}
606 };
607 
608 static struct attribute *adis16220_attributes[] = {
609 	&iio_dev_attr_reset.dev_attr.attr,
610 	&iio_dev_attr_capture.dev_attr.attr,
611 	&iio_dev_attr_capture_count.dev_attr.attr,
612 	NULL
613 };
614 
615 static const struct attribute_group adis16220_attribute_group = {
616 	.attrs = adis16220_attributes,
617 };
618 
619 static const struct iio_info adis16220_info = {
620 	.attrs = &adis16220_attribute_group,
621 	.driver_module = THIS_MODULE,
622 	.read_raw = &adis16220_read_raw,
623 };
624 
adis16220_probe(struct spi_device * spi)625 static int __devinit adis16220_probe(struct spi_device *spi)
626 {
627 	int ret;
628 	struct adis16220_state *st;
629 	struct iio_dev *indio_dev;
630 
631 	/* setup the industrialio driver allocated elements */
632 	indio_dev = iio_allocate_device(sizeof(*st));
633 	if (indio_dev == NULL) {
634 		ret = -ENOMEM;
635 		goto error_ret;
636 	}
637 
638 	st = iio_priv(indio_dev);
639 	/* this is only used for removal purposes */
640 	spi_set_drvdata(spi, indio_dev);
641 
642 	st->us = spi;
643 	mutex_init(&st->buf_lock);
644 
645 	indio_dev->name = spi->dev.driver->name;
646 	indio_dev->dev.parent = &spi->dev;
647 	indio_dev->info = &adis16220_info;
648 	indio_dev->modes = INDIO_DIRECT_MODE;
649 	indio_dev->channels = adis16220_channels;
650 	indio_dev->num_channels = ARRAY_SIZE(adis16220_channels);
651 
652 	ret = iio_device_register(indio_dev);
653 	if (ret)
654 		goto error_free_dev;
655 
656 	ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &accel_bin);
657 	if (ret)
658 		goto error_unregister_dev;
659 
660 	ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc1_bin);
661 	if (ret)
662 		goto error_rm_accel_bin;
663 
664 	ret = sysfs_create_bin_file(&indio_dev->dev.kobj, &adc2_bin);
665 	if (ret)
666 		goto error_rm_adc1_bin;
667 
668 	/* Get the device into a sane initial state */
669 	ret = adis16220_initial_setup(indio_dev);
670 	if (ret)
671 		goto error_rm_adc2_bin;
672 	return 0;
673 
674 error_rm_adc2_bin:
675 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
676 error_rm_adc1_bin:
677 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
678 error_rm_accel_bin:
679 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
680 error_unregister_dev:
681 	iio_device_unregister(indio_dev);
682 error_free_dev:
683 	iio_free_device(indio_dev);
684 error_ret:
685 	return ret;
686 }
687 
adis16220_remove(struct spi_device * spi)688 static int adis16220_remove(struct spi_device *spi)
689 {
690 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
691 
692 	flush_scheduled_work();
693 
694 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc2_bin);
695 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &adc1_bin);
696 	sysfs_remove_bin_file(&indio_dev->dev.kobj, &accel_bin);
697 	iio_device_unregister(indio_dev);
698 	iio_free_device(indio_dev);
699 
700 	return 0;
701 }
702 
703 static struct spi_driver adis16220_driver = {
704 	.driver = {
705 		.name = "adis16220",
706 		.owner = THIS_MODULE,
707 	},
708 	.probe = adis16220_probe,
709 	.remove = __devexit_p(adis16220_remove),
710 };
711 module_spi_driver(adis16220_driver);
712 
713 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
714 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
715 MODULE_LICENSE("GPL v2");
716 MODULE_ALIAS("spi:adis16220");
717