1 /* 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 #include <linux/of.h>
14
15 #include <linux/iio/iio.h>
16 #include "iio_core.h"
17 #include <linux/iio/machine.h>
18 #include <linux/iio/driver.h>
19 #include <linux/iio/consumer.h>
20
21 struct iio_map_internal {
22 struct iio_dev *indio_dev;
23 struct iio_map *map;
24 struct list_head l;
25 };
26
27 static LIST_HEAD(iio_map_list);
28 static DEFINE_MUTEX(iio_map_list_lock);
29
iio_map_array_register(struct iio_dev * indio_dev,struct iio_map * maps)30 int 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_tail(&mapi->l, &iio_map_list);
48 i++;
49 }
50 error_ret:
51 mutex_unlock(&iio_map_list_lock);
52
53 return ret;
54 }
55 EXPORT_SYMBOL_GPL(iio_map_array_register);
56
57
58 /*
59 * Remove all map entries associated with the given iio device
60 */
iio_map_array_unregister(struct iio_dev * indio_dev)61 int iio_map_array_unregister(struct iio_dev *indio_dev)
62 {
63 int ret = -ENODEV;
64 struct iio_map_internal *mapi, *next;
65
66 mutex_lock(&iio_map_list_lock);
67 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
68 if (indio_dev == mapi->indio_dev) {
69 list_del(&mapi->l);
70 kfree(mapi);
71 ret = 0;
72 }
73 }
74 mutex_unlock(&iio_map_list_lock);
75 return ret;
76 }
77 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
78
79 static const struct iio_chan_spec
iio_chan_spec_from_name(const struct iio_dev * indio_dev,const char * name)80 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
81 {
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
94 #ifdef CONFIG_OF
95
iio_dev_node_match(struct device * dev,void * data)96 static int iio_dev_node_match(struct device *dev, void *data)
97 {
98 return dev->of_node == data && dev->type == &iio_device_type;
99 }
100
101 /**
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 */
__of_iio_simple_xlate(struct iio_dev * indio_dev,const struct of_phandle_args * iiospec)111 static 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
117 if (iiospec->args[0] >= indio_dev->num_channels) {
118 dev_err(&indio_dev->dev, "invalid channel index %u\n",
119 iiospec->args[0]);
120 return -EINVAL;
121 }
122
123 return iiospec->args[0];
124 }
125
__of_iio_channel_get(struct iio_channel * channel,struct device_node * np,int index)126 static 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 if (idev == NULL) {
143 of_node_put(iiospec.np);
144 return -EPROBE_DEFER;
145 }
146
147 indio_dev = dev_to_iio_dev(idev);
148 channel->indio_dev = indio_dev;
149 if (indio_dev->info->of_xlate)
150 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
151 else
152 index = __of_iio_simple_xlate(indio_dev, &iiospec);
153 of_node_put(iiospec.np);
154 if (index < 0)
155 goto err_put;
156 channel->channel = &indio_dev->channels[index];
157
158 return 0;
159
160 err_put:
161 iio_device_put(indio_dev);
162 return index;
163 }
164
of_iio_channel_get(struct device_node * np,int index)165 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
166 {
167 struct iio_channel *channel;
168 int err;
169
170 if (index < 0)
171 return ERR_PTR(-EINVAL);
172
173 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
174 if (channel == NULL)
175 return ERR_PTR(-ENOMEM);
176
177 err = __of_iio_channel_get(channel, np, index);
178 if (err)
179 goto err_free_channel;
180
181 return channel;
182
183 err_free_channel:
184 kfree(channel);
185 return ERR_PTR(err);
186 }
187
of_iio_channel_get_by_name(struct device_node * np,const char * name)188 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
189 const char *name)
190 {
191 struct iio_channel *chan = NULL;
192
193 /* Walk up the tree of devices looking for a matching iio channel */
194 while (np) {
195 int index = 0;
196
197 /*
198 * For named iio channels, first look up the name in the
199 * "io-channel-names" property. If it cannot be found, the
200 * index will be an error code, and of_iio_channel_get()
201 * will fail.
202 */
203 if (name)
204 index = of_property_match_string(np, "io-channel-names",
205 name);
206 chan = of_iio_channel_get(np, index);
207 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
208 break;
209 else if (name && index >= 0) {
210 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
211 np, name ? name : "", index);
212 return NULL;
213 }
214
215 /*
216 * No matching IIO channel found on this node.
217 * If the parent node has a "io-channel-ranges" property,
218 * then we can try one of its channels.
219 */
220 np = np->parent;
221 if (np && !of_get_property(np, "io-channel-ranges", NULL))
222 return NULL;
223 }
224
225 return chan;
226 }
227
of_iio_channel_get_all(struct device * dev)228 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
229 {
230 struct iio_channel *chans;
231 int i, mapind, nummaps = 0;
232 int ret;
233
234 do {
235 ret = of_parse_phandle_with_args(dev->of_node,
236 "io-channels",
237 "#io-channel-cells",
238 nummaps, NULL);
239 if (ret < 0)
240 break;
241 } while (++nummaps);
242
243 if (nummaps == 0) /* no error, return NULL to search map table */
244 return NULL;
245
246 /* NULL terminated array to save passing size */
247 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
248 if (chans == NULL)
249 return ERR_PTR(-ENOMEM);
250
251 /* Search for OF matches */
252 for (mapind = 0; mapind < nummaps; mapind++) {
253 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
254 mapind);
255 if (ret)
256 goto error_free_chans;
257 }
258 return chans;
259
260 error_free_chans:
261 for (i = 0; i < mapind; i++)
262 iio_device_put(chans[i].indio_dev);
263 kfree(chans);
264 return ERR_PTR(ret);
265 }
266
267 #else /* CONFIG_OF */
268
269 static inline struct iio_channel *
of_iio_channel_get_by_name(struct device_node * np,const char * name)270 of_iio_channel_get_by_name(struct device_node *np, const char *name)
271 {
272 return NULL;
273 }
274
of_iio_channel_get_all(struct device * dev)275 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
276 {
277 return NULL;
278 }
279
280 #endif /* CONFIG_OF */
281
iio_channel_get_sys(const char * name,const char * channel_name)282 static struct iio_channel *iio_channel_get_sys(const char *name,
283 const char *channel_name)
284 {
285 struct iio_map_internal *c_i = NULL, *c = NULL;
286 struct iio_channel *channel;
287 int err;
288
289 if (name == NULL && channel_name == NULL)
290 return ERR_PTR(-ENODEV);
291
292 /* first find matching entry the channel map */
293 mutex_lock(&iio_map_list_lock);
294 list_for_each_entry(c_i, &iio_map_list, l) {
295 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
296 (channel_name &&
297 strcmp(channel_name, c_i->map->consumer_channel) != 0))
298 continue;
299 c = c_i;
300 iio_device_get(c->indio_dev);
301 break;
302 }
303 mutex_unlock(&iio_map_list_lock);
304 if (c == NULL)
305 return ERR_PTR(-ENODEV);
306
307 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
308 if (channel == NULL) {
309 err = -ENOMEM;
310 goto error_no_mem;
311 }
312
313 channel->indio_dev = c->indio_dev;
314
315 if (c->map->adc_channel_label) {
316 channel->channel =
317 iio_chan_spec_from_name(channel->indio_dev,
318 c->map->adc_channel_label);
319
320 if (channel->channel == NULL) {
321 err = -EINVAL;
322 goto error_no_chan;
323 }
324 }
325
326 return channel;
327
328 error_no_chan:
329 kfree(channel);
330 error_no_mem:
331 iio_device_put(c->indio_dev);
332 return ERR_PTR(err);
333 }
334
iio_channel_get(struct device * dev,const char * channel_name)335 struct iio_channel *iio_channel_get(struct device *dev,
336 const char *channel_name)
337 {
338 const char *name = dev ? dev_name(dev) : NULL;
339 struct iio_channel *channel;
340
341 if (dev) {
342 channel = of_iio_channel_get_by_name(dev->of_node,
343 channel_name);
344 if (channel != NULL)
345 return channel;
346 }
347
348 return iio_channel_get_sys(name, channel_name);
349 }
350 EXPORT_SYMBOL_GPL(iio_channel_get);
351
iio_channel_release(struct iio_channel * channel)352 void iio_channel_release(struct iio_channel *channel)
353 {
354 if (!channel)
355 return;
356 iio_device_put(channel->indio_dev);
357 kfree(channel);
358 }
359 EXPORT_SYMBOL_GPL(iio_channel_release);
360
devm_iio_channel_free(struct device * dev,void * res)361 static void devm_iio_channel_free(struct device *dev, void *res)
362 {
363 struct iio_channel *channel = *(struct iio_channel **)res;
364
365 iio_channel_release(channel);
366 }
367
devm_iio_channel_match(struct device * dev,void * res,void * data)368 static int devm_iio_channel_match(struct device *dev, void *res, void *data)
369 {
370 struct iio_channel **r = res;
371
372 if (!r || !*r) {
373 WARN_ON(!r || !*r);
374 return 0;
375 }
376
377 return *r == data;
378 }
379
devm_iio_channel_get(struct device * dev,const char * channel_name)380 struct iio_channel *devm_iio_channel_get(struct device *dev,
381 const char *channel_name)
382 {
383 struct iio_channel **ptr, *channel;
384
385 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
386 if (!ptr)
387 return ERR_PTR(-ENOMEM);
388
389 channel = iio_channel_get(dev, channel_name);
390 if (IS_ERR(channel)) {
391 devres_free(ptr);
392 return channel;
393 }
394
395 *ptr = channel;
396 devres_add(dev, ptr);
397
398 return channel;
399 }
400 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
401
devm_iio_channel_release(struct device * dev,struct iio_channel * channel)402 void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
403 {
404 WARN_ON(devres_release(dev, devm_iio_channel_free,
405 devm_iio_channel_match, channel));
406 }
407 EXPORT_SYMBOL_GPL(devm_iio_channel_release);
408
iio_channel_get_all(struct device * dev)409 struct iio_channel *iio_channel_get_all(struct device *dev)
410 {
411 const char *name;
412 struct iio_channel *chans;
413 struct iio_map_internal *c = NULL;
414 int nummaps = 0;
415 int mapind = 0;
416 int i, ret;
417
418 if (dev == NULL)
419 return ERR_PTR(-EINVAL);
420
421 chans = of_iio_channel_get_all(dev);
422 if (chans)
423 return chans;
424
425 name = dev_name(dev);
426
427 mutex_lock(&iio_map_list_lock);
428 /* first count the matching maps */
429 list_for_each_entry(c, &iio_map_list, l)
430 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
431 continue;
432 else
433 nummaps++;
434
435 if (nummaps == 0) {
436 ret = -ENODEV;
437 goto error_ret;
438 }
439
440 /* NULL terminated array to save passing size */
441 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
442 if (chans == NULL) {
443 ret = -ENOMEM;
444 goto error_ret;
445 }
446
447 /* for each map fill in the chans element */
448 list_for_each_entry(c, &iio_map_list, l) {
449 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
450 continue;
451 chans[mapind].indio_dev = c->indio_dev;
452 chans[mapind].data = c->map->consumer_data;
453 chans[mapind].channel =
454 iio_chan_spec_from_name(chans[mapind].indio_dev,
455 c->map->adc_channel_label);
456 if (chans[mapind].channel == NULL) {
457 ret = -EINVAL;
458 goto error_free_chans;
459 }
460 iio_device_get(chans[mapind].indio_dev);
461 mapind++;
462 }
463 if (mapind == 0) {
464 ret = -ENODEV;
465 goto error_free_chans;
466 }
467 mutex_unlock(&iio_map_list_lock);
468
469 return chans;
470
471 error_free_chans:
472 for (i = 0; i < nummaps; i++)
473 iio_device_put(chans[i].indio_dev);
474 kfree(chans);
475 error_ret:
476 mutex_unlock(&iio_map_list_lock);
477
478 return ERR_PTR(ret);
479 }
480 EXPORT_SYMBOL_GPL(iio_channel_get_all);
481
iio_channel_release_all(struct iio_channel * channels)482 void iio_channel_release_all(struct iio_channel *channels)
483 {
484 struct iio_channel *chan = &channels[0];
485
486 while (chan->indio_dev) {
487 iio_device_put(chan->indio_dev);
488 chan++;
489 }
490 kfree(channels);
491 }
492 EXPORT_SYMBOL_GPL(iio_channel_release_all);
493
devm_iio_channel_free_all(struct device * dev,void * res)494 static void devm_iio_channel_free_all(struct device *dev, void *res)
495 {
496 struct iio_channel *channels = *(struct iio_channel **)res;
497
498 iio_channel_release_all(channels);
499 }
500
devm_iio_channel_get_all(struct device * dev)501 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
502 {
503 struct iio_channel **ptr, *channels;
504
505 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
506 if (!ptr)
507 return ERR_PTR(-ENOMEM);
508
509 channels = iio_channel_get_all(dev);
510 if (IS_ERR(channels)) {
511 devres_free(ptr);
512 return channels;
513 }
514
515 *ptr = channels;
516 devres_add(dev, ptr);
517
518 return channels;
519 }
520 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
521
devm_iio_channel_release_all(struct device * dev,struct iio_channel * channels)522 void devm_iio_channel_release_all(struct device *dev,
523 struct iio_channel *channels)
524 {
525 WARN_ON(devres_release(dev, devm_iio_channel_free_all,
526 devm_iio_channel_match, channels));
527 }
528 EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
529
iio_channel_read(struct iio_channel * chan,int * val,int * val2,enum iio_chan_info_enum info)530 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
531 enum iio_chan_info_enum info)
532 {
533 int unused;
534 int vals[INDIO_MAX_RAW_ELEMENTS];
535 int ret;
536 int val_len = 2;
537
538 if (val2 == NULL)
539 val2 = &unused;
540
541 if (!iio_channel_has_info(chan->channel, info))
542 return -EINVAL;
543
544 if (chan->indio_dev->info->read_raw_multi) {
545 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
546 chan->channel, INDIO_MAX_RAW_ELEMENTS,
547 vals, &val_len, info);
548 *val = vals[0];
549 *val2 = vals[1];
550 } else
551 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
552 chan->channel, val, val2, info);
553
554 return ret;
555 }
556
iio_read_channel_raw(struct iio_channel * chan,int * val)557 int iio_read_channel_raw(struct iio_channel *chan, int *val)
558 {
559 int ret;
560
561 mutex_lock(&chan->indio_dev->info_exist_lock);
562 if (chan->indio_dev->info == NULL) {
563 ret = -ENODEV;
564 goto err_unlock;
565 }
566
567 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
568 err_unlock:
569 mutex_unlock(&chan->indio_dev->info_exist_lock);
570
571 return ret;
572 }
573 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
574
iio_read_channel_average_raw(struct iio_channel * chan,int * val)575 int iio_read_channel_average_raw(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 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
586 err_unlock:
587 mutex_unlock(&chan->indio_dev->info_exist_lock);
588
589 return ret;
590 }
591 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
592
iio_convert_raw_to_processed_unlocked(struct iio_channel * chan,int raw,int * processed,unsigned int scale)593 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
594 int raw, int *processed, unsigned int scale)
595 {
596 int scale_type, scale_val, scale_val2;
597 int offset_type, offset_val, offset_val2;
598 s64 raw64 = raw;
599
600 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
601 IIO_CHAN_INFO_OFFSET);
602 if (offset_type >= 0) {
603 switch (offset_type) {
604 case IIO_VAL_INT:
605 break;
606 case IIO_VAL_INT_PLUS_MICRO:
607 case IIO_VAL_INT_PLUS_NANO:
608 /*
609 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
610 * implicitely truncate the offset to it's integer form.
611 */
612 break;
613 case IIO_VAL_FRACTIONAL:
614 offset_val /= offset_val2;
615 break;
616 case IIO_VAL_FRACTIONAL_LOG2:
617 offset_val >>= offset_val2;
618 break;
619 default:
620 return -EINVAL;
621 }
622
623 raw64 += offset_val;
624 }
625
626 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
627 IIO_CHAN_INFO_SCALE);
628 if (scale_type < 0) {
629 /*
630 * If no channel scaling is available apply consumer scale to
631 * raw value and return.
632 */
633 *processed = raw * scale;
634 return 0;
635 }
636
637 switch (scale_type) {
638 case IIO_VAL_INT:
639 *processed = raw64 * scale_val * scale;
640 break;
641 case IIO_VAL_INT_PLUS_MICRO:
642 if (scale_val2 < 0)
643 *processed = -raw64 * scale_val;
644 else
645 *processed = raw64 * scale_val;
646 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
647 1000000LL);
648 break;
649 case IIO_VAL_INT_PLUS_NANO:
650 if (scale_val2 < 0)
651 *processed = -raw64 * scale_val;
652 else
653 *processed = raw64 * scale_val;
654 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
655 1000000000LL);
656 break;
657 case IIO_VAL_FRACTIONAL:
658 *processed = div_s64(raw64 * (s64)scale_val * scale,
659 scale_val2);
660 break;
661 case IIO_VAL_FRACTIONAL_LOG2:
662 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
663 break;
664 default:
665 return -EINVAL;
666 }
667
668 return 0;
669 }
670
iio_convert_raw_to_processed(struct iio_channel * chan,int raw,int * processed,unsigned int scale)671 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
672 int *processed, unsigned int scale)
673 {
674 int ret;
675
676 mutex_lock(&chan->indio_dev->info_exist_lock);
677 if (chan->indio_dev->info == NULL) {
678 ret = -ENODEV;
679 goto err_unlock;
680 }
681
682 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
683 scale);
684 err_unlock:
685 mutex_unlock(&chan->indio_dev->info_exist_lock);
686
687 return ret;
688 }
689 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
690
iio_read_channel_attribute(struct iio_channel * chan,int * val,int * val2,enum iio_chan_info_enum attribute)691 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
692 enum iio_chan_info_enum attribute)
693 {
694 int ret;
695
696 mutex_lock(&chan->indio_dev->info_exist_lock);
697 if (chan->indio_dev->info == NULL) {
698 ret = -ENODEV;
699 goto err_unlock;
700 }
701
702 ret = iio_channel_read(chan, val, val2, attribute);
703 err_unlock:
704 mutex_unlock(&chan->indio_dev->info_exist_lock);
705
706 return ret;
707 }
708 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
709
iio_read_channel_offset(struct iio_channel * chan,int * val,int * val2)710 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
711 {
712 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
713 }
714 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
715
iio_read_channel_processed(struct iio_channel * chan,int * val)716 int iio_read_channel_processed(struct iio_channel *chan, int *val)
717 {
718 int ret;
719
720 mutex_lock(&chan->indio_dev->info_exist_lock);
721 if (chan->indio_dev->info == NULL) {
722 ret = -ENODEV;
723 goto err_unlock;
724 }
725
726 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
727 ret = iio_channel_read(chan, val, NULL,
728 IIO_CHAN_INFO_PROCESSED);
729 } else {
730 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
731 if (ret < 0)
732 goto err_unlock;
733 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
734 }
735
736 err_unlock:
737 mutex_unlock(&chan->indio_dev->info_exist_lock);
738
739 return ret;
740 }
741 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
742
iio_read_channel_scale(struct iio_channel * chan,int * val,int * val2)743 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
744 {
745 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
746 }
747 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
748
iio_channel_read_avail(struct iio_channel * chan,const int ** vals,int * type,int * length,enum iio_chan_info_enum info)749 static int iio_channel_read_avail(struct iio_channel *chan,
750 const int **vals, int *type, int *length,
751 enum iio_chan_info_enum info)
752 {
753 if (!iio_channel_has_available(chan->channel, info))
754 return -EINVAL;
755
756 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
757 vals, type, length, info);
758 }
759
iio_read_avail_channel_raw(struct iio_channel * chan,const int ** vals,int * length)760 int iio_read_avail_channel_raw(struct iio_channel *chan,
761 const int **vals, int *length)
762 {
763 int ret;
764 int type;
765
766 mutex_lock(&chan->indio_dev->info_exist_lock);
767 if (!chan->indio_dev->info) {
768 ret = -ENODEV;
769 goto err_unlock;
770 }
771
772 ret = iio_channel_read_avail(chan,
773 vals, &type, length, IIO_CHAN_INFO_RAW);
774 err_unlock:
775 mutex_unlock(&chan->indio_dev->info_exist_lock);
776
777 if (ret >= 0 && type != IIO_VAL_INT)
778 /* raw values are assumed to be IIO_VAL_INT */
779 ret = -EINVAL;
780
781 return ret;
782 }
783 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
784
iio_channel_read_max(struct iio_channel * chan,int * val,int * val2,int * type,enum iio_chan_info_enum info)785 static int iio_channel_read_max(struct iio_channel *chan,
786 int *val, int *val2, int *type,
787 enum iio_chan_info_enum info)
788 {
789 int unused;
790 const int *vals;
791 int length;
792 int ret;
793
794 if (!val2)
795 val2 = &unused;
796
797 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
798 switch (ret) {
799 case IIO_AVAIL_RANGE:
800 switch (*type) {
801 case IIO_VAL_INT:
802 *val = vals[2];
803 break;
804 default:
805 *val = vals[4];
806 *val2 = vals[5];
807 }
808 return 0;
809
810 case IIO_AVAIL_LIST:
811 if (length <= 0)
812 return -EINVAL;
813 switch (*type) {
814 case IIO_VAL_INT:
815 *val = vals[--length];
816 while (length) {
817 if (vals[--length] > *val)
818 *val = vals[length];
819 }
820 break;
821 default:
822 /* FIXME: learn about max for other iio values */
823 return -EINVAL;
824 }
825 return 0;
826
827 default:
828 return ret;
829 }
830 }
831
iio_read_max_channel_raw(struct iio_channel * chan,int * val)832 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
833 {
834 int ret;
835 int type;
836
837 mutex_lock(&chan->indio_dev->info_exist_lock);
838 if (!chan->indio_dev->info) {
839 ret = -ENODEV;
840 goto err_unlock;
841 }
842
843 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
844 err_unlock:
845 mutex_unlock(&chan->indio_dev->info_exist_lock);
846
847 return ret;
848 }
849 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
850
iio_get_channel_type(struct iio_channel * chan,enum iio_chan_type * type)851 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
852 {
853 int ret = 0;
854 /* Need to verify underlying driver has not gone away */
855
856 mutex_lock(&chan->indio_dev->info_exist_lock);
857 if (chan->indio_dev->info == NULL) {
858 ret = -ENODEV;
859 goto err_unlock;
860 }
861
862 *type = chan->channel->type;
863 err_unlock:
864 mutex_unlock(&chan->indio_dev->info_exist_lock);
865
866 return ret;
867 }
868 EXPORT_SYMBOL_GPL(iio_get_channel_type);
869
iio_channel_write(struct iio_channel * chan,int val,int val2,enum iio_chan_info_enum info)870 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
871 enum iio_chan_info_enum info)
872 {
873 return chan->indio_dev->info->write_raw(chan->indio_dev,
874 chan->channel, val, val2, info);
875 }
876
iio_write_channel_attribute(struct iio_channel * chan,int val,int val2,enum iio_chan_info_enum attribute)877 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
878 enum iio_chan_info_enum attribute)
879 {
880 int ret;
881
882 mutex_lock(&chan->indio_dev->info_exist_lock);
883 if (chan->indio_dev->info == NULL) {
884 ret = -ENODEV;
885 goto err_unlock;
886 }
887
888 ret = iio_channel_write(chan, val, val2, attribute);
889 err_unlock:
890 mutex_unlock(&chan->indio_dev->info_exist_lock);
891
892 return ret;
893 }
894 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
895
iio_write_channel_raw(struct iio_channel * chan,int val)896 int iio_write_channel_raw(struct iio_channel *chan, int val)
897 {
898 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
899 }
900 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
901
iio_get_channel_ext_info_count(struct iio_channel * chan)902 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
903 {
904 const struct iio_chan_spec_ext_info *ext_info;
905 unsigned int i = 0;
906
907 if (!chan->channel->ext_info)
908 return i;
909
910 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
911 ++i;
912
913 return i;
914 }
915 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
916
iio_lookup_ext_info(const struct iio_channel * chan,const char * attr)917 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
918 const struct iio_channel *chan,
919 const char *attr)
920 {
921 const struct iio_chan_spec_ext_info *ext_info;
922
923 if (!chan->channel->ext_info)
924 return NULL;
925
926 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
927 if (!strcmp(attr, ext_info->name))
928 return ext_info;
929 }
930
931 return NULL;
932 }
933
iio_read_channel_ext_info(struct iio_channel * chan,const char * attr,char * buf)934 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
935 const char *attr, char *buf)
936 {
937 const struct iio_chan_spec_ext_info *ext_info;
938
939 ext_info = iio_lookup_ext_info(chan, attr);
940 if (!ext_info)
941 return -EINVAL;
942
943 return ext_info->read(chan->indio_dev, ext_info->private,
944 chan->channel, buf);
945 }
946 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
947
iio_write_channel_ext_info(struct iio_channel * chan,const char * attr,const char * buf,size_t len)948 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
949 const char *buf, size_t len)
950 {
951 const struct iio_chan_spec_ext_info *ext_info;
952
953 ext_info = iio_lookup_ext_info(chan, attr);
954 if (!ext_info)
955 return -EINVAL;
956
957 return ext_info->write(chan->indio_dev, ext_info->private,
958 chan->channel, buf, len);
959 }
960 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
961