GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / iio / imu / adis16300_core.c
blobf1950d56cb1f24a425f4305af831f26497d0f560
1 /*
2 * ADIS16300 Four Degrees of Freedom Inertial Sensor Driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
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"
29 #include "adis16300.h"
31 #define DRIVER_NAME "adis16300"
33 static int adis16300_check_status(struct device *dev);
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 **/
41 static int adis16300_spi_write_reg_8(struct device *dev,
42 u8 reg_address,
43 u8 val)
45 int ret;
46 struct iio_dev *indio_dev = dev_get_drvdata(dev);
47 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
49 mutex_lock(&st->buf_lock);
50 st->tx[0] = ADIS16300_WRITE_REG(reg_address);
51 st->tx[1] = val;
53 ret = spi_write(st->us, st->tx, 2);
54 mutex_unlock(&st->buf_lock);
56 return ret;
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 **/
66 static int adis16300_spi_write_reg_16(struct device *dev,
67 u8 lower_reg_address,
68 u16 value)
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[] = {
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,
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;
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);
102 return ret;
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
112 static int adis16300_spi_read_reg_16(struct device *dev,
113 u8 lower_reg_address,
114 u16 *val)
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[] = {
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,
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;
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;
152 *val = (st->rx[0] << 8) | st->rx[1];
154 error_ret:
155 mutex_unlock(&st->buf_lock);
156 return ret;
159 static ssize_t adis16300_spi_read_signed(struct device *dev,
160 struct device_attribute *attr,
161 char *buf,
162 unsigned bits)
164 int ret;
165 s16 val = 0;
166 unsigned shift = 16 - bits;
167 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
169 ret = adis16300_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
170 if (ret)
171 return ret;
173 if (val & ADIS16300_ERROR_ACTIVE)
174 adis16300_check_status(dev);
175 val = ((s16)(val << shift) >> shift);
176 return sprintf(buf, "%d\n", val);
179 static ssize_t adis16300_read_12bit_unsigned(struct device *dev,
180 struct device_attribute *attr,
181 char *buf)
183 int ret;
184 u16 val = 0;
185 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
187 ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
188 if (ret)
189 return ret;
191 if (val & ADIS16300_ERROR_ACTIVE)
192 adis16300_check_status(dev);
194 return sprintf(buf, "%u\n", val & 0x0FFF);
197 static ssize_t adis16300_read_14bit_unsigned(struct device *dev,
198 struct device_attribute *attr,
199 char *buf)
201 int ret;
202 u16 val = 0;
203 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
205 ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
206 if (ret)
207 return ret;
209 if (val & ADIS16300_ERROR_ACTIVE)
210 adis16300_check_status(dev);
212 return sprintf(buf, "%u\n", val & 0x3FFF);
215 static ssize_t adis16300_read_14bit_signed(struct device *dev,
216 struct device_attribute *attr,
217 char *buf)
219 struct iio_dev *indio_dev = dev_get_drvdata(dev);
220 ssize_t ret;
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);
227 return ret;
230 static ssize_t adis16300_read_12bit_signed(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
234 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 ssize_t ret;
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);
242 return ret;
245 static ssize_t adis16300_read_13bit_signed(struct device *dev,
246 struct device_attribute *attr,
247 char *buf)
249 struct iio_dev *indio_dev = dev_get_drvdata(dev);
250 ssize_t ret;
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);
257 return ret;
260 static ssize_t adis16300_write_16bit(struct device *dev,
261 struct device_attribute *attr,
262 const char *buf,
263 size_t len)
265 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
266 int ret;
267 long val;
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);
274 error_ret:
275 return ret ? ret : len;
278 static ssize_t adis16300_read_frequency(struct device *dev,
279 struct device_attribute *attr,
280 char *buf)
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;
296 static ssize_t adis16300_write_frequency(struct device *dev,
297 struct device_attribute *attr,
298 const char *buf,
299 size_t len)
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;
307 ret = strict_strtol(buf, 10, &val);
308 if (ret)
309 return ret;
311 mutex_lock(&indio_dev->mlock);
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;
322 ret = adis16300_spi_write_reg_8(dev,
323 ADIS16300_SMPL_PRD,
326 mutex_unlock(&indio_dev->mlock);
328 return ret ? ret : len;
331 static int adis16300_reset(struct device *dev)
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");
340 return ret;
343 static ssize_t adis16300_write_reset(struct device *dev,
344 struct device_attribute *attr,
345 const char *buf, size_t len)
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);
355 return -1;
358 int adis16300_set_irq(struct device *dev, bool enable)
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;
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;
373 ret = adis16300_spi_write_reg_16(dev, ADIS16300_MSC_CTRL, msc);
374 if (ret)
375 goto error_ret;
377 error_ret:
378 return ret;
381 /* Power down the device */
382 static int adis16300_stop_device(struct device *dev)
384 int ret;
385 u16 val = ADIS16300_SLP_CNT_POWER_OFF;
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");
391 return ret;
394 static int adis16300_self_test(struct device *dev)
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;
405 adis16300_check_status(dev);
407 err_ret:
408 return ret;
411 static int adis16300_check_status(struct device *dev)
413 u16 status;
414 int ret;
416 ret = adis16300_spi_read_reg_16(dev, ADIS16300_DIAG_STAT, &status);
418 if (ret < 0) {
419 dev_err(dev, "Reading status failed\n");
420 goto error_ret;
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");
450 error_ret:
451 return ret;
454 static int adis16300_initial_setup(struct adis16300_state *st)
456 int ret;
457 u16 smp_prd;
458 struct device *dev = &st->indio_dev->dev;
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);
465 /* Disable IRQ */
466 ret = adis16300_set_irq(dev, false);
467 if (ret) {
468 dev_err(dev, "disable irq failed");
469 goto err_ret;
472 /* Do self test */
473 ret = adis16300_self_test(dev);
474 if (ret) {
475 dev_err(dev, "self test failure");
476 goto err_ret;
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;
492 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
493 st->us->chip_select, st->us->irq);
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);
502 err_ret:
503 return ret;
506 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
507 adis16300_read_12bit_signed,
508 adis16300_write_16bit,
509 ADIS16300_XACCL_OFF);
511 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
512 adis16300_read_12bit_signed,
513 adis16300_write_16bit,
514 ADIS16300_YACCL_OFF);
516 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
517 adis16300_read_12bit_signed,
518 adis16300_write_16bit,
519 ADIS16300_ZACCL_OFF);
521 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16300_read_14bit_unsigned,
522 ADIS16300_SUPPLY_OUT);
523 static IIO_CONST_ATTR(in_supply_scale, "0.00242");
525 static IIO_DEV_ATTR_GYRO_X(adis16300_read_14bit_signed,
526 ADIS16300_XGYRO_OUT);
527 static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s");
529 static IIO_DEV_ATTR_ACCEL_X(adis16300_read_14bit_signed,
530 ADIS16300_XACCL_OUT);
531 static IIO_DEV_ATTR_ACCEL_Y(adis16300_read_14bit_signed,
532 ADIS16300_YACCL_OUT);
533 static IIO_DEV_ATTR_ACCEL_Z(adis16300_read_14bit_signed,
534 ADIS16300_ZACCL_OUT);
535 static IIO_CONST_ATTR(accel_scale, "0.0006 g");
537 static IIO_DEV_ATTR_INCLI_X(adis16300_read_13bit_signed,
538 ADIS16300_XINCLI_OUT);
539 static IIO_DEV_ATTR_INCLI_Y(adis16300_read_13bit_signed,
540 ADIS16300_YINCLI_OUT);
541 static IIO_CONST_ATTR(incli_scale, "0.044 d");
543 static IIO_DEV_ATTR_TEMP_RAW(adis16300_read_12bit_unsigned);
544 static IIO_CONST_ATTR(temp_offset, "198.16 K");
545 static IIO_CONST_ATTR(temp_scale, "0.14 K");
547 static IIO_DEV_ATTR_IN_RAW(0, adis16300_read_12bit_unsigned,
548 ADIS16300_AUX_ADC);
549 static IIO_CONST_ATTR(in0_scale, "0.000806");
551 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
552 adis16300_read_frequency,
553 adis16300_write_frequency);
555 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16300_write_reset, 0);
557 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
559 static IIO_CONST_ATTR(name, "adis16300");
561 static struct attribute *adis16300_event_attributes[] = {
562 NULL
565 static struct attribute_group adis16300_event_attribute_group = {
566 .attrs = adis16300_event_attributes,
569 static 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
596 static const struct attribute_group adis16300_attribute_group = {
597 .attrs = adis16300_attributes,
600 static int __devinit adis16300_probe(struct spi_device *spi)
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;
608 /* this is only used for removal purposes */
609 spi_set_drvdata(spi, st);
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;
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;
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;
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;
639 ret = adis16300_configure_ring(st->indio_dev);
640 if (ret)
641 goto error_free_dev;
643 ret = iio_device_register(st->indio_dev);
644 if (ret)
645 goto error_unreg_ring_funcs;
646 regdone = 1;
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;
654 if (spi->irq) {
655 ret = iio_register_interrupt_line(spi->irq,
656 st->indio_dev,
658 IRQF_TRIGGER_RISING,
659 "adis16300");
660 if (ret)
661 goto error_uninitialize_ring;
663 ret = adis16300_probe_trigger(st->indio_dev);
664 if (ret)
665 goto error_unregister_line;
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;
674 error_remove_trigger:
675 adis16300_remove_trigger(st->indio_dev);
676 error_unregister_line:
677 if (spi->irq)
678 iio_unregister_interrupt_line(st->indio_dev, 0);
679 error_uninitialize_ring:
680 iio_ring_buffer_unregister(st->indio_dev->ring);
681 error_unreg_ring_funcs:
682 adis16300_unconfigure_ring(st->indio_dev);
683 error_free_dev:
684 if (regdone)
685 iio_device_unregister(st->indio_dev);
686 else
687 iio_free_device(st->indio_dev);
688 error_free_tx:
689 kfree(st->tx);
690 error_free_rx:
691 kfree(st->rx);
692 error_free_st:
693 kfree(st);
694 error_ret:
695 return ret;
698 static int adis16300_remove(struct spi_device *spi)
700 int ret;
701 struct adis16300_state *st = spi_get_drvdata(spi);
702 struct iio_dev *indio_dev = st->indio_dev;
704 ret = adis16300_stop_device(&(indio_dev->dev));
705 if (ret)
706 goto err_ret;
708 flush_scheduled_work();
710 adis16300_remove_trigger(indio_dev);
711 if (spi->irq)
712 iio_unregister_interrupt_line(indio_dev, 0);
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);
721 return 0;
723 err_ret:
724 return ret;
727 static struct spi_driver adis16300_driver = {
728 .driver = {
729 .name = "adis16300",
730 .owner = THIS_MODULE,
732 .probe = adis16300_probe,
733 .remove = __devexit_p(adis16300_remove),
736 static __init int adis16300_init(void)
738 return spi_register_driver(&adis16300_driver);
740 module_init(adis16300_init);
742 static __exit void adis16300_exit(void)
744 spi_unregister_driver(&adis16300_driver);
746 module_exit(adis16300_exit);
748 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
749 MODULE_DESCRIPTION("Analog Devices ADIS16300 IMU SPI driver");
750 MODULE_LICENSE("GPL v2");