Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[pandora-kernel.git] / drivers / staging / iio / accel / adis16240_core.c
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
31 static int adis16240_check_status(struct device *dev);
32
33 /**
34  * adis16240_spi_write_reg_8() - write single byte to a register
35  * @dev: device associated with child of actual device (iio_dev or iio_trig)
36  * @reg_address: the address of the register to be written
37  * @val: the value to write
38  **/
39 static int adis16240_spi_write_reg_8(struct device *dev,
40                 u8 reg_address,
41                 u8 val)
42 {
43         int ret;
44         struct iio_dev *indio_dev = dev_get_drvdata(dev);
45         struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
46
47         mutex_lock(&st->buf_lock);
48         st->tx[0] = ADIS16240_WRITE_REG(reg_address);
49         st->tx[1] = val;
50
51         ret = spi_write(st->us, st->tx, 2);
52         mutex_unlock(&st->buf_lock);
53
54         return ret;
55 }
56
57 /**
58  * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
59  * @dev: device associated with child of actual device (iio_dev or iio_trig)
60  * @reg_address: the address of the lower of the two registers. Second register
61  *               is assumed to have address one greater.
62  * @val: value to be written
63  **/
64 static int adis16240_spi_write_reg_16(struct device *dev,
65                 u8 lower_reg_address,
66                 u16 value)
67 {
68         int ret;
69         struct spi_message msg;
70         struct iio_dev *indio_dev = dev_get_drvdata(dev);
71         struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
72         struct spi_transfer xfers[] = {
73                 {
74                         .tx_buf = st->tx,
75                         .bits_per_word = 8,
76                         .len = 2,
77                         .cs_change = 1,
78                         .delay_usecs = 35,
79                 }, {
80                         .tx_buf = st->tx + 2,
81                         .bits_per_word = 8,
82                         .len = 2,
83                         .cs_change = 1,
84                         .delay_usecs = 35,
85                 },
86         };
87
88         mutex_lock(&st->buf_lock);
89         st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
90         st->tx[1] = value & 0xFF;
91         st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
92         st->tx[3] = (value >> 8) & 0xFF;
93
94         spi_message_init(&msg);
95         spi_message_add_tail(&xfers[0], &msg);
96         spi_message_add_tail(&xfers[1], &msg);
97         ret = spi_sync(st->us, &msg);
98         mutex_unlock(&st->buf_lock);
99
100         return ret;
101 }
102
103 /**
104  * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
105  * @dev: device associated with child of actual device (iio_dev or iio_trig)
106  * @reg_address: the address of the lower of the two registers. Second register
107  *               is assumed to have address one greater.
108  * @val: somewhere to pass back the value read
109  **/
110 static int adis16240_spi_read_reg_16(struct device *dev,
111                 u8 lower_reg_address,
112                 u16 *val)
113 {
114         struct spi_message msg;
115         struct iio_dev *indio_dev = dev_get_drvdata(dev);
116         struct adis16240_state *st = iio_dev_get_devdata(indio_dev);
117         int ret;
118         struct spi_transfer xfers[] = {
119                 {
120                         .tx_buf = st->tx,
121                         .bits_per_word = 8,
122                         .len = 2,
123                         .cs_change = 1,
124                         .delay_usecs = 35,
125                 }, {
126                         .rx_buf = st->rx,
127                         .bits_per_word = 8,
128                         .len = 2,
129                         .cs_change = 1,
130                         .delay_usecs = 35,
131                 },
132         };
133
134         mutex_lock(&st->buf_lock);
135         st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
136         st->tx[1] = 0;
137         st->tx[2] = 0;
138         st->tx[3] = 0;
139
140         spi_message_init(&msg);
141         spi_message_add_tail(&xfers[0], &msg);
142         spi_message_add_tail(&xfers[1], &msg);
143         ret = spi_sync(st->us, &msg);
144         if (ret) {
145                 dev_err(&st->us->dev,
146                         "problem when reading 16 bit register 0x%02X",
147                         lower_reg_address);
148                 goto error_ret;
149         }
150         *val = (st->rx[0] << 8) | st->rx[1];
151
152 error_ret:
153         mutex_unlock(&st->buf_lock);
154         return ret;
155 }
156
157 static ssize_t adis16240_spi_read_signed(struct device *dev,
158                 struct device_attribute *attr,
159                 char *buf,
160                 unsigned bits)
161 {
162         int ret;
163         s16 val = 0;
164         unsigned shift = 16 - bits;
165         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
166
167         ret = adis16240_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
168         if (ret)
169                 return ret;
170
171         if (val & ADIS16240_ERROR_ACTIVE)
172                 adis16240_check_status(dev);
173
174         val = ((s16)(val << shift) >> shift);
175         return sprintf(buf, "%d\n", val);
176 }
177
178 static ssize_t adis16240_read_10bit_unsigned(struct device *dev,
179                 struct device_attribute *attr,
180                 char *buf)
181 {
182         int ret;
183         u16 val = 0;
184         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
185
186         ret = adis16240_spi_read_reg_16(dev, this_attr->address, &val);
187         if (ret)
188                 return ret;
189
190         if (val & ADIS16240_ERROR_ACTIVE)
191                 adis16240_check_status(dev);
192
193         return sprintf(buf, "%u\n", val & 0x03FF);
194 }
195
196 static ssize_t adis16240_read_10bit_signed(struct device *dev,
197                 struct device_attribute *attr,
198                 char *buf)
199 {
200         struct iio_dev *indio_dev = dev_get_drvdata(dev);
201         ssize_t ret;
202
203         /* Take the iio_dev status lock */
204         mutex_lock(&indio_dev->mlock);
205         ret =  adis16240_spi_read_signed(dev, attr, buf, 10);
206         mutex_unlock(&indio_dev->mlock);
207
208         return ret;
209 }
210
211 static ssize_t adis16240_read_12bit_signed(struct device *dev,
212                 struct device_attribute *attr,
213                 char *buf)
214 {
215         struct iio_dev *indio_dev = dev_get_drvdata(dev);
216         ssize_t ret;
217
218         /* Take the iio_dev status lock */
219         mutex_lock(&indio_dev->mlock);
220         ret =  adis16240_spi_read_signed(dev, attr, buf, 12);
221         mutex_unlock(&indio_dev->mlock);
222
223         return ret;
224 }
225
226 static ssize_t adis16240_write_16bit(struct device *dev,
227                 struct device_attribute *attr,
228                 const char *buf,
229                 size_t len)
230 {
231         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
232         int ret;
233         long val;
234
235         ret = strict_strtol(buf, 10, &val);
236         if (ret)
237                 goto error_ret;
238         ret = adis16240_spi_write_reg_16(dev, this_attr->address, val);
239
240 error_ret:
241         return ret ? ret : len;
242 }
243
244 static int adis16240_reset(struct device *dev)
245 {
246         int ret;
247         ret = adis16240_spi_write_reg_8(dev,
248                         ADIS16240_GLOB_CMD,
249                         ADIS16240_GLOB_CMD_SW_RESET);
250         if (ret)
251                 dev_err(dev, "problem resetting device");
252
253         return ret;
254 }
255
256 static ssize_t adis16240_write_reset(struct device *dev,
257                 struct device_attribute *attr,
258                 const char *buf, size_t len)
259 {
260         if (len < 1)
261                 return -EINVAL;
262         switch (buf[0]) {
263         case '1':
264         case 'y':
265         case 'Y':
266                 return adis16240_reset(dev);
267         }
268         return -EINVAL;
269 }
270
271 int adis16240_set_irq(struct device *dev, bool enable)
272 {
273         int ret = 0;
274         u16 msc;
275
276         ret = adis16240_spi_read_reg_16(dev, ADIS16240_MSC_CTRL, &msc);
277         if (ret)
278                 goto error_ret;
279
280         msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
281         msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
282         if (enable)
283                 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
284         else
285                 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
286
287         ret = adis16240_spi_write_reg_16(dev, ADIS16240_MSC_CTRL, msc);
288
289 error_ret:
290         return ret;
291 }
292
293 static int adis16240_self_test(struct device *dev)
294 {
295         int ret;
296         ret = adis16240_spi_write_reg_16(dev,
297                         ADIS16240_MSC_CTRL,
298                         ADIS16240_MSC_CTRL_SELF_TEST_EN);
299         if (ret) {
300                 dev_err(dev, "problem starting self test");
301                 goto err_ret;
302         }
303
304         msleep(ADIS16240_STARTUP_DELAY);
305
306         adis16240_check_status(dev);
307
308 err_ret:
309         return ret;
310 }
311
312 static int adis16240_check_status(struct device *dev)
313 {
314         u16 status;
315         int ret;
316
317         ret = adis16240_spi_read_reg_16(dev, ADIS16240_DIAG_STAT, &status);
318
319         if (ret < 0) {
320                 dev_err(dev, "Reading status failed\n");
321                 goto error_ret;
322         }
323
324         ret = status & 0x2F;
325         if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
326                 dev_err(dev, "Power-on, self-test fail\n");
327         if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
328                 dev_err(dev, "SPI failure\n");
329         if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
330                 dev_err(dev, "Flash update failed\n");
331         if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
332                 dev_err(dev, "Power supply above 3.625V\n");
333         if (status & ADIS16240_DIAG_STAT_POWER_LOW)
334                 dev_err(dev, "Power supply below 2.225V\n");
335
336 error_ret:
337         return ret;
338 }
339
340 static int adis16240_initial_setup(struct adis16240_state *st)
341 {
342         int ret;
343         struct device *dev = &st->indio_dev->dev;
344
345         /* Disable IRQ */
346         ret = adis16240_set_irq(dev, false);
347         if (ret) {
348                 dev_err(dev, "disable irq failed");
349                 goto err_ret;
350         }
351
352         /* Do self test */
353         ret = adis16240_self_test(dev);
354         if (ret) {
355                 dev_err(dev, "self test failure");
356                 goto err_ret;
357         }
358
359         /* Read status register to check the result */
360         ret = adis16240_check_status(dev);
361         if (ret) {
362                 adis16240_reset(dev);
363                 dev_err(dev, "device not playing ball -> reset");
364                 msleep(ADIS16240_STARTUP_DELAY);
365                 ret = adis16240_check_status(dev);
366                 if (ret) {
367                         dev_err(dev, "giving up");
368                         goto err_ret;
369                 }
370         }
371
372         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
373                         st->us->chip_select, st->us->irq);
374
375 err_ret:
376         return ret;
377 }
378
379 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16240_read_10bit_unsigned,
380                 ADIS16240_SUPPLY_OUT);
381 static IIO_DEV_ATTR_IN_RAW(1, adis16240_read_10bit_signed,
382                 ADIS16240_AUX_ADC);
383 static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.00488");
384
385 static IIO_CONST_ATTR_ACCEL_SCALE("0.50406181");
386 static IIO_CONST_ATTR(accel_peak_scale, "6.6292954");
387 static IIO_DEV_ATTR_ACCEL_X(adis16240_read_10bit_signed,
388                 ADIS16240_XACCL_OUT);
389 static IIO_DEVICE_ATTR(accel_x_peak_raw, S_IRUGO,
390                        adis16240_read_10bit_signed, NULL,
391                        ADIS16240_XPEAK_OUT);
392 static IIO_DEV_ATTR_ACCEL_Y(adis16240_read_10bit_signed,
393                 ADIS16240_YACCL_OUT);
394 static IIO_DEVICE_ATTR(accel_y_peak_raw, S_IRUGO,
395                        adis16240_read_10bit_signed, NULL,
396                        ADIS16240_YPEAK_OUT);
397 static IIO_DEV_ATTR_ACCEL_Z(adis16240_read_10bit_signed,
398                 ADIS16240_ZACCL_OUT);
399 static IIO_DEVICE_ATTR(accel_z_peak_raw, S_IRUGO,
400                        adis16240_read_10bit_signed, NULL,
401                        ADIS16240_ZPEAK_OUT);
402
403 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
404                        adis16240_read_12bit_signed, NULL,
405                        ADIS16240_XYZPEAK_OUT);
406 static IIO_DEV_ATTR_ACCEL_X_CALIBBIAS(S_IWUSR | S_IRUGO,
407                 adis16240_read_10bit_signed,
408                 adis16240_write_16bit,
409                 ADIS16240_XACCL_OFF);
410 static IIO_DEV_ATTR_ACCEL_Y_CALIBBIAS(S_IWUSR | S_IRUGO,
411                 adis16240_read_10bit_signed,
412                 adis16240_write_16bit,
413                 ADIS16240_YACCL_OFF);
414 static IIO_DEV_ATTR_ACCEL_Z_CALIBBIAS(S_IWUSR | S_IRUGO,
415                 adis16240_read_10bit_signed,
416                 adis16240_write_16bit,
417                 ADIS16240_ZACCL_OFF);
418 static IIO_DEV_ATTR_TEMP_RAW(adis16240_read_10bit_unsigned);
419 static IIO_CONST_ATTR_TEMP_SCALE("0.244");
420
421 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
422
423 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
424
425 static IIO_CONST_ATTR_NAME("adis16240");
426
427 static struct attribute *adis16240_event_attributes[] = {
428         NULL
429 };
430
431 static struct attribute_group adis16240_event_attribute_group = {
432         .attrs = adis16240_event_attributes,
433 };
434
435 static struct attribute *adis16240_attributes[] = {
436         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
437         &iio_const_attr_in0_supply_scale.dev_attr.attr,
438         &iio_dev_attr_in1_raw.dev_attr.attr,
439         &iio_const_attr_accel_scale.dev_attr.attr,
440         &iio_const_attr_accel_peak_scale.dev_attr.attr,
441         &iio_dev_attr_accel_x_raw.dev_attr.attr,
442         &iio_dev_attr_accel_x_calibbias.dev_attr.attr,
443         &iio_dev_attr_accel_x_peak_raw.dev_attr.attr,
444         &iio_dev_attr_accel_y_raw.dev_attr.attr,
445         &iio_dev_attr_accel_y_calibbias.dev_attr.attr,
446         &iio_dev_attr_accel_y_peak_raw.dev_attr.attr,
447         &iio_dev_attr_accel_z_raw.dev_attr.attr,
448         &iio_dev_attr_accel_z_calibbias.dev_attr.attr,
449         &iio_dev_attr_accel_z_peak_raw.dev_attr.attr,
450         &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
451         &iio_dev_attr_temp_raw.dev_attr.attr,
452         &iio_const_attr_temp_scale.dev_attr.attr,
453         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
454         &iio_dev_attr_reset.dev_attr.attr,
455         &iio_const_attr_name.dev_attr.attr,
456         NULL
457 };
458
459 static const struct attribute_group adis16240_attribute_group = {
460         .attrs = adis16240_attributes,
461 };
462
463 static int __devinit adis16240_probe(struct spi_device *spi)
464 {
465         int ret, regdone = 0;
466         struct adis16240_state *st = kzalloc(sizeof *st, GFP_KERNEL);
467         if (!st) {
468                 ret =  -ENOMEM;
469                 goto error_ret;
470         }
471         /* this is only used for removal purposes */
472         spi_set_drvdata(spi, st);
473
474         /* Allocate the comms buffers */
475         st->rx = kzalloc(sizeof(*st->rx)*ADIS16240_MAX_RX, GFP_KERNEL);
476         if (st->rx == NULL) {
477                 ret = -ENOMEM;
478                 goto error_free_st;
479         }
480         st->tx = kzalloc(sizeof(*st->tx)*ADIS16240_MAX_TX, GFP_KERNEL);
481         if (st->tx == NULL) {
482                 ret = -ENOMEM;
483                 goto error_free_rx;
484         }
485         st->us = spi;
486         mutex_init(&st->buf_lock);
487         /* setup the industrialio driver allocated elements */
488         st->indio_dev = iio_allocate_device();
489         if (st->indio_dev == NULL) {
490                 ret = -ENOMEM;
491                 goto error_free_tx;
492         }
493
494         st->indio_dev->dev.parent = &spi->dev;
495         st->indio_dev->num_interrupt_lines = 1;
496         st->indio_dev->event_attrs = &adis16240_event_attribute_group;
497         st->indio_dev->attrs = &adis16240_attribute_group;
498         st->indio_dev->dev_data = (void *)(st);
499         st->indio_dev->driver_module = THIS_MODULE;
500         st->indio_dev->modes = INDIO_DIRECT_MODE;
501
502         ret = adis16240_configure_ring(st->indio_dev);
503         if (ret)
504                 goto error_free_dev;
505
506         ret = iio_device_register(st->indio_dev);
507         if (ret)
508                 goto error_unreg_ring_funcs;
509         regdone = 1;
510
511         ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
512         if (ret) {
513                 printk(KERN_ERR "failed to initialize the ring\n");
514                 goto error_unreg_ring_funcs;
515         }
516
517         if (spi->irq) {
518                 ret = iio_register_interrupt_line(spi->irq,
519                                 st->indio_dev,
520                                 0,
521                                 IRQF_TRIGGER_RISING,
522                                 "adis16240");
523                 if (ret)
524                         goto error_uninitialize_ring;
525
526                 ret = adis16240_probe_trigger(st->indio_dev);
527                 if (ret)
528                         goto error_unregister_line;
529         }
530
531         /* Get the device into a sane initial state */
532         ret = adis16240_initial_setup(st);
533         if (ret)
534                 goto error_remove_trigger;
535         return 0;
536
537 error_remove_trigger:
538         adis16240_remove_trigger(st->indio_dev);
539 error_unregister_line:
540         if (spi->irq)
541                 iio_unregister_interrupt_line(st->indio_dev, 0);
542 error_uninitialize_ring:
543         iio_ring_buffer_unregister(st->indio_dev->ring);
544 error_unreg_ring_funcs:
545         adis16240_unconfigure_ring(st->indio_dev);
546 error_free_dev:
547         if (regdone)
548                 iio_device_unregister(st->indio_dev);
549         else
550                 iio_free_device(st->indio_dev);
551 error_free_tx:
552         kfree(st->tx);
553 error_free_rx:
554         kfree(st->rx);
555 error_free_st:
556         kfree(st);
557 error_ret:
558         return ret;
559 }
560
561 static int adis16240_remove(struct spi_device *spi)
562 {
563         struct adis16240_state *st = spi_get_drvdata(spi);
564         struct iio_dev *indio_dev = st->indio_dev;
565
566         flush_scheduled_work();
567
568         adis16240_remove_trigger(indio_dev);
569         if (spi->irq)
570                 iio_unregister_interrupt_line(indio_dev, 0);
571
572         iio_ring_buffer_unregister(indio_dev->ring);
573         iio_device_unregister(indio_dev);
574         adis16240_unconfigure_ring(indio_dev);
575         kfree(st->tx);
576         kfree(st->rx);
577         kfree(st);
578
579         return 0;
580 }
581
582 static struct spi_driver adis16240_driver = {
583         .driver = {
584                 .name = "adis16240",
585                 .owner = THIS_MODULE,
586         },
587         .probe = adis16240_probe,
588         .remove = __devexit_p(adis16240_remove),
589 };
590
591 static __init int adis16240_init(void)
592 {
593         return spi_register_driver(&adis16240_driver);
594 }
595 module_init(adis16240_init);
596
597 static __exit void adis16240_exit(void)
598 {
599         spi_unregister_driver(&adis16240_driver);
600 }
601 module_exit(adis16240_exit);
602
603 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
604 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
605 MODULE_LICENSE("GPL v2");