blob: 25b00761005add0e738766f258b61ef27b7aa954 [file] [log] [blame]
Jonathan Camerone27d75d2012-02-15 19:48:01 +00001/* The industrial I/O core in kernel channel mapping
2 *
3 * Copyright (c) 2011 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9#include <linux/err.h>
10#include <linux/export.h>
11#include <linux/slab.h>
12#include <linux/mutex.h>
13
Jonathan Cameron06458e22012-04-25 15:54:58 +010014#include <linux/iio/iio.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000015#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010016#include <linux/iio/machine.h>
17#include <linux/iio/driver.h>
18#include <linux/iio/consumer.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000019
20struct iio_map_internal {
21 struct iio_dev *indio_dev;
22 struct iio_map *map;
23 struct list_head l;
24};
25
26static LIST_HEAD(iio_map_list);
27static DEFINE_MUTEX(iio_map_list_lock);
28
29int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
30{
31 int i = 0, ret = 0;
32 struct iio_map_internal *mapi;
33
34 if (maps == NULL)
35 return 0;
36
37 mutex_lock(&iio_map_list_lock);
38 while (maps[i].consumer_dev_name != NULL) {
39 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
40 if (mapi == NULL) {
41 ret = -ENOMEM;
42 goto error_ret;
43 }
44 mapi->map = &maps[i];
45 mapi->indio_dev = indio_dev;
46 list_add(&mapi->l, &iio_map_list);
47 i++;
48 }
49error_ret:
50 mutex_unlock(&iio_map_list_lock);
51
52 return ret;
53}
54EXPORT_SYMBOL_GPL(iio_map_array_register);
55
56
57/* Assumes the exact same array (e.g. memory locations)
58 * used at unregistration as used at registration rather than
59 * more complex checking of contents.
60 */
61int iio_map_array_unregister(struct iio_dev *indio_dev,
62 struct iio_map *maps)
63{
64 int i = 0, ret = 0;
65 bool found_it;
66 struct iio_map_internal *mapi;
67
68 if (maps == NULL)
69 return 0;
70
71 mutex_lock(&iio_map_list_lock);
72 while (maps[i].consumer_dev_name != NULL) {
73 found_it = false;
74 list_for_each_entry(mapi, &iio_map_list, l)
75 if (&maps[i] == mapi->map) {
76 list_del(&mapi->l);
77 kfree(mapi);
78 found_it = true;
79 break;
80 }
81 if (found_it == false) {
82 ret = -ENODEV;
83 goto error_ret;
84 }
Lothar Waßmann218f4d42012-03-24 07:19:25 +010085 i++;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000086 }
87error_ret:
88 mutex_unlock(&iio_map_list_lock);
89
90 return ret;
91}
92EXPORT_SYMBOL_GPL(iio_map_array_unregister);
93
94static const struct iio_chan_spec
Jonathan Cameron314be142012-05-01 21:04:24 +010095*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000096{
97 int i;
98 const struct iio_chan_spec *chan = NULL;
99
100 for (i = 0; i < indio_dev->num_channels; i++)
101 if (indio_dev->channels[i].datasheet_name &&
102 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
103 chan = &indio_dev->channels[i];
104 break;
105 }
106 return chan;
107}
108
109
Jonathan Cameron314be142012-05-01 21:04:24 +0100110struct iio_channel *iio_channel_get(const char *name, const char *channel_name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000111{
112 struct iio_map_internal *c_i = NULL, *c = NULL;
113 struct iio_channel *channel;
114
115 if (name == NULL && channel_name == NULL)
116 return ERR_PTR(-ENODEV);
117
118 /* first find matching entry the channel map */
119 mutex_lock(&iio_map_list_lock);
120 list_for_each_entry(c_i, &iio_map_list, l) {
121 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
122 (channel_name &&
123 strcmp(channel_name, c_i->map->consumer_channel) != 0))
124 continue;
125 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200126 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000127 break;
128 }
129 mutex_unlock(&iio_map_list_lock);
130 if (c == NULL)
131 return ERR_PTR(-ENODEV);
132
Kim, Milo2cc412b2012-09-14 02:24:00 +0100133 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000134 if (channel == NULL)
135 return ERR_PTR(-ENOMEM);
136
137 channel->indio_dev = c->indio_dev;
138
Kim, Milob2b79ff2012-09-17 09:44:00 +0100139 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000140 channel->channel =
141 iio_chan_spec_from_name(channel->indio_dev,
142 c->map->adc_channel_label);
143
Kim, Milob2b79ff2012-09-17 09:44:00 +0100144 if (channel->channel == NULL)
145 goto error_no_chan;
146 }
147
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000148 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100149
150error_no_chan:
151 iio_device_put(c->indio_dev);
152 kfree(channel);
153 return ERR_PTR(-EINVAL);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000154}
Jonathan Cameron314be142012-05-01 21:04:24 +0100155EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000156
Jonathan Cameron314be142012-05-01 21:04:24 +0100157void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000158{
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200159 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000160 kfree(channel);
161}
Jonathan Cameron314be142012-05-01 21:04:24 +0100162EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000163
Jonathan Cameron314be142012-05-01 21:04:24 +0100164struct iio_channel *iio_channel_get_all(const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000165{
166 struct iio_channel *chans;
167 struct iio_map_internal *c = NULL;
168 int nummaps = 0;
169 int mapind = 0;
170 int i, ret;
171
172 if (name == NULL)
173 return ERR_PTR(-EINVAL);
174
175 mutex_lock(&iio_map_list_lock);
176 /* first count the matching maps */
177 list_for_each_entry(c, &iio_map_list, l)
178 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
179 continue;
180 else
181 nummaps++;
182
183 if (nummaps == 0) {
184 ret = -ENODEV;
185 goto error_ret;
186 }
187
188 /* NULL terminated array to save passing size */
189 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
190 if (chans == NULL) {
191 ret = -ENOMEM;
192 goto error_ret;
193 }
194
195 /* for each map fill in the chans element */
196 list_for_each_entry(c, &iio_map_list, l) {
197 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
198 continue;
199 chans[mapind].indio_dev = c->indio_dev;
200 chans[mapind].channel =
201 iio_chan_spec_from_name(chans[mapind].indio_dev,
202 c->map->adc_channel_label);
203 if (chans[mapind].channel == NULL) {
204 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000205 goto error_free_chans;
206 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200207 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000208 mapind++;
209 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000210 if (mapind == 0) {
211 ret = -ENODEV;
212 goto error_free_chans;
213 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100214 mutex_unlock(&iio_map_list_lock);
215
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000216 return chans;
217
218error_free_chans:
219 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200220 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000221 kfree(chans);
222error_ret:
223 mutex_unlock(&iio_map_list_lock);
224
225 return ERR_PTR(ret);
226}
Jonathan Cameron314be142012-05-01 21:04:24 +0100227EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000228
Jonathan Cameron314be142012-05-01 21:04:24 +0100229void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000230{
231 struct iio_channel *chan = &channels[0];
232
233 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200234 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000235 chan++;
236 }
237 kfree(channels);
238}
Jonathan Cameron314be142012-05-01 21:04:24 +0100239EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000240
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100241static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
242 enum iio_chan_info_enum info)
243{
244 int unused;
245
246 if (val2 == NULL)
247 val2 = &unused;
248
249 return chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
250 val, val2, info);
251}
252
Jonathan Cameron314be142012-05-01 21:04:24 +0100253int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000254{
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100255 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000256
257 mutex_lock(&chan->indio_dev->info_exist_lock);
258 if (chan->indio_dev->info == NULL) {
259 ret = -ENODEV;
260 goto err_unlock;
261 }
262
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100263 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000264err_unlock:
265 mutex_unlock(&chan->indio_dev->info_exist_lock);
266
267 return ret;
268}
Jonathan Cameron314be142012-05-01 21:04:24 +0100269EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000270
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100271static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
272 int raw, int *processed, unsigned int scale)
273{
274 int scale_type, scale_val, scale_val2, offset;
275 s64 raw64 = raw;
276 int ret;
277
278 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_SCALE);
279 if (ret == 0)
280 raw64 += offset;
281
282 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
283 IIO_CHAN_INFO_SCALE);
284 if (scale_type < 0)
285 return scale_type;
286
287 switch (scale_type) {
288 case IIO_VAL_INT:
289 *processed = raw64 * scale_val;
290 break;
291 case IIO_VAL_INT_PLUS_MICRO:
292 if (scale_val2 < 0)
293 *processed = -raw64 * scale_val;
294 else
295 *processed = raw64 * scale_val;
296 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
297 1000000LL);
298 break;
299 case IIO_VAL_INT_PLUS_NANO:
300 if (scale_val2 < 0)
301 *processed = -raw64 * scale_val;
302 else
303 *processed = raw64 * scale_val;
304 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
305 1000000000LL);
306 break;
307 case IIO_VAL_FRACTIONAL:
308 *processed = div_s64(raw64 * (s64)scale_val * scale,
309 scale_val2);
310 break;
311 default:
312 return -EINVAL;
313 }
314
315 return 0;
316}
317
318int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
319 int *processed, unsigned int scale)
320{
321 int ret;
322
323 mutex_lock(&chan->indio_dev->info_exist_lock);
324 if (chan->indio_dev->info == NULL) {
325 ret = -ENODEV;
326 goto err_unlock;
327 }
328
329 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
330 scale);
331err_unlock:
332 mutex_unlock(&chan->indio_dev->info_exist_lock);
333
334 return ret;
335}
336EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
337
338int iio_read_channel_processed(struct iio_channel *chan, int *val)
339{
340 int ret;
341
342 mutex_lock(&chan->indio_dev->info_exist_lock);
343 if (chan->indio_dev->info == NULL) {
344 ret = -ENODEV;
345 goto err_unlock;
346 }
347
348 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
349 ret = iio_channel_read(chan, val, NULL,
350 IIO_CHAN_INFO_PROCESSED);
351 } else {
352 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
353 if (ret < 0)
354 goto err_unlock;
355 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
356 }
357
358err_unlock:
359 mutex_unlock(&chan->indio_dev->info_exist_lock);
360
361 return ret;
362}
363EXPORT_SYMBOL_GPL(iio_read_channel_processed);
364
Jonathan Cameron314be142012-05-01 21:04:24 +0100365int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000366{
367 int ret;
368
369 mutex_lock(&chan->indio_dev->info_exist_lock);
370 if (chan->indio_dev->info == NULL) {
371 ret = -ENODEV;
372 goto err_unlock;
373 }
374
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100375 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000376err_unlock:
377 mutex_unlock(&chan->indio_dev->info_exist_lock);
378
379 return ret;
380}
Jonathan Cameron314be142012-05-01 21:04:24 +0100381EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000382
Jonathan Cameron314be142012-05-01 21:04:24 +0100383int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000384{
385 int ret = 0;
386 /* Need to verify underlying driver has not gone away */
387
388 mutex_lock(&chan->indio_dev->info_exist_lock);
389 if (chan->indio_dev->info == NULL) {
390 ret = -ENODEV;
391 goto err_unlock;
392 }
393
394 *type = chan->channel->type;
395err_unlock:
396 mutex_unlock(&chan->indio_dev->info_exist_lock);
397
398 return ret;
399}
Jonathan Cameron314be142012-05-01 21:04:24 +0100400EXPORT_SYMBOL_GPL(iio_get_channel_type);