staging:iio:adc:ad7606 Convert to new channel registration method Update Add missing...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7606_ring.c
blob15531b62e2fdd1a7be8e0085ce1e760973c55638
1 /*
2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
6 */
8 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
16 #include "../iio.h"
17 #include "../ring_generic.h"
18 #include "../ring_sw.h"
19 #include "../trigger.h"
20 #include "../sysfs.h"
22 #include "ad7606.h"
24 int ad7606_scan_from_ring(struct ad7606_state *st, unsigned ch)
26 struct iio_ring_buffer *ring = st->indio_dev->ring;
27 int ret;
28 u16 *ring_data;
30 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
31 if (ring_data == NULL) {
32 ret = -ENOMEM;
33 goto error_ret;
35 ret = ring->access.read_last(ring, (u8 *) ring_data);
36 if (ret)
37 goto error_free_ring_data;
39 ret = ring_data[ch];
41 error_free_ring_data:
42 kfree(ring_data);
43 error_ret:
44 return ret;
47 /**
48 * ad7606_ring_preenable() setup the parameters of the ring before enabling
50 * The complex nature of the setting of the nuber of bytes per datum is due
51 * to this driver currently ensuring that the timestamp is stored at an 8
52 * byte boundary.
53 **/
54 static int ad7606_ring_preenable(struct iio_dev *indio_dev)
56 struct ad7606_state *st = indio_dev->dev_data;
57 struct iio_ring_buffer *ring = indio_dev->ring;
58 size_t d_size;
60 d_size = st->chip_info->num_channels *
61 st->chip_info->channels[0].scan_type.storagebits / 8;
63 if (ring->scan_timestamp) {
64 d_size += sizeof(s64);
66 if (d_size % sizeof(s64))
67 d_size += sizeof(s64) - (d_size % sizeof(s64));
70 if (ring->access.set_bytes_per_datum)
71 ring->access.set_bytes_per_datum(ring, d_size);
73 st->d_size = d_size;
75 return 0;
78 /**
79 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
81 **/
82 static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
84 struct iio_poll_func *pf = p;
85 struct iio_dev *indio_dev = pf->private_data;
86 struct ad7606_state *st = indio_dev->dev_data;
88 gpio_set_value(st->pdata->gpio_convst, 1);
90 return IRQ_HANDLED;
93 /**
94 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
95 * @work_s: the work struct through which this was scheduled
97 * Currently there is no option in this driver to disable the saving of
98 * timestamps within the ring.
99 * I think the one copy of this at a time was to avoid problems if the
100 * trigger was set far too high and the reads then locked up the computer.
102 static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
104 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
105 poll_work);
106 struct iio_dev *indio_dev = st->indio_dev;
107 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
108 struct iio_ring_buffer *ring = indio_dev->ring;
109 s64 time_ns;
110 __u8 *buf;
111 int ret;
113 buf = kzalloc(st->d_size, GFP_KERNEL);
114 if (buf == NULL)
115 return;
117 if (st->have_frstdata) {
118 ret = st->bops->read_block(st->dev, 1, buf);
119 if (ret)
120 goto done;
121 if (!gpio_get_value(st->pdata->gpio_frstdata)) {
122 /* This should never happen. However
123 * some signal glitch caused by bad PCB desgin or
124 * electrostatic discharge, could cause an extra read
125 * or clock. This allows recovery.
127 ad7606_reset(st);
128 goto done;
130 ret = st->bops->read_block(st->dev,
131 st->chip_info->num_channels - 1, buf + 2);
132 if (ret)
133 goto done;
134 } else {
135 ret = st->bops->read_block(st->dev,
136 st->chip_info->num_channels, buf);
137 if (ret)
138 goto done;
141 time_ns = iio_get_time_ns();
143 if (ring->scan_timestamp)
144 memcpy(buf + st->d_size - sizeof(s64),
145 &time_ns, sizeof(time_ns));
147 ring->access.store_to(&sw_ring->buf, buf, time_ns);
148 done:
149 gpio_set_value(st->pdata->gpio_convst, 0);
150 iio_trigger_notify_done(indio_dev->trig);
151 kfree(buf);
154 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
156 struct ad7606_state *st = indio_dev->dev_data;
157 int ret;
159 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
160 if (!indio_dev->ring) {
161 ret = -ENOMEM;
162 goto error_ret;
165 /* Effectively select the ring buffer implementation */
166 iio_ring_sw_register_funcs(&indio_dev->ring->access);
167 indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
168 if (indio_dev->pollfunc == NULL) {
169 ret = -ENOMEM;
170 goto error_deallocate_sw_rb;
173 indio_dev->pollfunc->private_data = indio_dev;
174 indio_dev->pollfunc->h = &ad7606_trigger_handler_th_bh;
175 indio_dev->pollfunc->thread = &ad7606_trigger_handler_th_bh;
176 indio_dev->pollfunc->name =
177 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
178 indio_dev->id);
179 if (indio_dev->pollfunc->name == NULL) {
180 ret = -ENOMEM;
181 goto error_free_poll_func;
183 /* Ring buffer functions - here trigger setup related */
185 indio_dev->ring->preenable = &ad7606_ring_preenable;
186 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
187 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
188 indio_dev->ring->scan_timestamp = true ;
190 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
192 /* Flag that polled ring buffering is possible */
193 indio_dev->modes |= INDIO_RING_TRIGGERED;
194 return 0;
195 error_free_poll_func:
196 kfree(indio_dev->pollfunc);
197 error_deallocate_sw_rb:
198 iio_sw_rb_free(indio_dev->ring);
199 error_ret:
200 return ret;
203 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
205 if (indio_dev->trig) {
206 iio_put_trigger(indio_dev->trig);
207 iio_trigger_dettach_poll_func(indio_dev->trig,
208 indio_dev->pollfunc);
210 kfree(indio_dev->pollfunc->name);
211 kfree(indio_dev->pollfunc);
212 iio_sw_rb_free(indio_dev->ring);