Merge branch 'viafb-next' of git://github.com/schandinat/linux-2.6
[pandora-kernel.git] / drivers / staging / iio / imu / adis16350_core.c
1 /*
2  * ADIS16350/54/55/60/62/64/65 high precision tri-axis inertial sensor
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 "../adc/adc.h"
26 #include "../gyro/gyro.h"
27
28 #include "adis16350.h"
29
30 #define DRIVER_NAME             "adis16350"
31
32 static int adis16350_check_status(struct device *dev);
33
34 /**
35  * adis16350_spi_write_reg_8() - write single byte to a register
36  * @dev: device associated with child of actual device (iio_dev or iio_trig)
37  * @reg_address: the address of the register to be written
38  * @val: the value to write
39  **/
40 static int adis16350_spi_write_reg_8(struct device *dev,
41                 u8 reg_address,
42                 u8 val)
43 {
44         int ret;
45         struct iio_dev *indio_dev = dev_get_drvdata(dev);
46         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
47
48         mutex_lock(&st->buf_lock);
49         st->tx[0] = ADIS16350_WRITE_REG(reg_address);
50         st->tx[1] = val;
51
52         ret = spi_write(st->us, st->tx, 2);
53         mutex_unlock(&st->buf_lock);
54
55         return ret;
56 }
57
58 /**
59  * adis16350_spi_write_reg_16() - write 2 bytes to a pair of registers
60  * @dev: device associated with child of actual device (iio_dev or iio_trig)
61  * @reg_address: the address of the lower of the two registers. Second register
62  *               is assumed to have address one greater.
63  * @val: value to be written
64  **/
65 static int adis16350_spi_write_reg_16(struct device *dev,
66                 u8 lower_reg_address,
67                 u16 value)
68 {
69         int ret;
70         struct spi_message msg;
71         struct iio_dev *indio_dev = dev_get_drvdata(dev);
72         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
73         struct spi_transfer xfers[] = {
74                 {
75                         .tx_buf = st->tx,
76                         .bits_per_word = 8,
77                         .len = 2,
78                         .cs_change = 1,
79                         .delay_usecs = 35,
80                 }, {
81                         .tx_buf = st->tx + 2,
82                         .bits_per_word = 8,
83                         .len = 2,
84                         .cs_change = 1,
85                         .delay_usecs = 35,
86                 },
87         };
88
89         mutex_lock(&st->buf_lock);
90         st->tx[0] = ADIS16350_WRITE_REG(lower_reg_address);
91         st->tx[1] = value & 0xFF;
92         st->tx[2] = ADIS16350_WRITE_REG(lower_reg_address + 1);
93         st->tx[3] = (value >> 8) & 0xFF;
94
95         spi_message_init(&msg);
96         spi_message_add_tail(&xfers[0], &msg);
97         spi_message_add_tail(&xfers[1], &msg);
98         ret = spi_sync(st->us, &msg);
99         mutex_unlock(&st->buf_lock);
100
101         return ret;
102 }
103
104 /**
105  * adis16350_spi_read_reg_16() - read 2 bytes from a 16-bit register
106  * @dev: device associated with child of actual device (iio_dev or iio_trig)
107  * @reg_address: the address of the lower of the two registers. Second register
108  *               is assumed to have address one greater.
109  * @val: somewhere to pass back the value read
110  **/
111 static int adis16350_spi_read_reg_16(struct device *dev,
112                 u8 lower_reg_address,
113                 u16 *val)
114 {
115         struct spi_message msg;
116         struct iio_dev *indio_dev = dev_get_drvdata(dev);
117         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
118         int ret;
119         struct spi_transfer xfers[] = {
120                 {
121                         .tx_buf = st->tx,
122                         .bits_per_word = 8,
123                         .len = 2,
124                         .cs_change = 1,
125                         .delay_usecs = 35,
126                 }, {
127                         .rx_buf = st->rx,
128                         .bits_per_word = 8,
129                         .len = 2,
130                         .cs_change = 1,
131                         .delay_usecs = 35,
132                 },
133         };
134
135         mutex_lock(&st->buf_lock);
136         st->tx[0] = ADIS16350_READ_REG(lower_reg_address);
137         st->tx[1] = 0;
138         st->tx[2] = 0;
139         st->tx[3] = 0;
140
141         spi_message_init(&msg);
142         spi_message_add_tail(&xfers[0], &msg);
143         spi_message_add_tail(&xfers[1], &msg);
144         ret = spi_sync(st->us, &msg);
145         if (ret) {
146                 dev_err(&st->us->dev,
147                         "problem when reading 16 bit register 0x%02X",
148                         lower_reg_address);
149                 goto error_ret;
150         }
151         *val = (st->rx[0] << 8) | st->rx[1];
152
153 error_ret:
154         mutex_unlock(&st->buf_lock);
155         return ret;
156 }
157
158
159 static ssize_t adis16350_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 = adis16350_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
170         if (ret)
171                 return ret;
172
173         if (val & ADIS16350_ERROR_ACTIVE)
174                 adis16350_check_status(dev);
175         val = ((s16)(val << shift) >> shift);
176         return sprintf(buf, "%d\n", val);
177 }
178
179 static ssize_t adis16350_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 = adis16350_spi_read_reg_16(dev, this_attr->address, &val);
188         if (ret)
189                 return ret;
190
191         if (val & ADIS16350_ERROR_ACTIVE)
192                 adis16350_check_status(dev);
193
194         return sprintf(buf, "%u\n", val & 0x0FFF);
195 }
196
197 static ssize_t adis16350_read_14bit_signed(struct device *dev,
198                 struct device_attribute *attr,
199                 char *buf)
200 {
201         struct iio_dev *indio_dev = dev_get_drvdata(dev);
202         ssize_t ret;
203
204         /* Take the iio_dev status lock */
205         mutex_lock(&indio_dev->mlock);
206         ret =  adis16350_spi_read_signed(dev, attr, buf, 14);
207         mutex_unlock(&indio_dev->mlock);
208
209         return ret;
210 }
211
212 static ssize_t adis16350_read_12bit_signed(struct device *dev,
213                 struct device_attribute *attr,
214                 char *buf)
215 {
216         struct iio_dev *indio_dev = dev_get_drvdata(dev);
217         ssize_t ret;
218
219         /* Take the iio_dev status lock */
220         mutex_lock(&indio_dev->mlock);
221         ret =  adis16350_spi_read_signed(dev, attr, buf, 12);
222         mutex_unlock(&indio_dev->mlock);
223
224         return ret;
225 }
226
227 static ssize_t adis16350_write_16bit(struct device *dev,
228                 struct device_attribute *attr,
229                 const char *buf,
230                 size_t len)
231 {
232         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
233         int ret;
234         long val;
235
236         ret = strict_strtol(buf, 10, &val);
237         if (ret)
238                 goto error_ret;
239         ret = adis16350_spi_write_reg_16(dev, this_attr->address, val);
240
241 error_ret:
242         return ret ? ret : len;
243 }
244
245 static ssize_t adis16350_read_frequency(struct device *dev,
246                 struct device_attribute *attr,
247                 char *buf)
248 {
249         int ret, len = 0;
250         u16 t;
251         int sps;
252         ret = adis16350_spi_read_reg_16(dev,
253                         ADIS16350_SMPL_PRD,
254                         &t);
255         if (ret)
256                 return ret;
257         sps =  (t & ADIS16350_SMPL_PRD_TIME_BASE) ? 53 : 1638;
258         sps /= (t & ADIS16350_SMPL_PRD_DIV_MASK) + 1;
259         len = sprintf(buf, "%d SPS\n", sps);
260         return len;
261 }
262
263 static ssize_t adis16350_write_frequency(struct device *dev,
264                 struct device_attribute *attr,
265                 const char *buf,
266                 size_t len)
267 {
268         struct iio_dev *indio_dev = dev_get_drvdata(dev);
269         struct adis16350_state *st = iio_dev_get_devdata(indio_dev);
270         long val;
271         int ret;
272         u8 t;
273
274         ret = strict_strtol(buf, 10, &val);
275         if (ret)
276                 return ret;
277
278         mutex_lock(&indio_dev->mlock);
279
280         t = (1638 / val);
281         if (t > 0)
282                 t--;
283         t &= ADIS16350_SMPL_PRD_DIV_MASK;
284         if ((t & ADIS16350_SMPL_PRD_DIV_MASK) >= 0x0A)
285                 st->us->max_speed_hz = ADIS16350_SPI_SLOW;
286         else
287                 st->us->max_speed_hz = ADIS16350_SPI_FAST;
288
289         ret = adis16350_spi_write_reg_8(dev,
290                         ADIS16350_SMPL_PRD,
291                         t);
292
293         mutex_unlock(&indio_dev->mlock);
294
295         return ret ? ret : len;
296 }
297
298 static int adis16350_reset(struct device *dev)
299 {
300         int ret;
301         ret = adis16350_spi_write_reg_8(dev,
302                         ADIS16350_GLOB_CMD,
303                         ADIS16350_GLOB_CMD_SW_RESET);
304         if (ret)
305                 dev_err(dev, "problem resetting device");
306
307         return ret;
308 }
309
310 static ssize_t adis16350_write_reset(struct device *dev,
311                 struct device_attribute *attr,
312                 const char *buf, size_t len)
313 {
314         if (len < 1)
315                 return -1;
316         switch (buf[0]) {
317         case '1':
318         case 'y':
319         case 'Y':
320                 return adis16350_reset(dev);
321         }
322         return -1;
323 }
324
325 int adis16350_set_irq(struct device *dev, bool enable)
326 {
327         int ret;
328         u16 msc;
329         ret = adis16350_spi_read_reg_16(dev, ADIS16350_MSC_CTRL, &msc);
330         if (ret)
331                 goto error_ret;
332
333         msc |= ADIS16350_MSC_CTRL_DATA_RDY_POL_HIGH;
334         msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_DIO2;
335
336         if (enable)
337                 msc |= ADIS16350_MSC_CTRL_DATA_RDY_EN;
338         else
339                 msc &= ~ADIS16350_MSC_CTRL_DATA_RDY_EN;
340
341         ret = adis16350_spi_write_reg_16(dev, ADIS16350_MSC_CTRL, msc);
342         if (ret)
343                 goto error_ret;
344
345 error_ret:
346         return ret;
347 }
348
349 /* Power down the device */
350 static int adis16350_stop_device(struct device *dev)
351 {
352         int ret;
353         u16 val = ADIS16350_SLP_CNT_POWER_OFF;
354
355         ret = adis16350_spi_write_reg_16(dev, ADIS16350_SLP_CNT, val);
356         if (ret)
357                 dev_err(dev, "problem with turning device off: SLP_CNT");
358
359         return ret;
360 }
361
362 static int adis16350_self_test(struct device *dev)
363 {
364         int ret;
365         ret = adis16350_spi_write_reg_16(dev,
366                         ADIS16350_MSC_CTRL,
367                         ADIS16350_MSC_CTRL_MEM_TEST);
368         if (ret) {
369                 dev_err(dev, "problem starting self test");
370                 goto err_ret;
371         }
372
373         adis16350_check_status(dev);
374
375 err_ret:
376         return ret;
377 }
378
379 static int adis16350_check_status(struct device *dev)
380 {
381         u16 status;
382         int ret;
383
384         ret = adis16350_spi_read_reg_16(dev, ADIS16350_DIAG_STAT, &status);
385
386         if (ret < 0) {
387                 dev_err(dev, "Reading status failed\n");
388                 goto error_ret;
389         }
390         ret = status;
391         if (status & ADIS16350_DIAG_STAT_ZACCL_FAIL)
392                 dev_err(dev, "Z-axis accelerometer self-test failure\n");
393         if (status & ADIS16350_DIAG_STAT_YACCL_FAIL)
394                 dev_err(dev, "Y-axis accelerometer self-test failure\n");
395         if (status & ADIS16350_DIAG_STAT_XACCL_FAIL)
396                 dev_err(dev, "X-axis accelerometer self-test failure\n");
397         if (status & ADIS16350_DIAG_STAT_XGYRO_FAIL)
398                 dev_err(dev, "X-axis gyroscope self-test failure\n");
399         if (status & ADIS16350_DIAG_STAT_YGYRO_FAIL)
400                 dev_err(dev, "Y-axis gyroscope self-test failure\n");
401         if (status & ADIS16350_DIAG_STAT_ZGYRO_FAIL)
402                 dev_err(dev, "Z-axis gyroscope self-test failure\n");
403         if (status & ADIS16350_DIAG_STAT_ALARM2)
404                 dev_err(dev, "Alarm 2 active\n");
405         if (status & ADIS16350_DIAG_STAT_ALARM1)
406                 dev_err(dev, "Alarm 1 active\n");
407         if (status & ADIS16350_DIAG_STAT_FLASH_CHK)
408                 dev_err(dev, "Flash checksum error\n");
409         if (status & ADIS16350_DIAG_STAT_SELF_TEST)
410                 dev_err(dev, "Self test error\n");
411         if (status & ADIS16350_DIAG_STAT_OVERFLOW)
412                 dev_err(dev, "Sensor overrange\n");
413         if (status & ADIS16350_DIAG_STAT_SPI_FAIL)
414                 dev_err(dev, "SPI failure\n");
415         if (status & ADIS16350_DIAG_STAT_FLASH_UPT)
416                 dev_err(dev, "Flash update failed\n");
417         if (status & ADIS16350_DIAG_STAT_POWER_HIGH)
418                 dev_err(dev, "Power supply above 5.25V\n");
419         if (status & ADIS16350_DIAG_STAT_POWER_LOW)
420                 dev_err(dev, "Power supply below 4.75V\n");
421
422 error_ret:
423         return ret;
424 }
425
426 static int adis16350_initial_setup(struct adis16350_state *st)
427 {
428         int ret;
429         u16 smp_prd;
430         struct device *dev = &st->indio_dev->dev;
431
432         /* use low spi speed for init */
433         st->us->max_speed_hz = ADIS16350_SPI_SLOW;
434         st->us->mode = SPI_MODE_3;
435         spi_setup(st->us);
436
437         /* Disable IRQ */
438         ret = adis16350_set_irq(dev, false);
439         if (ret) {
440                 dev_err(dev, "disable irq failed");
441                 goto err_ret;
442         }
443
444         /* Do self test */
445         ret = adis16350_self_test(dev);
446         if (ret) {
447                 dev_err(dev, "self test failure");
448                 goto err_ret;
449         }
450
451         /* Read status register to check the result */
452         ret = adis16350_check_status(dev);
453         if (ret) {
454                 adis16350_reset(dev);
455                 dev_err(dev, "device not playing ball -> reset");
456                 msleep(ADIS16350_STARTUP_DELAY);
457                 ret = adis16350_check_status(dev);
458                 if (ret) {
459                         dev_err(dev, "giving up");
460                         goto err_ret;
461                 }
462         }
463
464         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
465                         st->us->chip_select, st->us->irq);
466
467         /* use high spi speed if possible */
468         ret = adis16350_spi_read_reg_16(dev, ADIS16350_SMPL_PRD, &smp_prd);
469         if (!ret && (smp_prd & ADIS16350_SMPL_PRD_DIV_MASK) < 0x0A) {
470                 st->us->max_speed_hz = ADIS16350_SPI_SLOW;
471                 spi_setup(st->us);
472         }
473
474 err_ret:
475         return ret;
476 }
477
478 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
479                 adis16350_read_12bit_signed,
480                 adis16350_write_16bit,
481                 ADIS16350_XACCL_OFF);
482
483 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
484                 adis16350_read_12bit_signed,
485                 adis16350_write_16bit,
486                 ADIS16350_YACCL_OFF);
487
488 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
489                 adis16350_read_12bit_signed,
490                 adis16350_write_16bit,
491                 ADIS16350_ZACCL_OFF);
492
493 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16350_read_12bit_unsigned,
494                 ADIS16350_SUPPLY_OUT);
495 static IIO_CONST_ATTR(in_supply_scale, "0.002418");
496
497 static IIO_DEV_ATTR_GYRO_X(adis16350_read_14bit_signed,
498                 ADIS16350_XGYRO_OUT);
499 static IIO_DEV_ATTR_GYRO_Y(adis16350_read_14bit_signed,
500                 ADIS16350_YGYRO_OUT);
501 static IIO_DEV_ATTR_GYRO_Z(adis16350_read_14bit_signed,
502                 ADIS16350_ZGYRO_OUT);
503 static IIO_CONST_ATTR(gyro_scale, "0.05");
504
505 static IIO_DEV_ATTR_ACCEL_X(adis16350_read_14bit_signed,
506                 ADIS16350_XACCL_OUT);
507 static IIO_DEV_ATTR_ACCEL_Y(adis16350_read_14bit_signed,
508                 ADIS16350_YACCL_OUT);
509 static IIO_DEV_ATTR_ACCEL_Z(adis16350_read_14bit_signed,
510                 ADIS16350_ZACCL_OUT);
511 static IIO_CONST_ATTR(accel_scale, "0.00333");
512
513 static IIO_DEVICE_ATTR(temp_x_raw, S_IRUGO, adis16350_read_12bit_signed,
514                 NULL, ADIS16350_XTEMP_OUT);
515 static IIO_DEVICE_ATTR(temp_y_raw, S_IRUGO, adis16350_read_12bit_signed,
516                 NULL, ADIS16350_YTEMP_OUT);
517 static IIO_DEVICE_ATTR(temp_z_raw, S_IRUGO, adis16350_read_12bit_signed,
518                 NULL, ADIS16350_ZTEMP_OUT);
519 static IIO_CONST_ATTR(temp_scale, "0.0005");
520
521 static IIO_DEV_ATTR_IN_RAW(0, adis16350_read_12bit_unsigned,
522                 ADIS16350_AUX_ADC);
523 static IIO_CONST_ATTR(in0_scale, "0.000806");
524
525 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
526                 adis16350_read_frequency,
527                 adis16350_write_frequency);
528
529 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
530                 adis16350_write_reset, 0);
531
532 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
533
534 static IIO_CONST_ATTR(name, "adis16350");
535
536 static struct attribute *adis16350_attributes[] = {
537         &iio_dev_attr_accel_x_offset.dev_attr.attr,
538         &iio_dev_attr_accel_y_offset.dev_attr.attr,
539         &iio_dev_attr_accel_z_offset.dev_attr.attr,
540         &iio_dev_attr_in_supply_raw.dev_attr.attr,
541         &iio_const_attr_in_supply_scale.dev_attr.attr,
542         &iio_dev_attr_gyro_x_raw.dev_attr.attr,
543         &iio_dev_attr_gyro_y_raw.dev_attr.attr,
544         &iio_dev_attr_gyro_z_raw.dev_attr.attr,
545         &iio_const_attr_gyro_scale.dev_attr.attr,
546         &iio_dev_attr_accel_x_raw.dev_attr.attr,
547         &iio_dev_attr_accel_y_raw.dev_attr.attr,
548         &iio_dev_attr_accel_z_raw.dev_attr.attr,
549         &iio_const_attr_accel_scale.dev_attr.attr,
550         &iio_dev_attr_temp_x_raw.dev_attr.attr,
551         &iio_dev_attr_temp_y_raw.dev_attr.attr,
552         &iio_dev_attr_temp_z_raw.dev_attr.attr,
553         &iio_const_attr_temp_scale.dev_attr.attr,
554         &iio_dev_attr_in0_raw.dev_attr.attr,
555         &iio_const_attr_in0_scale.dev_attr.attr,
556         &iio_dev_attr_sampling_frequency.dev_attr.attr,
557         &iio_const_attr_available_sampling_frequency.dev_attr.attr,
558         &iio_dev_attr_reset.dev_attr.attr,
559         &iio_const_attr_name.dev_attr.attr,
560         NULL
561 };
562
563 static const struct attribute_group adis16350_attribute_group = {
564         .attrs = adis16350_attributes,
565 };
566
567 static struct attribute *adis16350_event_attributes[] = {
568         NULL,
569 };
570
571 static struct attribute_group adis16350_event_attribute_group = {
572         .attrs = adis16350_event_attributes,
573 };
574
575 static int __devinit adis16350_probe(struct spi_device *spi)
576 {
577         int ret, regdone = 0;
578         struct adis16350_state *st = kzalloc(sizeof *st, GFP_KERNEL);
579         if (!st) {
580                 ret =  -ENOMEM;
581                 goto error_ret;
582         }
583         /* this is only used for removal purposes */
584         spi_set_drvdata(spi, st);
585
586         /* Allocate the comms buffers */
587         st->rx = kzalloc(sizeof(*st->rx)*ADIS16350_MAX_RX, GFP_KERNEL);
588         if (st->rx == NULL) {
589                 ret = -ENOMEM;
590                 goto error_free_st;
591         }
592         st->tx = kzalloc(sizeof(*st->tx)*ADIS16350_MAX_TX, GFP_KERNEL);
593         if (st->tx == NULL) {
594                 ret = -ENOMEM;
595                 goto error_free_rx;
596         }
597         st->us = spi;
598         mutex_init(&st->buf_lock);
599         /* setup the industrialio driver allocated elements */
600         st->indio_dev = iio_allocate_device();
601         if (st->indio_dev == NULL) {
602                 ret = -ENOMEM;
603                 goto error_free_tx;
604         }
605
606         st->indio_dev->dev.parent = &spi->dev;
607         st->indio_dev->num_interrupt_lines = 1;
608         st->indio_dev->event_attrs = &adis16350_event_attribute_group;
609         st->indio_dev->attrs = &adis16350_attribute_group;
610         st->indio_dev->dev_data = (void *)(st);
611         st->indio_dev->driver_module = THIS_MODULE;
612         st->indio_dev->modes = INDIO_DIRECT_MODE;
613
614         ret = adis16350_configure_ring(st->indio_dev);
615         if (ret)
616                 goto error_free_dev;
617
618         ret = iio_device_register(st->indio_dev);
619         if (ret)
620                 goto error_unreg_ring_funcs;
621         regdone = 1;
622
623         ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
624         if (ret) {
625                 printk(KERN_ERR "failed to initialize the ring\n");
626                 goto error_unreg_ring_funcs;
627         }
628
629         if (spi->irq) {
630                 ret = iio_register_interrupt_line(spi->irq,
631                                 st->indio_dev,
632                                 0,
633                                 IRQF_TRIGGER_RISING,
634                                 "adis16350");
635                 if (ret)
636                         goto error_uninitialize_ring;
637
638                 ret = adis16350_probe_trigger(st->indio_dev);
639                 if (ret)
640                         goto error_unregister_line;
641         }
642
643         /* Get the device into a sane initial state */
644         ret = adis16350_initial_setup(st);
645         if (ret)
646                 goto error_remove_trigger;
647         return 0;
648
649 error_remove_trigger:
650         adis16350_remove_trigger(st->indio_dev);
651 error_unregister_line:
652         if (spi->irq)
653                 iio_unregister_interrupt_line(st->indio_dev, 0);
654 error_uninitialize_ring:
655         iio_ring_buffer_unregister(st->indio_dev->ring);
656 error_unreg_ring_funcs:
657         adis16350_unconfigure_ring(st->indio_dev);
658 error_free_dev:
659         if (regdone)
660                 iio_device_unregister(st->indio_dev);
661         else
662                 iio_free_device(st->indio_dev);
663 error_free_tx:
664         kfree(st->tx);
665 error_free_rx:
666         kfree(st->rx);
667 error_free_st:
668         kfree(st);
669 error_ret:
670         return ret;
671 }
672
673 static int adis16350_remove(struct spi_device *spi)
674 {
675         int ret;
676         struct adis16350_state *st = spi_get_drvdata(spi);
677         struct iio_dev *indio_dev = st->indio_dev;
678
679         ret = adis16350_stop_device(&(indio_dev->dev));
680         if (ret)
681                 goto err_ret;
682
683         flush_scheduled_work();
684
685         adis16350_remove_trigger(indio_dev);
686         if (spi->irq)
687                 iio_unregister_interrupt_line(indio_dev, 0);
688
689         iio_ring_buffer_unregister(indio_dev->ring);
690         iio_device_unregister(indio_dev);
691         adis16350_unconfigure_ring(indio_dev);
692         kfree(st->tx);
693         kfree(st->rx);
694         kfree(st);
695
696         return 0;
697
698 err_ret:
699         return ret;
700 }
701
702 static const struct spi_device_id adis16350_id[] = {
703         {"adis16350", 0},
704         {"adis16354", 0},
705         {"adis16355", 0},
706         {"adis16360", 0},
707         {"adis16362", 0},
708         {"adis16364", 0},
709         {"adis16365", 0},
710         {}
711 };
712
713 static struct spi_driver adis16350_driver = {
714         .driver = {
715                 .name = "adis16350",
716                 .owner = THIS_MODULE,
717         },
718         .probe = adis16350_probe,
719         .remove = __devexit_p(adis16350_remove),
720         .id_table = adis16350_id,
721 };
722
723 static __init int adis16350_init(void)
724 {
725         return spi_register_driver(&adis16350_driver);
726 }
727 module_init(adis16350_init);
728
729 static __exit void adis16350_exit(void)
730 {
731         spi_unregister_driver(&adis16350_driver);
732 }
733 module_exit(adis16350_exit);
734
735 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
736 MODULE_DESCRIPTION("Analog Devices ADIS16350/54/55/60/62/64/65 IMU SPI driver");
737 MODULE_LICENSE("GPL v2");