]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/accel/adis16203_core.c
61623112aca7c82cad920e326625b92389680283
[karo-tx-linux.git] / drivers / staging / iio / accel / adis16203_core.c
1 /*
2  * ADIS16203 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 #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 "accel.h"
24 #include "inclinometer.h"
25 #include "../gyro/gyro.h"
26 #include "../adc/adc.h"
27
28 #include "adis16203.h"
29
30 #define DRIVER_NAME             "adis16203"
31
32 static int adis16203_check_status(struct device *dev);
33
34 /**
35  * adis16203_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 adis16203_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 adis16203_state *st = iio_dev_get_devdata(indio_dev);
47
48         mutex_lock(&st->buf_lock);
49         st->tx[0] = ADIS16203_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  * adis16203_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 adis16203_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 adis16203_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] = ADIS16203_WRITE_REG(lower_reg_address);
89         st->tx[1] = value & 0xFF;
90         st->tx[2] = ADIS16203_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  * adis16203_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 adis16203_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 adis16203_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] = ADIS16203_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, "problem when reading 16 bit register 0x%02X",
143                                 lower_reg_address);
144                 goto error_ret;
145         }
146         *val = (st->rx[0] << 8) | st->rx[1];
147
148 error_ret:
149         mutex_unlock(&st->buf_lock);
150         return ret;
151 }
152
153 static ssize_t adis16203_read_12bit_unsigned(struct device *dev,
154                 struct device_attribute *attr,
155                 char *buf)
156 {
157         int ret;
158         u16 val = 0;
159         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160
161         ret = adis16203_spi_read_reg_16(dev, this_attr->address, &val);
162         if (ret)
163                 return ret;
164
165         if (val & ADIS16203_ERROR_ACTIVE)
166                 adis16203_check_status(dev);
167
168         return sprintf(buf, "%u\n", val & 0x0FFF);
169 }
170
171 static ssize_t adis16203_read_temp(struct device *dev,
172                 struct device_attribute *attr,
173                 char *buf)
174 {
175         struct iio_dev *indio_dev = dev_get_drvdata(dev);
176         ssize_t ret;
177         u16 val;
178
179         /* Take the iio_dev status lock */
180         mutex_lock(&indio_dev->mlock);
181
182         ret = adis16203_spi_read_reg_16(dev, ADIS16203_TEMP_OUT, (u16 *)&val);
183         if (ret)
184                 goto error_ret;
185
186         if (val & ADIS16203_ERROR_ACTIVE)
187                 adis16203_check_status(dev);
188
189         val &= 0xFFF;
190         ret = sprintf(buf, "%d\n", val);
191
192 error_ret:
193         mutex_unlock(&indio_dev->mlock);
194         return ret;
195 }
196
197 static ssize_t adis16203_read_14bit_signed(struct device *dev,
198                 struct device_attribute *attr,
199                 char *buf)
200 {
201         struct iio_dev *indio_dev = dev_get_drvdata(dev);
202         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
203         s16 val = 0;
204         ssize_t ret;
205
206         mutex_lock(&indio_dev->mlock);
207
208         ret = adis16203_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
209         if (!ret) {
210                 if (val & ADIS16203_ERROR_ACTIVE)
211                         adis16203_check_status(dev);
212
213                 val = ((s16)(val << 2) >> 2);
214                 ret = sprintf(buf, "%d\n", val);
215         }
216
217         mutex_unlock(&indio_dev->mlock);
218
219         return ret;
220 }
221
222 static ssize_t adis16203_write_16bit(struct device *dev,
223                 struct device_attribute *attr,
224                 const char *buf,
225                 size_t len)
226 {
227         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
228         int ret;
229         long val;
230
231         ret = strict_strtol(buf, 10, &val);
232         if (ret)
233                 goto error_ret;
234         ret = adis16203_spi_write_reg_16(dev, this_attr->address, val);
235
236 error_ret:
237         return ret ? ret : len;
238 }
239
240 static int adis16203_reset(struct device *dev)
241 {
242         int ret;
243         ret = adis16203_spi_write_reg_8(dev,
244                         ADIS16203_GLOB_CMD,
245                         ADIS16203_GLOB_CMD_SW_RESET);
246         if (ret)
247                 dev_err(dev, "problem resetting device");
248
249         return ret;
250 }
251
252 static ssize_t adis16203_write_reset(struct device *dev,
253                 struct device_attribute *attr,
254                 const char *buf, size_t len)
255 {
256         if (len < 1)
257                 return -EINVAL;
258         switch (buf[0]) {
259         case '1':
260         case 'y':
261         case 'Y':
262                 return adis16203_reset(dev);
263         }
264         return -EINVAL;
265 }
266
267 int adis16203_set_irq(struct device *dev, bool enable)
268 {
269         int ret = 0;
270         u16 msc;
271
272         ret = adis16203_spi_read_reg_16(dev, ADIS16203_MSC_CTRL, &msc);
273         if (ret)
274                 goto error_ret;
275
276         msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
277         msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
278         if (enable)
279                 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
280         else
281                 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
282
283         ret = adis16203_spi_write_reg_16(dev, ADIS16203_MSC_CTRL, msc);
284
285 error_ret:
286         return ret;
287 }
288
289 static int adis16203_check_status(struct device *dev)
290 {
291         u16 status;
292         int ret;
293
294         ret = adis16203_spi_read_reg_16(dev, ADIS16203_DIAG_STAT, &status);
295         if (ret < 0) {
296                 dev_err(dev, "Reading status failed\n");
297                 goto error_ret;
298         }
299         ret = status & 0x1F;
300
301         if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
302                 dev_err(dev, "Self test failure\n");
303         if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
304                 dev_err(dev, "SPI failure\n");
305         if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
306                 dev_err(dev, "Flash update failed\n");
307         if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
308                 dev_err(dev, "Power supply above 3.625V\n");
309         if (status & ADIS16203_DIAG_STAT_POWER_LOW)
310                 dev_err(dev, "Power supply below 3.15V\n");
311
312 error_ret:
313         return ret;
314 }
315
316 static int adis16203_self_test(struct device *dev)
317 {
318         int ret;
319         ret = adis16203_spi_write_reg_16(dev,
320                         ADIS16203_MSC_CTRL,
321                         ADIS16203_MSC_CTRL_SELF_TEST_EN);
322         if (ret) {
323                 dev_err(dev, "problem starting self test");
324                 goto err_ret;
325         }
326
327         adis16203_check_status(dev);
328
329 err_ret:
330         return ret;
331 }
332
333 static int adis16203_initial_setup(struct adis16203_state *st)
334 {
335         int ret;
336         struct device *dev = &st->indio_dev->dev;
337
338         /* Disable IRQ */
339         ret = adis16203_set_irq(dev, false);
340         if (ret) {
341                 dev_err(dev, "disable irq failed");
342                 goto err_ret;
343         }
344
345         /* Do self test */
346         ret = adis16203_self_test(dev);
347         if (ret) {
348                 dev_err(dev, "self test failure");
349                 goto err_ret;
350         }
351
352         /* Read status register to check the result */
353         ret = adis16203_check_status(dev);
354         if (ret) {
355                 adis16203_reset(dev);
356                 dev_err(dev, "device not playing ball -> reset");
357                 msleep(ADIS16203_STARTUP_DELAY);
358                 ret = adis16203_check_status(dev);
359                 if (ret) {
360                         dev_err(dev, "giving up");
361                         goto err_ret;
362                 }
363         }
364
365         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
366                         st->us->chip_select, st->us->irq);
367
368 err_ret:
369         return ret;
370 }
371
372 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16203_read_12bit_unsigned,
373                 ADIS16203_SUPPLY_OUT);
374 static IIO_CONST_ATTR(in0_supply_scale, "0.00122");
375 static IIO_DEV_ATTR_IN_RAW(1, adis16203_read_12bit_unsigned,
376                 ADIS16203_AUX_ADC);
377 static IIO_CONST_ATTR(in1_scale, "0.00061");
378
379 static IIO_DEV_ATTR_INCLI_X(adis16203_read_14bit_signed,
380                 ADIS16203_XINCL_OUT);
381 static IIO_DEV_ATTR_INCLI_Y(adis16203_read_14bit_signed,
382                 ADIS16203_YINCL_OUT);
383 static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR | S_IRUGO,
384                 adis16203_read_14bit_signed,
385                 adis16203_write_16bit,
386                 ADIS16203_INCL_NULL);
387 static IIO_CONST_ATTR(incli_scale, "0.025");
388
389 static IIO_DEV_ATTR_TEMP_RAW(adis16203_read_temp);
390 static IIO_CONST_ATTR(temp_offset, "25");
391 static IIO_CONST_ATTR(temp_scale, "-0.47");
392
393 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16203_write_reset, 0);
394
395 static struct attribute *adis16203_attributes[] = {
396         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
397         &iio_const_attr_in0_supply_scale.dev_attr.attr,
398         &iio_dev_attr_temp_raw.dev_attr.attr,
399         &iio_const_attr_temp_offset.dev_attr.attr,
400         &iio_const_attr_temp_scale.dev_attr.attr,
401         &iio_dev_attr_reset.dev_attr.attr,
402         &iio_dev_attr_in1_raw.dev_attr.attr,
403         &iio_const_attr_in1_scale.dev_attr.attr,
404         &iio_dev_attr_incli_x_raw.dev_attr.attr,
405         &iio_dev_attr_incli_y_raw.dev_attr.attr,
406         &iio_dev_attr_incli_x_offset.dev_attr.attr,
407         &iio_const_attr_incli_scale.dev_attr.attr,
408         NULL
409 };
410
411 static const struct attribute_group adis16203_attribute_group = {
412         .attrs = adis16203_attributes,
413 };
414
415 static int __devinit adis16203_probe(struct spi_device *spi)
416 {
417         int ret, regdone = 0;
418         struct adis16203_state *st = kzalloc(sizeof *st, GFP_KERNEL);
419         if (!st) {
420                 ret =  -ENOMEM;
421                 goto error_ret;
422         }
423         /* this is only used for removal purposes */
424         spi_set_drvdata(spi, st);
425
426         /* Allocate the comms buffers */
427         st->rx = kzalloc(sizeof(*st->rx)*ADIS16203_MAX_RX, GFP_KERNEL);
428         if (st->rx == NULL) {
429                 ret = -ENOMEM;
430                 goto error_free_st;
431         }
432         st->tx = kzalloc(sizeof(*st->tx)*ADIS16203_MAX_TX, GFP_KERNEL);
433         if (st->tx == NULL) {
434                 ret = -ENOMEM;
435                 goto error_free_rx;
436         }
437         st->us = spi;
438         mutex_init(&st->buf_lock);
439         /* setup the industrialio driver allocated elements */
440         st->indio_dev = iio_allocate_device(0);
441         if (st->indio_dev == NULL) {
442                 ret = -ENOMEM;
443                 goto error_free_tx;
444         }
445         st->indio_dev->name = spi->dev.driver->name;
446         st->indio_dev->dev.parent = &spi->dev;
447         st->indio_dev->attrs = &adis16203_attribute_group;
448         st->indio_dev->dev_data = (void *)(st);
449         st->indio_dev->driver_module = THIS_MODULE;
450         st->indio_dev->modes = INDIO_DIRECT_MODE;
451
452         ret = adis16203_configure_ring(st->indio_dev);
453         if (ret)
454                 goto error_free_dev;
455
456         ret = iio_device_register(st->indio_dev);
457         if (ret)
458                 goto error_unreg_ring_funcs;
459         regdone = 1;
460
461         ret = adis16203_initialize_ring(st->indio_dev->ring);
462         if (ret) {
463                 printk(KERN_ERR "failed to initialize the ring\n");
464                 goto error_unreg_ring_funcs;
465         }
466
467         if (spi->irq) {
468                 ret = adis16203_probe_trigger(st->indio_dev);
469                 if (ret)
470                         goto error_uninitialize_ring;
471         }
472
473         /* Get the device into a sane initial state */
474         ret = adis16203_initial_setup(st);
475         if (ret)
476                 goto error_remove_trigger;
477         return 0;
478
479 error_remove_trigger:
480         adis16203_remove_trigger(st->indio_dev);
481 error_uninitialize_ring:
482         adis16203_uninitialize_ring(st->indio_dev->ring);
483 error_unreg_ring_funcs:
484         adis16203_unconfigure_ring(st->indio_dev);
485 error_free_dev:
486         if (regdone)
487                 iio_device_unregister(st->indio_dev);
488         else
489                 iio_free_device(st->indio_dev);
490 error_free_tx:
491         kfree(st->tx);
492 error_free_rx:
493         kfree(st->rx);
494 error_free_st:
495         kfree(st);
496 error_ret:
497         return ret;
498 }
499
500 static int adis16203_remove(struct spi_device *spi)
501 {
502         struct adis16203_state *st = spi_get_drvdata(spi);
503         struct iio_dev *indio_dev = st->indio_dev;
504
505         flush_scheduled_work();
506
507         adis16203_remove_trigger(indio_dev);
508         adis16203_uninitialize_ring(indio_dev->ring);
509         iio_device_unregister(indio_dev);
510         adis16203_unconfigure_ring(indio_dev);
511         kfree(st->tx);
512         kfree(st->rx);
513         kfree(st);
514
515         return 0;
516 }
517
518 static struct spi_driver adis16203_driver = {
519         .driver = {
520                 .name = "adis16203",
521                 .owner = THIS_MODULE,
522         },
523         .probe = adis16203_probe,
524         .remove = __devexit_p(adis16203_remove),
525 };
526
527 static __init int adis16203_init(void)
528 {
529         return spi_register_driver(&adis16203_driver);
530 }
531 module_init(adis16203_init);
532
533 static __exit void adis16203_exit(void)
534 {
535         spi_unregister_driver(&adis16203_driver);
536 }
537 module_exit(adis16203_exit);
538
539 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
540 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
541 MODULE_LICENSE("GPL v2");