Merge branch 'drm-ttm-unmappable' into drm-core-next
[pandora-kernel.git] / drivers / staging / iio / adc / max1363_ring.c
1 /*
2  * Copyright (C) 2008 Jonathan Cameron
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * max1363_ring.c
9  */
10
11 #include <linux/interrupt.h>
12 #include <linux/gpio.h>
13 #include <linux/workqueue.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/sysfs.h>
18 #include <linux/list.h>
19 #include <linux/i2c.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 "max1363.h"
28
29 ssize_t max1363_scan_from_ring(struct device *dev,
30                                struct device_attribute *attr,
31                                char *buf)
32 {
33         struct iio_dev *dev_info = dev_get_drvdata(dev);
34         struct max1363_state *info = dev_info->dev_data;
35         int i, ret, len = 0;
36         char *ring_data;
37
38         ring_data = kmalloc(info->current_mode->numvals*2, GFP_KERNEL);
39         if (ring_data == NULL) {
40                 ret = -ENOMEM;
41                 goto error_ret;
42         }
43         ret = dev_info->ring->access.read_last(dev_info->ring, ring_data);
44         if (ret)
45                 goto error_free_ring_data;
46         len += sprintf(buf+len, "ring ");
47         for (i = 0; i < info->current_mode->numvals; i++)
48                 len += sprintf(buf + len, "%d ",
49                                ((int)(ring_data[i*2 + 0] & 0x0F) << 8)
50                                + ((int)(ring_data[i*2 + 1])));
51         len += sprintf(buf + len, "\n");
52         kfree(ring_data);
53
54         return len;
55
56 error_free_ring_data:
57         kfree(ring_data);
58 error_ret:
59         return ret;
60 }
61
62 /**
63  * max1363_ring_preenable() setup the parameters of the ring before enabling
64  *
65  * The complex nature of the setting of the nuber of bytes per datum is due
66  * to this driver currently ensuring that the timestamp is stored at an 8
67  * byte boundary.
68  **/
69 static int max1363_ring_preenable(struct iio_dev *indio_dev)
70 {
71         struct max1363_state *st = indio_dev->dev_data;
72         size_t d_size;
73
74         if (indio_dev->ring->access.set_bpd) {
75                 d_size = st->current_mode->numvals*2 + sizeof(s64);
76                 if (d_size % 8)
77                         d_size += 8 - (d_size % 8);
78                 indio_dev->ring->access.set_bpd(indio_dev->ring, d_size);
79         }
80
81         return 0;
82 }
83
84 /**
85  * max1363_ring_postenable() typical ring post enable
86  *
87  * Only not moved into the core for the hardware ring buffer cases
88  * that are more sophisticated.
89  **/
90 static int max1363_ring_postenable(struct iio_dev *indio_dev)
91 {
92         if (indio_dev->trig == NULL)
93                 return 0;
94         return iio_trigger_attach_poll_func(indio_dev->trig,
95                                             indio_dev->pollfunc);
96 }
97
98 /**
99  * max1363_ring_predisable() runs just prior to ring buffer being disabled
100  *
101  * Typical predisable function which ensures that no trigger events can
102  * occur before we disable the ring buffer (and hence would have no idea
103  * what to do with them)
104  **/
105 static int max1363_ring_predisable(struct iio_dev *indio_dev)
106 {
107         if (indio_dev->trig)
108                 return iio_trigger_dettach_poll_func(indio_dev->trig,
109                                                      indio_dev->pollfunc);
110         else
111                 return 0;
112 }
113
114 /**
115  * max1363_poll_func_th() th of trigger launched polling to ring buffer
116  *
117  * As sampling only occurs on i2c comms occuring, leave timestamping until
118  * then.  Some triggers will generate their own time stamp.  Currently
119  * there is no way of notifying them when no one cares.
120  **/
121 void max1363_poll_func_th(struct iio_dev *indio_dev)
122 {
123         struct max1363_state *st = indio_dev->dev_data;
124
125         schedule_work(&st->poll_work);
126
127         return;
128 }
129 /**
130  * max1363_poll_bh_to_ring() bh of trigger launched polling to ring buffer
131  * @work_s:     the work struct through which this was scheduled
132  *
133  * Currently there is no option in this driver to disable the saving of
134  * timestamps within the ring.
135  * I think the one copy of this at a time was to avoid problems if the
136  * trigger was set far too high and the reads then locked up the computer.
137  **/
138 static void max1363_poll_bh_to_ring(struct work_struct *work_s)
139 {
140         struct max1363_state *st = container_of(work_s, struct max1363_state,
141                                                   poll_work);
142         struct iio_dev *indio_dev = st->indio_dev;
143         struct iio_sw_ring_buffer *ring = iio_to_sw_ring(indio_dev->ring);
144         s64 time_ns;
145         __u8 *rxbuf;
146         int b_sent;
147         size_t d_size;
148
149         /* Ensure the timestamp is 8 byte aligned */
150         d_size = st->current_mode->numvals*2 + sizeof(s64);
151         if (d_size % sizeof(s64))
152                 d_size += sizeof(s64) - (d_size % sizeof(s64));
153
154         /* Ensure only one copy of this function running at a time */
155         if (atomic_inc_return(&st->protect_ring) > 1)
156                 return;
157
158         /* Monitor mode prevents reading. Whilst not currently implemented
159          * might as well have this test in here in the meantime as it does
160          * no harm.
161          */
162         if (st->current_mode->numvals == 0)
163                 return;
164
165         rxbuf = kmalloc(d_size, GFP_KERNEL);
166         if (rxbuf == NULL)
167                 return;
168
169         b_sent = i2c_master_recv(st->client,
170                                  rxbuf,
171                                  st->current_mode->numvals*2);
172         if (b_sent < 0)
173                 goto done;
174
175         time_ns = iio_get_time_ns();
176
177         memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
178
179         indio_dev->ring->access.store_to(&ring->buf, rxbuf, time_ns);
180 done:
181         kfree(rxbuf);
182         atomic_dec(&st->protect_ring);
183 }
184
185
186 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
187 {
188         struct max1363_state *st = indio_dev->dev_data;
189         int ret = 0;
190
191         indio_dev->ring = iio_sw_rb_allocate(indio_dev);
192         if (!indio_dev->ring) {
193                 ret = -ENOMEM;
194                 goto error_ret;
195         }
196         /* Effectively select the ring buffer implementation */
197         iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
198         indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
199         if (indio_dev->pollfunc == NULL) {
200                 ret = -ENOMEM;
201                 goto error_deallocate_sw_rb;
202         }
203         /* Configure the polling function called on trigger interrupts */
204         indio_dev->pollfunc->poll_func_main = &max1363_poll_func_th;
205         indio_dev->pollfunc->private_data = indio_dev;
206
207         /* Ring buffer functions - here trigger setup related */
208         indio_dev->ring->postenable = &max1363_ring_postenable;
209         indio_dev->ring->preenable = &max1363_ring_preenable;
210         indio_dev->ring->predisable = &max1363_ring_predisable;
211         INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
212
213         /* Flag that polled ring buffering is possible */
214         indio_dev->modes |= INDIO_RING_TRIGGERED;
215         return 0;
216 error_deallocate_sw_rb:
217         iio_sw_rb_free(indio_dev->ring);
218 error_ret:
219         return ret;
220 }
221
222 void max1363_ring_cleanup(struct iio_dev *indio_dev)
223 {
224         /* ensure that the trigger has been detached */
225         if (indio_dev->trig) {
226                 iio_put_trigger(indio_dev->trig);
227                 iio_trigger_dettach_poll_func(indio_dev->trig,
228                                               indio_dev->pollfunc);
229         }
230         kfree(indio_dev->pollfunc);
231         iio_sw_rb_free(indio_dev->ring);
232 }
233
234 void max1363_uninitialize_ring(struct iio_ring_buffer *ring)
235 {
236         iio_ring_buffer_unregister(ring);
237 };
238
239 int max1363_initialize_ring(struct iio_ring_buffer *ring)
240 {
241         return iio_ring_buffer_register(ring);
242 };