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