36e118cf32e2b95728fe3a0773f6272a92418a4a
[pandora-kernel.git] / drivers / staging / iio / adc / ad7887_ring.c
1 /*
2  * Copyright 2010-2011 Analog Devices Inc.
3  * Copyright (C) 2008 Jonathan Cameron
4  *
5  * Licensed under the GPL-2.
6  *
7  * ad7887_ring.c
8  */
9
10 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14
15 #include "../iio.h"
16 #include "../ring_generic.h"
17 #include "../ring_sw.h"
18 #include "../trigger_consumer.h"
19
20 #include "ad7887.h"
21
22 int ad7887_scan_from_ring(struct ad7887_state *st, int channum)
23 {
24         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
25         int count = 0, ret;
26         u16 *ring_data;
27
28         if (!(test_bit(channum, ring->scan_mask))) {
29                 ret = -EBUSY;
30                 goto error_ret;
31         }
32
33         ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
34                             GFP_KERNEL);
35         if (ring_data == NULL) {
36                 ret = -ENOMEM;
37                 goto error_ret;
38         }
39         ret = ring->access->read_last(ring, (u8 *) ring_data);
40         if (ret)
41                 goto error_free_ring_data;
42
43         /* for single channel scan the result is stored with zero offset */
44         if ((test_bit(1, ring->scan_mask) || test_bit(0, ring->scan_mask)) &&
45             (channum == 1))
46                 count = 1;
47
48         ret = be16_to_cpu(ring_data[count]);
49
50 error_free_ring_data:
51         kfree(ring_data);
52 error_ret:
53         return ret;
54 }
55
56 /**
57  * ad7887_ring_preenable() setup the parameters of the ring before enabling
58  *
59  * The complex nature of the setting of the nuber of bytes per datum is due
60  * to this driver currently ensuring that the timestamp is stored at an 8
61  * byte boundary.
62  **/
63 static int ad7887_ring_preenable(struct iio_dev *indio_dev)
64 {
65         struct ad7887_state *st = iio_priv(indio_dev);
66         struct iio_ring_buffer *ring = indio_dev->ring;
67
68         st->d_size = ring->scan_count *
69                 st->chip_info->channel[0].scan_type.storagebits / 8;
70
71         if (ring->scan_timestamp) {
72                 st->d_size += sizeof(s64);
73
74                 if (st->d_size % sizeof(s64))
75                         st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
76         }
77
78         if (indio_dev->ring->access->set_bytes_per_datum)
79                 indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
80                                                             st->d_size);
81
82         /* We know this is a single long so can 'cheat' */
83         switch (*ring->scan_mask) {
84         case (1 << 0):
85                 st->ring_msg = &st->msg[AD7887_CH0];
86                 break;
87         case (1 << 1):
88                 st->ring_msg = &st->msg[AD7887_CH1];
89                 /* Dummy read: push CH1 setting down to hardware */
90                 spi_sync(st->spi, st->ring_msg);
91                 break;
92         case ((1 << 1) | (1 << 0)):
93                 st->ring_msg = &st->msg[AD7887_CH0_CH1];
94                 break;
95         }
96
97         return 0;
98 }
99
100 static int ad7887_ring_postdisable(struct iio_dev *indio_dev)
101 {
102         struct ad7887_state *st = iio_priv(indio_dev);
103
104         /* dummy read: restore default CH0 settin */
105         return spi_sync(st->spi, &st->msg[AD7887_CH0]);
106 }
107
108 /**
109  * ad7887_trigger_handler() bh of trigger launched polling to ring buffer
110  *
111  * Currently there is no option in this driver to disable the saving of
112  * timestamps within the ring.
113  **/
114 static irqreturn_t ad7887_trigger_handler(int irq, void *p)
115 {
116         struct iio_poll_func *pf = p;
117         struct iio_dev *indio_dev = pf->indio_dev;
118         struct ad7887_state *st = iio_priv(indio_dev);
119         struct iio_ring_buffer *ring = indio_dev->ring;
120         s64 time_ns;
121         __u8 *buf;
122         int b_sent;
123
124         unsigned int bytes = ring->scan_count *
125                 st->chip_info->channel[0].scan_type.storagebits / 8;
126
127         buf = kzalloc(st->d_size, GFP_KERNEL);
128         if (buf == NULL)
129                 return -ENOMEM;
130
131         b_sent = spi_sync(st->spi, st->ring_msg);
132         if (b_sent)
133                 goto done;
134
135         time_ns = iio_get_time_ns();
136
137         memcpy(buf, st->data, bytes);
138         if (ring->scan_timestamp)
139                 memcpy(buf + st->d_size - sizeof(s64),
140                        &time_ns, sizeof(time_ns));
141
142         indio_dev->ring->access->store_to(indio_dev->ring, buf, time_ns);
143 done:
144         kfree(buf);
145         iio_trigger_notify_done(indio_dev->trig);
146
147         return IRQ_HANDLED;
148 }
149
150 static const struct iio_ring_setup_ops ad7887_ring_setup_ops = {
151         .preenable = &ad7887_ring_preenable,
152         .postenable = &iio_triggered_ring_postenable,
153         .predisable = &iio_triggered_ring_predisable,
154         .postdisable = &ad7887_ring_postdisable,
155 };
156
157 int ad7887_register_ring_funcs_and_init(struct iio_dev *indio_dev)
158 {
159         int ret;
160
161         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
162         if (!indio_dev->ring) {
163                 ret = -ENOMEM;
164                 goto error_ret;
165         }
166         /* Effectively select the ring buffer implementation */
167         indio_dev->ring->access = &ring_sw_access_funcs;
168         indio_dev->pollfunc = iio_alloc_pollfunc(&iio_pollfunc_store_time,
169                                                  &ad7887_trigger_handler,
170                                                  IRQF_ONESHOT,
171                                                  indio_dev,
172                                                  "ad7887_consumer%d",
173                                                  indio_dev->id);
174         if (indio_dev->pollfunc == NULL) {
175                 ret = -ENOMEM;
176                 goto error_deallocate_sw_rb;
177         }
178         /* Ring buffer functions - here trigger setup related */
179         indio_dev->ring->setup_ops = &ad7887_ring_setup_ops;
180
181         /* Flag that polled ring buffering is possible */
182         indio_dev->modes |= INDIO_RING_TRIGGERED;
183         return 0;
184
185 error_deallocate_sw_rb:
186         iio_sw_rb_free(indio_dev->ring);
187 error_ret:
188         return ret;
189 }
190
191 void ad7887_ring_cleanup(struct iio_dev *indio_dev)
192 {
193         iio_dealloc_pollfunc(indio_dev->pollfunc);
194         iio_sw_rb_free(indio_dev->ring);
195 }