staging:iio:adc:max1363 stop reading from buffer for sysfs access
[cascardo/linux.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/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/i2c.h>
15 #include <linux/bitops.h>
16
17 #include "../iio.h"
18 #include "../buffer.h"
19 #include "../ring_sw.h"
20 #include "../trigger_consumer.h"
21
22 #include "max1363.h"
23
24 int max1363_update_scan_mode(struct iio_dev *indio_dev,
25                              const unsigned long *scan_mask)
26 {
27         struct max1363_state *st = iio_priv(indio_dev);
28
29         /*
30          * Need to figure out the current mode based upon the requested
31          * scan mask in iio_dev
32          */
33         st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
34         if (!st->current_mode)
35                 return -EINVAL;
36         max1363_set_scan_mode(st);
37         return 0;
38 }
39
40 static irqreturn_t max1363_trigger_handler(int irq, void *p)
41 {
42         struct iio_poll_func *pf = p;
43         struct iio_dev *indio_dev = pf->indio_dev;
44         struct max1363_state *st = iio_priv(indio_dev);
45         s64 time_ns;
46         __u8 *rxbuf;
47         int b_sent;
48         size_t d_size;
49         unsigned long numvals = bitmap_weight(st->current_mode->modemask,
50                                               MAX1363_MAX_CHANNELS);
51
52         /* Ensure the timestamp is 8 byte aligned */
53         if (st->chip_info->bits != 8)
54                 d_size = numvals*2;
55         else
56                 d_size = numvals;
57         if (indio_dev->buffer->scan_timestamp) {
58                 d_size += sizeof(s64);
59                 if (d_size % sizeof(s64))
60                         d_size += sizeof(s64) - (d_size % sizeof(s64));
61         }
62         /* Monitor mode prevents reading. Whilst not currently implemented
63          * might as well have this test in here in the meantime as it does
64          * no harm.
65          */
66         if (numvals == 0)
67                 return IRQ_HANDLED;
68
69         rxbuf = kmalloc(d_size, GFP_KERNEL);
70         if (rxbuf == NULL)
71                 return -ENOMEM;
72         if (st->chip_info->bits != 8)
73                 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
74         else
75                 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
76         if (b_sent < 0)
77                 goto done;
78
79         time_ns = iio_get_time_ns();
80
81         if (indio_dev->buffer->scan_timestamp)
82                 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
83         iio_push_to_buffer(indio_dev->buffer, rxbuf, time_ns);
84
85 done:
86         iio_trigger_notify_done(indio_dev->trig);
87         kfree(rxbuf);
88
89         return IRQ_HANDLED;
90 }
91
92 static const struct iio_buffer_setup_ops max1363_ring_setup_ops = {
93         .postenable = &iio_triggered_buffer_postenable,
94         .preenable = &iio_sw_buffer_preenable,
95         .predisable = &iio_triggered_buffer_predisable,
96 };
97
98 int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
99 {
100         struct max1363_state *st = iio_priv(indio_dev);
101         int ret = 0;
102
103         indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
104         if (!indio_dev->buffer) {
105                 ret = -ENOMEM;
106                 goto error_ret;
107         }
108         indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
109                                                  &max1363_trigger_handler,
110                                                  IRQF_ONESHOT,
111                                                  indio_dev,
112                                                  "%s_consumer%d",
113                                                  st->client->name,
114                                                  indio_dev->id);
115         if (indio_dev->pollfunc == NULL) {
116                 ret = -ENOMEM;
117                 goto error_deallocate_sw_rb;
118         }
119         /* Effectively select the ring buffer implementation */
120         indio_dev->buffer->access = &ring_sw_access_funcs;
121         /* Ring buffer functions - here trigger setup related */
122         indio_dev->setup_ops = &max1363_ring_setup_ops;
123
124         /* Flag that polled ring buffering is possible */
125         indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
126
127         return 0;
128
129 error_deallocate_sw_rb:
130         iio_sw_rb_free(indio_dev->buffer);
131 error_ret:
132         return ret;
133 }
134
135 void max1363_ring_cleanup(struct iio_dev *indio_dev)
136 {
137         /* ensure that the trigger has been detached */
138         iio_dealloc_pollfunc(indio_dev->pollfunc);
139         iio_sw_rb_free(indio_dev->buffer);
140 }