1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core in kernel channel mapping
4 * Copyright (c) 2011 Jonathan Cameron
7 #include <linux/export.h>
8 #include <linux/slab.h>
9 #include <linux/mutex.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/iio-opaque.h>
15 #include <linux/iio/machine.h>
16 #include <linux/iio/driver.h>
17 #include <linux/iio/consumer.h>
19 struct iio_map_internal {
20 struct iio_dev *indio_dev;
25 static LIST_HEAD(iio_map_list);
26 static DEFINE_MUTEX(iio_map_list_lock);
28 static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
31 struct iio_map_internal *mapi, *next;
33 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
34 if (indio_dev == mapi->indio_dev) {
43 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
46 struct iio_map_internal *mapi;
51 mutex_lock(&iio_map_list_lock);
52 while (maps[i].consumer_dev_name != NULL) {
53 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
59 mapi->indio_dev = indio_dev;
60 list_add_tail(&mapi->l, &iio_map_list);
65 iio_map_array_unregister_locked(indio_dev);
66 mutex_unlock(&iio_map_list_lock);
70 EXPORT_SYMBOL_GPL(iio_map_array_register);
74 * Remove all map entries associated with the given iio device
76 int iio_map_array_unregister(struct iio_dev *indio_dev)
80 mutex_lock(&iio_map_list_lock);
81 ret = iio_map_array_unregister_locked(indio_dev);
82 mutex_unlock(&iio_map_list_lock);
86 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
88 static void iio_map_array_unregister_cb(void *indio_dev)
90 iio_map_array_unregister(indio_dev);
93 int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps)
97 ret = iio_map_array_register(indio_dev, maps);
101 return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
103 EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
105 static const struct iio_chan_spec
106 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
109 const struct iio_chan_spec *chan = NULL;
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];
122 static int iio_dev_node_match(struct device *dev, const void *data)
124 return dev->of_node == data && dev->type == &iio_device_type;
128 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
129 * @indio_dev: pointer to the iio_dev structure
130 * @iiospec: IIO specifier as found in the device tree
132 * This is simple translation function, suitable for the most 1:1 mapped
133 * channels in IIO chips. This function performs only one sanity check:
134 * whether IIO index is less than num_channels (that is specified in the
137 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
138 const struct of_phandle_args *iiospec)
140 if (!iiospec->args_count)
143 if (iiospec->args[0] >= indio_dev->num_channels) {
144 dev_err(&indio_dev->dev, "invalid channel index %u\n",
149 return iiospec->args[0];
152 static int __of_iio_channel_get(struct iio_channel *channel,
153 struct device_node *np, int index)
156 struct iio_dev *indio_dev;
158 struct of_phandle_args iiospec;
160 err = of_parse_phandle_with_args(np, "io-channels",
166 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
168 of_node_put(iiospec.np);
170 return -EPROBE_DEFER;
172 indio_dev = dev_to_iio_dev(idev);
173 channel->indio_dev = indio_dev;
174 if (indio_dev->info->of_xlate)
175 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
177 index = __of_iio_simple_xlate(indio_dev, &iiospec);
180 channel->channel = &indio_dev->channels[index];
185 iio_device_put(indio_dev);
189 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
191 struct iio_channel *channel;
195 return ERR_PTR(-EINVAL);
197 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
199 return ERR_PTR(-ENOMEM);
201 err = __of_iio_channel_get(channel, np, index);
203 goto err_free_channel;
212 struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
215 struct iio_channel *chan = NULL;
217 /* Walk up the tree of devices looking for a matching iio channel */
222 * For named iio channels, first look up the name in the
223 * "io-channel-names" property. If it cannot be found, the
224 * index will be an error code, and of_iio_channel_get()
228 index = of_property_match_string(np, "io-channel-names",
230 chan = of_iio_channel_get(np, index);
231 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
233 else if (name && index >= 0) {
234 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
235 np, name ? name : "", index);
240 * No matching IIO channel found on this node.
241 * If the parent node has a "io-channel-ranges" property,
242 * then we can try one of its channels.
245 if (np && !of_get_property(np, "io-channel-ranges", NULL))
251 EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name);
253 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
255 struct iio_channel *chans;
256 int i, mapind, nummaps = 0;
260 ret = of_parse_phandle_with_args(dev->of_node,
268 if (nummaps == 0) /* no error, return NULL to search map table */
271 /* NULL terminated array to save passing size */
272 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
274 return ERR_PTR(-ENOMEM);
276 /* Search for OF matches */
277 for (mapind = 0; mapind < nummaps; mapind++) {
278 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
281 goto error_free_chans;
286 for (i = 0; i < mapind; i++)
287 iio_device_put(chans[i].indio_dev);
292 #else /* CONFIG_OF */
294 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
299 #endif /* CONFIG_OF */
301 static struct iio_channel *iio_channel_get_sys(const char *name,
302 const char *channel_name)
304 struct iio_map_internal *c_i = NULL, *c = NULL;
305 struct iio_channel *channel;
308 if (name == NULL && channel_name == NULL)
309 return ERR_PTR(-ENODEV);
311 /* first find matching entry the channel map */
312 mutex_lock(&iio_map_list_lock);
313 list_for_each_entry(c_i, &iio_map_list, l) {
314 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
316 strcmp(channel_name, c_i->map->consumer_channel) != 0))
319 iio_device_get(c->indio_dev);
322 mutex_unlock(&iio_map_list_lock);
324 return ERR_PTR(-ENODEV);
326 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
327 if (channel == NULL) {
332 channel->indio_dev = c->indio_dev;
334 if (c->map->adc_channel_label) {
336 iio_chan_spec_from_name(channel->indio_dev,
337 c->map->adc_channel_label);
339 if (channel->channel == NULL) {
350 iio_device_put(c->indio_dev);
354 struct iio_channel *iio_channel_get(struct device *dev,
355 const char *channel_name)
357 const char *name = dev ? dev_name(dev) : NULL;
358 struct iio_channel *channel;
361 channel = of_iio_channel_get_by_name(dev->of_node,
367 return iio_channel_get_sys(name, channel_name);
369 EXPORT_SYMBOL_GPL(iio_channel_get);
371 void iio_channel_release(struct iio_channel *channel)
375 iio_device_put(channel->indio_dev);
378 EXPORT_SYMBOL_GPL(iio_channel_release);
380 static void devm_iio_channel_free(void *iio_channel)
382 iio_channel_release(iio_channel);
385 struct iio_channel *devm_iio_channel_get(struct device *dev,
386 const char *channel_name)
388 struct iio_channel *channel;
391 channel = iio_channel_get(dev, channel_name);
395 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
401 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
403 struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
404 struct device_node *np,
405 const char *channel_name)
407 struct iio_channel *channel;
410 channel = of_iio_channel_get_by_name(np, channel_name);
414 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
420 EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name);
422 struct iio_channel *iio_channel_get_all(struct device *dev)
425 struct iio_channel *chans;
426 struct iio_map_internal *c = NULL;
432 return ERR_PTR(-EINVAL);
434 chans = of_iio_channel_get_all(dev);
438 name = dev_name(dev);
440 mutex_lock(&iio_map_list_lock);
441 /* first count the matching maps */
442 list_for_each_entry(c, &iio_map_list, l)
443 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
453 /* NULL terminated array to save passing size */
454 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
460 /* for each map fill in the chans element */
461 list_for_each_entry(c, &iio_map_list, l) {
462 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
464 chans[mapind].indio_dev = c->indio_dev;
465 chans[mapind].data = c->map->consumer_data;
466 chans[mapind].channel =
467 iio_chan_spec_from_name(chans[mapind].indio_dev,
468 c->map->adc_channel_label);
469 if (chans[mapind].channel == NULL) {
471 goto error_free_chans;
473 iio_device_get(chans[mapind].indio_dev);
478 goto error_free_chans;
480 mutex_unlock(&iio_map_list_lock);
485 for (i = 0; i < nummaps; i++)
486 iio_device_put(chans[i].indio_dev);
489 mutex_unlock(&iio_map_list_lock);
493 EXPORT_SYMBOL_GPL(iio_channel_get_all);
495 void iio_channel_release_all(struct iio_channel *channels)
497 struct iio_channel *chan = &channels[0];
499 while (chan->indio_dev) {
500 iio_device_put(chan->indio_dev);
505 EXPORT_SYMBOL_GPL(iio_channel_release_all);
507 static void devm_iio_channel_free_all(void *iio_channels)
509 iio_channel_release_all(iio_channels);
512 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
514 struct iio_channel *channels;
517 channels = iio_channel_get_all(dev);
518 if (IS_ERR(channels))
521 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
528 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
530 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
531 enum iio_chan_info_enum info)
534 int vals[INDIO_MAX_RAW_ELEMENTS];
541 if (!iio_channel_has_info(chan->channel, info))
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);
551 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
552 chan->channel, val, val2, info);
557 int iio_read_channel_raw(struct iio_channel *chan, int *val)
559 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
562 mutex_lock(&iio_dev_opaque->info_exist_lock);
563 if (chan->indio_dev->info == NULL) {
568 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
570 mutex_unlock(&iio_dev_opaque->info_exist_lock);
574 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
576 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
578 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
581 mutex_lock(&iio_dev_opaque->info_exist_lock);
582 if (chan->indio_dev->info == NULL) {
587 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
589 mutex_unlock(&iio_dev_opaque->info_exist_lock);
593 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
595 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
596 int raw, int *processed, unsigned int scale)
598 int scale_type, scale_val, scale_val2;
599 int offset_type, offset_val, offset_val2;
602 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
603 IIO_CHAN_INFO_OFFSET);
604 if (offset_type >= 0) {
605 switch (offset_type) {
608 case IIO_VAL_INT_PLUS_MICRO:
609 case IIO_VAL_INT_PLUS_NANO:
611 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
612 * implicitely truncate the offset to it's integer form.
615 case IIO_VAL_FRACTIONAL:
616 offset_val /= offset_val2;
618 case IIO_VAL_FRACTIONAL_LOG2:
619 offset_val >>= offset_val2;
628 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
629 IIO_CHAN_INFO_SCALE);
630 if (scale_type < 0) {
632 * If no channel scaling is available apply consumer scale to
633 * raw value and return.
635 *processed = raw * scale;
639 switch (scale_type) {
641 *processed = raw64 * scale_val * scale;
643 case IIO_VAL_INT_PLUS_MICRO:
645 *processed = -raw64 * scale_val;
647 *processed = raw64 * scale_val;
648 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
651 case IIO_VAL_INT_PLUS_NANO:
653 *processed = -raw64 * scale_val;
655 *processed = raw64 * scale_val;
656 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
659 case IIO_VAL_FRACTIONAL:
660 *processed = div_s64(raw64 * (s64)scale_val * scale,
663 case IIO_VAL_FRACTIONAL_LOG2:
664 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
673 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
674 int *processed, unsigned int scale)
676 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
679 mutex_lock(&iio_dev_opaque->info_exist_lock);
680 if (chan->indio_dev->info == NULL) {
685 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
688 mutex_unlock(&iio_dev_opaque->info_exist_lock);
692 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
694 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
695 enum iio_chan_info_enum attribute)
697 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
700 mutex_lock(&iio_dev_opaque->info_exist_lock);
701 if (chan->indio_dev->info == NULL) {
706 ret = iio_channel_read(chan, val, val2, attribute);
708 mutex_unlock(&iio_dev_opaque->info_exist_lock);
712 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
714 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
716 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
718 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
720 int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
723 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
726 mutex_lock(&iio_dev_opaque->info_exist_lock);
727 if (chan->indio_dev->info == NULL) {
732 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
733 ret = iio_channel_read(chan, val, NULL,
734 IIO_CHAN_INFO_PROCESSED);
739 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
742 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
747 mutex_unlock(&iio_dev_opaque->info_exist_lock);
751 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
753 int iio_read_channel_processed(struct iio_channel *chan, int *val)
755 /* This is just a special case with scale factor 1 */
756 return iio_read_channel_processed_scale(chan, val, 1);
758 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
760 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
762 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
764 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
766 static int iio_channel_read_avail(struct iio_channel *chan,
767 const int **vals, int *type, int *length,
768 enum iio_chan_info_enum info)
770 if (!iio_channel_has_available(chan->channel, info))
773 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
774 vals, type, length, info);
777 int iio_read_avail_channel_attribute(struct iio_channel *chan,
778 const int **vals, int *type, int *length,
779 enum iio_chan_info_enum attribute)
781 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
784 mutex_lock(&iio_dev_opaque->info_exist_lock);
785 if (!chan->indio_dev->info) {
790 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
792 mutex_unlock(&iio_dev_opaque->info_exist_lock);
796 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
798 int iio_read_avail_channel_raw(struct iio_channel *chan,
799 const int **vals, int *length)
804 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
807 if (ret >= 0 && type != IIO_VAL_INT)
808 /* raw values are assumed to be IIO_VAL_INT */
813 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
815 static int iio_channel_read_max(struct iio_channel *chan,
816 int *val, int *val2, int *type,
817 enum iio_chan_info_enum info)
827 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
829 case IIO_AVAIL_RANGE:
845 *val = vals[--length];
847 if (vals[--length] > *val)
852 /* FIXME: learn about max for other iio values */
862 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
864 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
868 mutex_lock(&iio_dev_opaque->info_exist_lock);
869 if (!chan->indio_dev->info) {
874 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
876 mutex_unlock(&iio_dev_opaque->info_exist_lock);
880 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
882 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
884 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
886 /* Need to verify underlying driver has not gone away */
888 mutex_lock(&iio_dev_opaque->info_exist_lock);
889 if (chan->indio_dev->info == NULL) {
894 *type = chan->channel->type;
896 mutex_unlock(&iio_dev_opaque->info_exist_lock);
900 EXPORT_SYMBOL_GPL(iio_get_channel_type);
902 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
903 enum iio_chan_info_enum info)
905 return chan->indio_dev->info->write_raw(chan->indio_dev,
906 chan->channel, val, val2, info);
909 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
910 enum iio_chan_info_enum attribute)
912 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
915 mutex_lock(&iio_dev_opaque->info_exist_lock);
916 if (chan->indio_dev->info == NULL) {
921 ret = iio_channel_write(chan, val, val2, attribute);
923 mutex_unlock(&iio_dev_opaque->info_exist_lock);
927 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
929 int iio_write_channel_raw(struct iio_channel *chan, int val)
931 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
933 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
935 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
937 const struct iio_chan_spec_ext_info *ext_info;
940 if (!chan->channel->ext_info)
943 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
948 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
950 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
951 const struct iio_channel *chan,
954 const struct iio_chan_spec_ext_info *ext_info;
956 if (!chan->channel->ext_info)
959 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
960 if (!strcmp(attr, ext_info->name))
967 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
968 const char *attr, char *buf)
970 const struct iio_chan_spec_ext_info *ext_info;
972 ext_info = iio_lookup_ext_info(chan, attr);
976 return ext_info->read(chan->indio_dev, ext_info->private,
979 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
981 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
982 const char *buf, size_t len)
984 const struct iio_chan_spec_ext_info *ext_info;
986 ext_info = iio_lookup_ext_info(chan, attr);
990 return ext_info->write(chan->indio_dev, ext_info->private,
991 chan->channel, buf, len);
993 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);