Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* The industrial I/O core in kernel channel mapping
3 *
4 * Copyright (c) 2011 Jonathan Cameron
5 */
6#include <linux/err.h>
7#include <linux/export.h>
8#include <linux/slab.h>
9#include <linux/mutex.h>
10#include <linux/of.h>
11
12#include <linux/iio/iio.h>
13#include "iio_core.h"
14#include <linux/iio/machine.h>
15#include <linux/iio/driver.h>
16#include <linux/iio/consumer.h>
17
18struct iio_map_internal {
19 struct iio_dev *indio_dev;
20 struct iio_map *map;
21 struct list_head l;
22};
23
24static LIST_HEAD(iio_map_list);
25static DEFINE_MUTEX(iio_map_list_lock);
26
27int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
28{
29 int i = 0, ret = 0;
30 struct iio_map_internal *mapi;
31
32 if (maps == NULL)
33 return 0;
34
35 mutex_lock(&iio_map_list_lock);
36 while (maps[i].consumer_dev_name != NULL) {
37 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
38 if (mapi == NULL) {
39 ret = -ENOMEM;
40 goto error_ret;
41 }
42 mapi->map = &maps[i];
43 mapi->indio_dev = indio_dev;
44 list_add_tail(&mapi->l, &iio_map_list);
45 i++;
46 }
47error_ret:
48 mutex_unlock(&iio_map_list_lock);
49
50 return ret;
51}
52EXPORT_SYMBOL_GPL(iio_map_array_register);
53
54
55/*
56 * Remove all map entries associated with the given iio device
57 */
58int iio_map_array_unregister(struct iio_dev *indio_dev)
59{
60 int ret = -ENODEV;
61 struct iio_map_internal *mapi, *next;
62
63 mutex_lock(&iio_map_list_lock);
64 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
65 if (indio_dev == mapi->indio_dev) {
66 list_del(&mapi->l);
67 kfree(mapi);
68 ret = 0;
69 }
70 }
71 mutex_unlock(&iio_map_list_lock);
72 return ret;
73}
74EXPORT_SYMBOL_GPL(iio_map_array_unregister);
75
76static const struct iio_chan_spec
77*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
78{
79 int i;
80 const struct iio_chan_spec *chan = NULL;
81
82 for (i = 0; i < indio_dev->num_channels; i++)
83 if (indio_dev->channels[i].datasheet_name &&
84 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
85 chan = &indio_dev->channels[i];
86 break;
87 }
88 return chan;
89}
90
91#ifdef CONFIG_OF
92
93static int iio_dev_node_match(struct device *dev, const void *data)
94{
95 return dev->of_node == data && dev->type == &iio_device_type;
96}
97
98/**
99 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
100 * @indio_dev: pointer to the iio_dev structure
101 * @iiospec: IIO specifier as found in the device tree
102 *
103 * This is simple translation function, suitable for the most 1:1 mapped
104 * channels in IIO chips. This function performs only one sanity check:
105 * whether IIO index is less than num_channels (that is specified in the
106 * iio_dev).
107 */
108static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
109 const struct of_phandle_args *iiospec)
110{
111 if (!iiospec->args_count)
112 return 0;
113
114 if (iiospec->args[0] >= indio_dev->num_channels) {
115 dev_err(&indio_dev->dev, "invalid channel index %u\n",
116 iiospec->args[0]);
117 return -EINVAL;
118 }
119
120 return iiospec->args[0];
121}
122
123static int __of_iio_channel_get(struct iio_channel *channel,
124 struct device_node *np, int index)
125{
126 struct device *idev;
127 struct iio_dev *indio_dev;
128 int err;
129 struct of_phandle_args iiospec;
130
131 err = of_parse_phandle_with_args(np, "io-channels",
132 "#io-channel-cells",
133 index, &iiospec);
134 if (err)
135 return err;
136
137 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
138 iio_dev_node_match);
139 of_node_put(iiospec.np);
140 if (idev == NULL)
141 return -EPROBE_DEFER;
142
143 indio_dev = dev_to_iio_dev(idev);
144 channel->indio_dev = indio_dev;
145 if (indio_dev->info->of_xlate)
146 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
147 else
148 index = __of_iio_simple_xlate(indio_dev, &iiospec);
149 if (index < 0)
150 goto err_put;
151 channel->channel = &indio_dev->channels[index];
152
153 return 0;
154
155err_put:
156 iio_device_put(indio_dev);
157 return index;
158}
159
160static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
161{
162 struct iio_channel *channel;
163 int err;
164
165 if (index < 0)
166 return ERR_PTR(-EINVAL);
167
168 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
169 if (channel == NULL)
170 return ERR_PTR(-ENOMEM);
171
172 err = __of_iio_channel_get(channel, np, index);
173 if (err)
174 goto err_free_channel;
175
176 return channel;
177
178err_free_channel:
179 kfree(channel);
180 return ERR_PTR(err);
181}
182
183static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
184 const char *name)
185{
186 struct iio_channel *chan = NULL;
187
188 /* Walk up the tree of devices looking for a matching iio channel */
189 while (np) {
190 int index = 0;
191
192 /*
193 * For named iio channels, first look up the name in the
194 * "io-channel-names" property. If it cannot be found, the
195 * index will be an error code, and of_iio_channel_get()
196 * will fail.
197 */
198 if (name)
199 index = of_property_match_string(np, "io-channel-names",
200 name);
201 chan = of_iio_channel_get(np, index);
202 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
203 break;
204 else if (name && index >= 0) {
205 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
206 np, name ? name : "", index);
207 return NULL;
208 }
209
210 /*
211 * No matching IIO channel found on this node.
212 * If the parent node has a "io-channel-ranges" property,
213 * then we can try one of its channels.
214 */
215 np = np->parent;
216 if (np && !of_get_property(np, "io-channel-ranges", NULL))
217 return NULL;
218 }
219
220 return chan;
221}
222
223static struct iio_channel *of_iio_channel_get_all(struct device *dev)
224{
225 struct iio_channel *chans;
226 int i, mapind, nummaps = 0;
227 int ret;
228
229 do {
230 ret = of_parse_phandle_with_args(dev->of_node,
231 "io-channels",
232 "#io-channel-cells",
233 nummaps, NULL);
234 if (ret < 0)
235 break;
236 } while (++nummaps);
237
238 if (nummaps == 0) /* no error, return NULL to search map table */
239 return NULL;
240
241 /* NULL terminated array to save passing size */
242 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
243 if (chans == NULL)
244 return ERR_PTR(-ENOMEM);
245
246 /* Search for OF matches */
247 for (mapind = 0; mapind < nummaps; mapind++) {
248 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
249 mapind);
250 if (ret)
251 goto error_free_chans;
252 }
253 return chans;
254
255error_free_chans:
256 for (i = 0; i < mapind; i++)
257 iio_device_put(chans[i].indio_dev);
258 kfree(chans);
259 return ERR_PTR(ret);
260}
261
262#else /* CONFIG_OF */
263
264static inline struct iio_channel *
265of_iio_channel_get_by_name(struct device_node *np, const char *name)
266{
267 return NULL;
268}
269
270static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
271{
272 return NULL;
273}
274
275#endif /* CONFIG_OF */
276
277static struct iio_channel *iio_channel_get_sys(const char *name,
278 const char *channel_name)
279{
280 struct iio_map_internal *c_i = NULL, *c = NULL;
281 struct iio_channel *channel;
282 int err;
283
284 if (name == NULL && channel_name == NULL)
285 return ERR_PTR(-ENODEV);
286
287 /* first find matching entry the channel map */
288 mutex_lock(&iio_map_list_lock);
289 list_for_each_entry(c_i, &iio_map_list, l) {
290 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
291 (channel_name &&
292 strcmp(channel_name, c_i->map->consumer_channel) != 0))
293 continue;
294 c = c_i;
295 iio_device_get(c->indio_dev);
296 break;
297 }
298 mutex_unlock(&iio_map_list_lock);
299 if (c == NULL)
300 return ERR_PTR(-ENODEV);
301
302 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
303 if (channel == NULL) {
304 err = -ENOMEM;
305 goto error_no_mem;
306 }
307
308 channel->indio_dev = c->indio_dev;
309
310 if (c->map->adc_channel_label) {
311 channel->channel =
312 iio_chan_spec_from_name(channel->indio_dev,
313 c->map->adc_channel_label);
314
315 if (channel->channel == NULL) {
316 err = -EINVAL;
317 goto error_no_chan;
318 }
319 }
320
321 return channel;
322
323error_no_chan:
324 kfree(channel);
325error_no_mem:
326 iio_device_put(c->indio_dev);
327 return ERR_PTR(err);
328}
329
330struct iio_channel *iio_channel_get(struct device *dev,
331 const char *channel_name)
332{
333 const char *name = dev ? dev_name(dev) : NULL;
334 struct iio_channel *channel;
335
336 if (dev) {
337 channel = of_iio_channel_get_by_name(dev->of_node,
338 channel_name);
339 if (channel != NULL)
340 return channel;
341 }
342
343 return iio_channel_get_sys(name, channel_name);
344}
345EXPORT_SYMBOL_GPL(iio_channel_get);
346
347void iio_channel_release(struct iio_channel *channel)
348{
349 if (!channel)
350 return;
351 iio_device_put(channel->indio_dev);
352 kfree(channel);
353}
354EXPORT_SYMBOL_GPL(iio_channel_release);
355
356static void devm_iio_channel_free(struct device *dev, void *res)
357{
358 struct iio_channel *channel = *(struct iio_channel **)res;
359
360 iio_channel_release(channel);
361}
362
363struct iio_channel *devm_iio_channel_get(struct device *dev,
364 const char *channel_name)
365{
366 struct iio_channel **ptr, *channel;
367
368 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
369 if (!ptr)
370 return ERR_PTR(-ENOMEM);
371
372 channel = iio_channel_get(dev, channel_name);
373 if (IS_ERR(channel)) {
374 devres_free(ptr);
375 return channel;
376 }
377
378 *ptr = channel;
379 devres_add(dev, ptr);
380
381 return channel;
382}
383EXPORT_SYMBOL_GPL(devm_iio_channel_get);
384
385struct iio_channel *iio_channel_get_all(struct device *dev)
386{
387 const char *name;
388 struct iio_channel *chans;
389 struct iio_map_internal *c = NULL;
390 int nummaps = 0;
391 int mapind = 0;
392 int i, ret;
393
394 if (dev == NULL)
395 return ERR_PTR(-EINVAL);
396
397 chans = of_iio_channel_get_all(dev);
398 if (chans)
399 return chans;
400
401 name = dev_name(dev);
402
403 mutex_lock(&iio_map_list_lock);
404 /* first count the matching maps */
405 list_for_each_entry(c, &iio_map_list, l)
406 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
407 continue;
408 else
409 nummaps++;
410
411 if (nummaps == 0) {
412 ret = -ENODEV;
413 goto error_ret;
414 }
415
416 /* NULL terminated array to save passing size */
417 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
418 if (chans == NULL) {
419 ret = -ENOMEM;
420 goto error_ret;
421 }
422
423 /* for each map fill in the chans element */
424 list_for_each_entry(c, &iio_map_list, l) {
425 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
426 continue;
427 chans[mapind].indio_dev = c->indio_dev;
428 chans[mapind].data = c->map->consumer_data;
429 chans[mapind].channel =
430 iio_chan_spec_from_name(chans[mapind].indio_dev,
431 c->map->adc_channel_label);
432 if (chans[mapind].channel == NULL) {
433 ret = -EINVAL;
434 goto error_free_chans;
435 }
436 iio_device_get(chans[mapind].indio_dev);
437 mapind++;
438 }
439 if (mapind == 0) {
440 ret = -ENODEV;
441 goto error_free_chans;
442 }
443 mutex_unlock(&iio_map_list_lock);
444
445 return chans;
446
447error_free_chans:
448 for (i = 0; i < nummaps; i++)
449 iio_device_put(chans[i].indio_dev);
450 kfree(chans);
451error_ret:
452 mutex_unlock(&iio_map_list_lock);
453
454 return ERR_PTR(ret);
455}
456EXPORT_SYMBOL_GPL(iio_channel_get_all);
457
458void iio_channel_release_all(struct iio_channel *channels)
459{
460 struct iio_channel *chan = &channels[0];
461
462 while (chan->indio_dev) {
463 iio_device_put(chan->indio_dev);
464 chan++;
465 }
466 kfree(channels);
467}
468EXPORT_SYMBOL_GPL(iio_channel_release_all);
469
470static void devm_iio_channel_free_all(struct device *dev, void *res)
471{
472 struct iio_channel *channels = *(struct iio_channel **)res;
473
474 iio_channel_release_all(channels);
475}
476
477struct iio_channel *devm_iio_channel_get_all(struct device *dev)
478{
479 struct iio_channel **ptr, *channels;
480
481 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
482 if (!ptr)
483 return ERR_PTR(-ENOMEM);
484
485 channels = iio_channel_get_all(dev);
486 if (IS_ERR(channels)) {
487 devres_free(ptr);
488 return channels;
489 }
490
491 *ptr = channels;
492 devres_add(dev, ptr);
493
494 return channels;
495}
496EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
497
498static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
499 enum iio_chan_info_enum info)
500{
501 int unused;
502 int vals[INDIO_MAX_RAW_ELEMENTS];
503 int ret;
504 int val_len = 2;
505
506 if (val2 == NULL)
507 val2 = &unused;
508
509 if (!iio_channel_has_info(chan->channel, info))
510 return -EINVAL;
511
512 if (chan->indio_dev->info->read_raw_multi) {
513 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
514 chan->channel, INDIO_MAX_RAW_ELEMENTS,
515 vals, &val_len, info);
516 *val = vals[0];
517 *val2 = vals[1];
518 } else
519 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
520 chan->channel, val, val2, info);
521
522 return ret;
523}
524
525int iio_read_channel_raw(struct iio_channel *chan, int *val)
526{
527 int ret;
528
529 mutex_lock(&chan->indio_dev->info_exist_lock);
530 if (chan->indio_dev->info == NULL) {
531 ret = -ENODEV;
532 goto err_unlock;
533 }
534
535 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
536err_unlock:
537 mutex_unlock(&chan->indio_dev->info_exist_lock);
538
539 return ret;
540}
541EXPORT_SYMBOL_GPL(iio_read_channel_raw);
542
543int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
544{
545 int ret;
546
547 mutex_lock(&chan->indio_dev->info_exist_lock);
548 if (chan->indio_dev->info == NULL) {
549 ret = -ENODEV;
550 goto err_unlock;
551 }
552
553 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
554err_unlock:
555 mutex_unlock(&chan->indio_dev->info_exist_lock);
556
557 return ret;
558}
559EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
560
561static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
562 int raw, int *processed, unsigned int scale)
563{
564 int scale_type, scale_val, scale_val2, offset;
565 s64 raw64 = raw;
566 int ret;
567
568 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
569 if (ret >= 0)
570 raw64 += offset;
571
572 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
573 IIO_CHAN_INFO_SCALE);
574 if (scale_type < 0) {
575 /*
576 * Just pass raw values as processed if no scaling is
577 * available.
578 */
579 *processed = raw;
580 return 0;
581 }
582
583 switch (scale_type) {
584 case IIO_VAL_INT:
585 *processed = raw64 * scale_val;
586 break;
587 case IIO_VAL_INT_PLUS_MICRO:
588 if (scale_val2 < 0)
589 *processed = -raw64 * scale_val;
590 else
591 *processed = raw64 * scale_val;
592 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
593 1000000LL);
594 break;
595 case IIO_VAL_INT_PLUS_NANO:
596 if (scale_val2 < 0)
597 *processed = -raw64 * scale_val;
598 else
599 *processed = raw64 * scale_val;
600 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
601 1000000000LL);
602 break;
603 case IIO_VAL_FRACTIONAL:
604 *processed = div_s64(raw64 * (s64)scale_val * scale,
605 scale_val2);
606 break;
607 case IIO_VAL_FRACTIONAL_LOG2:
608 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
609 break;
610 default:
611 return -EINVAL;
612 }
613
614 return 0;
615}
616
617int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
618 int *processed, unsigned int scale)
619{
620 int ret;
621
622 mutex_lock(&chan->indio_dev->info_exist_lock);
623 if (chan->indio_dev->info == NULL) {
624 ret = -ENODEV;
625 goto err_unlock;
626 }
627
628 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
629 scale);
630err_unlock:
631 mutex_unlock(&chan->indio_dev->info_exist_lock);
632
633 return ret;
634}
635EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
636
637int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
638 enum iio_chan_info_enum attribute)
639{
640 int ret;
641
642 mutex_lock(&chan->indio_dev->info_exist_lock);
643 if (chan->indio_dev->info == NULL) {
644 ret = -ENODEV;
645 goto err_unlock;
646 }
647
648 ret = iio_channel_read(chan, val, val2, attribute);
649err_unlock:
650 mutex_unlock(&chan->indio_dev->info_exist_lock);
651
652 return ret;
653}
654EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
655
656int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
657{
658 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
659}
660EXPORT_SYMBOL_GPL(iio_read_channel_offset);
661
662int iio_read_channel_processed(struct iio_channel *chan, int *val)
663{
664 int ret;
665
666 mutex_lock(&chan->indio_dev->info_exist_lock);
667 if (chan->indio_dev->info == NULL) {
668 ret = -ENODEV;
669 goto err_unlock;
670 }
671
672 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
673 ret = iio_channel_read(chan, val, NULL,
674 IIO_CHAN_INFO_PROCESSED);
675 } else {
676 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
677 if (ret < 0)
678 goto err_unlock;
679 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
680 }
681
682err_unlock:
683 mutex_unlock(&chan->indio_dev->info_exist_lock);
684
685 return ret;
686}
687EXPORT_SYMBOL_GPL(iio_read_channel_processed);
688
689int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
690{
691 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
692}
693EXPORT_SYMBOL_GPL(iio_read_channel_scale);
694
695static int iio_channel_read_avail(struct iio_channel *chan,
696 const int **vals, int *type, int *length,
697 enum iio_chan_info_enum info)
698{
699 if (!iio_channel_has_available(chan->channel, info))
700 return -EINVAL;
701
702 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
703 vals, type, length, info);
704}
705
706int iio_read_avail_channel_attribute(struct iio_channel *chan,
707 const int **vals, int *type, int *length,
708 enum iio_chan_info_enum attribute)
709{
710 int ret;
711
712 mutex_lock(&chan->indio_dev->info_exist_lock);
713 if (!chan->indio_dev->info) {
714 ret = -ENODEV;
715 goto err_unlock;
716 }
717
718 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
719err_unlock:
720 mutex_unlock(&chan->indio_dev->info_exist_lock);
721
722 return ret;
723}
724EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
725
726int iio_read_avail_channel_raw(struct iio_channel *chan,
727 const int **vals, int *length)
728{
729 int ret;
730 int type;
731
732 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
733 IIO_CHAN_INFO_RAW);
734
735 if (ret >= 0 && type != IIO_VAL_INT)
736 /* raw values are assumed to be IIO_VAL_INT */
737 ret = -EINVAL;
738
739 return ret;
740}
741EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
742
743static int iio_channel_read_max(struct iio_channel *chan,
744 int *val, int *val2, int *type,
745 enum iio_chan_info_enum info)
746{
747 int unused;
748 const int *vals;
749 int length;
750 int ret;
751
752 if (!val2)
753 val2 = &unused;
754
755 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
756 switch (ret) {
757 case IIO_AVAIL_RANGE:
758 switch (*type) {
759 case IIO_VAL_INT:
760 *val = vals[2];
761 break;
762 default:
763 *val = vals[4];
764 *val2 = vals[5];
765 }
766 return 0;
767
768 case IIO_AVAIL_LIST:
769 if (length <= 0)
770 return -EINVAL;
771 switch (*type) {
772 case IIO_VAL_INT:
773 *val = vals[--length];
774 while (length) {
775 if (vals[--length] > *val)
776 *val = vals[length];
777 }
778 break;
779 default:
780 /* FIXME: learn about max for other iio values */
781 return -EINVAL;
782 }
783 return 0;
784
785 default:
786 return ret;
787 }
788}
789
790int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
791{
792 int ret;
793 int type;
794
795 mutex_lock(&chan->indio_dev->info_exist_lock);
796 if (!chan->indio_dev->info) {
797 ret = -ENODEV;
798 goto err_unlock;
799 }
800
801 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
802err_unlock:
803 mutex_unlock(&chan->indio_dev->info_exist_lock);
804
805 return ret;
806}
807EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
808
809int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
810{
811 int ret = 0;
812 /* Need to verify underlying driver has not gone away */
813
814 mutex_lock(&chan->indio_dev->info_exist_lock);
815 if (chan->indio_dev->info == NULL) {
816 ret = -ENODEV;
817 goto err_unlock;
818 }
819
820 *type = chan->channel->type;
821err_unlock:
822 mutex_unlock(&chan->indio_dev->info_exist_lock);
823
824 return ret;
825}
826EXPORT_SYMBOL_GPL(iio_get_channel_type);
827
828static int iio_channel_write(struct iio_channel *chan, int val, int val2,
829 enum iio_chan_info_enum info)
830{
831 return chan->indio_dev->info->write_raw(chan->indio_dev,
832 chan->channel, val, val2, info);
833}
834
835int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
836 enum iio_chan_info_enum attribute)
837{
838 int ret;
839
840 mutex_lock(&chan->indio_dev->info_exist_lock);
841 if (chan->indio_dev->info == NULL) {
842 ret = -ENODEV;
843 goto err_unlock;
844 }
845
846 ret = iio_channel_write(chan, val, val2, attribute);
847err_unlock:
848 mutex_unlock(&chan->indio_dev->info_exist_lock);
849
850 return ret;
851}
852EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
853
854int iio_write_channel_raw(struct iio_channel *chan, int val)
855{
856 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
857}
858EXPORT_SYMBOL_GPL(iio_write_channel_raw);
859
860unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
861{
862 const struct iio_chan_spec_ext_info *ext_info;
863 unsigned int i = 0;
864
865 if (!chan->channel->ext_info)
866 return i;
867
868 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
869 ++i;
870
871 return i;
872}
873EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
874
875static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
876 const struct iio_channel *chan,
877 const char *attr)
878{
879 const struct iio_chan_spec_ext_info *ext_info;
880
881 if (!chan->channel->ext_info)
882 return NULL;
883
884 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
885 if (!strcmp(attr, ext_info->name))
886 return ext_info;
887 }
888
889 return NULL;
890}
891
892ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
893 const char *attr, char *buf)
894{
895 const struct iio_chan_spec_ext_info *ext_info;
896
897 ext_info = iio_lookup_ext_info(chan, attr);
898 if (!ext_info)
899 return -EINVAL;
900
901 return ext_info->read(chan->indio_dev, ext_info->private,
902 chan->channel, buf);
903}
904EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
905
906ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
907 const char *buf, size_t len)
908{
909 const struct iio_chan_spec_ext_info *ext_info;
910
911 ext_info = iio_lookup_ext_info(chan, attr);
912 if (!ext_info)
913 return -EINVAL;
914
915 return ext_info->write(chan->indio_dev, ext_info->private,
916 chan->channel, buf, len);
917}
918EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
1// SPDX-License-Identifier: GPL-2.0-only
2/* The industrial I/O core in kernel channel mapping
3 *
4 * Copyright (c) 2011 Jonathan Cameron
5 */
6#include <linux/err.h>
7#include <linux/export.h>
8#include <linux/minmax.h>
9#include <linux/mutex.h>
10#include <linux/property.h>
11#include <linux/slab.h>
12
13#include <linux/iio/iio.h>
14#include <linux/iio/iio-opaque.h>
15#include "iio_core.h"
16#include <linux/iio/machine.h>
17#include <linux/iio/driver.h>
18#include <linux/iio/consumer.h>
19
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
29static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
30{
31 int ret = -ENODEV;
32 struct iio_map_internal *mapi, *next;
33
34 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
35 if (indio_dev == mapi->indio_dev) {
36 list_del(&mapi->l);
37 kfree(mapi);
38 ret = 0;
39 }
40 }
41 return ret;
42}
43
44int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
45{
46 int i = 0, ret = 0;
47 struct iio_map_internal *mapi;
48
49 if (!maps)
50 return 0;
51
52 mutex_lock(&iio_map_list_lock);
53 while (maps[i].consumer_dev_name) {
54 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
55 if (!mapi) {
56 ret = -ENOMEM;
57 goto error_ret;
58 }
59 mapi->map = &maps[i];
60 mapi->indio_dev = indio_dev;
61 list_add_tail(&mapi->l, &iio_map_list);
62 i++;
63 }
64error_ret:
65 if (ret)
66 iio_map_array_unregister_locked(indio_dev);
67 mutex_unlock(&iio_map_list_lock);
68
69 return ret;
70}
71EXPORT_SYMBOL_GPL(iio_map_array_register);
72
73/*
74 * Remove all map entries associated with the given iio device
75 */
76int iio_map_array_unregister(struct iio_dev *indio_dev)
77{
78 int ret;
79
80 mutex_lock(&iio_map_list_lock);
81 ret = iio_map_array_unregister_locked(indio_dev);
82 mutex_unlock(&iio_map_list_lock);
83
84 return ret;
85}
86EXPORT_SYMBOL_GPL(iio_map_array_unregister);
87
88static void iio_map_array_unregister_cb(void *indio_dev)
89{
90 iio_map_array_unregister(indio_dev);
91}
92
93int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps)
94{
95 int ret;
96
97 ret = iio_map_array_register(indio_dev, maps);
98 if (ret)
99 return ret;
100
101 return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
102}
103EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
104
105static const struct iio_chan_spec
106*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
107{
108 int i;
109 const struct iio_chan_spec *chan = NULL;
110
111 for (i = 0; i < indio_dev->num_channels; i++)
112 if (indio_dev->channels[i].datasheet_name &&
113 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
114 chan = &indio_dev->channels[i];
115 break;
116 }
117 return chan;
118}
119
120/**
121 * __fwnode_iio_simple_xlate - translate iiospec to the IIO channel index
122 * @indio_dev: pointer to the iio_dev structure
123 * @iiospec: IIO specifier as found in the device tree
124 *
125 * This is simple translation function, suitable for the most 1:1 mapped
126 * channels in IIO chips. This function performs only one sanity check:
127 * whether IIO index is less than num_channels (that is specified in the
128 * iio_dev).
129 */
130static int __fwnode_iio_simple_xlate(struct iio_dev *indio_dev,
131 const struct fwnode_reference_args *iiospec)
132{
133 if (!iiospec->nargs)
134 return 0;
135
136 if (iiospec->args[0] >= indio_dev->num_channels) {
137 dev_err(&indio_dev->dev, "invalid channel index %llu\n",
138 iiospec->args[0]);
139 return -EINVAL;
140 }
141
142 return iiospec->args[0];
143}
144
145static int __fwnode_iio_channel_get(struct iio_channel *channel,
146 struct fwnode_handle *fwnode, int index)
147{
148 struct fwnode_reference_args iiospec;
149 struct device *idev;
150 struct iio_dev *indio_dev;
151 int err;
152
153 err = fwnode_property_get_reference_args(fwnode, "io-channels",
154 "#io-channel-cells", 0,
155 index, &iiospec);
156 if (err)
157 return err;
158
159 idev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode);
160 if (!idev) {
161 fwnode_handle_put(iiospec.fwnode);
162 return -EPROBE_DEFER;
163 }
164
165 indio_dev = dev_to_iio_dev(idev);
166 channel->indio_dev = indio_dev;
167 if (indio_dev->info->fwnode_xlate)
168 index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
169 else
170 index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
171 fwnode_handle_put(iiospec.fwnode);
172 if (index < 0)
173 goto err_put;
174 channel->channel = &indio_dev->channels[index];
175
176 return 0;
177
178err_put:
179 iio_device_put(indio_dev);
180 return index;
181}
182
183static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode,
184 int index)
185{
186 struct iio_channel *channel;
187 int err;
188
189 if (index < 0)
190 return ERR_PTR(-EINVAL);
191
192 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
193 if (!channel)
194 return ERR_PTR(-ENOMEM);
195
196 err = __fwnode_iio_channel_get(channel, fwnode, index);
197 if (err)
198 goto err_free_channel;
199
200 return channel;
201
202err_free_channel:
203 kfree(channel);
204 return ERR_PTR(err);
205}
206
207static struct iio_channel *
208__fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, const char *name)
209{
210 struct iio_channel *chan;
211 int index = 0;
212
213 /*
214 * For named iio channels, first look up the name in the
215 * "io-channel-names" property. If it cannot be found, the
216 * index will be an error code, and fwnode_iio_channel_get()
217 * will fail.
218 */
219 if (name)
220 index = fwnode_property_match_string(fwnode, "io-channel-names",
221 name);
222
223 chan = fwnode_iio_channel_get(fwnode, index);
224 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
225 return chan;
226 if (name) {
227 if (index >= 0) {
228 pr_err("ERROR: could not get IIO channel %pfw:%s(%i)\n",
229 fwnode, name, index);
230 /*
231 * In this case, we found 'name' in 'io-channel-names'
232 * but somehow we still fail so that we should not proceed
233 * with any other lookup. Hence, explicitly return -EINVAL
234 * (maybe not the better error code) so that the caller
235 * won't do a system lookup.
236 */
237 return ERR_PTR(-EINVAL);
238 }
239 /*
240 * If index < 0, then fwnode_property_get_reference_args() fails
241 * with -EINVAL or -ENOENT (ACPI case) which is expected. We
242 * should not proceed if we get any other error.
243 */
244 if (PTR_ERR(chan) != -EINVAL && PTR_ERR(chan) != -ENOENT)
245 return chan;
246 } else if (PTR_ERR(chan) != -ENOENT) {
247 /*
248 * if !name, then we should only proceed the lookup if
249 * fwnode_property_get_reference_args() returns -ENOENT.
250 */
251 return chan;
252 }
253
254 /* so we continue the lookup */
255 return ERR_PTR(-ENODEV);
256}
257
258struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode,
259 const char *name)
260{
261 struct fwnode_handle *parent;
262 struct iio_channel *chan;
263
264 /* Walk up the tree of devices looking for a matching iio channel */
265 chan = __fwnode_iio_channel_get_by_name(fwnode, name);
266 if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV)
267 return chan;
268
269 /*
270 * No matching IIO channel found on this node.
271 * If the parent node has a "io-channel-ranges" property,
272 * then we can try one of its channels.
273 */
274 fwnode_for_each_parent_node(fwnode, parent) {
275 if (!fwnode_property_present(parent, "io-channel-ranges")) {
276 fwnode_handle_put(parent);
277 return ERR_PTR(-ENODEV);
278 }
279
280 chan = __fwnode_iio_channel_get_by_name(fwnode, name);
281 if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) {
282 fwnode_handle_put(parent);
283 return chan;
284 }
285 }
286
287 return ERR_PTR(-ENODEV);
288}
289EXPORT_SYMBOL_GPL(fwnode_iio_channel_get_by_name);
290
291static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev)
292{
293 struct fwnode_handle *fwnode = dev_fwnode(dev);
294 struct iio_channel *chans;
295 int i, mapind, nummaps = 0;
296 int ret;
297
298 do {
299 ret = fwnode_property_get_reference_args(fwnode, "io-channels",
300 "#io-channel-cells", 0,
301 nummaps, NULL);
302 if (ret < 0)
303 break;
304 } while (++nummaps);
305
306 if (nummaps == 0)
307 return ERR_PTR(-ENODEV);
308
309 /* NULL terminated array to save passing size */
310 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
311 if (!chans)
312 return ERR_PTR(-ENOMEM);
313
314 /* Search for FW matches */
315 for (mapind = 0; mapind < nummaps; mapind++) {
316 ret = __fwnode_iio_channel_get(&chans[mapind], fwnode, mapind);
317 if (ret)
318 goto error_free_chans;
319 }
320 return chans;
321
322error_free_chans:
323 for (i = 0; i < mapind; i++)
324 iio_device_put(chans[i].indio_dev);
325 kfree(chans);
326 return ERR_PTR(ret);
327}
328
329static struct iio_channel *iio_channel_get_sys(const char *name,
330 const char *channel_name)
331{
332 struct iio_map_internal *c_i = NULL, *c = NULL;
333 struct iio_channel *channel;
334 int err;
335
336 if (!(name || channel_name))
337 return ERR_PTR(-ENODEV);
338
339 /* first find matching entry the channel map */
340 mutex_lock(&iio_map_list_lock);
341 list_for_each_entry(c_i, &iio_map_list, l) {
342 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
343 (channel_name &&
344 strcmp(channel_name, c_i->map->consumer_channel) != 0))
345 continue;
346 c = c_i;
347 iio_device_get(c->indio_dev);
348 break;
349 }
350 mutex_unlock(&iio_map_list_lock);
351 if (!c)
352 return ERR_PTR(-ENODEV);
353
354 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
355 if (!channel) {
356 err = -ENOMEM;
357 goto error_no_mem;
358 }
359
360 channel->indio_dev = c->indio_dev;
361
362 if (c->map->adc_channel_label) {
363 channel->channel =
364 iio_chan_spec_from_name(channel->indio_dev,
365 c->map->adc_channel_label);
366
367 if (!channel->channel) {
368 err = -EINVAL;
369 goto error_no_chan;
370 }
371 }
372
373 return channel;
374
375error_no_chan:
376 kfree(channel);
377error_no_mem:
378 iio_device_put(c->indio_dev);
379 return ERR_PTR(err);
380}
381
382struct iio_channel *iio_channel_get(struct device *dev,
383 const char *channel_name)
384{
385 const char *name = dev ? dev_name(dev) : NULL;
386 struct iio_channel *channel;
387
388 if (dev) {
389 channel = fwnode_iio_channel_get_by_name(dev_fwnode(dev),
390 channel_name);
391 if (!IS_ERR(channel) || PTR_ERR(channel) != -ENODEV)
392 return channel;
393 }
394
395 return iio_channel_get_sys(name, channel_name);
396}
397EXPORT_SYMBOL_GPL(iio_channel_get);
398
399void iio_channel_release(struct iio_channel *channel)
400{
401 if (!channel)
402 return;
403 iio_device_put(channel->indio_dev);
404 kfree(channel);
405}
406EXPORT_SYMBOL_GPL(iio_channel_release);
407
408static void devm_iio_channel_free(void *iio_channel)
409{
410 iio_channel_release(iio_channel);
411}
412
413struct iio_channel *devm_iio_channel_get(struct device *dev,
414 const char *channel_name)
415{
416 struct iio_channel *channel;
417 int ret;
418
419 channel = iio_channel_get(dev, channel_name);
420 if (IS_ERR(channel))
421 return channel;
422
423 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
424 if (ret)
425 return ERR_PTR(ret);
426
427 return channel;
428}
429EXPORT_SYMBOL_GPL(devm_iio_channel_get);
430
431struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev,
432 struct fwnode_handle *fwnode,
433 const char *channel_name)
434{
435 struct iio_channel *channel;
436 int ret;
437
438 channel = fwnode_iio_channel_get_by_name(fwnode, channel_name);
439 if (IS_ERR(channel))
440 return channel;
441
442 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
443 if (ret)
444 return ERR_PTR(ret);
445
446 return channel;
447}
448EXPORT_SYMBOL_GPL(devm_fwnode_iio_channel_get_by_name);
449
450struct iio_channel *iio_channel_get_all(struct device *dev)
451{
452 const char *name;
453 struct iio_channel *chans;
454 struct iio_map_internal *c = NULL;
455 int nummaps = 0;
456 int mapind = 0;
457 int i, ret;
458
459 if (!dev)
460 return ERR_PTR(-EINVAL);
461
462 chans = fwnode_iio_channel_get_all(dev);
463 /*
464 * We only want to carry on if the error is -ENODEV. Anything else
465 * should be reported up the stack.
466 */
467 if (!IS_ERR(chans) || PTR_ERR(chans) != -ENODEV)
468 return chans;
469
470 name = dev_name(dev);
471
472 mutex_lock(&iio_map_list_lock);
473 /* first count the matching maps */
474 list_for_each_entry(c, &iio_map_list, l)
475 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
476 continue;
477 else
478 nummaps++;
479
480 if (nummaps == 0) {
481 ret = -ENODEV;
482 goto error_ret;
483 }
484
485 /* NULL terminated array to save passing size */
486 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
487 if (!chans) {
488 ret = -ENOMEM;
489 goto error_ret;
490 }
491
492 /* for each map fill in the chans element */
493 list_for_each_entry(c, &iio_map_list, l) {
494 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
495 continue;
496 chans[mapind].indio_dev = c->indio_dev;
497 chans[mapind].data = c->map->consumer_data;
498 chans[mapind].channel =
499 iio_chan_spec_from_name(chans[mapind].indio_dev,
500 c->map->adc_channel_label);
501 if (!chans[mapind].channel) {
502 ret = -EINVAL;
503 goto error_free_chans;
504 }
505 iio_device_get(chans[mapind].indio_dev);
506 mapind++;
507 }
508 if (mapind == 0) {
509 ret = -ENODEV;
510 goto error_free_chans;
511 }
512 mutex_unlock(&iio_map_list_lock);
513
514 return chans;
515
516error_free_chans:
517 for (i = 0; i < nummaps; i++)
518 iio_device_put(chans[i].indio_dev);
519 kfree(chans);
520error_ret:
521 mutex_unlock(&iio_map_list_lock);
522
523 return ERR_PTR(ret);
524}
525EXPORT_SYMBOL_GPL(iio_channel_get_all);
526
527void iio_channel_release_all(struct iio_channel *channels)
528{
529 struct iio_channel *chan = &channels[0];
530
531 while (chan->indio_dev) {
532 iio_device_put(chan->indio_dev);
533 chan++;
534 }
535 kfree(channels);
536}
537EXPORT_SYMBOL_GPL(iio_channel_release_all);
538
539static void devm_iio_channel_free_all(void *iio_channels)
540{
541 iio_channel_release_all(iio_channels);
542}
543
544struct iio_channel *devm_iio_channel_get_all(struct device *dev)
545{
546 struct iio_channel *channels;
547 int ret;
548
549 channels = iio_channel_get_all(dev);
550 if (IS_ERR(channels))
551 return channels;
552
553 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
554 channels);
555 if (ret)
556 return ERR_PTR(ret);
557
558 return channels;
559}
560EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
561
562static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
563 enum iio_chan_info_enum info)
564{
565 int unused;
566 int vals[INDIO_MAX_RAW_ELEMENTS];
567 int ret;
568 int val_len = 2;
569
570 if (!val2)
571 val2 = &unused;
572
573 if (!iio_channel_has_info(chan->channel, info))
574 return -EINVAL;
575
576 if (chan->indio_dev->info->read_raw_multi) {
577 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
578 chan->channel, INDIO_MAX_RAW_ELEMENTS,
579 vals, &val_len, info);
580 *val = vals[0];
581 *val2 = vals[1];
582 } else {
583 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
584 chan->channel, val, val2, info);
585 }
586
587 return ret;
588}
589
590int iio_read_channel_raw(struct iio_channel *chan, int *val)
591{
592 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
593 int ret;
594
595 mutex_lock(&iio_dev_opaque->info_exist_lock);
596 if (!chan->indio_dev->info) {
597 ret = -ENODEV;
598 goto err_unlock;
599 }
600
601 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
602err_unlock:
603 mutex_unlock(&iio_dev_opaque->info_exist_lock);
604
605 return ret;
606}
607EXPORT_SYMBOL_GPL(iio_read_channel_raw);
608
609int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
610{
611 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
612 int ret;
613
614 mutex_lock(&iio_dev_opaque->info_exist_lock);
615 if (!chan->indio_dev->info) {
616 ret = -ENODEV;
617 goto err_unlock;
618 }
619
620 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
621err_unlock:
622 mutex_unlock(&iio_dev_opaque->info_exist_lock);
623
624 return ret;
625}
626EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
627
628static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
629 int raw, int *processed,
630 unsigned int scale)
631{
632 int scale_type, scale_val, scale_val2;
633 int offset_type, offset_val, offset_val2;
634 s64 raw64 = raw;
635
636 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
637 IIO_CHAN_INFO_OFFSET);
638 if (offset_type >= 0) {
639 switch (offset_type) {
640 case IIO_VAL_INT:
641 break;
642 case IIO_VAL_INT_PLUS_MICRO:
643 case IIO_VAL_INT_PLUS_NANO:
644 /*
645 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
646 * implicitely truncate the offset to it's integer form.
647 */
648 break;
649 case IIO_VAL_FRACTIONAL:
650 offset_val /= offset_val2;
651 break;
652 case IIO_VAL_FRACTIONAL_LOG2:
653 offset_val >>= offset_val2;
654 break;
655 default:
656 return -EINVAL;
657 }
658
659 raw64 += offset_val;
660 }
661
662 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
663 IIO_CHAN_INFO_SCALE);
664 if (scale_type < 0) {
665 /*
666 * If no channel scaling is available apply consumer scale to
667 * raw value and return.
668 */
669 *processed = raw * scale;
670 return 0;
671 }
672
673 switch (scale_type) {
674 case IIO_VAL_INT:
675 *processed = raw64 * scale_val * scale;
676 break;
677 case IIO_VAL_INT_PLUS_MICRO:
678 if (scale_val2 < 0)
679 *processed = -raw64 * scale_val;
680 else
681 *processed = raw64 * scale_val;
682 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
683 1000000LL);
684 break;
685 case IIO_VAL_INT_PLUS_NANO:
686 if (scale_val2 < 0)
687 *processed = -raw64 * scale_val;
688 else
689 *processed = raw64 * scale_val;
690 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
691 1000000000LL);
692 break;
693 case IIO_VAL_FRACTIONAL:
694 *processed = div_s64(raw64 * (s64)scale_val * scale,
695 scale_val2);
696 break;
697 case IIO_VAL_FRACTIONAL_LOG2:
698 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
699 break;
700 default:
701 return -EINVAL;
702 }
703
704 return 0;
705}
706
707int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
708 int *processed, unsigned int scale)
709{
710 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
711 int ret;
712
713 mutex_lock(&iio_dev_opaque->info_exist_lock);
714 if (!chan->indio_dev->info) {
715 ret = -ENODEV;
716 goto err_unlock;
717 }
718
719 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
720 scale);
721err_unlock:
722 mutex_unlock(&iio_dev_opaque->info_exist_lock);
723
724 return ret;
725}
726EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
727
728int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
729 enum iio_chan_info_enum attribute)
730{
731 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
732 int ret;
733
734 mutex_lock(&iio_dev_opaque->info_exist_lock);
735 if (!chan->indio_dev->info) {
736 ret = -ENODEV;
737 goto err_unlock;
738 }
739
740 ret = iio_channel_read(chan, val, val2, attribute);
741err_unlock:
742 mutex_unlock(&iio_dev_opaque->info_exist_lock);
743
744 return ret;
745}
746EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
747
748int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
749{
750 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
751}
752EXPORT_SYMBOL_GPL(iio_read_channel_offset);
753
754int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
755 unsigned int scale)
756{
757 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
758 int ret;
759
760 mutex_lock(&iio_dev_opaque->info_exist_lock);
761 if (!chan->indio_dev->info) {
762 ret = -ENODEV;
763 goto err_unlock;
764 }
765
766 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
767 ret = iio_channel_read(chan, val, NULL,
768 IIO_CHAN_INFO_PROCESSED);
769 if (ret < 0)
770 goto err_unlock;
771 *val *= scale;
772 } else {
773 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
774 if (ret < 0)
775 goto err_unlock;
776 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
777 scale);
778 }
779
780err_unlock:
781 mutex_unlock(&iio_dev_opaque->info_exist_lock);
782
783 return ret;
784}
785EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
786
787int iio_read_channel_processed(struct iio_channel *chan, int *val)
788{
789 /* This is just a special case with scale factor 1 */
790 return iio_read_channel_processed_scale(chan, val, 1);
791}
792EXPORT_SYMBOL_GPL(iio_read_channel_processed);
793
794int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
795{
796 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
797}
798EXPORT_SYMBOL_GPL(iio_read_channel_scale);
799
800static int iio_channel_read_avail(struct iio_channel *chan,
801 const int **vals, int *type, int *length,
802 enum iio_chan_info_enum info)
803{
804 if (!iio_channel_has_available(chan->channel, info))
805 return -EINVAL;
806
807 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
808 vals, type, length, info);
809}
810
811int iio_read_avail_channel_attribute(struct iio_channel *chan,
812 const int **vals, int *type, int *length,
813 enum iio_chan_info_enum attribute)
814{
815 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
816 int ret;
817
818 mutex_lock(&iio_dev_opaque->info_exist_lock);
819 if (!chan->indio_dev->info) {
820 ret = -ENODEV;
821 goto err_unlock;
822 }
823
824 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
825err_unlock:
826 mutex_unlock(&iio_dev_opaque->info_exist_lock);
827
828 return ret;
829}
830EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
831
832int iio_read_avail_channel_raw(struct iio_channel *chan,
833 const int **vals, int *length)
834{
835 int ret;
836 int type;
837
838 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
839 IIO_CHAN_INFO_RAW);
840
841 if (ret >= 0 && type != IIO_VAL_INT)
842 /* raw values are assumed to be IIO_VAL_INT */
843 ret = -EINVAL;
844
845 return ret;
846}
847EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
848
849static int iio_channel_read_max(struct iio_channel *chan,
850 int *val, int *val2, int *type,
851 enum iio_chan_info_enum info)
852{
853 const int *vals;
854 int length;
855 int ret;
856
857 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
858 if (ret < 0)
859 return ret;
860
861 switch (ret) {
862 case IIO_AVAIL_RANGE:
863 switch (*type) {
864 case IIO_VAL_INT:
865 *val = vals[2];
866 break;
867 default:
868 *val = vals[4];
869 if (val2)
870 *val2 = vals[5];
871 }
872 return 0;
873
874 case IIO_AVAIL_LIST:
875 if (length <= 0)
876 return -EINVAL;
877 switch (*type) {
878 case IIO_VAL_INT:
879 *val = max_array(vals, length);
880 break;
881 default:
882 /* TODO: learn about max for other iio values */
883 return -EINVAL;
884 }
885 return 0;
886
887 default:
888 return -EINVAL;
889 }
890}
891
892int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
893{
894 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
895 int ret;
896 int type;
897
898 mutex_lock(&iio_dev_opaque->info_exist_lock);
899 if (!chan->indio_dev->info) {
900 ret = -ENODEV;
901 goto err_unlock;
902 }
903
904 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
905err_unlock:
906 mutex_unlock(&iio_dev_opaque->info_exist_lock);
907
908 return ret;
909}
910EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
911
912static int iio_channel_read_min(struct iio_channel *chan,
913 int *val, int *val2, int *type,
914 enum iio_chan_info_enum info)
915{
916 const int *vals;
917 int length;
918 int ret;
919
920 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
921 if (ret < 0)
922 return ret;
923
924 switch (ret) {
925 case IIO_AVAIL_RANGE:
926 switch (*type) {
927 case IIO_VAL_INT:
928 *val = vals[0];
929 break;
930 default:
931 *val = vals[0];
932 if (val2)
933 *val2 = vals[1];
934 }
935 return 0;
936
937 case IIO_AVAIL_LIST:
938 if (length <= 0)
939 return -EINVAL;
940 switch (*type) {
941 case IIO_VAL_INT:
942 *val = min_array(vals, length);
943 break;
944 default:
945 /* TODO: learn about min for other iio values */
946 return -EINVAL;
947 }
948 return 0;
949
950 default:
951 return -EINVAL;
952 }
953}
954
955int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
956{
957 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
958 int ret;
959 int type;
960
961 mutex_lock(&iio_dev_opaque->info_exist_lock);
962 if (!chan->indio_dev->info) {
963 ret = -ENODEV;
964 goto err_unlock;
965 }
966
967 ret = iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
968err_unlock:
969 mutex_unlock(&iio_dev_opaque->info_exist_lock);
970
971 return ret;
972}
973EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
974
975int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
976{
977 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
978 int ret = 0;
979 /* Need to verify underlying driver has not gone away */
980
981 mutex_lock(&iio_dev_opaque->info_exist_lock);
982 if (!chan->indio_dev->info) {
983 ret = -ENODEV;
984 goto err_unlock;
985 }
986
987 *type = chan->channel->type;
988err_unlock:
989 mutex_unlock(&iio_dev_opaque->info_exist_lock);
990
991 return ret;
992}
993EXPORT_SYMBOL_GPL(iio_get_channel_type);
994
995static int iio_channel_write(struct iio_channel *chan, int val, int val2,
996 enum iio_chan_info_enum info)
997{
998 return chan->indio_dev->info->write_raw(chan->indio_dev,
999 chan->channel, val, val2, info);
1000}
1001
1002int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
1003 enum iio_chan_info_enum attribute)
1004{
1005 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
1006 int ret;
1007
1008 mutex_lock(&iio_dev_opaque->info_exist_lock);
1009 if (!chan->indio_dev->info) {
1010 ret = -ENODEV;
1011 goto err_unlock;
1012 }
1013
1014 ret = iio_channel_write(chan, val, val2, attribute);
1015err_unlock:
1016 mutex_unlock(&iio_dev_opaque->info_exist_lock);
1017
1018 return ret;
1019}
1020EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
1021
1022int iio_write_channel_raw(struct iio_channel *chan, int val)
1023{
1024 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
1025}
1026EXPORT_SYMBOL_GPL(iio_write_channel_raw);
1027
1028unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
1029{
1030 const struct iio_chan_spec_ext_info *ext_info;
1031 unsigned int i = 0;
1032
1033 if (!chan->channel->ext_info)
1034 return i;
1035
1036 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
1037 ++i;
1038
1039 return i;
1040}
1041EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
1042
1043static const struct iio_chan_spec_ext_info *
1044iio_lookup_ext_info(const struct iio_channel *chan, const char *attr)
1045{
1046 const struct iio_chan_spec_ext_info *ext_info;
1047
1048 if (!chan->channel->ext_info)
1049 return NULL;
1050
1051 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
1052 if (!strcmp(attr, ext_info->name))
1053 return ext_info;
1054 }
1055
1056 return NULL;
1057}
1058
1059ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
1060 const char *attr, char *buf)
1061{
1062 const struct iio_chan_spec_ext_info *ext_info;
1063
1064 ext_info = iio_lookup_ext_info(chan, attr);
1065 if (!ext_info)
1066 return -EINVAL;
1067
1068 return ext_info->read(chan->indio_dev, ext_info->private,
1069 chan->channel, buf);
1070}
1071EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
1072
1073ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
1074 const char *buf, size_t len)
1075{
1076 const struct iio_chan_spec_ext_info *ext_info;
1077
1078 ext_info = iio_lookup_ext_info(chan, attr);
1079 if (!ext_info)
1080 return -EINVAL;
1081
1082 return ext_info->write(chan->indio_dev, ext_info->private,
1083 chan->channel, buf, len);
1084}
1085EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);