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