diff options
author | Barry Song <barry.song@analog.com> | 2010-10-27 21:43:49 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-11-09 18:46:33 -0500 |
commit | f7fe1d1dd5a512a44f0ada40ff7f120664e2e082 (patch) | |
tree | c4dc7b59c8b176936061fd713c7b4dd7eee486c9 /drivers/staging/iio/accel/adis16201_core.c | |
parent | 6f125f17945a65e0bed37a4dfd7e5397a2c7a886 (diff) |
staging: iio: new adis16201 driver
IIO driver for dual Axis Accelerometer/inclinometer adis16201 parts.
Signed-off-by: Barry Song <barry.song@analog.com>
Signed-off-by: Michael Hennerich <michael.hennerich@analog.com>
Acked-by: Jonathan Cameron <jic23@cam.ac.uk>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/staging/iio/accel/adis16201_core.c')
-rw-r--r-- | drivers/staging/iio/accel/adis16201_core.c | 659 |
1 files changed, 659 insertions, 0 deletions
diff --git a/drivers/staging/iio/accel/adis16201_core.c b/drivers/staging/iio/accel/adis16201_core.c new file mode 100644 index 00000000000..79b785a0013 --- /dev/null +++ b/drivers/staging/iio/accel/adis16201_core.c | |||
@@ -0,0 +1,659 @@ | |||
1 | /* | ||
2 | * ADIS16201 Programmable Digital Vibration 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> | ||
17 | #include <linux/slab.h> | ||
18 | #include <linux/sysfs.h> | ||
19 | #include <linux/list.h> | ||
20 | |||
21 | #include "../iio.h" | ||
22 | #include "../sysfs.h" | ||
23 | #include "accel.h" | ||
24 | #include "inclinometer.h" | ||
25 | #include "../gyro/gyro.h" | ||
26 | #include "../adc/adc.h" | ||
27 | |||
28 | #include "adis16201.h" | ||
29 | |||
30 | #define DRIVER_NAME "adis16201" | ||
31 | |||
32 | static int adis16201_check_status(struct device *dev); | ||
33 | |||
34 | /** | ||
35 | * adis16201_spi_write_reg_8() - write single byte to a register | ||
36 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | ||
37 | * @reg_address: the address of the register to be written | ||
38 | * @val: the value to write | ||
39 | **/ | ||
40 | static int adis16201_spi_write_reg_8(struct device *dev, | ||
41 | u8 reg_address, | ||
42 | u8 val) | ||
43 | { | ||
44 | int ret; | ||
45 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
46 | struct adis16201_state *st = iio_dev_get_devdata(indio_dev); | ||
47 | |||
48 | mutex_lock(&st->buf_lock); | ||
49 | st->tx[0] = ADIS16201_WRITE_REG(reg_address); | ||
50 | st->tx[1] = val; | ||
51 | |||
52 | ret = spi_write(st->us, st->tx, 2); | ||
53 | mutex_unlock(&st->buf_lock); | ||
54 | |||
55 | return ret; | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers | ||
60 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | ||
61 | * @reg_address: the address of the lower of the two registers. Second register | ||
62 | * is assumed to have address one greater. | ||
63 | * @val: value to be written | ||
64 | **/ | ||
65 | static int adis16201_spi_write_reg_16(struct device *dev, | ||
66 | u8 lower_reg_address, | ||
67 | u16 value) | ||
68 | { | ||
69 | int ret; | ||
70 | struct spi_message msg; | ||
71 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
72 | struct adis16201_state *st = iio_dev_get_devdata(indio_dev); | ||
73 | struct spi_transfer xfers[] = { | ||
74 | { | ||
75 | .tx_buf = st->tx, | ||
76 | .bits_per_word = 8, | ||
77 | .len = 2, | ||
78 | .cs_change = 1, | ||
79 | }, { | ||
80 | .tx_buf = st->tx + 2, | ||
81 | .bits_per_word = 8, | ||
82 | .len = 2, | ||
83 | .cs_change = 1, | ||
84 | }, | ||
85 | }; | ||
86 | |||
87 | mutex_lock(&st->buf_lock); | ||
88 | st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address); | ||
89 | st->tx[1] = value & 0xFF; | ||
90 | st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1); | ||
91 | st->tx[3] = (value >> 8) & 0xFF; | ||
92 | |||
93 | spi_message_init(&msg); | ||
94 | spi_message_add_tail(&xfers[0], &msg); | ||
95 | spi_message_add_tail(&xfers[1], &msg); | ||
96 | ret = spi_sync(st->us, &msg); | ||
97 | mutex_unlock(&st->buf_lock); | ||
98 | |||
99 | return ret; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register | ||
104 | * @dev: device associated with child of actual device (iio_dev or iio_trig) | ||
105 | * @reg_address: the address of the lower of the two registers. Second register | ||
106 | * is assumed to have address one greater. | ||
107 | * @val: somewhere to pass back the value read | ||
108 | **/ | ||
109 | static int adis16201_spi_read_reg_16(struct device *dev, | ||
110 | u8 lower_reg_address, | ||
111 | u16 *val) | ||
112 | { | ||
113 | struct spi_message msg; | ||
114 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
115 | struct adis16201_state *st = iio_dev_get_devdata(indio_dev); | ||
116 | int ret; | ||
117 | struct spi_transfer xfers[] = { | ||
118 | { | ||
119 | .tx_buf = st->tx, | ||
120 | .bits_per_word = 8, | ||
121 | .len = 2, | ||
122 | .cs_change = 1, | ||
123 | .delay_usecs = 20, | ||
124 | }, { | ||
125 | .rx_buf = st->rx, | ||
126 | .bits_per_word = 8, | ||
127 | .len = 2, | ||
128 | .cs_change = 1, | ||
129 | .delay_usecs = 20, | ||
130 | }, | ||
131 | }; | ||
132 | |||
133 | mutex_lock(&st->buf_lock); | ||
134 | st->tx[0] = ADIS16201_READ_REG(lower_reg_address); | ||
135 | st->tx[1] = 0; | ||
136 | |||
137 | spi_message_init(&msg); | ||
138 | spi_message_add_tail(&xfers[0], &msg); | ||
139 | spi_message_add_tail(&xfers[1], &msg); | ||
140 | ret = spi_sync(st->us, &msg); | ||
141 | if (ret) { | ||
142 | dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X", | ||
143 | lower_reg_address); | ||
144 | goto error_ret; | ||
145 | } | ||
146 | *val = (st->rx[0] << 8) | st->rx[1]; | ||
147 | |||
148 | error_ret: | ||
149 | mutex_unlock(&st->buf_lock); | ||
150 | return ret; | ||
151 | } | ||
152 | |||
153 | static ssize_t adis16201_read_12bit_unsigned(struct device *dev, | ||
154 | struct device_attribute *attr, | ||
155 | char *buf) | ||
156 | { | ||
157 | int ret; | ||
158 | u16 val = 0; | ||
159 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | ||
160 | |||
161 | ret = adis16201_spi_read_reg_16(dev, this_attr->address, &val); | ||
162 | if (ret) | ||
163 | return ret; | ||
164 | |||
165 | if (val & ADIS16201_ERROR_ACTIVE) { | ||
166 | ret = adis16201_check_status(dev); | ||
167 | if (ret) | ||
168 | return ret; | ||
169 | } | ||
170 | |||
171 | return sprintf(buf, "%u\n", val & 0x0FFF); | ||
172 | } | ||
173 | |||
174 | static ssize_t adis16201_read_temp(struct device *dev, | ||
175 | struct device_attribute *attr, | ||
176 | char *buf) | ||
177 | { | ||
178 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
179 | ssize_t ret; | ||
180 | u16 val; | ||
181 | |||
182 | /* Take the iio_dev status lock */ | ||
183 | mutex_lock(&indio_dev->mlock); | ||
184 | |||
185 | ret = adis16201_spi_read_reg_16(dev, ADIS16201_TEMP_OUT, (u16 *)&val); | ||
186 | if (ret) | ||
187 | goto error_ret; | ||
188 | |||
189 | if (val & ADIS16201_ERROR_ACTIVE) { | ||
190 | ret = adis16201_check_status(dev); | ||
191 | if (ret) | ||
192 | goto error_ret; | ||
193 | } | ||
194 | |||
195 | val &= 0xFFF; | ||
196 | ret = sprintf(buf, "%d\n", val); | ||
197 | |||
198 | error_ret: | ||
199 | mutex_unlock(&indio_dev->mlock); | ||
200 | return ret; | ||
201 | } | ||
202 | |||
203 | static ssize_t adis16201_read_9bit_signed(struct device *dev, | ||
204 | struct device_attribute *attr, | ||
205 | char *buf) | ||
206 | { | ||
207 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
208 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | ||
209 | s16 val = 0; | ||
210 | ssize_t ret; | ||
211 | |||
212 | mutex_lock(&indio_dev->mlock); | ||
213 | |||
214 | ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | ||
215 | if (!ret) { | ||
216 | if (val & ADIS16201_ERROR_ACTIVE) { | ||
217 | ret = adis16201_check_status(dev); | ||
218 | if (ret) | ||
219 | goto error_ret; | ||
220 | } | ||
221 | val = ((s16)(val << 7) >> 7); | ||
222 | ret = sprintf(buf, "%d\n", val); | ||
223 | } | ||
224 | |||
225 | error_ret: | ||
226 | mutex_unlock(&indio_dev->mlock); | ||
227 | |||
228 | return ret; | ||
229 | } | ||
230 | |||
231 | static ssize_t adis16201_read_12bit_signed(struct device *dev, | ||
232 | struct device_attribute *attr, | ||
233 | char *buf) | ||
234 | { | ||
235 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
236 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | ||
237 | s16 val = 0; | ||
238 | ssize_t ret; | ||
239 | |||
240 | mutex_lock(&indio_dev->mlock); | ||
241 | |||
242 | ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | ||
243 | if (!ret) { | ||
244 | if (val & ADIS16201_ERROR_ACTIVE) { | ||
245 | ret = adis16201_check_status(dev); | ||
246 | if (ret) | ||
247 | goto error_ret; | ||
248 | } | ||
249 | |||
250 | val = ((s16)(val << 4) >> 4); | ||
251 | ret = sprintf(buf, "%d\n", val); | ||
252 | } | ||
253 | |||
254 | error_ret: | ||
255 | mutex_unlock(&indio_dev->mlock); | ||
256 | |||
257 | return ret; | ||
258 | } | ||
259 | |||
260 | static ssize_t adis16201_read_14bit_signed(struct device *dev, | ||
261 | struct device_attribute *attr, | ||
262 | char *buf) | ||
263 | { | ||
264 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
265 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | ||
266 | s16 val = 0; | ||
267 | ssize_t ret; | ||
268 | |||
269 | mutex_lock(&indio_dev->mlock); | ||
270 | |||
271 | ret = adis16201_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | ||
272 | if (!ret) { | ||
273 | if (val & ADIS16201_ERROR_ACTIVE) { | ||
274 | ret = adis16201_check_status(dev); | ||
275 | if (ret) | ||
276 | goto error_ret; | ||
277 | } | ||
278 | |||
279 | val = ((s16)(val << 2) >> 2); | ||
280 | ret = sprintf(buf, "%d\n", val); | ||
281 | } | ||
282 | |||
283 | error_ret: | ||
284 | mutex_unlock(&indio_dev->mlock); | ||
285 | |||
286 | return ret; | ||
287 | } | ||
288 | |||
289 | static ssize_t adis16201_write_16bit(struct device *dev, | ||
290 | struct device_attribute *attr, | ||
291 | const char *buf, | ||
292 | size_t len) | ||
293 | { | ||
294 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | ||
295 | int ret; | ||
296 | long val; | ||
297 | |||
298 | ret = strict_strtol(buf, 10, &val); | ||
299 | if (ret) | ||
300 | goto error_ret; | ||
301 | ret = adis16201_spi_write_reg_16(dev, this_attr->address, val); | ||
302 | |||
303 | error_ret: | ||
304 | return ret ? ret : len; | ||
305 | } | ||
306 | |||
307 | static int adis16201_reset(struct device *dev) | ||
308 | { | ||
309 | int ret; | ||
310 | ret = adis16201_spi_write_reg_8(dev, | ||
311 | ADIS16201_GLOB_CMD, | ||
312 | ADIS16201_GLOB_CMD_SW_RESET); | ||
313 | if (ret) | ||
314 | dev_err(dev, "problem resetting device"); | ||
315 | |||
316 | return ret; | ||
317 | } | ||
318 | |||
319 | static ssize_t adis16201_write_reset(struct device *dev, | ||
320 | struct device_attribute *attr, | ||
321 | const char *buf, size_t len) | ||
322 | { | ||
323 | if (len < 1) | ||
324 | return -EINVAL; | ||
325 | switch (buf[0]) { | ||
326 | case '1': | ||
327 | case 'y': | ||
328 | case 'Y': | ||
329 | return adis16201_reset(dev); | ||
330 | } | ||
331 | return -EINVAL; | ||
332 | } | ||
333 | |||
334 | int adis16201_set_irq(struct device *dev, bool enable) | ||
335 | { | ||
336 | int ret = 0; | ||
337 | u16 msc; | ||
338 | |||
339 | ret = adis16201_spi_read_reg_16(dev, ADIS16201_MSC_CTRL, &msc); | ||
340 | if (ret) | ||
341 | goto error_ret; | ||
342 | |||
343 | msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH; | ||
344 | msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1; | ||
345 | if (enable) | ||
346 | msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN; | ||
347 | else | ||
348 | msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN; | ||
349 | |||
350 | ret = adis16201_spi_write_reg_16(dev, ADIS16201_MSC_CTRL, msc); | ||
351 | |||
352 | error_ret: | ||
353 | return ret; | ||
354 | } | ||
355 | |||
356 | static int adis16201_check_status(struct device *dev) | ||
357 | { | ||
358 | u16 status; | ||
359 | int ret; | ||
360 | |||
361 | ret = adis16201_spi_read_reg_16(dev, ADIS16201_DIAG_STAT, &status); | ||
362 | if (ret < 0) { | ||
363 | dev_err(dev, "Reading status failed\n"); | ||
364 | goto error_ret; | ||
365 | } | ||
366 | ret = status & 0xF; | ||
367 | if (ret) | ||
368 | ret = -EFAULT; | ||
369 | |||
370 | if (status & ADIS16201_DIAG_STAT_SPI_FAIL) | ||
371 | dev_err(dev, "SPI failure\n"); | ||
372 | if (status & ADIS16201_DIAG_STAT_FLASH_UPT) | ||
373 | dev_err(dev, "Flash update failed\n"); | ||
374 | if (status & ADIS16201_DIAG_STAT_POWER_HIGH) | ||
375 | dev_err(dev, "Power supply above 3.625V\n"); | ||
376 | if (status & ADIS16201_DIAG_STAT_POWER_LOW) | ||
377 | dev_err(dev, "Power supply below 3.15V\n"); | ||
378 | |||
379 | error_ret: | ||
380 | return ret; | ||
381 | } | ||
382 | |||
383 | static int adis16201_self_test(struct device *dev) | ||
384 | { | ||
385 | int ret; | ||
386 | ret = adis16201_spi_write_reg_16(dev, | ||
387 | ADIS16201_MSC_CTRL, | ||
388 | ADIS16201_MSC_CTRL_SELF_TEST_EN); | ||
389 | if (ret) { | ||
390 | dev_err(dev, "problem starting self test"); | ||
391 | goto err_ret; | ||
392 | } | ||
393 | |||
394 | ret = adis16201_check_status(dev); | ||
395 | |||
396 | err_ret: | ||
397 | return ret; | ||
398 | } | ||
399 | |||
400 | static int adis16201_initial_setup(struct adis16201_state *st) | ||
401 | { | ||
402 | int ret; | ||
403 | struct device *dev = &st->indio_dev->dev; | ||
404 | |||
405 | /* Disable IRQ */ | ||
406 | ret = adis16201_set_irq(dev, false); | ||
407 | if (ret) { | ||
408 | dev_err(dev, "disable irq failed"); | ||
409 | goto err_ret; | ||
410 | } | ||
411 | |||
412 | /* Do self test */ | ||
413 | ret = adis16201_self_test(dev); | ||
414 | if (ret) { | ||
415 | dev_err(dev, "self test failure"); | ||
416 | goto err_ret; | ||
417 | } | ||
418 | |||
419 | /* Read status register to check the result */ | ||
420 | ret = adis16201_check_status(dev); | ||
421 | if (ret) { | ||
422 | adis16201_reset(dev); | ||
423 | dev_err(dev, "device not playing ball -> reset"); | ||
424 | msleep(ADIS16201_STARTUP_DELAY); | ||
425 | ret = adis16201_check_status(dev); | ||
426 | if (ret) { | ||
427 | dev_err(dev, "giving up"); | ||
428 | goto err_ret; | ||
429 | } | ||
430 | } | ||
431 | |||
432 | printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n", | ||
433 | st->us->chip_select, st->us->irq); | ||
434 | |||
435 | err_ret: | ||
436 | return ret; | ||
437 | } | ||
438 | |||
439 | static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16201_read_12bit_unsigned, | ||
440 | ADIS16201_SUPPLY_OUT); | ||
441 | static IIO_CONST_ATTR(in0_supply_scale, "0.00122"); | ||
442 | static IIO_DEV_ATTR_IN_RAW(1, adis16201_read_12bit_unsigned, | ||
443 | ADIS16201_AUX_ADC); | ||
444 | static IIO_CONST_ATTR(in1_scale, "0.00061"); | ||
445 | |||
446 | static IIO_DEV_ATTR_ACCEL_X(adis16201_read_14bit_signed, | ||
447 | ADIS16201_XACCL_OUT); | ||
448 | static IIO_DEV_ATTR_ACCEL_Y(adis16201_read_14bit_signed, | ||
449 | ADIS16201_YACCL_OUT); | ||
450 | static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO, | ||
451 | adis16201_read_12bit_signed, | ||
452 | adis16201_write_16bit, | ||
453 | ADIS16201_XACCL_OFFS); | ||
454 | static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO, | ||
455 | adis16201_read_12bit_signed, | ||
456 | adis16201_write_16bit, | ||
457 | ADIS16201_YACCL_OFFS); | ||
458 | static IIO_CONST_ATTR(accel_scale, "0.4625"); | ||
459 | |||
460 | static IIO_DEV_ATTR_INCLI_X(adis16201_read_14bit_signed, | ||
461 | ADIS16201_XINCL_OUT); | ||
462 | static IIO_DEV_ATTR_INCLI_Y(adis16201_read_14bit_signed, | ||
463 | ADIS16201_YINCL_OUT); | ||
464 | static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR | S_IRUGO, | ||
465 | adis16201_read_9bit_signed, | ||
466 | adis16201_write_16bit, | ||
467 | ADIS16201_XACCL_OFFS); | ||
468 | static IIO_DEV_ATTR_INCLI_Y_OFFSET(S_IWUSR | S_IRUGO, | ||
469 | adis16201_read_9bit_signed, | ||
470 | adis16201_write_16bit, | ||
471 | ADIS16201_YACCL_OFFS); | ||
472 | static IIO_CONST_ATTR(incli_scale, "0.1"); | ||
473 | |||
474 | static IIO_DEV_ATTR_TEMP_RAW(adis16201_read_temp); | ||
475 | static IIO_CONST_ATTR(temp_offset, "25"); | ||
476 | static IIO_CONST_ATTR(temp_scale, "-0.47"); | ||
477 | |||
478 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0); | ||
479 | |||
480 | static IIO_CONST_ATTR(name, "adis16201"); | ||
481 | |||
482 | static struct attribute *adis16201_event_attributes[] = { | ||
483 | NULL | ||
484 | }; | ||
485 | |||
486 | static struct attribute_group adis16201_event_attribute_group = { | ||
487 | .attrs = adis16201_event_attributes, | ||
488 | }; | ||
489 | |||
490 | static struct attribute *adis16201_attributes[] = { | ||
491 | &iio_dev_attr_in0_supply_raw.dev_attr.attr, | ||
492 | &iio_const_attr_in0_supply_scale.dev_attr.attr, | ||
493 | &iio_dev_attr_temp_raw.dev_attr.attr, | ||
494 | &iio_const_attr_temp_offset.dev_attr.attr, | ||
495 | &iio_const_attr_temp_scale.dev_attr.attr, | ||
496 | &iio_dev_attr_reset.dev_attr.attr, | ||
497 | &iio_const_attr_name.dev_attr.attr, | ||
498 | &iio_dev_attr_in1_raw.dev_attr.attr, | ||
499 | &iio_const_attr_in1_scale.dev_attr.attr, | ||
500 | &iio_dev_attr_accel_x_raw.dev_attr.attr, | ||
501 | &iio_dev_attr_accel_y_raw.dev_attr.attr, | ||
502 | &iio_dev_attr_accel_x_offset.dev_attr.attr, | ||
503 | &iio_dev_attr_accel_y_offset.dev_attr.attr, | ||
504 | &iio_const_attr_accel_scale.dev_attr.attr, | ||
505 | &iio_dev_attr_incli_x_raw.dev_attr.attr, | ||
506 | &iio_dev_attr_incli_y_raw.dev_attr.attr, | ||
507 | &iio_dev_attr_incli_x_offset.dev_attr.attr, | ||
508 | &iio_dev_attr_incli_y_offset.dev_attr.attr, | ||
509 | &iio_const_attr_incli_scale.dev_attr.attr, | ||
510 | NULL | ||
511 | }; | ||
512 | |||
513 | static const struct attribute_group adis16201_attribute_group = { | ||
514 | .attrs = adis16201_attributes, | ||
515 | }; | ||
516 | |||
517 | static int __devinit adis16201_probe(struct spi_device *spi) | ||
518 | { | ||
519 | int ret, regdone = 0; | ||
520 | struct adis16201_state *st = kzalloc(sizeof *st, GFP_KERNEL); | ||
521 | if (!st) { | ||
522 | ret = -ENOMEM; | ||
523 | goto error_ret; | ||
524 | } | ||
525 | /* this is only used for removal purposes */ | ||
526 | spi_set_drvdata(spi, st); | ||
527 | |||
528 | /* Allocate the comms buffers */ | ||
529 | st->rx = kzalloc(sizeof(*st->rx)*ADIS16201_MAX_RX, GFP_KERNEL); | ||
530 | if (st->rx == NULL) { | ||
531 | ret = -ENOMEM; | ||
532 | goto error_free_st; | ||
533 | } | ||
534 | st->tx = kzalloc(sizeof(*st->tx)*ADIS16201_MAX_TX, GFP_KERNEL); | ||
535 | if (st->tx == NULL) { | ||
536 | ret = -ENOMEM; | ||
537 | goto error_free_rx; | ||
538 | } | ||
539 | st->us = spi; | ||
540 | mutex_init(&st->buf_lock); | ||
541 | /* setup the industrialio driver allocated elements */ | ||
542 | st->indio_dev = iio_allocate_device(); | ||
543 | if (st->indio_dev == NULL) { | ||
544 | ret = -ENOMEM; | ||
545 | goto error_free_tx; | ||
546 | } | ||
547 | |||
548 | st->indio_dev->dev.parent = &spi->dev; | ||
549 | st->indio_dev->num_interrupt_lines = 1; | ||
550 | st->indio_dev->event_attrs = &adis16201_event_attribute_group; | ||
551 | st->indio_dev->attrs = &adis16201_attribute_group; | ||
552 | st->indio_dev->dev_data = (void *)(st); | ||
553 | st->indio_dev->driver_module = THIS_MODULE; | ||
554 | st->indio_dev->modes = INDIO_DIRECT_MODE; | ||
555 | |||
556 | ret = adis16201_configure_ring(st->indio_dev); | ||
557 | if (ret) | ||
558 | goto error_free_dev; | ||
559 | |||
560 | ret = iio_device_register(st->indio_dev); | ||
561 | if (ret) | ||
562 | goto error_unreg_ring_funcs; | ||
563 | regdone = 1; | ||
564 | |||
565 | ret = adis16201_initialize_ring(st->indio_dev->ring); | ||
566 | if (ret) { | ||
567 | printk(KERN_ERR "failed to initialize the ring\n"); | ||
568 | goto error_unreg_ring_funcs; | ||
569 | } | ||
570 | |||
571 | if (spi->irq) { | ||
572 | ret = iio_register_interrupt_line(spi->irq, | ||
573 | st->indio_dev, | ||
574 | 0, | ||
575 | IRQF_TRIGGER_RISING, | ||
576 | "adis16201"); | ||
577 | if (ret) | ||
578 | goto error_uninitialize_ring; | ||
579 | |||
580 | ret = adis16201_probe_trigger(st->indio_dev); | ||
581 | if (ret) | ||
582 | goto error_unregister_line; | ||
583 | } | ||
584 | |||
585 | /* Get the device into a sane initial state */ | ||
586 | ret = adis16201_initial_setup(st); | ||
587 | if (ret) | ||
588 | goto error_remove_trigger; | ||
589 | return 0; | ||
590 | |||
591 | error_remove_trigger: | ||
592 | adis16201_remove_trigger(st->indio_dev); | ||
593 | error_unregister_line: | ||
594 | if (spi->irq) | ||
595 | iio_unregister_interrupt_line(st->indio_dev, 0); | ||
596 | error_uninitialize_ring: | ||
597 | adis16201_uninitialize_ring(st->indio_dev->ring); | ||
598 | error_unreg_ring_funcs: | ||
599 | adis16201_unconfigure_ring(st->indio_dev); | ||
600 | error_free_dev: | ||
601 | if (regdone) | ||
602 | iio_device_unregister(st->indio_dev); | ||
603 | else | ||
604 | iio_free_device(st->indio_dev); | ||
605 | error_free_tx: | ||
606 | kfree(st->tx); | ||
607 | error_free_rx: | ||
608 | kfree(st->rx); | ||
609 | error_free_st: | ||
610 | kfree(st); | ||
611 | error_ret: | ||
612 | return ret; | ||
613 | } | ||
614 | |||
615 | static int adis16201_remove(struct spi_device *spi) | ||
616 | { | ||
617 | struct adis16201_state *st = spi_get_drvdata(spi); | ||
618 | struct iio_dev *indio_dev = st->indio_dev; | ||
619 | |||
620 | flush_scheduled_work(); | ||
621 | |||
622 | adis16201_remove_trigger(indio_dev); | ||
623 | if (spi->irq) | ||
624 | iio_unregister_interrupt_line(indio_dev, 0); | ||
625 | |||
626 | adis16201_uninitialize_ring(indio_dev->ring); | ||
627 | iio_device_unregister(indio_dev); | ||
628 | adis16201_unconfigure_ring(indio_dev); | ||
629 | kfree(st->tx); | ||
630 | kfree(st->rx); | ||
631 | kfree(st); | ||
632 | |||
633 | return 0; | ||
634 | } | ||
635 | |||
636 | static struct spi_driver adis16201_driver = { | ||
637 | .driver = { | ||
638 | .name = "adis16201", | ||
639 | .owner = THIS_MODULE, | ||
640 | }, | ||
641 | .probe = adis16201_probe, | ||
642 | .remove = __devexit_p(adis16201_remove), | ||
643 | }; | ||
644 | |||
645 | static __init int adis16201_init(void) | ||
646 | { | ||
647 | return spi_register_driver(&adis16201_driver); | ||
648 | } | ||
649 | module_init(adis16201_init); | ||
650 | |||
651 | static __exit void adis16201_exit(void) | ||
652 | { | ||
653 | spi_unregister_driver(&adis16201_driver); | ||
654 | } | ||
655 | module_exit(adis16201_exit); | ||
656 | |||
657 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); | ||
658 | MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver"); | ||
659 | MODULE_LICENSE("GPL v2"); | ||