Merge branches 'imx/pata' and 'imx/sata' into next/driver
[pandora-kernel.git] / drivers / staging / iio / accel / adis16240_core.c
1 /*
2  * ADIS16240 Programmable Impact Sensor and Recorder driver
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/gpio.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/spi/spi.h>
17 #include <linux/slab.h>
18 #include <linux/sysfs.h>
19 #include <linux/list.h>
20
21 #include "../iio.h"
22 #include "../sysfs.h"
23 #include "../ring_generic.h"
24 #include "accel.h"
25 #include "../adc/adc.h"
26
27 #include "adis16240.h"
28
29 #define DRIVER_NAME             "adis16240"
30
31 static int adis16240_check_status(struct iio_dev *indio_dev);
32
33 /**
34  * adis16240_spi_write_reg_8() - write single byte to a register
35  * @indio_dev: iio_dev associated with device
36  * @reg_address: the address of the register to be written
37  * @val: the value to write
38  **/
39 static int adis16240_spi_write_reg_8(struct iio_dev *indio_dev,
40                                      u8 reg_address,
41                                      u8 val)
42 {
43         int ret;
44         struct adis16240_state *st = iio_priv(indio_dev);
45
46         mutex_lock(&st->buf_lock);
47         st->tx[0] = ADIS16240_WRITE_REG(reg_address);
48         st->tx[1] = val;
49
50         ret = spi_write(st->us, st->tx, 2);
51         mutex_unlock(&st->buf_lock);
52
53         return ret;
54 }
55
56 /**
57  * adis16240_spi_write_reg_16() - write 2 bytes to a pair of registers
58  * @indio_dev: iio_dev for this device
59  * @reg_address: the address of the lower of the two registers. Second register
60  *               is assumed to have address one greater.
61  * @val: value to be written
62  **/
63 static int adis16240_spi_write_reg_16(struct iio_dev *indio_dev,
64                                       u8 lower_reg_address,
65                                       u16 value)
66 {
67         int ret;
68         struct spi_message msg;
69         struct adis16240_state *st = iio_priv(indio_dev);
70         struct spi_transfer xfers[] = {
71                 {
72                         .tx_buf = st->tx,
73                         .bits_per_word = 8,
74                         .len = 2,
75                         .cs_change = 1,
76                         .delay_usecs = 35,
77                 }, {
78                         .tx_buf = st->tx + 2,
79                         .bits_per_word = 8,
80                         .len = 2,
81                         .delay_usecs = 35,
82                 },
83         };
84
85         mutex_lock(&st->buf_lock);
86         st->tx[0] = ADIS16240_WRITE_REG(lower_reg_address);
87         st->tx[1] = value & 0xFF;
88         st->tx[2] = ADIS16240_WRITE_REG(lower_reg_address + 1);
89         st->tx[3] = (value >> 8) & 0xFF;
90
91         spi_message_init(&msg);
92         spi_message_add_tail(&xfers[0], &msg);
93         spi_message_add_tail(&xfers[1], &msg);
94         ret = spi_sync(st->us, &msg);
95         mutex_unlock(&st->buf_lock);
96
97         return ret;
98 }
99
100 /**
101  * adis16240_spi_read_reg_16() - read 2 bytes from a 16-bit register
102  * @indio_dev: iio_dev for this device
103  * @reg_address: the address of the lower of the two registers. Second register
104  *               is assumed to have address one greater.
105  * @val: somewhere to pass back the value read
106  **/
107 static int adis16240_spi_read_reg_16(struct iio_dev *indio_dev,
108                 u8 lower_reg_address,
109                 u16 *val)
110 {
111         struct spi_message msg;
112         struct adis16240_state *st = iio_priv(indio_dev);
113         int ret;
114         struct spi_transfer xfers[] = {
115                 {
116                         .tx_buf = st->tx,
117                         .bits_per_word = 8,
118                         .len = 2,
119                         .cs_change = 1,
120                         .delay_usecs = 35,
121                 }, {
122                         .rx_buf = st->rx,
123                         .bits_per_word = 8,
124                         .len = 2,
125                         .cs_change = 1,
126                         .delay_usecs = 35,
127                 },
128         };
129
130         mutex_lock(&st->buf_lock);
131         st->tx[0] = ADIS16240_READ_REG(lower_reg_address);
132         st->tx[1] = 0;
133         st->tx[2] = 0;
134         st->tx[3] = 0;
135
136         spi_message_init(&msg);
137         spi_message_add_tail(&xfers[0], &msg);
138         spi_message_add_tail(&xfers[1], &msg);
139         ret = spi_sync(st->us, &msg);
140         if (ret) {
141                 dev_err(&st->us->dev,
142                         "problem when reading 16 bit register 0x%02X",
143                         lower_reg_address);
144                 goto error_ret;
145         }
146         *val = (st->rx[0] << 8) | st->rx[1];
147
148 error_ret:
149         mutex_unlock(&st->buf_lock);
150         return ret;
151 }
152
153 static ssize_t adis16240_spi_read_signed(struct device *dev,
154                 struct device_attribute *attr,
155                 char *buf,
156                 unsigned bits)
157 {
158         struct iio_dev *indio_dev = dev_get_drvdata(dev);
159         int ret;
160         s16 val = 0;
161         unsigned shift = 16 - bits;
162         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
163
164         ret = adis16240_spi_read_reg_16(indio_dev,
165                                         this_attr->address, (u16 *)&val);
166         if (ret)
167                 return ret;
168
169         if (val & ADIS16240_ERROR_ACTIVE)
170                 adis16240_check_status(indio_dev);
171
172         val = ((s16)(val << shift) >> shift);
173         return sprintf(buf, "%d\n", val);
174 }
175
176 static ssize_t adis16240_read_12bit_signed(struct device *dev,
177                 struct device_attribute *attr,
178                 char *buf)
179 {
180         ssize_t ret;
181         struct iio_dev *indio_dev = dev_get_drvdata(dev);
182
183         /* Take the iio_dev status lock */
184         mutex_lock(&indio_dev->mlock);
185         ret =  adis16240_spi_read_signed(dev, attr, buf, 12);
186         mutex_unlock(&indio_dev->mlock);
187
188         return ret;
189 }
190
191 static int adis16240_reset(struct iio_dev *indio_dev)
192 {
193         int ret;
194         ret = adis16240_spi_write_reg_8(indio_dev,
195                         ADIS16240_GLOB_CMD,
196                         ADIS16240_GLOB_CMD_SW_RESET);
197         if (ret)
198                 dev_err(&indio_dev->dev, "problem resetting device");
199
200         return ret;
201 }
202
203 static ssize_t adis16240_write_reset(struct device *dev,
204                 struct device_attribute *attr,
205                 const char *buf, size_t len)
206 {
207         struct iio_dev *indio_dev = dev_get_drvdata(dev);
208
209         if (len < 1)
210                 return -EINVAL;
211         switch (buf[0]) {
212         case '1':
213         case 'y':
214         case 'Y':
215                 return adis16240_reset(indio_dev);
216         }
217         return -EINVAL;
218 }
219
220 int adis16240_set_irq(struct iio_dev *indio_dev, bool enable)
221 {
222         int ret = 0;
223         u16 msc;
224
225         ret = adis16240_spi_read_reg_16(indio_dev,
226                                         ADIS16240_MSC_CTRL, &msc);
227         if (ret)
228                 goto error_ret;
229
230         msc |= ADIS16240_MSC_CTRL_ACTIVE_HIGH;
231         msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_DIO2;
232         if (enable)
233                 msc |= ADIS16240_MSC_CTRL_DATA_RDY_EN;
234         else
235                 msc &= ~ADIS16240_MSC_CTRL_DATA_RDY_EN;
236
237         ret = adis16240_spi_write_reg_16(indio_dev,
238                                          ADIS16240_MSC_CTRL, msc);
239
240 error_ret:
241         return ret;
242 }
243
244 static int adis16240_self_test(struct iio_dev *indio_dev)
245 {
246         int ret;
247         ret = adis16240_spi_write_reg_16(indio_dev,
248                         ADIS16240_MSC_CTRL,
249                         ADIS16240_MSC_CTRL_SELF_TEST_EN);
250         if (ret) {
251                 dev_err(&indio_dev->dev, "problem starting self test");
252                 goto err_ret;
253         }
254
255         msleep(ADIS16240_STARTUP_DELAY);
256
257         adis16240_check_status(indio_dev);
258
259 err_ret:
260         return ret;
261 }
262
263 static int adis16240_check_status(struct iio_dev *indio_dev)
264 {
265         u16 status;
266         int ret;
267         struct device *dev = &indio_dev->dev;
268
269         ret = adis16240_spi_read_reg_16(indio_dev,
270                                         ADIS16240_DIAG_STAT, &status);
271
272         if (ret < 0) {
273                 dev_err(dev, "Reading status failed\n");
274                 goto error_ret;
275         }
276
277         ret = status & 0x2F;
278         if (status & ADIS16240_DIAG_STAT_PWRON_FAIL)
279                 dev_err(dev, "Power-on, self-test fail\n");
280         if (status & ADIS16240_DIAG_STAT_SPI_FAIL)
281                 dev_err(dev, "SPI failure\n");
282         if (status & ADIS16240_DIAG_STAT_FLASH_UPT)
283                 dev_err(dev, "Flash update failed\n");
284         if (status & ADIS16240_DIAG_STAT_POWER_HIGH)
285                 dev_err(dev, "Power supply above 3.625V\n");
286         if (status & ADIS16240_DIAG_STAT_POWER_LOW)
287                 dev_err(dev, "Power supply below 2.225V\n");
288
289 error_ret:
290         return ret;
291 }
292
293 static int adis16240_initial_setup(struct iio_dev *indio_dev)
294 {
295         int ret;
296         struct device *dev = &indio_dev->dev;
297
298         /* Disable IRQ */
299         ret = adis16240_set_irq(indio_dev, false);
300         if (ret) {
301                 dev_err(dev, "disable irq failed");
302                 goto err_ret;
303         }
304
305         /* Do self test */
306         ret = adis16240_self_test(indio_dev);
307         if (ret) {
308                 dev_err(dev, "self test failure");
309                 goto err_ret;
310         }
311
312         /* Read status register to check the result */
313         ret = adis16240_check_status(indio_dev);
314         if (ret) {
315                 adis16240_reset(indio_dev);
316                 dev_err(dev, "device not playing ball -> reset");
317                 msleep(ADIS16240_STARTUP_DELAY);
318                 ret = adis16240_check_status(indio_dev);
319                 if (ret) {
320                         dev_err(dev, "giving up");
321                         goto err_ret;
322                 }
323         }
324
325 err_ret:
326         return ret;
327 }
328
329 static IIO_DEVICE_ATTR(accel_xyz_squared_peak_raw, S_IRUGO,
330                        adis16240_read_12bit_signed, NULL,
331                        ADIS16240_XYZPEAK_OUT);
332
333 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16240_write_reset, 0);
334
335 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("4096");
336
337 enum adis16240_chan {
338         in_supply,
339         in_aux,
340         accel_x,
341         accel_y,
342         accel_z,
343         temp,
344 };
345
346 static const u8 adis16240_addresses[6][3] = {
347         [in_supply] = { ADIS16240_SUPPLY_OUT },
348         [in_aux] = { ADIS16240_AUX_ADC },
349         [accel_x] = { ADIS16240_XACCL_OUT, ADIS16240_XACCL_OFF,
350                       ADIS16240_XPEAK_OUT },
351         [accel_y] = { ADIS16240_YACCL_OUT, ADIS16240_YACCL_OFF,
352                       ADIS16240_YPEAK_OUT },
353         [accel_z] = { ADIS16240_ZACCL_OUT, ADIS16240_ZACCL_OFF,
354                       ADIS16240_ZPEAK_OUT },
355         [temp] = { ADIS16240_TEMP_OUT },
356 };
357
358 static int adis16240_read_raw(struct iio_dev *indio_dev,
359                               struct iio_chan_spec const *chan,
360                               int *val, int *val2,
361                               long mask)
362 {
363         int ret;
364         int bits;
365         u8 addr;
366         s16 val16;
367
368         switch (mask) {
369         case 0:
370                 mutex_lock(&indio_dev->mlock);
371                 addr = adis16240_addresses[chan->address][0];
372                 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
373                 if (ret) {
374                         mutex_unlock(&indio_dev->mlock);
375                         return ret;
376                 }
377
378                 if (val16 & ADIS16240_ERROR_ACTIVE) {
379                         ret = adis16240_check_status(indio_dev);
380                         if (ret) {
381                                 mutex_unlock(&indio_dev->mlock);
382                                 return ret;
383                         }
384                 }
385                 val16 = val16 & ((1 << chan->scan_type.realbits) - 1);
386                 if (chan->scan_type.sign == 's')
387                         val16 = (s16)(val16 <<
388                                       (16 - chan->scan_type.realbits)) >>
389                                 (16 - chan->scan_type.realbits);
390                 *val = val16;
391                 mutex_unlock(&indio_dev->mlock);
392                 return IIO_VAL_INT;
393         case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
394         case (1 << IIO_CHAN_INFO_SCALE_SHARED):
395                 switch (chan->type) {
396                 case IIO_IN:
397                         *val = 0;
398                         if (chan->channel == 0)
399                                 *val2 = 4880;
400                         else
401                                 return -EINVAL;
402                         return IIO_VAL_INT_PLUS_MICRO;
403                 case IIO_TEMP:
404                         *val = 0;
405                         *val2 = 244000;
406                         return IIO_VAL_INT_PLUS_MICRO;
407                 case IIO_ACCEL:
408                         *val = 0;
409                         *val2 = 504062;
410                         return IIO_VAL_INT_PLUS_MICRO;
411                 default:
412                         return -EINVAL;
413                 }
414                 break;
415         case (1 << IIO_CHAN_INFO_PEAK_SCALE_SHARED):
416                 *val = 6;
417                 *val2 = 629295;
418                 return IIO_VAL_INT_PLUS_MICRO;
419         case (1 << IIO_CHAN_INFO_OFFSET_SEPARATE):
420                 *val = 25;
421                 return IIO_VAL_INT;
422         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
423                 bits = 10;
424                 mutex_lock(&indio_dev->mlock);
425                 addr = adis16240_addresses[chan->address][1];
426                 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
427                 if (ret) {
428                         mutex_unlock(&indio_dev->mlock);
429                         return ret;
430                 }
431                 val16 &= (1 << bits) - 1;
432                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
433                 *val = val16;
434                 mutex_unlock(&indio_dev->mlock);
435                 return IIO_VAL_INT;
436         case (1 << IIO_CHAN_INFO_PEAK_SEPARATE):
437                 bits = 10;
438                 mutex_lock(&indio_dev->mlock);
439                 addr = adis16240_addresses[chan->address][2];
440                 ret = adis16240_spi_read_reg_16(indio_dev, addr, &val16);
441                 if (ret) {
442                         mutex_unlock(&indio_dev->mlock);
443                         return ret;
444                 }
445                 val16 &= (1 << bits) - 1;
446                 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits);
447                 *val = val16;
448                 mutex_unlock(&indio_dev->mlock);
449                 return IIO_VAL_INT;
450         }
451         return -EINVAL;
452 }
453
454 static int adis16240_write_raw(struct iio_dev *indio_dev,
455                                struct iio_chan_spec const *chan,
456                                int val,
457                                int val2,
458                                long mask)
459 {
460         int bits = 10;
461         s16 val16;
462         u8 addr;
463         switch (mask) {
464         case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
465                 val16 = val & ((1 << bits) - 1);
466                 addr = adis16240_addresses[chan->address][1];
467                 return adis16240_spi_write_reg_16(indio_dev, addr, val16);
468         }
469         return -EINVAL;
470 }
471
472 static struct iio_chan_spec adis16240_channels[] = {
473         IIO_CHAN(IIO_IN, 0, 1, 0, "supply", 0, 0,
474                  (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
475                  in_supply, ADIS16240_SCAN_SUPPLY,
476                  IIO_ST('u', 10, 16, 0), 0),
477         IIO_CHAN(IIO_IN, 0, 1, 0, NULL, 1, 0,
478                  0,
479                  in_aux, ADIS16240_SCAN_AUX_ADC,
480                  IIO_ST('u', 10, 16, 0), 0),
481         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_X,
482                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
483                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
484                  accel_x, ADIS16240_SCAN_ACC_X,
485                  IIO_ST('s', 10, 16, 0), 0),
486         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Y,
487                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
488                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
489                  accel_y, ADIS16240_SCAN_ACC_Y,
490                  IIO_ST('s', 10, 16, 0), 0),
491         IIO_CHAN(IIO_ACCEL, 1, 0, 0, NULL, 0, IIO_MOD_Z,
492                  (1 << IIO_CHAN_INFO_SCALE_SHARED) |
493                  (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE),
494                  accel_z, ADIS16240_SCAN_ACC_Z,
495                  IIO_ST('s', 10, 16, 0), 0),
496         IIO_CHAN(IIO_TEMP, 0, 1, 0, NULL, 0, 0,
497                  (1 << IIO_CHAN_INFO_SCALE_SEPARATE),
498                  temp, ADIS16240_SCAN_TEMP,
499                  IIO_ST('u', 10, 16, 0), 0),
500         IIO_CHAN_SOFT_TIMESTAMP(6)
501 };
502
503 static struct attribute *adis16240_attributes[] = {
504         &iio_dev_attr_accel_xyz_squared_peak_raw.dev_attr.attr,
505         &iio_const_attr_sampling_frequency_available.dev_attr.attr,
506         &iio_dev_attr_reset.dev_attr.attr,
507         NULL
508 };
509
510 static const struct attribute_group adis16240_attribute_group = {
511         .attrs = adis16240_attributes,
512 };
513
514 static const struct iio_info adis16240_info = {
515         .attrs = &adis16240_attribute_group,
516         .read_raw = &adis16240_read_raw,
517         .write_raw = &adis16240_write_raw,
518         .driver_module = THIS_MODULE,
519 };
520
521 static int __devinit adis16240_probe(struct spi_device *spi)
522 {
523         int ret, regdone = 0;
524         struct adis16240_state *st;
525         struct iio_dev *indio_dev;
526
527         /* setup the industrialio driver allocated elements */
528         indio_dev = iio_allocate_device(sizeof(*st));
529         if (indio_dev == NULL) {
530                 ret = -ENOMEM;
531                 goto error_ret;
532         }
533         st = iio_priv(indio_dev);
534         /* this is only used for removal purposes */
535         spi_set_drvdata(spi, indio_dev);
536
537         st->us = spi;
538         mutex_init(&st->buf_lock);
539
540         indio_dev->name = spi->dev.driver->name;
541         indio_dev->dev.parent = &spi->dev;
542         indio_dev->info = &adis16240_info;
543         indio_dev->channels = adis16240_channels;
544         indio_dev->num_channels = ARRAY_SIZE(adis16240_channels);
545         indio_dev->modes = INDIO_DIRECT_MODE;
546
547         ret = adis16240_configure_ring(indio_dev);
548         if (ret)
549                 goto error_free_dev;
550
551         ret = iio_device_register(indio_dev);
552         if (ret)
553                 goto error_unreg_ring_funcs;
554         regdone = 1;
555
556         ret = iio_ring_buffer_register_ex(indio_dev->ring, 0,
557                                           adis16240_channels,
558                                           ARRAY_SIZE(adis16240_channels));
559         if (ret) {
560                 printk(KERN_ERR "failed to initialize the ring\n");
561                 goto error_unreg_ring_funcs;
562         }
563
564         if (spi->irq) {
565                 ret = adis16240_probe_trigger(indio_dev);
566                 if (ret)
567                         goto error_uninitialize_ring;
568         }
569
570         /* Get the device into a sane initial state */
571         ret = adis16240_initial_setup(indio_dev);
572         if (ret)
573                 goto error_remove_trigger;
574         return 0;
575
576 error_remove_trigger:
577         adis16240_remove_trigger(indio_dev);
578 error_uninitialize_ring:
579         iio_ring_buffer_unregister(indio_dev->ring);
580 error_unreg_ring_funcs:
581         adis16240_unconfigure_ring(indio_dev);
582 error_free_dev:
583         if (regdone)
584                 iio_device_unregister(indio_dev);
585         else
586                 iio_free_device(indio_dev);
587 error_ret:
588         return ret;
589 }
590
591 static int adis16240_remove(struct spi_device *spi)
592 {
593
594         struct iio_dev *indio_dev = spi_get_drvdata(spi);
595
596         flush_scheduled_work();
597
598         adis16240_remove_trigger(indio_dev);
599         iio_ring_buffer_unregister(indio_dev->ring);
600         iio_device_unregister(indio_dev);
601         adis16240_unconfigure_ring(indio_dev);
602
603         return 0;
604 }
605
606 static struct spi_driver adis16240_driver = {
607         .driver = {
608                 .name = "adis16240",
609                 .owner = THIS_MODULE,
610         },
611         .probe = adis16240_probe,
612         .remove = __devexit_p(adis16240_remove),
613 };
614
615 static __init int adis16240_init(void)
616 {
617         return spi_register_driver(&adis16240_driver);
618 }
619 module_init(adis16240_init);
620
621 static __exit void adis16240_exit(void)
622 {
623         spi_unregister_driver(&adis16240_driver);
624 }
625 module_exit(adis16240_exit);
626
627 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
628 MODULE_DESCRIPTION("Analog Devices Programmable Impact Sensor and Recorder");
629 MODULE_LICENSE("GPL v2");