]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/adc/ad7476_ring.c
staging:iio: rationalization of different buffer implementation hooks.
[karo-tx-linux.git] / drivers / staging / iio / adc / ad7476_ring.c
1 /*
2  * Copyright 2010 Analog Devices Inc.
3  * Copyright (C) 2008 Jonathan Cameron
4  *
5  * Licensed under the GPL-2 or later.
6  *
7  * ad7476_ring.c
8  */
9
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/spi/spi.h>
16
17 #include "../iio.h"
18 #include "../ring_generic.h"
19 #include "../ring_sw.h"
20 #include "../trigger.h"
21 #include "../sysfs.h"
22
23 #include "ad7476.h"
24
25 int ad7476_scan_from_ring(struct ad7476_state *st)
26 {
27         struct iio_ring_buffer *ring = st->indio_dev->ring;
28         int ret;
29         u8 *ring_data;
30
31         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
32                             GFP_KERNEL);
33         if (ring_data == NULL) {
34                 ret = -ENOMEM;
35                 goto error_ret;
36         }
37         ret = ring->access->read_last(ring, ring_data);
38         if (ret)
39                 goto error_free_ring_data;
40
41         ret = (ring_data[0] << 8) | ring_data[1];
42
43 error_free_ring_data:
44         kfree(ring_data);
45 error_ret:
46         return ret;
47 }
48
49 /**
50  * ad7476_ring_preenable() setup the parameters of the ring before enabling
51  *
52  * The complex nature of the setting of the nuber of bytes per datum is due
53  * to this driver currently ensuring that the timestamp is stored at an 8
54  * byte boundary.
55  **/
56 static int ad7476_ring_preenable(struct iio_dev *indio_dev)
57 {
58         struct ad7476_state *st = indio_dev->dev_data;
59         struct iio_ring_buffer *ring = indio_dev->ring;
60
61         st->d_size = ring->scan_count *
62                 st->chip_info->channel[0].scan_type.storagebits / 8;
63
64         if (ring->scan_timestamp) {
65                 st->d_size += sizeof(s64);
66
67                 if (st->d_size % sizeof(s64))
68                         st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
69         }
70
71         if (indio_dev->ring->access->set_bytes_per_datum)
72                 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
73                                                             st->d_size);
74
75         return 0;
76 }
77
78 static irqreturn_t ad7476_trigger_handler(int irq, void  *p)
79 {
80         struct iio_poll_func *pf = p;
81         struct iio_dev *indio_dev = pf->private_data;
82         struct ad7476_state *st = iio_dev_get_devdata(indio_dev);
83         s64 time_ns;
84         __u8 *rxbuf;
85         int b_sent;
86
87         rxbuf = kzalloc(st->d_size, GFP_KERNEL);
88         if (rxbuf == NULL)
89                 return -ENOMEM;
90
91         b_sent = spi_read(st->spi, rxbuf,
92                           st->chip_info->channel[0].scan_type.storagebits / 8);
93         if (b_sent < 0)
94                 goto done;
95
96         time_ns = iio_get_time_ns();
97
98         if (indio_dev->ring->scan_timestamp)
99                 memcpy(rxbuf + st->d_size - sizeof(s64),
100                         &time_ns, sizeof(time_ns));
101
102         indio_dev->ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
103 done:
104         iio_trigger_notify_done(indio_dev->trig);
105         kfree(rxbuf);
106
107         return IRQ_HANDLED;
108 }
109
110 static const struct iio_ring_setup_ops ad7476_ring_setup_ops = {
111         .preenable = &ad7476_ring_preenable,
112         .postenable = &iio_triggered_ring_postenable,
113         .predisable = &iio_triggered_ring_predisable,
114 };
115
116 int ad7476_register_ring_funcs_and_init(struct iio_dev *indio_dev)
117 {
118         struct ad7476_state *st = indio_dev->dev_data;
119         int ret = 0;
120
121         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
122         if (!indio_dev->ring) {
123                 ret = -ENOMEM;
124                 goto error_ret;
125         }
126         /* Effectively select the ring buffer implementation */
127         indio_dev->ring->access = &ring_sw_access_funcs;
128         indio_dev->pollfunc = kzalloc(sizeof(indio_dev->pollfunc), GFP_KERNEL);
129         if (indio_dev->pollfunc == NULL) {
130                 ret = -ENOMEM;
131                 goto error_deallocate_sw_rb;
132         }
133         indio_dev->pollfunc->private_data = indio_dev;
134         indio_dev->pollfunc->thread = &ad7476_trigger_handler;
135         indio_dev->pollfunc->type = IRQF_ONESHOT;
136         indio_dev->pollfunc->name
137                 = kasprintf(GFP_KERNEL, "%s_consumer%d",
138                             spi_get_device_id(st->spi)->name,
139                             indio_dev->id);
140         if (indio_dev->pollfunc->name == NULL) {
141                 ret = -ENOMEM;
142                 goto error_free_pollfunc;
143         }
144
145         /* Ring buffer functions - here trigger setup related */
146         indio_dev->ring->setup_ops = &ad7476_ring_setup_ops;
147         indio_dev->ring->scan_timestamp = true;
148
149         /* Flag that polled ring buffering is possible */
150         indio_dev->modes |= INDIO_RING_TRIGGERED;
151         return 0;
152 error_free_pollfunc:
153         kfree(indio_dev->pollfunc);
154 error_deallocate_sw_rb:
155         iio_sw_rb_free(indio_dev->ring);
156 error_ret:
157         return ret;
158 }
159
160 void ad7476_ring_cleanup(struct iio_dev *indio_dev)
161 {
162         /* ensure that the trigger has been detached */
163         if (indio_dev->trig) {
164                 iio_put_trigger(indio_dev->trig);
165                 iio_trigger_dettach_poll_func(indio_dev->trig,
166                                               indio_dev->pollfunc);
167         }
168         kfree(indio_dev->pollfunc->name);
169         kfree(indio_dev->pollfunc);
170         iio_sw_rb_free(indio_dev->ring);
171 }