]> Pileus Git - ~andy/linux/blob - drivers/staging/iio/accel/adis16201_core.c
Merge branch 'perf-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[~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 enum adis16201_chan {
28         in_supply,
29         temp,
30         accel_x,
31         accel_y,
32         incli_x,
33         incli_y,
34         in_aux,
35 };
36
37 /**
38  * adis16201_spi_write_reg_8() - write single byte to a register
39  * @dev: device associated with child of actual device (iio_dev or iio_trig)
40  * @reg_address: the address of the register to be written
41  * @val: the value to write
42  **/
43 static int adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
44                 u8 reg_address,
45                 u8 val)
46 {
47         int ret;
48         struct adis16201_state *st = iio_priv(indio_dev);
49
50         mutex_lock(&st->buf_lock);
51         st->tx[0] = ADIS16201_WRITE_REG(reg_address);
52         st->tx[1] = val;
53
54         ret = spi_write(st->us, st->tx, 2);
55         mutex_unlock(&st->buf_lock);
56
57         return ret;
58 }
59
60 /**
61  * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
62  * @indio_dev: iio device associated with child of actual device
63  * @reg_address: the address of the lower of the two registers. Second register
64  *               is assumed to have address one greater.
65  * @val: value to be written
66  **/
67 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
68                                       u8 lower_reg_address,
69                                       u16 value)
70 {
71         int ret;
72         struct spi_message msg;
73         struct adis16201_state *st = iio_priv(indio_dev);
74         struct spi_transfer xfers[] = {
75                 {
76                         .tx_buf = st->tx,
77                         .bits_per_word = 8,
78                         .len = 2,
79                         .cs_change = 1,
80                 }, {
81                         .tx_buf = st->tx + 2,
82                         .bits_per_word = 8,
83                         .len = 2,
84                 },
85         };
86
87         mutex_lock(&st->buf_lock);
88         st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
89         st->tx[1] = value & 0xFF;
90         st->tx[2] = ADIS16201_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  * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
104  * @indio_dev: iio device associated with child of actual device
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 adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
110                 u8 lower_reg_address,
111                 u16 *val)
112 {
113         struct spi_message msg;
114         struct adis16201_state *st = iio_priv(indio_dev);
115         int ret;
116         struct spi_transfer xfers[] = {
117                 {
118                         .tx_buf = st->tx,
119                         .bits_per_word = 8,
120                         .len = 2,
121                         .cs_change = 1,
122                         .delay_usecs = 20,
123                 }, {
124                         .rx_buf = st->rx,
125                         .bits_per_word = 8,
126                         .len = 2,
127                         .delay_usecs = 20,
128                 },
129         };
130
131         mutex_lock(&st->buf_lock);
132         st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
133         st->tx[1] = 0;
134
135         spi_message_init(&msg);
136         spi_message_add_tail(&xfers[0], &msg);
137         spi_message_add_tail(&xfers[1], &msg);
138         ret = spi_sync(st->us, &msg);
139         if (ret) {
140                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
141                                 lower_reg_address);
142                 goto error_ret;
143         }
144         *val = (st->rx[0] << 8) | st->rx[1];
145
146 error_ret:
147         mutex_unlock(&st->buf_lock);
148         return ret;
149 }
150
151 static int adis16201_reset(struct iio_dev *indio_dev)
152 {
153         int ret;
154         struct adis16201_state *st = iio_priv(indio_dev);
155
156         ret = adis16201_spi_write_reg_8(indio_dev,
157                         ADIS16201_GLOB_CMD,
158                         ADIS16201_GLOB_CMD_SW_RESET);
159         if (ret)
160                 dev_err(&st->us->dev, "problem resetting device");
161
162         return ret;
163 }
164
165 static ssize_t adis16201_write_reset(struct device *dev,
166                 struct device_attribute *attr,
167                 const char *buf, size_t len)
168 {
169         int ret;
170         bool res;
171
172         if (len < 1)
173                 return -EINVAL;
174         ret = strtobool(buf, &res);
175         if (ret || !res)
176                 return ret;
177         return adis16201_reset(dev_get_drvdata(dev));
178 }
179
180 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
181 {
182         int ret = 0;
183         u16 msc;
184
185         ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
186         if (ret)
187                 goto error_ret;
188
189         msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
190         msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
191         if (enable)
192                 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
193         else
194                 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
195
196         ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
197
198 error_ret:
199         return ret;
200 }
201
202 static int adis16201_check_status(struct iio_dev *indio_dev)
203 {
204         u16 status;
205         int ret;
206
207         ret = adis16201_spi_read_reg_16(indio_dev,
208                                         ADIS16201_DIAG_STAT, &status);
209         if (ret < 0) {
210                 dev_err(&indio_dev->dev, "Reading status failed\n");
211                 goto error_ret;
212         }
213         ret = status & 0xF;
214         if (ret)
215                 ret = -EFAULT;
216
217         if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
218                 dev_err(&indio_dev->dev, "SPI failure\n");
219         if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
220                 dev_err(&indio_dev->dev, "Flash update failed\n");
221         if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
222                 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
223         if (status & ADIS16201_DIAG_STAT_POWER_LOW)
224                 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
225
226 error_ret:
227         return ret;
228 }
229
230 static int adis16201_self_test(struct iio_dev *indio_dev)
231 {
232         int ret;
233         ret = adis16201_spi_write_reg_16(indio_dev,
234                         ADIS16201_MSC_CTRL,
235                         ADIS16201_MSC_CTRL_SELF_TEST_EN);
236         if (ret) {
237                 dev_err(&indio_dev->dev, "problem starting self test");
238                 goto err_ret;
239         }
240
241         ret = adis16201_check_status(indio_dev);
242
243 err_ret:
244         return ret;
245 }
246
247 static int adis16201_initial_setup(struct iio_dev *indio_dev)
248 {
249         int ret;
250         struct device *dev = &indio_dev->dev;
251
252         /* Disable IRQ */
253         ret = adis16201_set_irq(indio_dev, false);
254         if (ret) {
255                 dev_err(dev, "disable irq failed");
256                 goto err_ret;
257         }
258
259         /* Do self test */
260         ret = adis16201_self_test(indio_dev);
261         if (ret) {
262                 dev_err(dev, "self test failure");
263                 goto err_ret;
264         }
265
266         /* Read status register to check the result */
267         ret = adis16201_check_status(indio_dev);
268         if (ret) {
269                 adis16201_reset(indio_dev);
270                 dev_err(dev, "device not playing ball -> reset");
271                 msleep(ADIS16201_STARTUP_DELAY);
272                 ret = adis16201_check_status(indio_dev);
273                 if (ret) {
274                         dev_err(dev, "giving up");
275                         goto err_ret;
276                 }
277         }
278
279 err_ret:
280         return ret;
281 }
282
283 static u8 adis16201_addresses[7][2] = {
284         [in_supply] = { ADIS16201_SUPPLY_OUT, },
285         [temp] = { ADIS16201_TEMP_OUT },
286         [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
287         [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
288         [in_aux] = { ADIS16201_AUX_ADC },
289         [incli_x] = { ADIS16201_XINCL_OUT },
290         [incli_y] = { ADIS16201_YINCL_OUT },
291 };
292
293 static int adis16201_read_raw(struct iio_dev *indio_dev,
294                               struct iio_chan_spec const *chan,
295                               int *val, int *val2,
296                               long mask)
297 {
298         int ret;
299         int bits;
300         u8 addr;
301         s16 val16;
302
303         switch (mask) {
304         case 0:
305                 mutex_lock(&indio_dev->mlock);
306                 addr = adis16201_addresses[chan->address][0];
307                 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
308                 if (ret) {
309                         mutex_unlock(&indio_dev->mlock);
310                         return ret;
311                 }
312
313                 if (val16 & ADIS16201_ERROR_ACTIVE) {
314                         ret = adis16201_check_status(indio_dev);
315                         if (ret) {
316                                 mutex_unlock(&indio_dev->mlock);
317                                 return ret;
318                         }
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;
471         struct iio_dev *indio_dev;
472
473         /* setup the industrialio driver allocated elements */
474         indio_dev = iio_allocate_device(sizeof(*st));
475         if (indio_dev == NULL) {
476                 ret = -ENOMEM;
477                 goto error_ret;
478         }
479         st = iio_priv(indio_dev);
480         /* this is only used for removal purposes */
481         spi_set_drvdata(spi, indio_dev);
482
483         st->us = spi;
484         mutex_init(&st->buf_lock);
485
486         indio_dev->name = spi->dev.driver->name;
487         indio_dev->dev.parent = &spi->dev;
488         indio_dev->info = &adis16201_info;
489
490         indio_dev->channels = adis16201_channels;
491         indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
492         indio_dev->modes = INDIO_DIRECT_MODE;
493
494         ret = adis16201_configure_ring(indio_dev);
495         if (ret)
496                 goto error_free_dev;
497
498         ret = iio_device_register(indio_dev);
499         if (ret)
500                 goto error_unreg_ring_funcs;
501         regdone = 1;
502
503         ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
504                                           adis16201_channels,
505                                           ARRAY_SIZE(adis16201_channels));
506         if (ret) {
507                 printk(KERN_ERR "failed to initialize the ring\n");
508                 goto error_unreg_ring_funcs;
509         }
510
511         if (spi->irq) {
512                 ret = adis16201_probe_trigger(indio_dev);
513                 if (ret)
514                         goto error_uninitialize_ring;
515         }
516
517         /* Get the device into a sane initial state */
518         ret = adis16201_initial_setup(indio_dev);
519         if (ret)
520                 goto error_remove_trigger;
521         return 0;
522
523 error_remove_trigger:
524         adis16201_remove_trigger(indio_dev);
525 error_uninitialize_ring:
526         iio_ring_buffer_unregister(indio_dev->ring);
527 error_unreg_ring_funcs:
528         adis16201_unconfigure_ring(indio_dev);
529 error_free_dev:
530         if (regdone)
531                 iio_device_unregister(indio_dev);
532         else
533                 iio_free_device(indio_dev);
534 error_ret:
535         return ret;
536 }
537
538 static int adis16201_remove(struct spi_device *spi)
539 {
540         struct iio_dev *indio_dev = spi_get_drvdata(spi);
541
542         adis16201_remove_trigger(indio_dev);
543         iio_ring_buffer_unregister(indio_dev->ring);
544         iio_device_unregister(indio_dev);
545         adis16201_unconfigure_ring(indio_dev);
546
547         return 0;
548 }
549
550 static struct spi_driver adis16201_driver = {
551         .driver = {
552                 .name = "adis16201",
553                 .owner = THIS_MODULE,
554         },
555         .probe = adis16201_probe,
556         .remove = __devexit_p(adis16201_remove),
557 };
558
559 static __init int adis16201_init(void)
560 {
561         return spi_register_driver(&adis16201_driver);
562 }
563 module_init(adis16201_init);
564
565 static __exit void adis16201_exit(void)
566 {
567         spi_unregister_driver(&adis16201_driver);
568 }
569 module_exit(adis16201_exit);
570
571 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
572 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
573 MODULE_LICENSE("GPL v2");