/drivers/staging/iio/accel/adis16240_core.c
C | 647 lines | 535 code | 77 blank | 35 comment | 45 complexity | e6d2f492c9ea429ce0f44d046c9e026b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
1/*
2 * ADIS16240 Programmable Impact Sensor and Recorder 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 "../ring_generic.h"
24#include "accel.h"
25#include "../adc/adc.h"
26
27#include "adis16240.h"
28
29#define DRIVER_NAME "adis16240"
30
31static int adis16240_check_status(struct iio_dev *indio_dev);
32
33/**
34 * adis16240_spi_write_reg_8() - write single byte to a register
35 * @indio_dev: iio_dev associated with device
36 * @reg_address: the address of the register to be written
37 * @val: the value to write
38 **/
39static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
40 u8 reg_address,
41 u8 val)
42{
43 int ret;
44 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
45
46 mutex_lock(&st->buf_lock);
47 st->tx[0] = ADIS16240_WRITE_REG(reg_address);
48 st->tx[1] = val;
49
50 ret = spi_write(st->us, st->tx, 2);
51 mutex_unlock(&st->buf_lock);
52
53 return ret;
54}
55
56/**
57 * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
58 * @indio_dev: iio_dev for this device
59 * @reg_address: the address of the lower of the two registers. Second register
60 * is assumed to have address one greater.
61 * @val: value to be written
62 **/
63static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
64 u8 lower_reg_address,
65 u16 value)
66{
67 int ret;
68 struct spi_message msg;
69 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
70 struct spi_transfer xfers[] = {
71 {
72 .tx_buf = st->tx,
73 .bits_per_word = 8,
74 .len = 2,
75 .cs_change = 1,
76 .delay_usecs = 35,
77 }, {
78 .tx_buf = st->tx + 2,
79 .bits_per_word = 8,
80 .len = 2,
81 .delay_usecs = 35,
82 },
83 };
84
85 mutex_lock(&st->buf_lock);
86 st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
87 st->tx[1] = value & 0xFF;
88 st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
89 st->tx[3] = (value >> 8) & 0xFF;
90
91 spi_message_init(&msg);
92 spi_message_add_tail(&xfers[0], &msg);
93 spi_message_add_tail(&xfers[1], &msg);
94 ret = spi_sync(st->us, &msg);
95 mutex_unlock(&st->buf_lock);
96
97 return ret;
98}
99
100/**
101 * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @indio_dev: iio_dev for this device
103 * @reg_address: the address of the lower of the two registers. Second register
104 * is assumed to have address one greater.
105 * @val: somewhere to pass back the value read
106 **/
107static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
108 u8 lower_reg_address,
109 u16 *val)
110{
111 struct spi_message msg;
112 struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
113 int ret;
114 struct spi_transfer xfers[] = {
115 {
116 .tx_buf = st->tx,
117 .bits_per_word = 8,
118 .len = 2,
119 .cs_change = 1,
120 .delay_usecs = 35,
121 }, {
122 .rx_buf = st->rx,
123 .bits_per_word = 8,
124 .len = 2,
125 .cs_change = 1,
126 .delay_usecs = 35,
127 },
128 };
129
130 mutex_lock(&st->buf_lock);
131 st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
132 st->tx[1] = 0;
133 st->tx[2] = 0;
134 st->tx[3] = 0;
135
136 spi_message_init(&msg);
137 spi_message_add_tail(&xfers[0], &msg);
138 spi_message_add_tail(&xfers[1], &msg);
139 ret = spi_sync(st->us, &msg);
140 if (ret) {
141 dev_err(&st->us->dev,
142 "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
148error_ret:
149 mutex_unlock(&st->buf_lock);
150 return ret;
151}
152
153static ssize_t adis16240_spi_read_signed(struct device *dev,
154 struct device_attribute *attr,
155 char *buf,
156 unsigned bits)
157{
158 struct iio_dev *indio_dev = dev_get_drvdata(dev);
159 int ret;
160 s16 val = 0;
161 unsigned shift = 16 - bits;
162 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163
164 ret = adis16240_spi_read_reg_16(indio_dev,
165 this_attr->address, (u16 *)&val);
166 if (ret)
167 return ret;
168
169 if (val & ADIS16240_ERROR_ACTIVE)
170 adis16240_check_status(indio_dev);
171
172 val = ((s16)(val << shift) >> shift);
173 return sprintf(buf, "%d\n", val);
174}
175
176static ssize_t adis16240_read_12bit_signed(struct device *dev,
177 struct device_attribute *attr,
178 char *buf)
179{
180 ssize_t ret;
181 struct iio_dev *indio_dev = dev_get_drvdata(dev);
182
183 /* Take the iio_dev status lock */
184 mutex_lock(&indio_dev->mlock);
185 ret = adis16240_spi_read_signed(dev, attr, buf, 12);
186 mutex_unlock(&indio_dev->mlock);
187
188 return ret;
189}
190
191static int adis16240_reset(struct iio_dev *indio_dev)
192{
193 int ret;
194 ret = adis16240_spi_write_reg_8(indio_dev,
195 ADIS16240_GLOB_CMD,
196 ADIS16240_GLOB_CMD_SW_RESET);
197 if (ret)
198 dev_err(&indio_dev->dev, "problem resetting device");
199
200 return ret;
201}
202
203static ssize_t adis16240_write_reset(struct device *dev,
204 struct device_attribute *attr,
205 const char *buf, size_t len)
206{
207 struct iio_dev *indio_dev = dev_get_drvdata(dev);
208
209 if (len < 1)
210 return -EINVAL;
211 switch (buf[0]) {
212 case '1':
213 case 'y':
214 case 'Y':
215 return adis16240_reset(indio_dev);
216 }
217 return -EINVAL;
218}
219
220int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
221{
222 int ret = 0;
223 u16 msc;
224
225 ret = adis16240_spi_read_reg_16(indio_dev,
226 ADIS16240_MSC_CTRL, &msc);
227 if (ret)
228 goto error_ret;
229
230 msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
231 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
232 if (enable)
233 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
234 else
235 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
236
237 ret = adis16240_spi_write_reg_16(indio_dev,
238 ADIS16240_MSC_CTRL, msc);
239
240error_ret:
241 return ret;
242}
243
244static int adis16240_self_test(struct iio_dev *indio_dev)
245{
246 int ret;
247 ret = adis16240_spi_write_reg_16(indio_dev,
248 ADIS16240_MSC_CTRL,
249 ADIS16240_MSC_CTRL_SELF_TEST_EN);
250 if (ret) {
251 dev_err(&indio_dev->dev, "problem starting self test");
252 goto err_ret;
253 }
254
255 msleep(ADIS16240_STARTUP_DELAY);
256
257 adis16240_check_status(indio_dev);
258
259err_ret:
260 return ret;
261}
262
263static int adis16240_check_status(struct iio_dev *indio_dev)
264{
265 u16 status;
266 int ret;
267 struct device *dev = &indio_dev->dev;
268
269 ret = adis16240_spi_read_reg_16(indio_dev,
270 ADIS16240_DIAG_STAT, &status);
271
272 if (ret < 0) {
273 dev_err(dev, "Reading status failed\n");
274 goto error_ret;
275 }
276
277 ret = status & 0x2F;
278 if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
279 dev_err(dev, "Power-on, self-test fail\n");
280 if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
281 dev_err(dev, "SPI failure\n");
282 if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
283 dev_err(dev, "Flash update failed\n");
284 if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
285 dev_err(dev, "Power supply above 3.625V\n");
286 if (status & ADIS16240_DIAG_STAT_POWER_LOW)
287 dev_err(dev, "Power supply below 2.225V\n");
288
289error_ret:
290 return ret;
291}
292
293static int adis16240_initial_setup(struct iio_dev *indio_dev)
294{
295 int ret;
296 struct device *dev = &indio_dev->dev;
297
298 /* Disable IRQ */
299 ret = adis16240_set_irq(indio_dev, false);
300 if (ret) {
301 dev_err(dev, "disable irq failed");
302 goto err_ret;
303 }
304
305 /* Do self test */
306 ret = adis16240_self_test(indio_dev);
307 if (ret) {
308 dev_err(dev, "self test failure");
309 goto err_ret;
310 }
311
312 /* Read status register to check the result */
313 ret = adis16240_check_status(indio_dev);
314 if (ret) {
315 adis16240_reset(indio_dev);
316 dev_err(dev, "device not playing ball -> reset");
317 msleep(ADIS16240_STARTUP_DELAY);
318 ret = adis16240_check_status(indio_dev);
319 if (ret) {
320 dev_err(dev, "giving up");
321 goto err_ret;
322 }
323 }
324
325err_ret:
326 return ret;
327}
328
329static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
330 adis16240_read_12bit_signed, NULL,
331 ADIS16240_XYZPEAK_OUT);
332
333static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
334
335static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
336
337enum adis16240_chan {
338 in_supply,
339 in_aux,
340 accel_x,
341 accel_y,
342 accel_z,
343 temp,
344};
345
346static const u8 adis16240_addresses[6][3] = {
347 [in_supply] = { ADIS16240_SUPPLY_OUT },
348 [in_aux] = { ADIS16240_AUX_ADC },
349 [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF,
350 ADIS16240_XPEAK_OUT },
351 [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF,
352 ADIS16240_YPEAK_OUT },
353 [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF,
354 ADIS16240_ZPEAK_OUT },
355 [temp] = { ADIS16240_TEMP_OUT },
356};
357
358static int adis16240_read_raw(struct iio_dev *indio_dev,
359 struct iio_chan_spec const *chan,
360 int *val, int *val2,
361 long mask)
362{
363 int ret;
364 int bits;
365 u8 addr;
366 s16 val16;
367
368 switch (mask) {
369 case 0:
370 mutex_lock(&indio_dev->mlock);
371 addr = adis16240_addresses[chan->address][0];
372 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
373 if (ret)
374 return ret;
375
376 if (val16 & ADIS16240_ERROR_ACTIVE) {
377 ret = adis16240_check_status(indio_dev);
378 if (ret)
379 return ret;
380 }
381 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
382 if (chan->scan_type.sign == 's')
383 val16 = (s16)(val16 <<
384 (16 - chan->scan_type.realbits)) >>
385 (16 - chan->scan_type.realbits);
386 *val = val16;
387 mutex_unlock(&indio_dev->mlock);
388 return IIO_VAL_INT;
389 case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
390 case (1 << IIO_CHAN_INFO_SCALE_SHARED):
391 switch (chan->type) {
392 case IIO_IN:
393 *val = 0;
394 if (chan->channel == 0)
395 *val2 = 4880;
396 else
397 return -EINVAL;
398 return IIO_VAL_INT_PLUS_MICRO;
399 case IIO_TEMP:
400 *val = 0;
401 *val2 = 244000;
402 return IIO_VAL_INT_PLUS_MICRO;
403 case IIO_ACCEL:
404 *val = 0;
405 *val2 = 504062;
406 return IIO_VAL_INT_PLUS_MICRO;
407 default:
408 return -EINVAL;
409 }
410 break;
411 case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED):
412 *val = 6;
413 *val2 = 629295;
414 return IIO_VAL_INT_PLUS_MICRO;
415 case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
416 *val = 25;
417 return IIO_VAL_INT;
418 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
419 bits = 10;
420 mutex_lock(&indio_dev->mlock);
421 addr = adis16240_addresses[chan->address][1];
422 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
423 if (ret) {
424 mutex_unlock(&indio_dev->mlock);
425 return ret;
426 }
427 val16 &= (1 << bits) - 1;
428 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
429 *val = val16;
430 mutex_unlock(&indio_dev->mlock);
431 return IIO_VAL_INT;
432 case (1 << IIO_CHAN_INFO_PEAK_SEPARATE):
433 bits = 10;
434 mutex_lock(&indio_dev->mlock);
435 addr = adis16240_addresses[chan->address][2];
436 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
437 if (ret) {
438 mutex_unlock(&indio_dev->mlock);
439 return ret;
440 }
441 val16 &= (1 << bits) - 1;
442 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
443 *val = val16;
444 mutex_unlock(&indio_dev->mlock);
445 return IIO_VAL_INT;
446 }
447 return -EINVAL;
448}
449
450static int adis16240_write_raw(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan,
452 int val,
453 int val2,
454 long mask)
455{
456 int bits = 10;
457 s16 val16;
458 u8 addr;
459 switch (mask) {
460 case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
461 val16 = val & ((1 << bits) - 1);
462 addr = adis16240_addresses[chan->address][1];
463 return adis16240_spi_write_reg_16(indio_dev, addr, val16);
464 }
465 return -EINVAL;
466}
467
468static struct iio_chan_spec adis16240_channels[] = {
469 IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
470 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
471 in_supply, ADIS16240_SCAN_SUPPLY,
472 IIO_ST('u', 10, 16, 0), 0),
473 IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
474 0,
475 in_aux, ADIS16240_SCAN_AUX_ADC,
476 IIO_ST('u', 10, 16, 0), 0),
477 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
478 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
479 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
480 accel_x, ADIS16240_SCAN_ACC_X,
481 IIO_ST('s', 10, 16, 0), 0),
482 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
483 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
484 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
485 accel_y, ADIS16240_SCAN_ACC_Y,
486 IIO_ST('s', 10, 16, 0), 0),
487 IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z,
488 (1 << IIO_CHAN_INFO_SCALE_SHARED) |
489 (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
490 accel_z, ADIS16240_SCAN_ACC_Z,
491 IIO_ST('s', 10, 16, 0), 0),
492 IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
493 (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
494 temp, ADIS16240_SCAN_TEMP,
495 IIO_ST('u', 10, 16, 0), 0),
496 IIO_CHAN_SOFT_TIMESTAMP(6)
497};
498
499static struct attribute *adis16240_attributes[] = {
500 &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
501 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
502 &iio_dev_attr_reset.dev_attr.attr,
503 NULL
504};
505
506static const struct attribute_group adis16240_attribute_group = {
507 .attrs = adis16240_attributes,
508};
509
510static const struct iio_info adis16240_info = {
511 .attrs = &adis16240_attribute_group,
512 .read_raw = &adis16240_read_raw,
513 .write_raw = &adis16240_write_raw,
514 .driver_module = THIS_MODULE,
515};
516
517static int __devinit adis16240_probe(struct spi_device *spi)
518{
519 int ret, regdone = 0;
520 struct adis16240_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)*ADIS16240_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)*ADIS16240_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(0);
543 if (st->indio_dev == NULL) {
544 ret = -ENOMEM;
545 goto error_free_tx;
546 }
547
548 st->indio_dev->name = spi->dev.driver->name;
549 st->indio_dev->dev.parent = &spi->dev;
550 st->indio_dev->info = &adis16240_info;
551 st->indio_dev->channels = adis16240_channels;
552 st->indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
553 st->indio_dev->dev_data = (void *)(st);
554 st->indio_dev->modes = INDIO_DIRECT_MODE;
555
556 ret = adis16240_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 = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
566 adis16240_channels,
567 ARRAY_SIZE(adis16240_channels));
568 if (ret) {
569 printk(KERN_ERR "failed to initialize the ring\n");
570 goto error_unreg_ring_funcs;
571 }
572
573 if (spi->irq) {
574 ret = adis16240_probe_trigger(st->indio_dev);
575 if (ret)
576 goto error_uninitialize_ring;
577 }
578
579 /* Get the device into a sane initial state */
580 ret = adis16240_initial_setup(st->indio_dev);
581 if (ret)
582 goto error_remove_trigger;
583 return 0;
584
585error_remove_trigger:
586 adis16240_remove_trigger(st->indio_dev);
587error_uninitialize_ring:
588 iio_ring_buffer_unregister(st->indio_dev->ring);
589error_unreg_ring_funcs:
590 adis16240_unconfigure_ring(st->indio_dev);
591error_free_dev:
592 if (regdone)
593 iio_device_unregister(st->indio_dev);
594 else
595 iio_free_device(st->indio_dev);
596error_free_tx:
597 kfree(st->tx);
598error_free_rx:
599 kfree(st->rx);
600error_free_st:
601 kfree(st);
602error_ret:
603 return ret;
604}
605
606static int adis16240_remove(struct spi_device *spi)
607{
608 struct adis16240_state *st = spi_get_drvdata(spi);
609 struct iio_dev *indio_dev = st->indio_dev;
610
611 flush_scheduled_work();
612
613 adis16240_remove_trigger(indio_dev);
614 iio_ring_buffer_unregister(indio_dev->ring);
615 iio_device_unregister(indio_dev);
616 adis16240_unconfigure_ring(indio_dev);
617 kfree(st->tx);
618 kfree(st->rx);
619 kfree(st);
620
621 return 0;
622}
623
624static struct spi_driver adis16240_driver = {
625 .driver = {
626 .name = "adis16240",
627 .owner = THIS_MODULE,
628 },
629 .probe = adis16240_probe,
630 .remove = __devexit_p(adis16240_remove),
631};
632
633static __init int adis16240_init(void)
634{
635 return spi_register_driver(&adis16240_driver);
636}
637module_init(adis16240_init);
638
639static __exit void adis16240_exit(void)
640{
641 spi_unregister_driver(&adis16240_driver);
642}
643module_exit(adis16240_exit);
644
645MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
646MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
647MODULE_LICENSE("GPL v2");