Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/teigland/dlm
[cascardo/linux.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(supply, adis16240_read_10bit_unsigned,
380                 ADIS16240_SUPPLY_OUT);
381 static IIO_DEV_ATTR_IN_RAW(0, adis16240_read_10bit_signed,
382                 ADIS16240_AUX_ADC);
383 static IIO_CONST_ATTR(in_supply_scale, "0.00488");
384 static IIO_DEV_ATTR_ACCEL_X(adis16240_read_10bit_signed,
385                 ADIS16240_XACCL_OUT);
386 static IIO_DEVICE_ATTR(accel_x_peak_raw, S_IRUGO,
387                        adis16240_read_10bit_signed, NULL,
388                        ADIS16240_XPEAK_OUT);
389 static IIO_DEV_ATTR_ACCEL_Y(adis16240_read_10bit_signed,
390                 ADIS16240_YACCL_OUT);
391 static IIO_DEVICE_ATTR(accel_y_peak_raw, S_IRUGO,
392                        adis16240_read_10bit_signed, NULL,
393                        ADIS16240_YPEAK_OUT);
394 static IIO_DEV_ATTR_ACCEL_Z(adis16240_read_10bit_signed,
395                 ADIS16240_ZACCL_OUT);
396 static IIO_DEVICE_ATTR(accel_z_peak_raw, S_IRUGO,
397                        adis16240_read_10bit_signed, NULL,
398                        ADIS16240_ZPEAK_OUT);
399
400 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
401                        adis16240_read_12bit_signed, NULL,
402                        ADIS16240_XYZPEAK_OUT);
403 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
404                 adis16240_read_10bit_signed,
405                 adis16240_write_16bit,
406                 ADIS16240_XACCL_OFF);
407 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
408                 adis16240_read_10bit_signed,
409                 adis16240_write_16bit,
410                 ADIS16240_YACCL_OFF);
411 static IIO_DEV_ATTR_ACCEL_Z_OFFSET(S_IWUSR | S_IRUGO,
412                 adis16240_read_10bit_signed,
413                 adis16240_write_16bit,
414                 ADIS16240_ZACCL_OFF);
415 static IIO_DEV_ATTR_TEMP_RAW(adis16240_read_10bit_unsigned);
416 static IIO_CONST_ATTR(temp_scale, "0.244");
417
418 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
419
420 static IIO_CONST_ATTR_AVAIL_SAMP_FREQ("4096");
421
422 static IIO_CONST_ATTR(name, "adis16240");
423
424 static struct attribute *adis16240_event_attributes[] = {
425         NULL
426 };
427
428 static struct attribute_group adis16240_event_attribute_group = {
429         .attrs = adis16240_event_attributes,
430 };
431
432 static struct attribute *adis16240_attributes[] = {
433         &iio_dev_attr_in_supply_raw.dev_attr.attr,
434         &iio_const_attr_in_supply_scale.dev_attr.attr,
435         &iio_dev_attr_in0_raw.dev_attr.attr,
436         &iio_dev_attr_accel_x_raw.dev_attr.attr,
437         &iio_dev_attr_accel_x_offset.dev_attr.attr,
438         &iio_dev_attr_accel_x_peak_raw.dev_attr.attr,
439         &iio_dev_attr_accel_y_raw.dev_attr.attr,
440         &iio_dev_attr_accel_y_offset.dev_attr.attr,
441         &iio_dev_attr_accel_y_peak_raw.dev_attr.attr,
442         &iio_dev_attr_accel_z_raw.dev_attr.attr,
443         &iio_dev_attr_accel_z_offset.dev_attr.attr,
444         &iio_dev_attr_accel_z_peak_raw.dev_attr.attr,
445         &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
446         &iio_dev_attr_temp_raw.dev_attr.attr,
447         &iio_const_attr_temp_scale.dev_attr.attr,
448         &iio_const_attr_available_sampling_frequency.dev_attr.attr,
449         &iio_dev_attr_reset.dev_attr.attr,
450         &iio_const_attr_name.dev_attr.attr,
451         NULL
452 };
453
454 static const struct attribute_group adis16240_attribute_group = {
455         .attrs = adis16240_attributes,
456 };
457
458 static int __devinit adis16240_probe(struct spi_device *spi)
459 {
460         int ret, regdone = 0;
461         struct adis16240_state *st = kzalloc(sizeof *st, GFP_KERNEL);
462         if (!st) {
463                 ret =  -ENOMEM;
464                 goto error_ret;
465         }
466         /* this is only used for removal purposes */
467         spi_set_drvdata(spi, st);
468
469         /* Allocate the comms buffers */
470         st->rx = kzalloc(sizeof(*st->rx)*ADIS16240_MAX_RX, GFP_KERNEL);
471         if (st->rx == NULL) {
472                 ret = -ENOMEM;
473                 goto error_free_st;
474         }
475         st->tx = kzalloc(sizeof(*st->tx)*ADIS16240_MAX_TX, GFP_KERNEL);
476         if (st->tx == NULL) {
477                 ret = -ENOMEM;
478                 goto error_free_rx;
479         }
480         st->us = spi;
481         mutex_init(&st->buf_lock);
482         /* setup the industrialio driver allocated elements */
483         st->indio_dev = iio_allocate_device();
484         if (st->indio_dev == NULL) {
485                 ret = -ENOMEM;
486                 goto error_free_tx;
487         }
488
489         st->indio_dev->dev.parent = &spi->dev;
490         st->indio_dev->num_interrupt_lines = 1;
491         st->indio_dev->event_attrs = &adis16240_event_attribute_group;
492         st->indio_dev->attrs = &adis16240_attribute_group;
493         st->indio_dev->dev_data = (void *)(st);
494         st->indio_dev->driver_module = THIS_MODULE;
495         st->indio_dev->modes = INDIO_DIRECT_MODE;
496
497         ret = adis16240_configure_ring(st->indio_dev);
498         if (ret)
499                 goto error_free_dev;
500
501         ret = iio_device_register(st->indio_dev);
502         if (ret)
503                 goto error_unreg_ring_funcs;
504         regdone = 1;
505
506         ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
507         if (ret) {
508                 printk(KERN_ERR "failed to initialize the ring\n");
509                 goto error_unreg_ring_funcs;
510         }
511
512         if (spi->irq) {
513                 ret = iio_register_interrupt_line(spi->irq,
514                                 st->indio_dev,
515                                 0,
516                                 IRQF_TRIGGER_RISING,
517                                 "adis16240");
518                 if (ret)
519                         goto error_uninitialize_ring;
520
521                 ret = adis16240_probe_trigger(st->indio_dev);
522                 if (ret)
523                         goto error_unregister_line;
524         }
525
526         /* Get the device into a sane initial state */
527         ret = adis16240_initial_setup(st);
528         if (ret)
529                 goto error_remove_trigger;
530         return 0;
531
532 error_remove_trigger:
533         adis16240_remove_trigger(st->indio_dev);
534 error_unregister_line:
535         if (spi->irq)
536                 iio_unregister_interrupt_line(st->indio_dev, 0);
537 error_uninitialize_ring:
538         iio_ring_buffer_unregister(st->indio_dev->ring);
539 error_unreg_ring_funcs:
540         adis16240_unconfigure_ring(st->indio_dev);
541 error_free_dev:
542         if (regdone)
543                 iio_device_unregister(st->indio_dev);
544         else
545                 iio_free_device(st->indio_dev);
546 error_free_tx:
547         kfree(st->tx);
548 error_free_rx:
549         kfree(st->rx);
550 error_free_st:
551         kfree(st);
552 error_ret:
553         return ret;
554 }
555
556 static int adis16240_remove(struct spi_device *spi)
557 {
558         struct adis16240_state *st = spi_get_drvdata(spi);
559         struct iio_dev *indio_dev = st->indio_dev;
560
561         flush_scheduled_work();
562
563         adis16240_remove_trigger(indio_dev);
564         if (spi->irq)
565                 iio_unregister_interrupt_line(indio_dev, 0);
566
567         iio_ring_buffer_unregister(indio_dev->ring);
568         iio_device_unregister(indio_dev);
569         adis16240_unconfigure_ring(indio_dev);
570         kfree(st->tx);
571         kfree(st->rx);
572         kfree(st);
573
574         return 0;
575 }
576
577 static struct spi_driver adis16240_driver = {
578         .driver = {
579                 .name = "adis16240",
580                 .owner = THIS_MODULE,
581         },
582         .probe = adis16240_probe,
583         .remove = __devexit_p(adis16240_remove),
584 };
585
586 static __init int adis16240_init(void)
587 {
588         return spi_register_driver(&adis16240_driver);
589 }
590 module_init(adis16240_init);
591
592 static __exit void adis16240_exit(void)
593 {
594         spi_unregister_driver(&adis16240_driver);
595 }
596 module_exit(adis16240_exit);
597
598 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
599 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
600 MODULE_LICENSE("GPL v2");