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