blob: d33590e89337e500569fac35e1341b0f695e5e7f [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>
Guenter Roeck17d82b42013-02-07 17:09:00 +000013#include <linux/of.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000014
Jonathan Cameron06458e22012-04-25 15:54:58 +010015#include <linux/iio/iio.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000016#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010017#include <linux/iio/machine.h>
18#include <linux/iio/driver.h>
19#include <linux/iio/consumer.h>
Jonathan Camerone27d75d2012-02-15 19:48:01 +000020
21struct iio_map_internal {
22 struct iio_dev *indio_dev;
23 struct iio_map *map;
24 struct list_head l;
25};
26
27static LIST_HEAD(iio_map_list);
28static DEFINE_MUTEX(iio_map_list_lock);
29
30int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
31{
32 int i = 0, ret = 0;
33 struct iio_map_internal *mapi;
34
35 if (maps == NULL)
36 return 0;
37
38 mutex_lock(&iio_map_list_lock);
39 while (maps[i].consumer_dev_name != NULL) {
40 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
41 if (mapi == NULL) {
42 ret = -ENOMEM;
43 goto error_ret;
44 }
45 mapi->map = &maps[i];
46 mapi->indio_dev = indio_dev;
47 list_add(&mapi->l, &iio_map_list);
48 i++;
49 }
50error_ret:
51 mutex_unlock(&iio_map_list_lock);
52
53 return ret;
54}
55EXPORT_SYMBOL_GPL(iio_map_array_register);
56
57
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000058/*
59 * Remove all map entries associated with the given iio device
Jonathan Camerone27d75d2012-02-15 19:48:01 +000060 */
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000061int iio_map_array_unregister(struct iio_dev *indio_dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000062{
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000063 int ret = -ENODEV;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000064 struct iio_map_internal *mapi;
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000065 struct list_head *pos, *tmp;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000066
67 mutex_lock(&iio_map_list_lock);
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000068 list_for_each_safe(pos, tmp, &iio_map_list) {
69 mapi = list_entry(pos, struct iio_map_internal, l);
70 if (indio_dev == mapi->indio_dev) {
71 list_del(&mapi->l);
72 kfree(mapi);
73 ret = 0;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000074 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +000075 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +000076 mutex_unlock(&iio_map_list_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000077 return ret;
78}
79EXPORT_SYMBOL_GPL(iio_map_array_unregister);
80
81static const struct iio_chan_spec
Jonathan Cameron314be142012-05-01 21:04:24 +010082*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000083{
84 int i;
85 const struct iio_chan_spec *chan = NULL;
86
87 for (i = 0; i < indio_dev->num_channels; i++)
88 if (indio_dev->channels[i].datasheet_name &&
89 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
90 chan = &indio_dev->channels[i];
91 break;
92 }
93 return chan;
94}
95
Guenter Roeck17d82b42013-02-07 17:09:00 +000096#ifdef CONFIG_OF
97
98static int iio_dev_node_match(struct device *dev, void *data)
99{
100 return dev->of_node == data && dev->type == &iio_device_type;
101}
102
103static int __of_iio_channel_get(struct iio_channel *channel,
104 struct device_node *np, int index)
105{
106 struct device *idev;
107 struct iio_dev *indio_dev;
108 int err;
109 struct of_phandle_args iiospec;
110
111 err = of_parse_phandle_with_args(np, "io-channels",
112 "#io-channel-cells",
113 index, &iiospec);
114 if (err)
115 return err;
116
117 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
118 iio_dev_node_match);
119 of_node_put(iiospec.np);
120 if (idev == NULL)
121 return -EPROBE_DEFER;
122
123 indio_dev = dev_to_iio_dev(idev);
124 channel->indio_dev = indio_dev;
125 index = iiospec.args_count ? iiospec.args[0] : 0;
126 if (index >= indio_dev->num_channels) {
Joe Perchese916b802013-06-04 15:44:00 +0100127 err = -EINVAL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000128 goto err_put;
129 }
130 channel->channel = &indio_dev->channels[index];
131
132 return 0;
133
134err_put:
135 iio_device_put(indio_dev);
136 return err;
137}
138
139static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
140{
141 struct iio_channel *channel;
142 int err;
143
144 if (index < 0)
145 return ERR_PTR(-EINVAL);
146
147 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
148 if (channel == NULL)
149 return ERR_PTR(-ENOMEM);
150
151 err = __of_iio_channel_get(channel, np, index);
152 if (err)
153 goto err_free_channel;
154
155 return channel;
156
157err_free_channel:
158 kfree(channel);
159 return ERR_PTR(err);
160}
161
162static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
163 const char *name)
164{
165 struct iio_channel *chan = NULL;
166
167 /* Walk up the tree of devices looking for a matching iio channel */
168 while (np) {
169 int index = 0;
170
171 /*
172 * For named iio channels, first look up the name in the
173 * "io-channel-names" property. If it cannot be found, the
174 * index will be an error code, and of_iio_channel_get()
175 * will fail.
176 */
177 if (name)
178 index = of_property_match_string(np, "io-channel-names",
179 name);
180 chan = of_iio_channel_get(np, index);
Johannes Pointner872687f2014-08-25 09:04:00 +0100181 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000182 break;
183 else if (name && index >= 0) {
184 pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
185 np->full_name, name ? name : "", index);
Adam Thomsona2c12492014-11-06 12:11:00 +0000186 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000187 }
188
189 /*
190 * No matching IIO channel found on this node.
191 * If the parent node has a "io-channel-ranges" property,
192 * then we can try one of its channels.
193 */
194 np = np->parent;
195 if (np && !of_get_property(np, "io-channel-ranges", NULL))
Adam Thomsona2c12492014-11-06 12:11:00 +0000196 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000197 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000198
Guenter Roeck17d82b42013-02-07 17:09:00 +0000199 return chan;
200}
201
202static struct iio_channel *of_iio_channel_get_all(struct device *dev)
203{
204 struct iio_channel *chans;
205 int i, mapind, nummaps = 0;
206 int ret;
207
208 do {
209 ret = of_parse_phandle_with_args(dev->of_node,
210 "io-channels",
211 "#io-channel-cells",
212 nummaps, NULL);
213 if (ret < 0)
214 break;
215 } while (++nummaps);
216
217 if (nummaps == 0) /* no error, return NULL to search map table */
218 return NULL;
219
220 /* NULL terminated array to save passing size */
221 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
222 if (chans == NULL)
223 return ERR_PTR(-ENOMEM);
224
225 /* Search for OF matches */
226 for (mapind = 0; mapind < nummaps; mapind++) {
227 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
228 mapind);
229 if (ret)
230 goto error_free_chans;
231 }
232 return chans;
233
234error_free_chans:
235 for (i = 0; i < mapind; i++)
236 iio_device_put(chans[i].indio_dev);
237 kfree(chans);
238 return ERR_PTR(ret);
239}
240
241#else /* CONFIG_OF */
242
243static inline struct iio_channel *
244of_iio_channel_get_by_name(struct device_node *np, const char *name)
245{
246 return NULL;
247}
248
249static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
250{
251 return NULL;
252}
253
254#endif /* CONFIG_OF */
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000255
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000256static struct iio_channel *iio_channel_get_sys(const char *name,
257 const char *channel_name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000258{
259 struct iio_map_internal *c_i = NULL, *c = NULL;
260 struct iio_channel *channel;
Kim, Milo3183bac2012-09-18 05:56:00 +0100261 int err;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000262
263 if (name == NULL && channel_name == NULL)
264 return ERR_PTR(-ENODEV);
265
266 /* first find matching entry the channel map */
267 mutex_lock(&iio_map_list_lock);
268 list_for_each_entry(c_i, &iio_map_list, l) {
269 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
270 (channel_name &&
271 strcmp(channel_name, c_i->map->consumer_channel) != 0))
272 continue;
273 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200274 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000275 break;
276 }
277 mutex_unlock(&iio_map_list_lock);
278 if (c == NULL)
279 return ERR_PTR(-ENODEV);
280
Kim, Milo2cc412b2012-09-14 02:24:00 +0100281 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Kim, Milo3183bac2012-09-18 05:56:00 +0100282 if (channel == NULL) {
283 err = -ENOMEM;
Kim, Milo801c4b52012-09-18 05:55:00 +0100284 goto error_no_mem;
Kim, Milo3183bac2012-09-18 05:56:00 +0100285 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000286
287 channel->indio_dev = c->indio_dev;
288
Kim, Milob2b79ff2012-09-17 09:44:00 +0100289 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000290 channel->channel =
291 iio_chan_spec_from_name(channel->indio_dev,
292 c->map->adc_channel_label);
293
Kim, Milo3183bac2012-09-18 05:56:00 +0100294 if (channel->channel == NULL) {
295 err = -EINVAL;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100296 goto error_no_chan;
Kim, Milo3183bac2012-09-18 05:56:00 +0100297 }
Kim, Milob2b79ff2012-09-17 09:44:00 +0100298 }
299
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000300 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100301
302error_no_chan:
Kim, Milob2b79ff2012-09-17 09:44:00 +0100303 kfree(channel);
Kim, Milo801c4b52012-09-18 05:55:00 +0100304error_no_mem:
305 iio_device_put(c->indio_dev);
Kim, Milo3183bac2012-09-18 05:56:00 +0100306 return ERR_PTR(err);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000307}
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000308
309struct iio_channel *iio_channel_get(struct device *dev,
310 const char *channel_name)
311{
312 const char *name = dev ? dev_name(dev) : NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000313 struct iio_channel *channel;
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000314
Guenter Roeck17d82b42013-02-07 17:09:00 +0000315 if (dev) {
316 channel = of_iio_channel_get_by_name(dev->of_node,
317 channel_name);
318 if (channel != NULL)
319 return channel;
320 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000321
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000322 return iio_channel_get_sys(name, channel_name);
323}
Jonathan Cameron314be142012-05-01 21:04:24 +0100324EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000325
Jonathan Cameron314be142012-05-01 21:04:24 +0100326void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000327{
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200328 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000329 kfree(channel);
330}
Jonathan Cameron314be142012-05-01 21:04:24 +0100331EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000332
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000333struct iio_channel *iio_channel_get_all(struct device *dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000334{
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000335 const char *name;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000336 struct iio_channel *chans;
337 struct iio_map_internal *c = NULL;
338 int nummaps = 0;
339 int mapind = 0;
340 int i, ret;
341
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000342 if (dev == NULL)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000343 return ERR_PTR(-EINVAL);
Guenter Roeck17d82b42013-02-07 17:09:00 +0000344
345 chans = of_iio_channel_get_all(dev);
346 if (chans)
347 return chans;
348
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000349 name = dev_name(dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000350
351 mutex_lock(&iio_map_list_lock);
352 /* first count the matching maps */
353 list_for_each_entry(c, &iio_map_list, l)
354 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
355 continue;
356 else
357 nummaps++;
358
359 if (nummaps == 0) {
360 ret = -ENODEV;
361 goto error_ret;
362 }
363
364 /* NULL terminated array to save passing size */
365 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
366 if (chans == NULL) {
367 ret = -ENOMEM;
368 goto error_ret;
369 }
370
371 /* for each map fill in the chans element */
372 list_for_each_entry(c, &iio_map_list, l) {
373 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
374 continue;
375 chans[mapind].indio_dev = c->indio_dev;
Jonathan Cameron04644152012-06-30 20:06:00 +0100376 chans[mapind].data = c->map->consumer_data;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000377 chans[mapind].channel =
378 iio_chan_spec_from_name(chans[mapind].indio_dev,
379 c->map->adc_channel_label);
380 if (chans[mapind].channel == NULL) {
381 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000382 goto error_free_chans;
383 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200384 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000385 mapind++;
386 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000387 if (mapind == 0) {
388 ret = -ENODEV;
389 goto error_free_chans;
390 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100391 mutex_unlock(&iio_map_list_lock);
392
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000393 return chans;
394
395error_free_chans:
396 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200397 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000398 kfree(chans);
399error_ret:
400 mutex_unlock(&iio_map_list_lock);
401
402 return ERR_PTR(ret);
403}
Jonathan Cameron314be142012-05-01 21:04:24 +0100404EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000405
Jonathan Cameron314be142012-05-01 21:04:24 +0100406void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000407{
408 struct iio_channel *chan = &channels[0];
409
410 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200411 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000412 chan++;
413 }
414 kfree(channels);
415}
Jonathan Cameron314be142012-05-01 21:04:24 +0100416EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000417
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100418static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
419 enum iio_chan_info_enum info)
420{
421 int unused;
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100422 int vals[INDIO_MAX_RAW_ELEMENTS];
423 int ret;
424 int val_len = 2;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100425
426 if (val2 == NULL)
427 val2 = &unused;
428
Fabien Proriol65de7652015-01-01 12:46:48 +0000429 if(!iio_channel_has_info(chan->channel, info))
430 return -EINVAL;
431
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100432 if (chan->indio_dev->info->read_raw_multi) {
433 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
434 chan->channel, INDIO_MAX_RAW_ELEMENTS,
435 vals, &val_len, info);
436 *val = vals[0];
437 *val2 = vals[1];
438 } else
439 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
440 chan->channel, val, val2, info);
441
442 return ret;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100443}
444
Jonathan Cameron314be142012-05-01 21:04:24 +0100445int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000446{
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100447 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000448
449 mutex_lock(&chan->indio_dev->info_exist_lock);
450 if (chan->indio_dev->info == NULL) {
451 ret = -ENODEV;
452 goto err_unlock;
453 }
454
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100455 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000456err_unlock:
457 mutex_unlock(&chan->indio_dev->info_exist_lock);
458
459 return ret;
460}
Jonathan Cameron314be142012-05-01 21:04:24 +0100461EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000462
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100463int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
464{
465 int ret;
466
467 mutex_lock(&chan->indio_dev->info_exist_lock);
468 if (chan->indio_dev->info == NULL) {
469 ret = -ENODEV;
470 goto err_unlock;
471 }
472
473 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
474err_unlock:
475 mutex_unlock(&chan->indio_dev->info_exist_lock);
476
477 return ret;
478}
479EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
480
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100481static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
482 int raw, int *processed, unsigned int scale)
483{
484 int scale_type, scale_val, scale_val2, offset;
485 s64 raw64 = raw;
486 int ret;
487
Michael Hennerich6c5d4c92013-06-03 09:04:00 +0100488 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
Alexandre Bellonif91d1b62013-07-01 17:40:00 +0100489 if (ret >= 0)
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100490 raw64 += offset;
491
492 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
493 IIO_CHAN_INFO_SCALE);
494 if (scale_type < 0)
495 return scale_type;
496
497 switch (scale_type) {
498 case IIO_VAL_INT:
499 *processed = raw64 * scale_val;
500 break;
501 case IIO_VAL_INT_PLUS_MICRO:
502 if (scale_val2 < 0)
503 *processed = -raw64 * scale_val;
504 else
505 *processed = raw64 * scale_val;
506 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
507 1000000LL);
508 break;
509 case IIO_VAL_INT_PLUS_NANO:
510 if (scale_val2 < 0)
511 *processed = -raw64 * scale_val;
512 else
513 *processed = raw64 * scale_val;
514 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
515 1000000000LL);
516 break;
517 case IIO_VAL_FRACTIONAL:
518 *processed = div_s64(raw64 * (s64)scale_val * scale,
519 scale_val2);
520 break;
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100521 case IIO_VAL_FRACTIONAL_LOG2:
522 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
523 break;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100524 default:
525 return -EINVAL;
526 }
527
528 return 0;
529}
530
531int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
532 int *processed, unsigned int scale)
533{
534 int ret;
535
536 mutex_lock(&chan->indio_dev->info_exist_lock);
537 if (chan->indio_dev->info == NULL) {
538 ret = -ENODEV;
539 goto err_unlock;
540 }
541
542 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
543 scale);
544err_unlock:
545 mutex_unlock(&chan->indio_dev->info_exist_lock);
546
547 return ret;
548}
549EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
550
551int iio_read_channel_processed(struct iio_channel *chan, int *val)
552{
553 int ret;
554
555 mutex_lock(&chan->indio_dev->info_exist_lock);
556 if (chan->indio_dev->info == NULL) {
557 ret = -ENODEV;
558 goto err_unlock;
559 }
560
561 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
562 ret = iio_channel_read(chan, val, NULL,
563 IIO_CHAN_INFO_PROCESSED);
564 } else {
565 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
566 if (ret < 0)
567 goto err_unlock;
568 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
569 }
570
571err_unlock:
572 mutex_unlock(&chan->indio_dev->info_exist_lock);
573
574 return ret;
575}
576EXPORT_SYMBOL_GPL(iio_read_channel_processed);
577
Jonathan Cameron314be142012-05-01 21:04:24 +0100578int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000579{
580 int ret;
581
582 mutex_lock(&chan->indio_dev->info_exist_lock);
583 if (chan->indio_dev->info == NULL) {
584 ret = -ENODEV;
585 goto err_unlock;
586 }
587
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100588 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000589err_unlock:
590 mutex_unlock(&chan->indio_dev->info_exist_lock);
591
592 return ret;
593}
Jonathan Cameron314be142012-05-01 21:04:24 +0100594EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000595
Jonathan Cameron314be142012-05-01 21:04:24 +0100596int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000597{
598 int ret = 0;
599 /* Need to verify underlying driver has not gone away */
600
601 mutex_lock(&chan->indio_dev->info_exist_lock);
602 if (chan->indio_dev->info == NULL) {
603 ret = -ENODEV;
604 goto err_unlock;
605 }
606
607 *type = chan->channel->type;
608err_unlock:
609 mutex_unlock(&chan->indio_dev->info_exist_lock);
610
611 return ret;
612}
Jonathan Cameron314be142012-05-01 21:04:24 +0100613EXPORT_SYMBOL_GPL(iio_get_channel_type);