1 /*
2  * Copyright 2011-2012 Analog Devices Inc.
3  *
4  * Licensed under the GPL-2.
5  *
6  */
7 
8 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 
14 #include <linux/iio/iio.h>
15 #include <linux/iio/buffer.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/triggered_buffer.h>
18 
19 #include "ad7606.h"
20 
21 /**
22  * ad7606_trigger_handler_th() th/bh of trigger launched polling to ring buffer
23  *
24  **/
ad7606_trigger_handler_th_bh(int irq,void * p)25 static irqreturn_t ad7606_trigger_handler_th_bh(int irq, void *p)
26 {
27 	struct iio_poll_func *pf = p;
28 	struct ad7606_state *st = iio_priv(pf->indio_dev);
29 
30 	gpio_set_value(st->pdata->gpio_convst, 1);
31 
32 	return IRQ_HANDLED;
33 }
34 
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  **/
ad7606_poll_bh_to_ring(struct work_struct * work_s)44 static 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);
48 	struct iio_dev *indio_dev = iio_priv_to_dev(st);
49 	__u8 *buf;
50 	int ret;
51 
52 	buf = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
53 	if (!buf)
54 		return;
55 
56 	if (gpio_is_valid(st->pdata->gpio_frstdata)) {
57 		ret = st->bops->read_block(st->dev, 1, buf);
58 		if (ret)
59 			goto done;
60 		if (!gpio_get_value(st->pdata->gpio_frstdata)) {
61 			/* This should never happen. However
62 			 * some signal glitch caused by bad PCB desgin or
63 			 * electrostatic discharge, could cause an extra read
64 			 * or clock. This allows recovery.
65 			 */
66 			ad7606_reset(st);
67 			goto done;
68 		}
69 		ret = st->bops->read_block(st->dev,
70 			st->chip_info->num_channels - 1, buf + 2);
71 		if (ret)
72 			goto done;
73 	} else {
74 		ret = st->bops->read_block(st->dev,
75 			st->chip_info->num_channels, buf);
76 		if (ret)
77 			goto done;
78 	}
79 
80 	iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
81 done:
82 	gpio_set_value(st->pdata->gpio_convst, 0);
83 	iio_trigger_notify_done(indio_dev->trig);
84 	kfree(buf);
85 }
86 
ad7606_register_ring_funcs_and_init(struct iio_dev * indio_dev)87 int ad7606_register_ring_funcs_and_init(struct iio_dev *indio_dev)
88 {
89 	struct ad7606_state *st = iio_priv(indio_dev);
90 
91 	INIT_WORK(&st->poll_work, &ad7606_poll_bh_to_ring);
92 
93 	return iio_triggered_buffer_setup(indio_dev,
94 		&ad7606_trigger_handler_th_bh, &ad7606_trigger_handler_th_bh,
95 		NULL);
96 }
97 
ad7606_ring_cleanup(struct iio_dev * indio_dev)98 void ad7606_ring_cleanup(struct iio_dev *indio_dev)
99 {
100 	iio_triggered_buffer_cleanup(indio_dev);
101 }
102