staging: fix olpc_dcon build errors
[firefly-linux-kernel-4.4.55.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/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 "accel.h"
24 #include "inclinometer.h"
25 #include "../gyro/gyro.h"
26 #include "../adc/adc.h"
27
28 #include "adis16203.h"
29
30 #define DRIVER_NAME             "adis16203"
31
32 static int adis16203_check_status(struct device *dev);
33
34 /**
35  * adis16203_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 adis16203_spi_write_reg_8(struct device *dev,
41                 u8 reg_address,
42                 u8 val)
43 {
44         int ret;
45         struct iio_dev *indio_dev = dev_get_drvdata(dev);
46         struct adis16203_state *st = iio_dev_get_devdata(indio_dev);
47
48         mutex_lock(&st->buf_lock);
49         st->tx[0] = ADIS16203_WRITE_REG(reg_address);
50         st->tx[1] = val;
51
52         ret = spi_write(st->us, st->tx, 2);
53         mutex_unlock(&st->buf_lock);
54
55         return ret;
56 }
57
58 /**
59  * adis16203_spi_write_reg_16() - write 2 bytes to a pair of registers
60  * @dev: device associated with child of actual device (iio_dev or iio_trig)
61  * @reg_address: the address of the lower of the two registers. Second register
62  *               is assumed to have address one greater.
63  * @val: value to be written
64  **/
65 static int adis16203_spi_write_reg_16(struct device *dev,
66                 u8 lower_reg_address,
67                 u16 value)
68 {
69         int ret;
70         struct spi_message msg;
71         struct iio_dev *indio_dev = dev_get_drvdata(dev);
72         struct adis16203_state *st = iio_dev_get_devdata(indio_dev);
73         struct spi_transfer xfers[] = {
74                 {
75                         .tx_buf = st->tx,
76                         .bits_per_word = 8,
77                         .len = 2,
78                         .cs_change = 1,
79                 }, {
80                         .tx_buf = st->tx + 2,
81                         .bits_per_word = 8,
82                         .len = 2,
83                         .cs_change = 1,
84                 },
85         };
86
87         mutex_lock(&st->buf_lock);
88         st->tx[0] = ADIS16203_WRITE_REG(lower_reg_address);
89         st->tx[1] = value & 0xFF;
90         st->tx[2] = ADIS16203_WRITE_REG(lower_reg_address + 1);
91         st->tx[3] = (value >> 8) & 0xFF;
92
93         spi_message_init(&msg);
94         spi_message_add_tail(&xfers[0], &msg);
95         spi_message_add_tail(&xfers[1], &msg);
96         ret = spi_sync(st->us, &msg);
97         mutex_unlock(&st->buf_lock);
98
99         return ret;
100 }
101
102 /**
103  * adis16203_spi_read_reg_16() - read 2 bytes from a 16-bit register
104  * @dev: device associated with child of actual device (iio_dev or iio_trig)
105  * @reg_address: the address of the lower of the two registers. Second register
106  *               is assumed to have address one greater.
107  * @val: somewhere to pass back the value read
108  **/
109 static int adis16203_spi_read_reg_16(struct device *dev,
110                 u8 lower_reg_address,
111                 u16 *val)
112 {
113         struct spi_message msg;
114         struct iio_dev *indio_dev = dev_get_drvdata(dev);
115         struct adis16203_state *st = iio_dev_get_devdata(indio_dev);
116         int ret;
117         struct spi_transfer xfers[] = {
118                 {
119                         .tx_buf = st->tx,
120                         .bits_per_word = 8,
121                         .len = 2,
122                         .cs_change = 1,
123                         .delay_usecs = 20,
124                 }, {
125                         .rx_buf = st->rx,
126                         .bits_per_word = 8,
127                         .len = 2,
128                         .cs_change = 1,
129                         .delay_usecs = 20,
130                 },
131         };
132
133         mutex_lock(&st->buf_lock);
134         st->tx[0] = ADIS16203_READ_REG(lower_reg_address);
135         st->tx[1] = 0;
136
137         spi_message_init(&msg);
138         spi_message_add_tail(&xfers[0], &msg);
139         spi_message_add_tail(&xfers[1], &msg);
140         ret = spi_sync(st->us, &msg);
141         if (ret) {
142                 dev_err(&st->us->dev, "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 adis16203_read_12bit_unsigned(struct device *dev,
154                 struct device_attribute *attr,
155                 char *buf)
156 {
157         int ret;
158         u16 val = 0;
159         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
160
161         ret = adis16203_spi_read_reg_16(dev, this_attr->address, &val);
162         if (ret)
163                 return ret;
164
165         if (val & ADIS16203_ERROR_ACTIVE)
166                 adis16203_check_status(dev);
167
168         return sprintf(buf, "%u\n", val & 0x0FFF);
169 }
170
171 static ssize_t adis16203_read_temp(struct device *dev,
172                 struct device_attribute *attr,
173                 char *buf)
174 {
175         struct iio_dev *indio_dev = dev_get_drvdata(dev);
176         ssize_t ret;
177         u16 val;
178
179         /* Take the iio_dev status lock */
180         mutex_lock(&indio_dev->mlock);
181
182         ret = adis16203_spi_read_reg_16(dev, ADIS16203_TEMP_OUT, (u16 *)&val);
183         if (ret)
184                 goto error_ret;
185
186         if (val & ADIS16203_ERROR_ACTIVE)
187                 adis16203_check_status(dev);
188
189         val &= 0xFFF;
190         ret = sprintf(buf, "%d\n", val);
191
192 error_ret:
193         mutex_unlock(&indio_dev->mlock);
194         return ret;
195 }
196
197 static ssize_t adis16203_read_14bit_signed(struct device *dev,
198                 struct device_attribute *attr,
199                 char *buf)
200 {
201         struct iio_dev *indio_dev = dev_get_drvdata(dev);
202         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
203         s16 val = 0;
204         ssize_t ret;
205
206         mutex_lock(&indio_dev->mlock);
207
208         ret = adis16203_spi_read_reg_16(dev, this_attr->address, (u16 *)&val);
209         if (!ret) {
210                 if (val & ADIS16203_ERROR_ACTIVE)
211                         adis16203_check_status(dev);
212
213                 val = ((s16)(val << 2) >> 2);
214                 ret = sprintf(buf, "%d\n", val);
215         }
216
217         mutex_unlock(&indio_dev->mlock);
218
219         return ret;
220 }
221
222 static ssize_t adis16203_write_16bit(struct device *dev,
223                 struct device_attribute *attr,
224                 const char *buf,
225                 size_t len)
226 {
227         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
228         int ret;
229         long val;
230
231         ret = strict_strtol(buf, 10, &val);
232         if (ret)
233                 goto error_ret;
234         ret = adis16203_spi_write_reg_16(dev, this_attr->address, val);
235
236 error_ret:
237         return ret ? ret : len;
238 }
239
240 static int adis16203_reset(struct device *dev)
241 {
242         int ret;
243         ret = adis16203_spi_write_reg_8(dev,
244                         ADIS16203_GLOB_CMD,
245                         ADIS16203_GLOB_CMD_SW_RESET);
246         if (ret)
247                 dev_err(dev, "problem resetting device");
248
249         return ret;
250 }
251
252 static ssize_t adis16203_write_reset(struct device *dev,
253                 struct device_attribute *attr,
254                 const char *buf, size_t len)
255 {
256         if (len < 1)
257                 return -EINVAL;
258         switch (buf[0]) {
259         case '1':
260         case 'y':
261         case 'Y':
262                 return adis16203_reset(dev);
263         }
264         return -EINVAL;
265 }
266
267 int adis16203_set_irq(struct device *dev, bool enable)
268 {
269         int ret = 0;
270         u16 msc;
271
272         ret = adis16203_spi_read_reg_16(dev, ADIS16203_MSC_CTRL, &msc);
273         if (ret)
274                 goto error_ret;
275
276         msc |= ADIS16203_MSC_CTRL_ACTIVE_HIGH;
277         msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_DIO1;
278         if (enable)
279                 msc |= ADIS16203_MSC_CTRL_DATA_RDY_EN;
280         else
281                 msc &= ~ADIS16203_MSC_CTRL_DATA_RDY_EN;
282
283         ret = adis16203_spi_write_reg_16(dev, ADIS16203_MSC_CTRL, msc);
284
285 error_ret:
286         return ret;
287 }
288
289 static int adis16203_check_status(struct device *dev)
290 {
291         u16 status;
292         int ret;
293
294         ret = adis16203_spi_read_reg_16(dev, ADIS16203_DIAG_STAT, &status);
295         if (ret < 0) {
296                 dev_err(dev, "Reading status failed\n");
297                 goto error_ret;
298         }
299         ret = status & 0x1F;
300
301         if (status & ADIS16203_DIAG_STAT_SELFTEST_FAIL)
302                 dev_err(dev, "Self test failure\n");
303         if (status & ADIS16203_DIAG_STAT_SPI_FAIL)
304                 dev_err(dev, "SPI failure\n");
305         if (status & ADIS16203_DIAG_STAT_FLASH_UPT)
306                 dev_err(dev, "Flash update failed\n");
307         if (status & ADIS16203_DIAG_STAT_POWER_HIGH)
308                 dev_err(dev, "Power supply above 3.625V\n");
309         if (status & ADIS16203_DIAG_STAT_POWER_LOW)
310                 dev_err(dev, "Power supply below 3.15V\n");
311
312 error_ret:
313         return ret;
314 }
315
316 static int adis16203_self_test(struct device *dev)
317 {
318         int ret;
319         ret = adis16203_spi_write_reg_16(dev,
320                         ADIS16203_MSC_CTRL,
321                         ADIS16203_MSC_CTRL_SELF_TEST_EN);
322         if (ret) {
323                 dev_err(dev, "problem starting self test");
324                 goto err_ret;
325         }
326
327         adis16203_check_status(dev);
328
329 err_ret:
330         return ret;
331 }
332
333 static int adis16203_initial_setup(struct adis16203_state *st)
334 {
335         int ret;
336         struct device *dev = &st->indio_dev->dev;
337
338         /* Disable IRQ */
339         ret = adis16203_set_irq(dev, false);
340         if (ret) {
341                 dev_err(dev, "disable irq failed");
342                 goto err_ret;
343         }
344
345         /* Do self test */
346         ret = adis16203_self_test(dev);
347         if (ret) {
348                 dev_err(dev, "self test failure");
349                 goto err_ret;
350         }
351
352         /* Read status register to check the result */
353         ret = adis16203_check_status(dev);
354         if (ret) {
355                 adis16203_reset(dev);
356                 dev_err(dev, "device not playing ball -> reset");
357                 msleep(ADIS16203_STARTUP_DELAY);
358                 ret = adis16203_check_status(dev);
359                 if (ret) {
360                         dev_err(dev, "giving up");
361                         goto err_ret;
362                 }
363         }
364
365         printk(KERN_INFO DRIVER_NAME ": at CS%d (irq %d)\n",
366                         st->us->chip_select, st->us->irq);
367
368 err_ret:
369         return ret;
370 }
371
372 static IIO_DEV_ATTR_IN_NAMED_RAW(0, supply, adis16203_read_12bit_unsigned,
373                 ADIS16203_SUPPLY_OUT);
374 static IIO_CONST_ATTR(in0_supply_scale, "0.00122");
375 static IIO_DEV_ATTR_IN_RAW(1, adis16203_read_12bit_unsigned,
376                 ADIS16203_AUX_ADC);
377 static IIO_CONST_ATTR(in1_scale, "0.00061");
378
379 static IIO_DEV_ATTR_INCLI_X(adis16203_read_14bit_signed,
380                 ADIS16203_XINCL_OUT);
381 static IIO_DEV_ATTR_INCLI_Y(adis16203_read_14bit_signed,
382                 ADIS16203_YINCL_OUT);
383 static IIO_DEV_ATTR_INCLI_X_OFFSET(S_IWUSR | S_IRUGO,
384                 adis16203_read_14bit_signed,
385                 adis16203_write_16bit,
386                 ADIS16203_INCL_NULL);
387 static IIO_CONST_ATTR(incli_scale, "0.025");
388
389 static IIO_DEV_ATTR_TEMP_RAW(adis16203_read_temp);
390 static IIO_CONST_ATTR(temp_offset, "25");
391 static IIO_CONST_ATTR(temp_scale, "-0.47");
392
393 static IIO_DEVICE_ATTR(reset, S_IWUSR, NULL, adis16203_write_reset, 0);
394
395 static IIO_CONST_ATTR(name, "adis16203");
396
397 static struct attribute *adis16203_event_attributes[] = {
398         NULL
399 };
400
401 static struct attribute_group adis16203_event_attribute_group = {
402         .attrs = adis16203_event_attributes,
403 };
404
405 static struct attribute *adis16203_attributes[] = {
406         &iio_dev_attr_in0_supply_raw.dev_attr.attr,
407         &iio_const_attr_in0_supply_scale.dev_attr.attr,
408         &iio_dev_attr_temp_raw.dev_attr.attr,
409         &iio_const_attr_temp_offset.dev_attr.attr,
410         &iio_const_attr_temp_scale.dev_attr.attr,
411         &iio_dev_attr_reset.dev_attr.attr,
412         &iio_const_attr_name.dev_attr.attr,
413         &iio_dev_attr_in1_raw.dev_attr.attr,
414         &iio_const_attr_in1_scale.dev_attr.attr,
415         &iio_dev_attr_incli_x_raw.dev_attr.attr,
416         &iio_dev_attr_incli_y_raw.dev_attr.attr,
417         &iio_dev_attr_incli_x_offset.dev_attr.attr,
418         &iio_const_attr_incli_scale.dev_attr.attr,
419         NULL
420 };
421
422 static const struct attribute_group adis16203_attribute_group = {
423         .attrs = adis16203_attributes,
424 };
425
426 static int __devinit adis16203_probe(struct spi_device *spi)
427 {
428         int ret, regdone = 0;
429         struct adis16203_state *st = kzalloc(sizeof *st, GFP_KERNEL);
430         if (!st) {
431                 ret =  -ENOMEM;
432                 goto error_ret;
433         }
434         /* this is only used for removal purposes */
435         spi_set_drvdata(spi, st);
436
437         /* Allocate the comms buffers */
438         st->rx = kzalloc(sizeof(*st->rx)*ADIS16203_MAX_RX, GFP_KERNEL);
439         if (st->rx == NULL) {
440                 ret = -ENOMEM;
441                 goto error_free_st;
442         }
443         st->tx = kzalloc(sizeof(*st->tx)*ADIS16203_MAX_TX, GFP_KERNEL);
444         if (st->tx == NULL) {
445                 ret = -ENOMEM;
446                 goto error_free_rx;
447         }
448         st->us = spi;
449         mutex_init(&st->buf_lock);
450         /* setup the industrialio driver allocated elements */
451         st->indio_dev = iio_allocate_device();
452         if (st->indio_dev == NULL) {
453                 ret = -ENOMEM;
454                 goto error_free_tx;
455         }
456
457         st->indio_dev->dev.parent = &spi->dev;
458         st->indio_dev->num_interrupt_lines = 1;
459         st->indio_dev->event_attrs = &adis16203_event_attribute_group;
460         st->indio_dev->attrs = &adis16203_attribute_group;
461         st->indio_dev->dev_data = (void *)(st);
462         st->indio_dev->driver_module = THIS_MODULE;
463         st->indio_dev->modes = INDIO_DIRECT_MODE;
464
465         ret = adis16203_configure_ring(st->indio_dev);
466         if (ret)
467                 goto error_free_dev;
468
469         ret = iio_device_register(st->indio_dev);
470         if (ret)
471                 goto error_unreg_ring_funcs;
472         regdone = 1;
473
474         ret = adis16203_initialize_ring(st->indio_dev->ring);
475         if (ret) {
476                 printk(KERN_ERR "failed to initialize the ring\n");
477                 goto error_unreg_ring_funcs;
478         }
479
480         if (spi->irq) {
481                 ret = iio_register_interrupt_line(spi->irq,
482                                 st->indio_dev,
483                                 0,
484                                 IRQF_TRIGGER_RISING,
485                                 "adis16203");
486                 if (ret)
487                         goto error_uninitialize_ring;
488
489                 ret = adis16203_probe_trigger(st->indio_dev);
490                 if (ret)
491                         goto error_unregister_line;
492         }
493
494         /* Get the device into a sane initial state */
495         ret = adis16203_initial_setup(st);
496         if (ret)
497                 goto error_remove_trigger;
498         return 0;
499
500 error_remove_trigger:
501         adis16203_remove_trigger(st->indio_dev);
502 error_unregister_line:
503         if (spi->irq)
504                 iio_unregister_interrupt_line(st->indio_dev, 0);
505 error_uninitialize_ring:
506         adis16203_uninitialize_ring(st->indio_dev->ring);
507 error_unreg_ring_funcs:
508         adis16203_unconfigure_ring(st->indio_dev);
509 error_free_dev:
510         if (regdone)
511                 iio_device_unregister(st->indio_dev);
512         else
513                 iio_free_device(st->indio_dev);
514 error_free_tx:
515         kfree(st->tx);
516 error_free_rx:
517         kfree(st->rx);
518 error_free_st:
519         kfree(st);
520 error_ret:
521         return ret;
522 }
523
524 static int adis16203_remove(struct spi_device *spi)
525 {
526         struct adis16203_state *st = spi_get_drvdata(spi);
527         struct iio_dev *indio_dev = st->indio_dev;
528
529         flush_scheduled_work();
530
531         adis16203_remove_trigger(indio_dev);
532         if (spi->irq)
533                 iio_unregister_interrupt_line(indio_dev, 0);
534
535         adis16203_uninitialize_ring(indio_dev->ring);
536         iio_device_unregister(indio_dev);
537         adis16203_unconfigure_ring(indio_dev);
538         kfree(st->tx);
539         kfree(st->rx);
540         kfree(st);
541
542         return 0;
543 }
544
545 static struct spi_driver adis16203_driver = {
546         .driver = {
547                 .name = "adis16203",
548                 .owner = THIS_MODULE,
549         },
550         .probe = adis16203_probe,
551         .remove = __devexit_p(adis16203_remove),
552 };
553
554 static __init int adis16203_init(void)
555 {
556         return spi_register_driver(&adis16203_driver);
557 }
558 module_init(adis16203_init);
559
560 static __exit void adis16203_exit(void)
561 {
562         spi_unregister_driver(&adis16203_driver);
563 }
564 module_exit(adis16203_exit);
565
566 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
567 MODULE_DESCRIPTION("Analog Devices ADIS16203 Programmable Digital Vibration Sensor driver");
568 MODULE_LICENSE("GPL v2");