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