staging:iio:adc:AD7298: Use private data space from iio_allocate_device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / adc / ad7298_ring.c
blobd0a0aeaaf398d578e8d9ee3a986ee8a9ea9067a0
1 /*
2 * AD7298 SPI ADC driver
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.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 "ad7298.h"
24 int ad7298_scan_from_ring(struct iio_dev *dev_info, long ch)
26 struct iio_ring_buffer *ring = dev_info->ring;
27 int ret;
28 u16 *ring_data;
30 if (!(ring->scan_mask & (1 << ch))) {
31 ret = -EBUSY;
32 goto error_ret;
35 ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
36 GFP_KERNEL);
37 if (ring_data == NULL) {
38 ret = -ENOMEM;
39 goto error_ret;
41 ret = ring->access->read_last(ring, (u8 *) ring_data);
42 if (ret)
43 goto error_free_ring_data;
45 ret = be16_to_cpu(ring_data[ch]);
47 error_free_ring_data:
48 kfree(ring_data);
49 error_ret:
50 return ret;
53 /**
54 * ad7298_ring_preenable() setup the parameters of the ring before enabling
56 * The complex nature of the setting of the number of bytes per datum is due
57 * to this driver currently ensuring that the timestamp is stored at an 8
58 * byte boundary.
59 **/
60 static int ad7298_ring_preenable(struct iio_dev *indio_dev)
62 struct ad7298_state *st = iio_priv(indio_dev);
63 struct iio_ring_buffer *ring = indio_dev->ring;
64 size_t d_size;
65 int i, m;
66 unsigned short command;
68 d_size = ring->scan_count * (AD7298_STORAGE_BITS / 8);
70 if (ring->scan_timestamp) {
71 d_size += sizeof(s64);
73 if (d_size % sizeof(s64))
74 d_size += sizeof(s64) - (d_size % sizeof(s64));
77 if (ring->access->set_bytes_per_datum)
78 ring->access->set_bytes_per_datum(ring, d_size);
80 st->d_size = d_size;
82 command = AD7298_WRITE | st->ext_ref;
84 for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
85 if (ring->scan_mask & (1 << i))
86 command |= m;
88 st->tx_buf[0] = cpu_to_be16(command);
90 /* build spi ring message */
91 st->ring_xfer[0].tx_buf = &st->tx_buf[0];
92 st->ring_xfer[0].len = 2;
93 st->ring_xfer[0].cs_change = 1;
94 st->ring_xfer[1].tx_buf = &st->tx_buf[1];
95 st->ring_xfer[1].len = 2;
96 st->ring_xfer[1].cs_change = 1;
98 spi_message_init(&st->ring_msg);
99 spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
100 spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);
102 for (i = 0; i < ring->scan_count; i++) {
103 st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
104 st->ring_xfer[i + 2].len = 2;
105 st->ring_xfer[i + 2].cs_change = 1;
106 spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
108 /* make sure last transfer cs_change is not set */
109 st->ring_xfer[i + 1].cs_change = 0;
111 return 0;
115 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
117 * Currently there is no option in this driver to disable the saving of
118 * timestamps within the ring.
120 static irqreturn_t ad7298_trigger_handler(int irq, void *p)
122 struct iio_poll_func *pf = p;
123 struct iio_dev *indio_dev = pf->private_data;
124 struct ad7298_state *st = iio_priv(indio_dev);
125 struct iio_ring_buffer *ring = indio_dev->ring;
126 s64 time_ns;
127 __u16 buf[16];
128 int b_sent, i;
130 b_sent = spi_sync(st->spi, &st->ring_msg);
131 if (b_sent)
132 return b_sent;
134 if (ring->scan_timestamp) {
135 time_ns = iio_get_time_ns();
136 memcpy((u8 *)buf + st->d_size - sizeof(s64),
137 &time_ns, sizeof(time_ns));
140 for (i = 0; i < ring->scan_count; i++)
141 buf[i] = be16_to_cpu(st->rx_buf[i]);
143 indio_dev->ring->access->store_to(ring, (u8 *)buf, time_ns);
144 iio_trigger_notify_done(indio_dev->trig);
146 return IRQ_HANDLED;
149 static const struct iio_ring_setup_ops ad7298_ring_setup_ops = {
150 .preenable = &ad7298_ring_preenable,
151 .postenable = &iio_triggered_ring_postenable,
152 .predisable = &iio_triggered_ring_predisable,
155 int ad7298_register_ring_funcs_and_init(struct iio_dev *indio_dev)
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;
164 /* Effectively select the ring buffer implementation */
165 indio_dev->ring->access = &ring_sw_access_funcs;
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;
172 indio_dev->pollfunc->private_data = indio_dev;
173 indio_dev->pollfunc->thread = &ad7298_trigger_handler;
174 indio_dev->pollfunc->type = IRQF_ONESHOT;
175 indio_dev->pollfunc->name =
176 kasprintf(GFP_KERNEL, "ad7298_consumer%d", indio_dev->id);
177 if (indio_dev->pollfunc->name == NULL) {
178 ret = -ENOMEM;
179 goto error_free_poll_func;
181 /* Ring buffer functions - here trigger setup related */
182 indio_dev->ring->setup_ops = &ad7298_ring_setup_ops;
183 indio_dev->ring->scan_timestamp = true;
185 /* Flag that polled ring buffering is possible */
186 indio_dev->modes |= INDIO_RING_TRIGGERED;
187 return 0;
188 error_free_poll_func:
189 kfree(indio_dev->pollfunc);
190 error_deallocate_sw_rb:
191 iio_sw_rb_free(indio_dev->ring);
192 error_ret:
193 return ret;
196 void ad7298_ring_cleanup(struct iio_dev *indio_dev)
198 if (indio_dev->trig) {
199 iio_put_trigger(indio_dev->trig);
200 iio_trigger_dettach_poll_func(indio_dev->trig,
201 indio_dev->pollfunc);
203 kfree(indio_dev->pollfunc->name);
204 kfree(indio_dev->pollfunc);
205 iio_sw_rb_free(indio_dev->ring);