staging:iio:dac Add IIO_CHAN_INFO_RAW entries to all drivers.
[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>
089a4198
BS
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>
1cb6c1f5 16#include <linux/slab.h>
089a4198
BS
17#include <linux/sysfs.h>
18#include <linux/list.h>
99c97852 19#include <linux/module.h>
089a4198
BS
20
21#include "../iio.h"
22#include "../sysfs.h"
af5046af 23#include "../buffer.h"
089a4198
BS
24
25#include "adis16260.h"
26
27#define DRIVER_NAME "adis16260"
28
29b7f43e 29static int adis16260_check_status(struct iio_dev *indio_dev);
089a4198
BS
30
31/**
32 * adis16260_spi_write_reg_8() - write single byte to a register
29b7f43e 33 * @indio_dev: iio_dev for the device
089a4198
BS
34 * @reg_address: the address of the register to be written
35 * @val: the value to write
36 **/
29b7f43e 37static int adis16260_spi_write_reg_8(struct iio_dev *indio_dev,
089a4198
BS
38 u8 reg_address,
39 u8 val)
40{
41 int ret;
d088ab83 42 struct adis16260_state *st = iio_priv(indio_dev);
089a4198
BS
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
29b7f43e 56 * @indio_dev: iio_dev for the device
089a4198
BS
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 **/
29b7f43e 61static int adis16260_spi_write_reg_16(struct iio_dev *indio_dev,
089a4198
BS
62 u8 lower_reg_address,
63 u16 value)
64{
65 int ret;
66 struct spi_message msg;
d088ab83 67 struct adis16260_state *st = iio_priv(indio_dev);
089a4198
BS
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,
089a4198
BS
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
29b7f43e 100 * @indio_dev: iio_dev for the device
089a4198
BS
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 **/
29b7f43e 105static int adis16260_spi_read_reg_16(struct iio_dev *indio_dev,
089a4198
BS
106 u8 lower_reg_address,
107 u16 *val)
108{
109 struct spi_message msg;
d088ab83 110 struct adis16260_state *st = iio_priv(indio_dev);
089a4198
BS
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,
089a4198
BS
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;
089a4198
BS
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
143error_ret:
144 mutex_unlock(&st->buf_lock);
145 return ret;
146}
147
03d1b7d3
JC
148static 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_get_drvdata(dev);
d088ab83 153 struct adis16260_state *st = iio_priv(indio_dev);
03d1b7d3
JC
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
089a4198
BS
160static ssize_t adis16260_read_frequency(struct device *dev,
161 struct device_attribute *attr,
162 char *buf)
163{
03d1b7d3 164 struct iio_dev *indio_dev = dev_get_drvdata(dev);
d088ab83 165 struct adis16260_state *st = iio_priv(indio_dev);
089a4198
BS
166 int ret, len = 0;
167 u16 t;
168 int sps;
29b7f43e 169 ret = adis16260_spi_read_reg_16(indio_dev,
089a4198
BS
170 ADIS16260_SMPL_PRD,
171 &t);
172 if (ret)
173 return ret;
03d1b7d3
JC
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;
089a4198
BS
179 sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
180 len = sprintf(buf, "%d SPS\n", sps);
181 return len;
182}
183
184static 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_get_drvdata(dev);
d088ab83 190 struct adis16260_state *st = iio_priv(indio_dev);
089a4198
BS
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);
03d1b7d3
JC
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 }
089a4198
BS
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;
29b7f43e 215 ret = adis16260_spi_write_reg_8(indio_dev,
089a4198
BS
216 ADIS16260_SMPL_PRD,
217 t);
218
219 mutex_unlock(&indio_dev->mlock);
220
221 return ret ? ret : len;
222}
223
29b7f43e 224static int adis16260_reset(struct iio_dev *indio_dev)
089a4198
BS
225{
226 int ret;
29b7f43e 227 ret = adis16260_spi_write_reg_8(indio_dev,
089a4198
BS
228 ADIS16260_GLOB_CMD,
229 ADIS16260_GLOB_CMD_SW_RESET);
230 if (ret)
29b7f43e 231 dev_err(&indio_dev->dev, "problem resetting device");
089a4198
BS
232
233 return ret;
234}
235
236static ssize_t adis16260_write_reset(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t len)
239{
29b7f43e 240 struct iio_dev *indio_dev = dev_get_drvdata(dev);
089a4198
BS
241 if (len < 1)
242 return -EINVAL;
243 switch (buf[0]) {
244 case '1':
245 case 'y':
246 case 'Y':
29b7f43e 247 return adis16260_reset(indio_dev);
089a4198
BS
248 }
249 return -EINVAL;
250}
251
29b7f43e 252int adis16260_set_irq(struct iio_dev *indio_dev, bool enable)
089a4198
BS
253{
254 int ret;
255 u16 msc;
29b7f43e 256 ret = adis16260_spi_read_reg_16(indio_dev, ADIS16260_MSC_CTRL, &msc);
089a4198
BS
257 if (ret)
258 goto error_ret;
259
260 msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
261 if (enable)
262 msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
263 else
264 msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
265
29b7f43e 266 ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_MSC_CTRL, msc);
089a4198
BS
267 if (ret)
268 goto error_ret;
269
270error_ret:
271 return ret;
272}
273
274/* Power down the device */
29b7f43e 275static int adis16260_stop_device(struct iio_dev *indio_dev)
089a4198
BS
276{
277 int ret;
278 u16 val = ADIS16260_SLP_CNT_POWER_OFF;
279
29b7f43e 280 ret = adis16260_spi_write_reg_16(indio_dev, ADIS16260_SLP_CNT, val);
089a4198 281 if (ret)
29b7f43e 282 dev_err(&indio_dev->dev, "problem with turning device off: SLP_CNT");
089a4198
BS
283
284 return ret;
285}
286
29b7f43e 287static int adis16260_self_test(struct iio_dev *indio_dev)
089a4198
BS
288{
289 int ret;
29b7f43e 290 ret = adis16260_spi_write_reg_16(indio_dev,
089a4198
BS
291 ADIS16260_MSC_CTRL,
292 ADIS16260_MSC_CTRL_MEM_TEST);
293 if (ret) {
29b7f43e 294 dev_err(&indio_dev->dev, "problem starting self test");
089a4198
BS
295 goto err_ret;
296 }
297
29b7f43e 298 adis16260_check_status(indio_dev);
089a4198
BS
299
300err_ret:
301 return ret;
302}
303
29b7f43e 304static int adis16260_check_status(struct iio_dev *indio_dev)
089a4198
BS
305{
306 u16 status;
307 int ret;
29b7f43e 308 struct device *dev = &indio_dev->dev;
089a4198 309
29b7f43e
JC
310 ret = adis16260_spi_read_reg_16(indio_dev,
311 ADIS16260_DIAG_STAT,
312 &status);
089a4198
BS
313
314 if (ret < 0) {
315 dev_err(dev, "Reading status failed\n");
316 goto error_ret;
317 }
318 ret = status & 0x7F;
319 if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
320 dev_err(dev, "Flash checksum error\n");
321 if (status & ADIS16260_DIAG_STAT_SELF_TEST)
322 dev_err(dev, "Self test error\n");
323 if (status & ADIS16260_DIAG_STAT_OVERFLOW)
324 dev_err(dev, "Sensor overrange\n");
325 if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
326 dev_err(dev, "SPI failure\n");
327 if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
328 dev_err(dev, "Flash update failed\n");
329 if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
330 dev_err(dev, "Power supply above 5.25V\n");
331 if (status & ADIS16260_DIAG_STAT_POWER_LOW)
332 dev_err(dev, "Power supply below 4.75V\n");
333
334error_ret:
335 return ret;
336}
337
29b7f43e 338static int adis16260_initial_setup(struct iio_dev *indio_dev)
089a4198
BS
339{
340 int ret;
29b7f43e 341 struct device *dev = &indio_dev->dev;
089a4198
BS
342
343 /* Disable IRQ */
29b7f43e 344 ret = adis16260_set_irq(indio_dev, false);
089a4198
BS
345 if (ret) {
346 dev_err(dev, "disable irq failed");
347 goto err_ret;
348 }
349
350 /* Do self test */
29b7f43e 351 ret = adis16260_self_test(indio_dev);
089a4198
BS
352 if (ret) {
353 dev_err(dev, "self test failure");
354 goto err_ret;
355 }
356
357 /* Read status register to check the result */
29b7f43e 358 ret = adis16260_check_status(indio_dev);
089a4198 359 if (ret) {
29b7f43e 360 adis16260_reset(indio_dev);
089a4198
BS
361 dev_err(dev, "device not playing ball -> reset");
362 msleep(ADIS16260_STARTUP_DELAY);
29b7f43e 363 ret = adis16260_check_status(indio_dev);
089a4198
BS
364 if (ret) {
365 dev_err(dev, "giving up");
366 goto err_ret;
367 }
368 }
369
089a4198
BS
370err_ret:
371 return ret;
372}
373
089a4198
BS
374static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
375 adis16260_read_frequency,
376 adis16260_write_frequency);
089a4198
BS
377
378static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0);
379
03d1b7d3
JC
380static IIO_DEVICE_ATTR(sampling_frequency_available,
381 S_IRUGO, adis16260_read_frequency_available, NULL, 0);
089a4198 382
29b7f43e
JC
383enum adis16260_channel {
384 gyro,
385 temp,
386 in_supply,
387 in_aux,
388 angle,
389};
390#define ADIS16260_GYRO_CHANNEL_SET(axis, mod) \
391 struct iio_chan_spec adis16260_channels_##axis[] = { \
bbdb9555
JC
392 { \
393 .type = IIO_ANGL_VEL, \
394 .modified = 1, \
395 .channel2 = mod, \
396 .info_mask = IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT | \
397 IIO_CHAN_INFO_CALIBSCALE_SEPARATE_BIT | \
398 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
399 .address = gyro, \
400 .scan_index = ADIS16260_SCAN_GYRO, \
401 .scan_type = { \
402 .sign = 's', \
403 .realbits = 14, \
404 .storagebits = 16, \
405 }, \
406 }, { \
407 .type = IIO_ANGL, \
408 .modified = 1, \
409 .channel2 = mod, \
410 .address = angle, \
411 .scan_index = ADIS16260_SCAN_ANGL, \
412 .scan_type = { \
413 .sign = 'u', \
414 .realbits = 14, \
415 .storagebits = 16, \
416 }, \
417 }, { \
418 .type = IIO_TEMP, \
419 .indexed = 1, \
420 .channel = 0, \
421 .info_mask = IIO_CHAN_INFO_OFFSET_SEPARATE_BIT | \
422 IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
423 .address = temp, \
424 .scan_index = ADIS16260_SCAN_TEMP, \
425 .scan_type = { \
426 .sign = 'u', \
427 .realbits = 12, \
428 .storagebits = 16, \
429 }, \
430 }, { \
431 .type = IIO_VOLTAGE, \
432 .indexed = 1, \
433 .channel = 0, \
434 .extend_name = "supply", \
435 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
436 .address = in_supply, \
437 .scan_index = ADIS16260_SCAN_SUPPLY, \
438 .scan_type = { \
439 .sign = 'u', \
440 .realbits = 12, \
441 .storagebits = 16, \
442 }, \
443 }, { \
444 .type = IIO_VOLTAGE, \
445 .indexed = 1, \
446 .channel = 1, \
447 .info_mask = IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
448 .address = in_aux, \
449 .scan_index = ADIS16260_SCAN_AUX_ADC, \
450 .scan_type = { \
451 .sign = 'u', \
452 .realbits = 12, \
453 .storagebits = 16, \
454 }, \
455 }, \
456 IIO_CHAN_SOFT_TIMESTAMP(5), \
29b7f43e 457 }
089a4198 458
29b7f43e
JC
459static const ADIS16260_GYRO_CHANNEL_SET(x, IIO_MOD_X);
460static const ADIS16260_GYRO_CHANNEL_SET(y, IIO_MOD_Y);
461static const ADIS16260_GYRO_CHANNEL_SET(z, IIO_MOD_Z);
462
463static const u8 adis16260_addresses[5][3] = {
464 [gyro] = { ADIS16260_GYRO_OUT,
465 ADIS16260_GYRO_OFF,
466 ADIS16260_GYRO_SCALE },
467 [angle] = { ADIS16260_ANGL_OUT },
468 [in_supply] = { ADIS16260_SUPPLY_OUT },
469 [in_aux] = { ADIS16260_AUX_ADC },
470 [temp] = { ADIS16260_TEMP_OUT },
471};
472static int adis16260_read_raw(struct iio_dev *indio_dev,
473 struct iio_chan_spec const *chan,
474 int *val, int *val2,
475 long mask)
476{
d088ab83 477 struct adis16260_state *st = iio_priv(indio_dev);
29b7f43e
JC
478 int ret;
479 int bits;
480 u8 addr;
481 s16 val16;
482
483 switch (mask) {
484 case 0:
485 mutex_lock(&indio_dev->mlock);
486 addr = adis16260_addresses[chan->address][0];
487 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
8f896155
DC
488 if (ret) {
489 mutex_unlock(&indio_dev->mlock);
29b7f43e 490 return ret;
8f896155 491 }
29b7f43e
JC
492
493 if (val16 & ADIS16260_ERROR_ACTIVE) {
494 ret = adis16260_check_status(indio_dev);
8f896155
DC
495 if (ret) {
496 mutex_unlock(&indio_dev->mlock);
29b7f43e 497 return ret;
8f896155 498 }
29b7f43e
JC
499 }
500 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
501 if (chan->scan_type.sign == 's')
502 val16 = (s16)(val16 <<
503 (16 - chan->scan_type.realbits)) >>
504 (16 - chan->scan_type.realbits);
505 *val = val16;
506 mutex_unlock(&indio_dev->mlock);
507 return IIO_VAL_INT;
c8a9f805 508 case IIO_CHAN_INFO_SCALE:
29b7f43e 509 switch (chan->type) {
41ea040c 510 case IIO_ANGL_VEL:
29b7f43e
JC
511 *val = 0;
512 if (spi_get_device_id(st->us)->driver_data)
513 *val2 = 320;
514 else
515 *val2 = 1278;
516 return IIO_VAL_INT_PLUS_MICRO;
6835cb6b 517 case IIO_VOLTAGE:
29b7f43e
JC
518 *val = 0;
519 if (chan->channel == 0)
520 *val2 = 18315;
521 else
522 *val2 = 610500;
523 return IIO_VAL_INT_PLUS_MICRO;
524 case IIO_TEMP:
525 *val = 0;
526 *val2 = 145300;
527 return IIO_VAL_INT_PLUS_MICRO;
528 default:
529 return -EINVAL;
530 }
531 break;
c8a9f805 532 case IIO_CHAN_INFO_OFFSET:
29b7f43e
JC
533 *val = 25;
534 return IIO_VAL_INT;
c8a9f805 535 case IIO_CHAN_INFO_CALIBBIAS:
29b7f43e 536 switch (chan->type) {
41ea040c 537 case IIO_ANGL_VEL:
29b7f43e
JC
538 bits = 12;
539 break;
540 default:
541 return -EINVAL;
542 };
543 mutex_lock(&indio_dev->mlock);
544 addr = adis16260_addresses[chan->address][1];
545 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
546 if (ret) {
547 mutex_unlock(&indio_dev->mlock);
548 return ret;
549 }
550 val16 &= (1 << bits) - 1;
551 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
552 *val = val16;
553 mutex_unlock(&indio_dev->mlock);
554 return IIO_VAL_INT;
c8a9f805 555 case IIO_CHAN_INFO_CALIBSCALE:
29b7f43e 556 switch (chan->type) {
41ea040c 557 case IIO_ANGL_VEL:
29b7f43e
JC
558 bits = 12;
559 break;
560 default:
561 return -EINVAL;
562 };
563 mutex_lock(&indio_dev->mlock);
564 addr = adis16260_addresses[chan->address][2];
565 ret = adis16260_spi_read_reg_16(indio_dev, addr, &val16);
566 if (ret) {
567 mutex_unlock(&indio_dev->mlock);
568 return ret;
569 }
570 *val = (1 << bits) - 1;
571 mutex_unlock(&indio_dev->mlock);
572 return IIO_VAL_INT;
573 }
574 return -EINVAL;
575}
576
577static int adis16260_write_raw(struct iio_dev *indio_dev,
578 struct iio_chan_spec const *chan,
579 int val,
580 int val2,
581 long mask)
582{
583 int bits = 12;
584 s16 val16;
585 u8 addr;
586 switch (mask) {
c8a9f805 587 case IIO_CHAN_INFO_CALIBBIAS:
29b7f43e
JC
588 val16 = val & ((1 << bits) - 1);
589 addr = adis16260_addresses[chan->address][1];
590 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
c8a9f805 591 case IIO_CHAN_INFO_CALIBSCALE:
29b7f43e
JC
592 val16 = val & ((1 << bits) - 1);
593 addr = adis16260_addresses[chan->address][2];
594 return adis16260_spi_write_reg_16(indio_dev, addr, val16);
595 }
596 return -EINVAL;
597}
598
599static struct attribute *adis16260_attributes[] = {
600 &iio_dev_attr_sampling_frequency.dev_attr.attr,
601 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
602 &iio_dev_attr_reset.dev_attr.attr,
603 NULL
604};
605
606static const struct attribute_group adis16260_attribute_group = {
607 .attrs = adis16260_attributes,
608};
089a4198 609
6fe8135f
JC
610static const struct iio_info adis16260_info = {
611 .attrs = &adis16260_attribute_group,
612 .read_raw = &adis16260_read_raw,
613 .write_raw = &adis16260_write_raw,
614 .driver_module = THIS_MODULE,
615};
616
089a4198
BS
617static int __devinit adis16260_probe(struct spi_device *spi)
618{
26d25ae3 619 int ret;
fe346048 620 struct adis16260_platform_data *pd = spi->dev.platform_data;
d088ab83
JC
621 struct adis16260_state *st;
622 struct iio_dev *indio_dev;
623
624 /* setup the industrialio driver allocated elements */
625 indio_dev = iio_allocate_device(sizeof(*st));
626 if (indio_dev == NULL) {
627 ret = -ENOMEM;
089a4198
BS
628 goto error_ret;
629 }
d088ab83 630 st = iio_priv(indio_dev);
fe346048
JC
631 if (pd)
632 st->negate = pd->negate;
089a4198
BS
633 /* this is only used for removal purposes */
634 spi_set_drvdata(spi, st);
635
089a4198
BS
636 st->us = spi;
637 mutex_init(&st->buf_lock);
089a4198 638
d088ab83
JC
639 indio_dev->name = spi_get_device_id(st->us)->name;
640 indio_dev->dev.parent = &spi->dev;
641 indio_dev->info = &adis16260_info;
642 indio_dev->num_channels
29b7f43e 643 = ARRAY_SIZE(adis16260_channels_x);
fe346048
JC
644 if (pd && pd->direction)
645 switch (pd->direction) {
646 case 'x':
d088ab83 647 indio_dev->channels = adis16260_channels_x;
fe346048
JC
648 break;
649 case 'y':
d088ab83 650 indio_dev->channels = adis16260_channels_y;
fe346048
JC
651 break;
652 case 'z':
d088ab83 653 indio_dev->channels = adis16260_channels_z;
fe346048
JC
654 break;
655 default:
29b7f43e 656 return -EINVAL;
fe346048
JC
657 }
658 else
d088ab83 659 indio_dev->channels = adis16260_channels_x;
7bcf302f 660 indio_dev->num_channels = ARRAY_SIZE(adis16260_channels_x);
d088ab83 661 indio_dev->modes = INDIO_DIRECT_MODE;
089a4198 662
d088ab83 663 ret = adis16260_configure_ring(indio_dev);
089a4198
BS
664 if (ret)
665 goto error_free_dev;
666
14555b14
JC
667 ret = iio_buffer_register(indio_dev,
668 indio_dev->channels,
669 ARRAY_SIZE(adis16260_channels_x));
089a4198
BS
670 if (ret) {
671 printk(KERN_ERR "failed to initialize the ring\n");
672 goto error_unreg_ring_funcs;
673 }
14555b14 674 if (indio_dev->buffer) {
bd94c6a8 675 /* Set default scan mode */
f79a9098
JC
676 iio_scan_mask_set(indio_dev, indio_dev->buffer,
677 ADIS16260_SCAN_SUPPLY);
678 iio_scan_mask_set(indio_dev, indio_dev->buffer,
679 ADIS16260_SCAN_GYRO);
680 iio_scan_mask_set(indio_dev, indio_dev->buffer,
681 ADIS16260_SCAN_AUX_ADC);
682 iio_scan_mask_set(indio_dev, indio_dev->buffer,
683 ADIS16260_SCAN_TEMP);
684 iio_scan_mask_set(indio_dev, indio_dev->buffer,
685 ADIS16260_SCAN_ANGL);
bd94c6a8 686 }
089a4198 687 if (spi->irq) {
d088ab83 688 ret = adis16260_probe_trigger(indio_dev);
089a4198 689 if (ret)
ab8d48d4 690 goto error_uninitialize_ring;
089a4198
BS
691 }
692
693 /* Get the device into a sane initial state */
d088ab83 694 ret = adis16260_initial_setup(indio_dev);
089a4198
BS
695 if (ret)
696 goto error_remove_trigger;
26d25ae3
JC
697 ret = iio_device_register(indio_dev);
698 if (ret)
699 goto error_remove_trigger;
700
089a4198
BS
701 return 0;
702
703error_remove_trigger:
d088ab83 704 adis16260_remove_trigger(indio_dev);
089a4198 705error_uninitialize_ring:
14555b14 706 iio_buffer_unregister(indio_dev);
089a4198 707error_unreg_ring_funcs:
d088ab83 708 adis16260_unconfigure_ring(indio_dev);
089a4198 709error_free_dev:
26d25ae3 710 iio_free_device(indio_dev);
089a4198
BS
711error_ret:
712 return ret;
713}
714
715static int adis16260_remove(struct spi_device *spi)
716{
717 int ret;
d088ab83 718 struct iio_dev *indio_dev = spi_get_drvdata(spi);
089a4198 719
d2fffd6c
JC
720 iio_device_unregister(indio_dev);
721
29b7f43e 722 ret = adis16260_stop_device(indio_dev);
089a4198
BS
723 if (ret)
724 goto err_ret;
725
726 flush_scheduled_work();
727
728 adis16260_remove_trigger(indio_dev);
14555b14 729 iio_buffer_unregister(indio_dev);
089a4198 730 adis16260_unconfigure_ring(indio_dev);
d2fffd6c 731 iio_free_device(indio_dev);
089a4198 732
089a4198
BS
733err_ret:
734 return ret;
735}
736
fe346048
JC
737/*
738 * These parts do not need to be differentiated until someone adds
739 * support for the on chip filtering.
740 */
a9672951
JC
741static const struct spi_device_id adis16260_id[] = {
742 {"adis16260", 0},
743 {"adis16265", 0},
fe346048
JC
744 {"adis16250", 0},
745 {"adis16255", 0},
03d1b7d3 746 {"adis16251", 1},
a9672951
JC
747 {}
748};
55e4390c 749MODULE_DEVICE_TABLE(spi, adis16260_id);
a9672951 750
089a4198
BS
751static struct spi_driver adis16260_driver = {
752 .driver = {
753 .name = "adis16260",
754 .owner = THIS_MODULE,
755 },
756 .probe = adis16260_probe,
757 .remove = __devexit_p(adis16260_remove),
a9672951 758 .id_table = adis16260_id,
089a4198 759};
ae6ae6fe 760module_spi_driver(adis16260_driver);
089a4198
BS
761
762MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
763MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
764MODULE_LICENSE("GPL v2");
This page took 0.275112 seconds and 5 git commands to generate.