staging:iio:imu ADIS16400 and ADIS16405 driver
[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>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/gpio.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/spi/spi.h>
24
25#include <linux/sysfs.h>
26#include <linux/list.h>
27
28#include "../iio.h"
29#include "../sysfs.h"
30#include "../accel/accel.h"
31#include "../adc/adc.h"
32#include "../gyro/gyro.h"
33#include "../magnetometer/magnet.h"
34
35#include "adis16400.h"
36
37#define DRIVER_NAME "adis16400"
38
39/* At the moment the spi framework doesn't allow global setting of cs_change.
40 * It's in the likely to be added comment at the top of spi.h.
41 * This means that use cannot be made of spi_write etc.
42 */
43
44/**
45 * adis16400_spi_write_reg_8() - write single byte to a register
46 * @dev: device associated with child of actual device (iio_dev or iio_trig)
47 * @reg_address: the address of the register to be written
48 * @val: the value to write
49 **/
50int adis16400_spi_write_reg_8(struct device *dev,
51 u8 reg_address,
52 u8 val)
53{
54 int ret;
55 struct iio_dev *indio_dev = dev_get_drvdata(dev);
56 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
57
58 mutex_lock(&st->buf_lock);
59 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
60 st->tx[1] = val;
61
62 ret = spi_write(st->us, st->tx, 2);
63 mutex_unlock(&st->buf_lock);
64
65 return ret;
66}
67
68/**
69 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
70 * @dev: device associated with child of actual device (iio_dev or iio_trig)
71 * @reg_address: the address of the lower of the two registers. Second register
72 * is assumed to have address one greater.
73 * @val: value to be written
74 **/
75static int adis16400_spi_write_reg_16(struct device *dev,
76 u8 lower_reg_address,
77 u16 value)
78{
79 int ret;
80 struct spi_message msg;
81 struct iio_dev *indio_dev = dev_get_drvdata(dev);
82 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
83 struct spi_transfer xfers[] = {
84 {
85 .tx_buf = st->tx,
86 .bits_per_word = 8,
87 .len = 2,
88 .cs_change = 1,
89 }, {
90 .tx_buf = st->tx + 2,
91 .bits_per_word = 8,
92 .len = 2,
93 .cs_change = 1,
94 },
95 };
96
97 mutex_lock(&st->buf_lock);
98 st->tx[0] = ADIS16400_WRITE_REG(lower_reg_address);
99 st->tx[1] = value & 0xFF;
100 st->tx[2] = ADIS16400_WRITE_REG(lower_reg_address + 1);
101 st->tx[3] = (value >> 8) & 0xFF;
102
103 spi_message_init(&msg);
104 spi_message_add_tail(&xfers[0], &msg);
105 spi_message_add_tail(&xfers[1], &msg);
106 ret = spi_sync(st->us, &msg);
107 mutex_unlock(&st->buf_lock);
108
109 return ret;
110}
111
112/**
113 * adis16400_spi_read_reg_16() - read 2 bytes from a 16-bit register
114 * @dev: device associated with child of actual device (iio_dev or iio_trig)
115 * @reg_address: the address of the lower of the two registers. Second register
116 * is assumed to have address one greater.
117 * @val: somewhere to pass back the value read
118 **/
119static int adis16400_spi_read_reg_16(struct device *dev,
120 u8 lower_reg_address,
121 u16 *val)
122{
123 struct spi_message msg;
124 struct iio_dev *indio_dev = dev_get_drvdata(dev);
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,
137 .cs_change = 1,
138 },
139 };
140
141 mutex_lock(&st->buf_lock);
142 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
143 st->tx[1] = 0;
144 st->tx[2] = 0;
145 st->tx[3] = 0;
146
147 spi_message_init(&msg);
148 spi_message_add_tail(&xfers[0], &msg);
149 spi_message_add_tail(&xfers[1], &msg);
150 ret = spi_sync(st->us, &msg);
151 if (ret) {
152 dev_err(&st->us->dev,
153 "problem when reading 16 bit register 0x%02X",
154 lower_reg_address);
155 goto error_ret;
156 }
157 *val = (st->rx[0] << 8) | st->rx[1];
158
159error_ret:
160 mutex_unlock(&st->buf_lock);
161 return ret;
162}
163
164/**
165 * adis16400_spi_read_burst() - read all data registers
166 * @dev: device associated with child of actual device (iio_dev or iio_trig)
167 * @rx: somewhere to pass back the value read (min size is 24 bytes)
168 **/
169int adis16400_spi_read_burst(struct device *dev, u8 *rx)
170{
171 struct spi_message msg;
172 struct iio_dev *indio_dev = dev_get_drvdata(dev);
173 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
174 u32 old_speed_hz = st->us->max_speed_hz;
175 int ret;
176
177 struct spi_transfer xfers[] = {
178 {
179 .tx_buf = st->tx,
180 .bits_per_word = 8,
181 .len = 2,
182 .cs_change = 0,
183 }, {
184 .rx_buf = rx,
185 .bits_per_word = 8,
186 .len = 24,
187 .cs_change = 1,
188 },
189 };
190
191 mutex_lock(&st->buf_lock);
192 st->tx[0] = ADIS16400_READ_REG(ADIS16400_GLOB_CMD);
193 st->tx[1] = 0;
194
195 spi_message_init(&msg);
196 spi_message_add_tail(&xfers[0], &msg);
197 spi_message_add_tail(&xfers[1], &msg);
198
199 st->us->max_speed_hz = min(ADIS16400_SPI_BURST, old_speed_hz);
200 spi_setup(st->us);
201
202 ret = spi_sync(st->us, &msg);
203 if (ret)
204 dev_err(&st->us->dev, "problem when burst reading");
205
206 st->us->max_speed_hz = old_speed_hz;
207 spi_setup(st->us);
208 mutex_unlock(&st->buf_lock);
209 return ret;
210}
211
212/**
213 * adis16400_spi_read_sequence() - read a sequence of 16-bit registers
214 * @dev: device associated with child of actual device (iio_dev or iio_trig)
215 * @tx: register addresses in bytes 0,2,4,6... (min size is 2*num bytes)
216 * @rx: somewhere to pass back the value read (min size is 2*num bytes)
217 **/
218int adis16400_spi_read_sequence(struct device *dev,
219 u8 *tx, u8 *rx, int num)
220{
221 struct spi_message msg;
222 struct spi_transfer *xfers;
223 struct iio_dev *indio_dev = dev_get_drvdata(dev);
224 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
225 int ret, i;
226
227 xfers = kzalloc(num + 1, GFP_KERNEL);
228 if (xfers == NULL) {
229 dev_err(&st->us->dev, "memory alloc failed");
230 ret = -ENOMEM;
231 goto error_ret;
232 }
233
234 /* tx: |add1|addr2|addr3|...|addrN |zero|
235 * rx: |zero|res1 |res2 |...|resN-1|resN| */
236 spi_message_init(&msg);
237 for (i = 0; i < num + 1; i++) {
238 if (i > 0)
239 xfers[i].rx_buf = st->rx + 2*(i - 1);
240 if (i < num)
241 xfers[i].tx_buf = st->tx + 2*i;
242 xfers[i].bits_per_word = 8;
243 xfers[i].len = 2;
244 xfers[i].cs_change = 1;
245 spi_message_add_tail(&xfers[i], &msg);
246 }
247
248 mutex_lock(&st->buf_lock);
249
250 ret = spi_sync(st->us, &msg);
251 if (ret)
252 dev_err(&st->us->dev, "problem when reading sequence");
253
254 mutex_unlock(&st->buf_lock);
255 kfree(xfers);
256
257error_ret:
258 return ret;
259}
260
261static ssize_t adis16400_spi_read_signed(struct device *dev,
262 struct device_attribute *attr,
263 char *buf,
264 unsigned bits)
265{
266 int ret;
267 s16 val = 0;
268 unsigned shift = 16 - bits;
269 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
270
271 ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
272 if (ret)
273 return ret;
274
275 if (val & ADIS16400_ERROR_ACTIVE)
276 adis16400_check_status(dev);
277 val = ((s16)(val << shift) >> shift);
278 return sprintf(buf, "%d\n", val);
279}
280
281static ssize_t adis16400_read_12bit_unsigned(struct device *dev,
282 struct device_attribute *attr,
283 char *buf)
284{
285 int ret;
286 u16 val = 0;
287 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
288
289 ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val);
290 if (ret)
291 return ret;
292
293 if (val & ADIS16400_ERROR_ACTIVE)
294 adis16400_check_status(dev);
295
296 return sprintf(buf, "%u\n", val & 0x0FFF);
297}
298
299static ssize_t adis16400_read_14bit_signed(struct device *dev,
300 struct device_attribute *attr,
301 char *buf)
302{
303 struct iio_dev *indio_dev = dev_get_drvdata(dev);
304 ssize_t ret;
305
306 /* Take the iio_dev status lock */
307 mutex_lock(&indio_dev->mlock);
308 ret = adis16400_spi_read_signed(dev, attr, buf, 14);
309 mutex_unlock(&indio_dev->mlock);
310
311 return ret;
312}
313
314static ssize_t adis16400_read_12bit_signed(struct device *dev,
315 struct device_attribute *attr,
316 char *buf)
317{
318 struct iio_dev *indio_dev = dev_get_drvdata(dev);
319 ssize_t ret;
320
321 /* Take the iio_dev status lock */
322 mutex_lock(&indio_dev->mlock);
323 ret = adis16400_spi_read_signed(dev, attr, buf, 12);
324 mutex_unlock(&indio_dev->mlock);
325
326 return ret;
327}
328
329
330static ssize_t adis16400_write_16bit(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf,
333 size_t len)
334{
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
336 int ret;
337 long val;
338
339 ret = strict_strtol(buf, 10, &val);
340 if (ret)
341 goto error_ret;
342 ret = adis16400_spi_write_reg_16(dev, this_attr->address, val);
343
344error_ret:
345 return ret ? ret : len;
346}
347
348static ssize_t adis16400_read_frequency(struct device *dev,
349 struct device_attribute *attr,
350 char *buf)
351{
352 int ret, len = 0;
353 u16 t;
354 int sps;
355 ret = adis16400_spi_read_reg_16(dev,
356 ADIS16400_SMPL_PRD,
357 &t);
358 if (ret)
359 return ret;
360 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
361 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
362 len = sprintf(buf, "%d SPS\n", sps);
363 return len;
364}
365
366static ssize_t adis16400_write_frequency(struct device *dev,
367 struct device_attribute *attr,
368 const char *buf,
369 size_t len)
370{
371 struct iio_dev *indio_dev = dev_get_drvdata(dev);
372 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
373 long val;
374 int ret;
375 u8 t;
376
377 ret = strict_strtol(buf, 10, &val);
378 if (ret)
379 return ret;
380
381 mutex_lock(&indio_dev->mlock);
382
383 t = (1638 / val);
384 if (t > 0)
385 t--;
386 t &= ADIS16400_SMPL_PRD_DIV_MASK;
387 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
388 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
389 else
390 st->us->max_speed_hz = ADIS16400_SPI_FAST;
391
392 ret = adis16400_spi_write_reg_8(dev,
393 ADIS16400_SMPL_PRD,
394 t);
395
396 mutex_unlock(&indio_dev->mlock);
397
398 return ret ? ret : len;
399}
400
401static ssize_t adis16400_write_reset(struct device *dev,
402 struct device_attribute *attr,
403 const char *buf, size_t len)
404{
405 if (len < 1)
406 return -1;
407 switch (buf[0]) {
408 case '1':
409 case 'y':
410 case 'Y':
411 return adis16400_reset(dev);
412 }
413 return -1;
414}
415
416
417
418int adis16400_set_irq(struct device *dev, bool enable)
419{
420 int ret;
421 u16 msc;
422 ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc);
423 if (ret)
424 goto error_ret;
425
426 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
427 if (enable)
428 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
429 else
430 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
431
432 ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc);
433 if (ret)
434 goto error_ret;
435
436error_ret:
437 return ret;
438}
439
440int adis16400_reset(struct device *dev)
441{
442 int ret;
443 ret = adis16400_spi_write_reg_8(dev,
444 ADIS16400_GLOB_CMD,
445 ADIS16400_GLOB_CMD_SW_RESET);
446 if (ret)
447 dev_err(dev, "problem resetting device");
448
449 return ret;
450}
451
452/* Power down the device */
453int adis16400_stop_device(struct device *dev)
454{
455 int ret;
456 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
457
458 ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val);
459 if (ret)
460 dev_err(dev, "problem with turning device off: SLP_CNT");
461
462 return ret;
463}
464
465int adis16400_self_test(struct device *dev)
466{
467 int ret;
468 ret = adis16400_spi_write_reg_16(dev,
469 ADIS16400_MSC_CTRL,
470 ADIS16400_MSC_CTRL_MEM_TEST);
471 if (ret) {
472 dev_err(dev, "problem starting self test");
473 goto err_ret;
474 }
475
476 adis16400_check_status(dev);
477
478err_ret:
479 return ret;
480}
481
482int adis16400_check_status(struct device *dev)
483{
484 u16 status;
485 int ret;
486
487 ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status);
488
489 if (ret < 0) {
490 dev_err(dev, "Reading status failed\n");
491 goto error_ret;
492 }
493 ret = status;
494 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
495 dev_err(dev, "Z-axis accelerometer self-test failure\n");
496 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
497 dev_err(dev, "Y-axis accelerometer self-test failure\n");
498 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
499 dev_err(dev, "X-axis accelerometer self-test failure\n");
500 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
501 dev_err(dev, "X-axis gyroscope self-test failure\n");
502 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
503 dev_err(dev, "Y-axis gyroscope self-test failure\n");
504 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
505 dev_err(dev, "Z-axis gyroscope self-test failure\n");
506 if (status & ADIS16400_DIAG_STAT_ALARM2)
507 dev_err(dev, "Alarm 2 active\n");
508 if (status & ADIS16400_DIAG_STAT_ALARM1)
509 dev_err(dev, "Alarm 1 active\n");
510 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
511 dev_err(dev, "Flash checksum error\n");
512 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
513 dev_err(dev, "Self test error\n");
514 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
515 dev_err(dev, "Sensor overrange\n");
516 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
517 dev_err(dev, "SPI failure\n");
518 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
519 dev_err(dev, "Flash update failed\n");
520 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
521 dev_err(dev, "Power supply above 5.25V\n");
522 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
523 dev_err(dev, "Power supply below 4.75V\n");
524
525error_ret:
526 return ret;
527}
528
529static int adis16400_initial_setup(struct adis16400_state *st)
530{
531 int ret;
532 u16 prod_id, smp_prd;
533 struct device *dev = &st->indio_dev->dev;
534
535 /* use low spi speed for init */
536 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
537 st->us->mode = SPI_MODE_3;
538 spi_setup(st->us);
539
540 /* Disable IRQ */
541 ret = adis16400_set_irq(dev, false);
542 if (ret) {
543 dev_err(dev, "disable irq failed");
544 goto err_ret;
545 }
546
547 /* Do self test */
548
549 /* Read status register to check the result */
550 ret = adis16400_check_status(dev);
551 if (ret) {
552 adis16400_reset(dev);
553 dev_err(dev, "device not playing ball -> reset");
554 msleep(ADIS16400_STARTUP_DELAY);
555 ret = adis16400_check_status(dev);
556 if (ret) {
557 dev_err(dev, "giving up");
558 goto err_ret;
559 }
560 }
561
562 ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id);
563 if (ret)
564 goto err_ret;
565
566 if (prod_id != ADIS16400_PRODUCT_ID_DEFAULT)
567 dev_warn(dev, "unknown product id");
568
569 printk(KERN_INFO DRIVER_NAME ": prod_id 0x%04x at CS%d (irq %d)\n",
570 prod_id, st->us->chip_select, st->us->irq);
571
572 /* use high spi speed if possible */
573 ret = adis16400_spi_read_reg_16(dev, ADIS16400_SMPL_PRD, &smp_prd);
574 if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) {
575 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
576 spi_setup(st->us);
577 }
578
579
580err_ret:
581
582 return ret;
583}
584
585static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
586 adis16400_read_12bit_signed,
587 adis16400_write_16bit,
588 ADIS16400_XACCL_OFF);
589
590static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
591 adis16400_read_12bit_signed,
592 adis16400_write_16bit,
593 ADIS16400_YACCL_OFF);
594
595static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
596 adis16400_read_12bit_signed,
597 adis16400_write_16bit,
598 ADIS16400_ZACCL_OFF);
599
600static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16400_read_14bit_signed,
601 ADIS16400_SUPPLY_OUT);
602static IIO_CONST_ATTR(in_supply_scale, "0.002418");
603
604static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed,
605 ADIS16400_XGYRO_OUT);
606static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed,
607 ADIS16400_YGYRO_OUT);
608static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed,
609 ADIS16400_ZGYRO_OUT);
610static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s");
611
612static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed,
613 ADIS16400_XACCL_OUT);
614static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed,
615 ADIS16400_YACCL_OUT);
616static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed,
617 ADIS16400_ZACCL_OUT);
618static IIO_CONST_ATTR(accel_scale, "0.00333 g");
619
620static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed,
621 ADIS16400_XMAGN_OUT);
622static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed,
623 ADIS16400_YMAGN_OUT);
624static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed,
625 ADIS16400_ZMAGN_OUT);
626static IIO_CONST_ATTR(magn_scale, "0.0005 Gs");
627
628
629static IIO_DEV_ATTR_TEMP(adis16400_read_12bit_signed);
630static IIO_CONST_ATTR(temp_offset, "198.16 K");
631static IIO_CONST_ATTR(temp_scale, "0.14 K");
632
633static IIO_DEV_ATTR_IN_RAW(0, adis16400_read_12bit_unsigned,
634 ADIS16400_AUX_ADC);
635static IIO_CONST_ATTR(in0_scale, "0.000806");
636
637static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
638 adis16400_read_frequency,
639 adis16400_write_frequency);
640
641static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
642
643static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638");
644
645static IIO_CONST_ATTR(name, "adis16400");
646
647static struct attribute *adis16400_event_attributes[] = {
648 NULL
649};
650
651static struct attribute_group adis16400_event_attribute_group = {
652 .attrs = adis16400_event_attributes,
653};
654
655static struct attribute *adis16400_attributes[] = {
656 &iio_dev_attr_accel_x_offset.dev_attr.attr,
657 &iio_dev_attr_accel_y_offset.dev_attr.attr,
658 &iio_dev_attr_accel_z_offset.dev_attr.attr,
659 &iio_dev_attr_in_supply_raw.dev_attr.attr,
660 &iio_const_attr_in_supply_scale.dev_attr.attr,
661 &iio_dev_attr_gyro_x.dev_attr.attr,
662 &iio_dev_attr_gyro_y.dev_attr.attr,
663 &iio_dev_attr_gyro_z.dev_attr.attr,
664 &iio_const_attr_gyro_scale.dev_attr.attr,
665 &iio_dev_attr_accel_x_raw.dev_attr.attr,
666 &iio_dev_attr_accel_y_raw.dev_attr.attr,
667 &iio_dev_attr_accel_z_raw.dev_attr.attr,
668 &iio_const_attr_accel_scale.dev_attr.attr,
669 &iio_dev_attr_magn_x.dev_attr.attr,
670 &iio_dev_attr_magn_y.dev_attr.attr,
671 &iio_dev_attr_magn_z.dev_attr.attr,
672 &iio_const_attr_magn_scale.dev_attr.attr,
673 &iio_dev_attr_temp.dev_attr.attr,
674 &iio_const_attr_temp_offset.dev_attr.attr,
675 &iio_const_attr_temp_scale.dev_attr.attr,
676 &iio_dev_attr_in0_raw.dev_attr.attr,
677 &iio_const_attr_in0_scale.dev_attr.attr,
678 &iio_dev_attr_sampling_frequency.dev_attr.attr,
679 &iio_const_attr_available_sampling_frequency.dev_attr.attr,
680 &iio_dev_attr_reset.dev_attr.attr,
681 &iio_const_attr_name.dev_attr.attr,
682 NULL
683};
684
685static const struct attribute_group adis16400_attribute_group = {
686 .attrs = adis16400_attributes,
687};
688
689static int __devinit adis16400_probe(struct spi_device *spi)
690{
691 int ret, regdone = 0;
692 struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL);
693 if (!st) {
694 ret = -ENOMEM;
695 goto error_ret;
696 }
697 /* this is only used for removal purposes */
698 spi_set_drvdata(spi, st);
699
700 /* Allocate the comms buffers */
701 st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL);
702 if (st->rx == NULL) {
703 ret = -ENOMEM;
704 goto error_free_st;
705 }
706 st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL);
707 if (st->tx == NULL) {
708 ret = -ENOMEM;
709 goto error_free_rx;
710 }
711 st->us = spi;
712 mutex_init(&st->buf_lock);
713 /* setup the industrialio driver allocated elements */
714 st->indio_dev = iio_allocate_device();
715 if (st->indio_dev == NULL) {
716 ret = -ENOMEM;
717 goto error_free_tx;
718 }
719
720 st->indio_dev->dev.parent = &spi->dev;
721 st->indio_dev->num_interrupt_lines = 1;
722 st->indio_dev->event_attrs = &adis16400_event_attribute_group;
723 st->indio_dev->attrs = &adis16400_attribute_group;
724 st->indio_dev->dev_data = (void *)(st);
725 st->indio_dev->driver_module = THIS_MODULE;
726 st->indio_dev->modes = INDIO_DIRECT_MODE;
727
728 ret = adis16400_configure_ring(st->indio_dev);
729 if (ret)
730 goto error_free_dev;
731
732 ret = iio_device_register(st->indio_dev);
733 if (ret)
734 goto error_unreg_ring_funcs;
735 regdone = 1;
736
737 ret = adis16400_initialize_ring(st->indio_dev->ring);
738 if (ret) {
739 printk(KERN_ERR "failed to initialize the ring\n");
740 goto error_unreg_ring_funcs;
741 }
742
743 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
744#if 0 /* fixme: here we should support */
745 iio_init_work_cont(&st->work_cont_thresh,
746 NULL,
747 adis16400_thresh_handler_bh_no_check,
748 0,
749 0,
750 st);
751#endif
752 ret = iio_register_interrupt_line(spi->irq,
753 st->indio_dev,
754 0,
755 IRQF_TRIGGER_RISING,
756 "adis16400");
757 if (ret)
758 goto error_uninitialize_ring;
759
760 ret = adis16400_probe_trigger(st->indio_dev);
761 if (ret)
762 goto error_unregister_line;
763 }
764
765 /* Get the device into a sane initial state */
766 ret = adis16400_initial_setup(st);
767 if (ret)
768 goto error_remove_trigger;
769 return 0;
770
771error_remove_trigger:
772 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
773 adis16400_remove_trigger(st->indio_dev);
774error_unregister_line:
775 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
776 iio_unregister_interrupt_line(st->indio_dev, 0);
777error_uninitialize_ring:
778 adis16400_uninitialize_ring(st->indio_dev->ring);
779error_unreg_ring_funcs:
780 adis16400_unconfigure_ring(st->indio_dev);
781error_free_dev:
782 if (regdone)
783 iio_device_unregister(st->indio_dev);
784 else
785 iio_free_device(st->indio_dev);
786error_free_tx:
787 kfree(st->tx);
788error_free_rx:
789 kfree(st->rx);
790error_free_st:
791 kfree(st);
792error_ret:
793 return ret;
794}
795
796/* fixme, confirm ordering in this function */
797static int adis16400_remove(struct spi_device *spi)
798{
799 int ret;
800 struct adis16400_state *st = spi_get_drvdata(spi);
801 struct iio_dev *indio_dev = st->indio_dev;
802
803 ret = adis16400_stop_device(&(indio_dev->dev));
804 if (ret)
805 goto err_ret;
806
807 flush_scheduled_work();
808
809 adis16400_remove_trigger(indio_dev);
810 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0)
811 iio_unregister_interrupt_line(indio_dev, 0);
812
813 adis16400_uninitialize_ring(indio_dev->ring);
814 adis16400_unconfigure_ring(indio_dev);
815 iio_device_unregister(indio_dev);
816 kfree(st->tx);
817 kfree(st->rx);
818 kfree(st);
819
820 return 0;
821
822err_ret:
823 return ret;
824}
825
826static struct spi_driver adis16400_driver = {
827 .driver = {
828 .name = "adis16400",
829 .owner = THIS_MODULE,
830 },
831 .probe = adis16400_probe,
832 .remove = __devexit_p(adis16400_remove),
833};
834
835static __init int adis16400_init(void)
836{
837 return spi_register_driver(&adis16400_driver);
838}
839module_init(adis16400_init);
840
841static __exit void adis16400_exit(void)
842{
843 spi_unregister_driver(&adis16400_driver);
844}
845module_exit(adis16400_exit);
846
847MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
848MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
849MODULE_LICENSE("GPL v2");
This page took 0.067275 seconds and 5 git commands to generate.