• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/iio/imu/
1/*
2 * ADIS16300 Four Degrees of Freedom Inertial Sensor 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/mutex.h>
14#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/spi/spi.h>
17#include <linux/slab.h>
18#include <linux/sysfs.h>
19#include <linux/list.h>
20
21#include "../iio.h"
22#include "../sysfs.h"
23#include "../ring_generic.h"
24#include "../accel/accel.h"
25#include "../accel/inclinometer.h"
26#include "../gyro/gyro.h"
27#include "../adc/adc.h"
28
29#include "adis16300.h"
30
31#define DRIVER_NAME		"adis16300"
32
33static int adis16300_check_status(struct device *dev);
34
35/**
36 * adis16300_spi_write_reg_8() - write single byte to a register
37 * @dev: device associated with child of actual device (iio_dev or iio_trig)
38 * @reg_address: the address of the register to be written
39 * @val: the value to write
40 **/
41static int adis16300_spi_write_reg_8(struct device *dev,
42		u8 reg_address,
43		u8 val)
44{
45	int ret;
46	struct iio_dev *indio_dev = dev_get_drvdata(dev);
47	struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
48
49	mutex_lock(&st->buf_lock);
50	st->tx[0] = ADIS16300_WRITE_REG(reg_address);
51	st->tx[1] = val;
52
53	ret = spi_write(st->us, st->tx, 2);
54	mutex_unlock(&st->buf_lock);
55
56	return ret;
57}
58
59/**
60 * adis16300_spi_write_reg_16() - write 2 bytes to a pair of registers
61 * @dev: device associated with child of actual device (iio_dev or iio_trig)
62 * @reg_address: the address of the lower of the two registers. Second register
63 *               is assumed to have address one greater.
64 * @val: value to be written
65 **/
66static int adis16300_spi_write_reg_16(struct device *dev,
67		u8 lower_reg_address,
68		u16 value)
69{
70	int ret;
71	struct spi_message msg;
72	struct iio_dev *indio_dev = dev_get_drvdata(dev);
73	struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
74	struct spi_transfer xfers[] = {
75		{
76			.tx_buf = st->tx,
77			.bits_per_word = 8,
78			.len = 2,
79			.cs_change = 1,
80			.delay_usecs = 75,
81		}, {
82			.tx_buf = st->tx + 2,
83			.bits_per_word = 8,
84			.len = 2,
85			.cs_change = 1,
86			.delay_usecs = 75,
87		},
88	};
89
90	mutex_lock(&st->buf_lock);
91	st->tx[0] = ADIS16300_WRITE_REG(lower_reg_address);
92	st->tx[1] = value & 0xFF;
93	st->tx[2] = ADIS16300_WRITE_REG(lower_reg_address + 1);
94	st->tx[3] = (value >> 8) & 0xFF;
95
96	spi_message_init(&msg);
97	spi_message_add_tail(&xfers[0], &msg);
98	spi_message_add_tail(&xfers[1], &msg);
99	ret = spi_sync(st->us, &msg);
100	mutex_unlock(&st->buf_lock);
101
102	return ret;
103}
104
105/**
106 * adis16300_spi_read_reg_16() - read 2 bytes from a 16-bit register
107 * @dev: device associated with child of actual device (iio_dev or iio_trig)
108 * @reg_address: the address of the lower of the two registers. Second register
109 *               is assumed to have address one greater.
110 * @val: somewhere to pass back the value read
111 **/
112static int adis16300_spi_read_reg_16(struct device *dev,
113		u8 lower_reg_address,
114		u16 *val)
115{
116	struct spi_message msg;
117	struct iio_dev *indio_dev = dev_get_drvdata(dev);
118	struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
119	int ret;
120	struct spi_transfer xfers[] = {
121		{
122			.tx_buf = st->tx,
123			.bits_per_word = 8,
124			.len = 2,
125			.cs_change = 1,
126			.delay_usecs = 75,
127		}, {
128			.rx_buf = st->rx,
129			.bits_per_word = 8,
130			.len = 2,
131			.cs_change = 1,
132			.delay_usecs = 75,
133		},
134	};
135
136	mutex_lock(&st->buf_lock);
137	st->tx[0] = ADIS16300_READ_REG(lower_reg_address);
138	st->tx[1] = 0;
139	st->tx[2] = 0;
140	st->tx[3] = 0;
141
142	spi_message_init(&msg);
143	spi_message_add_tail(&xfers[0], &msg);
144	spi_message_add_tail(&xfers[1], &msg);
145	ret = spi_sync(st->us, &msg);
146	if (ret) {
147		dev_err(&st->us->dev,
148			"problem when reading 16 bit register 0x%02X",
149			lower_reg_address);
150		goto error_ret;
151	}
152	*val = (st->rx[0] << 8) | st->rx[1];
153
154error_ret:
155	mutex_unlock(&st->buf_lock);
156	return ret;
157}
158
159static ssize_t adis16300_spi_read_signed(struct device *dev,
160		struct device_attribute *attr,
161		char *buf,
162		unsigned bits)
163{
164	int ret;
165	s16 val = 0;
166	unsigned shift = 16 - bits;
167	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
168
169	ret = adis16300_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
170	if (ret)
171		return ret;
172
173	if (val & ADIS16300_ERROR_ACTIVE)
174		adis16300_check_status(dev);
175	val = ((s16)(val << shift) >> shift);
176	return sprintf(buf, "%d\n", val);
177}
178
179static ssize_t adis16300_read_12bit_unsigned(struct device *dev,
180		struct device_attribute *attr,
181		char *buf)
182{
183	int ret;
184	u16 val = 0;
185	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
186
187	ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
188	if (ret)
189		return ret;
190
191	if (val & ADIS16300_ERROR_ACTIVE)
192		adis16300_check_status(dev);
193
194	return sprintf(buf, "%u\n", val & 0x0FFF);
195}
196
197static ssize_t adis16300_read_14bit_unsigned(struct device *dev,
198		struct device_attribute *attr,
199		char *buf)
200{
201	int ret;
202	u16 val = 0;
203	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
204
205	ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
206	if (ret)
207		return ret;
208
209	if (val & ADIS16300_ERROR_ACTIVE)
210		adis16300_check_status(dev);
211
212	return sprintf(buf, "%u\n", val & 0x3FFF);
213}
214
215static ssize_t adis16300_read_14bit_signed(struct device *dev,
216		struct device_attribute *attr,
217		char *buf)
218{
219	struct iio_dev *indio_dev = dev_get_drvdata(dev);
220	ssize_t ret;
221
222	/* Take the iio_dev status lock */
223	mutex_lock(&indio_dev->mlock);
224	ret =  adis16300_spi_read_signed(dev, attr, buf, 14);
225	mutex_unlock(&indio_dev->mlock);
226
227	return ret;
228}
229
230static ssize_t adis16300_read_12bit_signed(struct device *dev,
231		struct device_attribute *attr,
232		char *buf)
233{
234	struct iio_dev *indio_dev = dev_get_drvdata(dev);
235	ssize_t ret;
236
237	/* Take the iio_dev status lock */
238	mutex_lock(&indio_dev->mlock);
239	ret =  adis16300_spi_read_signed(dev, attr, buf, 12);
240	mutex_unlock(&indio_dev->mlock);
241
242	return ret;
243}
244
245static ssize_t adis16300_read_13bit_signed(struct device *dev,
246		struct device_attribute *attr,
247		char *buf)
248{
249	struct iio_dev *indio_dev = dev_get_drvdata(dev);
250	ssize_t ret;
251
252	/* Take the iio_dev status lock */
253	mutex_lock(&indio_dev->mlock);
254	ret =  adis16300_spi_read_signed(dev, attr, buf, 13);
255	mutex_unlock(&indio_dev->mlock);
256
257	return ret;
258}
259
260static ssize_t adis16300_write_16bit(struct device *dev,
261		struct device_attribute *attr,
262		const char *buf,
263		size_t len)
264{
265	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
266	int ret;
267	long val;
268
269	ret = strict_strtol(buf, 10, &val);
270	if (ret)
271		goto error_ret;
272	ret = adis16300_spi_write_reg_16(dev, this_attr->address, val);
273
274error_ret:
275	return ret ? ret : len;
276}
277
278static ssize_t adis16300_read_frequency(struct device *dev,
279		struct device_attribute *attr,
280		char *buf)
281{
282	int ret, len = 0;
283	u16 t;
284	int sps;
285	ret = adis16300_spi_read_reg_16(dev,
286			ADIS16300_SMPL_PRD,
287			&t);
288	if (ret)
289		return ret;
290	sps =  (t & ADIS16300_SMPL_PRD_TIME_BASE) ? 53 : 1638;
291	sps /= (t & ADIS16300_SMPL_PRD_DIV_MASK) + 1;
292	len = sprintf(buf, "%d SPS\n", sps);
293	return len;
294}
295
296static ssize_t adis16300_write_frequency(struct device *dev,
297		struct device_attribute *attr,
298		const char *buf,
299		size_t len)
300{
301	struct iio_dev *indio_dev = dev_get_drvdata(dev);
302	struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
303	long val;
304	int ret;
305	u8 t;
306
307	ret = strict_strtol(buf, 10, &val);
308	if (ret)
309		return ret;
310
311	mutex_lock(&indio_dev->mlock);
312
313	t = (1638 / val);
314	if (t > 0)
315		t--;
316	t &= ADIS16300_SMPL_PRD_DIV_MASK;
317	if ((t & ADIS16300_SMPL_PRD_DIV_MASK) >= 0x0A)
318		st->us->max_speed_hz = ADIS16300_SPI_SLOW;
319	else
320		st->us->max_speed_hz = ADIS16300_SPI_FAST;
321
322	ret = adis16300_spi_write_reg_8(dev,
323			ADIS16300_SMPL_PRD,
324			t);
325
326	mutex_unlock(&indio_dev->mlock);
327
328	return ret ? ret : len;
329}
330
331static int adis16300_reset(struct device *dev)
332{
333	int ret;
334	ret = adis16300_spi_write_reg_8(dev,
335			ADIS16300_GLOB_CMD,
336			ADIS16300_GLOB_CMD_SW_RESET);
337	if (ret)
338		dev_err(dev, "problem resetting device");
339
340	return ret;
341}
342
343static ssize_t adis16300_write_reset(struct device *dev,
344		struct device_attribute *attr,
345		const char *buf, size_t len)
346{
347	if (len < 1)
348		return -1;
349	switch (buf[0]) {
350	case '1':
351	case 'y':
352	case 'Y':
353		return adis16300_reset(dev);
354	}
355	return -1;
356}
357
358int adis16300_set_irq(struct device *dev, bool enable)
359{
360	int ret;
361	u16 msc;
362	ret = adis16300_spi_read_reg_16(dev, ADIS16300_MSC_CTRL, &msc);
363	if (ret)
364		goto error_ret;
365
366	msc |= ADIS16300_MSC_CTRL_DATA_RDY_POL_HIGH;
367	msc &= ~ADIS16300_MSC_CTRL_DATA_RDY_DIO2;
368	if (enable)
369		msc |= ADIS16300_MSC_CTRL_DATA_RDY_EN;
370	else
371		msc &= ~ADIS16300_MSC_CTRL_DATA_RDY_EN;
372
373	ret = adis16300_spi_write_reg_16(dev, ADIS16300_MSC_CTRL, msc);
374	if (ret)
375		goto error_ret;
376
377error_ret:
378	return ret;
379}
380
381/* Power down the device */
382static int adis16300_stop_device(struct device *dev)
383{
384	int ret;
385	u16 val = ADIS16300_SLP_CNT_POWER_OFF;
386
387	ret = adis16300_spi_write_reg_16(dev, ADIS16300_SLP_CNT, val);
388	if (ret)
389		dev_err(dev, "problem with turning device off: SLP_CNT");
390
391	return ret;
392}
393
394static int adis16300_self_test(struct device *dev)
395{
396	int ret;
397	ret = adis16300_spi_write_reg_16(dev,
398			ADIS16300_MSC_CTRL,
399			ADIS16300_MSC_CTRL_MEM_TEST);
400	if (ret) {
401		dev_err(dev, "problem starting self test");
402		goto err_ret;
403	}
404
405	adis16300_check_status(dev);
406
407err_ret:
408	return ret;
409}
410
411static int adis16300_check_status(struct device *dev)
412{
413	u16 status;
414	int ret;
415
416	ret = adis16300_spi_read_reg_16(dev, ADIS16300_DIAG_STAT, &status);
417
418	if (ret < 0) {
419		dev_err(dev, "Reading status failed\n");
420		goto error_ret;
421	}
422	ret = status;
423	if (status & ADIS16300_DIAG_STAT_ZACCL_FAIL)
424		dev_err(dev, "Z-axis accelerometer self-test failure\n");
425	if (status & ADIS16300_DIAG_STAT_YACCL_FAIL)
426		dev_err(dev, "Y-axis accelerometer self-test failure\n");
427	if (status & ADIS16300_DIAG_STAT_XACCL_FAIL)
428		dev_err(dev, "X-axis accelerometer self-test failure\n");
429	if (status & ADIS16300_DIAG_STAT_XGYRO_FAIL)
430		dev_err(dev, "X-axis gyroscope self-test failure\n");
431	if (status & ADIS16300_DIAG_STAT_ALARM2)
432		dev_err(dev, "Alarm 2 active\n");
433	if (status & ADIS16300_DIAG_STAT_ALARM1)
434		dev_err(dev, "Alarm 1 active\n");
435	if (status & ADIS16300_DIAG_STAT_FLASH_CHK)
436		dev_err(dev, "Flash checksum error\n");
437	if (status & ADIS16300_DIAG_STAT_SELF_TEST)
438		dev_err(dev, "Self test error\n");
439	if (status & ADIS16300_DIAG_STAT_OVERFLOW)
440		dev_err(dev, "Sensor overrange\n");
441	if (status & ADIS16300_DIAG_STAT_SPI_FAIL)
442		dev_err(dev, "SPI failure\n");
443	if (status & ADIS16300_DIAG_STAT_FLASH_UPT)
444		dev_err(dev, "Flash update failed\n");
445	if (status & ADIS16300_DIAG_STAT_POWER_HIGH)
446		dev_err(dev, "Power supply above 5.25V\n");
447	if (status & ADIS16300_DIAG_STAT_POWER_LOW)
448		dev_err(dev, "Power supply below 4.75V\n");
449
450error_ret:
451	return ret;
452}
453
454static int adis16300_initial_setup(struct adis16300_state *st)
455{
456	int ret;
457	u16 smp_prd;
458	struct device *dev = &st->indio_dev->dev;
459
460	/* use low spi speed for init */
461	st->us->max_speed_hz = ADIS16300_SPI_SLOW;
462	st->us->mode = SPI_MODE_3;
463	spi_setup(st->us);
464
465	/* Disable IRQ */
466	ret = adis16300_set_irq(dev, false);
467	if (ret) {
468		dev_err(dev, "disable irq failed");
469		goto err_ret;
470	}
471
472	/* Do self test */
473	ret = adis16300_self_test(dev);
474	if (ret) {
475		dev_err(dev, "self test failure");
476		goto err_ret;
477	}
478
479	/* Read status register to check the result */
480	ret = adis16300_check_status(dev);
481	if (ret) {
482		adis16300_reset(dev);
483		dev_err(dev, "device not playing ball -> reset");
484		msleep(ADIS16300_STARTUP_DELAY);
485		ret = adis16300_check_status(dev);
486		if (ret) {
487			dev_err(dev, "giving up");
488			goto err_ret;
489		}
490	}
491
492	printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
493			st->us->chip_select, st->us->irq);
494
495	/* use high spi speed if possible */
496	ret = adis16300_spi_read_reg_16(dev, ADIS16300_SMPL_PRD, &smp_prd);
497	if (!ret && (smp_prd & ADIS16300_SMPL_PRD_DIV_MASK) < 0x0A) {
498		st->us->max_speed_hz = ADIS16300_SPI_SLOW;
499		spi_setup(st->us);
500	}
501
502err_ret:
503	return ret;
504}
505
506static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
507		adis16300_read_12bit_signed,
508		adis16300_write_16bit,
509		ADIS16300_XACCL_OFF);
510
511static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
512		adis16300_read_12bit_signed,
513		adis16300_write_16bit,
514		ADIS16300_YACCL_OFF);
515
516static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
517		adis16300_read_12bit_signed,
518		adis16300_write_16bit,
519		ADIS16300_ZACCL_OFF);
520
521static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16300_read_14bit_unsigned,
522			   ADIS16300_SUPPLY_OUT);
523static IIO_CONST_ATTR(in_supply_scale, "0.00242");
524
525static IIO_DEV_ATTR_GYRO_X(adis16300_read_14bit_signed,
526		ADIS16300_XGYRO_OUT);
527static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s");
528
529static IIO_DEV_ATTR_ACCEL_X(adis16300_read_14bit_signed,
530		ADIS16300_XACCL_OUT);
531static IIO_DEV_ATTR_ACCEL_Y(adis16300_read_14bit_signed,
532		ADIS16300_YACCL_OUT);
533static IIO_DEV_ATTR_ACCEL_Z(adis16300_read_14bit_signed,
534		ADIS16300_ZACCL_OUT);
535static IIO_CONST_ATTR(accel_scale, "0.0006 g");
536
537static IIO_DEV_ATTR_INCLI_X(adis16300_read_13bit_signed,
538		ADIS16300_XINCLI_OUT);
539static IIO_DEV_ATTR_INCLI_Y(adis16300_read_13bit_signed,
540		ADIS16300_YINCLI_OUT);
541static IIO_CONST_ATTR(incli_scale, "0.044 d");
542
543static IIO_DEV_ATTR_TEMP_RAW(adis16300_read_12bit_unsigned);
544static IIO_CONST_ATTR(temp_offset, "198.16 K");
545static IIO_CONST_ATTR(temp_scale, "0.14 K");
546
547static IIO_DEV_ATTR_IN_RAW(0, adis16300_read_12bit_unsigned,
548		ADIS16300_AUX_ADC);
549static IIO_CONST_ATTR(in0_scale, "0.000806");
550
551static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
552		adis16300_read_frequency,
553		adis16300_write_frequency);
554
555static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16300_write_reset, 0);
556
557static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
558
559static IIO_CONST_ATTR(name, "adis16300");
560
561static struct attribute *adis16300_event_attributes[] = {
562	NULL
563};
564
565static struct attribute_group adis16300_event_attribute_group = {
566	.attrs = adis16300_event_attributes,
567};
568
569static struct attribute *adis16300_attributes[] = {
570	&iio_dev_attr_accel_x_offset.dev_attr.attr,
571	&iio_dev_attr_accel_y_offset.dev_attr.attr,
572	&iio_dev_attr_accel_z_offset.dev_attr.attr,
573	&iio_dev_attr_in_supply_raw.dev_attr.attr,
574	&iio_const_attr_in_supply_scale.dev_attr.attr,
575	&iio_dev_attr_gyro_x_raw.dev_attr.attr,
576	&iio_const_attr_gyro_scale.dev_attr.attr,
577	&iio_dev_attr_accel_x_raw.dev_attr.attr,
578	&iio_dev_attr_accel_y_raw.dev_attr.attr,
579	&iio_dev_attr_accel_z_raw.dev_attr.attr,
580	&iio_const_attr_accel_scale.dev_attr.attr,
581	&iio_dev_attr_incli_x_raw.dev_attr.attr,
582	&iio_dev_attr_incli_y_raw.dev_attr.attr,
583	&iio_const_attr_incli_scale.dev_attr.attr,
584	&iio_dev_attr_temp_raw.dev_attr.attr,
585	&iio_const_attr_temp_offset.dev_attr.attr,
586	&iio_const_attr_temp_scale.dev_attr.attr,
587	&iio_dev_attr_in0_raw.dev_attr.attr,
588	&iio_const_attr_in0_scale.dev_attr.attr,
589	&iio_dev_attr_sampling_frequency.dev_attr.attr,
590	&iio_const_attr_available_sampling_frequency.dev_attr.attr,
591	&iio_dev_attr_reset.dev_attr.attr,
592	&iio_const_attr_name.dev_attr.attr,
593	NULL
594};
595
596static const struct attribute_group adis16300_attribute_group = {
597	.attrs = adis16300_attributes,
598};
599
600static int __devinit adis16300_probe(struct spi_device *spi)
601{
602	int ret, regdone = 0;
603	struct adis16300_state *st = kzalloc(sizeof *st, GFP_KERNEL);
604	if (!st) {
605		ret =  -ENOMEM;
606		goto error_ret;
607	}
608	/* this is only used for removal purposes */
609	spi_set_drvdata(spi, st);
610
611	/* Allocate the comms buffers */
612	st->rx = kzalloc(sizeof(*st->rx)*ADIS16300_MAX_RX, GFP_KERNEL);
613	if (st->rx == NULL) {
614		ret = -ENOMEM;
615		goto error_free_st;
616	}
617	st->tx = kzalloc(sizeof(*st->tx)*ADIS16300_MAX_TX, GFP_KERNEL);
618	if (st->tx == NULL) {
619		ret = -ENOMEM;
620		goto error_free_rx;
621	}
622	st->us = spi;
623	mutex_init(&st->buf_lock);
624	/* setup the industrialio driver allocated elements */
625	st->indio_dev = iio_allocate_device();
626	if (st->indio_dev == NULL) {
627		ret = -ENOMEM;
628		goto error_free_tx;
629	}
630
631	st->indio_dev->dev.parent = &spi->dev;
632	st->indio_dev->num_interrupt_lines = 1;
633	st->indio_dev->event_attrs = &adis16300_event_attribute_group;
634	st->indio_dev->attrs = &adis16300_attribute_group;
635	st->indio_dev->dev_data = (void *)(st);
636	st->indio_dev->driver_module = THIS_MODULE;
637	st->indio_dev->modes = INDIO_DIRECT_MODE;
638
639	ret = adis16300_configure_ring(st->indio_dev);
640	if (ret)
641		goto error_free_dev;
642
643	ret = iio_device_register(st->indio_dev);
644	if (ret)
645		goto error_unreg_ring_funcs;
646	regdone = 1;
647
648	ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
649	if (ret) {
650		printk(KERN_ERR "failed to initialize the ring\n");
651		goto error_unreg_ring_funcs;
652	}
653
654	if (spi->irq) {
655		ret = iio_register_interrupt_line(spi->irq,
656				st->indio_dev,
657				0,
658				IRQF_TRIGGER_RISING,
659				"adis16300");
660		if (ret)
661			goto error_uninitialize_ring;
662
663		ret = adis16300_probe_trigger(st->indio_dev);
664		if (ret)
665			goto error_unregister_line;
666	}
667
668	/* Get the device into a sane initial state */
669	ret = adis16300_initial_setup(st);
670	if (ret)
671		goto error_remove_trigger;
672	return 0;
673
674error_remove_trigger:
675	adis16300_remove_trigger(st->indio_dev);
676error_unregister_line:
677	if (spi->irq)
678		iio_unregister_interrupt_line(st->indio_dev, 0);
679error_uninitialize_ring:
680	iio_ring_buffer_unregister(st->indio_dev->ring);
681error_unreg_ring_funcs:
682	adis16300_unconfigure_ring(st->indio_dev);
683error_free_dev:
684	if (regdone)
685		iio_device_unregister(st->indio_dev);
686	else
687		iio_free_device(st->indio_dev);
688error_free_tx:
689	kfree(st->tx);
690error_free_rx:
691	kfree(st->rx);
692error_free_st:
693	kfree(st);
694error_ret:
695	return ret;
696}
697
698static int adis16300_remove(struct spi_device *spi)
699{
700	int ret;
701	struct adis16300_state *st = spi_get_drvdata(spi);
702	struct iio_dev *indio_dev = st->indio_dev;
703
704	ret = adis16300_stop_device(&(indio_dev->dev));
705	if (ret)
706		goto err_ret;
707
708	flush_scheduled_work();
709
710	adis16300_remove_trigger(indio_dev);
711	if (spi->irq)
712		iio_unregister_interrupt_line(indio_dev, 0);
713
714	iio_ring_buffer_unregister(indio_dev->ring);
715	iio_device_unregister(indio_dev);
716	adis16300_unconfigure_ring(indio_dev);
717	kfree(st->tx);
718	kfree(st->rx);
719	kfree(st);
720
721	return 0;
722
723err_ret:
724	return ret;
725}
726
727static struct spi_driver adis16300_driver = {
728	.driver = {
729		.name = "adis16300",
730		.owner = THIS_MODULE,
731	},
732	.probe = adis16300_probe,
733	.remove = __devexit_p(adis16300_remove),
734};
735
736static __init int adis16300_init(void)
737{
738	return spi_register_driver(&adis16300_driver);
739}
740module_init(adis16300_init);
741
742static __exit void adis16300_exit(void)
743{
744	spi_unregister_driver(&adis16300_driver);
745}
746module_exit(adis16300_exit);
747
748MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
749MODULE_DESCRIPTION("Analog Devices ADIS16300 IMU SPI driver");
750MODULE_LICENSE("GPL v2");
751