Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal
[sfrench/cifs-2.6.git] / 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 #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 "adis16201.h"
23
24 enum adis16201_chan {
25         in_supply,
26         temp,
27         accel_x,
28         accel_y,
29         incli_x,
30         incli_y,
31         in_aux,
32 };
33
34 /**
35  * adis16201_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 adis16201_spi_write_reg_8(struct iio_dev *indio_dev,
41                 u8 reg_address,
42                 u8 val)
43 {
44         int ret;
45         struct adis16201_state *st = iio_priv(indio_dev);
46
47         mutex_lock(&st->buf_lock);
48         st->tx[0] = ADIS16201_WRITE_REG(reg_address);
49         st->tx[1] = val;
50
51         ret = spi_write(st->us, st->tx, 2);
52         mutex_unlock(&st->buf_lock);
53
54         return ret;
55 }
56
57 /**
58  * adis16201_spi_write_reg_16() - write 2 bytes to a pair of registers
59  * @indio_dev: iio device associated with child of actual device
60  * @reg_address: the address of the lower of the two registers. Second register
61  *               is assumed to have address one greater.
62  * @val: value to be written
63  **/
64 static int adis16201_spi_write_reg_16(struct iio_dev *indio_dev,
65                                       u8 lower_reg_address,
66                                       u16 value)
67 {
68         int ret;
69         struct spi_message msg;
70         struct adis16201_state *st = iio_priv(indio_dev);
71         struct spi_transfer xfers[] = {
72                 {
73                         .tx_buf = st->tx,
74                         .bits_per_word = 8,
75                         .len = 2,
76                         .cs_change = 1,
77                 }, {
78                         .tx_buf = st->tx + 2,
79                         .bits_per_word = 8,
80                         .len = 2,
81                 },
82         };
83
84         mutex_lock(&st->buf_lock);
85         st->tx[0] = ADIS16201_WRITE_REG(lower_reg_address);
86         st->tx[1] = value & 0xFF;
87         st->tx[2] = ADIS16201_WRITE_REG(lower_reg_address + 1);
88         st->tx[3] = (value >> 8) & 0xFF;
89
90         spi_message_init(&msg);
91         spi_message_add_tail(&xfers[0], &msg);
92         spi_message_add_tail(&xfers[1], &msg);
93         ret = spi_sync(st->us, &msg);
94         mutex_unlock(&st->buf_lock);
95
96         return ret;
97 }
98
99 /**
100  * adis16201_spi_read_reg_16() - read 2 bytes from a 16-bit register
101  * @indio_dev: iio device associated with child of actual device
102  * @reg_address: the address of the lower of the two registers. Second register
103  *               is assumed to have address one greater.
104  * @val: somewhere to pass back the value read
105  **/
106 static int adis16201_spi_read_reg_16(struct iio_dev *indio_dev,
107                 u8 lower_reg_address,
108                 u16 *val)
109 {
110         struct spi_message msg;
111         struct adis16201_state *st = iio_priv(indio_dev);
112         int ret;
113         struct spi_transfer xfers[] = {
114                 {
115                         .tx_buf = st->tx,
116                         .bits_per_word = 8,
117                         .len = 2,
118                         .cs_change = 1,
119                         .delay_usecs = 20,
120                 }, {
121                         .rx_buf = st->rx,
122                         .bits_per_word = 8,
123                         .len = 2,
124                         .delay_usecs = 20,
125                 },
126         };
127
128         mutex_lock(&st->buf_lock);
129         st->tx[0] = ADIS16201_READ_REG(lower_reg_address);
130         st->tx[1] = 0;
131
132         spi_message_init(&msg);
133         spi_message_add_tail(&xfers[0], &msg);
134         spi_message_add_tail(&xfers[1], &msg);
135         ret = spi_sync(st->us, &msg);
136         if (ret) {
137                 dev_err(&st->us->dev, "problem when reading 16 bit register 0x%02X",
138                                 lower_reg_address);
139                 goto error_ret;
140         }
141         *val = (st->rx[0] << 8) | st->rx[1];
142
143 error_ret:
144         mutex_unlock(&st->buf_lock);
145         return ret;
146 }
147
148 static int adis16201_reset(struct iio_dev *indio_dev)
149 {
150         int ret;
151         struct adis16201_state *st = iio_priv(indio_dev);
152
153         ret = adis16201_spi_write_reg_8(indio_dev,
154                         ADIS16201_GLOB_CMD,
155                         ADIS16201_GLOB_CMD_SW_RESET);
156         if (ret)
157                 dev_err(&st->us->dev, "problem resetting device");
158
159         return ret;
160 }
161
162 int adis16201_set_irq(struct iio_dev *indio_dev, bool enable)
163 {
164         int ret = 0;
165         u16 msc;
166
167         ret = adis16201_spi_read_reg_16(indio_dev, ADIS16201_MSC_CTRL, &msc);
168         if (ret)
169                 goto error_ret;
170
171         msc |= ADIS16201_MSC_CTRL_ACTIVE_HIGH;
172         msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_DIO1;
173         if (enable)
174                 msc |= ADIS16201_MSC_CTRL_DATA_RDY_EN;
175         else
176                 msc &= ~ADIS16201_MSC_CTRL_DATA_RDY_EN;
177
178         ret = adis16201_spi_write_reg_16(indio_dev, ADIS16201_MSC_CTRL, msc);
179
180 error_ret:
181         return ret;
182 }
183
184 static int adis16201_check_status(struct iio_dev *indio_dev)
185 {
186         u16 status;
187         int ret;
188
189         ret = adis16201_spi_read_reg_16(indio_dev,
190                                         ADIS16201_DIAG_STAT, &status);
191         if (ret < 0) {
192                 dev_err(&indio_dev->dev, "Reading status failed\n");
193                 goto error_ret;
194         }
195         ret = status & 0xF;
196         if (ret)
197                 ret = -EFAULT;
198
199         if (status & ADIS16201_DIAG_STAT_SPI_FAIL)
200                 dev_err(&indio_dev->dev, "SPI failure\n");
201         if (status & ADIS16201_DIAG_STAT_FLASH_UPT)
202                 dev_err(&indio_dev->dev, "Flash update failed\n");
203         if (status & ADIS16201_DIAG_STAT_POWER_HIGH)
204                 dev_err(&indio_dev->dev, "Power supply above 3.625V\n");
205         if (status & ADIS16201_DIAG_STAT_POWER_LOW)
206                 dev_err(&indio_dev->dev, "Power supply below 3.15V\n");
207
208 error_ret:
209         return ret;
210 }
211
212 static int adis16201_self_test(struct iio_dev *indio_dev)
213 {
214         int ret;
215         ret = adis16201_spi_write_reg_16(indio_dev,
216                         ADIS16201_MSC_CTRL,
217                         ADIS16201_MSC_CTRL_SELF_TEST_EN);
218         if (ret) {
219                 dev_err(&indio_dev->dev, "problem starting self test");
220                 goto err_ret;
221         }
222
223         ret = adis16201_check_status(indio_dev);
224
225 err_ret:
226         return ret;
227 }
228
229 static int adis16201_initial_setup(struct iio_dev *indio_dev)
230 {
231         int ret;
232         struct device *dev = &indio_dev->dev;
233
234         /* Disable IRQ */
235         ret = adis16201_set_irq(indio_dev, false);
236         if (ret) {
237                 dev_err(dev, "disable irq failed");
238                 goto err_ret;
239         }
240
241         /* Do self test */
242         ret = adis16201_self_test(indio_dev);
243         if (ret) {
244                 dev_err(dev, "self test failure");
245                 goto err_ret;
246         }
247
248         /* Read status register to check the result */
249         ret = adis16201_check_status(indio_dev);
250         if (ret) {
251                 adis16201_reset(indio_dev);
252                 dev_err(dev, "device not playing ball -> reset");
253                 msleep(ADIS16201_STARTUP_DELAY);
254                 ret = adis16201_check_status(indio_dev);
255                 if (ret) {
256                         dev_err(dev, "giving up");
257                         goto err_ret;
258                 }
259         }
260
261 err_ret:
262         return ret;
263 }
264
265 static u8 adis16201_addresses[7][2] = {
266         [in_supply] = { ADIS16201_SUPPLY_OUT, },
267         [temp] = { ADIS16201_TEMP_OUT },
268         [accel_x] = { ADIS16201_XACCL_OUT, ADIS16201_XACCL_OFFS },
269         [accel_y] = { ADIS16201_YACCL_OUT, ADIS16201_YACCL_OFFS },
270         [in_aux] = { ADIS16201_AUX_ADC },
271         [incli_x] = { ADIS16201_XINCL_OUT },
272         [incli_y] = { ADIS16201_YINCL_OUT },
273 };
274
275 static int adis16201_read_raw(struct iio_dev *indio_dev,
276                               struct iio_chan_spec const *chan,
277                               int *val, int *val2,
278                               long mask)
279 {
280         int ret;
281         int bits;
282         u8 addr;
283         s16 val16;
284
285         switch (mask) {
286         case IIO_CHAN_INFO_RAW:
287                 mutex_lock(&indio_dev->mlock);
288                 addr = adis16201_addresses[chan->address][0];
289                 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
290                 if (ret) {
291                         mutex_unlock(&indio_dev->mlock);
292                         return ret;
293                 }
294
295                 if (val16 & ADIS16201_ERROR_ACTIVE) {
296                         ret = adis16201_check_status(indio_dev);
297                         if (ret) {
298                                 mutex_unlock(&indio_dev->mlock);
299                                 return ret;
300                         }
301                 }
302                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
303                 if (chan->scan_type.sign == 's')
304                         val16 = (s16)(val16 <<
305                                       (16 - chan->scan_type.realbits)) >>
306                                 (16 - chan->scan_type.realbits);
307                 *val = val16;
308                 mutex_unlock(&indio_dev->mlock);
309                 return IIO_VAL_INT;
310         case IIO_CHAN_INFO_SCALE:
311                 switch (chan->type) {
312                 case IIO_VOLTAGE:
313                         *val = 0;
314                         if (chan->channel == 0)
315                                 *val2 = 1220;
316                         else
317                                 *val2 = 610;
318                         return IIO_VAL_INT_PLUS_MICRO;
319                 case IIO_TEMP:
320                         *val = 0;
321                         *val2 = -470000;
322                         return IIO_VAL_INT_PLUS_MICRO;
323                 case IIO_ACCEL:
324                         *val = 0;
325                         *val2 = 462500;
326                         return IIO_VAL_INT_PLUS_MICRO;
327                 case IIO_INCLI:
328                         *val = 0;
329                         *val2 = 100000;
330                         return IIO_VAL_INT_PLUS_MICRO;
331                 default:
332                         return -EINVAL;
333                 }
334                 break;
335         case IIO_CHAN_INFO_OFFSET:
336                 *val = 25;
337                 return IIO_VAL_INT;
338         case IIO_CHAN_INFO_CALIBBIAS:
339                 switch (chan->type) {
340                 case IIO_ACCEL:
341                         bits = 12;
342                         break;
343                 case IIO_INCLI:
344                         bits = 9;
345                         break;
346                 default:
347                         return -EINVAL;
348                 };
349                 mutex_lock(&indio_dev->mlock);
350                 addr = adis16201_addresses[chan->address][1];
351                 ret = adis16201_spi_read_reg_16(indio_dev, addr, &val16);
352                 if (ret) {
353                         mutex_unlock(&indio_dev->mlock);
354                         return ret;
355                 }
356                 val16 &= (1 << bits) - 1;
357                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
358                 *val = val16;
359                 mutex_unlock(&indio_dev->mlock);
360                 return IIO_VAL_INT;
361         }
362         return -EINVAL;
363 }
364
365 static int adis16201_write_raw(struct iio_dev *indio_dev,
366                                struct iio_chan_spec const *chan,
367                                int val,
368                                int val2,
369                                long mask)
370 {
371         int bits;
372         s16 val16;
373         u8 addr;
374         switch (mask) {
375         case IIO_CHAN_INFO_CALIBBIAS:
376                 switch (chan->type) {
377                 case IIO_ACCEL:
378                         bits = 12;
379                         break;
380                 case IIO_INCLI:
381                         bits = 9;
382                         break;
383                 default:
384                         return -EINVAL;
385                 };
386                 val16 = val & ((1 << bits) - 1);
387                 addr = adis16201_addresses[chan->address][1];
388                 return adis16201_spi_write_reg_16(indio_dev, addr, val16);
389         }
390         return -EINVAL;
391 }
392
393 static const struct iio_chan_spec adis16201_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 = ADIS16201_SCAN_SUPPLY,
403                 .scan_type = {
404                         .sign = 'u',
405                         .realbits = 12,
406                         .storagebits = 16,
407                 },
408         }, {
409                 .type = IIO_TEMP,
410                 .indexed = 1,
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 = ADIS16201_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 = ADIS16201_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 = ADIS16201_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 = ADIS16201_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                  IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
470                 .address = incli_x,
471                 .scan_index = ADIS16201_SCAN_INCLI_X,
472                 .scan_type = {
473                         .sign = 's',
474                         .realbits = 14,
475                         .storagebits = 16,
476                 },
477         }, {
478                 .type = IIO_INCLI,
479                 .modified = 1,
480                 .channel2 = IIO_MOD_Y,
481                 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
482                  IIO_CHAN_INFO_SCALE_SHARED_BIT |
483                  IIO_CHAN_INFO_CALIBBIAS_SEPARATE_BIT,
484                 .address = incli_y,
485                 .scan_index = ADIS16201_SCAN_INCLI_Y,
486                 .scan_type = {
487                         .sign = 's',
488                         .realbits = 14,
489                         .storagebits = 16,
490                 },
491         },
492         IIO_CHAN_SOFT_TIMESTAMP(7)
493 };
494
495 static const struct iio_info adis16201_info = {
496         .read_raw = &adis16201_read_raw,
497         .write_raw = &adis16201_write_raw,
498         .driver_module = THIS_MODULE,
499 };
500
501 static int __devinit adis16201_probe(struct spi_device *spi)
502 {
503         int ret;
504         struct adis16201_state *st;
505         struct iio_dev *indio_dev;
506
507         /* setup the industrialio driver allocated elements */
508         indio_dev = iio_device_alloc(sizeof(*st));
509         if (indio_dev == NULL) {
510                 ret = -ENOMEM;
511                 goto error_ret;
512         }
513         st = iio_priv(indio_dev);
514         /* this is only used for removal purposes */
515         spi_set_drvdata(spi, indio_dev);
516
517         st->us = spi;
518         mutex_init(&st->buf_lock);
519
520         indio_dev->name = spi->dev.driver->name;
521         indio_dev->dev.parent = &spi->dev;
522         indio_dev->info = &adis16201_info;
523
524         indio_dev->channels = adis16201_channels;
525         indio_dev->num_channels = ARRAY_SIZE(adis16201_channels);
526         indio_dev->modes = INDIO_DIRECT_MODE;
527
528         ret = adis16201_configure_ring(indio_dev);
529         if (ret)
530                 goto error_free_dev;
531
532         ret = iio_buffer_register(indio_dev,
533                                   adis16201_channels,
534                                   ARRAY_SIZE(adis16201_channels));
535         if (ret) {
536                 printk(KERN_ERR "failed to initialize the ring\n");
537                 goto error_unreg_ring_funcs;
538         }
539
540         if (spi->irq) {
541                 ret = adis16201_probe_trigger(indio_dev);
542                 if (ret)
543                         goto error_uninitialize_ring;
544         }
545
546         /* Get the device into a sane initial state */
547         ret = adis16201_initial_setup(indio_dev);
548         if (ret)
549                 goto error_remove_trigger;
550
551         ret = iio_device_register(indio_dev);
552         if (ret < 0)
553                 goto error_remove_trigger;
554         return 0;
555
556 error_remove_trigger:
557         adis16201_remove_trigger(indio_dev);
558 error_uninitialize_ring:
559         iio_buffer_unregister(indio_dev);
560 error_unreg_ring_funcs:
561         adis16201_unconfigure_ring(indio_dev);
562 error_free_dev:
563         iio_device_free(indio_dev);
564 error_ret:
565         return ret;
566 }
567
568 static int __devexit adis16201_remove(struct spi_device *spi)
569 {
570         struct iio_dev *indio_dev = spi_get_drvdata(spi);
571
572         iio_device_unregister(indio_dev);
573         adis16201_remove_trigger(indio_dev);
574         iio_buffer_unregister(indio_dev);
575         adis16201_unconfigure_ring(indio_dev);
576         iio_device_free(indio_dev);
577
578         return 0;
579 }
580
581 static struct spi_driver adis16201_driver = {
582         .driver = {
583                 .name = "adis16201",
584                 .owner = THIS_MODULE,
585         },
586         .probe = adis16201_probe,
587         .remove = __devexit_p(adis16201_remove),
588 };
589 module_spi_driver(adis16201_driver);
590
591 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
592 MODULE_DESCRIPTION("Analog Devices ADIS16201 Programmable Digital Vibration Sensor driver");
593 MODULE_LICENSE("GPL v2");
594 MODULE_ALIAS("spi:adis16201");