Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / drivers / staging / iio / accel / adis16220_core.c
1 /*
2  * ADIS16220 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 "../adc/adc.h"
25
26 #include "adis16220.h"
27
28 #define DRIVER_NAME             "adis16220"
29
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)
39 {
40         int ret;
41         struct iio_dev *indio_dev = dev_get_drvdata(dev);
42         struct adis16220_state *st = iio_dev_get_devdata(indio_dev);
43
44         mutex_lock(&st->buf_lock);
45         st->tx[0] = ADIS16220_WRITE_REG(reg_address);
46         st->tx[1] = val;
47
48         ret = spi_write(st->us, st->tx, 2);
49         mutex_unlock(&st->buf_lock);
50
51         return ret;
52 }
53
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)
64 {
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[] = {
70                 {
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,
82                 },
83         };
84
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;
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  * 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
106  **/
107 static int adis16220_spi_read_reg_16(struct device *dev,
108                 u8 lower_reg_address,
109                 u16 *val)
110 {
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[] = {
116                 {
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,
128                 },
129         };
130
131         mutex_lock(&st->buf_lock);
132         st->tx[0] = ADIS16220_READ_REG(lower_reg_address);
133         st->tx[1] = 0;
134
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;
144         }
145         *val = (st->rx[0] << 8) | st->rx[1];
146
147 error_ret:
148         mutex_unlock(&st->buf_lock);
149         return ret;
150 }
151
152 static ssize_t adis16220_spi_read_signed(struct device *dev,
153                 struct device_attribute *attr,
154                 char *buf,
155                 unsigned bits)
156 {
157         int ret;
158         s16 val = 0;
159         unsigned shift = 16 - bits;
160         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
161
162         ret = adis16220_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
163         if (ret)
164                 return ret;
165
166         val = ((s16)(val << shift) >> shift);
167         return sprintf(buf, "%d\n", val);
168 }
169
170 static ssize_t adis16220_read_12bit_unsigned(struct device *dev,
171                 struct device_attribute *attr,
172                 char *buf)
173 {
174         int ret;
175         u16 val = 0;
176         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
177
178         ret = adis16220_spi_read_reg_16(dev, this_attr->address, &val);
179         if (ret)
180                 return ret;
181
182         return sprintf(buf, "%u\n", val & 0x0FFF);
183 }
184
185 static ssize_t adis16220_read_16bit(struct device *dev,
186                 struct device_attribute *attr,
187                 char *buf)
188 {
189         struct iio_dev *indio_dev = dev_get_drvdata(dev);
190         ssize_t ret;
191
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);
196
197         return ret;
198 }
199
200 static ssize_t adis16220_write_16bit(struct device *dev,
201                 struct device_attribute *attr,
202                 const char *buf,
203                 size_t len)
204 {
205         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
206         int ret;
207         long val;
208
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);
213
214 error_ret:
215         return ret ? ret : len;
216 }
217
218 static int adis16220_capture(struct device *dev)
219 {
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");
226
227         msleep(10); /* delay for capture to finish */
228
229         return ret;
230 }
231
232 static int adis16220_reset(struct device *dev)
233 {
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");
240
241         return ret;
242 }
243
244 static ssize_t adis16220_write_reset(struct device *dev,
245                 struct device_attribute *attr,
246                 const char *buf, size_t len)
247 {
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;
255         }
256         return -1;
257 }
258
259 static ssize_t adis16220_write_capture(struct device *dev,
260                 struct device_attribute *attr,
261                 const char *buf, size_t len)
262 {
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;
270         }
271         return -1;
272 }
273
274 static int adis16220_check_status(struct device *dev)
275 {
276         u16 status;
277         int ret;
278
279         ret = adis16220_spi_read_reg_16(dev, ADIS16220_DIAG_STAT, &status);
280
281         if (ret < 0) {
282                 dev_err(dev, "Reading status failed\n");
283                 goto error_ret;
284         }
285         ret = status & 0x7F;
286
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");
297
298 error_ret:
299         return ret;
300 }
301
302 static int adis16220_self_test(struct device *dev)
303 {
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;
311         }
312
313         adis16220_check_status(dev);
314
315 err_ret:
316         return ret;
317 }
318
319 static int adis16220_initial_setup(struct adis16220_state *st)
320 {
321         int ret;
322         struct device *dev = &st->indio_dev->dev;
323
324         /* Do self test */
325         ret = adis16220_self_test(dev);
326         if (ret) {
327                 dev_err(dev, "self test failure");
328                 goto err_ret;
329         }
330
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;
341                 }
342         }
343
344         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
345                         st->us->chip_select, st->us->irq);
346
347 err_ret:
348         return ret;
349 }
350
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)
356 {
357         struct spi_message msg;
358         struct spi_transfer xfers[] = {
359                 {
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,
371                 },
372         };
373         int ret;
374         int i;
375
376         if (unlikely(!count))
377                 return count;
378
379         if ((off >= ADIS16220_CAPTURE_SIZE) || (count & 1) || (off & 1))
380                 return -EINVAL;
381
382         if (off + count > ADIS16220_CAPTURE_SIZE)
383                 count = ADIS16220_CAPTURE_SIZE - off;
384
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;
391
392         /* read count/2 values from capture buffer */
393         mutex_lock(&st->buf_lock);
394
395         for (i = 0; i < count; i += 2) {
396                 st->tx[i] = ADIS16220_READ_REG(addr);
397                 st->tx[i + 1] = 0;
398         }
399         xfers[1].len = count;
400
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) {
406
407                 mutex_unlock(&st->buf_lock);
408                 return -EIO;
409         }
410
411         memcpy(buf, st->rx, count);
412
413         mutex_unlock(&st->buf_lock);
414         return count;
415 }
416
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)
422 {
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);
426
427         return adis16220_capture_buffer_read(st, buf,
428                                         off, count,
429                                         ADIS16220_CAPT_BUFA);
430 }
431
432 static struct bin_attribute accel_bin = {
433         .attr = {
434                 .name = "accel_bin",
435                 .mode = S_IRUGO,
436         },
437         .read = adis16220_accel_bin_read,
438         .size = ADIS16220_CAPTURE_SIZE,
439 };
440
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)
445 {
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);
449
450         return adis16220_capture_buffer_read(st, buf,
451                                         off, count,
452                                         ADIS16220_CAPT_BUF1);
453 }
454
455 static struct bin_attribute adc1_bin = {
456         .attr = {
457                 .name = "in0_bin",
458                 .mode = S_IRUGO,
459         },
460         .read =  adis16220_adc1_bin_read,
461         .size = ADIS16220_CAPTURE_SIZE,
462 };
463
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)
468 {
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);
472
473         return adis16220_capture_buffer_read(st, buf,
474                                         off, count,
475                                         ADIS16220_CAPT_BUF2);
476 }
477
478
479 static struct bin_attribute adc2_bin = {
480         .attr = {
481                 .name = "in1_bin",
482                 .mode = S_IRUGO,
483         },
484         .read =  adis16220_adc2_bin_read,
485         .size = ADIS16220_CAPTURE_SIZE,
486 };
487
488 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16220_read_12bit_unsigned,
489                 ADIS16220_CAPT_SUPPLY);
490 static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "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_CONST_ATTR_ACCEL_SCALE("0.18704223545");
499 static IIO_DEV_ATTR_TEMP_RAW(adis16220_read_12bit_unsigned);
500 static IIO_CONST_ATTR_TEMP_OFFSET("25");
501 static IIO_CONST_ATTR_TEMP_SCALE("-0.47");
502
503 static IIO_DEV_ATTR_IN_RAW(1, adis16220_read_16bit, ADIS16220_CAPT_BUF1);
504 static IIO_DEV_ATTR_IN_RAW(2, adis16220_read_16bit, ADIS16220_CAPT_BUF2);
505
506 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL,
507                 adis16220_write_reset, 0);
508
509 #define IIO_DEV_ATTR_CAPTURE(_store)                            \
510         IIO_DEVICE_ATTR(capture, S_IWUGO, NULL, _store, 0)
511
512 static IIO_DEV_ATTR_CAPTURE(adis16220_write_capture);
513
514 #define IIO_DEV_ATTR_CAPTURE_COUNT(_mode, _show, _store, _addr)         \
515         IIO_DEVICE_ATTR(capture_count, _mode, _show, _store, _addr)
516
517 static IIO_DEV_ATTR_CAPTURE_COUNT(S_IWUSR | S_IRUGO,
518                 adis16220_read_16bit,
519                 adis16220_write_16bit,
520                 ADIS16220_CAPT_PNTR);
521
522 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100200");
523
524 static IIO_CONST_ATTR_NAME("adis16220");
525
526 static struct attribute *adis16220_attributes[] = {
527         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
528         &iio_const_attr_in0_supply_scale.dev_attr.attr,
529         &iio_dev_attr_accel_raw.dev_attr.attr,
530         &iio_dev_attr_accel_offset.dev_attr.attr,
531         &iio_dev_attr_accel_peak_raw.dev_attr.attr,
532         &iio_const_attr_accel_scale.dev_attr.attr,
533         &iio_dev_attr_temp_raw.dev_attr.attr,
534         &iio_dev_attr_in1_raw.dev_attr.attr,
535         &iio_dev_attr_in2_raw.dev_attr.attr,
536         &iio_const_attr_temp_offset.dev_attr.attr,
537         &iio_const_attr_temp_scale.dev_attr.attr,
538         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
539         &iio_dev_attr_reset.dev_attr.attr,
540         &iio_dev_attr_capture.dev_attr.attr,
541         &iio_dev_attr_capture_count.dev_attr.attr,
542         &iio_const_attr_name.dev_attr.attr,
543         NULL
544 };
545
546 static const struct attribute_group adis16220_attribute_group = {
547         .attrs = adis16220_attributes,
548 };
549
550 static int __devinit adis16220_probe(struct spi_device *spi)
551 {
552         int ret, regdone = 0;
553         struct adis16220_state *st = kzalloc(sizeof *st, GFP_KERNEL);
554         if (!st) {
555                 ret =  -ENOMEM;
556                 goto error_ret;
557         }
558         /* this is only used for removal purposes */
559         spi_set_drvdata(spi, st);
560
561         /* Allocate the comms buffers */
562         st->rx = kzalloc(sizeof(*st->rx)*ADIS16220_MAX_RX, GFP_KERNEL);
563         if (st->rx == NULL) {
564                 ret = -ENOMEM;
565                 goto error_free_st;
566         }
567         st->tx = kzalloc(sizeof(*st->tx)*ADIS16220_MAX_TX, GFP_KERNEL);
568         if (st->tx == NULL) {
569                 ret = -ENOMEM;
570                 goto error_free_rx;
571         }
572         st->us = spi;
573         mutex_init(&st->buf_lock);
574         /* setup the industrialio driver allocated elements */
575         st->indio_dev = iio_allocate_device();
576         if (st->indio_dev == NULL) {
577                 ret = -ENOMEM;
578                 goto error_free_tx;
579         }
580
581         st->indio_dev->dev.parent = &spi->dev;
582         st->indio_dev->attrs = &adis16220_attribute_group;
583         st->indio_dev->dev_data = (void *)(st);
584         st->indio_dev->driver_module = THIS_MODULE;
585         st->indio_dev->modes = INDIO_DIRECT_MODE;
586
587         ret = iio_device_register(st->indio_dev);
588         if (ret)
589                 goto error_free_dev;
590         regdone = 1;
591
592         ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
593         if (ret)
594                 goto error_free_dev;
595
596         ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
597         if (ret)
598                 goto error_rm_accel_bin;
599
600         ret = sysfs_create_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
601         if (ret)
602                 goto error_rm_adc1_bin;
603
604         /* Get the device into a sane initial state */
605         ret = adis16220_initial_setup(st);
606         if (ret)
607                 goto error_rm_adc2_bin;
608         return 0;
609
610 error_rm_adc2_bin:
611         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
612 error_rm_adc1_bin:
613         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
614 error_rm_accel_bin:
615         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
616 error_free_dev:
617         if (regdone)
618                 iio_device_unregister(st->indio_dev);
619         else
620                 iio_free_device(st->indio_dev);
621 error_free_tx:
622         kfree(st->tx);
623 error_free_rx:
624         kfree(st->rx);
625 error_free_st:
626         kfree(st);
627 error_ret:
628         return ret;
629 }
630
631 static int adis16220_remove(struct spi_device *spi)
632 {
633         struct adis16220_state *st = spi_get_drvdata(spi);
634         struct iio_dev *indio_dev = st->indio_dev;
635
636         flush_scheduled_work();
637
638         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc2_bin);
639         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &adc1_bin);
640         sysfs_remove_bin_file(&st->indio_dev->dev.kobj, &accel_bin);
641         iio_device_unregister(indio_dev);
642         kfree(st->tx);
643         kfree(st->rx);
644         kfree(st);
645
646         return 0;
647 }
648
649 static struct spi_driver adis16220_driver = {
650         .driver = {
651                 .name = "adis16220",
652                 .owner = THIS_MODULE,
653         },
654         .probe = adis16220_probe,
655         .remove = __devexit_p(adis16220_remove),
656 };
657
658 static __init int adis16220_init(void)
659 {
660         return spi_register_driver(&adis16220_driver);
661 }
662 module_init(adis16220_init);
663
664 static __exit void adis16220_exit(void)
665 {
666         spi_unregister_driver(&adis16220_driver);
667 }
668 module_exit(adis16220_exit);
669
670 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
671 MODULE_DESCRIPTION("Analog Devices ADIS16220 Digital Vibration Sensor");
672 MODULE_LICENSE("GPL v2");