Merge remote-tracking branch 'upstream' into next
[cascardo/linux.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/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 #include <linux/list.h>
17 #include <linux/module.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22
23 #include "adis16209.h"
24
25 #define DRIVER_NAME             "adis16209"
26
27 /**
28  * adis16209_spi_write_reg_8() - write single byte to a register
29  * @indio_dev: iio device associated with actual device
30  * @reg_address: the address of the register to be written
31  * @val: the value to write
32  **/
33 static int adis16209_spi_write_reg_8(struct iio_dev *indio_dev,
34                                      u8 reg_address,
35                                      u8 val)
36 {
37         int ret;
38         struct adis16209_state *st = iio_priv(indio_dev);
39
40         mutex_lock(&st->buf_lock);
41         st->tx[0] = ADIS16209_WRITE_REG(reg_address);
42         st->tx[1] = val;
43
44         ret = spi_write(st->us, st->tx, 2);
45         mutex_unlock(&st->buf_lock);
46
47         return ret;
48 }
49
50 /**
51  * adis16209_spi_write_reg_16() - write 2 bytes to a pair of registers
52  * @indio_dev: iio device associated actual device
53  * @reg_address: the address of the lower of the two registers. Second register
54  *               is assumed to have address one greater.
55  * @val: value to be written
56  **/
57 static int adis16209_spi_write_reg_16(struct iio_dev *indio_dev,
58                                       u8 lower_reg_address,
59                                       u16 value)
60 {
61         int ret;
62         struct spi_message msg;
63         struct adis16209_state *st = iio_priv(indio_dev);
64         struct spi_transfer xfers[] = {
65                 {
66                         .tx_buf = st->tx,
67                         .bits_per_word = 8,
68                         .len = 2,
69                         .cs_change = 1,
70                         .delay_usecs = 30,
71                 }, {
72                         .tx_buf = st->tx + 2,
73                         .bits_per_word = 8,
74                         .len = 2,
75                         .delay_usecs = 30,
76                 },
77         };
78
79         mutex_lock(&st->buf_lock);
80         st->tx[0] = ADIS16209_WRITE_REG(lower_reg_address);
81         st->tx[1] = value & 0xFF;
82         st->tx[2] = ADIS16209_WRITE_REG(lower_reg_address + 1);
83         st->tx[3] = (value >> 8) & 0xFF;
84
85         spi_message_init(&msg);
86         spi_message_add_tail(&xfers[0], &msg);
87         spi_message_add_tail(&xfers[1], &msg);
88         ret = spi_sync(st->us, &msg);
89         mutex_unlock(&st->buf_lock);
90
91         return ret;
92 }
93
94 /**
95  * adis16209_spi_read_reg_16() - read 2 bytes from a 16-bit register
96  * @indio_dev: iio device associated with device
97  * @reg_address: the address of the lower of the two registers. Second register
98  *               is assumed to have address one greater.
99  * @val: somewhere to pass back the value read
100  **/
101 static int adis16209_spi_read_reg_16(struct iio_dev *indio_dev,
102                                      u8 lower_reg_address,
103                                      u16 *val)
104 {
105         struct spi_message msg;
106         struct adis16209_state *st = iio_priv(indio_dev);
107         int ret;
108         struct spi_transfer xfers[] = {
109                 {
110                         .tx_buf = st->tx,
111                         .bits_per_word = 8,
112                         .len = 2,
113                         .cs_change = 1,
114                         .delay_usecs = 30,
115                 }, {
116                         .rx_buf = st->rx,
117                         .bits_per_word = 8,
118                         .len = 2,
119                         .delay_usecs = 30,
120                 },
121         };
122
123         mutex_lock(&st->buf_lock);
124         st->tx[0] = ADIS16209_READ_REG(lower_reg_address);
125         st->tx[1] = 0;
126
127         spi_message_init(&msg);
128         spi_message_add_tail(&xfers[0], &msg);
129         spi_message_add_tail(&xfers[1], &msg);
130         ret = spi_sync(st->us, &msg);
131         if (ret) {
132                 dev_err(&st->us->dev,
133                         "problem when reading 16 bit register 0x%02X",
134                         lower_reg_address);
135                 goto error_ret;
136         }
137         *val = (st->rx[0] << 8) | st->rx[1];
138
139 error_ret:
140         mutex_unlock(&st->buf_lock);
141         return ret;
142 }
143
144 static int adis16209_reset(struct iio_dev *indio_dev)
145 {
146         int ret;
147         ret = adis16209_spi_write_reg_8(indio_dev,
148                         ADIS16209_GLOB_CMD,
149                         ADIS16209_GLOB_CMD_SW_RESET);
150         if (ret)
151                 dev_err(&indio_dev->dev, "problem resetting device");
152
153         return ret;
154 }
155
156 int adis16209_set_irq(struct iio_dev *indio_dev, bool enable)
157 {
158         int ret = 0;
159         u16 msc;
160
161         ret = adis16209_spi_read_reg_16(indio_dev, ADIS16209_MSC_CTRL, &msc);
162         if (ret)
163                 goto error_ret;
164
165         msc |= ADIS16209_MSC_CTRL_ACTIVE_HIGH;
166         msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_DIO2;
167         if (enable)
168                 msc |= ADIS16209_MSC_CTRL_DATA_RDY_EN;
169         else
170                 msc &= ~ADIS16209_MSC_CTRL_DATA_RDY_EN;
171
172         ret = adis16209_spi_write_reg_16(indio_dev, ADIS16209_MSC_CTRL, msc);
173
174 error_ret:
175         return ret;
176 }
177
178 static int adis16209_check_status(struct iio_dev *indio_dev)
179 {
180         u16 status;
181         int ret;
182
183         ret = adis16209_spi_read_reg_16(indio_dev,
184                                         ADIS16209_DIAG_STAT, &status);
185         if (ret < 0) {
186                 dev_err(&indio_dev->dev, "Reading status failed\n");
187                 goto error_ret;
188         }
189         ret = status & 0x1F;
190
191         if (status & ADIS16209_DIAG_STAT_SELFTEST_FAIL)
192                 dev_err(&indio_dev->dev, "Self test failure\n");
193         if (status & ADIS16209_DIAG_STAT_SPI_FAIL)
194                 dev_err(&indio_dev->dev, "SPI failure\n");
195         if (status & ADIS16209_DIAG_STAT_FLASH_UPT)
196                 dev_err(&indio_dev->dev, "Flash update failed\n");
197         if (status & ADIS16209_DIAG_STAT_POWER_HIGH)
198                 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
199         if (status & ADIS16209_DIAG_STAT_POWER_LOW)
200                 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
201
202 error_ret:
203         return ret;
204 }
205
206 static int adis16209_self_test(struct iio_dev *indio_dev)
207 {
208         int ret;
209         ret = adis16209_spi_write_reg_16(indio_dev,
210                         ADIS16209_MSC_CTRL,
211                         ADIS16209_MSC_CTRL_SELF_TEST_EN);
212         if (ret) {
213                 dev_err(&indio_dev->dev, "problem starting self test");
214                 goto err_ret;
215         }
216
217         adis16209_check_status(indio_dev);
218
219 err_ret:
220         return ret;
221 }
222
223 static int adis16209_initial_setup(struct iio_dev *indio_dev)
224 {
225         int ret;
226
227         /* Disable IRQ */
228         ret = adis16209_set_irq(indio_dev, false);
229         if (ret) {
230                 dev_err(&indio_dev->dev, "disable irq failed");
231                 goto err_ret;
232         }
233
234         /* Do self test */
235         ret = adis16209_self_test(indio_dev);
236         if (ret) {
237                 dev_err(&indio_dev->dev, "self test failure");
238                 goto err_ret;
239         }
240
241         /* Read status register to check the result */
242         ret = adis16209_check_status(indio_dev);
243         if (ret) {
244                 adis16209_reset(indio_dev);
245                 dev_err(&indio_dev->dev, "device not playing ball -> reset");
246                 msleep(ADIS16209_STARTUP_DELAY);
247                 ret = adis16209_check_status(indio_dev);
248                 if (ret) {
249                         dev_err(&indio_dev->dev, "giving up");
250                         goto err_ret;
251                 }
252         }
253
254 err_ret:
255         return ret;
256 }
257
258 enum adis16209_chan {
259         in_supply,
260         temp,
261         accel_x,
262         accel_y,
263         incli_x,
264         incli_y,
265         in_aux,
266         rot,
267 };
268
269 static const u8 adis16209_addresses[8][2] = {
270         [in_supply] = { ADIS16209_SUPPLY_OUT },
271         [in_aux] = { ADIS16209_AUX_ADC },
272         [accel_x] = { ADIS16209_XACCL_OUT, ADIS16209_XACCL_NULL },
273         [accel_y] = { ADIS16209_YACCL_OUT, ADIS16209_YACCL_NULL },
274         [incli_x] = { ADIS16209_XINCL_OUT, ADIS16209_XINCL_NULL },
275         [incli_y] = { ADIS16209_YINCL_OUT, ADIS16209_YINCL_NULL },
276         [rot] = { ADIS16209_ROT_OUT },
277         [temp] = { ADIS16209_TEMP_OUT },
278 };
279
280 static int adis16209_write_raw(struct iio_dev *indio_dev,
281                                struct iio_chan_spec const *chan,
282                                int val,
283                                int val2,
284                                long mask)
285 {
286         int bits;
287         s16 val16;
288         u8 addr;
289         switch (mask) {
290         case IIO_CHAN_INFO_CALIBBIAS:
291                 switch (chan->type) {
292                 case IIO_ACCEL:
293                 case IIO_INCLI:
294                         bits = 14;
295                         break;
296                 default:
297                         return -EINVAL;
298                 };
299                 val16 = val & ((1 << bits) - 1);
300                 addr = adis16209_addresses[chan->address][1];
301                 return adis16209_spi_write_reg_16(indio_dev, addr, val16);
302         }
303         return -EINVAL;
304 }
305
306 static int adis16209_read_raw(struct iio_dev *indio_dev,
307                               struct iio_chan_spec const *chan,
308                               int *val, int *val2,
309                               long mask)
310 {
311         int ret;
312         int bits;
313         u8 addr;
314         s16 val16;
315
316         switch (mask) {
317         case IIO_CHAN_INFO_RAW:
318                 mutex_lock(&indio_dev->mlock);
319                 addr = adis16209_addresses[chan->address][0];
320                 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
321                 if (ret) {
322                         mutex_unlock(&indio_dev->mlock);
323                         return ret;
324                 }
325
326                 if (val16 & ADIS16209_ERROR_ACTIVE) {
327                         ret = adis16209_check_status(indio_dev);
328                         if (ret) {
329                                 mutex_unlock(&indio_dev->mlock);
330                                 return ret;
331                         }
332                 }
333                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
334                 if (chan->scan_type.sign == 's')
335                         val16 = (s16)(val16 <<
336                                       (16 - chan->scan_type.realbits)) >>
337                                 (16 - chan->scan_type.realbits);
338                 *val = val16;
339                 mutex_unlock(&indio_dev->mlock);
340                 return IIO_VAL_INT;
341         case IIO_CHAN_INFO_SCALE:
342                 switch (chan->type) {
343                 case IIO_VOLTAGE:
344                         *val = 0;
345                         if (chan->channel == 0)
346                                 *val2 = 305180;
347                         else
348                                 *val2 = 610500;
349                         return IIO_VAL_INT_PLUS_MICRO;
350                 case IIO_TEMP:
351                         *val = 0;
352                         *val2 = -470000;
353                         return IIO_VAL_INT_PLUS_MICRO;
354                 case IIO_ACCEL:
355                         *val = 0;
356                         *val2 = 2394;
357                         return IIO_VAL_INT_PLUS_MICRO;
358                 case IIO_INCLI:
359                         *val = 0;
360                         *val2 = 436;
361                         return IIO_VAL_INT_PLUS_MICRO;
362                 default:
363                         return -EINVAL;
364                 }
365                 break;
366         case IIO_CHAN_INFO_OFFSET:
367                 *val = 25;
368                 return IIO_VAL_INT;
369         case IIO_CHAN_INFO_CALIBBIAS:
370                 switch (chan->type) {
371                 case IIO_ACCEL:
372                         bits = 14;
373                         break;
374                 default:
375                         return -EINVAL;
376                 };
377                 mutex_lock(&indio_dev->mlock);
378                 addr = adis16209_addresses[chan->address][1];
379                 ret = adis16209_spi_read_reg_16(indio_dev, addr, &val16);
380                 if (ret) {
381                         mutex_unlock(&indio_dev->mlock);
382                         return ret;
383                 }
384                 val16 &= (1 << bits) - 1;
385                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
386                 *val = val16;
387                 mutex_unlock(&indio_dev->mlock);
388                 return IIO_VAL_INT;
389         }
390         return -EINVAL;
391 }
392
393 static struct iio_chan_spec adis16209_channels[] = {
394         {
395                 .type = IIO_VOLTAGE,
396                 .indexed = 1,
397                 .channel = 0,
398                 .extend_name = "supply",
399                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
400                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
401                 .address = in_supply,
402                 .scan_index = ADIS16209_SCAN_SUPPLY,
403                 .scan_type = {
404                         .sign = 'u',
405                         .realbits = 14,
406                         .storagebits = 16,
407                 },
408         }, {
409                 .type = IIO_TEMP,
410                 .indexed = 0,
411                 .channel = 0,
412                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
413                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
414                 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
415                 .address = temp,
416                 .scan_index = ADIS16209_SCAN_TEMP,
417                 .scan_type = {
418                         .sign = 'u',
419                         .realbits = 12,
420                         .storagebits = 16,
421                 },
422         }, {
423                 .type = IIO_ACCEL,
424                 .modified = 1,
425                 .channel2 = IIO_MOD_X,
426                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
427                 IIO_CHAN_INFO_SCALE_SHARED_BIT |
428                 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
429                 .address = accel_x,
430                 .scan_index = ADIS16209_SCAN_ACC_X,
431                 .scan_type = {
432                         .sign = 's',
433                         .realbits = 14,
434                         .storagebits = 16,
435                 },
436         }, {
437                 .type = IIO_ACCEL,
438                 .modified = 1,
439                 .channel2 = IIO_MOD_Y,
440                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
441                 IIO_CHAN_INFO_SCALE_SHARED_BIT |
442                 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
443                 .address = accel_y,
444                 .scan_index = ADIS16209_SCAN_ACC_Y,
445                 .scan_type = {
446                         .sign = 's',
447                         .realbits = 14,
448                         .storagebits = 16,
449                 },
450         }, {
451                 .type = IIO_VOLTAGE,
452                 .indexed = 1,
453                 .channel = 1,
454                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
455                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
456                 .address = in_aux,
457                 .scan_index = ADIS16209_SCAN_AUX_ADC,
458                 .scan_type = {
459                         .sign = 'u',
460                         .realbits = 12,
461                         .storagebits = 16,
462                 },
463         }, {
464                 .type = IIO_INCLI,
465                 .modified = 1,
466                 .channel2 = IIO_MOD_X,
467                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
468                 IIO_CHAN_INFO_SCALE_SHARED_BIT,
469                 .address = incli_x,
470                 .scan_index = ADIS16209_SCAN_INCLI_X,
471                 .scan_type = {
472                         .sign = 's',
473                         .realbits = 14,
474                         .storagebits = 16,
475                 },
476         }, {
477                 .type = IIO_INCLI,
478                 .modified = 1,
479                 .channel2 = IIO_MOD_Y,
480                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
481                 IIO_CHAN_INFO_SCALE_SHARED_BIT,
482                 .address = incli_y,
483                 .scan_index = ADIS16209_SCAN_INCLI_Y,
484                 .scan_type = {
485                         .sign = 's',
486                         .realbits = 14,
487                         .storagebits = 16,
488                 },
489         }, {
490                 .type = IIO_ROT,
491                 .modified = 1,
492                 .channel2 = IIO_MOD_X,
493                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
494                 .address = rot,
495                 .scan_index = ADIS16209_SCAN_ROT,
496                 .scan_type = {
497                         .sign = 's',
498                         .realbits = 14,
499                         .storagebits = 16,
500                 },
501         },
502         IIO_CHAN_SOFT_TIMESTAMP(8)
503 };
504
505 static const struct iio_info adis16209_info = {
506         .read_raw = &adis16209_read_raw,
507         .write_raw = &adis16209_write_raw,
508         .driver_module = THIS_MODULE,
509 };
510
511 static int __devinit adis16209_probe(struct spi_device *spi)
512 {
513         int ret;
514         struct adis16209_state *st;
515         struct iio_dev *indio_dev;
516
517         /* setup the industrialio driver allocated elements */
518         indio_dev = iio_device_alloc(sizeof(*st));
519         if (indio_dev == NULL) {
520                 ret = -ENOMEM;
521                 goto error_ret;
522         }
523         st = iio_priv(indio_dev);
524         /* this is only used for removal purposes */
525         spi_set_drvdata(spi, indio_dev);
526         st->us = spi;
527         mutex_init(&st->buf_lock);
528
529         indio_dev->name = spi->dev.driver->name;
530         indio_dev->dev.parent = &spi->dev;
531         indio_dev->info = &adis16209_info;
532         indio_dev->channels = adis16209_channels;
533         indio_dev->num_channels = ARRAY_SIZE(adis16209_channels);
534         indio_dev->modes = INDIO_DIRECT_MODE;
535
536         ret = adis16209_configure_ring(indio_dev);
537         if (ret)
538                 goto error_free_dev;
539
540         ret = iio_buffer_register(indio_dev,
541                                   adis16209_channels,
542                                   ARRAY_SIZE(adis16209_channels));
543         if (ret) {
544                 printk(KERN_ERR "failed to initialize the ring\n");
545                 goto error_unreg_ring_funcs;
546         }
547
548         if (spi->irq) {
549                 ret = adis16209_probe_trigger(indio_dev);
550                 if (ret)
551                         goto error_uninitialize_ring;
552         }
553
554         /* Get the device into a sane initial state */
555         ret = adis16209_initial_setup(indio_dev);
556         if (ret)
557                 goto error_remove_trigger;
558         ret = iio_device_register(indio_dev);
559         if (ret)
560                 goto error_remove_trigger;
561
562         return 0;
563
564 error_remove_trigger:
565         adis16209_remove_trigger(indio_dev);
566 error_uninitialize_ring:
567         iio_buffer_unregister(indio_dev);
568 error_unreg_ring_funcs:
569         adis16209_unconfigure_ring(indio_dev);
570 error_free_dev:
571         iio_device_free(indio_dev);
572 error_ret:
573         return ret;
574 }
575
576 static int adis16209_remove(struct spi_device *spi)
577 {
578         struct iio_dev *indio_dev = spi_get_drvdata(spi);
579
580         iio_device_unregister(indio_dev);
581         adis16209_remove_trigger(indio_dev);
582         iio_buffer_unregister(indio_dev);
583         adis16209_unconfigure_ring(indio_dev);
584         iio_device_free(indio_dev);
585
586         return 0;
587 }
588
589 static struct spi_driver adis16209_driver = {
590         .driver = {
591                 .name = "adis16209",
592                 .owner = THIS_MODULE,
593         },
594         .probe = adis16209_probe,
595         .remove = __devexit_p(adis16209_remove),
596 };
597 module_spi_driver(adis16209_driver);
598
599 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
600 MODULE_DESCRIPTION("Analog Devices ADIS16209 Digital Vibration Sensor driver");
601 MODULE_LICENSE("GPL v2");
602 MODULE_ALIAS("spi:adis16209");