1 /*
2 * Copyright (C) 2010-2012 Michael Hennerich, Analog Devices Inc.
3 * Copyright (C) 2008-2010 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * ad799x_ring.c
10 */
11
12 #include <linux/interrupt.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
17 #include <linux/bitops.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/triggered_buffer.h>
23
24 #include "ad799x.h"
25
26 /**
27 * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
28 *
29 * Currently there is no option in this driver to disable the saving of
30 * timestamps within the ring.
31 **/
32
ad799x_trigger_handler(int irq,void * p)33 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
34 {
35 struct iio_poll_func *pf = p;
36 struct iio_dev *indio_dev = pf->indio_dev;
37 struct ad799x_state *st = iio_priv(indio_dev);
38 s64 time_ns;
39 int b_sent;
40 u8 cmd;
41
42 switch (st->id) {
43 case ad7991:
44 case ad7995:
45 case ad7999:
46 cmd = st->config |
47 (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
48 break;
49 case ad7992:
50 case ad7993:
51 case ad7994:
52 cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
53 AD7998_CONV_RES_REG;
54 break;
55 case ad7997:
56 case ad7998:
57 cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
58 break;
59 default:
60 cmd = 0;
61 }
62
63 b_sent = i2c_smbus_read_i2c_block_data(st->client,
64 cmd, st->transfer_size, st->rx_buf);
65 if (b_sent < 0)
66 goto out;
67
68 time_ns = iio_get_time_ns();
69
70 if (indio_dev->scan_timestamp)
71 memcpy(st->rx_buf + indio_dev->scan_bytes - sizeof(s64),
72 &time_ns, sizeof(time_ns));
73
74 iio_push_to_buffers(indio_dev, st->rx_buf);
75 out:
76 iio_trigger_notify_done(indio_dev->trig);
77
78 return IRQ_HANDLED;
79 }
80
ad799x_register_ring_funcs_and_init(struct iio_dev * indio_dev)81 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
82 {
83 return iio_triggered_buffer_setup(indio_dev, NULL,
84 &ad799x_trigger_handler, NULL);
85 }
86
ad799x_ring_cleanup(struct iio_dev * indio_dev)87 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
88 {
89 iio_triggered_buffer_cleanup(indio_dev);
90 }
91