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