]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/accel/adis16201_core.c
Merge branch 'iommu-for-tony' of git://github.com/ohadbc/omap-iommu into devel-fixes
[~andy/linux] / drivers / staging / iio / accel / adis16201_core.c
1 /*
2  * ADIS16201 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/delay.h>
10 #include <linux/mutex.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/spi/spi.h>
14 #include <linux/slab.h>
15 #include <linux/sysfs.h>
16
17 #include "../iio.h"
18 #include "../sysfs.h"
19 #include "../ring_generic.h"
20
21 #include "accel.h"
22 #include "inclinometer.h"
23 #include "../adc/adc.h"
24
25 #include "adis16201.h"
26
27 #define DRIVER_NAME             "adis16201"
28
29 enum adis16201_chan {
30         in_supply,
31         temp,
32         accel_x,
33         accel_y,
34         incli_x,
35         incli_y,
36         in_aux,
37 };
38
39 /**
40  * adis16201_spi_write_reg_8() - write single byte to a register
41  * @dev: device associated with child of actual device (iio_dev or iio_trig)
42  * @reg_address: the address of the register to be written
43  * @val: the value to write
44  **/
45 static int adis16201_spi_write_reg_8(struct device *dev,
46                 u8 reg_address,
47                 u8 val)
48 {
49         int ret;
50         struct iio_dev *indio_dev = dev_get_drvdata(dev);
51         struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
52
53         mutex_lock(&st->buf_lock);
54         st->tx[0] = ADIS16201_WRITE_REG(reg_address);
55         st->tx[1] = val;
56
57         ret = spi_write(st->us, st->tx, 2);
58         mutex_unlock(&st->buf_lock);
59
60         return ret;
61 }
62
63 /**
64  * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
65  * @indio_dev: iio device associated with child of actual device
66  * @reg_address: the address of the lower of the two registers. Second register
67  *               is assumed to have address one greater.
68  * @val: value to be written
69  **/
70 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
71                                       u8 lower_reg_address,
72                                       u16 value)
73 {
74         int ret;
75         struct spi_message msg;
76         struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
77         struct spi_transfer xfers[] = {
78                 {
79                         .tx_buf = st->tx,
80                         .bits_per_word = 8,
81                         .len = 2,
82                         .cs_change = 1,
83                 }, {
84                         .tx_buf = st->tx + 2,
85                         .bits_per_word = 8,
86                         .len = 2,
87                 },
88         };
89
90         mutex_lock(&st->buf_lock);
91         st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
92         st->tx[1] = value & 0xFF;
93         st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1);
94         st->tx[3] = (value >> 8) & 0xFF;
95
96         spi_message_init(&msg);
97         spi_message_add_tail(&xfers[0], &msg);
98         spi_message_add_tail(&xfers[1], &msg);
99         ret = spi_sync(st->us, &msg);
100         mutex_unlock(&st->buf_lock);
101
102         return ret;
103 }
104
105 /**
106  * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
107  * @indio_dev: iio device associated with child of actual device
108  * @reg_address: the address of the lower of the two registers. Second register
109  *               is assumed to have address one greater.
110  * @val: somewhere to pass back the value read
111  **/
112 static int adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
113                 u8 lower_reg_address,
114                 u16 *val)
115 {
116         struct spi_message msg;
117         struct adis16201_state *st = iio_dev_get_devdata(indio_dev);
118         int ret;
119         struct spi_transfer xfers[] = {
120                 {
121                         .tx_buf = st->tx,
122                         .bits_per_word = 8,
123                         .len = 2,
124                         .cs_change = 1,
125                         .delay_usecs = 20,
126                 }, {
127                         .rx_buf = st->rx,
128                         .bits_per_word = 8,
129                         .len = 2,
130                         .delay_usecs = 20,
131                 },
132         };
133
134         mutex_lock(&st->buf_lock);
135         st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
136         st->tx[1] = 0;
137
138         spi_message_init(&msg);
139         spi_message_add_tail(&xfers[0], &msg);
140         spi_message_add_tail(&xfers[1], &msg);
141         ret = spi_sync(st->us, &msg);
142         if (ret) {
143                 dev_err(&st->us->dev, "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 int adis16201_reset(struct device *dev)
155 {
156         int ret;
157         ret = adis16201_spi_write_reg_8(dev,
158                         ADIS16201_GLOB_CMD,
159                         ADIS16201_GLOB_CMD_SW_RESET);
160         if (ret)
161                 dev_err(dev, "problem resetting device");
162
163         return ret;
164 }
165
166 static ssize_t adis16201_write_reset(struct device *dev,
167                 struct device_attribute *attr,
168                 const char *buf, size_t len)
169 {
170         if (len < 1)
171                 return -EINVAL;
172         switch (buf[0]) {
173         case '1':
174         case 'y':
175         case 'Y':
176                 return adis16201_reset(dev);
177         }
178         return -EINVAL;
179 }
180
181 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
182 {
183         int ret = 0;
184         u16 msc;
185
186         ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
187         if (ret)
188                 goto error_ret;
189
190         msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
191         msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
192         if (enable)
193                 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
194         else
195                 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
196
197         ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
198
199 error_ret:
200         return ret;
201 }
202
203 static int adis16201_check_status(struct iio_dev *indio_dev)
204 {
205         u16 status;
206         int ret;
207
208         ret = adis16201_spi_read_reg_16(indio_dev,
209                                         ADIS16201_DIAG_STAT, &status);
210         if (ret < 0) {
211                 dev_err(&indio_dev->dev, "Reading status failed\n");
212                 goto error_ret;
213         }
214         ret = status & 0xF;
215         if (ret)
216                 ret = -EFAULT;
217
218         if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
219                 dev_err(&indio_dev->dev, "SPI failure\n");
220         if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
221                 dev_err(&indio_dev->dev, "Flash update failed\n");
222         if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
223                 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
224         if (status & ADIS16201_DIAG_STAT_POWER_LOW)
225                 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
226
227 error_ret:
228         return ret;
229 }
230
231 static int adis16201_self_test(struct iio_dev *indio_dev)
232 {
233         int ret;
234         ret = adis16201_spi_write_reg_16(indio_dev,
235                         ADIS16201_MSC_CTRL,
236                         ADIS16201_MSC_CTRL_SELF_TEST_EN);
237         if (ret) {
238                 dev_err(&indio_dev->dev, "problem starting self test");
239                 goto err_ret;
240         }
241
242         ret = adis16201_check_status(indio_dev);
243
244 err_ret:
245         return ret;
246 }
247
248 static int adis16201_initial_setup(struct adis16201_state *st)
249 {
250         int ret;
251         struct device *dev = &st->indio_dev->dev;
252
253         /* Disable IRQ */
254         ret = adis16201_set_irq(st->indio_dev, false);
255         if (ret) {
256                 dev_err(dev, "disable irq failed");
257                 goto err_ret;
258         }
259
260         /* Do self test */
261         ret = adis16201_self_test(st->indio_dev);
262         if (ret) {
263                 dev_err(dev, "self test failure");
264                 goto err_ret;
265         }
266
267         /* Read status register to check the result */
268         ret = adis16201_check_status(st->indio_dev);
269         if (ret) {
270                 adis16201_reset(dev);
271                 dev_err(dev, "device not playing ball -> reset");
272                 msleep(ADIS16201_STARTUP_DELAY);
273                 ret = adis16201_check_status(st->indio_dev);
274                 if (ret) {
275                         dev_err(dev, "giving up");
276                         goto err_ret;
277                 }
278         }
279
280         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
281                         st->us->chip_select, st->us->irq);
282
283 err_ret:
284         return ret;
285 }
286
287 static u8 adis16201_addresses[7][2] = {
288         [in_supply] = { ADIS16201_SUPPLY_OUT, },
289         [temp] = { ADIS16201_TEMP_OUT },
290         [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
291         [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
292         [in_aux] = { ADIS16201_AUX_ADC },
293         [incli_x] = { ADIS16201_XINCL_OUT },
294         [incli_y] = { ADIS16201_YINCL_OUT },
295 };
296
297 static int adis16201_read_raw(struct iio_dev *indio_dev,
298                               struct iio_chan_spec const *chan,
299                               int *val, int *val2,
300                               long mask)
301 {
302         int ret;
303         int bits;
304         u8 addr;
305         s16 val16;
306
307         switch (mask) {
308         case 0:
309                 mutex_lock(&indio_dev->mlock);
310                 addr = adis16201_addresses[chan->address][0];
311                 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
312                 if (ret)
313                         return ret;
314
315                 if (val16 & ADIS16201_ERROR_ACTIVE) {
316                         ret = adis16201_check_status(indio_dev);
317                         if (ret)
318                                 return ret;
319                 }
320                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
321                 if (chan->scan_type.sign == 's')
322                         val16 = (s16)(val16 <<
323                                       (16 - chan->scan_type.realbits)) >>
324                                 (16 - chan->scan_type.realbits);
325                 *val = val16;
326                 mutex_unlock(&indio_dev->mlock);
327                 return IIO_VAL_INT;
328         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
329         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
330                 switch (chan->type) {
331                 case IIO_IN:
332                         *val = 0;
333                         if (chan->channel == 0)
334                                 *val2 = 1220;
335                         else
336                                 *val2 = 610;
337                         return IIO_VAL_INT_PLUS_MICRO;
338                 case IIO_TEMP:
339                         *val = 0;
340                         *val2 = -470000;
341                         return IIO_VAL_INT_PLUS_MICRO;
342                 case IIO_ACCEL:
343                         *val = 0;
344                         *val2 = 462500;
345                         return IIO_VAL_INT_PLUS_MICRO;
346                 case IIO_INCLI:
347                         *val = 0;
348                         *val2 = 100000;
349                         return IIO_VAL_INT_PLUS_MICRO;
350                 default:
351                         return -EINVAL;
352                 }
353                 break;
354         case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
355                 *val = 25;
356                 return IIO_VAL_INT;
357         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
358                 switch (chan->type) {
359                 case IIO_ACCEL:
360                         bits = 12;
361                         break;
362                 case IIO_INCLI:
363                         bits = 9;
364                         break;
365                 default:
366                         return -EINVAL;
367                 };
368                 mutex_lock(&indio_dev->mlock);
369                 addr = adis16201_addresses[chan->address][1];
370                 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
371                 if (ret) {
372                         mutex_unlock(&indio_dev->mlock);
373                         return ret;
374                 }
375                 val16 &= (1 << bits) - 1;
376                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
377                 *val = val16;
378                 mutex_unlock(&indio_dev->mlock);
379                 return IIO_VAL_INT;
380         }
381         return -EINVAL;
382 }
383
384 static int adis16201_write_raw(struct iio_dev *indio_dev,
385                                struct iio_chan_spec const *chan,
386                                int val,
387                                int val2,
388                                long mask)
389 {
390         int bits;
391         s16 val16;
392         u8 addr;
393         switch (mask) {
394         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
395                 switch (chan->type) {
396                 case IIO_ACCEL:
397                         bits = 12;
398                         break;
399                 case IIO_INCLI:
400                         bits = 9;
401                         break;
402                 default:
403                         return -EINVAL;
404                 };
405                 val16 = val & ((1 << bits) - 1);
406                 addr = adis16201_addresses[chan->address][1];
407                 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
408         }
409         return -EINVAL;
410 }
411
412 static struct iio_chan_spec adis16201_channels[] = {
413         IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
414                  (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
415                  in_supply, ADIS16201_SCAN_SUPPLY,
416                  IIO_ST('u', 12, 16, 0), 0),
417         IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
418                  (1 << IIO_CHAN_INFO_SCALE_SEPARATE) |
419                  (1 << IIO_CHAN_INFO_OFFSET_SEPARATE),
420                  temp, ADIS16201_SCAN_TEMP,
421                  IIO_ST('u', 12, 16, 0), 0),
422         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
423                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
424                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
425                  accel_x, ADIS16201_SCAN_ACC_X,
426                  IIO_ST('s', 14, 16, 0), 0),
427         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
428                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
429                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
430                  accel_y, ADIS16201_SCAN_ACC_Y,
431                  IIO_ST('s', 14, 16, 0), 0),
432         IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
433                  (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
434                  in_aux, ADIS16201_SCAN_AUX_ADC,
435                  IIO_ST('u', 12, 16, 0), 0),
436         IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_X,
437                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
438                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
439                  incli_x, ADIS16201_SCAN_INCLI_X,
440                  IIO_ST('s', 14, 16, 0), 0),
441         IIO_CHAN(IIO_INCLI, 1, 0, 0, NULL, 0, IIO_MOD_Y,
442                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
443                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
444                  incli_y, ADIS16201_SCAN_INCLI_Y,
445                  IIO_ST('s', 14, 16, 0), 0),
446         IIO_CHAN_SOFT_TIMESTAMP(7)
447 };
448
449 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16201_write_reset, 0);
450
451 static struct attribute *adis16201_attributes[] = {
452         &iio_dev_attr_reset.dev_attr.attr,
453         NULL
454 };
455
456 static const struct attribute_group adis16201_attribute_group = {
457         .attrs = adis16201_attributes,
458 };
459
460 static const struct iio_info adis16201_info = {
461         .attrs = &adis16201_attribute_group,
462         .read_raw = &adis16201_read_raw,
463         .write_raw = &adis16201_write_raw,
464         .driver_module = THIS_MODULE,
465 };
466
467 static int __devinit adis16201_probe(struct spi_device *spi)
468 {
469         int ret, regdone = 0;
470         struct adis16201_state *st = kzalloc(sizeof *st, GFP_KERNEL);
471         if (!st) {
472                 ret =  -ENOMEM;
473                 goto error_ret;
474         }
475         /* this is only used for removal purposes */
476         spi_set_drvdata(spi, st);
477
478         /* Allocate the comms buffers */
479         st->rx = kzalloc(sizeof(*st->rx)*ADIS16201_MAX_RX, GFP_KERNEL);
480         if (st->rx == NULL) {
481                 ret = -ENOMEM;
482                 goto error_free_st;
483         }
484         st->tx = kzalloc(sizeof(*st->tx)*ADIS16201_MAX_TX, GFP_KERNEL);
485         if (st->tx == NULL) {
486                 ret = -ENOMEM;
487                 goto error_free_rx;
488         }
489         st->us = spi;
490         mutex_init(&st->buf_lock);
491         /* setup the industrialio driver allocated elements */
492         st->indio_dev = iio_allocate_device(0);
493         if (st->indio_dev == NULL) {
494                 ret = -ENOMEM;
495                 goto error_free_tx;
496         }
497
498         st->indio_dev->name = spi->dev.driver->name;
499         st->indio_dev->dev.parent = &spi->dev;
500         st->indio_dev->info = &adis16201_info;
501
502         st->indio_dev->channels = adis16201_channels;
503         st->indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
504         st->indio_dev->dev_data = (void *)(st);
505         st->indio_dev->modes = INDIO_DIRECT_MODE;
506
507         ret = adis16201_configure_ring(st->indio_dev);
508         if (ret)
509                 goto error_free_dev;
510
511         ret = iio_device_register(st->indio_dev);
512         if (ret)
513                 goto error_unreg_ring_funcs;
514         regdone = 1;
515
516         ret = iio_ring_buffer_register_ex(st->indio_dev->ring, 0,
517                                           adis16201_channels,
518                                           ARRAY_SIZE(adis16201_channels));
519         if (ret) {
520                 printk(KERN_ERR "failed to initialize the ring\n");
521                 goto error_unreg_ring_funcs;
522         }
523
524         if (spi->irq) {
525                 ret = adis16201_probe_trigger(st->indio_dev);
526                 if (ret)
527                         goto error_uninitialize_ring;
528         }
529
530         /* Get the device into a sane initial state */
531         ret = adis16201_initial_setup(st);
532         if (ret)
533                 goto error_remove_trigger;
534         return 0;
535
536 error_remove_trigger:
537         adis16201_remove_trigger(st->indio_dev);
538 error_uninitialize_ring:
539         iio_ring_buffer_unregister(st->indio_dev->ring);
540 error_unreg_ring_funcs:
541         adis16201_unconfigure_ring(st->indio_dev);
542 error_free_dev:
543         if (regdone)
544                 iio_device_unregister(st->indio_dev);
545         else
546                 iio_free_device(st->indio_dev);
547 error_free_tx:
548         kfree(st->tx);
549 error_free_rx:
550         kfree(st->rx);
551 error_free_st:
552         kfree(st);
553 error_ret:
554         return ret;
555 }
556
557 static int adis16201_remove(struct spi_device *spi)
558 {
559         struct adis16201_state *st = spi_get_drvdata(spi);
560         struct iio_dev *indio_dev = st->indio_dev;
561
562         adis16201_remove_trigger(indio_dev);
563         iio_ring_buffer_unregister(indio_dev->ring);
564         iio_device_unregister(indio_dev);
565         adis16201_unconfigure_ring(indio_dev);
566         kfree(st->tx);
567         kfree(st->rx);
568         kfree(st);
569
570         return 0;
571 }
572
573 static struct spi_driver adis16201_driver = {
574         .driver = {
575                 .name = "adis16201",
576                 .owner = THIS_MODULE,
577         },
578         .probe = adis16201_probe,
579         .remove = __devexit_p(adis16201_remove),
580 };
581
582 static __init int adis16201_init(void)
583 {
584         return spi_register_driver(&adis16201_driver);
585 }
586 module_init(adis16201_init);
587
588 static __exit void adis16201_exit(void)
589 {
590         spi_unregister_driver(&adis16201_driver);
591 }
592 module_exit(adis16201_exit);
593
594 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
595 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
596 MODULE_LICENSE("GPL v2");