blob: 80fbbfd76faf9e1859eefb1b5dc55b2129f9858b [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;
Anshul Gargc34c1812015-12-08 09:45:56 -080064 struct iio_map_internal *mapi, *next;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000065
66 mutex_lock(&iio_map_list_lock);
Anshul Gargc34c1812015-12-08 09:45:56 -080067 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
Guenter Roeck6cb2afd2013-01-31 21:43:00 +000068 if (indio_dev == mapi->indio_dev) {
69 list_del(&mapi->l);
70 kfree(mapi);
71 ret = 0;
Jonathan Camerone27d75d2012-02-15 19:48:01 +000072 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +000073 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +000074 mutex_unlock(&iio_map_list_lock);
Jonathan Camerone27d75d2012-02-15 19:48:01 +000075 return ret;
76}
77EXPORT_SYMBOL_GPL(iio_map_array_unregister);
78
79static const struct iio_chan_spec
Jonathan Cameron314be142012-05-01 21:04:24 +010080*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +000081{
82 int i;
83 const struct iio_chan_spec *chan = NULL;
84
85 for (i = 0; i < indio_dev->num_channels; i++)
86 if (indio_dev->channels[i].datasheet_name &&
87 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
88 chan = &indio_dev->channels[i];
89 break;
90 }
91 return chan;
92}
93
Guenter Roeck17d82b42013-02-07 17:09:00 +000094#ifdef CONFIG_OF
95
96static int iio_dev_node_match(struct device *dev, void *data)
97{
98 return dev->of_node == data && dev->type == &iio_device_type;
99}
100
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300101/**
102 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
103 * @indio_dev: pointer to the iio_dev structure
104 * @iiospec: IIO specifier as found in the device tree
105 *
106 * This is simple translation function, suitable for the most 1:1 mapped
107 * channels in IIO chips. This function performs only one sanity check:
108 * whether IIO index is less than num_channels (that is specified in the
109 * iio_dev).
110 */
111static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
112 const struct of_phandle_args *iiospec)
113{
114 if (!iiospec->args_count)
115 return 0;
116
Stefan Wahren1f202722015-01-01 18:13:24 +0000117 if (iiospec->args[0] >= indio_dev->num_channels) {
118 dev_err(&indio_dev->dev, "invalid channel index %u\n",
119 iiospec->args[0]);
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300120 return -EINVAL;
Stefan Wahren1f202722015-01-01 18:13:24 +0000121 }
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300122
123 return iiospec->args[0];
124}
125
Guenter Roeck17d82b42013-02-07 17:09:00 +0000126static int __of_iio_channel_get(struct iio_channel *channel,
127 struct device_node *np, int index)
128{
129 struct device *idev;
130 struct iio_dev *indio_dev;
131 int err;
132 struct of_phandle_args iiospec;
133
134 err = of_parse_phandle_with_args(np, "io-channels",
135 "#io-channel-cells",
136 index, &iiospec);
137 if (err)
138 return err;
139
140 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
141 iio_dev_node_match);
142 of_node_put(iiospec.np);
143 if (idev == NULL)
144 return -EPROBE_DEFER;
145
146 indio_dev = dev_to_iio_dev(idev);
147 channel->indio_dev = indio_dev;
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300148 if (indio_dev->info->of_xlate)
149 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
150 else
151 index = __of_iio_simple_xlate(indio_dev, &iiospec);
152 if (index < 0)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000153 goto err_put;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000154 channel->channel = &indio_dev->channels[index];
155
156 return 0;
157
158err_put:
159 iio_device_put(indio_dev);
Ivan T. Ivanovacd82562014-10-22 18:29:43 +0300160 return index;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000161}
162
163static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
164{
165 struct iio_channel *channel;
166 int err;
167
168 if (index < 0)
169 return ERR_PTR(-EINVAL);
170
171 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
172 if (channel == NULL)
173 return ERR_PTR(-ENOMEM);
174
175 err = __of_iio_channel_get(channel, np, index);
176 if (err)
177 goto err_free_channel;
178
179 return channel;
180
181err_free_channel:
182 kfree(channel);
183 return ERR_PTR(err);
184}
185
186static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
187 const char *name)
188{
189 struct iio_channel *chan = NULL;
190
191 /* Walk up the tree of devices looking for a matching iio channel */
192 while (np) {
193 int index = 0;
194
195 /*
196 * For named iio channels, first look up the name in the
197 * "io-channel-names" property. If it cannot be found, the
198 * index will be an error code, and of_iio_channel_get()
199 * will fail.
200 */
201 if (name)
202 index = of_property_match_string(np, "io-channel-names",
203 name);
204 chan = of_iio_channel_get(np, index);
Johannes Pointner872687f2014-08-25 09:04:00 +0100205 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
Guenter Roeck17d82b42013-02-07 17:09:00 +0000206 break;
207 else if (name && index >= 0) {
208 pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
209 np->full_name, name ? name : "", index);
Adam Thomsona2c12492014-11-06 12:11:00 +0000210 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000211 }
212
213 /*
214 * No matching IIO channel found on this node.
215 * If the parent node has a "io-channel-ranges" property,
216 * then we can try one of its channels.
217 */
218 np = np->parent;
219 if (np && !of_get_property(np, "io-channel-ranges", NULL))
Adam Thomsona2c12492014-11-06 12:11:00 +0000220 return NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000221 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000222
Guenter Roeck17d82b42013-02-07 17:09:00 +0000223 return chan;
224}
225
226static struct iio_channel *of_iio_channel_get_all(struct device *dev)
227{
228 struct iio_channel *chans;
229 int i, mapind, nummaps = 0;
230 int ret;
231
232 do {
233 ret = of_parse_phandle_with_args(dev->of_node,
234 "io-channels",
235 "#io-channel-cells",
236 nummaps, NULL);
237 if (ret < 0)
238 break;
239 } while (++nummaps);
240
241 if (nummaps == 0) /* no error, return NULL to search map table */
242 return NULL;
243
244 /* NULL terminated array to save passing size */
245 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
246 if (chans == NULL)
247 return ERR_PTR(-ENOMEM);
248
249 /* Search for OF matches */
250 for (mapind = 0; mapind < nummaps; mapind++) {
251 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
252 mapind);
253 if (ret)
254 goto error_free_chans;
255 }
256 return chans;
257
258error_free_chans:
259 for (i = 0; i < mapind; i++)
260 iio_device_put(chans[i].indio_dev);
261 kfree(chans);
262 return ERR_PTR(ret);
263}
264
265#else /* CONFIG_OF */
266
267static inline struct iio_channel *
268of_iio_channel_get_by_name(struct device_node *np, const char *name)
269{
270 return NULL;
271}
272
273static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
274{
275 return NULL;
276}
277
278#endif /* CONFIG_OF */
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000279
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000280static struct iio_channel *iio_channel_get_sys(const char *name,
281 const char *channel_name)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000282{
283 struct iio_map_internal *c_i = NULL, *c = NULL;
284 struct iio_channel *channel;
Kim, Milo3183bac2012-09-18 05:56:00 +0100285 int err;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000286
287 if (name == NULL && channel_name == NULL)
288 return ERR_PTR(-ENODEV);
289
290 /* first find matching entry the channel map */
291 mutex_lock(&iio_map_list_lock);
292 list_for_each_entry(c_i, &iio_map_list, l) {
293 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
294 (channel_name &&
295 strcmp(channel_name, c_i->map->consumer_channel) != 0))
296 continue;
297 c = c_i;
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200298 iio_device_get(c->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000299 break;
300 }
301 mutex_unlock(&iio_map_list_lock);
302 if (c == NULL)
303 return ERR_PTR(-ENODEV);
304
Kim, Milo2cc412b2012-09-14 02:24:00 +0100305 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
Kim, Milo3183bac2012-09-18 05:56:00 +0100306 if (channel == NULL) {
307 err = -ENOMEM;
Kim, Milo801c4b52012-09-18 05:55:00 +0100308 goto error_no_mem;
Kim, Milo3183bac2012-09-18 05:56:00 +0100309 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000310
311 channel->indio_dev = c->indio_dev;
312
Kim, Milob2b79ff2012-09-17 09:44:00 +0100313 if (c->map->adc_channel_label) {
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000314 channel->channel =
315 iio_chan_spec_from_name(channel->indio_dev,
316 c->map->adc_channel_label);
317
Kim, Milo3183bac2012-09-18 05:56:00 +0100318 if (channel->channel == NULL) {
319 err = -EINVAL;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100320 goto error_no_chan;
Kim, Milo3183bac2012-09-18 05:56:00 +0100321 }
Kim, Milob2b79ff2012-09-17 09:44:00 +0100322 }
323
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000324 return channel;
Kim, Milob2b79ff2012-09-17 09:44:00 +0100325
326error_no_chan:
Kim, Milob2b79ff2012-09-17 09:44:00 +0100327 kfree(channel);
Kim, Milo801c4b52012-09-18 05:55:00 +0100328error_no_mem:
329 iio_device_put(c->indio_dev);
Kim, Milo3183bac2012-09-18 05:56:00 +0100330 return ERR_PTR(err);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000331}
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000332
333struct iio_channel *iio_channel_get(struct device *dev,
334 const char *channel_name)
335{
336 const char *name = dev ? dev_name(dev) : NULL;
Guenter Roeck17d82b42013-02-07 17:09:00 +0000337 struct iio_channel *channel;
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000338
Guenter Roeck17d82b42013-02-07 17:09:00 +0000339 if (dev) {
340 channel = of_iio_channel_get_by_name(dev->of_node,
341 channel_name);
342 if (channel != NULL)
343 return channel;
344 }
Adam Thomsona2c12492014-11-06 12:11:00 +0000345
Guenter Roeck5aa57f02013-02-04 20:26:00 +0000346 return iio_channel_get_sys(name, channel_name);
347}
Jonathan Cameron314be142012-05-01 21:04:24 +0100348EXPORT_SYMBOL_GPL(iio_channel_get);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000349
Jonathan Cameron314be142012-05-01 21:04:24 +0100350void iio_channel_release(struct iio_channel *channel)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000351{
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200352 iio_device_put(channel->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000353 kfree(channel);
354}
Jonathan Cameron314be142012-05-01 21:04:24 +0100355EXPORT_SYMBOL_GPL(iio_channel_release);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000356
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000357struct iio_channel *iio_channel_get_all(struct device *dev)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000358{
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000359 const char *name;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000360 struct iio_channel *chans;
361 struct iio_map_internal *c = NULL;
362 int nummaps = 0;
363 int mapind = 0;
364 int i, ret;
365
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000366 if (dev == NULL)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000367 return ERR_PTR(-EINVAL);
Guenter Roeck17d82b42013-02-07 17:09:00 +0000368
369 chans = of_iio_channel_get_all(dev);
370 if (chans)
371 return chans;
372
Guenter Roeckca7d98d2013-01-31 21:43:00 +0000373 name = dev_name(dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000374
375 mutex_lock(&iio_map_list_lock);
376 /* first count the matching maps */
377 list_for_each_entry(c, &iio_map_list, l)
378 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
379 continue;
380 else
381 nummaps++;
382
383 if (nummaps == 0) {
384 ret = -ENODEV;
385 goto error_ret;
386 }
387
388 /* NULL terminated array to save passing size */
389 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
390 if (chans == NULL) {
391 ret = -ENOMEM;
392 goto error_ret;
393 }
394
395 /* for each map fill in the chans element */
396 list_for_each_entry(c, &iio_map_list, l) {
397 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
398 continue;
399 chans[mapind].indio_dev = c->indio_dev;
Jonathan Cameron04644152012-06-30 20:06:00 +0100400 chans[mapind].data = c->map->consumer_data;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000401 chans[mapind].channel =
402 iio_chan_spec_from_name(chans[mapind].indio_dev,
403 c->map->adc_channel_label);
404 if (chans[mapind].channel == NULL) {
405 ret = -EINVAL;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000406 goto error_free_chans;
407 }
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200408 iio_device_get(chans[mapind].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000409 mapind++;
410 }
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000411 if (mapind == 0) {
412 ret = -ENODEV;
413 goto error_free_chans;
414 }
Dan Carpentere59b9af2012-07-11 07:34:00 +0100415 mutex_unlock(&iio_map_list_lock);
416
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000417 return chans;
418
419error_free_chans:
420 for (i = 0; i < nummaps; i++)
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200421 iio_device_put(chans[i].indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000422 kfree(chans);
423error_ret:
424 mutex_unlock(&iio_map_list_lock);
425
426 return ERR_PTR(ret);
427}
Jonathan Cameron314be142012-05-01 21:04:24 +0100428EXPORT_SYMBOL_GPL(iio_channel_get_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000429
Jonathan Cameron314be142012-05-01 21:04:24 +0100430void iio_channel_release_all(struct iio_channel *channels)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000431{
432 struct iio_channel *chan = &channels[0];
433
434 while (chan->indio_dev) {
Lars-Peter Clausen1875ffd2012-06-04 10:50:03 +0200435 iio_device_put(chan->indio_dev);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000436 chan++;
437 }
438 kfree(channels);
439}
Jonathan Cameron314be142012-05-01 21:04:24 +0100440EXPORT_SYMBOL_GPL(iio_channel_release_all);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000441
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100442static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
443 enum iio_chan_info_enum info)
444{
445 int unused;
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100446 int vals[INDIO_MAX_RAW_ELEMENTS];
447 int ret;
448 int val_len = 2;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100449
450 if (val2 == NULL)
451 val2 = &unused;
452
Fabien Proriol65de7652015-01-01 12:46:48 +0000453 if(!iio_channel_has_info(chan->channel, info))
454 return -EINVAL;
455
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100456 if (chan->indio_dev->info->read_raw_multi) {
457 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
458 chan->channel, INDIO_MAX_RAW_ELEMENTS,
459 vals, &val_len, info);
460 *val = vals[0];
461 *val2 = vals[1];
462 } else
463 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
464 chan->channel, val, val2, info);
465
466 return ret;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100467}
468
Jonathan Cameron314be142012-05-01 21:04:24 +0100469int iio_read_channel_raw(struct iio_channel *chan, int *val)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000470{
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100471 int ret;
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000472
473 mutex_lock(&chan->indio_dev->info_exist_lock);
474 if (chan->indio_dev->info == NULL) {
475 ret = -ENODEV;
476 goto err_unlock;
477 }
478
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100479 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000480err_unlock:
481 mutex_unlock(&chan->indio_dev->info_exist_lock);
482
483 return ret;
484}
Jonathan Cameron314be142012-05-01 21:04:24 +0100485EXPORT_SYMBOL_GPL(iio_read_channel_raw);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000486
Sebastian Reichel476d4af2014-10-03 17:25:00 +0100487int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
488{
489 int ret;
490
491 mutex_lock(&chan->indio_dev->info_exist_lock);
492 if (chan->indio_dev->info == NULL) {
493 ret = -ENODEV;
494 goto err_unlock;
495 }
496
497 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
498err_unlock:
499 mutex_unlock(&chan->indio_dev->info_exist_lock);
500
501 return ret;
502}
503EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
504
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100505static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
506 int raw, int *processed, unsigned int scale)
507{
508 int scale_type, scale_val, scale_val2, offset;
509 s64 raw64 = raw;
510 int ret;
511
Michael Hennerich6c5d4c92013-06-03 09:04:00 +0100512 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
Alexandre Bellonif91d1b62013-07-01 17:40:00 +0100513 if (ret >= 0)
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100514 raw64 += offset;
515
516 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
517 IIO_CHAN_INFO_SCALE);
518 if (scale_type < 0)
519 return scale_type;
520
521 switch (scale_type) {
522 case IIO_VAL_INT:
523 *processed = raw64 * scale_val;
524 break;
525 case IIO_VAL_INT_PLUS_MICRO:
526 if (scale_val2 < 0)
527 *processed = -raw64 * scale_val;
528 else
529 *processed = raw64 * scale_val;
530 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
531 1000000LL);
532 break;
533 case IIO_VAL_INT_PLUS_NANO:
534 if (scale_val2 < 0)
535 *processed = -raw64 * scale_val;
536 else
537 *processed = raw64 * scale_val;
538 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
539 1000000000LL);
540 break;
541 case IIO_VAL_FRACTIONAL:
542 *processed = div_s64(raw64 * (s64)scale_val * scale,
543 scale_val2);
544 break;
Lars-Peter Clausen103d9fb2012-10-16 17:29:00 +0100545 case IIO_VAL_FRACTIONAL_LOG2:
546 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
547 break;
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100548 default:
549 return -EINVAL;
550 }
551
552 return 0;
553}
554
555int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
556 int *processed, unsigned int scale)
557{
558 int ret;
559
560 mutex_lock(&chan->indio_dev->info_exist_lock);
561 if (chan->indio_dev->info == NULL) {
562 ret = -ENODEV;
563 goto err_unlock;
564 }
565
566 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
567 scale);
568err_unlock:
569 mutex_unlock(&chan->indio_dev->info_exist_lock);
570
571 return ret;
572}
573EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
574
575int iio_read_channel_processed(struct iio_channel *chan, int *val)
576{
577 int ret;
578
579 mutex_lock(&chan->indio_dev->info_exist_lock);
580 if (chan->indio_dev->info == NULL) {
581 ret = -ENODEV;
582 goto err_unlock;
583 }
584
585 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
586 ret = iio_channel_read(chan, val, NULL,
587 IIO_CHAN_INFO_PROCESSED);
588 } else {
589 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
590 if (ret < 0)
591 goto err_unlock;
592 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
593 }
594
595err_unlock:
596 mutex_unlock(&chan->indio_dev->info_exist_lock);
597
598 return ret;
599}
600EXPORT_SYMBOL_GPL(iio_read_channel_processed);
601
Jonathan Cameron314be142012-05-01 21:04:24 +0100602int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000603{
604 int ret;
605
606 mutex_lock(&chan->indio_dev->info_exist_lock);
607 if (chan->indio_dev->info == NULL) {
608 ret = -ENODEV;
609 goto err_unlock;
610 }
611
Lars-Peter Clausen48e44ce2012-09-17 13:17:00 +0100612 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000613err_unlock:
614 mutex_unlock(&chan->indio_dev->info_exist_lock);
615
616 return ret;
617}
Jonathan Cameron314be142012-05-01 21:04:24 +0100618EXPORT_SYMBOL_GPL(iio_read_channel_scale);
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000619
Jonathan Cameron314be142012-05-01 21:04:24 +0100620int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
Jonathan Camerone27d75d2012-02-15 19:48:01 +0000621{
622 int ret = 0;
623 /* Need to verify underlying driver has not gone away */
624
625 mutex_lock(&chan->indio_dev->info_exist_lock);
626 if (chan->indio_dev->info == NULL) {
627 ret = -ENODEV;
628 goto err_unlock;
629 }
630
631 *type = chan->channel->type;
632err_unlock:
633 mutex_unlock(&chan->indio_dev->info_exist_lock);
634
635 return ret;
636}
Jonathan Cameron314be142012-05-01 21:04:24 +0100637EXPORT_SYMBOL_GPL(iio_get_channel_type);
Dmitry Eremin-Solenikovf9380e72014-11-27 01:42:45 +0300638
639static int iio_channel_write(struct iio_channel *chan, int val, int val2,
640 enum iio_chan_info_enum info)
641{
642 return chan->indio_dev->info->write_raw(chan->indio_dev,
643 chan->channel, val, val2, info);
644}
645
646int iio_write_channel_raw(struct iio_channel *chan, int val)
647{
648 int ret;
649
650 mutex_lock(&chan->indio_dev->info_exist_lock);
651 if (chan->indio_dev->info == NULL) {
652 ret = -ENODEV;
653 goto err_unlock;
654 }
655
656 ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
657err_unlock:
658 mutex_unlock(&chan->indio_dev->info_exist_lock);
659
660 return ret;
661}
662EXPORT_SYMBOL_GPL(iio_write_channel_raw);