staging:iio:imu:adis16400 move to irq based triggers and channel spec channel registr...
[deliverable/linux.git] / drivers / staging / iio / imu / adis16400_core.c
CommitLineData
a9d26f00
BS
1/*
2 * adis16400.c support Analog Devices ADIS16400/5
3 * 3d 2g Linear Accelerometers,
4 * 3d Gyroscopes,
5 * 3d Magnetometers via SPI
6 *
7 * Copyright (c) 2009 Manuel Stahl <manuel.stahl@iis.fraunhofer.de>
8 * Copyright (c) 2007 Jonathan Cameron <jic23@cam.ac.uk>
6a6ec623 9 * Copyright (c) 2011 Analog Devices Inc.
a9d26f00
BS
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/gpio.h>
20#include <linux/delay.h>
21#include <linux/mutex.h>
22#include <linux/device.h>
23#include <linux/kernel.h>
24#include <linux/spi/spi.h>
1cb6c1f5 25#include <linux/slab.h>
a9d26f00
BS
26#include <linux/sysfs.h>
27#include <linux/list.h>
28
29#include "../iio.h"
30#include "../sysfs.h"
2662051e 31#include "../ring_generic.h"
a9d26f00
BS
32#include "../accel/accel.h"
33#include "../adc/adc.h"
34#include "../gyro/gyro.h"
35#include "../magnetometer/magnet.h"
36
37#include "adis16400.h"
38
39#define DRIVER_NAME "adis16400"
40
e7854845 41static int adis16400_check_status(struct iio_dev *indio_dev);
3fd66da1 42
a9d26f00
BS
43/* At the moment the spi framework doesn't allow global setting of cs_change.
44 * It's in the likely to be added comment at the top of spi.h.
45 * This means that use cannot be made of spi_write etc.
46 */
47
48/**
49 * adis16400_spi_write_reg_8() - write single byte to a register
50 * @dev: device associated with child of actual device (iio_dev or iio_trig)
51 * @reg_address: the address of the register to be written
52 * @val: the value to write
53 **/
e7854845
JC
54static int adis16400_spi_write_reg_8(struct iio_dev *indio_dev,
55 u8 reg_address,
56 u8 val)
a9d26f00
BS
57{
58 int ret;
a9d26f00
BS
59 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
60
61 mutex_lock(&st->buf_lock);
62 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
63 st->tx[1] = val;
64
65 ret = spi_write(st->us, st->tx, 2);
66 mutex_unlock(&st->buf_lock);
67
68 return ret;
69}
70
71/**
72 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
73 * @dev: device associated with child of actual device (iio_dev or iio_trig)
74 * @reg_address: the address of the lower of the two registers. Second register
75 * is assumed to have address one greater.
76 * @val: value to be written
77 **/
e7854845 78static int adis16400_spi_write_reg_16(struct iio_dev *indio_dev,
a9d26f00
BS
79 u8 lower_reg_address,
80 u16 value)
81{
82 int ret;
83 struct spi_message msg;
a9d26f00
BS
84 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
85 struct spi_transfer xfers[] = {
86 {
87 .tx_buf = st->tx,
88 .bits_per_word = 8,
89 .len = 2,
90 .cs_change = 1,
91 }, {
92 .tx_buf = st->tx + 2,
93 .bits_per_word = 8,
94 .len = 2,
a9d26f00
BS
95 },
96 };
97
98 mutex_lock(&st->buf_lock);
99 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
100 st->tx[1] = value & 0xFF;
101 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
102 st->tx[3] = (value >> 8) & 0xFF;
103
104 spi_message_init(&msg);
105 spi_message_add_tail(&xfers[0], &msg);
106 spi_message_add_tail(&xfers[1], &msg);
107 ret = spi_sync(st->us, &msg);
108 mutex_unlock(&st->buf_lock);
109
110 return ret;
111}
112
113/**
114 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
e7854845 115 * @indio_dev: iio device
a9d26f00
BS
116 * @reg_address: the address of the lower of the two registers. Second register
117 * is assumed to have address one greater.
118 * @val: somewhere to pass back the value read
119 **/
e7854845 120static int adis16400_spi_read_reg_16(struct iio_dev *indio_dev,
a9d26f00
BS
121 u8 lower_reg_address,
122 u16 *val)
123{
124 struct spi_message msg;
a9d26f00
BS
125 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
126 int ret;
127 struct spi_transfer xfers[] = {
128 {
129 .tx_buf = st->tx,
130 .bits_per_word = 8,
131 .len = 2,
132 .cs_change = 1,
133 }, {
134 .rx_buf = st->rx,
135 .bits_per_word = 8,
136 .len = 2,
a9d26f00
BS
137 },
138 };
139
140 mutex_lock(&st->buf_lock);
141 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
142 st->tx[1] = 0;
143 st->tx[2] = 0;
144 st->tx[3] = 0;
145
146 spi_message_init(&msg);
147 spi_message_add_tail(&xfers[0], &msg);
148 spi_message_add_tail(&xfers[1], &msg);
149 ret = spi_sync(st->us, &msg);
150 if (ret) {
151 dev_err(&st->us->dev,
152 "problem when reading 16 bit register 0x%02X",
153 lower_reg_address);
154 goto error_ret;
155 }
156 *val = (st->rx[0] << 8) | st->rx[1];
157
158error_ret:
159 mutex_unlock(&st->buf_lock);
160 return ret;
161}
162
a9d26f00
BS
163static ssize_t adis16400_read_frequency(struct device *dev,
164 struct device_attribute *attr,
165 char *buf)
166{
e7854845 167 struct iio_dev *indio_dev = dev_get_drvdata(dev);
a9d26f00
BS
168 int ret, len = 0;
169 u16 t;
170 int sps;
e7854845 171 ret = adis16400_spi_read_reg_16(indio_dev,
a9d26f00
BS
172 ADIS16400_SMPL_PRD,
173 &t);
174 if (ret)
175 return ret;
176 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
177 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
178 len = sprintf(buf, "%d SPS\n", sps);
179 return len;
180}
181
182static ssize_t adis16400_write_frequency(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf,
185 size_t len)
186{
187 struct iio_dev *indio_dev = dev_get_drvdata(dev);
188 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
189 long val;
190 int ret;
191 u8 t;
192
193 ret = strict_strtol(buf, 10, &val);
194 if (ret)
195 return ret;
196
197 mutex_lock(&indio_dev->mlock);
198
199 t = (1638 / val);
200 if (t > 0)
201 t--;
202 t &= ADIS16400_SMPL_PRD_DIV_MASK;
203 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
204 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
205 else
206 st->us->max_speed_hz = ADIS16400_SPI_FAST;
207
e7854845 208 ret = adis16400_spi_write_reg_8(indio_dev,
a9d26f00
BS
209 ADIS16400_SMPL_PRD,
210 t);
211
212 mutex_unlock(&indio_dev->mlock);
213
214 return ret ? ret : len;
215}
216
e7854845 217static int adis16400_reset(struct iio_dev *indio_dev)
3fd66da1
BS
218{
219 int ret;
e7854845 220 ret = adis16400_spi_write_reg_8(indio_dev,
3fd66da1
BS
221 ADIS16400_GLOB_CMD,
222 ADIS16400_GLOB_CMD_SW_RESET);
223 if (ret)
e7854845 224 dev_err(&indio_dev->dev, "problem resetting device");
3fd66da1
BS
225
226 return ret;
227}
228
a9d26f00
BS
229static ssize_t adis16400_write_reset(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf, size_t len)
232{
e7854845
JC
233 struct iio_dev *indio_dev = dev_get_drvdata(dev);
234
a9d26f00
BS
235 if (len < 1)
236 return -1;
237 switch (buf[0]) {
238 case '1':
239 case 'y':
240 case 'Y':
e7854845 241 return adis16400_reset(indio_dev);
a9d26f00
BS
242 }
243 return -1;
244}
245
e7854845 246int adis16400_set_irq(struct iio_dev *indio_dev, bool enable)
a9d26f00
BS
247{
248 int ret;
249 u16 msc;
e7854845 250 ret = adis16400_spi_read_reg_16(indio_dev, ADIS16400_MSC_CTRL, &msc);
a9d26f00
BS
251 if (ret)
252 goto error_ret;
253
254 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
255 if (enable)
256 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
257 else
258 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
259
e7854845 260 ret = adis16400_spi_write_reg_16(indio_dev, ADIS16400_MSC_CTRL, msc);
a9d26f00
BS
261 if (ret)
262 goto error_ret;
263
264error_ret:
265 return ret;
266}
267
a9d26f00 268/* Power down the device */
e7854845 269static int adis16400_stop_device(struct iio_dev *indio_dev)
a9d26f00
BS
270{
271 int ret;
272 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
273
e7854845 274 ret = adis16400_spi_write_reg_16(indio_dev, ADIS16400_SLP_CNT, val);
a9d26f00 275 if (ret)
e7854845
JC
276 dev_err(&indio_dev->dev,
277 "problem with turning device off: SLP_CNT");
a9d26f00
BS
278
279 return ret;
280}
281
e7854845 282static int adis16400_self_test(struct iio_dev *indio_dev)
a9d26f00
BS
283{
284 int ret;
e7854845 285 ret = adis16400_spi_write_reg_16(indio_dev,
a9d26f00
BS
286 ADIS16400_MSC_CTRL,
287 ADIS16400_MSC_CTRL_MEM_TEST);
288 if (ret) {
e7854845 289 dev_err(&indio_dev->dev, "problem starting self test");
a9d26f00
BS
290 goto err_ret;
291 }
e7854845 292
c59c95ce 293 msleep(ADIS16400_MTEST_DELAY);
e7854845 294 adis16400_check_status(indio_dev);
a9d26f00
BS
295
296err_ret:
297 return ret;
298}
299
e7854845 300static int adis16400_check_status(struct iio_dev *indio_dev)
a9d26f00
BS
301{
302 u16 status;
303 int ret;
e7854845 304 struct device *dev = &indio_dev->dev;
a9d26f00 305
e7854845
JC
306 ret = adis16400_spi_read_reg_16(indio_dev,
307 ADIS16400_DIAG_STAT, &status);
a9d26f00
BS
308
309 if (ret < 0) {
310 dev_err(dev, "Reading status failed\n");
311 goto error_ret;
312 }
313 ret = status;
314 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
315 dev_err(dev, "Z-axis accelerometer self-test failure\n");
316 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
317 dev_err(dev, "Y-axis accelerometer self-test failure\n");
318 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
319 dev_err(dev, "X-axis accelerometer self-test failure\n");
320 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
321 dev_err(dev, "X-axis gyroscope self-test failure\n");
322 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
323 dev_err(dev, "Y-axis gyroscope self-test failure\n");
324 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
325 dev_err(dev, "Z-axis gyroscope self-test failure\n");
326 if (status & ADIS16400_DIAG_STAT_ALARM2)
327 dev_err(dev, "Alarm 2 active\n");
328 if (status & ADIS16400_DIAG_STAT_ALARM1)
329 dev_err(dev, "Alarm 1 active\n");
330 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
331 dev_err(dev, "Flash checksum error\n");
332 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
333 dev_err(dev, "Self test error\n");
334 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
335 dev_err(dev, "Sensor overrange\n");
336 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
337 dev_err(dev, "SPI failure\n");
338 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
339 dev_err(dev, "Flash update failed\n");
340 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
341 dev_err(dev, "Power supply above 5.25V\n");
342 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
343 dev_err(dev, "Power supply below 4.75V\n");
344
345error_ret:
346 return ret;
347}
348
349static int adis16400_initial_setup(struct adis16400_state *st)
350{
351 int ret;
352 u16 prod_id, smp_prd;
353 struct device *dev = &st->indio_dev->dev;
354
355 /* use low spi speed for init */
356 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
357 st->us->mode = SPI_MODE_3;
358 spi_setup(st->us);
359
360 /* Disable IRQ */
e7854845 361 ret = adis16400_set_irq(st->indio_dev, false);
a9d26f00
BS
362 if (ret) {
363 dev_err(dev, "disable irq failed");
364 goto err_ret;
365 }
366
367 /* Do self test */
e7854845 368 ret = adis16400_self_test(st->indio_dev);
3fd66da1
BS
369 if (ret) {
370 dev_err(dev, "self test failure");
371 goto err_ret;
372 }
a9d26f00
BS
373
374 /* Read status register to check the result */
e7854845 375 ret = adis16400_check_status(st->indio_dev);
a9d26f00 376 if (ret) {
e7854845 377 adis16400_reset(st->indio_dev);
a9d26f00
BS
378 dev_err(dev, "device not playing ball -> reset");
379 msleep(ADIS16400_STARTUP_DELAY);
e7854845 380 ret = adis16400_check_status(st->indio_dev);
a9d26f00
BS
381 if (ret) {
382 dev_err(dev, "giving up");
383 goto err_ret;
384 }
385 }
386
e7854845
JC
387 ret = adis16400_spi_read_reg_16(st->indio_dev,
388 ADIS16400_PRODUCT_ID, &prod_id);
a9d26f00
BS
389 if (ret)
390 goto err_ret;
391
4d1ea4a6 392 if ((prod_id & 0xF000) != ADIS16400_PRODUCT_ID_DEFAULT)
a9d26f00
BS
393 dev_warn(dev, "unknown product id");
394
6a6ec623
MH
395
396 dev_info(dev, ": prod_id 0x%04x at CS%d (irq %d)\n",
a9d26f00
BS
397 prod_id, st->us->chip_select, st->us->irq);
398
399 /* use high spi speed if possible */
e7854845
JC
400 ret = adis16400_spi_read_reg_16(st->indio_dev,
401 ADIS16400_SMPL_PRD, &smp_prd);
a9d26f00
BS
402 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
403 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
404 spi_setup(st->us);
405 }
406
407
408err_ret:
409
410 return ret;
411}
412
a9d26f00
BS
413static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
414 adis16400_read_frequency,
415 adis16400_write_frequency);
416
417static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
418
51a0a5b0 419static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
a9d26f00 420
51a0a5b0 421static IIO_CONST_ATTR_NAME("adis16400");
a9d26f00 422
e7854845
JC
423enum adis16400_chan {
424 in_supply,
425 gyro_x,
426 gyro_y,
427 gyro_z,
428 accel_x,
429 accel_y,
430 accel_z,
431 magn_x,
432 magn_y,
433 magn_z,
434 temp,
435 in1
436};
437
438static u8 adis16400_addresses[12][2] = {
439 [in_supply] = { ADIS16400_SUPPLY_OUT, 0 },
440 [gyro_x] = { ADIS16400_XGYRO_OUT, ADIS16400_XGYRO_OFF },
441 [gyro_y] = { ADIS16400_YGYRO_OUT, ADIS16400_YGYRO_OFF },
442 [gyro_z] = { ADIS16400_ZGYRO_OUT, ADIS16400_ZGYRO_OFF },
443 [accel_x] = { ADIS16400_XACCL_OUT, ADIS16400_XACCL_OFF },
444 [accel_y] = { ADIS16400_YACCL_OUT, ADIS16400_YACCL_OFF },
445 [accel_z] = { ADIS16400_ZACCL_OUT, ADIS16400_ZACCL_OFF },
446 [magn_x] = { ADIS16400_XMAGN_OUT, 0 },
447 [magn_y] = { ADIS16400_YMAGN_OUT, 0 },
448 [magn_z] = { ADIS16400_ZMAGN_OUT, 0 },
449 [temp] = { ADIS16400_TEMP_OUT, 0 },
450 [in1] = { ADIS16400_AUX_ADC , 0 },
451};
452
453static int adis16400_write_raw(struct iio_dev *indio_dev,
454 struct iio_chan_spec const *chan,
455 int val,
456 int val2,
457 long mask)
458{
459 int ret;
460 switch (mask) {
461 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
462 mutex_lock(&indio_dev->mlock);
463 ret = adis16400_spi_write_reg_16(indio_dev,
464 adis16400_addresses[chan->address][1],
465 val);
466 mutex_unlock(&indio_dev->mlock);
467 return ret;
468 default:
469 return -EINVAL;
470 }
471}
472
473static int adis16400_read_raw(struct iio_dev *indio_dev,
474 struct iio_chan_spec const *chan,
475 int *val,
476 int *val2,
477 long mask)
478{
479 int ret;
480 s16 val16;
481 int shift;
482
483 switch (mask) {
484 case 0:
485 mutex_lock(&indio_dev->mlock);
486 ret = adis16400_spi_read_reg_16(indio_dev,
487 adis16400_addresses[chan->address][0],
488 &val16);
489 if (ret) {
490 mutex_unlock(&indio_dev->mlock);
491 return ret;
492 }
493 val16 &= (1 << chan->scan_type.realbits) - 1;
494 if (chan->scan_type.sign == 's') {
495 shift = 16 - chan->scan_type.realbits;
496 val16 = (s16)(val16 << shift) >> shift;
497 }
498 *val = val16;
499 mutex_unlock(&indio_dev->mlock);
500 return IIO_VAL_INT;
501 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
502 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
503 switch (chan->type) {
504 case IIO_GYRO:
505 *val = 0;
506 *val2 = 873;
507 return IIO_VAL_INT_PLUS_MICRO;
508 case IIO_IN:
509 *val = 0;
510 if (chan->channel == 0)
511 *val2 = 2418;
512 else
513 *val2 = 806;
514 return IIO_VAL_INT_PLUS_MICRO;
515 case IIO_ACCEL:
516 *val = 0;
517 *val2 = 32656;
518 return IIO_VAL_INT_PLUS_MICRO;
519 case IIO_MAGN:
520 *val = 0;
521 *val2 = 500;
522 return IIO_VAL_INT_PLUS_MICRO;
523 case IIO_TEMP:
524 *val = 0;
525 *val2 = 140000;
526 return IIO_VAL_INT_PLUS_MICRO;
527 default:
528 return -EINVAL;
529 }
530 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
531 mutex_lock(&indio_dev->mlock);
532 ret = adis16400_spi_read_reg_16(indio_dev,
533 adis16400_addresses[chan->address][1],
534 &val16);
535 if (ret) {
536 mutex_unlock(&indio_dev->mlock);
537 return ret;
538 }
539 val16 = ((val16 & 0xFFF) << 4) >> 4;
540 *val = val16;
541 return IIO_VAL_INT;
542 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
543 /* currently only temperature */
544 *val = 198;
545 *val2 = 160000;
546 return IIO_VAL_INT_PLUS_MICRO;
547 default:
548 return -EINVAL;
549 }
550}
551
552static struct iio_chan_spec adis16400_channels[] = {
553 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
554 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
555 in_supply, ADIS16400_SCAN_SUPPLY,
556 IIO_ST('u', 14, 16, 0), 0),
557 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, IIO_MOD_X,
558 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
559 (1 << IIO_CHAN_INFO_SCALE_SHARED),
560 gyro_x, ADIS16400_SCAN_GYRO_X, IIO_ST('s', 14, 16, 0), 0),
561 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, IIO_MOD_Y,
562 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
563 (1 << IIO_CHAN_INFO_SCALE_SHARED),
564 gyro_y, ADIS16400_SCAN_GYRO_Y, IIO_ST('s', 14, 16, 0), 0),
565 IIO_CHAN(IIO_GYRO, 1, 0, 0, NULL, 0, IIO_MOD_Z,
566 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
567 (1 << IIO_CHAN_INFO_SCALE_SHARED),
568 gyro_z, ADIS16400_SCAN_GYRO_Z, IIO_ST('s', 14, 16, 0), 0),
569 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
570 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
571 (1 << IIO_CHAN_INFO_SCALE_SHARED),
572 accel_x, ADIS16400_SCAN_ACC_X, IIO_ST('s', 14, 16, 0), 0),
573 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
574 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
575 (1 << IIO_CHAN_INFO_SCALE_SHARED),
576 accel_y, ADIS16400_SCAN_ACC_Y, IIO_ST('s', 14, 16, 0), 0),
577 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z,
578 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
579 (1 << IIO_CHAN_INFO_SCALE_SHARED),
580 accel_z, ADIS16400_SCAN_ACC_Z, IIO_ST('s', 14, 16, 0), 0),
581 IIO_CHAN(IIO_MAGN, 1, 0, 0, NULL, 0, IIO_MOD_X,
582 (1 << IIO_CHAN_INFO_SCALE_SHARED),
583 magn_x, ADIS16400_SCAN_MAGN_X, IIO_ST('s', 14, 16, 0), 0),
584 IIO_CHAN(IIO_MAGN, 1, 0, 0, NULL, 0, IIO_MOD_Y,
585 (1 << IIO_CHAN_INFO_SCALE_SHARED),
586 magn_y, ADIS16400_SCAN_MAGN_Y, IIO_ST('s', 14, 16, 0), 0),
587 IIO_CHAN(IIO_MAGN, 1, 0, 0, NULL, 0, IIO_MOD_Z,
588 (1 << IIO_CHAN_INFO_SCALE_SHARED),
589 magn_z, ADIS16400_SCAN_MAGN_Z, IIO_ST('s', 14, 16, 0), 0),
590 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
591 (1 << IIO_CHAN_INFO_OFFSET_SEPARATE) |
592 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
593 temp, ADIS16400_SCAN_TEMP, IIO_ST('s', 12, 16, 0), 0),
594 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
595 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
596 in1, ADIS16400_SCAN_ADC_0, IIO_ST('s', 12, 16, 0), 0),
597 IIO_CHAN_SOFT_TIMESTAMP(12)
598};
599
a9d26f00 600static struct attribute *adis16400_attributes[] = {
a9d26f00 601 &iio_dev_attr_sampling_frequency.dev_attr.attr,
51a0a5b0 602 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
a9d26f00
BS
603 &iio_dev_attr_reset.dev_attr.attr,
604 &iio_const_attr_name.dev_attr.attr,
605 NULL
606};
607
608static const struct attribute_group adis16400_attribute_group = {
609 .attrs = adis16400_attributes,
610};
611
612static int __devinit adis16400_probe(struct spi_device *spi)
613{
614 int ret, regdone = 0;
615 struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL);
616 if (!st) {
617 ret = -ENOMEM;
618 goto error_ret;
619 }
620 /* this is only used for removal purposes */
621 spi_set_drvdata(spi, st);
622
623 /* Allocate the comms buffers */
624 st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL);
625 if (st->rx == NULL) {
626 ret = -ENOMEM;
627 goto error_free_st;
628 }
629 st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL);
630 if (st->tx == NULL) {
631 ret = -ENOMEM;
632 goto error_free_rx;
633 }
634 st->us = spi;
635 mutex_init(&st->buf_lock);
636 /* setup the industrialio driver allocated elements */
6f7c8ee5 637 st->indio_dev = iio_allocate_device(0);
a9d26f00
BS
638 if (st->indio_dev == NULL) {
639 ret = -ENOMEM;
640 goto error_free_tx;
641 }
642
643 st->indio_dev->dev.parent = &spi->dev;
a9d26f00 644 st->indio_dev->attrs = &adis16400_attribute_group;
e7854845
JC
645 st->indio_dev->channels = adis16400_channels;
646 st->indio_dev->num_channels = ARRAY_SIZE(adis16400_channels);
647 st->indio_dev->read_raw = &adis16400_read_raw;
648 st->indio_dev->write_raw = &adis16400_write_raw;
a9d26f00
BS
649 st->indio_dev->dev_data = (void *)(st);
650 st->indio_dev->driver_module = THIS_MODULE;
651 st->indio_dev->modes = INDIO_DIRECT_MODE;
652
653 ret = adis16400_configure_ring(st->indio_dev);
654 if (ret)
655 goto error_free_dev;
656
657 ret = iio_device_register(st->indio_dev);
658 if (ret)
659 goto error_unreg_ring_funcs;
660 regdone = 1;
661
2662051e 662 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
a9d26f00 663 if (ret) {
6a6ec623 664 dev_err(&spi->dev, "failed to initialize the ring\n");
a9d26f00
BS
665 goto error_unreg_ring_funcs;
666 }
667
668 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
a9d26f00
BS
669 ret = adis16400_probe_trigger(st->indio_dev);
670 if (ret)
b333a240 671 goto error_uninitialize_ring;
a9d26f00
BS
672 }
673
674 /* Get the device into a sane initial state */
675 ret = adis16400_initial_setup(st);
676 if (ret)
677 goto error_remove_trigger;
678 return 0;
679
680error_remove_trigger:
681 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
682 adis16400_remove_trigger(st->indio_dev);
a9d26f00 683error_uninitialize_ring:
2662051e 684 iio_ring_buffer_unregister(st->indio_dev->ring);
a9d26f00
BS
685error_unreg_ring_funcs:
686 adis16400_unconfigure_ring(st->indio_dev);
687error_free_dev:
688 if (regdone)
689 iio_device_unregister(st->indio_dev);
690 else
691 iio_free_device(st->indio_dev);
692error_free_tx:
693 kfree(st->tx);
694error_free_rx:
695 kfree(st->rx);
696error_free_st:
697 kfree(st);
698error_ret:
699 return ret;
700}
701
702/* fixme, confirm ordering in this function */
703static int adis16400_remove(struct spi_device *spi)
704{
705 int ret;
706 struct adis16400_state *st = spi_get_drvdata(spi);
707 struct iio_dev *indio_dev = st->indio_dev;
708
e7854845 709 ret = adis16400_stop_device(indio_dev);
a9d26f00
BS
710 if (ret)
711 goto err_ret;
712
a9d26f00 713 adis16400_remove_trigger(indio_dev);
2662051e 714 iio_ring_buffer_unregister(st->indio_dev->ring);
a9d26f00
BS
715 adis16400_unconfigure_ring(indio_dev);
716 iio_device_unregister(indio_dev);
717 kfree(st->tx);
718 kfree(st->rx);
719 kfree(st);
720
721 return 0;
722
723err_ret:
724 return ret;
725}
726
727static struct spi_driver adis16400_driver = {
728 .driver = {
729 .name = "adis16400",
730 .owner = THIS_MODULE,
731 },
732 .probe = adis16400_probe,
733 .remove = __devexit_p(adis16400_remove),
734};
735
736static __init int adis16400_init(void)
737{
738 return spi_register_driver(&adis16400_driver);
739}
740module_init(adis16400_init);
741
742static __exit void adis16400_exit(void)
743{
744 spi_unregister_driver(&adis16400_driver);
745}
746module_exit(adis16400_exit);
747
748MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
749MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
750MODULE_LICENSE("GPL v2");
This page took 0.166773 seconds and 5 git commands to generate.