~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to drivers/staging/iio/adc/ad799x_ring.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 */
11
11
 
12
12
#include <linux/interrupt.h>
13
 
#include <linux/workqueue.h>
14
13
#include <linux/device.h>
15
14
#include <linux/slab.h>
16
15
#include <linux/kernel.h>
29
28
 
30
29
int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask)
31
30
{
32
 
        struct iio_ring_buffer *ring = st->indio_dev->ring;
 
31
        struct iio_ring_buffer *ring = iio_priv_to_dev(st)->ring;
33
32
        int count = 0, ret;
34
33
        u16 *ring_data;
35
34
 
38
37
                goto error_ret;
39
38
        }
40
39
 
41
 
        ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
 
40
        ring_data = kmalloc(ring->access->get_bytes_per_datum(ring),
 
41
                            GFP_KERNEL);
42
42
        if (ring_data == NULL) {
43
43
                ret = -ENOMEM;
44
44
                goto error_ret;
45
45
        }
46
 
        ret = ring->access.read_last(ring, (u8 *) ring_data);
 
46
        ret = ring->access->read_last(ring, (u8 *) ring_data);
47
47
        if (ret)
48
48
                goto error_free_ring_data;
49
49
        /* Need a count of channels prior to this one */
72
72
static int ad799x_ring_preenable(struct iio_dev *indio_dev)
73
73
{
74
74
        struct iio_ring_buffer *ring = indio_dev->ring;
75
 
        struct ad799x_state *st = indio_dev->dev_data;
76
 
        size_t d_size;
77
 
        unsigned long numvals;
 
75
        struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
78
76
 
79
77
        /*
80
78
         * Need to figure out the current mode based upon the requested
82
80
         */
83
81
 
84
82
        if (st->id == ad7997 || st->id == ad7998)
85
 
                ad799x_set_scan_mode(st, ring->scan_mask);
86
 
 
87
 
        numvals = ring->scan_count;
88
 
 
89
 
        if (ring->access.set_bytes_per_datum) {
90
 
                d_size = numvals*2 + sizeof(s64);
91
 
                if (d_size % 8)
92
 
                        d_size += 8 - (d_size % 8);
93
 
                ring->access.set_bytes_per_datum(ring, d_size);
 
83
                ad7997_8_set_scan_mode(st, ring->scan_mask);
 
84
 
 
85
        st->d_size = ring->scan_count * 2;
 
86
 
 
87
        if (ring->scan_timestamp) {
 
88
                st->d_size += sizeof(s64);
 
89
 
 
90
                if (st->d_size % sizeof(s64))
 
91
                        st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
94
92
        }
95
93
 
 
94
        if (indio_dev->ring->access->set_bytes_per_datum)
 
95
                indio_dev->ring->access->set_bytes_per_datum(indio_dev->ring,
 
96
                                                            st->d_size);
 
97
 
96
98
        return 0;
97
99
}
98
100
 
99
101
/**
100
 
 * ad799x_poll_func_th() th of trigger launched polling to ring buffer
101
 
 *
102
 
 * As sampling only occurs on i2c comms occuring, leave timestamping until
103
 
 * then.  Some triggers will generate their own time stamp.  Currently
104
 
 * there is no way of notifying them when no one cares.
105
 
 **/
106
 
static void ad799x_poll_func_th(struct iio_dev *indio_dev, s64 time)
107
 
{
108
 
        struct ad799x_state *st = indio_dev->dev_data;
109
 
 
110
 
        schedule_work(&st->poll_work);
111
 
 
112
 
        return;
113
 
}
114
 
/**
115
 
 * ad799x_poll_bh_to_ring() bh of trigger launched polling to ring buffer
116
 
 * @work_s:     the work struct through which this was scheduled
 
102
 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
117
103
 *
118
104
 * Currently there is no option in this driver to disable the saving of
119
105
 * timestamps within the ring.
120
 
 * I think the one copy of this at a time was to avoid problems if the
121
 
 * trigger was set far too high and the reads then locked up the computer.
122
106
 **/
123
 
static void ad799x_poll_bh_to_ring(struct work_struct *work_s)
 
107
 
 
108
static irqreturn_t ad799x_trigger_handler(int irq, void *p)
124
109
{
125
 
        struct ad799x_state *st = container_of(work_s, struct ad799x_state,
126
 
                                                  poll_work);
127
 
        struct iio_dev *indio_dev = st->indio_dev;
 
110
        struct iio_poll_func *pf = p;
 
111
        struct iio_dev *indio_dev = pf->private_data;
 
112
        struct ad799x_state *st = iio_dev_get_devdata(indio_dev);
128
113
        struct iio_ring_buffer *ring = indio_dev->ring;
129
 
        struct iio_sw_ring_buffer *ring_sw = iio_to_sw_ring(indio_dev->ring);
130
114
        s64 time_ns;
131
115
        __u8 *rxbuf;
132
116
        int b_sent;
133
 
        size_t d_size;
134
117
        u8 cmd;
135
118
 
136
 
        unsigned long numvals = ring->scan_count;
137
 
 
138
 
        /* Ensure the timestamp is 8 byte aligned */
139
 
        d_size = numvals*2 + sizeof(s64);
140
 
 
141
 
        if (d_size % sizeof(s64))
142
 
                d_size += sizeof(s64) - (d_size % sizeof(s64));
143
 
 
144
 
        /* Ensure only one copy of this function running at a time */
145
 
        if (atomic_inc_return(&st->protect_ring) > 1)
146
 
                return;
147
 
 
148
 
        /* Monitor mode prevents reading. Whilst not currently implemented
149
 
         * might as well have this test in here in the meantime as it does
150
 
         * no harm.
151
 
         */
152
 
        if (numvals == 0)
153
 
                return;
154
 
 
155
 
        rxbuf = kmalloc(d_size, GFP_KERNEL);
 
119
        rxbuf = kmalloc(st->d_size, GFP_KERNEL);
156
120
        if (rxbuf == NULL)
157
 
                return;
 
121
                goto out;
158
122
 
159
123
        switch (st->id) {
160
124
        case ad7991:
177
141
        }
178
142
 
179
143
        b_sent = i2c_smbus_read_i2c_block_data(st->client,
180
 
                        cmd, numvals*2, rxbuf);
 
144
                        cmd, ring->scan_count * 2, rxbuf);
181
145
        if (b_sent < 0)
182
146
                goto done;
183
147
 
184
148
        time_ns = iio_get_time_ns();
185
149
 
186
 
        memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
 
150
        if (ring->scan_timestamp)
 
151
                memcpy(rxbuf + st->d_size - sizeof(s64),
 
152
                        &time_ns, sizeof(time_ns));
187
153
 
188
 
        ring->access.store_to(&ring_sw->buf, rxbuf, time_ns);
 
154
        ring->access->store_to(indio_dev->ring, rxbuf, time_ns);
189
155
done:
190
156
        kfree(rxbuf);
191
 
        atomic_dec(&st->protect_ring);
 
157
        if (b_sent < 0)
 
158
                return b_sent;
 
159
out:
 
160
        iio_trigger_notify_done(indio_dev->trig);
 
161
 
 
162
        return IRQ_HANDLED;
192
163
}
193
164
 
 
165
static const struct iio_ring_setup_ops ad799x_buf_setup_ops = {
 
166
        .preenable = &ad799x_ring_preenable,
 
167
        .postenable = &iio_triggered_ring_postenable,
 
168
        .predisable = &iio_triggered_ring_predisable,
 
169
};
194
170
 
