staging:iio:adis16350 replace unnecessary event line registration.
[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
3fd66da1
BS
41static int adis16400_check_status(struct device *dev);
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 **/
b155498b 54static int adis16400_spi_write_reg_8(struct device *dev,
a9d26f00
BS
55 u8 reg_address,
56 u8 val)
57{
58 int ret;
59 struct iio_dev *indio_dev = dev_get_drvdata(dev);
60 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
61
62 mutex_lock(&st->buf_lock);
63 st->tx[0] = ADIS16400_WRITE_REG(reg_address);
64 st->tx[1] = val;
65
66 ret = spi_write(st->us, st->tx, 2);
67 mutex_unlock(&st->buf_lock);
68
69 return ret;
70}
71
72/**
73 * adis16400_spi_write_reg_16() - write 2 bytes to a pair of registers
74 * @dev: device associated with child of actual device (iio_dev or iio_trig)
75 * @reg_address: the address of the lower of the two registers. Second register
76 * is assumed to have address one greater.
77 * @val: value to be written
78 **/
79static int adis16400_spi_write_reg_16(struct device *dev,
80 u8 lower_reg_address,
81 u16 value)
82{
83 int ret;
84 struct spi_message msg;
85 struct iio_dev *indio_dev = dev_get_drvdata(dev);
86 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
87 struct spi_transfer xfers[] = {
88 {
89 .tx_buf = st->tx,
90 .bits_per_word = 8,
91 .len = 2,
92 .cs_change = 1,
93 }, {
94 .tx_buf = st->tx + 2,
95 .bits_per_word = 8,
96 .len = 2,
a9d26f00
BS
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 **/
122static 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,
a9d26f00
BS
140 },
141 };
142
143 mutex_lock(&st->buf_lock);
144 st->tx[0] = ADIS16400_READ_REG(lower_reg_address);
145 st->tx[1] = 0;
146 st->tx[2] = 0;
147 st->tx[3] = 0;
148
149 spi_message_init(&msg);
150 spi_message_add_tail(&xfers[0], &msg);
151 spi_message_add_tail(&xfers[1], &msg);
152 ret = spi_sync(st->us, &msg);
153 if (ret) {
154 dev_err(&st->us->dev,
155 "problem when reading 16 bit register 0x%02X",
156 lower_reg_address);
157 goto error_ret;
158 }
159 *val = (st->rx[0] << 8) | st->rx[1];
160
161error_ret:
162 mutex_unlock(&st->buf_lock);
163 return ret;
164}
165
a9d26f00
BS
166static ssize_t adis16400_spi_read_signed(struct device *dev,
167 struct device_attribute *attr,
168 char *buf,
169 unsigned bits)
170{
171 int ret;
172 s16 val = 0;
173 unsigned shift = 16 - bits;
174 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
175
176 ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
177 if (ret)
178 return ret;
179
180 if (val & ADIS16400_ERROR_ACTIVE)
181 adis16400_check_status(dev);
182 val = ((s16)(val << shift) >> shift);
183 return sprintf(buf, "%d\n", val);
184}
185
186static ssize_t adis16400_read_12bit_unsigned(struct device *dev,
187 struct device_attribute *attr,
188 char *buf)
189{
190 int ret;
191 u16 val = 0;
192 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
193
194 ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val);
195 if (ret)
196 return ret;
197
198 if (val & ADIS16400_ERROR_ACTIVE)
199 adis16400_check_status(dev);
200
201 return sprintf(buf, "%u\n", val & 0x0FFF);
202}
203
204static ssize_t adis16400_read_14bit_signed(struct device *dev,
205 struct device_attribute *attr,
206 char *buf)
207{
208 struct iio_dev *indio_dev = dev_get_drvdata(dev);
209 ssize_t ret;
210
211 /* Take the iio_dev status lock */
212 mutex_lock(&indio_dev->mlock);
213 ret = adis16400_spi_read_signed(dev, attr, buf, 14);
214 mutex_unlock(&indio_dev->mlock);
215
216 return ret;
217}
218
219static ssize_t adis16400_read_12bit_signed(struct device *dev,
220 struct device_attribute *attr,
221 char *buf)
222{
223 struct iio_dev *indio_dev = dev_get_drvdata(dev);
224 ssize_t ret;
225
226 /* Take the iio_dev status lock */
227 mutex_lock(&indio_dev->mlock);
228 ret = adis16400_spi_read_signed(dev, attr, buf, 12);
229 mutex_unlock(&indio_dev->mlock);
230
231 return ret;
232}
233
a9d26f00
BS
234static ssize_t adis16400_write_16bit(struct device *dev,
235 struct device_attribute *attr,
236 const char *buf,
237 size_t len)
238{
239 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
240 int ret;
241 long val;
242
243 ret = strict_strtol(buf, 10, &val);
244 if (ret)
245 goto error_ret;
246 ret = adis16400_spi_write_reg_16(dev, this_attr->address, val);
247
248error_ret:
249 return ret ? ret : len;
250}
251
252static ssize_t adis16400_read_frequency(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
255{
256 int ret, len = 0;
257 u16 t;
258 int sps;
259 ret = adis16400_spi_read_reg_16(dev,
260 ADIS16400_SMPL_PRD,
261 &t);
262 if (ret)
263 return ret;
264 sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638;
265 sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1;
266 len = sprintf(buf, "%d SPS\n", sps);
267 return len;
268}
269
270static ssize_t adis16400_write_frequency(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf,
273 size_t len)
274{
275 struct iio_dev *indio_dev = dev_get_drvdata(dev);
276 struct adis16400_state *st = iio_dev_get_devdata(indio_dev);
277 long val;
278 int ret;
279 u8 t;
280
281 ret = strict_strtol(buf, 10, &val);
282 if (ret)
283 return ret;
284
285 mutex_lock(&indio_dev->mlock);
286
287 t = (1638 / val);
288 if (t > 0)
289 t--;
290 t &= ADIS16400_SMPL_PRD_DIV_MASK;
291 if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A)
292 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
293 else
294 st->us->max_speed_hz = ADIS16400_SPI_FAST;
295
296 ret = adis16400_spi_write_reg_8(dev,
297 ADIS16400_SMPL_PRD,
298 t);
299
300 mutex_unlock(&indio_dev->mlock);
301
302 return ret ? ret : len;
303}
304
3fd66da1
BS
305static int adis16400_reset(struct device *dev)
306{
307 int ret;
308 ret = adis16400_spi_write_reg_8(dev,
309 ADIS16400_GLOB_CMD,
310 ADIS16400_GLOB_CMD_SW_RESET);
311 if (ret)
312 dev_err(dev, "problem resetting device");
313
314 return ret;
315}
316
a9d26f00
BS
317static ssize_t adis16400_write_reset(struct device *dev,
318 struct device_attribute *attr,
319 const char *buf, size_t len)
320{
321 if (len < 1)
322 return -1;
323 switch (buf[0]) {
324 case '1':
325 case 'y':
326 case 'Y':
327 return adis16400_reset(dev);
328 }
329 return -1;
330}
331
a9d26f00
BS
332int adis16400_set_irq(struct device *dev, bool enable)
333{
334 int ret;
335 u16 msc;
336 ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc);
337 if (ret)
338 goto error_ret;
339
340 msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH;
341 if (enable)
342 msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN;
343 else
344 msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN;
345
346 ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc);
347 if (ret)
348 goto error_ret;
349
350error_ret:
351 return ret;
352}
353
a9d26f00 354/* Power down the device */
b155498b 355static int adis16400_stop_device(struct device *dev)
a9d26f00
BS
356{
357 int ret;
358 u16 val = ADIS16400_SLP_CNT_POWER_OFF;
359
360 ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val);
361 if (ret)
362 dev_err(dev, "problem with turning device off: SLP_CNT");
363
364 return ret;
365}
366
b155498b 367static int adis16400_self_test(struct device *dev)
a9d26f00
BS
368{
369 int ret;
370 ret = adis16400_spi_write_reg_16(dev,
371 ADIS16400_MSC_CTRL,
372 ADIS16400_MSC_CTRL_MEM_TEST);
373 if (ret) {
374 dev_err(dev, "problem starting self test");
375 goto err_ret;
376 }
c59c95ce 377 msleep(ADIS16400_MTEST_DELAY);
a9d26f00
BS
378 adis16400_check_status(dev);
379
380err_ret:
381 return ret;
382}
383
3fd66da1 384static int adis16400_check_status(struct device *dev)
a9d26f00
BS
385{
386 u16 status;
387 int ret;
388
389 ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status);
390
391 if (ret < 0) {
392 dev_err(dev, "Reading status failed\n");
393 goto error_ret;
394 }
395 ret = status;
396 if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL)
397 dev_err(dev, "Z-axis accelerometer self-test failure\n");
398 if (status & ADIS16400_DIAG_STAT_YACCL_FAIL)
399 dev_err(dev, "Y-axis accelerometer self-test failure\n");
400 if (status & ADIS16400_DIAG_STAT_XACCL_FAIL)
401 dev_err(dev, "X-axis accelerometer self-test failure\n");
402 if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL)
403 dev_err(dev, "X-axis gyroscope self-test failure\n");
404 if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL)
405 dev_err(dev, "Y-axis gyroscope self-test failure\n");
406 if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL)
407 dev_err(dev, "Z-axis gyroscope self-test failure\n");
408 if (status & ADIS16400_DIAG_STAT_ALARM2)
409 dev_err(dev, "Alarm 2 active\n");
410 if (status & ADIS16400_DIAG_STAT_ALARM1)
411 dev_err(dev, "Alarm 1 active\n");
412 if (status & ADIS16400_DIAG_STAT_FLASH_CHK)
413 dev_err(dev, "Flash checksum error\n");
414 if (status & ADIS16400_DIAG_STAT_SELF_TEST)
415 dev_err(dev, "Self test error\n");
416 if (status & ADIS16400_DIAG_STAT_OVERFLOW)
417 dev_err(dev, "Sensor overrange\n");
418 if (status & ADIS16400_DIAG_STAT_SPI_FAIL)
419 dev_err(dev, "SPI failure\n");
420 if (status & ADIS16400_DIAG_STAT_FLASH_UPT)
421 dev_err(dev, "Flash update failed\n");
422 if (status & ADIS16400_DIAG_STAT_POWER_HIGH)
423 dev_err(dev, "Power supply above 5.25V\n");
424 if (status & ADIS16400_DIAG_STAT_POWER_LOW)
425 dev_err(dev, "Power supply below 4.75V\n");
426
427error_ret:
428 return ret;
429}
430
431static int adis16400_initial_setup(struct adis16400_state *st)
432{
433 int ret;
434 u16 prod_id, smp_prd;
435 struct device *dev = &st->indio_dev->dev;
436
437 /* use low spi speed for init */
438 st->us->max_speed_hz = ADIS16400_SPI_SLOW;
439 st->us->mode = SPI_MODE_3;
440 spi_setup(st->us);
441
442 /* Disable IRQ */
443 ret = adis16400_set_irq(dev, false);
444 if (ret) {
445 dev_err(dev, "disable irq failed");
446 goto err_ret;
447 }
448
449 /* Do self test */
3fd66da1
BS
450 ret = adis16400_self_test(dev);
451 if (ret) {
452 dev_err(dev, "self test failure");
453 goto err_ret;
454 }
a9d26f00
BS
455
456 /* Read status register to check the result */
457 ret = adis16400_check_status(dev);
458 if (ret) {
459 adis16400_reset(dev);
460 dev_err(dev, "device not playing ball -> reset");
461 msleep(ADIS16400_STARTUP_DELAY);
462 ret = adis16400_check_status(dev);
463 if (ret) {
464 dev_err(dev, "giving up");
465 goto err_ret;
466 }
467 }
468
469 ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id);
470 if (ret)
471 goto err_ret;
472
4d1ea4a6 473 if ((prod_id & 0xF000) != ADIS16400_PRODUCT_ID_DEFAULT)
a9d26f00
BS
474 dev_warn(dev, "unknown product id");
475
6a6ec623
MH
476
477 dev_info(dev, ": prod_id 0x%04x at CS%d (irq %d)\n",
a9d26f00
BS
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
488err_ret:
489
490 return ret;
491}
492
51a0a5b0
MS
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)
a9d26f00 498
51a0a5b0 499static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_X, ADIS16400_XGYRO_OFF);
b1811197
MH
500static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_Y, ADIS16400_YGYRO_OFF);
501static ADIS16400_DEV_ATTR_CALIBBIAS(GYRO_Z, ADIS16400_ZGYRO_OFF);
51a0a5b0
MS
502
503static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_X, ADIS16400_XACCL_OFF);
b1811197
MH
504static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_Y, ADIS16400_YACCL_OFF);
505static ADIS16400_DEV_ATTR_CALIBBIAS(ACCEL_Z, ADIS16400_ZACCL_OFF);
a9d26f00 506
a9d26f00 507
1755b0ae 508static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16400_read_14bit_signed,
a9d26f00 509 ADIS16400_SUPPLY_OUT);
1755b0ae 510static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.002418 V");
a9d26f00
BS
511
512static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed,
513 ADIS16400_XGYRO_OUT);
514static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed,
515 ADIS16400_YGYRO_OUT);
516static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed,
517 ADIS16400_ZGYRO_OUT);
4f64b801 518static IIO_CONST_ATTR(gyro_scale, "0.0008726646");
a9d26f00
BS
519
520static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed,
521 ADIS16400_XACCL_OUT);
522static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed,
523 ADIS16400_YACCL_OUT);
524static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed,
525 ADIS16400_ZACCL_OUT);
4f64b801 526static IIO_CONST_ATTR(accel_scale, "0.0326561445");
a9d26f00
BS
527
528static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed,
529 ADIS16400_XMAGN_OUT);
530static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed,
531 ADIS16400_YMAGN_OUT);
532static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed,
533 ADIS16400_ZMAGN_OUT);
534static IIO_CONST_ATTR(magn_scale, "0.0005 Gs");
535
536
a1169c5a 537static IIO_DEV_ATTR_TEMP_RAW(adis16400_read_12bit_signed);
51a0a5b0
MS
538static IIO_CONST_ATTR_TEMP_OFFSET("198.16 K");
539static IIO_CONST_ATTR_TEMP_SCALE("0.14 K");
a9d26f00 540
1755b0ae 541static IIO_DEV_ATTR_IN_RAW(1, adis16400_read_12bit_unsigned,
a9d26f00 542 ADIS16400_AUX_ADC);
1755b0ae 543static IIO_CONST_ATTR(in1_scale, "0.000806 V");
a9d26f00
BS
544
545static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
546 adis16400_read_frequency,
547 adis16400_write_frequency);
548
549static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0);
550
51a0a5b0 551static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
a9d26f00 552
51a0a5b0 553static IIO_CONST_ATTR_NAME("adis16400");
a9d26f00
BS
554
555static struct attribute *adis16400_event_attributes[] = {
556 NULL
557};
558
559static struct attribute_group adis16400_event_attribute_group = {
560 .attrs = adis16400_event_attributes,
561};
562
563static struct attribute *adis16400_attributes[] = {
51a0a5b0
MS
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,
1755b0ae
JC
570 &iio_dev_attr_in0_supply_raw.dev_attr.attr,
571 &iio_const_attr_in0_supply_scale.dev_attr.attr,
a1169c5a
JC
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,
a9d26f00
BS
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,
a1169c5a
JC
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,
a9d26f00 583 &iio_const_attr_magn_scale.dev_attr.attr,
a1169c5a 584 &iio_dev_attr_temp_raw.dev_attr.attr,
a9d26f00
BS
585 &iio_const_attr_temp_offset.dev_attr.attr,
586 &iio_const_attr_temp_scale.dev_attr.attr,
1755b0ae
JC
587 &iio_dev_attr_in1_raw.dev_attr.attr,
588 &iio_const_attr_in1_scale.dev_attr.attr,
a9d26f00 589 &iio_dev_attr_sampling_frequency.dev_attr.attr,
51a0a5b0 590 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
a9d26f00
BS
591 &iio_dev_attr_reset.dev_attr.attr,
592 &iio_const_attr_name.dev_attr.attr,
593 NULL
594};
595
596static const struct attribute_group adis16400_attribute_group = {
597 .attrs = adis16400_attributes,
598};
599
600static 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 */
6f7c8ee5 625 st->indio_dev = iio_allocate_device(0);
a9d26f00
BS
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
2662051e 648 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
a9d26f00 649 if (ret) {
6a6ec623 650 dev_err(&spi->dev, "failed to initialize the ring\n");
a9d26f00
BS
651 goto error_unreg_ring_funcs;
652 }
653
654 if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) {
a9d26f00
BS
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
674error_remove_trigger:
675 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
676 adis16400_remove_trigger(st->indio_dev);
677error_unregister_line:
678 if (st->indio_dev->modes & INDIO_RING_TRIGGERED)
679 iio_unregister_interrupt_line(st->indio_dev, 0);
680error_uninitialize_ring:
2662051e 681 iio_ring_buffer_unregister(st->indio_dev->ring);
a9d26f00
BS
682error_unreg_ring_funcs:
683 adis16400_unconfigure_ring(st->indio_dev);
684error_free_dev:
685 if (regdone)
686 iio_device_unregister(st->indio_dev);
687 else
688 iio_free_device(st->indio_dev);
689error_free_tx:
690 kfree(st->tx);
691error_free_rx:
692 kfree(st->rx);
693error_free_st:
694 kfree(st);
695error_ret:
696 return ret;
697}
698
699/* fixme, confirm ordering in this function */
700static 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
2662051e 716 iio_ring_buffer_unregister(st->indio_dev->ring);
a9d26f00
BS
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
725err_ret:
726 return ret;
727}
728
729static 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
738static __init int adis16400_init(void)
739{
740 return spi_register_driver(&adis16400_driver);
741}
742module_init(adis16400_init);
743
744static __exit void adis16400_exit(void)
745{
746 spi_unregister_driver(&adis16400_driver);
747}
748module_exit(adis16400_exit);
749
750MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>");
751MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver");
752MODULE_LICENSE("GPL v2");
This page took 0.143542 seconds and 5 git commands to generate.