staging:iio:adc:ad799x removed unused headers.
[pandora-kernel.git] / drivers / staging / iio / adc / ad799x_ring.c
1 /*
2  * Copyright (C) 2010 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/device.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/i2c.h>
19 #include <linux/bitops.h>
20
21 #include "../iio.h"
22 #include "../ring_generic.h"
23 #include "../ring_sw.h"
24 #include "../trigger.h"
25 #include "../sysfs.h"
26
27 #include "ad799x.h"
28
29 int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask)
30 {
31         struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
32         int count = 0, ret;
33         u16 *ring_data;
34
35         if (!(ring->scan_mask & mask)) {
36                 ret = -EBUSY;
37                 goto error_ret;
38         }
39
40         ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
41         if (ring_data == NULL) {
42                 ret = -ENOMEM;
43                 goto error_ret;
44         }
45         ret = ring->access.read_last(ring, (u8 *) ring_data);
46         if (ret)
47                 goto error_free_ring_data;
48         /* Need a count of channels prior to this one */
49         mask >>= 1;
50         while (mask) {
51                 if (mask & ring->scan_mask)
52                         count++;
53                 mask >>= 1;
54         }
55
56         ret = be16_to_cpu(ring_data[count]);
57
58 error_free_ring_data:
59         kfree(ring_data);
60 error_ret:
61         return ret;
62 }
63
64 /**
65  * ad799x_ring_preenable() setup the parameters of the ring before enabling
66  *
67  * The complex nature of the setting of the nuber of bytes per datum is due
68  * to this driver currently ensuring that the timestamp is stored at an 8
69  * byte boundary.
70  **/
71 static int ad799x_ring_preenable(struct iio_dev *indio_dev)
72 {
73         struct iio_ring_buffer *ring = indio_dev->ring;
74         struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
75
76         /*
77          * Need to figure out the current mode based upon the requested
78          * scan mask in iio_dev
79          */
80
81         if (st->id == ad7997 || st->id == ad7998)
82                 ad7997_8_set_scan_mode(st, ring->scan_mask);
83
84         st->d_size = ring->scan_count * 2;
85
86         if (ring->scan_timestamp) {
87                 st->d_size += sizeof(s64);
88
89                 if (st->d_size % sizeof(s64))
90                         st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
91         }
92
93         if (indio_dev->ring->access.set_bytes_per_datum)
94                 indio_dev->ring->access.set_bytes_per_datum(indio_dev->ring,
95                                                             st->d_size);
96
97         return 0;
98 }
99
100 /**
101  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
102  *
103  * Currently there is no option in this driver to disable the saving of
104  * timestamps within the ring.
105  **/
106
107 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
108 {
109         struct iio_poll_func *pf = p;
110         struct iio_dev *indio_dev = pf->private_data;
111         struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
112         struct iio_ring_buffer *ring = indio_dev->ring;
113         struct iio_sw_ring_buffer *ring_sw = iio_to_sw_ring(indio_dev->ring);
114         s64 time_ns;
115         __u8 *rxbuf;
116         int b_sent;
117         u8 cmd;
118
119         rxbuf = kmalloc(st->d_size, GFP_KERNEL);
120         if (rxbuf == NULL)
121                 goto out;
122
123         switch (st->id) {
124         case ad7991:
125         case ad7995:
126         case ad7999:
127                 cmd = st->config | (ring->scan_mask << AD799X_CHANNEL_SHIFT);
128                 break;
129         case ad7992:
130         case ad7993:
131         case ad7994:
132                 cmd = (ring->scan_mask << AD799X_CHANNEL_SHIFT) |
133                         AD7998_CONV_RES_REG;
134                 break;
135         case ad7997:
136         case ad7998:
137                 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
138                 break;
139         default:
140                 cmd = 0;
141         }
142
143         b_sent = i2c_smbus_read_i2c_block_data(st->client,
144                         cmd, ring->scan_count * 2, rxbuf);
145         if (b_sent < 0)
146                 goto done;
147
148         time_ns = iio_get_time_ns();
149
150         if (ring->scan_timestamp)
151                 memcpy(rxbuf + st->d_size - sizeof(s64),
152                         &time_ns, sizeof(time_ns));
153
154         ring->access.store_to(&ring_sw->buf, rxbuf, time_ns);
155 done:
156         kfree(rxbuf);
157         if (b_sent < 0)
158                 return b_sent;
159 out:
160         iio_trigger_notify_done(indio_dev->trig);
161
162         return IRQ_HANDLED;
163 }
164
165
166 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
167 {
168         int ret = 0;
169
170         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
171         if (!indio_dev->ring) {
172                 ret = -ENOMEM;
173                 goto error_ret;
174         }
175         /* Effectively select the ring buffer implementation */
176         iio_ring_sw_register_funcs(&indio_dev->ring->access);
177         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
178         if (indio_dev->pollfunc == NULL) {
179                 ret = -ENOMEM;
180                 goto error_deallocate_sw_rb;
181         }
182         indio_dev->pollfunc->private_data = indio_dev;
183         indio_dev->pollfunc->thread = &ad799x_trigger_handler;
184         indio_dev->pollfunc->type = IRQF_ONESHOT;
185         indio_dev->pollfunc->name =
186                 kasprintf(GFP_KERNEL, "%s_consumer%d", indio_dev->name,
187                           indio_dev->id);
188         if (indio_dev->pollfunc->name == NULL) {
189                 ret = -ENOMEM;
190                 goto error_free_poll_func;
191         }
192         /* Ring buffer functions - here trigger setup related */
193
194         indio_dev->ring->preenable = &ad799x_ring_preenable;
195         indio_dev->ring->postenable = &iio_triggered_ring_postenable;
196         indio_dev->ring->predisable = &iio_triggered_ring_predisable;
197         indio_dev->ring->scan_timestamp = true;
198
199         /* Flag that polled ring buffering is possible */
200         indio_dev->modes |= INDIO_RING_TRIGGERED;
201         return 0;
202 error_free_poll_func:
203         kfree(indio_dev->pollfunc);
204 error_deallocate_sw_rb:
205         iio_sw_rb_free(indio_dev->ring);
206 error_ret:
207         return ret;
208 }
209
210 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
211 {
212         /* ensure that the trigger has been detached */
213         if (indio_dev->trig) {
214                 iio_put_trigger(indio_dev->trig);
215                 iio_trigger_dettach_poll_func(indio_dev->trig,
216                                               indio_dev->pollfunc);
217         }
218         kfree(indio_dev->pollfunc->name);
219         kfree(indio_dev->pollfunc);
220         iio_sw_rb_free(indio_dev->ring);
221 }