1 /*
2  * Copyright (C) 2010 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 "../iio.h"
20 #include "../buffer.h"
21 #include "../ring_sw.h"
22 #include "../trigger_consumer.h"
23 
24 #include "ad799x.h"
25 
26 /**
27  * ad799x_ring_preenable() setup the parameters of the ring before enabling
28  *
29  * The complex nature of the setting of the number of bytes per datum is due
30  * to this driver currently ensuring that the timestamp is stored at an 8
31  * byte boundary.
32  **/
ad799x_ring_preenable(struct iio_dev * indio_dev)33 static int ad799x_ring_preenable(struct iio_dev *indio_dev)
34 {
35 	struct iio_buffer *ring = indio_dev->buffer;
36 	struct ad799x_state *st = iio_priv(indio_dev);
37 
38 	/*
39 	 * Need to figure out the current mode based upon the requested
40 	 * scan mask in iio_dev
41 	 */
42 
43 	if (st->id == ad7997 || st->id == ad7998)
44 		ad7997_8_set_scan_mode(st, *indio_dev->active_scan_mask);
45 
46 	st->d_size = bitmap_weight(indio_dev->active_scan_mask,
47 				   indio_dev->masklength) * 2;
48 
49 	if (ring->scan_timestamp) {
50 		st->d_size += sizeof(s64);
51 
52 		if (st->d_size % sizeof(s64))
53 			st->d_size += sizeof(s64) - (st->d_size % sizeof(s64));
54 	}
55 
56 	if (indio_dev->buffer->access->set_bytes_per_datum)
57 		indio_dev->buffer->access->
58 			set_bytes_per_datum(indio_dev->buffer, st->d_size);
59 
60 	return 0;
61 }
62 
63 /**
64  * ad799x_trigger_handler() bh of trigger launched polling to ring buffer
65  *
66  * Currently there is no option in this driver to disable the saving of
67  * timestamps within the ring.
68  **/
69 
ad799x_trigger_handler(int irq,void * p)70 static irqreturn_t ad799x_trigger_handler(int irq, void *p)
71 {
72 	struct iio_poll_func *pf = p;
73 	struct iio_dev *indio_dev = pf->indio_dev;
74 	struct ad799x_state *st = iio_priv(indio_dev);
75 	struct iio_buffer *ring = indio_dev->buffer;
76 	s64 time_ns;
77 	__u8 *rxbuf;
78 	int b_sent;
79 	u8 cmd;
80 
81 	rxbuf = kmalloc(st->d_size, GFP_KERNEL);
82 	if (rxbuf == NULL)
83 		goto out;
84 
85 	switch (st->id) {
86 	case ad7991:
87 	case ad7995:
88 	case ad7999:
89 		cmd = st->config |
90 			(*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT);
91 		break;
92 	case ad7992:
93 	case ad7993:
94 	case ad7994:
95 		cmd = (*indio_dev->active_scan_mask << AD799X_CHANNEL_SHIFT) |
96 			AD7998_CONV_RES_REG;
97 		break;
98 	case ad7997:
99 	case ad7998:
100 		cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG;
101 		break;
102 	default:
103 		cmd = 0;
104 	}
105 
106 	b_sent = i2c_smbus_read_i2c_block_data(st->client,
107 			cmd, bitmap_weight(indio_dev->active_scan_mask,
108 					   indio_dev->masklength) * 2, rxbuf);
109 	if (b_sent < 0)
110 		goto done;
111 
112 	time_ns = iio_get_time_ns();
113 
114 	if (ring->scan_timestamp)
115 		memcpy(rxbuf + st->d_size - sizeof(s64),
116 			&time_ns, sizeof(time_ns));
117 
118 	ring->access->store_to(indio_dev->buffer, rxbuf, time_ns);
119 done:
120 	kfree(rxbuf);
121 	if (b_sent < 0)
122 		return b_sent;
123 out:
124 	iio_trigger_notify_done(indio_dev->trig);
125 
126 	return IRQ_HANDLED;
127 }
128 
129 static const struct iio_buffer_setup_ops ad799x_buf_setup_ops = {
130 	.preenable = &ad799x_ring_preenable,
131 	.postenable = &iio_triggered_buffer_postenable,
132 	.predisable = &iio_triggered_buffer_predisable,
133 };
134 
ad799x_register_ring_funcs_and_init(struct iio_dev * indio_dev)135 int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev)
136 {
137 	int ret = 0;
138 
139 	indio_dev->buffer = iio_sw_rb_allocate(indio_dev);
140 	if (!indio_dev->buffer) {
141 		ret = -ENOMEM;
142 		goto error_ret;
143 	}
144 	indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
145 						 &ad799x_trigger_handler,
146 						 IRQF_ONESHOT,
147 						 indio_dev,
148 						 "%s_consumer%d",
149 						 indio_dev->name,
150 						 indio_dev->id);
151 	if (indio_dev->pollfunc == NULL) {
152 		ret = -ENOMEM;
153 		goto error_deallocate_sw_rb;
154 	}
155 
156 	/* Ring buffer functions - here trigger setup related */
157 	indio_dev->setup_ops = &ad799x_buf_setup_ops;
158 	indio_dev->buffer->scan_timestamp = true;
159 
160 	/* Flag that polled ring buffering is possible */
161 	indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
162 	return 0;
163 
164 error_deallocate_sw_rb:
165 	iio_sw_rb_free(indio_dev->buffer);
166 error_ret:
167 	return ret;
168 }
169 
ad799x_ring_cleanup(struct iio_dev * indio_dev)170 void ad799x_ring_cleanup(struct iio_dev *indio_dev)
171 {
172 	iio_dealloc_pollfunc(indio_dev->pollfunc);
173 	iio_sw_rb_free(indio_dev->buffer);
174 }
175