staging: iio: adis16400 clean out some unused code
[linux-2.6.git] / drivers / staging / iio / imu / adis16400_core.c
blobe69e2ce47da3e20706e9e1ca76b3ca08b9b9eb2d
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>
25 #include <linux/sysfs.h>
26 #include <linux/list.h>
28 #include "../iio.h"
29 #include "../sysfs.h"
30 #include "../accel/accel.h"
31 #include "../adc/adc.h"
32 #include "../gyro/gyro.h"
33 #include "../magnetometer/magnet.h"
35 #include "adis16400.h"
37 #define DRIVER_NAME "adis16400"
39 /* At the moment the spi framework doesn't allow global setting of cs_change.
40 * It's in the likely to be added comment at the top of spi.h.
41 * This means that use cannot be made of spi_write etc.
44 /**
45 * adis16400_spi_write_reg_8() - write single byte to a register
46 * @dev: device associated with child of actual device (iio_dev or iio_trig)
47 * @reg_address: the address of the register to be written
48 * @val: the value to write
49 **/
50 static int adis16400_spi_write_reg_8(struct device *dev,
51 u8 reg_address,
52 u8 val)
54 int ret;
55 struct iio_dev *indio_dev = dev_get_drvdata(dev);
56 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
58 mutex_lock(&st->buf_lock);
59 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
60 st->tx[1] = val;
62 ret = spi_write(st->us, st->tx, 2);
63 mutex_unlock(&st->buf_lock);
65 return ret;
68 /**
69 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
70 * @dev: device associated with child of actual device (iio_dev or iio_trig)
71 * @reg_address: the address of the lower of the two registers. Second register
72 * is assumed to have address one greater.
73 * @val: value to be written
74 **/
75 static int adis16400_spi_write_reg_16(struct device *dev,
76 u8 lower_reg_address,
77 u16 value)
79 int ret;
80 struct spi_message msg;
81 struct iio_dev *indio_dev = dev_get_drvdata(dev);
82 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
83 struct spi_transfer xfers[] = {
85 .tx_buf = st->tx,
86 .bits_per_word = 8,
87 .len = 2,
88 .cs_change = 1,
89 }, {
90 .tx_buf = st->tx + 2,
91 .bits_per_word = 8,
92 .len = 2,
93 .cs_change = 1,
97 mutex_lock(&st->buf_lock);
98 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
99 st->tx[1] = value & 0xFF;
100 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
101 st->tx[3] = (value >> 8) & 0xFF;
103 spi_message_init(&msg);
104 spi_message_add_tail(&xfers[0], &msg);
105 spi_message_add_tail(&xfers[1], &msg);
106 ret = spi_sync(st->us, &msg);
107 mutex_unlock(&st->buf_lock);
109 return ret;
113 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
114 * @dev: device associated with child of actual device (iio_dev or iio_trig)
115 * @reg_address: the address of the lower of the two registers. Second register
116 * is assumed to have address one greater.
117 * @val: somewhere to pass back the value read
119 static int adis16400_spi_read_reg_16(struct device *dev,
120 u8 lower_reg_address,
121 u16 *val)
123 struct spi_message msg;
124 struct iio_dev *indio_dev = dev_get_drvdata(dev);
125 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
126 int ret;
127 struct spi_transfer xfers[] = {
129 .tx_buf = st->tx,
130 .bits_per_word = 8,
131 .len = 2,
132 .cs_change = 1,
133 }, {
134 .rx_buf = st->rx,
135 .bits_per_word = 8,
136 .len = 2,
137 .cs_change = 1,
141 mutex_lock(&st->buf_lock);
142 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
143 st->tx[1] = 0;
144 st->tx[2] = 0;
145 st->tx[3] = 0;
147 spi_message_init(&msg);
148 spi_message_add_tail(&xfers[0], &msg);
149 spi_message_add_tail(&xfers[1], &msg);
150 ret = spi_sync(st->us, &msg);
151 if (ret) {
152 dev_err(&st->us->dev,
153 "problem when reading 16 bit register 0x%02X",
154 lower_reg_address);
155 goto error_ret;
157 *val = (st->rx[0] << 8) | st->rx[1];
159 error_ret:
160 mutex_unlock(&st->buf_lock);
161 return ret;
165 * adis16400_spi_read_burst() - read all data registers
166 * @dev: device associated with child of actual device (iio_dev or iio_trig)
167 * @rx: somewhere to pass back the value read (min size is 24 bytes)
169 int adis16400_spi_read_burst(struct device *dev, u8 *rx)
171 struct spi_message msg;
172 struct iio_dev *indio_dev = dev_get_drvdata(dev);
173 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
174 u32 old_speed_hz = st->us->max_speed_hz;
175 int ret;
177 struct spi_transfer xfers[] = {
179 .tx_buf = st->tx,
180 .bits_per_word = 8,
181 .len = 2,
182 .cs_change = 0,
183 }, {
184 .rx_buf = rx,
185 .bits_per_word = 8,
186 .len = 24,
187 .cs_change = 1,
191 mutex_lock(&st->buf_lock);
192 st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
193 st->tx[1] = 0;
195 spi_message_init(&msg);
196 spi_message_add_tail(&xfers[0], &msg);
197 spi_message_add_tail(&xfers[1], &msg);
199 st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
200 spi_setup(st->us);
202 ret = spi_sync(st->us, &msg);
203 if (ret)
204 dev_err(&st->us->dev, "problem when burst reading");
206 st->us->max_speed_hz = old_speed_hz;
207 spi_setup(st->us);
208 mutex_unlock(&st->buf_lock);
209 return ret;
212 static ssize_t adis16400_spi_read_signed(struct device *dev,
213 struct device_attribute *attr,
214 char *buf,
215 unsigned bits)
217 int ret;
218 s16 val = 0;
219 unsigned shift = 16 - bits;
220 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
222 ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
223 if (ret)
224 return ret;
226 if (val & ADIS16400_ERROR_ACTIVE)
227 adis16400_check_status(dev);
228 val = ((s16)(val << shift) >> shift);
229 return sprintf(buf, "%d\n", val);
232 static ssize_t adis16400_read_12bit_unsigned(struct device *dev,
233 struct device_attribute *attr,
234 char *buf)
236 int ret;
237 u16 val = 0;
238 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
240 ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val);
241 if (ret)
242 return ret;
244 if (val & ADIS16400_ERROR_ACTIVE)
245 adis16400_check_status(dev);
247 return sprintf(buf, "%u\n", val & 0x0FFF);
250 static ssize_t adis16400_read_14bit_signed(struct device *dev,
251 struct device_attribute *attr,
252 char *buf)
254 struct iio_dev *indio_dev = dev_get_drvdata(dev);
255 ssize_t ret;
257 /* Take the iio_dev status lock */
258 mutex_lock(&indio_dev->mlock);
259 ret = adis16400_spi_read_signed(dev, attr, buf, 14);
260 mutex_unlock(&indio_dev->mlock);
262 return ret;
265 static ssize_t adis16400_read_12bit_signed(struct device *dev,
266 struct device_attribute *attr,
267 char *buf)
269 struct iio_dev *indio_dev = dev_get_drvdata(dev);
270 ssize_t ret;
272 /* Take the iio_dev status lock */
273 mutex_lock(&indio_dev->mlock);
274 ret = adis16400_spi_read_signed(dev, attr, buf, 12);
275 mutex_unlock(&indio_dev->mlock);
277 return ret;
281 static ssize_t adis16400_write_16bit(struct device *dev,
282 struct device_attribute *attr,
283 const char *buf,
284 size_t len)
286 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
287 int ret;
288 long val;
290 ret = strict_strtol(buf, 10, &val);
291 if (ret)
292 goto error_ret;
293 ret = adis16400_spi_write_reg_16(dev, this_attr->address, val);
295 error_ret:
296 return ret ? ret : len;
299 static ssize_t adis16400_read_frequency(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
303 int ret, len = 0;
304 u16 t;
305 int sps;
306 ret = adis16400_spi_read_reg_16(dev,
307 ADIS16400_SMPL_PRD,
308 &t);
309 if (ret)
310 return ret;
311 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
312 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
313 len = sprintf(buf, "%d SPS\n", sps);
314 return len;
317 static ssize_t adis16400_write_frequency(struct device *dev,
318 struct device_attribute *attr,
319 const char *buf,
320 size_t len)
322 struct iio_dev *indio_dev = dev_get_drvdata(dev);
323 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
324 long val;
325 int ret;
326 u8 t;
328 ret = strict_strtol(buf, 10, &val);
329 if (ret)
330 return ret;
332 mutex_lock(&indio_dev->mlock);
334 t = (1638 / val);
335 if (t > 0)
336 t--;
337 t &= ADIS16400_SMPL_PRD_DIV_MASK;
338 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
339 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
340 else
341 st->us->max_speed_hz = ADIS16400_SPI_FAST;
343 ret = adis16400_spi_write_reg_8(dev,
344 ADIS16400_SMPL_PRD,
347 mutex_unlock(&indio_dev->mlock);
349 return ret ? ret : len;
352 static ssize_t adis16400_write_reset(struct device *dev,
353 struct device_attribute *attr,
354 const char *buf, size_t len)
356 if (len < 1)
357 return -1;
358 switch (buf[0]) {
359 case '1':
360 case 'y':
361 case 'Y':
362 return adis16400_reset(dev);
364 return -1;
369 int adis16400_set_irq(struct device *dev, bool enable)
371 int ret;
372 u16 msc;
373 ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc);
374 if (ret)
375 goto error_ret;
377 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
378 if (enable)
379 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
380 else
381 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
383 ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc);
384 if (ret)
385 goto error_ret;
387 error_ret:
388 return ret;
391 int adis16400_reset(struct device *dev)
393 int ret;
394 ret = adis16400_spi_write_reg_8(dev,
395 ADIS16400_GLOB_CMD,
396 ADIS16400_GLOB_CMD_SW_RESET);
397 if (ret)
398 dev_err(dev, "problem resetting device");
400 return ret;
403 /* Power down the device */
404 static int adis16400_stop_device(struct device *dev)
406 int ret;
407 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
409 ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val);
410 if (ret)
411 dev_err(dev, "problem with turning device off: SLP_CNT");
413 return ret;
416 static int adis16400_self_test(struct device *dev)
418 int ret;
419 ret = adis16400_spi_write_reg_16(dev,
420 ADIS16400_MSC_CTRL,
421 ADIS16400_MSC_CTRL_MEM_TEST);
422 if (ret) {
423 dev_err(dev, "problem starting self test");
424 goto err_ret;
427 adis16400_check_status(dev);
429 err_ret:
430 return ret;
433 int adis16400_check_status(struct device *dev)
435 u16 status;
436 int ret;
438 ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status);
440 if (ret < 0) {
441 dev_err(dev, "Reading status failed\n");
442 goto error_ret;
444 ret = status;
445 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
446 dev_err(dev, "Z-axis accelerometer self-test failure\n");
447 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
448 dev_err(dev, "Y-axis accelerometer self-test failure\n");
449 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
450 dev_err(dev, "X-axis accelerometer self-test failure\n");
451 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
452 dev_err(dev, "X-axis gyroscope self-test failure\n");
453 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
454 dev_err(dev, "Y-axis gyroscope self-test failure\n");
455 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
456 dev_err(dev, "Z-axis gyroscope self-test failure\n");
457 if (status & ADIS16400_DIAG_STAT_ALARM2)
458 dev_err(dev, "Alarm 2 active\n");
459 if (status & ADIS16400_DIAG_STAT_ALARM1)
460 dev_err(dev, "Alarm 1 active\n");
461 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
462 dev_err(dev, "Flash checksum error\n");
463 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
464 dev_err(dev, "Self test error\n");
465 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
466 dev_err(dev, "Sensor overrange\n");
467 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
468 dev_err(dev, "SPI failure\n");
469 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
470 dev_err(dev, "Flash update failed\n");
471 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
472 dev_err(dev, "Power supply above 5.25V\n");
473 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
474 dev_err(dev, "Power supply below 4.75V\n");
476 error_ret:
477 return ret;
480 static int adis16400_initial_setup(struct adis16400_state *st)
482 int ret;
483 u16 prod_id, smp_prd;
484 struct device *dev = &st->indio_dev->dev;
486 /* use low spi speed for init */
487 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
488 st->us->mode = SPI_MODE_3;
489 spi_setup(st->us);
491 /* Disable IRQ */
492 ret = adis16400_set_irq(dev, false);
493 if (ret) {
494 dev_err(dev, "disable irq failed");
495 goto err_ret;
498 /* Do self test */
500 /* Read status register to check the result */
501 ret = adis16400_check_status(dev);
502 if (ret) {
503 adis16400_reset(dev);
504 dev_err(dev, "device not playing ball -> reset");
505 msleep(ADIS16400_STARTUP_DELAY);
506 ret = adis16400_check_status(dev);
507 if (ret) {
508 dev_err(dev, "giving up");
509 goto err_ret;
513 ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id);
514 if (ret)
515 goto err_ret;
517 if (prod_id != ADIS16400_PRODUCT_ID_DEFAULT)
518 dev_warn(dev, "unknown product id");
520 printk(KERN_INFO DRIVER_NAME ": prod_id 0x%04x at CS%d (irq %d)\n",
521 prod_id, st->us->chip_select, st->us->irq);
523 /* use high spi speed if possible */
524 ret = adis16400_spi_read_reg_16(dev, ADIS16400_SMPL_PRD, &smp_prd);
525 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
526 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
527 spi_setup(st->us);
531 err_ret:
533 return ret;
536 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
537 adis16400_read_12bit_signed,
538 adis16400_write_16bit,
539 ADIS16400_XACCL_OFF);
541 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
542 adis16400_read_12bit_signed,
543 adis16400_write_16bit,
544 ADIS16400_YACCL_OFF);
546 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
547 adis16400_read_12bit_signed,
548 adis16400_write_16bit,
549 ADIS16400_ZACCL_OFF);
551 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16400_read_14bit_signed,
552 ADIS16400_SUPPLY_OUT);
553 static IIO_CONST_ATTR(in_supply_scale, "0.002418");
555 static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed,
556 ADIS16400_XGYRO_OUT);
557 static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed,
558 ADIS16400_YGYRO_OUT);
559 static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed,
560 ADIS16400_ZGYRO_OUT);
561 static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s");
563 static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed,
564 ADIS16400_XACCL_OUT);
565 static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed,
566 ADIS16400_YACCL_OUT);
567 static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed,
568 ADIS16400_ZACCL_OUT);
569 static IIO_CONST_ATTR(accel_scale, "0.00333 g");
571 static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed,
572 ADIS16400_XMAGN_OUT);
573 static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed,
574 ADIS16400_YMAGN_OUT);
575 static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed,
576 ADIS16400_ZMAGN_OUT);
577 static IIO_CONST_ATTR(magn_scale, "0.0005 Gs");
580 static IIO_DEV_ATTR_TEMP_RAW(adis16400_read_12bit_signed);
581 static IIO_CONST_ATTR(temp_offset, "198.16 K");
582 static IIO_CONST_ATTR(temp_scale, "0.14 K");
584 static IIO_DEV_ATTR_IN_RAW(0, adis16400_read_12bit_unsigned,
585 ADIS16400_AUX_ADC);
586 static IIO_CONST_ATTR(in0_scale, "0.000806");
588 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
589 adis16400_read_frequency,
590 adis16400_write_frequency);
592 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
594 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
596 static IIO_CONST_ATTR(name, "adis16400");
598 static struct attribute *adis16400_event_attributes[] = {
599 NULL
602 static struct attribute_group adis16400_event_attribute_group = {
603 .attrs = adis16400_event_attributes,
606 static struct attribute *adis16400_attributes[] = {
607 &iio_dev_attr_accel_x_offset.dev_attr.attr,
608 &iio_dev_attr_accel_y_offset.dev_attr.attr,
609 &iio_dev_attr_accel_z_offset.dev_attr.attr,
610 &iio_dev_attr_in_supply_raw.dev_attr.attr,
611 &iio_const_attr_in_supply_scale.dev_attr.attr,
612 &iio_dev_attr_gyro_x_raw.dev_attr.attr,
613 &iio_dev_attr_gyro_y_raw.dev_attr.attr,
614 &iio_dev_attr_gyro_z_raw.dev_attr.attr,
615 &iio_const_attr_gyro_scale.dev_attr.attr,
616 &iio_dev_attr_accel_x_raw.dev_attr.attr,
617 &iio_dev_attr_accel_y_raw.dev_attr.attr,
618 &iio_dev_attr_accel_z_raw.dev_attr.attr,
619 &iio_const_attr_accel_scale.dev_attr.attr,
620 &iio_dev_attr_magn_x_raw.dev_attr.attr,
621 &iio_dev_attr_magn_y_raw.dev_attr.attr,
622 &iio_dev_attr_magn_z_raw.dev_attr.attr,
623 &iio_const_attr_magn_scale.dev_attr.attr,
624 &iio_dev_attr_temp_raw.dev_attr.attr,
625 &iio_const_attr_temp_offset.dev_attr.attr,
626 &iio_const_attr_temp_scale.dev_attr.attr,
627 &iio_dev_attr_in0_raw.dev_attr.attr,
628 &iio_const_attr_in0_scale.dev_attr.attr,
629 &iio_dev_attr_sampling_frequency.dev_attr.attr,
630 &iio_const_attr_available_sampling_frequency.dev_attr.attr,
631 &iio_dev_attr_reset.dev_attr.attr,
632 &iio_const_attr_name.dev_attr.attr,
633 NULL
636 static const struct attribute_group adis16400_attribute_group = {
637 .attrs = adis16400_attributes,
640 static int __devinit adis16400_probe(struct spi_device *spi)
642 int ret, regdone = 0;
643 struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL);
644 if (!st) {
645 ret = -ENOMEM;
646 goto error_ret;
648 /* this is only used for removal purposes */
649 spi_set_drvdata(spi, st);
651 /* Allocate the comms buffers */
652 st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL);
653 if (st->rx == NULL) {
654 ret = -ENOMEM;
655 goto error_free_st;
657 st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL);
658 if (st->tx == NULL) {
659 ret = -ENOMEM;
660 goto error_free_rx;
662 st->us = spi;
663 mutex_init(&st->buf_lock);
664 /* setup the industrialio driver allocated elements */
665 st->indio_dev = iio_allocate_device();
666 if (st->indio_dev == NULL) {
667 ret = -ENOMEM;
668 goto error_free_tx;
671 st->indio_dev->dev.parent = &spi->dev;
672 st->indio_dev->num_interrupt_lines = 1;
673 st->indio_dev->event_attrs = &adis16400_event_attribute_group;
674 st->indio_dev->attrs = &adis16400_attribute_group;
675 st->indio_dev->dev_data = (void *)(st);
676 st->indio_dev->driver_module = THIS_MODULE;
677 st->indio_dev->modes = INDIO_DIRECT_MODE;
679 ret = adis16400_configure_ring(st->indio_dev);
680 if (ret)
681 goto error_free_dev;
683 ret = iio_device_register(st->indio_dev);
684 if (ret)
685 goto error_unreg_ring_funcs;
686 regdone = 1;
688 ret = adis16400_initialize_ring(st->indio_dev->ring);
689 if (ret) {
690 printk(KERN_ERR "failed to initialize the ring\n");
691 goto error_unreg_ring_funcs;
694 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
695 #if 0 /* fixme: here we should support */
696 iio_init_work_cont(&st->work_cont_thresh,
697 NULL,
698 adis16400_thresh_handler_bh_no_check,
701 st);
702 #endif
703 ret = iio_register_interrupt_line(spi->irq,
704 st->indio_dev,
706 IRQF_TRIGGER_RISING,
707 "adis16400");
708 if (ret)
709 goto error_uninitialize_ring;
711 ret = adis16400_probe_trigger(st->indio_dev);
712 if (ret)
713 goto error_unregister_line;
716 /* Get the device into a sane initial state */
717 ret = adis16400_initial_setup(st);
718 if (ret)
719 goto error_remove_trigger;
720 return 0;
722 error_remove_trigger:
723 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
724 adis16400_remove_trigger(st->indio_dev);
725 error_unregister_line:
726 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
727 iio_unregister_interrupt_line(st->indio_dev, 0);
728 error_uninitialize_ring:
729 adis16400_uninitialize_ring(st->indio_dev->ring);
730 error_unreg_ring_funcs:
731 adis16400_unconfigure_ring(st->indio_dev);
732 error_free_dev:
733 if (regdone)
734 iio_device_unregister(st->indio_dev);
735 else
736 iio_free_device(st->indio_dev);
737 error_free_tx:
738 kfree(st->tx);
739 error_free_rx:
740 kfree(st->rx);
741 error_free_st:
742 kfree(st);
743 error_ret:
744 return ret;
747 /* fixme, confirm ordering in this function */
748 static int adis16400_remove(struct spi_device *spi)
750 int ret;
751 struct adis16400_state *st = spi_get_drvdata(spi);
752 struct iio_dev *indio_dev = st->indio_dev;
754 ret = adis16400_stop_device(&(indio_dev->dev));
755 if (ret)
756 goto err_ret;
758 flush_scheduled_work();
760 adis16400_remove_trigger(indio_dev);
761 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
762 iio_unregister_interrupt_line(indio_dev, 0);
764 adis16400_uninitialize_ring(indio_dev->ring);
765 adis16400_unconfigure_ring(indio_dev);
766 iio_device_unregister(indio_dev);
767 kfree(st->tx);
768 kfree(st->rx);
769 kfree(st);
771 return 0;
773 err_ret:
774 return ret;
777 static struct spi_driver adis16400_driver = {
778 .driver = {
779 .name = "adis16400",
780 .owner = THIS_MODULE,
782 .probe = adis16400_probe,
783 .remove = __devexit_p(adis16400_remove),
786 static __init int adis16400_init(void)
788 return spi_register_driver(&adis16400_driver);
790 module_init(adis16400_init);
792 static __exit void adis16400_exit(void)
794 spi_unregister_driver(&adis16400_driver);
796 module_exit(adis16400_exit);
798 MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
799 MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
800 MODULE_LICENSE("GPL v2");