staging:iio:ad7606: Factor out common code between periodic and one-shot capture
[linux-2.6-block.git] / drivers / staging / iio / adc / ad7606_ring.c
CommitLineData
b9618c0c 1/*
f316983f 2 * Copyright 2011-2012 Analog Devices Inc.
b9618c0c
MH
3 *
4 * Licensed under the GPL-2.
5 *
6 */
7
8#include <linux/interrupt.h>
9#include <linux/gpio.h>
b9618c0c
MH
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
b9618c0c 13
06458e27
JC
14#include <linux/iio/iio.h>
15#include <linux/iio/buffer.h>
06458e27 16#include <linux/iio/trigger_consumer.h>
74ed9649 17#include <linux/iio/triggered_buffer.h>
b9618c0c
MH
18
19#include "ad7606.h"
20
b9618c0c 21/**
1caf7cb4 22 * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
b9618c0c
MH
23 *
24 **/
1caf7cb4 25static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
b9618c0c 26{
13cfac20 27 struct iio_poll_func *pf = p;
e65bc6ac 28 struct ad7606_state *st = iio_priv(pf->indio_dev);
1caf7cb4 29
b9618c0c
MH
30 gpio_set_value(st->pdata->gpio_convst, 1);
31
13cfac20 32 return IRQ_HANDLED;
b9618c0c 33}
13cfac20 34
b9618c0c
MH
35/**
36 * ad7606_poll_bh_to_ring() bh of trigger launched polling to ring buffer
37 * @work_s: the work struct through which this was scheduled
38 *
39 * Currently there is no option in this driver to disable the saving of
40 * timestamps within the ring.
41 * I think the one copy of this at a time was to avoid problems if the
42 * trigger was set far too high and the reads then locked up the computer.
43 **/
44static void ad7606_poll_bh_to_ring(struct work_struct *work_s)
45{
46 struct ad7606_state *st = container_of(work_s, struct ad7606_state,
47 poll_work);
e61181d0 48 struct iio_dev *indio_dev = iio_priv_to_dev(st);
b9618c0c
MH
49 int ret;
50
91b7334b
LPC
51 ret = ad7606_read_samples(st);
52 if (ret == 0)
53 iio_push_to_buffers_with_timestamp(indio_dev, st->data,
54 iio_get_time_ns(indio_dev));
b9618c0c 55
b9618c0c 56 gpio_set_value(st->pdata->gpio_convst, 0);
1caf7cb4 57 iio_trigger_notify_done(indio_dev->trig);
b9618c0c
MH
58}
59
60int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
61{
44039a67 62 struct ad7606_state *st = iio_priv(indio_dev);
b9618c0c
MH
63
64 INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
65
74ed9649
LPC
66 return iio_triggered_buffer_setup(indio_dev,
67 &ad7606_trigger_handler_th_bh, &ad7606_trigger_handler_th_bh,
68 NULL);
b9618c0c
MH
69}
70
71void ad7606_ring_cleanup(struct iio_dev *indio_dev)
72{
74ed9649 73 iio_triggered_buffer_cleanup(indio_dev);
b9618c0c 74}