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