Merge tag 'mmc-fixes-for-3.7' of git://git.kernel.org/pub/scm/linux/kernel/git/cjb/mmc
[cascardo/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/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/module.h>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21
22 #include "adis16203.h"
23
24 #define DRIVER_NAME             "adis16203"
25
26 /**
27  * adis16203_spi_write_reg_8() - write single byte to a register
28  * @indio_dev: iio device associated with child of actual device
29  * @reg_address: the address of the register to be written
30  * @val: the value to write
31  **/
32 static int adis16203_spi_write_reg_8(struct iio_dev *indio_dev,
33                                      u8 reg_address,
34                                      u8 val)
35 {
36         int ret;
37         struct adis16203_state *st = iio_priv(indio_dev);
38
39         mutex_lock(&st->buf_lock);
40         st->tx[0] = ADIS16203_WRITE_REG(reg_address);
41         st->tx[1] = val;
42
43         ret = spi_write(st->us, st->tx, 2);
44         mutex_unlock(&st->buf_lock);
45
46         return ret;
47 }
48
49 /**
50  * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
51  * @indio_dev: iio device associated with child of actual device
52  * @reg_address: the address of the lower of the two registers. Second register
53  *               is assumed to have address one greater.
54  * @val: value to be written
55  **/
56 static int adis16203_spi_write_reg_16(struct iio_dev *indio_dev,
57                                       u8 lower_reg_address,
58                                       u16 value)
59 {
60         int ret;
61         struct spi_message msg;
62         struct adis16203_state *st = iio_priv(indio_dev);
63         struct spi_transfer xfers[] = {
64                 {
65                         .tx_buf = st->tx,
66                         .bits_per_word = 8,
67                         .len = 2,
68                         .cs_change = 1,
69                 }, {
70                         .tx_buf = st->tx + 2,
71                         .bits_per_word = 8,
72                         .len = 2,
73                 },
74         };
75
76         mutex_lock(&st->buf_lock);
77         st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
78         st->tx[1] = value & 0xFF;
79         st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
80         st->tx[3] = (value >> 8) & 0xFF;
81
82         spi_message_init(&msg);
83         spi_message_add_tail(&xfers[0], &msg);
84         spi_message_add_tail(&xfers[1], &msg);
85         ret = spi_sync(st->us, &msg);
86         mutex_unlock(&st->buf_lock);
87
88         return ret;
89 }
90
91 /**
92  * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
93  * @indio_dev: iio device associated with child of actual device
94  * @reg_address: the address of the lower of the two registers. Second register
95  *               is assumed to have address one greater.
96  * @val: somewhere to pass back the value read
97  **/
98 static int adis16203_spi_read_reg_16(struct iio_dev *indio_dev,
99                 u8 lower_reg_address,
100                 u16 *val)
101 {
102         struct spi_message msg;
103         struct adis16203_state *st = iio_priv(indio_dev);
104         int ret;
105         struct spi_transfer xfers[] = {
106                 {
107                         .tx_buf = st->tx,
108                         .bits_per_word = 8,
109                         .len = 2,
110                         .cs_change = 1,
111                         .delay_usecs = 20,
112                 }, {
113                         .rx_buf = st->rx,
114                         .bits_per_word = 8,
115                         .len = 2,
116                         .delay_usecs = 20,
117                 },
118         };
119
120         mutex_lock(&st->buf_lock);
121         st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
122         st->tx[1] = 0;
123
124         spi_message_init(&msg);
125         spi_message_add_tail(&xfers[0], &msg);
126         spi_message_add_tail(&xfers[1], &msg);
127         ret = spi_sync(st->us, &msg);
128         if (ret) {
129                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
130                                 lower_reg_address);
131                 goto error_ret;
132         }
133         *val = (st->rx[0] << 8) | st->rx[1];
134
135 error_ret:
136         mutex_unlock(&st->buf_lock);
137         return ret;
138 }
139
140 static int adis16203_check_status(struct iio_dev *indio_dev)
141 {
142         u16 status;
143         int ret;
144
145         ret = adis16203_spi_read_reg_16(indio_dev,
146                                         ADIS16203_DIAG_STAT,
147                                         &status);
148         if (ret < 0) {
149                 dev_err(&indio_dev->dev, "Reading status failed\n");
150                 goto error_ret;
151         }
152         ret = status & 0x1F;
153
154         if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
155                 dev_err(&indio_dev->dev, "Self test failure\n");
156         if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
157                 dev_err(&indio_dev->dev, "SPI failure\n");
158         if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
159                 dev_err(&indio_dev->dev, "Flash update failed\n");
160         if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
161                 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
162         if (status & ADIS16203_DIAG_STAT_POWER_LOW)
163                 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
164
165 error_ret:
166         return ret;
167 }
168
169 static int adis16203_reset(struct iio_dev *indio_dev)
170 {
171         int ret;
172         ret = adis16203_spi_write_reg_8(indio_dev,
173                         ADIS16203_GLOB_CMD,
174                         ADIS16203_GLOB_CMD_SW_RESET);
175         if (ret)
176                 dev_err(&indio_dev->dev, "problem resetting device");
177
178         return ret;
179 }
180
181 int adis16203_set_irq(struct iio_dev *indio_dev, bool enable)
182 {
183         int ret = 0;
184         u16 msc;
185
186         ret = adis16203_spi_read_reg_16(indio_dev, ADIS16203_MSC_CTRL, &msc);
187         if (ret)
188                 goto error_ret;
189
190         msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
191         msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
192         if (enable)
193                 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
194         else
195                 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
196
197         ret = adis16203_spi_write_reg_16(indio_dev, ADIS16203_MSC_CTRL, msc);
198
199 error_ret:
200         return ret;
201 }
202
203 static int adis16203_self_test(struct iio_dev *indio_dev)
204 {
205         int ret;
206         ret = adis16203_spi_write_reg_16(indio_dev,
207                         ADIS16203_MSC_CTRL,
208                         ADIS16203_MSC_CTRL_SELF_TEST_EN);
209         if (ret) {
210                 dev_err(&indio_dev->dev, "problem starting self test");
211                 goto err_ret;
212         }
213
214         adis16203_check_status(indio_dev);
215
216 err_ret:
217         return ret;
218 }
219
220 static int adis16203_initial_setup(struct iio_dev *indio_dev)
221 {
222         int ret;
223
224         /* Disable IRQ */
225         ret = adis16203_set_irq(indio_dev, false);
226         if (ret) {
227                 dev_err(&indio_dev->dev, "disable irq failed");
228                 goto err_ret;
229         }
230
231         /* Do self test */
232         ret = adis16203_self_test(indio_dev);
233         if (ret) {
234                 dev_err(&indio_dev->dev, "self test failure");
235                 goto err_ret;
236         }
237
238         /* Read status register to check the result */
239         ret = adis16203_check_status(indio_dev);
240         if (ret) {
241                 adis16203_reset(indio_dev);
242                 dev_err(&indio_dev->dev, "device not playing ball -> reset");
243                 msleep(ADIS16203_STARTUP_DELAY);
244                 ret = adis16203_check_status(indio_dev);
245                 if (ret) {
246                         dev_err(&indio_dev->dev, "giving up");
247                         goto err_ret;
248                 }
249         }
250
251 err_ret:
252         return ret;
253 }
254
255 enum adis16203_chan {
256         in_supply,
257         in_aux,
258         incli_x,
259         incli_y,
260         temp,
261 };
262
263 static u8 adis16203_addresses[5][2] = {
264         [in_supply] = { ADIS16203_SUPPLY_OUT },
265         [in_aux] = { ADIS16203_AUX_ADC },
266         [incli_x] = { ADIS16203_XINCL_OUT, ADIS16203_INCL_NULL},
267         [incli_y] = { ADIS16203_YINCL_OUT },
268         [temp] = { ADIS16203_TEMP_OUT }
269 };
270
271 static int adis16203_write_raw(struct iio_dev *indio_dev,
272                                struct iio_chan_spec const *chan,
273                                int val,
274                                int val2,
275                                long mask)
276 {
277         /* currently only one writable parameter which keeps this simple */
278         u8 addr = adis16203_addresses[chan->address][1];
279         return adis16203_spi_write_reg_16(indio_dev, addr, val & 0x3FFF);
280 }
281
282 static int adis16203_read_raw(struct iio_dev *indio_dev,
283                               struct iio_chan_spec const *chan,
284                               int *val, int *val2,
285                               long mask)
286 {
287         int ret;
288         int bits;
289         u8 addr;
290         s16 val16;
291         switch (mask) {
292         case IIO_CHAN_INFO_RAW:
293                 mutex_lock(&indio_dev->mlock);
294                 addr = adis16203_addresses[chan->address][0];
295                 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
296                 if (ret) {
297                         mutex_unlock(&indio_dev->mlock);
298                         return ret;
299                 }
300
301                 if (val16 & ADIS16203_ERROR_ACTIVE) {
302                         ret = adis16203_check_status(indio_dev);
303                         if (ret) {
304                                 mutex_unlock(&indio_dev->mlock);
305                                 return ret;
306                         }
307                 }
308                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
309                 if (chan->scan_type.sign == 's')
310                         val16 = (s16)(val16 <<
311                                       (16 - chan->scan_type.realbits)) >>
312                                 (16 - chan->scan_type.realbits);
313                 *val = val16;
314                 mutex_unlock(&indio_dev->mlock);
315                 return IIO_VAL_INT;
316         case IIO_CHAN_INFO_SCALE:
317                 switch (chan->type) {
318                 case IIO_VOLTAGE:
319                         if (chan->channel == 0) {
320                                 *val = 1;
321                                 *val2 = 220000; /* 1.22 mV */
322                         } else {
323                                 *val = 0;
324                                 *val2 = 610000; /* 0.61 mV */
325                         }
326                         return IIO_VAL_INT_PLUS_MICRO;
327                 case IIO_TEMP:
328                         *val = -470; /* -0.47 C */
329                         *val2 = 0;
330                         return IIO_VAL_INT_PLUS_MICRO;
331                 case IIO_INCLI:
332                         *val = 0;
333                         *val2 = 25000; /* 0.025 degree */
334                         return IIO_VAL_INT_PLUS_MICRO;
335                 default:
336                         return -EINVAL;
337                 }
338         case IIO_CHAN_INFO_OFFSET:
339                 *val = 25000 / -470 - 1278; /* 25 C = 1278 */
340                 return IIO_VAL_INT;
341         case IIO_CHAN_INFO_CALIBBIAS:
342                 bits = 14;
343                 mutex_lock(&indio_dev->mlock);
344                 addr = adis16203_addresses[chan->address][1];
345                 ret = adis16203_spi_read_reg_16(indio_dev, addr, &val16);
346                 if (ret) {
347                         mutex_unlock(&indio_dev->mlock);
348                         return ret;
349                 }
350                 val16 &= (1 << bits) - 1;
351                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
352                 *val = val16;
353                 mutex_unlock(&indio_dev->mlock);
354                 return IIO_VAL_INT;
355         default:
356                 return -EINVAL;
357         }
358 }
359
360 static const struct iio_chan_spec adis16203_channels[] = {
361         {
362                 .type = IIO_VOLTAGE,
363                 .indexed = 1,
364                 .channel = 0,
365                 .extend_name = "supply",
366                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
367                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
368                 .address = in_supply,
369                 .scan_index = ADIS16203_SCAN_SUPPLY,
370                 .scan_type = {
371                         .sign = 'u',
372                         .realbits = 12,
373                         .storagebits = 16,
374                 },
375         }, {
376                 .type = IIO_VOLTAGE,
377                 .indexed = 1,
378                 .channel = 1,
379                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
380                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
381                 .address = in_aux,
382                 .scan_index = ADIS16203_SCAN_AUX_ADC,
383                 .scan_type = {
384                         .sign = 'u',
385                         .realbits = 12,
386                         .storagebits = 16,
387                 },
388         }, {
389                 .type = IIO_INCLI,
390                 .modified = 1,
391                 .channel2 = IIO_MOD_X,
392                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
393                 IIO_CHAN_INFO_SCALE_SHARED_BIT |
394                 IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
395                 .address = incli_x,
396                 .scan_index = ADIS16203_SCAN_INCLI_X,
397                 .scan_type = {
398                         .sign = 's',
399                         .realbits = 14,
400                         .storagebits = 16,
401                 },
402         }, { /* Fixme: Not what it appears to be - see data sheet */
403                 .type = IIO_INCLI,
404                 .modified = 1,
405                 .channel2 = IIO_MOD_Y,
406                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
407                 IIO_CHAN_INFO_SCALE_SHARED_BIT,
408                 .address = incli_y,
409                 .scan_index = ADIS16203_SCAN_INCLI_Y,
410                 .scan_type = {
411                         .sign = 's',
412                         .realbits = 14,
413                         .storagebits = 16,
414                 },
415         }, {
416                 .type = IIO_TEMP,
417                 .indexed = 1,
418                 .channel = 0,
419                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
420                 IIO_CHAN_INFO_SCALE_SEPARATE_BIT |
421                 IIO_CHAN_INFO_OFFSET_SEPARATE_BIT,
422                 .address = temp,
423                 .scan_index = ADIS16203_SCAN_TEMP,
424                 .scan_type = {
425                         .sign = 'u',
426                         .realbits = 12,
427                         .storagebits = 16,
428                 },
429         },
430         IIO_CHAN_SOFT_TIMESTAMP(5),
431 };
432
433 static const struct iio_info adis16203_info = {
434         .read_raw = &adis16203_read_raw,
435         .write_raw = &adis16203_write_raw,
436         .driver_module = THIS_MODULE,
437 };
438
439 static int __devinit adis16203_probe(struct spi_device *spi)
440 {
441         int ret;
442         struct iio_dev *indio_dev;
443         struct adis16203_state *st;
444
445         /* setup the industrialio driver allocated elements */
446         indio_dev = iio_device_alloc(sizeof(*st));
447         if (indio_dev == NULL) {
448                 ret = -ENOMEM;
449                 goto error_ret;
450         }
451         st = iio_priv(indio_dev);
452         /* this is only used for removal purposes */
453         spi_set_drvdata(spi, indio_dev);
454         st->us = spi;
455         mutex_init(&st->buf_lock);
456
457         indio_dev->name = spi->dev.driver->name;
458         indio_dev->dev.parent = &spi->dev;
459         indio_dev->channels = adis16203_channels;
460         indio_dev->num_channels = ARRAY_SIZE(adis16203_channels);
461         indio_dev->info = &adis16203_info;
462         indio_dev->modes = INDIO_DIRECT_MODE;
463
464         ret = adis16203_configure_ring(indio_dev);
465         if (ret)
466                 goto error_free_dev;
467
468         ret = iio_buffer_register(indio_dev,
469                                   adis16203_channels,
470                                   ARRAY_SIZE(adis16203_channels));
471         if (ret) {
472                 printk(KERN_ERR "failed to initialize the ring\n");
473                 goto error_unreg_ring_funcs;
474         }
475
476         if (spi->irq) {
477                 ret = adis16203_probe_trigger(indio_dev);
478                 if (ret)
479                         goto error_uninitialize_ring;
480         }
481
482         /* Get the device into a sane initial state */
483         ret = adis16203_initial_setup(indio_dev);
484         if (ret)
485                 goto error_remove_trigger;
486
487         ret = iio_device_register(indio_dev);
488         if (ret)
489                 goto error_remove_trigger;
490
491         return 0;
492
493 error_remove_trigger:
494         adis16203_remove_trigger(indio_dev);
495 error_uninitialize_ring:
496         iio_buffer_unregister(indio_dev);
497 error_unreg_ring_funcs:
498         adis16203_unconfigure_ring(indio_dev);
499 error_free_dev:
500         iio_device_free(indio_dev);
501 error_ret:
502         return ret;
503 }
504
505 static int __devexit adis16203_remove(struct spi_device *spi)
506 {
507         struct iio_dev *indio_dev = spi_get_drvdata(spi);
508
509         iio_device_unregister(indio_dev);
510         adis16203_remove_trigger(indio_dev);
511         iio_buffer_unregister(indio_dev);
512         adis16203_unconfigure_ring(indio_dev);
513         iio_device_free(indio_dev);
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 module_spi_driver(adis16203_driver);
527
528 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
529 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
530 MODULE_LICENSE("GPL v2");
531 MODULE_ALIAS("spi:adis16203");