Commit | Line | Data |
---|---|---|
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 | **/ | |
b155498b | 50 | static int adis16400_spi_write_reg_8(struct device *dev, |
a9d26f00 BS |
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 | **/ | |
75 | static 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 | **/ | |
119 | static 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 | ||
159 | error_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 | **/ | |
169 | int 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 | ||
a9d26f00 BS |
212 | static ssize_t adis16400_spi_read_signed(struct device *dev, |
213 | struct device_attribute *attr, | |
214 | char *buf, | |
215 | unsigned bits) | |
216 | { | |
217 | int ret; | |
218 | s16 val = 0; | |
219 | unsigned shift = 16 - bits; | |
220 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
221 | ||
222 | ret = adis16400_spi_read_reg_16(dev, this_attr->address, (u16 *)&val); | |
223 | if (ret) | |
224 | return ret; | |
225 | ||
226 | if (val & ADIS16400_ERROR_ACTIVE) | |
227 | adis16400_check_status(dev); | |
228 | val = ((s16)(val << shift) >> shift); | |
229 | return sprintf(buf, "%d\n", val); | |
230 | } | |
231 | ||
232 | static ssize_t adis16400_read_12bit_unsigned(struct device *dev, | |
233 | struct device_attribute *attr, | |
234 | char *buf) | |
235 | { | |
236 | int ret; | |
237 | u16 val = 0; | |
238 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
239 | ||
240 | ret = adis16400_spi_read_reg_16(dev, this_attr->address, &val); | |
241 | if (ret) | |
242 | return ret; | |
243 | ||
244 | if (val & ADIS16400_ERROR_ACTIVE) | |
245 | adis16400_check_status(dev); | |
246 | ||
247 | return sprintf(buf, "%u\n", val & 0x0FFF); | |
248 | } | |
249 | ||
250 | static ssize_t adis16400_read_14bit_signed(struct device *dev, | |
251 | struct device_attribute *attr, | |
252 | char *buf) | |
253 | { | |
254 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
255 | ssize_t ret; | |
256 | ||
257 | /* Take the iio_dev status lock */ | |
258 | mutex_lock(&indio_dev->mlock); | |
259 | ret = adis16400_spi_read_signed(dev, attr, buf, 14); | |
260 | mutex_unlock(&indio_dev->mlock); | |
261 | ||
262 | return ret; | |
263 | } | |
264 | ||
265 | static ssize_t adis16400_read_12bit_signed(struct device *dev, | |
266 | struct device_attribute *attr, | |
267 | char *buf) | |
268 | { | |
269 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
270 | ssize_t ret; | |
271 | ||
272 | /* Take the iio_dev status lock */ | |
273 | mutex_lock(&indio_dev->mlock); | |
274 | ret = adis16400_spi_read_signed(dev, attr, buf, 12); | |
275 | mutex_unlock(&indio_dev->mlock); | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
280 | ||
281 | static ssize_t adis16400_write_16bit(struct device *dev, | |
282 | struct device_attribute *attr, | |
283 | const char *buf, | |
284 | size_t len) | |
285 | { | |
286 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
287 | int ret; | |
288 | long val; | |
289 | ||
290 | ret = strict_strtol(buf, 10, &val); | |
291 | if (ret) | |
292 | goto error_ret; | |
293 | ret = adis16400_spi_write_reg_16(dev, this_attr->address, val); | |
294 | ||
295 | error_ret: | |
296 | return ret ? ret : len; | |
297 | } | |
298 | ||
299 | static ssize_t adis16400_read_frequency(struct device *dev, | |
300 | struct device_attribute *attr, | |
301 | char *buf) | |
302 | { | |
303 | int ret, len = 0; | |
304 | u16 t; | |
305 | int sps; | |
306 | ret = adis16400_spi_read_reg_16(dev, | |
307 | ADIS16400_SMPL_PRD, | |
308 | &t); | |
309 | if (ret) | |
310 | return ret; | |
311 | sps = (t & ADIS16400_SMPL_PRD_TIME_BASE) ? 53 : 1638; | |
312 | sps /= (t & ADIS16400_SMPL_PRD_DIV_MASK) + 1; | |
313 | len = sprintf(buf, "%d SPS\n", sps); | |
314 | return len; | |
315 | } | |
316 | ||
317 | static ssize_t adis16400_write_frequency(struct device *dev, | |
318 | struct device_attribute *attr, | |
319 | const char *buf, | |
320 | size_t len) | |
321 | { | |
322 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
323 | struct adis16400_state *st = iio_dev_get_devdata(indio_dev); | |
324 | long val; | |
325 | int ret; | |
326 | u8 t; | |
327 | ||
328 | ret = strict_strtol(buf, 10, &val); | |
329 | if (ret) | |
330 | return ret; | |
331 | ||
332 | mutex_lock(&indio_dev->mlock); | |
333 | ||
334 | t = (1638 / val); | |
335 | if (t > 0) | |
336 | t--; | |
337 | t &= ADIS16400_SMPL_PRD_DIV_MASK; | |
338 | if ((t & ADIS16400_SMPL_PRD_DIV_MASK) >= 0x0A) | |
339 | st->us->max_speed_hz = ADIS16400_SPI_SLOW; | |
340 | else | |
341 | st->us->max_speed_hz = ADIS16400_SPI_FAST; | |
342 | ||
343 | ret = adis16400_spi_write_reg_8(dev, | |
344 | ADIS16400_SMPL_PRD, | |
345 | t); | |
346 | ||
347 | mutex_unlock(&indio_dev->mlock); | |
348 | ||
349 | return ret ? ret : len; | |
350 | } | |
351 | ||
352 | static ssize_t adis16400_write_reset(struct device *dev, | |
353 | struct device_attribute *attr, | |
354 | const char *buf, size_t len) | |
355 | { | |
356 | if (len < 1) | |
357 | return -1; | |
358 | switch (buf[0]) { | |
359 | case '1': | |
360 | case 'y': | |
361 | case 'Y': | |
362 | return adis16400_reset(dev); | |
363 | } | |
364 | return -1; | |
365 | } | |
366 | ||
367 | ||
368 | ||
369 | int adis16400_set_irq(struct device *dev, bool enable) | |
370 | { | |
371 | int ret; | |
372 | u16 msc; | |
373 | ret = adis16400_spi_read_reg_16(dev, ADIS16400_MSC_CTRL, &msc); | |
374 | if (ret) | |
375 | goto error_ret; | |
376 | ||
377 | msc |= ADIS16400_MSC_CTRL_DATA_RDY_POL_HIGH; | |
378 | if (enable) | |
379 | msc |= ADIS16400_MSC_CTRL_DATA_RDY_EN; | |
380 | else | |
381 | msc &= ~ADIS16400_MSC_CTRL_DATA_RDY_EN; | |
382 | ||
383 | ret = adis16400_spi_write_reg_16(dev, ADIS16400_MSC_CTRL, msc); | |
384 | if (ret) | |
385 | goto error_ret; | |
386 | ||
387 | error_ret: | |
388 | return ret; | |
389 | } | |
390 | ||
391 | int adis16400_reset(struct device *dev) | |
392 | { | |
393 | int ret; | |
394 | ret = adis16400_spi_write_reg_8(dev, | |
395 | ADIS16400_GLOB_CMD, | |
396 | ADIS16400_GLOB_CMD_SW_RESET); | |
397 | if (ret) | |
398 | dev_err(dev, "problem resetting device"); | |
399 | ||
400 | return ret; | |
401 | } | |
402 | ||
403 | /* Power down the device */ | |
b155498b | 404 | static int adis16400_stop_device(struct device *dev) |
a9d26f00 BS |
405 | { |
406 | int ret; | |
407 | u16 val = ADIS16400_SLP_CNT_POWER_OFF; | |
408 | ||
409 | ret = adis16400_spi_write_reg_16(dev, ADIS16400_SLP_CNT, val); | |
410 | if (ret) | |
411 | dev_err(dev, "problem with turning device off: SLP_CNT"); | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
b155498b | 416 | static int adis16400_self_test(struct device *dev) |
a9d26f00 BS |
417 | { |
418 | int ret; | |
419 | ret = adis16400_spi_write_reg_16(dev, | |
420 | ADIS16400_MSC_CTRL, | |
421 | ADIS16400_MSC_CTRL_MEM_TEST); | |
422 | if (ret) { | |
423 | dev_err(dev, "problem starting self test"); | |
424 | goto err_ret; | |
425 | } | |
426 | ||
427 | adis16400_check_status(dev); | |
428 | ||
429 | err_ret: | |
430 | return ret; | |
431 | } | |
432 | ||
433 | int adis16400_check_status(struct device *dev) | |
434 | { | |
435 | u16 status; | |
436 | int ret; | |
437 | ||
438 | ret = adis16400_spi_read_reg_16(dev, ADIS16400_DIAG_STAT, &status); | |
439 | ||
440 | if (ret < 0) { | |
441 | dev_err(dev, "Reading status failed\n"); | |
442 | goto error_ret; | |
443 | } | |
444 | ret = status; | |
445 | if (status & ADIS16400_DIAG_STAT_ZACCL_FAIL) | |
446 | dev_err(dev, "Z-axis accelerometer self-test failure\n"); | |
447 | if (status & ADIS16400_DIAG_STAT_YACCL_FAIL) | |
448 | dev_err(dev, "Y-axis accelerometer self-test failure\n"); | |
449 | if (status & ADIS16400_DIAG_STAT_XACCL_FAIL) | |
450 | dev_err(dev, "X-axis accelerometer self-test failure\n"); | |
451 | if (status & ADIS16400_DIAG_STAT_XGYRO_FAIL) | |
452 | dev_err(dev, "X-axis gyroscope self-test failure\n"); | |
453 | if (status & ADIS16400_DIAG_STAT_YGYRO_FAIL) | |
454 | dev_err(dev, "Y-axis gyroscope self-test failure\n"); | |
455 | if (status & ADIS16400_DIAG_STAT_ZGYRO_FAIL) | |
456 | dev_err(dev, "Z-axis gyroscope self-test failure\n"); | |
457 | if (status & ADIS16400_DIAG_STAT_ALARM2) | |
458 | dev_err(dev, "Alarm 2 active\n"); | |
459 | if (status & ADIS16400_DIAG_STAT_ALARM1) | |
460 | dev_err(dev, "Alarm 1 active\n"); | |
461 | if (status & ADIS16400_DIAG_STAT_FLASH_CHK) | |
462 | dev_err(dev, "Flash checksum error\n"); | |
463 | if (status & ADIS16400_DIAG_STAT_SELF_TEST) | |
464 | dev_err(dev, "Self test error\n"); | |
465 | if (status & ADIS16400_DIAG_STAT_OVERFLOW) | |
466 | dev_err(dev, "Sensor overrange\n"); | |
467 | if (status & ADIS16400_DIAG_STAT_SPI_FAIL) | |
468 | dev_err(dev, "SPI failure\n"); | |
469 | if (status & ADIS16400_DIAG_STAT_FLASH_UPT) | |
470 | dev_err(dev, "Flash update failed\n"); | |
471 | if (status & ADIS16400_DIAG_STAT_POWER_HIGH) | |
472 | dev_err(dev, "Power supply above 5.25V\n"); | |
473 | if (status & ADIS16400_DIAG_STAT_POWER_LOW) | |
474 | dev_err(dev, "Power supply below 4.75V\n"); | |
475 | ||
476 | error_ret: | |
477 | return ret; | |
478 | } | |
479 | ||
480 | static int adis16400_initial_setup(struct adis16400_state *st) | |
481 | { | |
482 | int ret; | |
483 | u16 prod_id, smp_prd; | |
484 | struct device *dev = &st->indio_dev->dev; | |
485 | ||
486 | /* use low spi speed for init */ | |
487 | st->us->max_speed_hz = ADIS16400_SPI_SLOW; | |
488 | st->us->mode = SPI_MODE_3; | |
489 | spi_setup(st->us); | |
490 | ||
491 | /* Disable IRQ */ | |
492 | ret = adis16400_set_irq(dev, false); | |
493 | if (ret) { | |
494 | dev_err(dev, "disable irq failed"); | |
495 | goto err_ret; | |
496 | } | |
497 | ||
498 | /* Do self test */ | |
499 | ||
500 | /* Read status register to check the result */ | |
501 | ret = adis16400_check_status(dev); | |
502 | if (ret) { | |
503 | adis16400_reset(dev); | |
504 | dev_err(dev, "device not playing ball -> reset"); | |
505 | msleep(ADIS16400_STARTUP_DELAY); | |
506 | ret = adis16400_check_status(dev); | |
507 | if (ret) { | |
508 | dev_err(dev, "giving up"); | |
509 | goto err_ret; | |
510 | } | |
511 | } | |
512 | ||
513 | ret = adis16400_spi_read_reg_16(dev, ADIS16400_PRODUCT_ID, &prod_id); | |
514 | if (ret) | |
515 | goto err_ret; | |
516 | ||
517 | if (prod_id != ADIS16400_PRODUCT_ID_DEFAULT) | |
518 | dev_warn(dev, "unknown product id"); | |
519 | ||
520 | printk(KERN_INFO DRIVER_NAME ": prod_id 0x%04x at CS%d (irq %d)\n", | |
521 | prod_id, st->us->chip_select, st->us->irq); | |
522 | ||
523 | /* use high spi speed if possible */ | |
524 | ret = adis16400_spi_read_reg_16(dev, ADIS16400_SMPL_PRD, &smp_prd); | |
525 | if (!ret && (smp_prd & ADIS16400_SMPL_PRD_DIV_MASK) < 0x0A) { | |
526 | st->us->max_speed_hz = ADIS16400_SPI_SLOW; | |
527 | spi_setup(st->us); | |
528 | } | |
529 | ||
530 | ||
531 | err_ret: | |
532 | ||
533 | return ret; | |
534 | } | |
535 | ||
536 | static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO, | |
537 | adis16400_read_12bit_signed, | |
538 | adis16400_write_16bit, | |
539 | ADIS16400_XACCL_OFF); | |
540 | ||
541 | static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO, | |
542 | adis16400_read_12bit_signed, | |
543 | adis16400_write_16bit, | |
544 | ADIS16400_YACCL_OFF); | |
545 | ||
546 | static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO, | |
547 | adis16400_read_12bit_signed, | |
548 | adis16400_write_16bit, | |
549 | ADIS16400_ZACCL_OFF); | |
550 | ||
551 | static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16400_read_14bit_signed, | |
552 | ADIS16400_SUPPLY_OUT); | |
553 | static IIO_CONST_ATTR(in_supply_scale, "0.002418"); | |
554 | ||
555 | static IIO_DEV_ATTR_GYRO_X(adis16400_read_14bit_signed, | |
556 | ADIS16400_XGYRO_OUT); | |
557 | static IIO_DEV_ATTR_GYRO_Y(adis16400_read_14bit_signed, | |
558 | ADIS16400_YGYRO_OUT); | |
559 | static IIO_DEV_ATTR_GYRO_Z(adis16400_read_14bit_signed, | |
560 | ADIS16400_ZGYRO_OUT); | |
561 | static IIO_CONST_ATTR(gyro_scale, "0.05 deg/s"); | |
562 | ||
563 | static IIO_DEV_ATTR_ACCEL_X(adis16400_read_14bit_signed, | |
564 | ADIS16400_XACCL_OUT); | |
565 | static IIO_DEV_ATTR_ACCEL_Y(adis16400_read_14bit_signed, | |
566 | ADIS16400_YACCL_OUT); | |
567 | static IIO_DEV_ATTR_ACCEL_Z(adis16400_read_14bit_signed, | |
568 | ADIS16400_ZACCL_OUT); | |
569 | static IIO_CONST_ATTR(accel_scale, "0.00333 g"); | |
570 | ||
571 | static IIO_DEV_ATTR_MAGN_X(adis16400_read_14bit_signed, | |
572 | ADIS16400_XMAGN_OUT); | |
573 | static IIO_DEV_ATTR_MAGN_Y(adis16400_read_14bit_signed, | |
574 | ADIS16400_YMAGN_OUT); | |
575 | static IIO_DEV_ATTR_MAGN_Z(adis16400_read_14bit_signed, | |
576 | ADIS16400_ZMAGN_OUT); | |
577 | static IIO_CONST_ATTR(magn_scale, "0.0005 Gs"); | |
578 | ||
579 | ||
a1169c5a | 580 | static IIO_DEV_ATTR_TEMP_RAW(adis16400_read_12bit_signed); |
a9d26f00 BS |
581 | static IIO_CONST_ATTR(temp_offset, "198.16 K"); |
582 | static IIO_CONST_ATTR(temp_scale, "0.14 K"); | |
583 | ||
584 | static IIO_DEV_ATTR_IN_RAW(0, adis16400_read_12bit_unsigned, | |
585 | ADIS16400_AUX_ADC); | |
586 | static IIO_CONST_ATTR(in0_scale, "0.000806"); | |
587 | ||
588 | static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, | |
589 | adis16400_read_frequency, | |
590 | adis16400_write_frequency); | |
591 | ||
592 | static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16400_write_reset, 0); | |
593 | ||
594 | static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("409 546 819 1638"); | |
595 | ||
596 | static IIO_CONST_ATTR(name, "adis16400"); | |
597 | ||
598 | static struct attribute *adis16400_event_attributes[] = { | |
599 | NULL | |
600 | }; | |
601 | ||
602 | static struct attribute_group adis16400_event_attribute_group = { | |
603 | .attrs = adis16400_event_attributes, | |
604 | }; | |
605 | ||
606 | static struct attribute *adis16400_attributes[] = { | |
607 | &iio_dev_attr_accel_x_offset.dev_attr.attr, | |
608 | &iio_dev_attr_accel_y_offset.dev_attr.attr, | |
609 | &iio_dev_attr_accel_z_offset.dev_attr.attr, | |
610 | &iio_dev_attr_in_supply_raw.dev_attr.attr, | |
611 | &iio_const_attr_in_supply_scale.dev_attr.attr, | |
a1169c5a JC |
612 | &iio_dev_attr_gyro_x_raw.dev_attr.attr, |
613 | &iio_dev_attr_gyro_y_raw.dev_attr.attr, | |
614 | &iio_dev_attr_gyro_z_raw.dev_attr.attr, | |
a9d26f00 BS |
615 | &iio_const_attr_gyro_scale.dev_attr.attr, |
616 | &iio_dev_attr_accel_x_raw.dev_attr.attr, | |
617 | &iio_dev_attr_accel_y_raw.dev_attr.attr, | |
618 | &iio_dev_attr_accel_z_raw.dev_attr.attr, | |
619 | &iio_const_attr_accel_scale.dev_attr.attr, | |
a1169c5a JC |
620 | &iio_dev_attr_magn_x_raw.dev_attr.attr, |
621 | &iio_dev_attr_magn_y_raw.dev_attr.attr, | |
622 | &iio_dev_attr_magn_z_raw.dev_attr.attr, | |
a9d26f00 | 623 | &iio_const_attr_magn_scale.dev_attr.attr, |
a1169c5a | 624 | &iio_dev_attr_temp_raw.dev_attr.attr, |
a9d26f00 BS |
625 | &iio_const_attr_temp_offset.dev_attr.attr, |
626 | &iio_const_attr_temp_scale.dev_attr.attr, | |
627 | &iio_dev_attr_in0_raw.dev_attr.attr, | |
628 | &iio_const_attr_in0_scale.dev_attr.attr, | |
629 | &iio_dev_attr_sampling_frequency.dev_attr.attr, | |
630 | &iio_const_attr_available_sampling_frequency.dev_attr.attr, | |
631 | &iio_dev_attr_reset.dev_attr.attr, | |
632 | &iio_const_attr_name.dev_attr.attr, | |
633 | NULL | |
634 | }; | |
635 | ||
636 | static const struct attribute_group adis16400_attribute_group = { | |
637 | .attrs = adis16400_attributes, | |
638 | }; | |
639 | ||
640 | static int __devinit adis16400_probe(struct spi_device *spi) | |
641 | { | |
642 | int ret, regdone = 0; | |
643 | struct adis16400_state *st = kzalloc(sizeof *st, GFP_KERNEL); | |
644 | if (!st) { | |
645 | ret = -ENOMEM; | |
646 | goto error_ret; | |
647 | } | |
648 | /* this is only used for removal purposes */ | |
649 | spi_set_drvdata(spi, st); | |
650 | ||
651 | /* Allocate the comms buffers */ | |
652 | st->rx = kzalloc(sizeof(*st->rx)*ADIS16400_MAX_RX, GFP_KERNEL); | |
653 | if (st->rx == NULL) { | |
654 | ret = -ENOMEM; | |
655 | goto error_free_st; | |
656 | } | |
657 | st->tx = kzalloc(sizeof(*st->tx)*ADIS16400_MAX_TX, GFP_KERNEL); | |
658 | if (st->tx == NULL) { | |
659 | ret = -ENOMEM; | |
660 | goto error_free_rx; | |
661 | } | |
662 | st->us = spi; | |
663 | mutex_init(&st->buf_lock); | |
664 | /* setup the industrialio driver allocated elements */ | |
665 | st->indio_dev = iio_allocate_device(); | |
666 | if (st->indio_dev == NULL) { | |
667 | ret = -ENOMEM; | |
668 | goto error_free_tx; | |
669 | } | |
670 | ||
671 | st->indio_dev->dev.parent = &spi->dev; | |
672 | st->indio_dev->num_interrupt_lines = 1; | |
673 | st->indio_dev->event_attrs = &adis16400_event_attribute_group; | |
674 | st->indio_dev->attrs = &adis16400_attribute_group; | |
675 | st->indio_dev->dev_data = (void *)(st); | |
676 | st->indio_dev->driver_module = THIS_MODULE; | |
677 | st->indio_dev->modes = INDIO_DIRECT_MODE; | |
678 | ||
679 | ret = adis16400_configure_ring(st->indio_dev); | |
680 | if (ret) | |
681 | goto error_free_dev; | |
682 | ||
683 | ret = iio_device_register(st->indio_dev); | |
684 | if (ret) | |
685 | goto error_unreg_ring_funcs; | |
686 | regdone = 1; | |
687 | ||
688 | ret = adis16400_initialize_ring(st->indio_dev->ring); | |
689 | if (ret) { | |
690 | printk(KERN_ERR "failed to initialize the ring\n"); | |
691 | goto error_unreg_ring_funcs; | |
692 | } | |
693 | ||
694 | if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) { | |
695 | #if 0 /* fixme: here we should support */ | |
696 | iio_init_work_cont(&st->work_cont_thresh, | |
697 | NULL, | |
698 | adis16400_thresh_handler_bh_no_check, | |
699 | 0, | |
700 | 0, | |
701 | st); | |
702 | #endif | |
703 | ret = iio_register_interrupt_line(spi->irq, | |
704 | st->indio_dev, | |
705 | 0, | |
706 | IRQF_TRIGGER_RISING, | |
707 | "adis16400"); | |
708 | if (ret) | |
709 | goto error_uninitialize_ring; | |
710 | ||
711 | ret = adis16400_probe_trigger(st->indio_dev); | |
712 | if (ret) | |
713 | goto error_unregister_line; | |
714 | } | |
715 | ||
716 | /* Get the device into a sane initial state */ | |
717 | ret = adis16400_initial_setup(st); | |
718 | if (ret) | |
719 | goto error_remove_trigger; | |
720 | return 0; | |
721 | ||
722 | error_remove_trigger: | |
723 | if (st->indio_dev->modes & INDIO_RING_TRIGGERED) | |
724 | adis16400_remove_trigger(st->indio_dev); | |
725 | error_unregister_line: | |
726 | if (st->indio_dev->modes & INDIO_RING_TRIGGERED) | |
727 | iio_unregister_interrupt_line(st->indio_dev, 0); | |
728 | error_uninitialize_ring: | |
729 | adis16400_uninitialize_ring(st->indio_dev->ring); | |
730 | error_unreg_ring_funcs: | |
731 | adis16400_unconfigure_ring(st->indio_dev); | |
732 | error_free_dev: | |
733 | if (regdone) | |
734 | iio_device_unregister(st->indio_dev); | |
735 | else | |
736 | iio_free_device(st->indio_dev); | |
737 | error_free_tx: | |
738 | kfree(st->tx); | |
739 | error_free_rx: | |
740 | kfree(st->rx); | |
741 | error_free_st: | |
742 | kfree(st); | |
743 | error_ret: | |
744 | return ret; | |
745 | } | |
746 | ||
747 | /* fixme, confirm ordering in this function */ | |
748 | static int adis16400_remove(struct spi_device *spi) | |
749 | { | |
750 | int ret; | |
751 | struct adis16400_state *st = spi_get_drvdata(spi); | |
752 | struct iio_dev *indio_dev = st->indio_dev; | |
753 | ||
754 | ret = adis16400_stop_device(&(indio_dev->dev)); | |
755 | if (ret) | |
756 | goto err_ret; | |
757 | ||
758 | flush_scheduled_work(); | |
759 | ||
760 | adis16400_remove_trigger(indio_dev); | |
761 | if (spi->irq && gpio_is_valid(irq_to_gpio(spi->irq)) > 0) | |
762 | iio_unregister_interrupt_line(indio_dev, 0); | |
763 | ||
764 | adis16400_uninitialize_ring(indio_dev->ring); | |
765 | adis16400_unconfigure_ring(indio_dev); | |
766 | iio_device_unregister(indio_dev); | |
767 | kfree(st->tx); | |
768 | kfree(st->rx); | |
769 | kfree(st); | |
770 | ||
771 | return 0; | |
772 | ||
773 | err_ret: | |
774 | return ret; | |
775 | } | |
776 | ||
777 | static struct spi_driver adis16400_driver = { | |
778 | .driver = { | |
779 | .name = "adis16400", | |
780 | .owner = THIS_MODULE, | |
781 | }, | |
782 | .probe = adis16400_probe, | |
783 | .remove = __devexit_p(adis16400_remove), | |
784 | }; | |
785 | ||
786 | static __init int adis16400_init(void) | |
787 | { | |
788 | return spi_register_driver(&adis16400_driver); | |
789 | } | |
790 | module_init(adis16400_init); | |
791 | ||
792 | static __exit void adis16400_exit(void) | |
793 | { | |
794 | spi_unregister_driver(&adis16400_driver); | |
795 | } | |
796 | module_exit(adis16400_exit); | |
797 | ||
798 | MODULE_AUTHOR("Manuel Stahl <manuel.stahl@iis.fraunhofer.de>"); | |
799 | MODULE_DESCRIPTION("Analog Devices ADIS16400/5 IMU SPI driver"); | |
800 | MODULE_LICENSE("GPL v2"); |