staging: IIO: IMU: ADIS16400: Fix product ID check, skip embedded revision number
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / imu / adis16400_core.c
blob0d8df6282b21bb13af9ad5ef96cb57587265ebc4
1 /*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/gpio.h>
19 #include <linux/delay.h>
20 #include <linux/mutex.h>
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/spi/spi.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26 #include <linux/list.h>
28 #include "../iio.h"
29 #include "../sysfs.h"
30 #include "../ring_generic.h"
31 #include "../accel/accel.h"
32 #include "../adc/adc.h"
33 #include "../gyro/gyro.h"
34 #include "../magnetometer/magnet.h"
36 #include "adis16400.h"
38 #define DRIVER_NAME "adis16400"
40 static int adis16400_check_status(struct device *dev);
42 /* At the moment the spi framework doesn't allow global setting of cs_change.
43 * It's in the likely to be added comment at the top of spi.h.
44 * This means that use cannot be made of spi_write etc.
47 /**
48 * adis16400_spi_write_reg_8() - write single byte to a register
49 * @dev: device associated with child of actual device (iio_dev or iio_trig)
50 * @reg_address: the address of the register to be written
51 * @val: the value to write
52 **/
53 static int adis16400_spi_write_reg_8(struct device *dev,
54 u8 reg_address,
55 u8 val)
57 int ret;
58 struct iio_dev *indio_dev = dev_get_drvdata(dev);
59 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
61 mutex_lock(&st->buf_lock);
62 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
63 st->tx[1] = val;
65 ret = spi_write(st->us, st->tx, 2);
66 mutex_unlock(&st->buf_lock);
68 return ret;
71 /**
72 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
73 * @dev: device associated with child of actual device (iio_dev or iio_trig)
74 * @reg_address: the address of the lower of the two registers. Second register
75 * is assumed to have address one greater.
76 * @val: value to be written
77 **/
78 static int adis16400_spi_write_reg_16(struct device *dev,
79 u8 lower_reg_address,
80 u16 value)
82 int ret;
83 struct spi_message msg;
84 struct iio_dev *indio_dev = dev_get_drvdata(dev);
85 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
86 struct spi_transfer xfers[] = {
88 .tx_buf = st->tx,
89 .bits_per_word = 8,
90 .len = 2,
91 .cs_change = 1,
92 }, {
93 .tx_buf = st->tx + 2,
94 .bits_per_word = 8,
95 .len = 2,
99 mutex_lock(&st->buf_lock);
100 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
101 st->tx[1] = value & 0xFF;
102 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
103 st->tx[3] = (value >> 8) & 0xFF;
105 spi_message_init(&msg);
106 spi_message_add_tail(&xfers[0], &msg);
107 spi_message_add_tail(&xfers[1], &msg);
108 ret = spi_sync(st->us, &msg);
109 mutex_unlock(&st->buf_lock);
111 return ret;
115 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
116 * @dev: device associated with child of actual device (iio_dev or iio_trig)
117 * @reg_address: the address of the lower of the two registers. Second register
118 * is assumed to have address one greater.
119 * @val: somewhere to pass back the value read
121 static int adis16400_spi_read_reg_16(struct device *dev,
122 u8 lower_reg_address,
123 u16 *val)
125 struct spi_message msg;
126 struct iio_dev *indio_dev = dev_get_drvdata(dev);
127 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
128 int ret;
129 struct spi_transfer xfers[] = {
131 .tx_buf = st->tx,
132 .bits_per_word = 8,
133 .len = 2,
134 .cs_change = 1,
135 }, {
136 .rx_buf = st->rx,
137 .bits_per_word = 8,
138 .len = 2,
142 mutex_lock(&st->buf_lock);
143 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
144 st->tx[1] = 0;
145 st->tx[2] = 0;
146 st->tx[3] = 0;
148 spi_message_init(&msg);
149 spi_message_add_tail(&xfers[0], &msg);
150 spi_message_add_tail(&xfers[1], &msg);
151 ret = spi_sync(st->us, &msg);
152 if (ret) {
153 dev_err(&st->us->dev,
154 "problem when reading 16 bit register 0x%02X",
155 lower_reg_address);
156 goto error_ret;
158 *val = (st->rx[0] << 8) | st->rx[1];
160 error_ret:
161 mutex_unlock(&st->buf_lock);
162 return ret;
165 static ssize_t adis16400_spi_read_signed(struct device *dev,
166 struct device_attribute *attr,
167 char *buf,
168 unsigned bits)
170 int ret;
171 s16 val = 0;
172 unsigned shift = 16 - bits;
173 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
175 ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
176 if (ret)
177 return ret;
179 if (val & ADIS16400_ERROR_ACTIVE)
180 adis16400_check_status(dev);
181 val = ((s16)(val << shift) >> shift);
182 return sprintf(buf, "%d\n", val);
185 static ssize_t adis16400_read_12bit_unsigned(struct device *dev,
186 struct device_attribute *attr,
187 char *buf)
189 int ret;
190 u16 val = 0;
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
193 ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val);
194 if (ret)
195 return ret;
197 if (val & ADIS16400_ERROR_ACTIVE)
198 adis16400_check_status(dev);
200 return sprintf(buf, "%u\n", val & 0x0FFF);
203 static ssize_t adis16400_read_14bit_signed(struct device *dev,
204 struct device_attribute *attr,
205 char *buf)
207 struct iio_dev *indio_dev = dev_get_drvdata(dev);
208 ssize_t ret;
210 /* Take the iio_dev status lock */
211 mutex_lock(&indio_dev->mlock);
212 ret = adis16400_spi_read_signed(dev, attr, buf, 14);
213 mutex_unlock(&indio_dev->mlock);
215 return ret;
218 static ssize_t adis16400_read_12bit_signed(struct device *dev,
219 struct device_attribute *attr,
220 char *buf)
222 struct iio_dev *indio_dev = dev_get_drvdata(dev);
223 ssize_t ret;
225 /* Take the iio_dev status lock */
226 mutex_lock(&indio_dev->mlock);
227 ret = adis16400_spi_read_signed(dev, attr, buf, 12);
228 mutex_unlock(&indio_dev->mlock);
230 return ret;
233 static ssize_t adis16400_write_16bit(struct device *dev,
234 struct device_attribute *attr,
235 const char *buf,
236 size_t len)
238 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
239 int ret;
240 long val;
242 ret = strict_strtol(buf, 10, &val);
243 if (ret)
244 goto error_ret;
245 ret = adis16400_spi_write_reg_16(dev, this_attr->address, val);
247 error_ret:
248 return ret ? ret : len;
251 static ssize_t adis16400_read_frequency(struct device *dev,
252 struct device_attribute *attr,
253 char *buf)
255 int ret, len = 0;
256 u16 t;
257 int sps;
258 ret = adis16400_spi_read_reg_16(dev,
259 ADIS16400_SMPL_PRD,
260 &t);
261 if (ret)
262 return ret;
263 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
264 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
265 len = sprintf(buf, "%d SPS\n", sps);
266 return len;
269 static ssize_t adis16400_write_frequency(struct device *dev,
270 struct device_attribute *attr,
271 const char *buf,
272 size_t len)
274 struct iio_dev *indio_dev = dev_get_drvdata(dev);
275 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
276 long val;
277 int ret;
278 u8 t;
280 ret = strict_strtol(buf, 10, &val);
281 if (ret)
282 return ret;
284 mutex_lock(&indio_dev->mlock);
286 t = (1638 / val);
287 if (t > 0)
288 t--;
289 t &= ADIS16400_SMPL_PRD_DIV_MASK;
290 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
291 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
292 else
293 st->us->max_speed_hz = ADIS16400_SPI_FAST;
295 ret = adis16400_spi_write_reg_8(dev,
296 ADIS16400_SMPL_PRD,
299 mutex_unlock(&indio_dev->mlock);
301 return ret ? ret : len;
304 static int adis16400_reset(struct device *dev)
306 int ret;
307 ret = adis16400_spi_write_reg_8(dev,
308 ADIS16400_GLOB_CMD,
309 ADIS16400_GLOB_CMD_SW_RESET);
310 if (ret)
311 dev_err(dev, "problem resetting device");
313 return ret;
316 static ssize_t adis16400_write_reset(struct device *dev,
317 struct device_attribute *attr,
318 const char *buf, size_t len)
320 if (len < 1)
321 return -1;
322 switch (buf[0]) {
323 case '1':
324 case 'y':
325 case 'Y':
326 return adis16400_reset(dev);
328 return -1;
331 int adis16400_set_irq(struct device *dev, bool enable)
333 int ret;
334 u16 msc;
335 ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc);
336 if (ret)
337 goto error_ret;
339 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
340 if (enable)
341 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
342 else
343 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
345 ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc);
346 if (ret)
347 goto error_ret;
349 error_ret:
350 return ret;
353 /* Power down the device */
354 static int adis16400_stop_device(struct device *dev)
356 int ret;
357 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
359 ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val);
360 if (ret)
361 dev_err(dev, "problem with turning device off: SLP_CNT");
363 return ret;
366 static int adis16400_self_test(struct device *dev)
368 int ret;
369 ret = adis16400_spi_write_reg_16(dev,
370 ADIS16400_MSC_CTRL,
371 ADIS16400_MSC_CTRL_MEM_TEST);
372 if (ret) {
373 dev_err(dev, "problem starting self test");
374 goto err_ret;
376 msleep(ADIS16400_MTEST_DELAY);
377 adis16400_check_status(dev);
379 err_ret:
380 return ret;
383 static int adis16400_check_status(struct device *dev)
385 u16 status;
386 int ret;
388 ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status);
390 if (ret < 0) {
391 dev_err(dev, "Reading status failed\n");
392 goto error_ret;
394 ret = status;
395 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
396 dev_err(dev, "Z-axis accelerometer self-test failure\n");
397 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
398 dev_err(dev, "Y-axis accelerometer self-test failure\n");
399 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
400 dev_err(dev, "X-axis accelerometer self-test failure\n");
401 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
402 dev_err(dev, "X-axis gyroscope self-test failure\n");
403 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
404 dev_err(dev, "Y-axis gyroscope self-test failure\n");
405 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
406 dev_err(dev, "Z-axis gyroscope self-test failure\n");
407 if (status & ADIS16400_DIAG_STAT_ALARM2)
408 dev_err(dev, "Alarm 2 active\n");
409 if (status & ADIS16400_DIAG_STAT_ALARM1)
410 dev_err(dev, "Alarm 1 active\n");
411 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
412 dev_err(dev, "Flash checksum error\n");
413 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
414 dev_err(dev, "Self test error\n");
415 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
416 dev_err(dev, "Sensor overrange\n");
417 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
418 dev_err(dev, "SPI failure\n");
419 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
420 dev_err(dev, "Flash update failed\n");
421 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
422 dev_err(dev, "Power supply above 5.25V\n");
423 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
424 dev_err(dev, "Power supply below 4.75V\n");
426 error_ret:
427 return ret;
430 static int adis16400_initial_setup(struct adis16400_state *st)
432 int ret;
433 u16 prod_id, smp_prd;
434 struct device *dev = &st->indio_dev->dev;
436 /* use low spi speed for init */
437 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
438 st->us->mode = SPI_MODE_3;
439 spi_setup(st->us);
441 /* Disable IRQ */
442 ret = adis16400_set_irq(dev, false);
443 if (ret) {
444 dev_err(dev, "disable irq failed");
445 goto err_ret;
448 /* Do self test */
449 ret = adis16400_self_test(dev);
450 if (ret) {
451 dev_err(dev, "self test failure");
452 goto err_ret;
455 /* Read status register to check the result */
456 ret = adis16400_check_status(dev);
457 if (ret) {
458 adis16400_reset(dev);
459 dev_err(dev, "device not playing ball -> reset");
460 msleep(ADIS16400_STARTUP_DELAY);
461 ret = adis16400_check_status(dev);
462 if (ret) {
463 dev_err(dev, "giving up");
464 goto err_ret;
468 ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id);
469 if (ret)
470 goto err_ret;
472 if ((prod_id & 0xF000) != ADIS16400_PRODUCT_ID_DEFAULT)
473 dev_warn(dev, "unknown product id");
475 printk(KERN_INFO DRIVER_NAME ": prod_id 0x%04x at CS%d (irq %d)\n",
476 prod_id, st->us->chip_select, st->us->irq);
478 /* use high spi speed if possible */
479 ret = adis16400_spi_read_reg_16(dev, ADIS16400_SMPL_PRD, &smp_prd);
480 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
481 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
482 spi_setup(st->us);
486 err_ret:
488 return ret;
491 #define ADIS16400_DEV_ATTR_CALIBBIAS(_channel, _reg) \
492 IIO_DEV_ATTR_##_channel##_CALIBBIAS(S_IWUSR | S_IRUGO, \
493 adis16400_read_12bit_signed, \
494 adis16400_write_16bit, \
495 _reg)
497 static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_X, ADIS16400_XGYRO_OFF);
498 static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_Y, ADIS16400_YGYRO_OFF);
499 static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_Z, ADIS16400_ZGYRO_OFF);
501 static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_X, ADIS16400_XACCL_OFF);
502 static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_Y, ADIS16400_YACCL_OFF);
503 static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_Z, ADIS16400_ZACCL_OFF);
506 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16400_read_14bit_signed,
507 ADIS16400_SUPPLY_OUT);
508 static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.002418 V");
510 static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed,
511 ADIS16400_XGYRO_OUT);
512 static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed,
513 ADIS16400_YGYRO_OUT);
514 static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed,
515 ADIS16400_ZGYRO_OUT);
516 static IIO_CONST_ATTR(gyro_scale, "0.0008726646");
518 static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed,
519 ADIS16400_XACCL_OUT);
520 static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed,
521 ADIS16400_YACCL_OUT);
522 static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed,
523 ADIS16400_ZACCL_OUT);
524 static IIO_CONST_ATTR(accel_scale, "0.0326561445");
526 static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed,
527 ADIS16400_XMAGN_OUT);
528 static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed,
529 ADIS16400_YMAGN_OUT);
530 static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed,
531 ADIS16400_ZMAGN_OUT);
532 static IIO_CONST_ATTR(magn_scale, "0.0005 Gs");
535 static IIO_DEV_ATTR_TEMP_RAW(adis16400_read_12bit_signed);
536 static IIO_CONST_ATTR_TEMP_OFFSET("198.16 K");
537 static IIO_CONST_ATTR_TEMP_SCALE("0.14 K");
539 static IIO_DEV_ATTR_IN_RAW(1, adis16400_read_12bit_unsigned,
540 ADIS16400_AUX_ADC);
541 static IIO_CONST_ATTR(in1_scale, "0.000806 V");
543 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
544 adis16400_read_frequency,
545 adis16400_write_frequency);
547 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
549 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
551 static IIO_CONST_ATTR_NAME("adis16400");
553 static struct attribute *adis16400_event_attributes[] = {
554 NULL
557 static struct attribute_group adis16400_event_attribute_group = {
558 .attrs = adis16400_event_attributes,
561 static struct attribute *adis16400_attributes[] = {
562 &iio_dev_attr_gyro_x_calibbias.dev_attr.attr,
563 &iio_dev_attr_gyro_y_calibbias.dev_attr.attr,
564 &iio_dev_attr_gyro_z_calibbias.dev_attr.attr,
565 &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
566 &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
567 &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
568 &iio_dev_attr_in0_supply_raw.dev_attr.attr,
569 &iio_const_attr_in0_supply_scale.dev_attr.attr,
570 &iio_dev_attr_gyro_x_raw.dev_attr.attr,
571 &iio_dev_attr_gyro_y_raw.dev_attr.attr,
572 &iio_dev_attr_gyro_z_raw.dev_attr.attr,
573 &iio_const_attr_gyro_scale.dev_attr.attr,
574 &iio_dev_attr_accel_x_raw.dev_attr.attr,
575 &iio_dev_attr_accel_y_raw.dev_attr.attr,
576 &iio_dev_attr_accel_z_raw.dev_attr.attr,
577 &iio_const_attr_accel_scale.dev_attr.attr,
578 &iio_dev_attr_magn_x_raw.dev_attr.attr,
579 &iio_dev_attr_magn_y_raw.dev_attr.attr,
580 &iio_dev_attr_magn_z_raw.dev_attr.attr,
581 &iio_const_attr_magn_scale.dev_attr.attr,
582 &iio_dev_attr_temp_raw.dev_attr.attr,
583 &iio_const_attr_temp_offset.dev_attr.attr,
584 &iio_const_attr_temp_scale.dev_attr.attr,
585 &iio_dev_attr_in1_raw.dev_attr.attr,
586 &iio_const_attr_in1_scale.dev_attr.attr,
587 &iio_dev_attr_sampling_frequency.dev_attr.attr,
588 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
589 &iio_dev_attr_reset.dev_attr.attr,
590 &iio_const_attr_name.dev_attr.attr,
591 NULL
594 static const struct attribute_group adis16400_attribute_group = {
595 .attrs = adis16400_attributes,
598 static int __devinit adis16400_probe(struct spi_device *spi)
600 int ret, regdone = 0;
601 struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL);
602 if (!st) {
603 ret = -ENOMEM;
604 goto error_ret;
606 /* this is only used for removal purposes */
607 spi_set_drvdata(spi, st);
609 /* Allocate the comms buffers */
610 st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL);
611 if (st->rx == NULL) {
612 ret = -ENOMEM;
613 goto error_free_st;
615 st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL);
616 if (st->tx == NULL) {
617 ret = -ENOMEM;
618 goto error_free_rx;
620 st->us = spi;
621 mutex_init(&st->buf_lock);
622 /* setup the industrialio driver allocated elements */
623 st->indio_dev = iio_allocate_device();
624 if (st->indio_dev == NULL) {
625 ret = -ENOMEM;
626 goto error_free_tx;
629 st->indio_dev->dev.parent = &spi->dev;
630 st->indio_dev->num_interrupt_lines = 1;
631 st->indio_dev->event_attrs = &adis16400_event_attribute_group;
632 st->indio_dev->attrs = &adis16400_attribute_group;
633 st->indio_dev->dev_data = (void *)(st);
634 st->indio_dev->driver_module = THIS_MODULE;
635 st->indio_dev->modes = INDIO_DIRECT_MODE;
637 ret = adis16400_configure_ring(st->indio_dev);
638 if (ret)
639 goto error_free_dev;
641 ret = iio_device_register(st->indio_dev);
642 if (ret)
643 goto error_unreg_ring_funcs;
644 regdone = 1;
646 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
647 if (ret) {
648 printk(KERN_ERR "failed to initialize the ring\n");
649 goto error_unreg_ring_funcs;
652 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
653 ret = iio_register_interrupt_line(spi->irq,
654 st->indio_dev,
656 IRQF_TRIGGER_RISING,
657 "adis16400");
658 if (ret)
659 goto error_uninitialize_ring;
661 ret = adis16400_probe_trigger(st->indio_dev);
662 if (ret)
663 goto error_unregister_line;
666 /* Get the device into a sane initial state */
667 ret = adis16400_initial_setup(st);
668 if (ret)
669 goto error_remove_trigger;
670 return 0;
672 error_remove_trigger:
673 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
674 adis16400_remove_trigger(st->indio_dev);
675 error_unregister_line:
676 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
677 iio_unregister_interrupt_line(st->indio_dev, 0);
678 error_uninitialize_ring:
679 iio_ring_buffer_unregister(st->indio_dev->ring);
680 error_unreg_ring_funcs:
681 adis16400_unconfigure_ring(st->indio_dev);
682 error_free_dev:
683 if (regdone)
684 iio_device_unregister(st->indio_dev);
685 else
686 iio_free_device(st->indio_dev);
687 error_free_tx:
688 kfree(st->tx);
689 error_free_rx:
690 kfree(st->rx);
691 error_free_st:
692 kfree(st);
693 error_ret:
694 return ret;
697 /* fixme, confirm ordering in this function */
698 static int adis16400_remove(struct spi_device *spi)
700 int ret;
701 struct adis16400_state *st = spi_get_drvdata(spi);
702 struct iio_dev *indio_dev = st->indio_dev;
704 ret = adis16400_stop_device(&(indio_dev->dev));
705 if (ret)
706 goto err_ret;
708 flush_scheduled_work();
710 adis16400_remove_trigger(indio_dev);
711 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
712 iio_unregister_interrupt_line(indio_dev, 0);
714 iio_ring_buffer_unregister(st->indio_dev->ring);
715 adis16400_unconfigure_ring(indio_dev);
716 iio_device_unregister(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 adis16400_driver = {
728 .driver = {
729 .name = "adis16400",
730 .owner = THIS_MODULE,
732 .probe = adis16400_probe,
733 .remove = __devexit_p(adis16400_remove),
736 static __init int adis16400_init(void)
738 return spi_register_driver(&adis16400_driver);
740 module_init(adis16400_init);
742 static __exit void adis16400_exit(void)
744 spi_unregister_driver(&adis16400_driver);
746 module_exit(adis16400_exit);
748 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
749 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
750 MODULE_LICENSE("GPL v2");