dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / inkern.c
blob5a8351c9a426591b9374445b4cc1182aeed2c2f9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core in kernel channel mapping
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>
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>
18 struct iio_map_internal {
19 struct iio_dev *indio_dev;
20 struct iio_map *map;
21 struct list_head l;
24 static LIST_HEAD(iio_map_list);
25 static DEFINE_MUTEX(iio_map_list_lock);
27 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
29 int i = 0, ret = 0;
30 struct iio_map_internal *mapi;
32 if (maps == NULL)
33 return 0;
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;
42 mapi->map = &maps[i];
43 mapi->indio_dev = indio_dev;
44 list_add_tail(&mapi->l, &iio_map_list);
45 i++;
47 error_ret:
48 mutex_unlock(&iio_map_list_lock);
50 return ret;
52 EXPORT_SYMBOL_GPL(iio_map_array_register);
56 * Remove all map entries associated with the given iio device
58 int iio_map_array_unregister(struct iio_dev *indio_dev)
60 int ret = -ENODEV;
61 struct iio_map_internal *mapi, *next;
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;
71 mutex_unlock(&iio_map_list_lock);
72 return ret;
74 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
76 static const struct iio_chan_spec
77 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
79 int i;
80 const struct iio_chan_spec *chan = NULL;
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;
88 return chan;
91 #ifdef CONFIG_OF
93 static int iio_dev_node_match(struct device *dev, const void *data)
95 return dev->of_node == data && dev->type == &iio_device_type;
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
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).
108 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
109 const struct of_phandle_args *iiospec)
111 if (!iiospec->args_count)
112 return 0;
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;
120 return iiospec->args[0];
123 static int __of_iio_channel_get(struct iio_channel *channel,
124 struct device_node *np, int index)
126 struct device *idev;
127 struct iio_dev *indio_dev;
128 int err;
129 struct of_phandle_args iiospec;
131 err = of_parse_phandle_with_args(np, "io-channels",
132 "#io-channel-cells",
133 index, &iiospec);
134 if (err)
135 return err;
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;
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];
153 return 0;
155 err_put:
156 iio_device_put(indio_dev);
157 return index;
160 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
162 struct iio_channel *channel;
163 int err;
165 if (index < 0)
166 return ERR_PTR(-EINVAL);
168 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
169 if (channel == NULL)
170 return ERR_PTR(-ENOMEM);
172 err = __of_iio_channel_get(channel, np, index);
173 if (err)
174 goto err_free_channel;
176 return channel;
178 err_free_channel:
179 kfree(channel);
180 return ERR_PTR(err);
183 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
184 const char *name)
186 struct iio_channel *chan = NULL;
188 /* Walk up the tree of devices looking for a matching iio channel */
189 while (np) {
190 int index = 0;
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.
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;
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.
215 np = np->parent;
216 if (np && !of_get_property(np, "io-channel-ranges", NULL))
217 return NULL;
220 return chan;
223 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
225 struct iio_channel *chans;
226 int i, mapind, nummaps = 0;
227 int ret;
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);
238 if (nummaps == 0) /* no error, return NULL to search map table */
239 return NULL;
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);
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;
253 return chans;
255 error_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);
262 #else /* CONFIG_OF */
264 static inline struct iio_channel *
265 of_iio_channel_get_by_name(struct device_node *np, const char *name)
267 return NULL;
270 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
272 return NULL;
275 #endif /* CONFIG_OF */
277 static struct iio_channel *iio_channel_get_sys(const char *name,
278 const char *channel_name)
280 struct iio_map_internal *c_i = NULL, *c = NULL;
281 struct iio_channel *channel;
282 int err;
284 if (name == NULL && channel_name == NULL)
285 return ERR_PTR(-ENODEV);
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;
298 mutex_unlock(&iio_map_list_lock);
299 if (c == NULL)
300 return ERR_PTR(-ENODEV);
302 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
303 if (channel == NULL) {
304 err = -ENOMEM;
305 goto error_no_mem;
308 channel->indio_dev = c->indio_dev;
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);
315 if (channel->channel == NULL) {
316 err = -EINVAL;
317 goto error_no_chan;
321 return channel;
323 error_no_chan:
324 kfree(channel);
325 error_no_mem:
326 iio_device_put(c->indio_dev);
327 return ERR_PTR(err);
330 struct iio_channel *iio_channel_get(struct device *dev,
331 const char *channel_name)
333 const char *name = dev ? dev_name(dev) : NULL;
334 struct iio_channel *channel;
336 if (dev) {
337 channel = of_iio_channel_get_by_name(dev->of_node,
338 channel_name);
339 if (channel != NULL)
340 return channel;
343 return iio_channel_get_sys(name, channel_name);
345 EXPORT_SYMBOL_GPL(iio_channel_get);
347 void iio_channel_release(struct iio_channel *channel)
349 if (!channel)
350 return;
351 iio_device_put(channel->indio_dev);
352 kfree(channel);
354 EXPORT_SYMBOL_GPL(iio_channel_release);
356 static void devm_iio_channel_free(struct device *dev, void *res)
358 struct iio_channel *channel = *(struct iio_channel **)res;
360 iio_channel_release(channel);
363 static int devm_iio_channel_match(struct device *dev, void *res, void *data)
365 struct iio_channel **r = res;
367 if (!r || !*r) {
368 WARN_ON(!r || !*r);
369 return 0;
372 return *r == data;
375 struct iio_channel *devm_iio_channel_get(struct device *dev,
376 const char *channel_name)
378 struct iio_channel **ptr, *channel;
380 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
381 if (!ptr)
382 return ERR_PTR(-ENOMEM);
384 channel = iio_channel_get(dev, channel_name);
385 if (IS_ERR(channel)) {
386 devres_free(ptr);
387 return channel;
390 *ptr = channel;
391 devres_add(dev, ptr);
393 return channel;
395 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
397 void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
399 WARN_ON(devres_release(dev, devm_iio_channel_free,
400 devm_iio_channel_match, channel));
402 EXPORT_SYMBOL_GPL(devm_iio_channel_release);
404 struct iio_channel *iio_channel_get_all(struct device *dev)
406 const char *name;
407 struct iio_channel *chans;
408 struct iio_map_internal *c = NULL;
409 int nummaps = 0;
410 int mapind = 0;
411 int i, ret;
413 if (dev == NULL)
414 return ERR_PTR(-EINVAL);
416 chans = of_iio_channel_get_all(dev);
417 if (chans)
418 return chans;
420 name = dev_name(dev);
422 mutex_lock(&iio_map_list_lock);
423 /* first count the matching maps */
424 list_for_each_entry(c, &iio_map_list, l)
425 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
426 continue;
427 else
428 nummaps++;
430 if (nummaps == 0) {
431 ret = -ENODEV;
432 goto error_ret;
435 /* NULL terminated array to save passing size */
436 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
437 if (chans == NULL) {
438 ret = -ENOMEM;
439 goto error_ret;
442 /* for each map fill in the chans element */
443 list_for_each_entry(c, &iio_map_list, l) {
444 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
445 continue;
446 chans[mapind].indio_dev = c->indio_dev;
447 chans[mapind].data = c->map->consumer_data;
448 chans[mapind].channel =
449 iio_chan_spec_from_name(chans[mapind].indio_dev,
450 c->map->adc_channel_label);
451 if (chans[mapind].channel == NULL) {
452 ret = -EINVAL;
453 goto error_free_chans;
455 iio_device_get(chans[mapind].indio_dev);
456 mapind++;
458 if (mapind == 0) {
459 ret = -ENODEV;
460 goto error_free_chans;
462 mutex_unlock(&iio_map_list_lock);
464 return chans;
466 error_free_chans:
467 for (i = 0; i < nummaps; i++)
468 iio_device_put(chans[i].indio_dev);
469 kfree(chans);
470 error_ret:
471 mutex_unlock(&iio_map_list_lock);
473 return ERR_PTR(ret);
475 EXPORT_SYMBOL_GPL(iio_channel_get_all);
477 void iio_channel_release_all(struct iio_channel *channels)
479 struct iio_channel *chan = &channels[0];
481 while (chan->indio_dev) {
482 iio_device_put(chan->indio_dev);
483 chan++;
485 kfree(channels);
487 EXPORT_SYMBOL_GPL(iio_channel_release_all);
489 static void devm_iio_channel_free_all(struct device *dev, void *res)
491 struct iio_channel *channels = *(struct iio_channel **)res;
493 iio_channel_release_all(channels);
496 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
498 struct iio_channel **ptr, *channels;
500 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
501 if (!ptr)
502 return ERR_PTR(-ENOMEM);
504 channels = iio_channel_get_all(dev);
505 if (IS_ERR(channels)) {
506 devres_free(ptr);
507 return channels;
510 *ptr = channels;
511 devres_add(dev, ptr);
513 return channels;
515 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
517 void devm_iio_channel_release_all(struct device *dev,
518 struct iio_channel *channels)
520 WARN_ON(devres_release(dev, devm_iio_channel_free_all,
521 devm_iio_channel_match, channels));
523 EXPORT_SYMBOL_GPL(devm_iio_channel_release_all);
525 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
526 enum iio_chan_info_enum info)
528 int unused;
529 int vals[INDIO_MAX_RAW_ELEMENTS];
530 int ret;
531 int val_len = 2;
533 if (val2 == NULL)
534 val2 = &unused;
536 if (!iio_channel_has_info(chan->channel, info))
537 return -EINVAL;
539 if (chan->indio_dev->info->read_raw_multi) {
540 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
541 chan->channel, INDIO_MAX_RAW_ELEMENTS,
542 vals, &val_len, info);
543 *val = vals[0];
544 *val2 = vals[1];
545 } else
546 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
547 chan->channel, val, val2, info);
549 return ret;
552 int iio_read_channel_raw(struct iio_channel *chan, int *val)
554 int ret;
556 mutex_lock(&chan->indio_dev->info_exist_lock);
557 if (chan->indio_dev->info == NULL) {
558 ret = -ENODEV;
559 goto err_unlock;
562 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
563 err_unlock:
564 mutex_unlock(&chan->indio_dev->info_exist_lock);
566 return ret;
568 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
570 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
572 int ret;
574 mutex_lock(&chan->indio_dev->info_exist_lock);
575 if (chan->indio_dev->info == NULL) {
576 ret = -ENODEV;
577 goto err_unlock;
580 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
581 err_unlock:
582 mutex_unlock(&chan->indio_dev->info_exist_lock);
584 return ret;
586 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
588 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
589 int raw, int *processed, unsigned int scale)
591 int scale_type, scale_val, scale_val2, offset;
592 s64 raw64 = raw;
593 int ret;
595 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
596 if (ret >= 0)
597 raw64 += offset;
599 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
600 IIO_CHAN_INFO_SCALE);
601 if (scale_type < 0) {
603 * Just pass raw values as processed if no scaling is
604 * available.
606 *processed = raw;
607 return 0;
610 switch (scale_type) {
611 case IIO_VAL_INT:
612 *processed = raw64 * scale_val;
613 break;
614 case IIO_VAL_INT_PLUS_MICRO:
615 if (scale_val2 < 0)
616 *processed = -raw64 * scale_val;
617 else
618 *processed = raw64 * scale_val;
619 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
620 1000000LL);
621 break;
622 case IIO_VAL_INT_PLUS_NANO:
623 if (scale_val2 < 0)
624 *processed = -raw64 * scale_val;
625 else
626 *processed = raw64 * scale_val;
627 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
628 1000000000LL);
629 break;
630 case IIO_VAL_FRACTIONAL:
631 *processed = div_s64(raw64 * (s64)scale_val * scale,
632 scale_val2);
633 break;
634 case IIO_VAL_FRACTIONAL_LOG2:
635 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
636 break;
637 default:
638 return -EINVAL;
641 return 0;
644 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
645 int *processed, unsigned int scale)
647 int ret;
649 mutex_lock(&chan->indio_dev->info_exist_lock);
650 if (chan->indio_dev->info == NULL) {
651 ret = -ENODEV;
652 goto err_unlock;
655 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
656 scale);
657 err_unlock:
658 mutex_unlock(&chan->indio_dev->info_exist_lock);
660 return ret;
662 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
664 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
665 enum iio_chan_info_enum attribute)
667 int ret;
669 mutex_lock(&chan->indio_dev->info_exist_lock);
670 if (chan->indio_dev->info == NULL) {
671 ret = -ENODEV;
672 goto err_unlock;
675 ret = iio_channel_read(chan, val, val2, attribute);
676 err_unlock:
677 mutex_unlock(&chan->indio_dev->info_exist_lock);
679 return ret;
681 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
683 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
685 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
687 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
689 int iio_read_channel_processed(struct iio_channel *chan, int *val)
691 int ret;
693 mutex_lock(&chan->indio_dev->info_exist_lock);
694 if (chan->indio_dev->info == NULL) {
695 ret = -ENODEV;
696 goto err_unlock;
699 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
700 ret = iio_channel_read(chan, val, NULL,
701 IIO_CHAN_INFO_PROCESSED);
702 } else {
703 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
704 if (ret < 0)
705 goto err_unlock;
706 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
709 err_unlock:
710 mutex_unlock(&chan->indio_dev->info_exist_lock);
712 return ret;
714 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
716 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
718 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
720 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
722 static int iio_channel_read_avail(struct iio_channel *chan,
723 const int **vals, int *type, int *length,
724 enum iio_chan_info_enum info)
726 if (!iio_channel_has_available(chan->channel, info))
727 return -EINVAL;
729 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
730 vals, type, length, info);
733 int iio_read_avail_channel_attribute(struct iio_channel *chan,
734 const int **vals, int *type, int *length,
735 enum iio_chan_info_enum attribute)
737 int ret;
739 mutex_lock(&chan->indio_dev->info_exist_lock);
740 if (!chan->indio_dev->info) {
741 ret = -ENODEV;
742 goto err_unlock;
745 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
746 err_unlock:
747 mutex_unlock(&chan->indio_dev->info_exist_lock);
749 return ret;
751 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
753 int iio_read_avail_channel_raw(struct iio_channel *chan,
754 const int **vals, int *length)
756 int ret;
757 int type;
759 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
760 IIO_CHAN_INFO_RAW);
762 if (ret >= 0 && type != IIO_VAL_INT)
763 /* raw values are assumed to be IIO_VAL_INT */
764 ret = -EINVAL;
766 return ret;
768 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
770 static int iio_channel_read_max(struct iio_channel *chan,
771 int *val, int *val2, int *type,
772 enum iio_chan_info_enum info)
774 int unused;
775 const int *vals;
776 int length;
777 int ret;
779 if (!val2)
780 val2 = &unused;
782 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
783 switch (ret) {
784 case IIO_AVAIL_RANGE:
785 switch (*type) {
786 case IIO_VAL_INT:
787 *val = vals[2];
788 break;
789 default:
790 *val = vals[4];
791 *val2 = vals[5];
793 return 0;
795 case IIO_AVAIL_LIST:
796 if (length <= 0)
797 return -EINVAL;
798 switch (*type) {
799 case IIO_VAL_INT:
800 *val = vals[--length];
801 while (length) {
802 if (vals[--length] > *val)
803 *val = vals[length];
805 break;
806 default:
807 /* FIXME: learn about max for other iio values */
808 return -EINVAL;
810 return 0;
812 default:
813 return ret;
817 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
819 int ret;
820 int type;
822 mutex_lock(&chan->indio_dev->info_exist_lock);
823 if (!chan->indio_dev->info) {
824 ret = -ENODEV;
825 goto err_unlock;
828 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
829 err_unlock:
830 mutex_unlock(&chan->indio_dev->info_exist_lock);
832 return ret;
834 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
836 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
838 int ret = 0;
839 /* Need to verify underlying driver has not gone away */
841 mutex_lock(&chan->indio_dev->info_exist_lock);
842 if (chan->indio_dev->info == NULL) {
843 ret = -ENODEV;
844 goto err_unlock;
847 *type = chan->channel->type;
848 err_unlock:
849 mutex_unlock(&chan->indio_dev->info_exist_lock);
851 return ret;
853 EXPORT_SYMBOL_GPL(iio_get_channel_type);
855 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
856 enum iio_chan_info_enum info)
858 return chan->indio_dev->info->write_raw(chan->indio_dev,
859 chan->channel, val, val2, info);
862 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
863 enum iio_chan_info_enum attribute)
865 int ret;
867 mutex_lock(&chan->indio_dev->info_exist_lock);
868 if (chan->indio_dev->info == NULL) {
869 ret = -ENODEV;
870 goto err_unlock;
873 ret = iio_channel_write(chan, val, val2, attribute);
874 err_unlock:
875 mutex_unlock(&chan->indio_dev->info_exist_lock);
877 return ret;
879 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
881 int iio_write_channel_raw(struct iio_channel *chan, int val)
883 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
885 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
887 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
889 const struct iio_chan_spec_ext_info *ext_info;
890 unsigned int i = 0;
892 if (!chan->channel->ext_info)
893 return i;
895 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
896 ++i;
898 return i;
900 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
902 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
903 const struct iio_channel *chan,
904 const char *attr)
906 const struct iio_chan_spec_ext_info *ext_info;
908 if (!chan->channel->ext_info)
909 return NULL;
911 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
912 if (!strcmp(attr, ext_info->name))
913 return ext_info;
916 return NULL;
919 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
920 const char *attr, char *buf)
922 const struct iio_chan_spec_ext_info *ext_info;
924 ext_info = iio_lookup_ext_info(chan, attr);
925 if (!ext_info)
926 return -EINVAL;
928 return ext_info->read(chan->indio_dev, ext_info->private,
929 chan->channel, buf);
931 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
933 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
934 const char *buf, size_t len)
936 const struct iio_chan_spec_ext_info *ext_info;
938 ext_info = iio_lookup_ext_info(chan, attr);
939 if (!ext_info)
940 return -EINVAL;
942 return ext_info->write(chan->indio_dev, ext_info->private,
943 chan->channel, buf, len);
945 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);