staging: iio: adc add numbers to naming of all adc channels as needed to associate...
[deliverable/linux.git] / drivers / staging / iio / imu / adis16300_core.c
CommitLineData
1e3864e6
BS
1/*
2 * ADIS16300 Four Degrees of Freedom Inertial Sensor Driver
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>
11#include <linux/gpio.h>
12#include <linux/delay.h>
13#include <linux/mutex.h>
14#include <linux/device.h>
15#include <linux/kernel.h>
16#include <linux/spi/spi.h>
1cb6c1f5 17#include <linux/slab.h>
1e3864e6
BS
18#include <linux/sysfs.h>
19#include <linux/list.h>
20
21#include "../iio.h"
22#include "../sysfs.h"
2662051e 23#include "../ring_generic.h"
1e3864e6 24#include "../accel/accel.h"
a1169c5a 25#include "../accel/inclinometer.h"
1e3864e6
BS
26#include "../gyro/gyro.h"
27#include "../adc/adc.h"
28
29#include "adis16300.h"
30
31#define DRIVER_NAME "adis16300"
32
ee9a691e 33static int adis16300_check_status(struct device *dev);
1e3864e6
BS
34
35/**
36 * adis16300_spi_write_reg_8() - write single byte to a register
37 * @dev: device associated with child of actual device (iio_dev or iio_trig)
38 * @reg_address: the address of the register to be written
39 * @val: the value to write
40 **/
4da54d93 41static int adis16300_spi_write_reg_8(struct device *dev,
1e3864e6
BS
42 u8 reg_address,
43 u8 val)
44{
45 int ret;
46 struct iio_dev *indio_dev = dev_get_drvdata(dev);
47 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
48
49 mutex_lock(&st->buf_lock);
50 st->tx[0] = ADIS16300_WRITE_REG(reg_address);
51 st->tx[1] = val;
52
53 ret = spi_write(st->us, st->tx, 2);
54 mutex_unlock(&st->buf_lock);
55
56 return ret;
57}
58
59/**
60 * adis16300_spi_write_reg_16() - write 2 bytes to a pair of registers
61 * @dev: device associated with child of actual device (iio_dev or iio_trig)
62 * @reg_address: the address of the lower of the two registers. Second register
63 * is assumed to have address one greater.
64 * @val: value to be written
65 **/
66static int adis16300_spi_write_reg_16(struct device *dev,
67 u8 lower_reg_address,
68 u16 value)
69{
70 int ret;
71 struct spi_message msg;
72 struct iio_dev *indio_dev = dev_get_drvdata(dev);
73 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
74 struct spi_transfer xfers[] = {
75 {
76 .tx_buf = st->tx,
77 .bits_per_word = 8,
78 .len = 2,
79 .cs_change = 1,
ee9a691e 80 .delay_usecs = 75,
1e3864e6
BS
81 }, {
82 .tx_buf = st->tx + 2,
83 .bits_per_word = 8,
84 .len = 2,
85 .cs_change = 1,
ee9a691e 86 .delay_usecs = 75,
1e3864e6
BS
87 },
88 };
89
90 mutex_lock(&st->buf_lock);
91 st->tx[0] = ADIS16300_WRITE_REG(lower_reg_address);
92 st->tx[1] = value & 0xFF;
93 st->tx[2] = ADIS16300_WRITE_REG(lower_reg_address + 1);
94 st->tx[3] = (value >> 8) & 0xFF;
95
96 spi_message_init(&msg);
97 spi_message_add_tail(&xfers[0], &msg);
98 spi_message_add_tail(&xfers[1], &msg);
99 ret = spi_sync(st->us, &msg);
100 mutex_unlock(&st->buf_lock);
101
102 return ret;
103}
104
105/**
106 * adis16300_spi_read_reg_16() - read 2 bytes from a 16-bit register
107 * @dev: device associated with child of actual device (iio_dev or iio_trig)
108 * @reg_address: the address of the lower of the two registers. Second register
109 * is assumed to have address one greater.
110 * @val: somewhere to pass back the value read
111 **/
112static int adis16300_spi_read_reg_16(struct device *dev,
113 u8 lower_reg_address,
114 u16 *val)
115{
116 struct spi_message msg;
117 struct iio_dev *indio_dev = dev_get_drvdata(dev);
118 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
119 int ret;
120 struct spi_transfer xfers[] = {
121 {
122 .tx_buf = st->tx,
123 .bits_per_word = 8,
124 .len = 2,
ee9a691e
BS
125 .cs_change = 1,
126 .delay_usecs = 75,
1e3864e6
BS
127 }, {
128 .rx_buf = st->rx,
129 .bits_per_word = 8,
130 .len = 2,
ee9a691e
BS
131 .cs_change = 1,
132 .delay_usecs = 75,
1e3864e6
BS
133 },
134 };
135
136 mutex_lock(&st->buf_lock);
137 st->tx[0] = ADIS16300_READ_REG(lower_reg_address);
138 st->tx[1] = 0;
139 st->tx[2] = 0;
140 st->tx[3] = 0;
141
142 spi_message_init(&msg);
143 spi_message_add_tail(&xfers[0], &msg);
144 spi_message_add_tail(&xfers[1], &msg);
145 ret = spi_sync(st->us, &msg);
146 if (ret) {
147 dev_err(&st->us->dev,
148 "problem when reading 16 bit register 0x%02X",
149 lower_reg_address);
150 goto error_ret;
151 }
152 *val = (st->rx[0] << 8) | st->rx[1];
153
154error_ret:
155 mutex_unlock(&st->buf_lock);
156 return ret;
157}
158
1e3864e6
BS
159static ssize_t adis16300_spi_read_signed(struct device *dev,
160 struct device_attribute *attr,
161 char *buf,
162 unsigned bits)
163{
164 int ret;
165 s16 val = 0;
166 unsigned shift = 16 - bits;
167 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
168
169 ret = adis16300_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
170 if (ret)
171 return ret;
172
173 if (val & ADIS16300_ERROR_ACTIVE)
174 adis16300_check_status(dev);
175 val = ((s16)(val << shift) >> shift);
176 return sprintf(buf, "%d\n", val);
177}
178
179static ssize_t adis16300_read_12bit_unsigned(struct device *dev,
180 struct device_attribute *attr,
181 char *buf)
182{
183 int ret;
184 u16 val = 0;
185 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
186
187 ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
188 if (ret)
189 return ret;
190
191 if (val & ADIS16300_ERROR_ACTIVE)
192 adis16300_check_status(dev);
193
194 return sprintf(buf, "%u\n", val & 0x0FFF);
195}
196
ee9a691e
BS
197static ssize_t adis16300_read_14bit_unsigned(struct device *dev,
198 struct device_attribute *attr,
199 char *buf)
200{
201 int ret;
202 u16 val = 0;
203 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
204
205 ret = adis16300_spi_read_reg_16(dev, this_attr->address, &val);
206 if (ret)
207 return ret;
208
209 if (val & ADIS16300_ERROR_ACTIVE)
210 adis16300_check_status(dev);
211
212 return sprintf(buf, "%u\n", val & 0x3FFF);
213}
214
1e3864e6
BS
215static ssize_t adis16300_read_14bit_signed(struct device *dev,
216 struct device_attribute *attr,
217 char *buf)
218{
219 struct iio_dev *indio_dev = dev_get_drvdata(dev);
220 ssize_t ret;
221
222 /* Take the iio_dev status lock */
223 mutex_lock(&indio_dev->mlock);
224 ret = adis16300_spi_read_signed(dev, attr, buf, 14);
225 mutex_unlock(&indio_dev->mlock);
226
227 return ret;
228}
229
230static ssize_t adis16300_read_12bit_signed(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
233{
234 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 ssize_t ret;
236
237 /* Take the iio_dev status lock */
238 mutex_lock(&indio_dev->mlock);
239 ret = adis16300_spi_read_signed(dev, attr, buf, 12);
240 mutex_unlock(&indio_dev->mlock);
241
242 return ret;
243}
244
245static ssize_t adis16300_read_13bit_signed(struct device *dev,
246 struct device_attribute *attr,
247 char *buf)
248{
249 struct iio_dev *indio_dev = dev_get_drvdata(dev);
250 ssize_t ret;
251
252 /* Take the iio_dev status lock */
253 mutex_lock(&indio_dev->mlock);
254 ret = adis16300_spi_read_signed(dev, attr, buf, 13);
255 mutex_unlock(&indio_dev->mlock);
256
257 return ret;
258}
259
260static ssize_t adis16300_write_16bit(struct device *dev,
261 struct device_attribute *attr,
262 const char *buf,
263 size_t len)
264{
265 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
266 int ret;
267 long val;
268
269 ret = strict_strtol(buf, 10, &val);
270 if (ret)
271 goto error_ret;
272 ret = adis16300_spi_write_reg_16(dev, this_attr->address, val);
273
274error_ret:
275 return ret ? ret : len;
276}
277
278static ssize_t adis16300_read_frequency(struct device *dev,
279 struct device_attribute *attr,
280 char *buf)
281{
282 int ret, len = 0;
283 u16 t;
284 int sps;
285 ret = adis16300_spi_read_reg_16(dev,
286 ADIS16300_SMPL_PRD,
287 &t);
288 if (ret)
289 return ret;
290 sps = (t & ADIS16300_SMPL_PRD_TIME_BASE) ? 53 : 1638;
291 sps /= (t & ADIS16300_SMPL_PRD_DIV_MASK) + 1;
292 len = sprintf(buf, "%d SPS\n", sps);
293 return len;
294}
295
296static ssize_t adis16300_write_frequency(struct device *dev,
297 struct device_attribute *attr,
298 const char *buf,
299 size_t len)
300{
301 struct iio_dev *indio_dev = dev_get_drvdata(dev);
302 struct adis16300_state *st = iio_dev_get_devdata(indio_dev);
303 long val;
304 int ret;
305 u8 t;
306
307 ret = strict_strtol(buf, 10, &val);
308 if (ret)
309 return ret;
310
311 mutex_lock(&indio_dev->mlock);
312
313 t = (1638 / val);
314 if (t > 0)
315 t--;
316 t &= ADIS16300_SMPL_PRD_DIV_MASK;
317 if ((t & ADIS16300_SMPL_PRD_DIV_MASK) >= 0x0A)
318 st->us->max_speed_hz = ADIS16300_SPI_SLOW;
319 else
320 st->us->max_speed_hz = ADIS16300_SPI_FAST;
321
322 ret = adis16300_spi_write_reg_8(dev,
323 ADIS16300_SMPL_PRD,
324 t);
325
326 mutex_unlock(&indio_dev->mlock);
327
328 return ret ? ret : len;
329}
330
ee9a691e
BS
331static int adis16300_reset(struct device *dev)
332{
333 int ret;
334 ret = adis16300_spi_write_reg_8(dev,
335 ADIS16300_GLOB_CMD,
336 ADIS16300_GLOB_CMD_SW_RESET);
337 if (ret)
338 dev_err(dev, "problem resetting device");
339
340 return ret;
341}
342
1e3864e6
BS
343static ssize_t adis16300_write_reset(struct device *dev,
344 struct device_attribute *attr,
345 const char *buf, size_t len)
346{
347 if (len < 1)
348 return -1;
349 switch (buf[0]) {
350 case '1':
351 case 'y':
352 case 'Y':
353 return adis16300_reset(dev);
354 }
355 return -1;
356}
357
1e3864e6
BS
358int adis16300_set_irq(struct device *dev, bool enable)
359{
360 int ret;
361 u16 msc;
362 ret = adis16300_spi_read_reg_16(dev, ADIS16300_MSC_CTRL, &msc);
363 if (ret)
364 goto error_ret;
365
366 msc |= ADIS16300_MSC_CTRL_DATA_RDY_POL_HIGH;
367 msc &= ~ADIS16300_MSC_CTRL_DATA_RDY_DIO2;
368 if (enable)
369 msc |= ADIS16300_MSC_CTRL_DATA_RDY_EN;
370 else
371 msc &= ~ADIS16300_MSC_CTRL_DATA_RDY_EN;
372
373 ret = adis16300_spi_write_reg_16(dev, ADIS16300_MSC_CTRL, msc);
374 if (ret)
375 goto error_ret;
376
377error_ret:
378 return ret;
379}
380
ee9a691e
BS
381/* Power down the device */
382static int adis16300_stop_device(struct device *dev)
1e3864e6
BS
383{
384 int ret;
ee9a691e
BS
385 u16 val = ADIS16300_SLP_CNT_POWER_OFF;
386
387 ret = adis16300_spi_write_reg_16(dev, ADIS16300_SLP_CNT, val);
1e3864e6 388 if (ret)
ee9a691e 389 dev_err(dev, "problem with turning device off: SLP_CNT");
1e3864e6
BS
390
391 return ret;
392}
393
ee9a691e 394static int adis16300_self_test(struct device *dev)
1e3864e6
BS
395{
396 int ret;
ee9a691e
BS
397 ret = adis16300_spi_write_reg_16(dev,
398 ADIS16300_MSC_CTRL,
399 ADIS16300_MSC_CTRL_MEM_TEST);
400 if (ret) {
401 dev_err(dev, "problem starting self test");
402 goto err_ret;
403 }
1e3864e6 404
ee9a691e 405 adis16300_check_status(dev);
1e3864e6 406
ee9a691e 407err_ret:
1e3864e6
BS
408 return ret;
409}
410
ee9a691e 411static int adis16300_check_status(struct device *dev)
1e3864e6
BS
412{
413 u16 status;
414 int ret;
415
416 ret = adis16300_spi_read_reg_16(dev, ADIS16300_DIAG_STAT, &status);
417
418 if (ret < 0) {
419 dev_err(dev, "Reading status failed\n");
420 goto error_ret;
421 }
422 ret = status;
423 if (status & ADIS16300_DIAG_STAT_ZACCL_FAIL)
424 dev_err(dev, "Z-axis accelerometer self-test failure\n");
425 if (status & ADIS16300_DIAG_STAT_YACCL_FAIL)
426 dev_err(dev, "Y-axis accelerometer self-test failure\n");
427 if (status & ADIS16300_DIAG_STAT_XACCL_FAIL)
428 dev_err(dev, "X-axis accelerometer self-test failure\n");
429 if (status & ADIS16300_DIAG_STAT_XGYRO_FAIL)
430 dev_err(dev, "X-axis gyroscope self-test failure\n");
431 if (status & ADIS16300_DIAG_STAT_ALARM2)
432 dev_err(dev, "Alarm 2 active\n");
433 if (status & ADIS16300_DIAG_STAT_ALARM1)
434 dev_err(dev, "Alarm 1 active\n");
435 if (status & ADIS16300_DIAG_STAT_FLASH_CHK)
436 dev_err(dev, "Flash checksum error\n");
437 if (status & ADIS16300_DIAG_STAT_SELF_TEST)
438 dev_err(dev, "Self test error\n");
439 if (status & ADIS16300_DIAG_STAT_OVERFLOW)
440 dev_err(dev, "Sensor overrange\n");
441 if (status & ADIS16300_DIAG_STAT_SPI_FAIL)
442 dev_err(dev, "SPI failure\n");
443 if (status & ADIS16300_DIAG_STAT_FLASH_UPT)
444 dev_err(dev, "Flash update failed\n");
445 if (status & ADIS16300_DIAG_STAT_POWER_HIGH)
446 dev_err(dev, "Power supply above 5.25V\n");
447 if (status & ADIS16300_DIAG_STAT_POWER_LOW)
448 dev_err(dev, "Power supply below 4.75V\n");
449
450error_ret:
451 return ret;
452}
453
454static int adis16300_initial_setup(struct adis16300_state *st)
455{
456 int ret;
457 u16 smp_prd;
458 struct device *dev = &st->indio_dev->dev;
459
460 /* use low spi speed for init */
461 st->us->max_speed_hz = ADIS16300_SPI_SLOW;
462 st->us->mode = SPI_MODE_3;
463 spi_setup(st->us);
464
465 /* Disable IRQ */
466 ret = adis16300_set_irq(dev, false);
467 if (ret) {
468 dev_err(dev, "disable irq failed");
469 goto err_ret;
470 }
471
472 /* Do self test */
ee9a691e
BS
473 ret = adis16300_self_test(dev);
474 if (ret) {
475 dev_err(dev, "self test failure");
476 goto err_ret;
477 }
1e3864e6
BS
478
479 /* Read status register to check the result */
480 ret = adis16300_check_status(dev);
481 if (ret) {
482 adis16300_reset(dev);
483 dev_err(dev, "device not playing ball -> reset");
484 msleep(ADIS16300_STARTUP_DELAY);
485 ret = adis16300_check_status(dev);
486 if (ret) {
487 dev_err(dev, "giving up");
488 goto err_ret;
489 }
490 }
491
492 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
493 st->us->chip_select, st->us->irq);
494
495 /* use high spi speed if possible */
496 ret = adis16300_spi_read_reg_16(dev, ADIS16300_SMPL_PRD, &smp_prd);
497 if (!ret && (smp_prd & ADIS16300_SMPL_PRD_DIV_MASK) < 0x0A) {
498 st->us->max_speed_hz = ADIS16300_SPI_SLOW;
499 spi_setup(st->us);
500 }
501
502err_ret:
503 return ret;
504}
505
51a0a5b0
MS
506static IIO_DEV_ATTR_GYRO_X_CALIBBIAS(S_IWUSR | S_IRUGO,
507 adis16300_read_12bit_signed,
508 adis16300_write_16bit,
509 ADIS16300_XGYRO_OFF);
510
511static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR | S_IRUGO,
1e3864e6
BS
512 adis16300_read_12bit_signed,
513 adis16300_write_16bit,
514 ADIS16300_XACCL_OFF);
515
51a0a5b0 516static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
1e3864e6
BS
517 adis16300_read_12bit_signed,
518 adis16300_write_16bit,
519 ADIS16300_YACCL_OFF);
520
51a0a5b0 521static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
1e3864e6
BS
522 adis16300_read_12bit_signed,
523 adis16300_write_16bit,
524 ADIS16300_ZACCL_OFF);
525
1755b0ae 526static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16300_read_14bit_unsigned,
1e3864e6 527 ADIS16300_SUPPLY_OUT);
1755b0ae 528static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.00242");
1e3864e6
BS
529
530static IIO_DEV_ATTR_GYRO_X(adis16300_read_14bit_signed,
531 ADIS16300_XGYRO_OUT);
4f64b801 532static IIO_CONST_ATTR_GYRO_SCALE("0.000872664");
1e3864e6
BS
533
534static IIO_DEV_ATTR_ACCEL_X(adis16300_read_14bit_signed,
535 ADIS16300_XACCL_OUT);
536static IIO_DEV_ATTR_ACCEL_Y(adis16300_read_14bit_signed,
537 ADIS16300_YACCL_OUT);
538static IIO_DEV_ATTR_ACCEL_Z(adis16300_read_14bit_signed,
539 ADIS16300_ZACCL_OUT);
4f64b801 540static IIO_CONST_ATTR_ACCEL_SCALE("0.00588399");
1e3864e6
BS
541
542static IIO_DEV_ATTR_INCLI_X(adis16300_read_13bit_signed,
543 ADIS16300_XINCLI_OUT);
544static IIO_DEV_ATTR_INCLI_Y(adis16300_read_13bit_signed,
545 ADIS16300_YINCLI_OUT);
4f64b801 546static IIO_CONST_ATTR_INCLI_SCALE("0.00076794487");
1e3864e6 547
ee9a691e 548static IIO_DEV_ATTR_TEMP_RAW(adis16300_read_12bit_unsigned);
4f64b801
MS
549static IIO_CONST_ATTR_TEMP_OFFSET("198.16");
550static IIO_CONST_ATTR_TEMP_SCALE("0.14");
1e3864e6 551
1755b0ae 552static IIO_DEV_ATTR_IN_RAW(1, adis16300_read_12bit_unsigned,
1e3864e6 553 ADIS16300_AUX_ADC);
1755b0ae 554static IIO_CONST_ATTR(in1_scale, "0.000806");
1e3864e6
BS
555
556static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
557 adis16300_read_frequency,
558 adis16300_write_frequency);
559
560static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16300_write_reset, 0);
561
51a0a5b0 562static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("409 546 819 1638");
1e3864e6 563
51a0a5b0 564static IIO_CONST_ATTR_NAME("adis16300");
1e3864e6
BS
565
566static struct attribute *adis16300_event_attributes[] = {
567 NULL
568};
569
570static struct attribute_group adis16300_event_attribute_group = {
571 .attrs = adis16300_event_attributes,
572};
573
574static struct attribute *adis16300_attributes[] = {
51a0a5b0
MS
575 &iio_dev_attr_gyro_x_calibbias.dev_attr.attr,
576 &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
577 &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
578 &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
1755b0ae
JC
579 &iio_dev_attr_in0_supply_raw.dev_attr.attr,
580 &iio_const_attr_in0_supply_scale.dev_attr.attr,
a1169c5a 581 &iio_dev_attr_gyro_x_raw.dev_attr.attr,
1e3864e6
BS
582 &iio_const_attr_gyro_scale.dev_attr.attr,
583 &iio_dev_attr_accel_x_raw.dev_attr.attr,
584 &iio_dev_attr_accel_y_raw.dev_attr.attr,
585 &iio_dev_attr_accel_z_raw.dev_attr.attr,
586 &iio_const_attr_accel_scale.dev_attr.attr,
a1169c5a
JC
587 &iio_dev_attr_incli_x_raw.dev_attr.attr,
588 &iio_dev_attr_incli_y_raw.dev_attr.attr,
1e3864e6 589 &iio_const_attr_incli_scale.dev_attr.attr,
a1169c5a 590 &iio_dev_attr_temp_raw.dev_attr.attr,
1e3864e6
BS
591 &iio_const_attr_temp_offset.dev_attr.attr,
592 &iio_const_attr_temp_scale.dev_attr.attr,
1755b0ae
JC
593 &iio_dev_attr_in1_raw.dev_attr.attr,
594 &iio_const_attr_in1_scale.dev_attr.attr,
1e3864e6 595 &iio_dev_attr_sampling_frequency.dev_attr.attr,
51a0a5b0 596 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1e3864e6
BS
597 &iio_dev_attr_reset.dev_attr.attr,
598 &iio_const_attr_name.dev_attr.attr,
599 NULL
600};
601
602static const struct attribute_group adis16300_attribute_group = {
603 .attrs = adis16300_attributes,
604};
605
606static int __devinit adis16300_probe(struct spi_device *spi)
607{
608 int ret, regdone = 0;
609 struct adis16300_state *st = kzalloc(sizeof *st, GFP_KERNEL);
610 if (!st) {
611 ret = -ENOMEM;
612 goto error_ret;
613 }
614 /* this is only used for removal purposes */
615 spi_set_drvdata(spi, st);
616
617 /* Allocate the comms buffers */
618 st->rx = kzalloc(sizeof(*st->rx)*ADIS16300_MAX_RX, GFP_KERNEL);
619 if (st->rx == NULL) {
620 ret = -ENOMEM;
621 goto error_free_st;
622 }
623 st->tx = kzalloc(sizeof(*st->tx)*ADIS16300_MAX_TX, GFP_KERNEL);
624 if (st->tx == NULL) {
625 ret = -ENOMEM;
626 goto error_free_rx;
627 }
628 st->us = spi;
629 mutex_init(&st->buf_lock);
630 /* setup the industrialio driver allocated elements */
631 st->indio_dev = iio_allocate_device();
632 if (st->indio_dev == NULL) {
633 ret = -ENOMEM;
634 goto error_free_tx;
635 }
636
637 st->indio_dev->dev.parent = &spi->dev;
638 st->indio_dev->num_interrupt_lines = 1;
639 st->indio_dev->event_attrs = &adis16300_event_attribute_group;
640 st->indio_dev->attrs = &adis16300_attribute_group;
641 st->indio_dev->dev_data = (void *)(st);
642 st->indio_dev->driver_module = THIS_MODULE;
643 st->indio_dev->modes = INDIO_DIRECT_MODE;
644
645 ret = adis16300_configure_ring(st->indio_dev);
646 if (ret)
647 goto error_free_dev;
648
649 ret = iio_device_register(st->indio_dev);
650 if (ret)
651 goto error_unreg_ring_funcs;
652 regdone = 1;
653
2662051e 654 ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
1e3864e6
BS
655 if (ret) {
656 printk(KERN_ERR "failed to initialize the ring\n");
657 goto error_unreg_ring_funcs;
658 }
659
ee9a691e 660 if (spi->irq) {
1e3864e6
BS
661 ret = iio_register_interrupt_line(spi->irq,
662 st->indio_dev,
663 0,
664 IRQF_TRIGGER_RISING,
665 "adis16300");
666 if (ret)
667 goto error_uninitialize_ring;
668
669 ret = adis16300_probe_trigger(st->indio_dev);
670 if (ret)
671 goto error_unregister_line;
672 }
673
674 /* Get the device into a sane initial state */
675 ret = adis16300_initial_setup(st);
676 if (ret)
677 goto error_remove_trigger;
678 return 0;
679
680error_remove_trigger:
ee9a691e 681 adis16300_remove_trigger(st->indio_dev);
1e3864e6 682error_unregister_line:
ee9a691e 683 if (spi->irq)
1e3864e6
BS
684 iio_unregister_interrupt_line(st->indio_dev, 0);
685error_uninitialize_ring:
2662051e 686 iio_ring_buffer_unregister(st->indio_dev->ring);
1e3864e6
BS
687error_unreg_ring_funcs:
688 adis16300_unconfigure_ring(st->indio_dev);
689error_free_dev:
690 if (regdone)
691 iio_device_unregister(st->indio_dev);
692 else
693 iio_free_device(st->indio_dev);
694error_free_tx:
695 kfree(st->tx);
696error_free_rx:
697 kfree(st->rx);
698error_free_st:
699 kfree(st);
700error_ret:
701 return ret;
702}
703
1e3864e6
BS
704static int adis16300_remove(struct spi_device *spi)
705{
706 int ret;
707 struct adis16300_state *st = spi_get_drvdata(spi);
708 struct iio_dev *indio_dev = st->indio_dev;
709
710 ret = adis16300_stop_device(&(indio_dev->dev));
711 if (ret)
712 goto err_ret;
713
714 flush_scheduled_work();
715
716 adis16300_remove_trigger(indio_dev);
ee9a691e 717 if (spi->irq)
1e3864e6
BS
718 iio_unregister_interrupt_line(indio_dev, 0);
719
2662051e 720 iio_ring_buffer_unregister(indio_dev->ring);
1e3864e6 721 iio_device_unregister(indio_dev);
ee9a691e 722 adis16300_unconfigure_ring(indio_dev);
1e3864e6
BS
723 kfree(st->tx);
724 kfree(st->rx);
725 kfree(st);
726
727 return 0;
728
729err_ret:
730 return ret;
731}
732
733static struct spi_driver adis16300_driver = {
734 .driver = {
735 .name = "adis16300",
736 .owner = THIS_MODULE,
737 },
738 .probe = adis16300_probe,
739 .remove = __devexit_p(adis16300_remove),
740};
741
742static __init int adis16300_init(void)
743{
744 return spi_register_driver(&adis16300_driver);
745}
746module_init(adis16300_init);
747
748static __exit void adis16300_exit(void)
749{
750 spi_unregister_driver(&adis16300_driver);
751}
752module_exit(adis16300_exit);
753
754MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
755MODULE_DESCRIPTION("Analog Devices ADIS16300 IMU SPI driver");
756MODULE_LICENSE("GPL v2");
This page took 0.101494 seconds and 5 git commands to generate.