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