]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/iio/gyro/adis16260_core.c
Revert "drm/radeon: rework pll selection (v3)"
[karo-tx-linux.git] / drivers / staging / iio / gyro / adis16260_core.c
1 /*
2  * ADIS16260/ADIS16265 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/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/slab.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
23 #include <linux/iio/buffer.h>
24
25 #include "adis16260.h"
26
27 #define DRIVER_NAME             "adis16260"
28
29 static int adis16260_check_status(struct iio_dev *indio_dev);
30
31 /**
32  * adis16260_spi_write_reg_8() - write single byte to a register
33  * @indio_dev: iio_dev for the device
34  * @reg_address: the address of the register to be written
35  * @val: the value to write
36  **/
37 static int adis16260_spi_write_reg_8(struct iio_dev *indio_dev,
38                 u8 reg_address,
39                 u8 val)
40 {
41         int ret;
42         struct adis16260_state *st = iio_priv(indio_dev);
43
44         mutex_lock(&st->buf_lock);
45         st->tx[0] = ADIS16260_WRITE_REG(reg_address);
46         st->tx[1] = val;
47
48         ret = spi_write(st->us, st->tx, 2);
49         mutex_unlock(&st->buf_lock);
50
51         return ret;
52 }
53
54 /**
55  * adis16260_spi_write_reg_16() - write 2 bytes to a pair of registers
56  * @indio_dev: iio_dev for the device
57  * @reg_address: the address of the lower of the two registers. Second register
58  *               is assumed to have address one greater.
59  * @val: value to be written
60  **/
61 static int adis16260_spi_write_reg_16(struct iio_dev *indio_dev,
62                 u8 lower_reg_address,
63                 u16 value)
64 {
65         int ret;
66         struct spi_message msg;
67         struct adis16260_state *st = iio_priv(indio_dev);
68         struct spi_transfer xfers[] = {
69                 {
70                         .tx_buf = st->tx,
71                         .bits_per_word = 8,
72                         .len = 2,
73                         .cs_change = 1,
74                         .delay_usecs = 20,
75                 }, {
76                         .tx_buf = st->tx + 2,
77                         .bits_per_word = 8,
78                         .len = 2,
79                         .delay_usecs = 20,
80                 },
81         };
82
83         mutex_lock(&st->buf_lock);
84         st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address);
85         st->tx[1] = value & 0xFF;
86         st->tx[2] = ADIS16260_WRITE_REG(lower_reg_address + 1);
87         st->tx[3] = (value >> 8) & 0xFF;
88
89         spi_message_init(&msg);
90         spi_message_add_tail(&xfers[0], &msg);
91         spi_message_add_tail(&xfers[1], &msg);
92         ret = spi_sync(st->us, &msg);
93         mutex_unlock(&st->buf_lock);
94
95         return ret;
96 }
97
98 /**
99  * adis16260_spi_read_reg_16() - read 2 bytes from a 16-bit register
100  * @indio_dev: iio_dev for the device
101  * @reg_address: the address of the lower of the two registers. Second register
102  *               is assumed to have address one greater.
103  * @val: somewhere to pass back the value read
104  **/
105 static int adis16260_spi_read_reg_16(struct iio_dev *indio_dev,
106                 u8 lower_reg_address,
107                 u16 *val)
108 {
109         struct spi_message msg;
110         struct adis16260_state *st = iio_priv(indio_dev);
111         int ret;
112         struct spi_transfer xfers[] = {
113                 {
114                         .tx_buf = st->tx,
115                         .bits_per_word = 8,
116                         .len = 2,
117                         .cs_change = 1,
118                         .delay_usecs = 30,
119                 }, {
120                         .rx_buf = st->rx,
121                         .bits_per_word = 8,
122                         .len = 2,
123                         .delay_usecs = 30,
124                 },
125         };
126
127         mutex_lock(&st->buf_lock);
128         st->tx[0] = ADIS16260_READ_REG(lower_reg_address);
129         st->tx[1] = 0;
130
131         spi_message_init(&msg);
132         spi_message_add_tail(&xfers[0], &msg);
133         spi_message_add_tail(&xfers[1], &msg);
134         ret = spi_sync(st->us, &msg);
135         if (ret) {
136                 dev_err(&st->us->dev,
137                         "problem when reading 16 bit register 0x%02X",
138                         lower_reg_address);
139                 goto error_ret;
140         }
141         *val = (st->rx[0] << 8) | st->rx[1];
142
143 error_ret:
144         mutex_unlock(&st->buf_lock);
145         return ret;
146 }
147
148 static ssize_t adis16260_read_frequency_available(struct device *dev,
149                                                   struct device_attribute *attr,
150                                                   char *buf)
151 {
152         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
153         struct adis16260_state *st = iio_priv(indio_dev);
154         if (spi_get_device_id(st->us)->driver_data)
155                 return sprintf(buf, "%s\n", "0.129 ~ 256");
156         else
157                 return sprintf(buf, "%s\n", "256 2048");
158 }
159
160 static ssize_t adis16260_read_frequency(struct device *dev,
161                 struct device_attribute *attr,
162                 char *buf)
163 {
164         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
165         struct adis16260_state *st = iio_priv(indio_dev);
166         int ret, len = 0;
167         u16 t;
168         int sps;
169         ret = adis16260_spi_read_reg_16(indio_dev,
170                         ADIS16260_SMPL_PRD,
171                         &t);
172         if (ret)
173                 return ret;
174
175         if (spi_get_device_id(st->us)->driver_data) /* If an adis16251 */
176                 sps =  (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 8 : 256;
177         else
178                 sps =  (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
179         sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
180         len = sprintf(buf, "%d SPS\n", sps);
181         return len;
182 }
183
184 static ssize_t adis16260_write_frequency(struct device *dev,
185                 struct device_attribute *attr,
186                 const char *buf,
187                 size_t len)
188 {
189         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
190         struct adis16260_state *st = iio_priv(indio_dev);
191         long val;
192         int ret;
193         u8 t;
194
195         ret = strict_strtol(buf, 10, &val);
196         if (ret)
197                 return ret;
198
199         mutex_lock(&indio_dev->mlock);
200         if (spi_get_device_id(st->us)) {
201                 t = (256 / val);
202                 if (t > 0)
203                         t--;
204                 t &= ADIS16260_SMPL_PRD_DIV_MASK;
205         } else {
206                 t = (2048 / val);
207                 if (t > 0)
208                         t--;
209                 t &= ADIS16260_SMPL_PRD_DIV_MASK;
210         }
211         if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
212                 st->us->max_speed_hz = ADIS16260_SPI_SLOW;
213         else
214                 st->us->max_speed_hz = ADIS16260_SPI_FAST;
215         ret = adis16260_spi_write_reg_8(indio_dev,
216                         ADIS16260_SMPL_PRD,
217                         t);
218
219         mutex_unlock(&indio_dev->mlock);
220
221         return ret ? ret : len;
222 }
223
224 static int adis16260_reset(struct iio_dev *indio_dev)
225 {
226         int ret;
227         ret = adis16260_spi_write_reg_8(indio_dev,
228                         ADIS16260_GLOB_CMD,
229                         ADIS16260_GLOB_CMD_SW_RESET);
230         if (ret)
231                 dev_err(&indio_dev->dev, "problem resetting device");
232
233         return ret;
234 }
235
236 int adis16260_set_irq(struct iio_dev *indio_dev, bool enable)
237 {
238         int ret;
239         u16 msc;
240         ret = adis16260_spi_read_reg_16(indio_dev, ADIS16260_MSC_CTRL, &msc);
241         if (ret)
242                 goto error_ret;
243
244         msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
245         if (enable)
246                 msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
247         else
248                 msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
249
250         ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_MSC_CTRL, msc);
251         if (ret)
252                 goto error_ret;
253
254 error_ret:
255         return ret;
256 }
257
258 /* Power down the device */
259 static int adis16260_stop_device(struct iio_dev *indio_dev)
260 {
261         int ret;
262         u16 val = ADIS16260_SLP_CNT_POWER_OFF;
263
264         ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_SLP_CNT, val);
265         if (ret)
266                 dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
267
268         return ret;
269 }
270
271 static int adis16260_self_test(struct iio_dev *indio_dev)
272 {
273         int ret;
274         ret = adis16260_spi_write_reg_16(indio_dev,
275                         ADIS16260_MSC_CTRL,
276                         ADIS16260_MSC_CTRL_MEM_TEST);
277         if (ret) {
278                 dev_err(&indio_dev->dev, "problem starting self test");
279                 goto err_ret;
280         }
281
282         adis16260_check_status(indio_dev);
283
284 err_ret:
285         return ret;
286 }
287
288 static int adis16260_check_status(struct iio_dev *indio_dev)
289 {
290         u16 status;
291         int ret;
292         struct device *dev = &indio_dev->dev;
293
294         ret = adis16260_spi_read_reg_16(indio_dev,
295                                         ADIS16260_DIAG_STAT,
296                                         &status);
297
298         if (ret < 0) {
299                 dev_err(dev, "Reading status failed\n");
300                 goto error_ret;
301         }
302         ret = status & 0x7F;
303         if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
304                 dev_err(dev, "Flash checksum error\n");
305         if (status & ADIS16260_DIAG_STAT_SELF_TEST)
306                 dev_err(dev, "Self test error\n");
307         if (status & ADIS16260_DIAG_STAT_OVERFLOW)
308                 dev_err(dev, "Sensor overrange\n");
309         if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
310                 dev_err(dev, "SPI failure\n");
311         if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
312                 dev_err(dev, "Flash update failed\n");
313         if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
314                 dev_err(dev, "Power supply above 5.25V\n");
315         if (status & ADIS16260_DIAG_STAT_POWER_LOW)
316                 dev_err(dev, "Power supply below 4.75V\n");
317
318 error_ret:
319         return ret;
320 }
321
322 static int adis16260_initial_setup(struct iio_dev *indio_dev)
323 {
324         int ret;
325         struct device *dev = &indio_dev->dev;
326
327         /* Disable IRQ */
328         ret = adis16260_set_irq(indio_dev, false);
329         if (ret) {
330                 dev_err(dev, "disable irq failed");
331                 goto err_ret;
332         }
333
334         /* Do self test */
335         ret = adis16260_self_test(indio_dev);
336         if (ret) {
337                 dev_err(dev, "self test failure");
338                 goto err_ret;
339         }
340
341         /* Read status register to check the result */
342         ret = adis16260_check_status(indio_dev);
343         if (ret) {
344                 adis16260_reset(indio_dev);
345                 dev_err(dev, "device not playing ball -> reset");
346                 msleep(ADIS16260_STARTUP_DELAY);
347                 ret = adis16260_check_status(indio_dev);
348                 if (ret) {
349                         dev_err(dev, "giving up");
350                         goto err_ret;
351                 }
352         }
353
354 err_ret:
355         return ret;
356 }
357
358 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
359                 adis16260_read_frequency,
360                 adis16260_write_frequency);
361
362 static IIO_DEVICE_ATTR(sampling_frequency_available,
363                        S_IRUGO, adis16260_read_frequency_available, NULL, 0);
364
365 enum adis16260_channel {
366         gyro,
367         temp,
368         in_supply,
369         in_aux,
370         angle,
371 };
372 #define ADIS16260_GYRO_CHANNEL_SET(axis, mod)                           \
373         struct iio_chan_spec adis16260_channels_##axis[] = {            \
374                 {                                                       \
375                         .type = IIO_ANGL_VEL,                           \
376                         .modified = 1,                                  \
377                         .channel2 = mod,                                \
378                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |   \
379                         IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT |          \
380                         IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT |         \
381                         IIO_CHAN_INFO_SCALE_SEPARATE_BIT,               \
382                         .address = gyro,                                \
383                         .scan_index = ADIS16260_SCAN_GYRO,              \
384                         .scan_type = {                                  \
385                                 .sign = 's',                            \
386                                 .realbits = 14,                         \
387                                 .storagebits = 16,                      \
388                         },                                              \
389                 }, {                                                    \
390                         .type = IIO_ANGL,                               \
391                         .modified = 1,                                  \
392                         .channel2 = mod,                                \
393                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,    \
394                         .address = angle,                               \
395                         .scan_index = ADIS16260_SCAN_ANGL,              \
396                         .scan_type = {                                  \
397                                 .sign = 'u',                            \
398                                 .realbits = 14,                         \
399                                 .storagebits = 16,                      \
400                         },                                              \
401                 }, {                                                    \
402                         .type = IIO_TEMP,                               \
403                         .indexed = 1,                                   \
404                         .channel = 0,                                   \
405                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |   \
406                         IIO_CHAN_INFO_OFFSET_SEPARATE_BIT |             \
407                         IIO_CHAN_INFO_SCALE_SEPARATE_BIT,               \
408                         .address = temp,                                \
409                         .scan_index = ADIS16260_SCAN_TEMP,              \
410                         .scan_type = {                                  \
411                                 .sign = 'u',                            \
412                                 .realbits = 12,                         \
413                                 .storagebits = 16,                      \
414                         },                                              \
415                 }, {                                                    \
416                         .type = IIO_VOLTAGE,                            \
417                         .indexed = 1,                                   \
418                         .channel = 0,                                   \
419                         .extend_name = "supply",                        \
420                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |   \
421                         IIO_CHAN_INFO_SCALE_SEPARATE_BIT,               \
422                         .address = in_supply,                           \
423                         .scan_index = ADIS16260_SCAN_SUPPLY,            \
424                         .scan_type = {                                  \
425                                 .sign = 'u',                            \
426                                 .realbits = 12,                         \
427                                 .storagebits = 16,                      \
428                         },                                              \
429                 }, {                                                    \
430                         .type = IIO_VOLTAGE,                            \
431                         .indexed = 1,                                   \
432                         .channel = 1,                                   \
433                         .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |   \
434                         IIO_CHAN_INFO_SCALE_SEPARATE_BIT,               \
435                         .address = in_aux,                              \
436                         .scan_index = ADIS16260_SCAN_AUX_ADC,           \
437                         .scan_type = {                                  \
438                                 .sign = 'u',                            \
439                                 .realbits = 12,                         \
440                                 .storagebits = 16,                      \
441                         },                                              \
442                 },                                                      \
443                 IIO_CHAN_SOFT_TIMESTAMP(5),                             \
444         }
445
446 static const ADIS16260_GYRO_CHANNEL_SET(x, IIO_MOD_X);
447 static const ADIS16260_GYRO_CHANNEL_SET(y, IIO_MOD_Y);
448 static const ADIS16260_GYRO_CHANNEL_SET(z, IIO_MOD_Z);
449
450 static const u8 adis16260_addresses[5][3] = {
451         [gyro] = { ADIS16260_GYRO_OUT,
452                    ADIS16260_GYRO_OFF,
453                    ADIS16260_GYRO_SCALE },
454         [angle] = { ADIS16260_ANGL_OUT },
455         [in_supply] = { ADIS16260_SUPPLY_OUT },
456         [in_aux] = { ADIS16260_AUX_ADC },
457         [temp] = { ADIS16260_TEMP_OUT },
458 };
459 static int adis16260_read_raw(struct iio_dev *indio_dev,
460                               struct iio_chan_spec const *chan,
461                               int *val, int *val2,
462                               long mask)
463 {
464         struct adis16260_state *st = iio_priv(indio_dev);
465         int ret;
466         int bits;
467         u8 addr;
468         s16 val16;
469
470         switch (mask) {
471         case IIO_CHAN_INFO_RAW:
472                 mutex_lock(&indio_dev->mlock);
473                 addr = adis16260_addresses[chan->address][0];
474                 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
475                 if (ret) {
476                         mutex_unlock(&indio_dev->mlock);
477                         return ret;
478                 }
479
480                 if (val16 & ADIS16260_ERROR_ACTIVE) {
481                         ret = adis16260_check_status(indio_dev);
482                         if (ret) {
483                                 mutex_unlock(&indio_dev->mlock);
484                                 return ret;
485                         }
486                 }
487                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
488                 if (chan->scan_type.sign == 's')
489                         val16 = (s16)(val16 <<
490                                       (16 - chan->scan_type.realbits)) >>
491                                 (16 - chan->scan_type.realbits);
492                 *val = val16;
493                 mutex_unlock(&indio_dev->mlock);
494                 return IIO_VAL_INT;
495         case IIO_CHAN_INFO_SCALE:
496                 switch (chan->type) {
497                 case IIO_ANGL_VEL:
498                         *val = 0;
499                         if (spi_get_device_id(st->us)->driver_data)
500                                 *val2 = 320;
501                         else
502                                 *val2 = 1278;
503                         return IIO_VAL_INT_PLUS_MICRO;
504                 case IIO_VOLTAGE:
505                         *val = 0;
506                         if (chan->channel == 0)
507                                 *val2 = 18315;
508                         else
509                                 *val2 = 610500;
510                         return IIO_VAL_INT_PLUS_MICRO;
511                 case IIO_TEMP:
512                         *val = 0;
513                         *val2 = 145300;
514                         return IIO_VAL_INT_PLUS_MICRO;
515                 default:
516                         return -EINVAL;
517                 }
518                 break;
519         case IIO_CHAN_INFO_OFFSET:
520                 *val = 25;
521                 return IIO_VAL_INT;
522         case IIO_CHAN_INFO_CALIBBIAS:
523                 switch (chan->type) {
524                 case IIO_ANGL_VEL:
525                         bits = 12;
526                         break;
527                 default:
528                         return -EINVAL;
529                 };
530                 mutex_lock(&indio_dev->mlock);
531                 addr = adis16260_addresses[chan->address][1];
532                 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
533                 if (ret) {
534                         mutex_unlock(&indio_dev->mlock);
535                         return ret;
536                 }
537                 val16 &= (1 << bits) - 1;
538                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
539                 *val = val16;
540                 mutex_unlock(&indio_dev->mlock);
541                 return IIO_VAL_INT;
542         case IIO_CHAN_INFO_CALIBSCALE:
543                 switch (chan->type) {
544                 case IIO_ANGL_VEL:
545                         bits = 12;
546                         break;
547                 default:
548                         return -EINVAL;
549                 };
550                 mutex_lock(&indio_dev->mlock);
551                 addr = adis16260_addresses[chan->address][2];
552                 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
553                 if (ret) {
554                         mutex_unlock(&indio_dev->mlock);
555                         return ret;
556                 }
557                 *val = (1 << bits) - 1;
558                 mutex_unlock(&indio_dev->mlock);
559                 return IIO_VAL_INT;
560         }
561         return -EINVAL;
562 }
563
564 static int adis16260_write_raw(struct iio_dev *indio_dev,
565                                struct iio_chan_spec const *chan,
566                                int val,
567                                int val2,
568                                long mask)
569 {
570         int bits = 12;
571         s16 val16;
572         u8 addr;
573         switch (mask) {
574         case IIO_CHAN_INFO_CALIBBIAS:
575                 val16 = val & ((1 << bits) - 1);
576                 addr = adis16260_addresses[chan->address][1];
577                 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
578         case IIO_CHAN_INFO_CALIBSCALE:
579                 val16 = val & ((1 << bits) - 1);
580                 addr = adis16260_addresses[chan->address][2];
581                 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
582         }
583         return -EINVAL;
584 }
585
586 static struct attribute *adis16260_attributes[] = {
587         &iio_dev_attr_sampling_frequency.dev_attr.attr,
588         &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
589         NULL
590 };
591
592 static const struct attribute_group adis16260_attribute_group = {
593         .attrs = adis16260_attributes,
594 };
595
596 static const struct iio_info adis16260_info = {
597         .attrs = &adis16260_attribute_group,
598         .read_raw = &adis16260_read_raw,
599         .write_raw = &adis16260_write_raw,
600         .driver_module = THIS_MODULE,
601 };
602
603 static int __devinit adis16260_probe(struct spi_device *spi)
604 {
605         int ret;
606         struct adis16260_platform_data *pd = spi->dev.platform_data;
607         struct adis16260_state *st;
608         struct iio_dev *indio_dev;
609
610         /* setup the industrialio driver allocated elements */
611         indio_dev = iio_device_alloc(sizeof(*st));
612         if (indio_dev == NULL) {
613                 ret = -ENOMEM;
614                 goto error_ret;
615         }
616         st = iio_priv(indio_dev);
617         if (pd)
618                 st->negate = pd->negate;
619         /* this is only used for removal purposes */
620         spi_set_drvdata(spi, indio_dev);
621
622         st->us = spi;
623         mutex_init(&st->buf_lock);
624
625         indio_dev->name = spi_get_device_id(st->us)->name;
626         indio_dev->dev.parent = &spi->dev;
627         indio_dev->info = &adis16260_info;
628         indio_dev->num_channels
629                 = ARRAY_SIZE(adis16260_channels_x);
630         if (pd && pd->direction)
631                 switch (pd->direction) {
632                 case 'x':
633                         indio_dev->channels = adis16260_channels_x;
634                         break;
635                 case 'y':
636                         indio_dev->channels = adis16260_channels_y;
637                         break;
638                 case 'z':
639                         indio_dev->channels = adis16260_channels_z;
640                         break;
641                 default:
642                         return -EINVAL;
643                 }
644         else
645                 indio_dev->channels = adis16260_channels_x;
646         indio_dev->num_channels = ARRAY_SIZE(adis16260_channels_x);
647         indio_dev->modes = INDIO_DIRECT_MODE;
648
649         ret = adis16260_configure_ring(indio_dev);
650         if (ret)
651                 goto error_free_dev;
652
653         ret = iio_buffer_register(indio_dev,
654                                   indio_dev->channels,
655                                   ARRAY_SIZE(adis16260_channels_x));
656         if (ret) {
657                 printk(KERN_ERR "failed to initialize the ring\n");
658                 goto error_unreg_ring_funcs;
659         }
660         if (indio_dev->buffer) {
661                 /* Set default scan mode */
662                 iio_scan_mask_set(indio_dev, indio_dev->buffer,
663                                   ADIS16260_SCAN_SUPPLY);
664                 iio_scan_mask_set(indio_dev, indio_dev->buffer,
665                                   ADIS16260_SCAN_GYRO);
666                 iio_scan_mask_set(indio_dev, indio_dev->buffer,
667                                   ADIS16260_SCAN_AUX_ADC);
668                 iio_scan_mask_set(indio_dev, indio_dev->buffer,
669                                   ADIS16260_SCAN_TEMP);
670                 iio_scan_mask_set(indio_dev, indio_dev->buffer,
671                                   ADIS16260_SCAN_ANGL);
672         }
673         if (spi->irq) {
674                 ret = adis16260_probe_trigger(indio_dev);
675                 if (ret)
676                         goto error_uninitialize_ring;
677         }
678
679         /* Get the device into a sane initial state */
680         ret = adis16260_initial_setup(indio_dev);
681         if (ret)
682                 goto error_remove_trigger;
683         ret = iio_device_register(indio_dev);
684         if (ret)
685                 goto error_remove_trigger;
686
687         return 0;
688
689 error_remove_trigger:
690         adis16260_remove_trigger(indio_dev);
691 error_uninitialize_ring:
692         iio_buffer_unregister(indio_dev);
693 error_unreg_ring_funcs:
694         adis16260_unconfigure_ring(indio_dev);
695 error_free_dev:
696         iio_device_free(indio_dev);
697 error_ret:
698         return ret;
699 }
700
701 static int adis16260_remove(struct spi_device *spi)
702 {
703         int ret;
704         struct iio_dev *indio_dev = spi_get_drvdata(spi);
705
706         iio_device_unregister(indio_dev);
707
708         ret = adis16260_stop_device(indio_dev);
709         if (ret)
710                 goto err_ret;
711
712         adis16260_remove_trigger(indio_dev);
713         iio_buffer_unregister(indio_dev);
714         adis16260_unconfigure_ring(indio_dev);
715         iio_device_free(indio_dev);
716
717 err_ret:
718         return ret;
719 }
720
721 /*
722  * These parts do not need to be differentiated until someone adds
723  * support for the on chip filtering.
724  */
725 static const struct spi_device_id adis16260_id[] = {
726         {"adis16260", 0},
727         {"adis16265", 0},
728         {"adis16250", 0},
729         {"adis16255", 0},
730         {"adis16251", 1},
731         {}
732 };
733 MODULE_DEVICE_TABLE(spi, adis16260_id);
734
735 static struct spi_driver adis16260_driver = {
736         .driver = {
737                 .name = "adis16260",
738                 .owner = THIS_MODULE,
739         },
740         .probe = adis16260_probe,
741         .remove = __devexit_p(adis16260_remove),
742         .id_table = adis16260_id,
743 };
744 module_spi_driver(adis16260_driver);
745
746 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
747 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
748 MODULE_LICENSE("GPL v2");