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