]>
Commit | Line | Data |
---|---|---|
985dbe77 MH |
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/workqueue.h> | |
14 | #include <linux/device.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/kernel.h> | |
17 | #include <linux/sysfs.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/i2c.h> | |
20 | #include <linux/bitops.h> | |
21 | ||
22 | #include "../iio.h" | |
23 | #include "../ring_generic.h" | |
24 | #include "../ring_sw.h" | |
25 | #include "../trigger.h" | |
26 | #include "../sysfs.h" | |
27 | ||
28 | #include "ad799x.h" | |
29 | ||
30 | int ad799x_single_channel_from_ring(struct ad799x_state *st, long mask) | |
31 | { | |
858f1ccf | 32 | struct iio_ring_buffer *ring = st->indio_dev->ring; |
985dbe77 MH |
33 | int count = 0, ret; |
34 | u16 *ring_data; | |
1139df53 | 35 | |
858f1ccf | 36 | if (!(ring->scan_mask & mask)) { |
985dbe77 MH |
37 | ret = -EBUSY; |
38 | goto error_ret; | |
39 | } | |
985dbe77 | 40 | |
858f1ccf | 41 | ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL); |
985dbe77 MH |
42 | if (ring_data == NULL) { |
43 | ret = -ENOMEM; | |
44 | goto error_ret; | |
45 | } | |
858f1ccf | 46 | ret = ring->access.read_last(ring, (u8 *) ring_data); |
985dbe77 MH |
47 | if (ret) |
48 | goto error_free_ring_data; | |
49 | /* Need a count of channels prior to this one */ | |
50 | mask >>= 1; | |
51 | while (mask) { | |
858f1ccf | 52 | if (mask & ring->scan_mask) |
985dbe77 MH |
53 | count++; |
54 | mask >>= 1; | |
55 | } | |
56 | ||
3ba2e493 | 57 | ret = be16_to_cpu(ring_data[count]); |
985dbe77 MH |
58 | |
59 | error_free_ring_data: | |
60 | kfree(ring_data); | |
61 | error_ret: | |
62 | return ret; | |
63 | } | |
64 | ||
65 | /** | |
66 | * ad799x_ring_preenable() setup the parameters of the ring before enabling | |
67 | * | |
68 | * The complex nature of the setting of the nuber of bytes per datum is due | |
69 | * to this driver currently ensuring that the timestamp is stored at an 8 | |
70 | * byte boundary. | |
71 | **/ | |
72 | static int ad799x_ring_preenable(struct iio_dev *indio_dev) | |
73 | { | |
1139df53 | 74 | struct iio_ring_buffer *ring = indio_dev->ring; |
985dbe77 | 75 | struct ad799x_state *st = indio_dev->dev_data; |
985dbe77 MH |
76 | |
77 | /* | |
78 | * Need to figure out the current mode based upon the requested | |
79 | * scan mask in iio_dev | |
80 | */ | |
81 | ||
82 | if (st->id == ad7997 || st->id == ad7998) | |
1139df53 | 83 | ad799x_set_scan_mode(st, ring->scan_mask); |
985dbe77 | 84 | |
a2396dfc | 85 | st->d_size = ring->scan_count * 2; |
985dbe77 | 86 | |
a2396dfc MH |
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)); | |
985dbe77 MH |
92 | } |
93 | ||
a2396dfc MH |
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 | ||
985dbe77 MH |
98 | return 0; |
99 | } | |
100 | ||
101 | /** | |
102 | * ad799x_poll_func_th() th of trigger launched polling to ring buffer | |
103 | * | |
25985edc | 104 | * As sampling only occurs on i2c comms occurring, leave timestamping until |
985dbe77 MH |
105 | * then. Some triggers will generate their own time stamp. Currently |
106 | * there is no way of notifying them when no one cares. | |
107 | **/ | |
108 | static void ad799x_poll_func_th(struct iio_dev *indio_dev, s64 time) | |
109 | { | |
110 | struct ad799x_state *st = indio_dev->dev_data; | |
111 | ||
112 | schedule_work(&st->poll_work); | |
113 | ||
114 | return; | |
115 | } | |
116 | /** | |
117 | * ad799x_poll_bh_to_ring() bh of trigger launched polling to ring buffer | |
118 | * @work_s: the work struct through which this was scheduled | |
119 | * | |
120 | * Currently there is no option in this driver to disable the saving of | |
121 | * timestamps within the ring. | |
122 | * I think the one copy of this at a time was to avoid problems if the | |
123 | * trigger was set far too high and the reads then locked up the computer. | |
124 | **/ | |
125 | static void ad799x_poll_bh_to_ring(struct work_struct *work_s) | |
126 | { | |
127 | struct ad799x_state *st = container_of(work_s, struct ad799x_state, | |
128 | poll_work); | |
129 | struct iio_dev *indio_dev = st->indio_dev; | |
1139df53 MH |
130 | struct iio_ring_buffer *ring = indio_dev->ring; |
131 | struct iio_sw_ring_buffer *ring_sw = iio_to_sw_ring(indio_dev->ring); | |
985dbe77 MH |
132 | s64 time_ns; |
133 | __u8 *rxbuf; | |
134 | int b_sent; | |
985dbe77 MH |
135 | u8 cmd; |
136 | ||
985dbe77 MH |
137 | /* Ensure only one copy of this function running at a time */ |
138 | if (atomic_inc_return(&st->protect_ring) > 1) | |
139 | return; | |
140 | ||
a2396dfc | 141 | rxbuf = kmalloc(st->d_size, GFP_KERNEL); |
985dbe77 MH |
142 | if (rxbuf == NULL) |
143 | return; | |
144 | ||
145 | switch (st->id) { | |
146 | case ad7991: | |
147 | case ad7995: | |
148 | case ad7999: | |
1139df53 | 149 | cmd = st->config | (ring->scan_mask << AD799X_CHANNEL_SHIFT); |
985dbe77 MH |
150 | break; |
151 | case ad7992: | |
152 | case ad7993: | |
153 | case ad7994: | |
1139df53 MH |
154 | cmd = (ring->scan_mask << AD799X_CHANNEL_SHIFT) | |
155 | AD7998_CONV_RES_REG; | |
985dbe77 MH |
156 | break; |
157 | case ad7997: | |
158 | case ad7998: | |
159 | cmd = AD7997_8_READ_SEQUENCE | AD7998_CONV_RES_REG; | |
160 | break; | |
161 | default: | |
162 | cmd = 0; | |
163 | } | |
164 | ||
165 | b_sent = i2c_smbus_read_i2c_block_data(st->client, | |
a2396dfc | 166 | cmd, ring->scan_count * 2, rxbuf); |
985dbe77 MH |
167 | if (b_sent < 0) |
168 | goto done; | |
169 | ||
170 | time_ns = iio_get_time_ns(); | |
171 | ||
a2396dfc MH |
172 | if (ring->scan_timestamp) |
173 | memcpy(rxbuf + st->d_size - sizeof(s64), | |
174 | &time_ns, sizeof(time_ns)); | |
985dbe77 | 175 | |
1139df53 | 176 | ring->access.store_to(&ring_sw->buf, rxbuf, time_ns); |
985dbe77 MH |
177 | done: |
178 | kfree(rxbuf); | |
179 | atomic_dec(&st->protect_ring); | |
180 | } | |
181 | ||
182 | ||
183 | int ad799x_register_ring_funcs_and_init(struct iio_dev *indio_dev) | |
184 | { | |
185 | struct ad799x_state *st = indio_dev->dev_data; | |
186 | int ret = 0; | |
187 | ||
188 | indio_dev->ring = iio_sw_rb_allocate(indio_dev); | |
189 | if (!indio_dev->ring) { | |
190 | ret = -ENOMEM; | |
191 | goto error_ret; | |
192 | } | |
193 | /* Effectively select the ring buffer implementation */ | |
194 | iio_ring_sw_register_funcs(&st->indio_dev->ring->access); | |
1139df53 MH |
195 | ret = iio_alloc_pollfunc(indio_dev, NULL, &ad799x_poll_func_th); |
196 | if (ret) | |
985dbe77 | 197 | goto error_deallocate_sw_rb; |
985dbe77 MH |
198 | |
199 | /* Ring buffer functions - here trigger setup related */ | |
200 | ||
201 | indio_dev->ring->preenable = &ad799x_ring_preenable; | |
202 | indio_dev->ring->postenable = &iio_triggered_ring_postenable; | |
203 | indio_dev->ring->predisable = &iio_triggered_ring_predisable; | |
a2396dfc | 204 | indio_dev->ring->scan_timestamp = true; |
985dbe77 MH |
205 | |
206 | INIT_WORK(&st->poll_work, &ad799x_poll_bh_to_ring); | |
207 | ||
208 | indio_dev->ring->scan_el_attrs = st->chip_info->scan_attrs; | |
209 | ||
210 | /* Flag that polled ring buffering is possible */ | |
211 | indio_dev->modes |= INDIO_RING_TRIGGERED; | |
212 | return 0; | |
213 | error_deallocate_sw_rb: | |
214 | iio_sw_rb_free(indio_dev->ring); | |
215 | error_ret: | |
216 | return ret; | |
217 | } | |
218 | ||
219 | void ad799x_ring_cleanup(struct iio_dev *indio_dev) | |
220 | { | |
221 | /* ensure that the trigger has been detached */ | |
222 | if (indio_dev->trig) { | |
223 | iio_put_trigger(indio_dev->trig); | |
224 | iio_trigger_dettach_poll_func(indio_dev->trig, | |
225 | indio_dev->pollfunc); | |
226 | } | |
227 | kfree(indio_dev->pollfunc); | |
228 | iio_sw_rb_free(indio_dev->ring); | |
229 | } |