]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/gyro/adis16251_core.c
staging:iio:gyro: add adis16251 support to adis16260 driver
[karo-tx-linux.git] / drivers / staging / iio / gyro / adis16251_core.c
1 /*
2  * ADIS16251 Programmable Digital Gyroscope 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 #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 "gyro.h"
24 #include "../adc/adc.h"
25
26 #include "adis16251.h"
27
28 #define DRIVER_NAME             "adis16251"
29
30 /* At the moment the spi framework doesn't allow global setting of cs_change.
31  * It's in the likely to be added comment at the top of spi.h.
32  * This means that use cannot be made of spi_write etc.
33  */
34
35 /**
36  * adis16251_spi_write_reg_8() - write single byte to a register
37  * @dev: device associated with child of actual device (iio_dev or iio_trig)
38  * @reg_address: the address of the register to be written
39  * @val: the value to write
40  **/
41 int adis16251_spi_write_reg_8(struct device *dev,
42                 u8 reg_address,
43                 u8 val)
44 {
45         int ret;
46         struct iio_dev *indio_dev = dev_get_drvdata(dev);
47         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
48
49         mutex_lock(&st->buf_lock);
50         st->tx[0] = ADIS16251_WRITE_REG(reg_address);
51         st->tx[1] = val;
52
53         ret = spi_write(st->us, st->tx, 2);
54         mutex_unlock(&st->buf_lock);
55
56         return ret;
57 }
58
59 /**
60  * adis16251_spi_write_reg_16() - write 2 bytes to a pair of registers
61  * @dev: device associated with child of actual device (iio_dev or iio_trig)
62  * @reg_address: the address of the lower of the two registers. Second register
63  *               is assumed to have address one greater.
64  * @val: value to be written
65  **/
66 static int adis16251_spi_write_reg_16(struct device *dev,
67                 u8 lower_reg_address,
68                 u16 value)
69 {
70         int ret;
71         struct spi_message msg;
72         struct iio_dev *indio_dev = dev_get_drvdata(dev);
73         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
74         struct spi_transfer xfers[] = {
75                 {
76                         .tx_buf = st->tx,
77                         .bits_per_word = 8,
78                         .len = 2,
79                         .cs_change = 1,
80                 }, {
81                         .tx_buf = st->tx + 2,
82                         .bits_per_word = 8,
83                         .len = 2,
84                         .cs_change = 1,
85                 },
86         };
87
88         mutex_lock(&st->buf_lock);
89         st->tx[0] = ADIS16251_WRITE_REG(lower_reg_address);
90         st->tx[1] = value & 0xFF;
91         st->tx[2] = ADIS16251_WRITE_REG(lower_reg_address + 1);
92         st->tx[3] = (value >> 8) & 0xFF;
93
94         spi_message_init(&msg);
95         spi_message_add_tail(&xfers[0], &msg);
96         spi_message_add_tail(&xfers[1], &msg);
97         ret = spi_sync(st->us, &msg);
98         mutex_unlock(&st->buf_lock);
99
100         return ret;
101 }
102
103 /**
104  * adis16251_spi_read_reg_16() - read 2 bytes from a 16-bit register
105  * @dev: device associated with child of actual device (iio_dev or iio_trig)
106  * @reg_address: the address of the lower of the two registers. Second register
107  *               is assumed to have address one greater.
108  * @val: somewhere to pass back the value read
109  **/
110 static int adis16251_spi_read_reg_16(struct device *dev,
111                 u8 lower_reg_address,
112                 u16 *val)
113 {
114         struct spi_message msg;
115         struct iio_dev *indio_dev = dev_get_drvdata(dev);
116         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
117         int ret;
118         struct spi_transfer xfers[] = {
119                 {
120                         .tx_buf = st->tx,
121                         .bits_per_word = 8,
122                         .len = 2,
123                         .cs_change = 1,
124                 }, {
125                         .rx_buf = st->rx,
126                         .bits_per_word = 8,
127                         .len = 2,
128                         .cs_change = 1,
129                 },
130         };
131
132         mutex_lock(&st->buf_lock);
133         st->tx[0] = ADIS16251_READ_REG(lower_reg_address);
134         st->tx[1] = 0;
135         st->tx[2] = 0;
136         st->tx[3] = 0;
137
138         spi_message_init(&msg);
139         spi_message_add_tail(&xfers[0], &msg);
140         spi_message_add_tail(&xfers[1], &msg);
141         ret = spi_sync(st->us, &msg);
142         if (ret) {
143                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
144                                 lower_reg_address);
145                 goto error_ret;
146         }
147         *val = (st->rx[0] << 8) | st->rx[1];
148
149 error_ret:
150         mutex_unlock(&st->buf_lock);
151         return ret;
152 }
153
154 /**
155  * adis16251_spi_read_burst() - read all data registers
156  * @dev: device associated with child of actual device (iio_dev or iio_trig)
157  * @rx: somewhere to pass back the value read (min size is 24 bytes)
158  **/
159 int adis16251_spi_read_burst(struct device *dev, u8 *rx)
160 {
161         struct spi_message msg;
162         struct iio_dev *indio_dev = dev_get_drvdata(dev);
163         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
164         u32 old_speed_hz = st->us->max_speed_hz;
165         int ret;
166
167         struct spi_transfer xfers[] = {
168                 {
169                         .tx_buf = st->tx,
170                         .bits_per_word = 8,
171                         .len = 2,
172                         .cs_change = 0,
173                 }, {
174                         .rx_buf = rx,
175                         .bits_per_word = 8,
176                         .len = 24,
177                         .cs_change = 1,
178                 },
179         };
180
181         mutex_lock(&st->buf_lock);
182         st->tx[0] = ADIS16251_READ_REG(ADIS16251_GLOB_CMD);
183         st->tx[1] = 0;
184
185         spi_message_init(&msg);
186         spi_message_add_tail(&xfers[0], &msg);
187         spi_message_add_tail(&xfers[1], &msg);
188
189         st->us->max_speed_hz = min(ADIS16251_SPI_BURST, old_speed_hz);
190         spi_setup(st->us);
191
192         ret = spi_sync(st->us, &msg);
193         if (ret)
194                 dev_err(&st->us->dev, "problem when burst reading");
195
196         st->us->max_speed_hz = old_speed_hz;
197         spi_setup(st->us);
198         mutex_unlock(&st->buf_lock);
199         return ret;
200 }
201
202 /**
203  * adis16251_spi_read_sequence() - read a sequence of 16-bit registers
204  * @dev: device associated with child of actual device (iio_dev or iio_trig)
205  * @tx: register addresses in bytes 0,2,4,6... (min size is 2*num bytes)
206  * @rx: somewhere to pass back the value read (min size is 2*num bytes)
207  **/
208 int adis16251_spi_read_sequence(struct device *dev,
209                 u8 *tx, u8 *rx, int num)
210 {
211         struct spi_message msg;
212         struct spi_transfer *xfers;
213         struct iio_dev *indio_dev = dev_get_drvdata(dev);
214         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
215         int ret, i;
216
217         xfers = kzalloc(num + 1, GFP_KERNEL);
218         if (xfers == NULL) {
219                 dev_err(&st->us->dev, "memory alloc failed");
220                 ret = -ENOMEM;
221                 goto error_ret;
222         }
223
224         /* tx: |add1|addr2|addr3|...|addrN |zero|
225          * rx: |zero|res1 |res2 |...|resN-1|resN| */
226         spi_message_init(&msg);
227         for (i = 0; i < num + 1; i++) {
228                 if (i > 0)
229                         xfers[i].rx_buf = st->rx + 2*(i - 1);
230                 if (i < num)
231                         xfers[i].tx_buf = st->tx + 2*i;
232                 xfers[i].bits_per_word = 8;
233                 xfers[i].len = 2;
234                 xfers[i].cs_change = 1;
235                 spi_message_add_tail(&xfers[i], &msg);
236         }
237
238         mutex_lock(&st->buf_lock);
239
240         ret = spi_sync(st->us, &msg);
241         if (ret)
242                 dev_err(&st->us->dev, "problem when reading sequence");
243
244         mutex_unlock(&st->buf_lock);
245         kfree(xfers);
246
247 error_ret:
248         return ret;
249 }
250
251 static ssize_t adis16251_spi_read_signed(struct device *dev,
252                 struct device_attribute *attr,
253                 char *buf,
254                 unsigned bits)
255 {
256         int ret;
257         s16 val = 0;
258         unsigned shift = 16 - bits;
259         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
260
261         ret = adis16251_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
262         if (ret)
263                 return ret;
264
265         if (val & ADIS16251_ERROR_ACTIVE)
266                 adis16251_check_status(dev);
267         val = ((s16)(val << shift) >> shift);
268         return sprintf(buf, "%d\n", val);
269 }
270
271 static ssize_t adis16251_read_12bit_unsigned(struct device *dev,
272                 struct device_attribute *attr,
273                 char *buf)
274 {
275         int ret;
276         u16 val = 0;
277         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
278
279         ret = adis16251_spi_read_reg_16(dev, this_attr->address, &val);
280         if (ret)
281                 return ret;
282
283         if (val & ADIS16251_ERROR_ACTIVE)
284                 adis16251_check_status(dev);
285
286         return sprintf(buf, "%u\n", val & 0x0FFF);
287 }
288
289 static ssize_t adis16251_read_14bit_signed(struct device *dev,
290                 struct device_attribute *attr,
291                 char *buf)
292 {
293         struct iio_dev *indio_dev = dev_get_drvdata(dev);
294         ssize_t ret;
295
296         /* Take the iio_dev status lock */
297         mutex_lock(&indio_dev->mlock);
298         ret =  adis16251_spi_read_signed(dev, attr, buf, 14);
299         mutex_unlock(&indio_dev->mlock);
300
301         return ret;
302 }
303
304 static ssize_t adis16251_read_12bit_signed(struct device *dev,
305                 struct device_attribute *attr,
306                 char *buf)
307 {
308         struct iio_dev *indio_dev = dev_get_drvdata(dev);
309         ssize_t ret;
310
311         /* Take the iio_dev status lock */
312         mutex_lock(&indio_dev->mlock);
313         ret =  adis16251_spi_read_signed(dev, attr, buf, 12);
314         mutex_unlock(&indio_dev->mlock);
315
316         return ret;
317 }
318
319 static ssize_t adis16251_write_16bit(struct device *dev,
320                 struct device_attribute *attr,
321                 const char *buf,
322                 size_t len)
323 {
324         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
325         int ret;
326         long val;
327
328         ret = strict_strtol(buf, 10, &val);
329         if (ret)
330                 goto error_ret;
331         ret = adis16251_spi_write_reg_16(dev, this_attr->address, val);
332
333 error_ret:
334         return ret ? ret : len;
335 }
336
337 static ssize_t adis16251_read_frequency(struct device *dev,
338                 struct device_attribute *attr,
339                 char *buf)
340 {
341         int ret, len = 0;
342         u16 t;
343         int sps;
344         ret = adis16251_spi_read_reg_16(dev,
345                         ADIS16251_SMPL_PRD,
346                         &t);
347         if (ret)
348                 return ret;
349         sps =  (t & ADIS16251_SMPL_PRD_TIME_BASE) ? 8 : 256;
350         sps /= (t & ADIS16251_SMPL_PRD_DIV_MASK) + 1;
351         len = sprintf(buf, "%d SPS\n", sps);
352         return len;
353 }
354
355 static ssize_t adis16251_write_frequency(struct device *dev,
356                 struct device_attribute *attr,
357                 const char *buf,
358                 size_t len)
359 {
360         struct iio_dev *indio_dev = dev_get_drvdata(dev);
361         struct adis16251_state *st = iio_dev_get_devdata(indio_dev);
362         long val;
363         int ret;
364         u8 t;
365
366         ret = strict_strtol(buf, 10, &val);
367         if (ret)
368                 return ret;
369
370         mutex_lock(&indio_dev->mlock);
371
372         t = (256 / val);
373         if (t > 0)
374                 t--;
375         t &= ADIS16251_SMPL_PRD_DIV_MASK;
376         if ((t & ADIS16251_SMPL_PRD_DIV_MASK) >= 0x0A)
377                 st->us->max_speed_hz = ADIS16251_SPI_SLOW;
378         else
379                 st->us->max_speed_hz = ADIS16251_SPI_FAST;
380
381         ret = adis16251_spi_write_reg_8(dev,
382                         ADIS16251_SMPL_PRD,
383                         t);
384
385         mutex_unlock(&indio_dev->mlock);
386
387         return ret ? ret : len;
388 }
389
390 static ssize_t adis16251_write_reset(struct device *dev,
391                 struct device_attribute *attr,
392                 const char *buf, size_t len)
393 {
394         if (len < 1)
395                 return -1;
396         switch (buf[0]) {
397         case '1':
398         case 'y':
399         case 'Y':
400                 return adis16251_reset(dev);
401         }
402         return -1;
403 }
404
405
406
407 int adis16251_set_irq(struct device *dev, bool enable)
408 {
409         int ret;
410         u16 msc;
411         ret = adis16251_spi_read_reg_16(dev, ADIS16251_MSC_CTRL, &msc);
412         if (ret)
413                 goto error_ret;
414
415         msc |= ADIS16251_MSC_CTRL_DATA_RDY_POL_HIGH;
416         if (enable)
417                 msc |= ADIS16251_MSC_CTRL_DATA_RDY_EN;
418         else
419                 msc &= ~ADIS16251_MSC_CTRL_DATA_RDY_EN;
420
421         ret = adis16251_spi_write_reg_16(dev, ADIS16251_MSC_CTRL, msc);
422         if (ret)
423                 goto error_ret;
424
425 error_ret:
426         return ret;
427 }
428
429 int adis16251_reset(struct device *dev)
430 {
431         int ret;
432         ret = adis16251_spi_write_reg_8(dev,
433                         ADIS16251_GLOB_CMD,
434                         ADIS16251_GLOB_CMD_SW_RESET);
435         if (ret)
436                 dev_err(dev, "problem resetting device");
437
438         return ret;
439 }
440
441 /* Power down the device */
442 int adis16251_stop_device(struct device *dev)
443 {
444         int ret;
445         u16 val = ADIS16251_SLP_CNT_POWER_OFF;
446
447         ret = adis16251_spi_write_reg_16(dev, ADIS16251_SLP_CNT, val);
448         if (ret)
449                 dev_err(dev, "problem with turning device off: SLP_CNT");
450
451         return ret;
452 }
453
454 static int adis16251_self_test(struct device *dev)
455 {
456         int ret;
457
458         ret = adis16251_spi_write_reg_16(dev,
459                         ADIS16251_MSC_CTRL,
460                         ADIS16251_MSC_CTRL_INT_SELF_TEST);
461         if (ret) {
462                 dev_err(dev, "problem starting self test");
463                 goto err_ret;
464         }
465
466         adis16251_check_status(dev);
467
468 err_ret:
469         return ret;
470 }
471
472 int adis16251_check_status(struct device *dev)
473 {
474         u16 status;
475         int ret;
476
477         ret = adis16251_spi_read_reg_16(dev, ADIS16251_DIAG_STAT, &status);
478
479         if (ret < 0) {
480                 dev_err(dev, "Reading status failed\n");
481                 goto error_ret;
482         }
483
484         if (!(status & ADIS16251_DIAG_STAT_ERR_MASK)) {
485                 ret = 0;
486                 goto error_ret;
487         }
488
489         ret = -EFAULT;
490
491         if (status & ADIS16251_DIAG_STAT_ALARM2)
492                 dev_err(dev, "Alarm 2 active\n");
493         if (status & ADIS16251_DIAG_STAT_ALARM1)
494                 dev_err(dev, "Alarm 1 active\n");
495         if (status & ADIS16251_DIAG_STAT_SELF_TEST)
496                 dev_err(dev, "Self test error\n");
497         if (status & ADIS16251_DIAG_STAT_OVERFLOW)
498                 dev_err(dev, "Sensor overrange\n");
499         if (status & ADIS16251_DIAG_STAT_SPI_FAIL)
500                 dev_err(dev, "SPI failure\n");
501         if (status & ADIS16251_DIAG_STAT_FLASH_UPT)
502                 dev_err(dev, "Flash update failed\n");
503         if (status & ADIS16251_DIAG_STAT_POWER_HIGH)
504                 dev_err(dev, "Power supply above 5.25V\n");
505         if (status & ADIS16251_DIAG_STAT_POWER_LOW)
506                 dev_err(dev, "Power supply below 4.75V\n");
507
508 error_ret:
509         return ret;
510 }
511
512 static int adis16251_initial_setup(struct adis16251_state *st)
513 {
514         int ret;
515         u16 smp_prd;
516         struct device *dev = &st->indio_dev->dev;
517
518         /* use low spi speed for init */
519         st->us->max_speed_hz = ADIS16251_SPI_SLOW;
520         st->us->mode = SPI_MODE_3;
521         spi_setup(st->us);
522
523         /* Disable IRQ */
524         ret = adis16251_set_irq(dev, false);
525         if (ret) {
526                 dev_err(dev, "disable irq failed");
527                 goto err_ret;
528         }
529
530         /* Do self test */
531
532         /* Read status register to check the result */
533         ret = adis16251_check_status(dev);
534         if (ret) {
535                 adis16251_reset(dev);
536                 dev_err(dev, "device not playing ball -> reset");
537                 msleep(ADIS16251_STARTUP_DELAY);
538                 ret = adis16251_check_status(dev);
539                 if (ret) {
540                         dev_err(dev, "giving up");
541                         goto err_ret;
542                 }
543         }
544
545         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
546                         st->us->chip_select, st->us->irq);
547
548         /* use high spi speed if possible */
549         ret = adis16251_spi_read_reg_16(dev, ADIS16251_SMPL_PRD, &smp_prd);
550         if (!ret && (smp_prd & ADIS16251_SMPL_PRD_DIV_MASK) < 0x0A) {
551                 st->us->max_speed_hz = ADIS16251_SPI_SLOW;
552                 spi_setup(st->us);
553         }
554
555 err_ret:
556         return ret;
557 }
558
559 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16251_read_12bit_signed,
560                 ADIS16251_SUPPLY_OUT);
561 static IIO_CONST_ATTR(in0_supply_scale, "0.0018315");
562
563 static IIO_DEV_ATTR_GYRO(adis16251_read_14bit_signed,
564                 ADIS16251_GYRO_OUT);
565 static IIO_DEV_ATTR_GYRO_SCALE(S_IWUSR | S_IRUGO,
566                 adis16251_read_12bit_signed,
567                 adis16251_write_16bit,
568                 ADIS16251_GYRO_SCALE);
569 static IIO_DEV_ATTR_GYRO_OFFSET(S_IWUSR | S_IRUGO,
570                 adis16251_read_12bit_signed,
571                 adis16251_write_16bit,
572                 ADIS16251_GYRO_OFF);
573
574 static IIO_DEV_ATTR_TEMP_RAW(adis16251_read_12bit_signed);
575 static IIO_CONST_ATTR(temp_offset, "25 K");
576 static IIO_CONST_ATTR(temp_scale, "0.1453 K");
577
578 static IIO_DEV_ATTR_IN_NAMED_RAW(1, aux, adis16251_read_12bit_unsigned,
579                 ADIS16251_AUX_ADC);
580 static IIO_CONST_ATTR(in1_aux_scale, "0.0006105");
581
582 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
583                 adis16251_read_frequency,
584                 adis16251_write_frequency);
585 static IIO_DEV_ATTR_ANGL(adis16251_read_14bit_signed,
586                 ADIS16251_ANGL_OUT);
587
588 static IIO_DEV_ATTR_RESET(adis16251_write_reset);
589
590 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("0.129 ~ 256");
591
592 static IIO_CONST_ATTR(name, "adis16251");
593
594 static struct attribute *adis16251_event_attributes[] = {
595         NULL
596 };
597
598 static struct attribute_group adis16251_event_attribute_group = {
599         .attrs = adis16251_event_attributes,
600 };
601
602 static struct attribute *adis16251_attributes[] = {
603         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
604         &iio_const_attr_in0_supply_scale.dev_attr.attr,
605         &iio_dev_attr_gyro_raw.dev_attr.attr,
606         &iio_dev_attr_gyro_scale.dev_attr.attr,
607         &iio_dev_attr_gyro_offset.dev_attr.attr,
608         &iio_dev_attr_angl_raw.dev_attr.attr,
609         &iio_dev_attr_temp_raw.dev_attr.attr,
610         &iio_const_attr_temp_offset.dev_attr.attr,
611         &iio_const_attr_temp_scale.dev_attr.attr,
612         &iio_dev_attr_in1_aux_raw.dev_attr.attr,
613         &iio_const_attr_in1_aux_scale.dev_attr.attr,
614         &iio_dev_attr_sampling_frequency.dev_attr.attr,
615         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
616         &iio_dev_attr_reset.dev_attr.attr,
617         &iio_const_attr_name.dev_attr.attr,
618         NULL
619 };
620
621 static const struct attribute_group adis16251_attribute_group = {
622         .attrs = adis16251_attributes,
623 };
624
625 static int __devinit adis16251_probe(struct spi_device *spi)
626 {
627         int ret, regdone = 0;
628         struct adis16251_state *st = kzalloc(sizeof *st, GFP_KERNEL);
629         if (!st) {
630                 ret =  -ENOMEM;
631                 goto error_ret;
632         }
633         /* this is only used for removal purposes */
634         spi_set_drvdata(spi, st);
635
636         /* Allocate the comms buffers */
637         st->rx = kzalloc(sizeof(*st->rx)*ADIS16251_MAX_RX, GFP_KERNEL);
638         if (st->rx == NULL) {
639                 ret = -ENOMEM;
640                 goto error_free_st;
641         }
642         st->tx = kzalloc(sizeof(*st->tx)*ADIS16251_MAX_TX, GFP_KERNEL);
643         if (st->tx == NULL) {
644                 ret = -ENOMEM;
645                 goto error_free_rx;
646         }
647         st->us = spi;
648         mutex_init(&st->buf_lock);
649         /* setup the industrialio driver allocated elements */
650         st->indio_dev = iio_allocate_device();
651         if (st->indio_dev == NULL) {
652                 ret = -ENOMEM;
653                 goto error_free_tx;
654         }
655
656         st->indio_dev->dev.parent = &spi->dev;
657         st->indio_dev->num_interrupt_lines = 1;
658         st->indio_dev->event_attrs = &adis16251_event_attribute_group;
659         st->indio_dev->attrs = &adis16251_attribute_group;
660         st->indio_dev->dev_data = (void *)(st);
661         st->indio_dev->driver_module = THIS_MODULE;
662         st->indio_dev->modes = INDIO_DIRECT_MODE;
663
664         ret = adis16251_configure_ring(st->indio_dev);
665         if (ret)
666                 goto error_free_dev;
667
668         ret = iio_device_register(st->indio_dev);
669         if (ret)
670                 goto error_unreg_ring_funcs;
671         regdone = 1;
672
673         ret = adis16251_initialize_ring(st->indio_dev->ring);
674         if (ret) {
675                 printk(KERN_ERR "failed to initialize the ring\n");
676                 goto error_unreg_ring_funcs;
677         }
678
679         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
680                 ret = iio_register_interrupt_line(spi->irq,
681                                 st->indio_dev,
682                                 0,
683                                 IRQF_TRIGGER_RISING,
684                                 "adis16251");
685                 if (ret)
686                         goto error_uninitialize_ring;
687
688                 ret = adis16251_probe_trigger(st->indio_dev);
689                 if (ret)
690                         goto error_unregister_line;
691         }
692
693         /* Get the device into a sane initial state */
694         ret = adis16251_initial_setup(st);
695         if (ret)
696                 goto error_remove_trigger;
697         return 0;
698
699 error_remove_trigger:
700         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
701                 adis16251_remove_trigger(st->indio_dev);
702 error_unregister_line:
703         if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
704                 iio_unregister_interrupt_line(st->indio_dev, 0);
705 error_uninitialize_ring:
706         adis16251_uninitialize_ring(st->indio_dev->ring);
707 error_unreg_ring_funcs:
708         adis16251_unconfigure_ring(st->indio_dev);
709 error_free_dev:
710         if (regdone)
711                 iio_device_unregister(st->indio_dev);
712         else
713                 iio_free_device(st->indio_dev);
714 error_free_tx:
715         kfree(st->tx);
716 error_free_rx:
717         kfree(st->rx);
718 error_free_st:
719         kfree(st);
720 error_ret:
721         return ret;
722 }
723
724 /* fixme, confirm ordering in this function */
725 static int adis16251_remove(struct spi_device *spi)
726 {
727         int ret;
728         struct adis16251_state *st = spi_get_drvdata(spi);
729         struct iio_dev *indio_dev = st->indio_dev;
730
731         ret = adis16251_stop_device(&(indio_dev->dev));
732         if (ret)
733                 goto err_ret;
734
735         flush_scheduled_work();
736
737         adis16251_remove_trigger(indio_dev);
738         if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
739                 iio_unregister_interrupt_line(indio_dev, 0);
740
741         adis16251_uninitialize_ring(indio_dev->ring);
742         adis16251_unconfigure_ring(indio_dev);
743         iio_device_unregister(indio_dev);
744         kfree(st->tx);
745         kfree(st->rx);
746         kfree(st);
747
748         return 0;
749
750 err_ret:
751         return ret;
752 }
753
754 static struct spi_driver adis16251_driver = {
755         .driver = {
756                 .name = "adis16251",
757                 .owner = THIS_MODULE,
758         },
759         .probe = adis16251_probe,
760         .remove = __devexit_p(adis16251_remove),
761 };
762
763 static __init int adis16251_init(void)
764 {
765         return spi_register_driver(&adis16251_driver);
766 }
767 module_init(adis16251_init);
768
769 static __exit void adis16251_exit(void)
770 {
771         spi_unregister_driver(&adis16251_driver);
772 }
773 module_exit(adis16251_exit);
774
775 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
776 MODULE_DESCRIPTION("Analog Devices ADIS16251 Digital Gyroscope Sensor SPI driver");
777 MODULE_LICENSE("GPL v2");