]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/iio/accel/adis16209_core.c
staging: iio: adis16209 driver
[net-next-2.6.git] / drivers / staging / iio / accel / adis16209_core.c
1 /*
2  * ADIS16209 Programmable Digital Vibration 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 "accel.h"
24 #include "inclinometer.h"
25 #include "../gyro/gyro.h"
26 #include "../adc/adc.h"
27
28 #include "adis16209.h"
29
30 #define DRIVER_NAME             "adis16209"
31
32 static int adis16209_check_status(struct device *dev);
33
34 /**
35  * adis16209_spi_write_reg_8() - write single byte to a register
36  * @dev: device associated with child of actual device (iio_dev or iio_trig)
37  * @reg_address: the address of the register to be written
38  * @val: the value to write
39  **/
40 static int adis16209_spi_write_reg_8(struct device *dev,
41                 u8 reg_address,
42                 u8 val)
43 {
44         int ret;
45         struct iio_dev *indio_dev = dev_get_drvdata(dev);
46         struct adis16209_state *st = iio_dev_get_devdata(indio_dev);
47
48         mutex_lock(&st->buf_lock);
49         st->tx[0] = ADIS16209_WRITE_REG(reg_address);
50         st->tx[1] = val;
51
52         ret = spi_write(st->us, st->tx, 2);
53         mutex_unlock(&st->buf_lock);
54
55         return ret;
56 }
57
58 /**
59  * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
60  * @dev: device associated with child of actual device (iio_dev or iio_trig)
61  * @reg_address: the address of the lower of the two registers. Second register
62  *               is assumed to have address one greater.
63  * @val: value to be written
64  **/
65 static int adis16209_spi_write_reg_16(struct device *dev,
66                 u8 lower_reg_address,
67                 u16 value)
68 {
69         int ret;
70         struct spi_message msg;
71         struct iio_dev *indio_dev = dev_get_drvdata(dev);
72         struct adis16209_state *st = iio_dev_get_devdata(indio_dev);
73         struct spi_transfer xfers[] = {
74                 {
75                         .tx_buf = st->tx,
76                         .bits_per_word = 8,
77                         .len = 2,
78                         .cs_change = 1,
79                 }, {
80                         .tx_buf = st->tx + 2,
81                         .bits_per_word = 8,
82                         .len = 2,
83                         .cs_change = 1,
84                 },
85         };
86
87         mutex_lock(&st->buf_lock);
88         st->tx[0] = ADIS16209_WRITE_REG(lower_reg_address);
89         st->tx[1] = value & 0xFF;
90         st->tx[2] = ADIS16209_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  * adis16209_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 adis16209_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 adis16209_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 = 20,
124                 }, {
125                         .rx_buf = st->rx,
126                         .bits_per_word = 8,
127                         .len = 2,
128                         .cs_change = 1,
129                         .delay_usecs = 20,
130                 },
131         };
132
133         mutex_lock(&st->buf_lock);
134         st->tx[0] = ADIS16209_READ_REG(lower_reg_address);
135         st->tx[1] = 0;
136
137         spi_message_init(&msg);
138         spi_message_add_tail(&xfers[0], &msg);
139         spi_message_add_tail(&xfers[1], &msg);
140         ret = spi_sync(st->us, &msg);
141         if (ret) {
142                 dev_err(&st->us->dev,
143                         "problem when reading 16 bit register 0x%02X",
144                         lower_reg_address);
145                 goto error_ret;
146         }
147         *val = (st->rx[0] << 8) | st->rx[1];
148
149 error_ret:
150         mutex_unlock(&st->buf_lock);
151         return ret;
152 }
153
154 static ssize_t adis16209_read_12bit_unsigned(struct device *dev,
155                 struct device_attribute *attr,
156                 char *buf)
157 {
158         int ret;
159         u16 val = 0;
160         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
161
162         ret = adis16209_spi_read_reg_16(dev, this_attr->address, &val);
163         if (ret)
164                 return ret;
165
166         if (val & ADIS16209_ERROR_ACTIVE)
167                 adis16209_check_status(dev);
168
169         return sprintf(buf, "%u\n", val & 0x0FFF);
170 }
171
172 static ssize_t adis16209_read_14bit_unsigned(struct device *dev,
173                 struct device_attribute *attr,
174                 char *buf)
175 {
176         int ret;
177         u16 val = 0;
178         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
179
180         ret = adis16209_spi_read_reg_16(dev, this_attr->address, &val);
181         if (ret)
182                 return ret;
183
184         if (val & ADIS16209_ERROR_ACTIVE)
185                 adis16209_check_status(dev);
186
187         return sprintf(buf, "%u\n", val & 0x3FFF);
188 }
189
190 static ssize_t adis16209_read_temp(struct device *dev,
191                 struct device_attribute *attr,
192                 char *buf)
193 {
194         struct iio_dev *indio_dev = dev_get_drvdata(dev);
195         ssize_t ret;
196         u16 val;
197
198         /* Take the iio_dev status lock */
199         mutex_lock(&indio_dev->mlock);
200
201         ret = adis16209_spi_read_reg_16(dev, ADIS16209_TEMP_OUT, (u16 *)&val);
202         if (ret)
203                 goto error_ret;
204
205         if (val & ADIS16209_ERROR_ACTIVE)
206                 adis16209_check_status(dev);
207
208         val &= 0xFFF;
209         ret = sprintf(buf, "%d\n", val);
210
211 error_ret:
212         mutex_unlock(&indio_dev->mlock);
213         return ret;
214 }
215
216 static ssize_t adis16209_read_14bit_signed(struct device *dev,
217                 struct device_attribute *attr,
218                 char *buf)
219 {
220         struct iio_dev *indio_dev = dev_get_drvdata(dev);
221         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
222         s16 val = 0;
223         ssize_t ret;
224
225         mutex_lock(&indio_dev->mlock);
226
227         ret = adis16209_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
228         if (!ret) {
229                 if (val & ADIS16209_ERROR_ACTIVE)
230                         adis16209_check_status(dev);
231
232                 val = ((s16)(val << 2) >> 2);
233                 ret = sprintf(buf, "%d\n", val);
234         }
235
236         mutex_unlock(&indio_dev->mlock);
237
238         return ret;
239 }
240
241 static ssize_t adis16209_write_16bit(struct device *dev,
242                 struct device_attribute *attr,
243                 const char *buf,
244                 size_t len)
245 {
246         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
247         int ret;
248         long val;
249
250         ret = strict_strtol(buf, 10, &val);
251         if (ret)
252                 goto error_ret;
253         ret = adis16209_spi_write_reg_16(dev, this_attr->address, val);
254
255 error_ret:
256         return ret ? ret : len;
257 }
258
259 static int adis16209_reset(struct device *dev)
260 {
261         int ret;
262         ret = adis16209_spi_write_reg_8(dev,
263                         ADIS16209_GLOB_CMD,
264                         ADIS16209_GLOB_CMD_SW_RESET);
265         if (ret)
266                 dev_err(dev, "problem resetting device");
267
268         return ret;
269 }
270
271 static ssize_t adis16209_write_reset(struct device *dev,
272                 struct device_attribute *attr,
273                 const char *buf, size_t len)
274 {
275         if (len < 1)
276                 return -EINVAL;
277         switch (buf[0]) {
278         case '1':
279         case 'y':
280         case 'Y':
281                 return adis16209_reset(dev);
282         }
283         return -EINVAL;
284 }
285
286 int adis16209_set_irq(struct device *dev, bool enable)
287 {
288         int ret = 0;
289         u16 msc;
290
291         ret = adis16209_spi_read_reg_16(dev, ADIS16209_MSC_CTRL, &msc);
292         if (ret)
293                 goto error_ret;
294
295         msc |= ADIS16209_MSC_CTRL_ACTIVE_HIGH;
296         msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2;
297         if (enable)
298                 msc |= ADIS16209_MSC_CTRL_DATA_RDY_EN;
299         else
300                 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_EN;
301
302         ret = adis16209_spi_write_reg_16(dev, ADIS16209_MSC_CTRL, msc);
303
304 error_ret:
305         return ret;
306 }
307
308 static int adis16209_check_status(struct device *dev)
309 {
310         u16 status;
311         int ret;
312
313         ret = adis16209_spi_read_reg_16(dev, ADIS16209_DIAG_STAT, &status);
314         if (ret < 0) {
315                 dev_err(dev, "Reading status failed\n");
316                 goto error_ret;
317         }
318         ret = status & 0x1F;
319
320         if (status & ADIS16209_DIAG_STAT_SELFTEST_FAIL)
321                 dev_err(dev, "Self test failure\n");
322         if (status & ADIS16209_DIAG_STAT_SPI_FAIL)
323                 dev_err(dev, "SPI failure\n");
324         if (status & ADIS16209_DIAG_STAT_FLASH_UPT)
325                 dev_err(dev, "Flash update failed\n");
326         if (status & ADIS16209_DIAG_STAT_POWER_HIGH)
327                 dev_err(dev, "Power supply above 3.625V\n");
328         if (status & ADIS16209_DIAG_STAT_POWER_LOW)
329                 dev_err(dev, "Power supply below 3.15V\n");
330
331 error_ret:
332         return ret;
333 }
334
335 static int adis16209_self_test(struct device *dev)
336 {
337         int ret;
338         ret = adis16209_spi_write_reg_16(dev,
339                         ADIS16209_MSC_CTRL,
340                         ADIS16209_MSC_CTRL_SELF_TEST_EN);
341         if (ret) {
342                 dev_err(dev, "problem starting self test");
343                 goto err_ret;
344         }
345
346         adis16209_check_status(dev);
347
348 err_ret:
349         return ret;
350 }
351
352 static int adis16209_initial_setup(struct adis16209_state *st)
353 {
354         int ret;
355         struct device *dev = &st->indio_dev->dev;
356
357         /* Disable IRQ */
358         ret = adis16209_set_irq(dev, false);
359         if (ret) {
360                 dev_err(dev, "disable irq failed");
361                 goto err_ret;
362         }
363
364         /* Do self test */
365         ret = adis16209_self_test(dev);
366         if (ret) {
367                 dev_err(dev, "self test failure");
368                 goto err_ret;
369         }
370
371         /* Read status register to check the result */
372         ret = adis16209_check_status(dev);
373         if (ret) {
374                 adis16209_reset(dev);
375                 dev_err(dev, "device not playing ball -> reset");
376                 msleep(ADIS16209_STARTUP_DELAY);
377                 ret = adis16209_check_status(dev);
378                 if (ret) {
379                         dev_err(dev, "giving up");
380                         goto err_ret;
381                 }
382         }
383
384         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
385                         st->us->chip_select, st->us->irq);
386
387 err_ret:
388         return ret;
389 }
390
391 static IIO_DEV_ATTR_IN_NAMED_RAW(supply, adis16209_read_14bit_unsigned,
392                 ADIS16209_SUPPLY_OUT);
393 static IIO_CONST_ATTR(in_supply_scale, "0.30518");
394 static IIO_DEV_ATTR_IN_RAW(0, adis16209_read_12bit_unsigned,
395                 ADIS16209_AUX_ADC);
396 static IIO_CONST_ATTR(in0_scale, "0.6105");
397
398 static IIO_DEV_ATTR_ACCEL_X(adis16209_read_14bit_signed,
399                 ADIS16209_XACCL_OUT);
400 static IIO_DEV_ATTR_ACCEL_Y(adis16209_read_14bit_signed,
401                 ADIS16209_YACCL_OUT);
402 static IIO_DEV_ATTR_ACCEL_X_OFFSET(S_IWUSR | S_IRUGO,
403                 adis16209_read_14bit_signed,
404                 adis16209_write_16bit,
405                 ADIS16209_XACCL_NULL);
406 static IIO_DEV_ATTR_ACCEL_Y_OFFSET(S_IWUSR | S_IRUGO,
407                 adis16209_read_14bit_signed,
408                 adis16209_write_16bit,
409                 ADIS16209_YACCL_NULL);
410 static IIO_CONST_ATTR(accel_scale, "0.24414");
411
412 static IIO_DEV_ATTR_INCLI_X(adis16209_read_14bit_signed,
413                 ADIS16209_XINCL_OUT);
414 static IIO_DEV_ATTR_INCLI_Y(adis16209_read_14bit_signed,
415                 ADIS16209_YINCL_OUT);
416 static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR | S_IRUGO,
417                 adis16209_read_14bit_signed,
418                 adis16209_write_16bit,
419                 ADIS16209_XACCL_NULL);
420 static IIO_DEV_ATTR_INCLI_Y_OFFSET(S_IWUSR | S_IRUGO,
421                 adis16209_read_14bit_signed,
422                 adis16209_write_16bit,
423                 ADIS16209_YACCL_NULL);
424 static IIO_CONST_ATTR(incli_scale, "0.025");
425
426 static IIO_DEVICE_ATTR(rot_raw, S_IRUGO, adis16209_read_14bit_signed,
427                        NULL, ADIS16209_ROT_OUT);
428
429 static IIO_DEV_ATTR_TEMP(adis16209_read_temp);
430 static IIO_CONST_ATTR(temp_offset, "25");
431 static IIO_CONST_ATTR(temp_scale, "-0.47");
432
433 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16209_write_reset, 0);
434
435 static IIO_CONST_ATTR(name, "adis16209");
436
437 static struct attribute *adis16209_event_attributes[] = {
438         NULL
439 };
440
441 static struct attribute_group adis16209_event_attribute_group = {
442         .attrs = adis16209_event_attributes,
443 };
444
445 static struct attribute *adis16209_attributes[] = {
446         &iio_dev_attr_in_supply_raw.dev_attr.attr,
447         &iio_const_attr_in_supply_scale.dev_attr.attr,
448         &iio_dev_attr_temp.dev_attr.attr,
449         &iio_const_attr_temp_offset.dev_attr.attr,
450         &iio_const_attr_temp_scale.dev_attr.attr,
451         &iio_dev_attr_reset.dev_attr.attr,
452         &iio_const_attr_name.dev_attr.attr,
453         &iio_dev_attr_in0_raw.dev_attr.attr,
454         &iio_const_attr_in0_scale.dev_attr.attr,
455         &iio_dev_attr_accel_x_raw.dev_attr.attr,
456         &iio_dev_attr_accel_y_raw.dev_attr.attr,
457         &iio_dev_attr_accel_x_offset.dev_attr.attr,
458         &iio_dev_attr_accel_y_offset.dev_attr.attr,
459         &iio_const_attr_accel_scale.dev_attr.attr,
460         &iio_dev_attr_incli_x_raw.dev_attr.attr,
461         &iio_dev_attr_incli_y_raw.dev_attr.attr,
462         &iio_dev_attr_incli_x_offset.dev_attr.attr,
463         &iio_dev_attr_incli_y_offset.dev_attr.attr,
464         &iio_const_attr_incli_scale.dev_attr.attr,
465         &iio_dev_attr_rot_raw.dev_attr.attr,
466         NULL
467 };
468
469 static const struct attribute_group adis16209_attribute_group = {
470         .attrs = adis16209_attributes,
471 };
472
473 static int __devinit adis16209_probe(struct spi_device *spi)
474 {
475         int ret, regdone = 0;
476         struct adis16209_state *st = kzalloc(sizeof *st, GFP_KERNEL);
477         if (!st) {
478                 ret =  -ENOMEM;
479                 goto error_ret;
480         }
481         /* this is only used for removal purposes */
482         spi_set_drvdata(spi, st);
483
484         /* Allocate the comms buffers */
485         st->rx = kzalloc(sizeof(*st->rx)*ADIS16209_MAX_RX, GFP_KERNEL);
486         if (st->rx == NULL) {
487                 ret = -ENOMEM;
488                 goto error_free_st;
489         }
490         st->tx = kzalloc(sizeof(*st->tx)*ADIS16209_MAX_TX, GFP_KERNEL);
491         if (st->tx == NULL) {
492                 ret = -ENOMEM;
493                 goto error_free_rx;
494         }
495         st->us = spi;
496         mutex_init(&st->buf_lock);
497         /* setup the industrialio driver allocated elements */
498         st->indio_dev = iio_allocate_device();
499         if (st->indio_dev == NULL) {
500                 ret = -ENOMEM;
501                 goto error_free_tx;
502         }
503
504         st->indio_dev->dev.parent = &spi->dev;
505         st->indio_dev->num_interrupt_lines = 1;
506         st->indio_dev->event_attrs = &adis16209_event_attribute_group;
507         st->indio_dev->attrs = &adis16209_attribute_group;
508         st->indio_dev->dev_data = (void *)(st);
509         st->indio_dev->driver_module = THIS_MODULE;
510         st->indio_dev->modes = INDIO_DIRECT_MODE;
511
512         ret = adis16209_configure_ring(st->indio_dev);
513         if (ret)
514                 goto error_free_dev;
515
516         ret = iio_device_register(st->indio_dev);
517         if (ret)
518                 goto error_unreg_ring_funcs;
519         regdone = 1;
520
521         ret = adis16209_initialize_ring(st->indio_dev->ring);
522         if (ret) {
523                 printk(KERN_ERR "failed to initialize the ring\n");
524                 goto error_unreg_ring_funcs;
525         }
526
527         if (spi->irq) {
528                 ret = iio_register_interrupt_line(spi->irq,
529                                 st->indio_dev,
530                                 0,
531                                 IRQF_TRIGGER_RISING,
532                                 "adis16209");
533                 if (ret)
534                         goto error_uninitialize_ring;
535
536                 ret = adis16209_probe_trigger(st->indio_dev);
537                 if (ret)
538                         goto error_unregister_line;
539         }
540
541         /* Get the device into a sane initial state */
542         ret = adis16209_initial_setup(st);
543         if (ret)
544                 goto error_remove_trigger;
545         return 0;
546
547 error_remove_trigger:
548         adis16209_remove_trigger(st->indio_dev);
549 error_unregister_line:
550         if (spi->irq)
551                 iio_unregister_interrupt_line(st->indio_dev, 0);
552 error_uninitialize_ring:
553         adis16209_uninitialize_ring(st->indio_dev->ring);
554 error_unreg_ring_funcs:
555         adis16209_unconfigure_ring(st->indio_dev);
556 error_free_dev:
557         if (regdone)
558                 iio_device_unregister(st->indio_dev);
559         else
560                 iio_free_device(st->indio_dev);
561 error_free_tx:
562         kfree(st->tx);
563 error_free_rx:
564         kfree(st->rx);
565 error_free_st:
566         kfree(st);
567 error_ret:
568         return ret;
569 }
570
571 static int adis16209_remove(struct spi_device *spi)
572 {
573         struct adis16209_state *st = spi_get_drvdata(spi);
574         struct iio_dev *indio_dev = st->indio_dev;
575
576         flush_scheduled_work();
577
578         adis16209_remove_trigger(indio_dev);
579         if (spi->irq)
580                 iio_unregister_interrupt_line(indio_dev, 0);
581
582         adis16209_uninitialize_ring(indio_dev->ring);
583         iio_device_unregister(indio_dev);
584         adis16209_unconfigure_ring(indio_dev);
585         kfree(st->tx);
586         kfree(st->rx);
587         kfree(st);
588
589         return 0;
590 }
591
592 static struct spi_driver adis16209_driver = {
593         .driver = {
594                 .name = "adis16209",
595                 .owner = THIS_MODULE,
596         },
597         .probe = adis16209_probe,
598         .remove = __devexit_p(adis16209_remove),
599 };
600
601 static __init int adis16209_init(void)
602 {
603         return spi_register_driver(&adis16209_driver);
604 }
605 module_init(adis16209_init);
606
607 static __exit void adis16209_exit(void)
608 {
609         spi_unregister_driver(&adis16209_driver);
610 }
611 module_exit(adis16209_exit);
612
613 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
614 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
615 MODULE_LICENSE("GPL v2");