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