195
171
int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
196
172
{
197
 
        struct ad799x_state *st = indio_dev->dev_data;
198
173
        int ret = 0;
199
174
 
200
175
        indio_dev->ring = iio_sw_rb_allocate(indio_dev);
203
178
                goto error_ret;
204
179
        }
205
180
        /* Effectively select the ring buffer implementation */
206
 
        iio_ring_sw_register_funcs(&st->indio_dev->ring->access);
207
 
        ret = iio_alloc_pollfunc(indio_dev, NULL, &ad799x_poll_func_th);
208
 
        if (ret)
 
181
        indio_dev->ring->access = &ring_sw_access_funcs;
 
182
        indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
 
183
                                                 &ad799x_trigger_handler,
 
184
                                                 IRQF_ONESHOT,
 
185
                                                 indio_dev,
 
186
                                                 "%s_consumer%d",
 
187
                                                 indio_dev->name,
 
188
                                                 indio_dev->id);
 
189
        if (indio_dev->pollfunc == NULL) {
 
190
                ret = -ENOMEM;
209
191
                goto error_deallocate_sw_rb;
 
192
        }
210
193
 
211
194
        /* Ring buffer functions - here trigger setup related */
212
 
 
213
 
        indio_dev->ring->preenable = &ad799x_ring_preenable;
214
 
        indio_dev->ring->postenable = &iio_triggered_ring_postenable;
215
 
        indio_dev->ring->predisable = &iio_triggered_ring_predisable;
216
 
 
217
 
        INIT_WORK(&st->poll_work, &ad799x_poll_bh_to_ring);
218
 
 
219
 
        indio_dev->ring->scan_el_attrs = st->chip_info->scan_attrs;
 
195
        indio_dev->ring->setup_ops = &ad799x_buf_setup_ops;
 
196
        indio_dev->ring->scan_timestamp = true;
220
197
 
221
198
        /* Flag that polled ring buffering is possible */
222
199
        indio_dev->modes |= INDIO_RING_TRIGGERED;
223
200
        return 0;
 
201
 
224
202
error_deallocate_sw_rb:
225
203
        iio_sw_rb_free(indio_dev->ring);
226
204
error_ret:
235
213
                iio_trigger_dettach_poll_func(indio_dev->trig,
236
214
                                              indio_dev->pollfunc);
237
215
        }
238
 
        kfree(indio_dev->pollfunc);
 
216
        iio_dealloc_pollfunc(indio_dev->pollfunc);
239
217
        iio_sw_rb_free(indio_dev->ring);
240
218
}