staging:iio:ad799x: Do not return error code in interrupt handler
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / adc / ad799x_ring.c
1 /*
2  * Copyright (C) 2010-2012 Michael Hennerich, Analog Devices Inc.
3  * Copyright (C) 2008-2010 Jonathan Cameron
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * ad799x_ring.c
10  */
11
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
17 #include <linux/bitops.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23
24 #include "ad799x.h"
25
26 /**
27  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
28  *
29  * Currently there is no option in this driver to disable the saving of
30  * timestamps within the ring.
31  **/
32
33 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
34 {
35         struct iio_poll_func *pf = p;
36         struct iio_dev *indio_dev = pf->indio_dev;
37         struct ad799x_state *st = iio_priv(indio_dev);
38         struct iio_buffer *ring = indio_dev->buffer;
39         s64 time_ns;
40         __u8 *rxbuf;
41         int b_sent;
42         u8 cmd;
43
44         rxbuf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
45         if (rxbuf == NULL)
46                 goto out;
47
48         switch (st->id) {
49         case ad7991:
50         case ad7995:
51         case ad7999:
52                 cmd = st->config |
53                         (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
54                 break;
55         case ad7992:
56         case ad7993:
57         case ad7994:
58                 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
59                         AD7998_CONV_RES_REG;
60                 break;
61         case ad7997:
62         case ad7998:
63                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
64                 break;
65         default:
66                 cmd = 0;
67         }
68
69         b_sent = i2c_smbus_read_i2c_block_data(st->client,
70                         cmd, bitmap_weight(indio_dev->active_scan_mask,
71                                            indio_dev->masklength) * 2, rxbuf);
72         if (b_sent < 0)
73                 goto done;
74
75         time_ns = iio_get_time_ns();
76
77         if (indio_dev->scan_timestamp)
78                 memcpy(rxbuf + indio_dev->scan_bytes - sizeof(s64),
79                         &time_ns, sizeof(time_ns));
80
81         ring->access->store_to(indio_dev->buffer, rxbuf, time_ns);
82 done:
83         kfree(rxbuf);
84 out:
85         iio_trigger_notify_done(indio_dev->trig);
86
87         return IRQ_HANDLED;
88 }
89
90 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
91 {
92         return iio_triggered_buffer_setup(indio_dev, NULL,
93                 &ad799x_trigger_handler, NULL);
94 }
95
96 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
97 {
98         iio_triggered_buffer_cleanup(indio_dev);
99 }