Merge branch 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelv...
[pandora-kernel.git] / drivers / staging / iio / gyro / adis16260_core.c
1 /*
2  * ADIS16260 Programmable Digital Gyroscope 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 "../ring_generic.h"
24 #include "../adc/adc.h"
25 #include "gyro.h"
26
27 #include "adis16260.h"
28
29 #define DRIVER_NAME             "adis16260"
30
31 static int adis16260_check_status(struct device *dev);
32
33 /**
34  * adis16260_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 adis16260_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 adis16260_state *st = iio_dev_get_devdata(indio_dev);
46
47         mutex_lock(&st->buf_lock);
48         st->tx[0] = ADIS16260_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  * adis16260_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 adis16260_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 adis16260_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 = 20,
79                 }, {
80                         .tx_buf = st->tx + 2,
81                         .bits_per_word = 8,
82                         .len = 2,
83                         .cs_change = 1,
84                         .delay_usecs = 20,
85                 },
86         };
87
88         mutex_lock(&st->buf_lock);
89         st->tx[0] = ADIS16260_WRITE_REG(lower_reg_address);
90         st->tx[1] = value & 0xFF;
91         st->tx[2] = ADIS16260_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  * adis16260_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 adis16260_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 adis16260_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 = 30,
125                 }, {
126                         .rx_buf = st->rx,
127                         .bits_per_word = 8,
128                         .len = 2,
129                         .cs_change = 1,
130                         .delay_usecs = 30,
131                 },
132         };
133
134         mutex_lock(&st->buf_lock);
135         st->tx[0] = ADIS16260_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 adis16260_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 = adis16260_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
168         if (ret)
169                 return ret;
170
171         if (val & ADIS16260_ERROR_ACTIVE)
172                 adis16260_check_status(dev);
173         val = ((s16)(val << shift) >> shift);
174         return sprintf(buf, "%d\n", val);
175 }
176
177 static ssize_t adis16260_read_12bit_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 = adis16260_spi_read_reg_16(dev, this_attr->address, &val);
186         if (ret)
187                 return ret;
188
189         if (val & ADIS16260_ERROR_ACTIVE)
190                 adis16260_check_status(dev);
191
192         return sprintf(buf, "%u\n", val & 0x0FFF);
193 }
194
195 static ssize_t adis16260_read_12bit_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 =  adis16260_spi_read_signed(dev, attr, buf, 12);
205         mutex_unlock(&indio_dev->mlock);
206
207         return ret;
208 }
209
210 static ssize_t adis16260_read_14bit_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 =  adis16260_spi_read_signed(dev, attr, buf, 14);
220         mutex_unlock(&indio_dev->mlock);
221
222         return ret;
223 }
224
225 static ssize_t adis16260_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 = adis16260_spi_write_reg_16(dev, this_attr->address, val);
238
239 error_ret:
240         return ret ? ret : len;
241 }
242
243 static ssize_t adis16260_read_frequency(struct device *dev,
244                 struct device_attribute *attr,
245                 char *buf)
246 {
247         int ret, len = 0;
248         u16 t;
249         int sps;
250         ret = adis16260_spi_read_reg_16(dev,
251                         ADIS16260_SMPL_PRD,
252                         &t);
253         if (ret)
254                 return ret;
255         sps =  (t & ADIS16260_SMPL_PRD_TIME_BASE) ? 66 : 2048;
256         sps /= (t & ADIS16260_SMPL_PRD_DIV_MASK) + 1;
257         len = sprintf(buf, "%d SPS\n", sps);
258         return len;
259 }
260
261 static ssize_t adis16260_write_frequency(struct device *dev,
262                 struct device_attribute *attr,
263                 const char *buf,
264                 size_t len)
265 {
266         struct iio_dev *indio_dev = dev_get_drvdata(dev);
267         struct adis16260_state *st = iio_dev_get_devdata(indio_dev);
268         long val;
269         int ret;
270         u8 t;
271
272         ret = strict_strtol(buf, 10, &val);
273         if (ret)
274                 return ret;
275
276         mutex_lock(&indio_dev->mlock);
277
278         t = (2048 / val);
279         if (t > 0)
280                 t--;
281         t &= ADIS16260_SMPL_PRD_DIV_MASK;
282         if ((t & ADIS16260_SMPL_PRD_DIV_MASK) >= 0x0A)
283                 st->us->max_speed_hz = ADIS16260_SPI_SLOW;
284         else
285                 st->us->max_speed_hz = ADIS16260_SPI_FAST;
286
287         ret = adis16260_spi_write_reg_8(dev,
288                         ADIS16260_SMPL_PRD,
289                         t);
290
291         mutex_unlock(&indio_dev->mlock);
292
293         return ret ? ret : len;
294 }
295
296 static int adis16260_reset(struct device *dev)
297 {
298         int ret;
299         ret = adis16260_spi_write_reg_8(dev,
300                         ADIS16260_GLOB_CMD,
301                         ADIS16260_GLOB_CMD_SW_RESET);
302         if (ret)
303                 dev_err(dev, "problem resetting device");
304
305         return ret;
306 }
307
308 static ssize_t adis16260_write_reset(struct device *dev,
309                 struct device_attribute *attr,
310                 const char *buf, size_t len)
311 {
312         if (len < 1)
313                 return -EINVAL;
314         switch (buf[0]) {
315         case '1':
316         case 'y':
317         case 'Y':
318                 return adis16260_reset(dev);
319         }
320         return -EINVAL;
321 }
322
323 int adis16260_set_irq(struct device *dev, bool enable)
324 {
325         int ret;
326         u16 msc;
327         ret = adis16260_spi_read_reg_16(dev, ADIS16260_MSC_CTRL, &msc);
328         if (ret)
329                 goto error_ret;
330
331         msc |= ADIS16260_MSC_CTRL_DATA_RDY_POL_HIGH;
332         if (enable)
333                 msc |= ADIS16260_MSC_CTRL_DATA_RDY_EN;
334         else
335                 msc &= ~ADIS16260_MSC_CTRL_DATA_RDY_EN;
336
337         ret = adis16260_spi_write_reg_16(dev, ADIS16260_MSC_CTRL, msc);
338         if (ret)
339                 goto error_ret;
340
341 error_ret:
342         return ret;
343 }
344
345 /* Power down the device */
346 static int adis16260_stop_device(struct device *dev)
347 {
348         int ret;
349         u16 val = ADIS16260_SLP_CNT_POWER_OFF;
350
351         ret = adis16260_spi_write_reg_16(dev, ADIS16260_SLP_CNT, val);
352         if (ret)
353                 dev_err(dev, "problem with turning device off: SLP_CNT");
354
355         return ret;
356 }
357
358 static int adis16260_self_test(struct device *dev)
359 {
360         int ret;
361         ret = adis16260_spi_write_reg_16(dev,
362                         ADIS16260_MSC_CTRL,
363                         ADIS16260_MSC_CTRL_MEM_TEST);
364         if (ret) {
365                 dev_err(dev, "problem starting self test");
366                 goto err_ret;
367         }
368
369         adis16260_check_status(dev);
370
371 err_ret:
372         return ret;
373 }
374
375 static int adis16260_check_status(struct device *dev)
376 {
377         u16 status;
378         int ret;
379
380         ret = adis16260_spi_read_reg_16(dev, ADIS16260_DIAG_STAT, &status);
381
382         if (ret < 0) {
383                 dev_err(dev, "Reading status failed\n");
384                 goto error_ret;
385         }
386         ret = status & 0x7F;
387         if (status & ADIS16260_DIAG_STAT_FLASH_CHK)
388                 dev_err(dev, "Flash checksum error\n");
389         if (status & ADIS16260_DIAG_STAT_SELF_TEST)
390                 dev_err(dev, "Self test error\n");
391         if (status & ADIS16260_DIAG_STAT_OVERFLOW)
392                 dev_err(dev, "Sensor overrange\n");
393         if (status & ADIS16260_DIAG_STAT_SPI_FAIL)
394                 dev_err(dev, "SPI failure\n");
395         if (status & ADIS16260_DIAG_STAT_FLASH_UPT)
396                 dev_err(dev, "Flash update failed\n");
397         if (status & ADIS16260_DIAG_STAT_POWER_HIGH)
398                 dev_err(dev, "Power supply above 5.25V\n");
399         if (status & ADIS16260_DIAG_STAT_POWER_LOW)
400                 dev_err(dev, "Power supply below 4.75V\n");
401
402 error_ret:
403         return ret;
404 }
405
406 static int adis16260_initial_setup(struct adis16260_state *st)
407 {
408         int ret;
409         struct device *dev = &st->indio_dev->dev;
410
411         /* Disable IRQ */
412         ret = adis16260_set_irq(dev, false);
413         if (ret) {
414                 dev_err(dev, "disable irq failed");
415                 goto err_ret;
416         }
417
418         /* Do self test */
419         ret = adis16260_self_test(dev);
420         if (ret) {
421                 dev_err(dev, "self test failure");
422                 goto err_ret;
423         }
424
425         /* Read status register to check the result */
426         ret = adis16260_check_status(dev);
427         if (ret) {
428                 adis16260_reset(dev);
429                 dev_err(dev, "device not playing ball -> reset");
430                 msleep(ADIS16260_STARTUP_DELAY);
431                 ret = adis16260_check_status(dev);
432                 if (ret) {
433                         dev_err(dev, "giving up");
434                         goto err_ret;
435                 }
436         }
437
438         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
439                         st->us->chip_select, st->us->irq);
440
441 err_ret:
442         return ret;
443 }
444
445 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply,
446                                 adis16260_read_12bit_unsigned,
447                                 ADIS16260_SUPPLY_OUT);
448 static IIO_CONST_ATTR_IN_NAMED_SCALE(0, supply, "0.0018315");
449
450 static IIO_DEV_ATTR_GYRO(adis16260_read_14bit_signed,
451                 ADIS16260_GYRO_OUT);
452 static IIO_CONST_ATTR_GYRO_SCALE("0.00127862821");
453 static IIO_DEV_ATTR_GYRO_CALIBSCALE(S_IWUSR | S_IRUGO,
454                 adis16260_read_14bit_signed,
455                 adis16260_write_16bit,
456                 ADIS16260_GYRO_SCALE);
457 static IIO_DEV_ATTR_GYRO_CALIBBIAS(S_IWUSR | S_IRUGO,
458                 adis16260_read_12bit_signed,
459                 adis16260_write_16bit,
460                 ADIS16260_GYRO_OFF);
461
462 static IIO_DEV_ATTR_TEMP_RAW(adis16260_read_12bit_unsigned);
463 static IIO_CONST_ATTR_TEMP_OFFSET("25");
464 static IIO_CONST_ATTR_TEMP_SCALE("0.1453");
465
466 static IIO_DEV_ATTR_IN_RAW(1, adis16260_read_12bit_unsigned,
467                 ADIS16260_AUX_ADC);
468 static IIO_CONST_ATTR(in1_scale, "0.0006105");
469
470 static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
471                 adis16260_read_frequency,
472                 adis16260_write_frequency);
473 static IIO_DEV_ATTR_ANGL(adis16260_read_14bit_signed,
474                 ADIS16260_ANGL_OUT);
475
476 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16260_write_reset, 0);
477
478 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("256 2048");
479
480 static IIO_CONST_ATTR_NAME("adis16260");
481
482 static struct attribute *adis16260_event_attributes[] = {
483         NULL
484 };
485
486 static struct attribute_group adis16260_event_attribute_group = {
487         .attrs = adis16260_event_attributes,
488 };
489
490 static struct attribute *adis16260_attributes[] = {
491         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
492         &iio_const_attr_in0_supply_scale.dev_attr.attr,
493         &iio_dev_attr_gyro_raw.dev_attr.attr,
494         &iio_const_attr_gyro_scale.dev_attr.attr,
495         &iio_dev_attr_gyro_calibscale.dev_attr.attr,
496         &iio_dev_attr_gyro_calibbias.dev_attr.attr,
497         &iio_dev_attr_angl_raw.dev_attr.attr,
498         &iio_dev_attr_temp_raw.dev_attr.attr,
499         &iio_const_attr_temp_offset.dev_attr.attr,
500         &iio_const_attr_temp_scale.dev_attr.attr,
501         &iio_dev_attr_in1_raw.dev_attr.attr,
502         &iio_const_attr_in1_scale.dev_attr.attr,
503         &iio_dev_attr_sampling_frequency.dev_attr.attr,
504         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
505         &iio_dev_attr_reset.dev_attr.attr,
506         &iio_const_attr_name.dev_attr.attr,
507         NULL
508 };
509
510 static const struct attribute_group adis16260_attribute_group = {
511         .attrs = adis16260_attributes,
512 };
513
514 static int __devinit adis16260_probe(struct spi_device *spi)
515 {
516         int ret, regdone = 0;
517         struct adis16260_state *st = kzalloc(sizeof *st, GFP_KERNEL);
518         if (!st) {
519                 ret =  -ENOMEM;
520                 goto error_ret;
521         }
522         /* this is only used for removal purposes */
523         spi_set_drvdata(spi, st);
524
525         /* Allocate the comms buffers */
526         st->rx = kzalloc(sizeof(*st->rx)*ADIS16260_MAX_RX, GFP_KERNEL);
527         if (st->rx == NULL) {
528                 ret = -ENOMEM;
529                 goto error_free_st;
530         }
531         st->tx = kzalloc(sizeof(*st->tx)*ADIS16260_MAX_TX, GFP_KERNEL);
532         if (st->tx == NULL) {
533                 ret = -ENOMEM;
534                 goto error_free_rx;
535         }
536         st->us = spi;
537         mutex_init(&st->buf_lock);
538         /* setup the industrialio driver allocated elements */
539         st->indio_dev = iio_allocate_device();
540         if (st->indio_dev == NULL) {
541                 ret = -ENOMEM;
542                 goto error_free_tx;
543         }
544
545         st->indio_dev->dev.parent = &spi->dev;
546         st->indio_dev->num_interrupt_lines = 1;
547         st->indio_dev->event_attrs = &adis16260_event_attribute_group;
548         st->indio_dev->attrs = &adis16260_attribute_group;
549         st->indio_dev->dev_data = (void *)(st);
550         st->indio_dev->driver_module = THIS_MODULE;
551         st->indio_dev->modes = INDIO_DIRECT_MODE;
552
553         ret = adis16260_configure_ring(st->indio_dev);
554         if (ret)
555                 goto error_free_dev;
556
557         ret = iio_device_register(st->indio_dev);
558         if (ret)
559                 goto error_unreg_ring_funcs;
560         regdone = 1;
561         ret = iio_ring_buffer_register(st->indio_dev->ring, 0);
562         if (ret) {
563                 printk(KERN_ERR "failed to initialize the ring\n");
564                 goto error_unreg_ring_funcs;
565         }
566
567         if (spi->irq) {
568                 ret = iio_register_interrupt_line(spi->irq,
569                                 st->indio_dev,
570                                 0,
571                                 IRQF_TRIGGER_RISING,
572                                 "adis16260");
573                 if (ret)
574                         goto error_uninitialize_ring;
575
576                 ret = adis16260_probe_trigger(st->indio_dev);
577                 if (ret)
578                         goto error_unregister_line;
579         }
580
581         /* Get the device into a sane initial state */
582         ret = adis16260_initial_setup(st);
583         if (ret)
584                 goto error_remove_trigger;
585         return 0;
586
587 error_remove_trigger:
588         adis16260_remove_trigger(st->indio_dev);
589 error_unregister_line:
590         if (spi->irq)
591                 iio_unregister_interrupt_line(st->indio_dev, 0);
592 error_uninitialize_ring:
593         iio_ring_buffer_unregister(st->indio_dev->ring);
594 error_unreg_ring_funcs:
595         adis16260_unconfigure_ring(st->indio_dev);
596 error_free_dev:
597         if (regdone)
598                 iio_device_unregister(st->indio_dev);
599         else
600                 iio_free_device(st->indio_dev);
601 error_free_tx:
602         kfree(st->tx);
603 error_free_rx:
604         kfree(st->rx);
605 error_free_st:
606         kfree(st);
607 error_ret:
608         return ret;
609 }
610
611 static int adis16260_remove(struct spi_device *spi)
612 {
613         int ret;
614         struct adis16260_state *st = spi_get_drvdata(spi);
615         struct iio_dev *indio_dev = st->indio_dev;
616
617         ret = adis16260_stop_device(&(indio_dev->dev));
618         if (ret)
619                 goto err_ret;
620
621         flush_scheduled_work();
622
623         adis16260_remove_trigger(indio_dev);
624         if (spi->irq)
625                 iio_unregister_interrupt_line(indio_dev, 0);
626
627         iio_ring_buffer_unregister(st->indio_dev->ring);
628         iio_device_unregister(indio_dev);
629         adis16260_unconfigure_ring(indio_dev);
630         kfree(st->tx);
631         kfree(st->rx);
632         kfree(st);
633
634 err_ret:
635         return ret;
636 }
637
638 static struct spi_driver adis16260_driver = {
639         .driver = {
640                 .name = "adis16260",
641                 .owner = THIS_MODULE,
642         },
643         .probe = adis16260_probe,
644         .remove = __devexit_p(adis16260_remove),
645 };
646
647 static __init int adis16260_init(void)
648 {
649         return spi_register_driver(&adis16260_driver);
650 }
651 module_init(adis16260_init);
652
653 static __exit void adis16260_exit(void)
654 {
655         spi_unregister_driver(&adis16260_driver);
656 }
657 module_exit(adis16260_exit);
658
659 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
660 MODULE_DESCRIPTION("Analog Devices ADIS16260/5 Digital Gyroscope Sensor");
661 MODULE_LICENSE("GPL v2");