GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / iio / accel / adis16220_core.c
blobab2d5fa0349ff7d2c115efb35a938989cac55d2c
1 /*
2 * ADIS16220 Programmable Digital Vibration Sensor driver
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "accel.h"
24 #include "../adc/adc.h"
26 #include "adis16220.h"
28 #define DRIVER_NAME "adis16220"
30 /**
31 * adis16220_spi_write_reg_8() - write single byte to a register
32 * @dev: device associated with child of actual device (iio_dev or iio_trig)
33 * @reg_address: the address of the register to be written
34 * @val: the value to write
35 **/
36 static int adis16220_spi_write_reg_8(struct device *dev,
37 u8 reg_address,
38 u8 val)
40 int ret;
41 struct iio_dev *indio_dev = dev_get_drvdata(dev);
42 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
44 mutex_lock(&st->buf_lock);
45 st->tx[0] = ADIS16220_WRITE_REG(reg_address);
46 st->tx[1] = val;
48 ret = spi_write(st->us, st->tx, 2);
49 mutex_unlock(&st->buf_lock);
51 return ret;
54 /**
55 * adis16220_spi_write_reg_16() - write 2 bytes to a pair of registers
56 * @dev: device associated with child of actual device (iio_dev or iio_trig)
57 * @reg_address: the address of the lower of the two registers. Second register
58 * is assumed to have address one greater.
59 * @val: value to be written
60 **/
61 static int adis16220_spi_write_reg_16(struct device *dev,
62 u8 lower_reg_address,
63 u16 value)
65 int ret;
66 struct spi_message msg;
67 struct iio_dev *indio_dev = dev_get_drvdata(dev);
68 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
69 struct spi_transfer xfers[] = {
71 .tx_buf = st->tx,
72 .bits_per_word = 8,
73 .len = 2,
74 .cs_change = 1,
75 .delay_usecs = 35,
76 }, {
77 .tx_buf = st->tx + 2,
78 .bits_per_word = 8,
79 .len = 2,
80 .cs_change = 1,
81 .delay_usecs = 35,
85 mutex_lock(&st->buf_lock);
86 st->tx[0] = ADIS16220_WRITE_REG(lower_reg_address);
87 st->tx[1] = value & 0xFF;
88 st->tx[2] = ADIS16220_WRITE_REG(lower_reg_address + 1);
89 st->tx[3] = (value >> 8) & 0xFF;
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);
97 return ret;
101 * adis16220_spi_read_reg_16() - read 2 bytes from a 16-bit register
102 * @dev: device associated with child of actual device (iio_dev or iio_trig)
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
107 static int adis16220_spi_read_reg_16(struct device *dev,
108 u8 lower_reg_address,
109 u16 *val)
111 struct spi_message msg;
112 struct iio_dev *indio_dev = dev_get_drvdata(dev);
113 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
114 int ret;
115 struct spi_transfer xfers[] = {
117 .tx_buf = st->tx,
118 .bits_per_word = 8,
119 .len = 2,
120 .cs_change = 1,
121 .delay_usecs = 35,
122 }, {
123 .rx_buf = st->rx,
124 .bits_per_word = 8,
125 .len = 2,
126 .cs_change = 1,
127 .delay_usecs = 35,
131 mutex_lock(&st->buf_lock);
132 st->tx[0] = ADIS16220_READ_REG(lower_reg_address);
133 st->tx[1] = 0;
135 spi_message_init(&msg);
136 spi_message_add_tail(&xfers[0], &msg);
137 spi_message_add_tail(&xfers[1], &msg);
138 ret = spi_sync(st->us, &msg);
139 if (ret) {
140 dev_err(&st->us->dev,
141 "problem when reading 16 bit register 0x%02X",
142 lower_reg_address);
143 goto error_ret;
145 *val = (st->rx[0] << 8) | st->rx[1];
147 error_ret:
148 mutex_unlock(&st->buf_lock);
149 return ret;
152 static ssize_t adis16220_spi_read_signed(struct device *dev,
153 struct device_attribute *attr,
154 char *buf,
155 unsigned bits)
157 int ret;
158 s16 val = 0;
159 unsigned shift = 16 - bits;
160 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
162 ret = adis16220_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
163 if (ret)
164 return ret;
166 val = ((s16)(val << shift) >> shift);
167 return sprintf(buf, "%d\n", val);
170 static ssize_t adis16220_read_12bit_unsigned(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
174 int ret;
175 u16 val = 0;
176 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
178 ret = adis16220_spi_read_reg_16(dev, this_attr->address, &val);
179 if (ret)
180 return ret;
182 return sprintf(buf, "%u\n", val & 0x0FFF);
185 static ssize_t adis16220_read_16bit(struct device *dev,
186 struct device_attribute *attr,
187 char *buf)
189 struct iio_dev *indio_dev = dev_get_drvdata(dev);
190 ssize_t ret;
192 /* Take the iio_dev status lock */
193 mutex_lock(&indio_dev->mlock);
194 ret = adis16220_spi_read_signed(dev, attr, buf, 16);
195 mutex_unlock(&indio_dev->mlock);
197 return ret;
200 static ssize_t adis16220_write_16bit(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf,
203 size_t len)
205 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
206 int ret;
207 long val;
209 ret = strict_strtol(buf, 10, &val);
210 if (ret)
211 goto error_ret;
212 ret = adis16220_spi_write_reg_16(dev, this_attr->address, val);
214 error_ret:
215 return ret ? ret : len;
218 static int adis16220_capture(struct device *dev)
220 int ret;
221 ret = adis16220_spi_write_reg_16(dev,
222 ADIS16220_GLOB_CMD,
223 0xBF08); /* initiates a manual data capture */
224 if (ret)
225 dev_err(dev, "problem beginning capture");
227 msleep(10); /* delay for capture to finish */
229 return ret;
232 static int adis16220_reset(struct device *dev)
234 int ret;
235 ret = adis16220_spi_write_reg_8(dev,
236 ADIS16220_GLOB_CMD,
237 ADIS16220_GLOB_CMD_SW_RESET);
238 if (ret)
239 dev_err(dev, "problem resetting device");
241 return ret;
244 static ssize_t adis16220_write_reset(struct device *dev,
245 struct device_attribute *attr,
246 const char *buf, size_t len)
248 if (len < 1)
249 return -1;
250 switch (buf[0]) {
251 case '1':
252 case 'y':
253 case 'Y':
254 return adis16220_reset(dev) == 0 ? len : -EIO;
256 return -1;
259 static ssize_t adis16220_write_capture(struct device *dev,
260 struct device_attribute *attr,
261 const char *buf, size_t len)
263 if (len < 1)
264 return -1;
265 switch (buf[0]) {
266 case '1':
267 case 'y':
268 case 'Y':
269 return adis16220_capture(dev) == 0 ? len : -EIO;
271 return -1;
274 static int adis16220_check_status(struct device *dev)
276 u16 status;
277 int ret;
279 ret = adis16220_spi_read_reg_16(dev, ADIS16220_DIAG_STAT, &status);
281 if (ret < 0) {
282 dev_err(dev, "Reading status failed\n");
283 goto error_ret;
285 ret = status & 0x7F;
287 if (status & ADIS16220_DIAG_STAT_VIOLATION)
288 dev_err(dev, "Capture period violation/interruption\n");
289 if (status & ADIS16220_DIAG_STAT_SPI_FAIL)
290 dev_err(dev, "SPI failure\n");
291 if (status & ADIS16220_DIAG_STAT_FLASH_UPT)
292 dev_err(dev, "Flash update failed\n");
293 if (status & ADIS16220_DIAG_STAT_POWER_HIGH)
294 dev_err(dev, "Power supply above 3.625V\n");
295 if (status & ADIS16220_DIAG_STAT_POWER_LOW)
296 dev_err(dev, "Power supply below 3.15V\n");
298 error_ret:
299 return ret;
302 static int adis16220_self_test(struct device *dev)
304 int ret;
305 ret = adis16220_spi_write_reg_16(dev,
306 ADIS16220_MSC_CTRL,
307 ADIS16220_MSC_CTRL_SELF_TEST_EN);
308 if (ret) {
309 dev_err(dev, "problem starting self test");
310 goto err_ret;
313 adis16220_check_status(dev);
315 err_ret:
316 return ret;
319 static int adis16220_initial_setup(struct adis16220_state *st)
321 int ret;
322 struct device *dev = &st->indio_dev->dev;
324 /* Do self test */
325 ret = adis16220_self_test(dev);
326 if (ret) {
327 dev_err(dev, "self test failure");
328 goto err_ret;
331 /* Read status register to check the result */
332 ret = adis16220_check_status(dev);
333 if (ret) {
334 adis16220_reset(dev);
335 dev_err(dev, "device not playing ball -> reset");
336 msleep(ADIS16220_STARTUP_DELAY);
337 ret = adis16220_check_status(dev);
338 if (ret) {
339 dev_err(dev, "giving up");
340 goto err_ret;
344 printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
345 st->us->chip_select, st->us->irq);
347 err_ret:
348 return ret;
351 static ssize_t adis16220_capture_buffer_read(struct adis16220_state *st,
352 char *buf,
353 loff_t off,
354 size_t count,
355 int addr)
357 struct spi_message msg;
358 struct spi_transfer xfers[] = {
360 .tx_buf = st->tx,
361 .bits_per_word = 8,
362 .len = 2,
363 .cs_change = 1,
364 .delay_usecs = 25,
365 }, {
366 .tx_buf = st->tx,
367 .rx_buf = st->rx,
368 .bits_per_word = 8,
369 .cs_change = 1,
370 .delay_usecs = 25,
373 int ret;
374 int i;
376 if (unlikely(!count))
377 return count;
379 if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
380 return -EINVAL;
382 if (off + count > ADIS16220_CAPTURE_SIZE)
383 count = ADIS16220_CAPTURE_SIZE - off;
385 /* write the begin position of capture buffer */
386 ret = adis16220_spi_write_reg_16(&st->indio_dev->dev,
387 ADIS16220_CAPT_PNTR,
388 off > 1);
389 if (ret)
390 return -EIO;
392 /* read count/2 values from capture buffer */
393 mutex_lock(&st->buf_lock);
395 for (i = 0; i < count; i += 2) {
396 st->tx[i] = ADIS16220_READ_REG(addr);
397 st->tx[i + 1] = 0;
399 xfers[1].len = count;
401 spi_message_init(&msg);
402 spi_message_add_tail(&xfers[0], &msg);
403 spi_message_add_tail(&xfers[1], &msg);
404 ret = spi_sync(st->us, &msg);
405 if (ret) {
407 mutex_unlock(&st->buf_lock);
408 return -EIO;
411 memcpy(buf, st->rx, count);
413 mutex_unlock(&st->buf_lock);
414 return count;
417 static ssize_t adis16220_accel_bin_read(struct file *filp, struct kobject *kobj,
418 struct bin_attribute *attr,
419 char *buf,
420 loff_t off,
421 size_t count)
423 struct device *dev = container_of(kobj, struct device, kobj);
424 struct iio_dev *indio_dev = dev_get_drvdata(dev);
425 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
427 return adis16220_capture_buffer_read(st, buf,
428 off, count,
429 ADIS16220_CAPT_BUFA);
432 static struct bin_attribute accel_bin = {
433 .attr = {
434 .name = "accel_bin",
435 .mode = S_IRUGO,
437 .read = adis16220_accel_bin_read,
438 .size = ADIS16220_CAPTURE_SIZE,
441 static ssize_t adis16220_adc1_bin_read(struct file *filp, struct kobject *kobj,
442 struct bin_attribute *attr,
443 char *buf, loff_t off,
444 size_t count)
446 struct device *dev = container_of(kobj, struct device, kobj);
447 struct iio_dev *indio_dev = dev_get_drvdata(dev);
448 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
450 return adis16220_capture_buffer_read(st, buf,
451 off, count,
452 ADIS16220_CAPT_BUF1);
455 static struct bin_attribute adc1_bin = {
456 .attr = {
457 .name = "in0_bin",
458 .mode = S_IRUGO,
460 .read = adis16220_adc1_bin_read,
461 .size = ADIS16220_CAPTURE_SIZE,
464 static ssize_t adis16220_adc2_bin_read(struct file *filp, struct kobject *kobj,
465 struct bin_attribute *attr,
466 char *buf, loff_t off,
467 size_t count)
469 struct device *dev = container_of(kobj, struct device, kobj);
470 struct iio_dev *indio_dev = dev_get_drvdata(dev);
471 struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
473 return adis16220_capture_buffer_read(st, buf,
474 off, count,
475 ADIS16220_CAPT_BUF2);
479 static struct bin_attribute adc2_bin = {
480 .attr = {
481 .name = "in1_bin",
482 .mode = S_IRUGO,
484 .read = adis16220_adc2_bin_read,
485 .size = ADIS16220_CAPTURE_SIZE,
488 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16220_read_12bit_unsigned,
489 ADIS16220_CAPT_SUPPLY);
490 static IIO_CONST_ATTR(in_supply_scale, "0.0012207");
491 static IIO_DEV_ATTR_ACCEL(adis16220_read_16bit, ADIS16220_CAPT_BUFA);
492 static IIO_DEVICE_ATTR(accel_peak_raw, S_IRUGO, adis16220_read_16bit,
493 NULL, ADIS16220_CAPT_PEAKA);
494 static IIO_DEV_ATTR_ACCEL_OFFSET(S_IWUSR | S_IRUGO,
495 adis16220_read_16bit,
496 adis16220_write_16bit,
497 ADIS16220_ACCL_NULL);
498 static IIO_DEV_ATTR_TEMP_RAW(adis16220_read_12bit_unsigned);
499 static IIO_CONST_ATTR(temp_offset, "25");
500 static IIO_CONST_ATTR(temp_scale, "-0.47");
502 static IIO_DEV_ATTR_IN_RAW(0, adis16220_read_16bit, ADIS16220_CAPT_BUF1);
503 static IIO_DEV_ATTR_IN_RAW(1, adis16220_read_16bit, ADIS16220_CAPT_BUF2);
505 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
506 adis16220_write_reset, 0);
508 #define IIO_DEV_ATTR_CAPTURE(_store) \
509 IIO_DEVICE_ATTR(capture, S_IWUSR, NULL, _store, 0)
511 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
513 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr) \
514 IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
516 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
517 adis16220_read_16bit,
518 adis16220_write_16bit,
519 ADIS16220_CAPT_PNTR);
521 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("100200");
523 static IIO_CONST_ATTR(name, "adis16220");
525 static struct attribute *adis16220_attributes[] = {
526 &iio_dev_attr_in_supply_raw.dev_attr.attr,
527 &iio_const_attr_in_supply_scale.dev_attr.attr,
528 &iio_dev_attr_accel_raw.dev_attr.attr,
529 &iio_dev_attr_accel_offset.dev_attr.attr,
530 &iio_dev_attr_accel_peak_raw.dev_attr.attr,
531 &iio_dev_attr_temp_raw.dev_attr.attr,
532 &iio_dev_attr_in0_raw.dev_attr.attr,
533 &iio_dev_attr_in1_raw.dev_attr.attr,
534 &iio_const_attr_temp_offset.dev_attr.attr,
535 &iio_const_attr_temp_scale.dev_attr.attr,
536 &iio_const_attr_available_sampling_frequency.dev_attr.attr,
537 &iio_dev_attr_reset.dev_attr.attr,
538 &iio_dev_attr_capture.dev_attr.attr,
539 &iio_dev_attr_capture_count.dev_attr.attr,
540 &iio_const_attr_name.dev_attr.attr,
541 NULL
544 static const struct attribute_group adis16220_attribute_group = {
545 .attrs = adis16220_attributes,
548 static int __devinit adis16220_probe(struct spi_device *spi)
550 int ret, regdone = 0;
551 struct adis16220_state *st = kzalloc(sizeof *st, GFP_KERNEL);
552 if (!st) {
553 ret = -ENOMEM;
554 goto error_ret;
556 /* this is only used for removal purposes */
557 spi_set_drvdata(spi, st);
559 /* Allocate the comms buffers */
560 st->rx = kzalloc(sizeof(*st->rx)*ADIS16220_MAX_RX, GFP_KERNEL);
561 if (st->rx == NULL) {
562 ret = -ENOMEM;
563 goto error_free_st;
565 st->tx = kzalloc(sizeof(*st->tx)*ADIS16220_MAX_TX, GFP_KERNEL);
566 if (st->tx == NULL) {
567 ret = -ENOMEM;
568 goto error_free_rx;
570 st->us = spi;
571 mutex_init(&st->buf_lock);
572 /* setup the industrialio driver allocated elements */
573 st->indio_dev = iio_allocate_device();
574 if (st->indio_dev == NULL) {
575 ret = -ENOMEM;
576 goto error_free_tx;
579 st->indio_dev->dev.parent = &spi->dev;
580 st->indio_dev->attrs = &adis16220_attribute_group;
581 st->indio_dev->dev_data = (void *)(st);
582 st->indio_dev->driver_module = THIS_MODULE;
583 st->indio_dev->modes = INDIO_DIRECT_MODE;
585 ret = iio_device_register(st->indio_dev);
586 if (ret)
587 goto error_free_dev;
588 regdone = 1;
590 ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
591 if (ret)
592 goto error_free_dev;
594 ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
595 if (ret)
596 goto error_rm_accel_bin;
598 ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
599 if (ret)
600 goto error_rm_adc1_bin;
602 /* Get the device into a sane initial state */
603 ret = adis16220_initial_setup(st);
604 if (ret)
605 goto error_rm_adc2_bin;
606 return 0;
608 error_rm_adc2_bin:
609 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
610 error_rm_adc1_bin:
611 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
612 error_rm_accel_bin:
613 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
614 error_free_dev:
615 if (regdone)
616 iio_device_unregister(st->indio_dev);
617 else
618 iio_free_device(st->indio_dev);
619 error_free_tx:
620 kfree(st->tx);
621 error_free_rx:
622 kfree(st->rx);
623 error_free_st:
624 kfree(st);
625 error_ret:
626 return ret;
629 static int adis16220_remove(struct spi_device *spi)
631 struct adis16220_state *st = spi_get_drvdata(spi);
632 struct iio_dev *indio_dev = st->indio_dev;
634 flush_scheduled_work();
636 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
637 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
638 sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
639 iio_device_unregister(indio_dev);
640 kfree(st->tx);
641 kfree(st->rx);
642 kfree(st);
644 return 0;
647 static struct spi_driver adis16220_driver = {
648 .driver = {
649 .name = "adis16220",
650 .owner = THIS_MODULE,
652 .probe = adis16220_probe,
653 .remove = __devexit_p(adis16220_remove),
656 static __init int adis16220_init(void)
658 return spi_register_driver(&adis16220_driver);
660 module_init(adis16220_init);
662 static __exit void adis16220_exit(void)
664 spi_unregister_driver(&adis16220_driver);
666 module_exit(adis16220_exit);
668 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
669 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
670 MODULE_LICENSE("GPL v2");