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 const struct iio_chan_spec
89 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
92 const struct iio_chan_spec *chan = NULL;
94 for (i = 0; i < indio_dev->num_channels; i++)
95 if (indio_dev->channels[i].datasheet_name &&
96 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
97 chan = &indio_dev->channels[i];
105 static int iio_dev_node_match(struct device *dev, const void *data)
107 return dev->of_node == data && dev->type == &iio_device_type;
111 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
112 * @indio_dev: pointer to the iio_dev structure
113 * @iiospec: IIO specifier as found in the device tree
115 * This is simple translation function, suitable for the most 1:1 mapped
116 * channels in IIO chips. This function performs only one sanity check:
117 * whether IIO index is less than num_channels (that is specified in the
120 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
121 const struct of_phandle_args *iiospec)
123 if (!iiospec->args_count)
126 if (iiospec->args[0] >= indio_dev->num_channels) {
127 dev_err(&indio_dev->dev, "invalid channel index %u\n",
132 return iiospec->args[0];
135 static int __of_iio_channel_get(struct iio_channel *channel,
136 struct device_node *np, int index)
139 struct iio_dev *indio_dev;
141 struct of_phandle_args iiospec;
143 err = of_parse_phandle_with_args(np, "io-channels",
149 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
151 of_node_put(iiospec.np);
153 return -EPROBE_DEFER;
155 indio_dev = dev_to_iio_dev(idev);
156 channel->indio_dev = indio_dev;
157 if (indio_dev->info->of_xlate)
158 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
160 index = __of_iio_simple_xlate(indio_dev, &iiospec);
163 channel->channel = &indio_dev->channels[index];
168 iio_device_put(indio_dev);
172 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
174 struct iio_channel *channel;
178 return ERR_PTR(-EINVAL);
180 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
184 err = __of_iio_channel_get(channel, np, index);
186 goto err_free_channel;
195 struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
198 struct iio_channel *chan = NULL;
200 /* Walk up the tree of devices looking for a matching iio channel */
205 * For named iio channels, first look up the name in the
206 * "io-channel-names" property. If it cannot be found, the
207 * index will be an error code, and of_iio_channel_get()
211 index = of_property_match_string(np, "io-channel-names",
213 chan = of_iio_channel_get(np, index);
214 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
216 else if (name && index >= 0) {
217 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
218 np, name ? name : "", index);
223 * No matching IIO channel found on this node.
224 * If the parent node has a "io-channel-ranges" property,
225 * then we can try one of its channels.
228 if (np && !of_get_property(np, "io-channel-ranges", NULL))
234 EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name);
236 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
238 struct iio_channel *chans;
239 int i, mapind, nummaps = 0;
243 ret = of_parse_phandle_with_args(dev->of_node,
251 if (nummaps == 0) /* no error, return NULL to search map table */
254 /* NULL terminated array to save passing size */
255 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
257 return ERR_PTR(-ENOMEM);
259 /* Search for OF matches */
260 for (mapind = 0; mapind < nummaps; mapind++) {
261 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
264 goto error_free_chans;
269 for (i = 0; i < mapind; i++)
270 iio_device_put(chans[i].indio_dev);
275 #else /* CONFIG_OF */
277 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
282 #endif /* CONFIG_OF */
284 static struct iio_channel *iio_channel_get_sys(const char *name,
285 const char *channel_name)
287 struct iio_map_internal *c_i = NULL, *c = NULL;
288 struct iio_channel *channel;
291 if (name == NULL && channel_name == NULL)
292 return ERR_PTR(-ENODEV);
294 /* first find matching entry the channel map */
295 mutex_lock(&iio_map_list_lock);
296 list_for_each_entry(c_i, &iio_map_list, l) {
297 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
299 strcmp(channel_name, c_i->map->consumer_channel) != 0))
302 iio_device_get(c->indio_dev);
305 mutex_unlock(&iio_map_list_lock);
307 return ERR_PTR(-ENODEV);
309 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
310 if (channel == NULL) {
315 channel->indio_dev = c->indio_dev;
317 if (c->map->adc_channel_label) {
319 iio_chan_spec_from_name(channel->indio_dev,
320 c->map->adc_channel_label);
322 if (channel->channel == NULL) {
333 iio_device_put(c->indio_dev);
337 struct iio_channel *iio_channel_get(struct device *dev,
338 const char *channel_name)
340 const char *name = dev ? dev_name(dev) : NULL;
341 struct iio_channel *channel;
344 channel = of_iio_channel_get_by_name(dev->of_node,
350 return iio_channel_get_sys(name, channel_name);
352 EXPORT_SYMBOL_GPL(iio_channel_get);
354 void iio_channel_release(struct iio_channel *channel)
358 iio_device_put(channel->indio_dev);
361 EXPORT_SYMBOL_GPL(iio_channel_release);
363 static void devm_iio_channel_free(void *iio_channel)
365 iio_channel_release(iio_channel);
368 struct iio_channel *devm_iio_channel_get(struct device *dev,
369 const char *channel_name)
371 struct iio_channel *channel;
374 channel = iio_channel_get(dev, channel_name);
378 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
384 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
386 struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
387 struct device_node *np,
388 const char *channel_name)
390 struct iio_channel *channel;
393 channel = of_iio_channel_get_by_name(np, channel_name);
397 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
403 EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name);
405 struct iio_channel *iio_channel_get_all(struct device *dev)
408 struct iio_channel *chans;
409 struct iio_map_internal *c = NULL;
415 return ERR_PTR(-EINVAL);
417 chans = of_iio_channel_get_all(dev);
421 name = dev_name(dev);
423 mutex_lock(&iio_map_list_lock);
424 /* first count the matching maps */
425 list_for_each_entry(c, &iio_map_list, l)
426 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
436 /* NULL terminated array to save passing size */
437 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
443 /* for each map fill in the chans element */
444 list_for_each_entry(c, &iio_map_list, l) {
445 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
447 chans[mapind].indio_dev = c->indio_dev;
448 chans[mapind].data = c->map->consumer_data;
449 chans[mapind].channel =
450 iio_chan_spec_from_name(chans[mapind].indio_dev,
451 c->map->adc_channel_label);
452 if (chans[mapind].channel == NULL) {
454 goto error_free_chans;
456 iio_device_get(chans[mapind].indio_dev);
461 goto error_free_chans;
463 mutex_unlock(&iio_map_list_lock);
468 for (i = 0; i < nummaps; i++)
469 iio_device_put(chans[i].indio_dev);
472 mutex_unlock(&iio_map_list_lock);
476 EXPORT_SYMBOL_GPL(iio_channel_get_all);
478 void iio_channel_release_all(struct iio_channel *channels)
480 struct iio_channel *chan = &channels[0];
482 while (chan->indio_dev) {
483 iio_device_put(chan->indio_dev);
488 EXPORT_SYMBOL_GPL(iio_channel_release_all);
490 static void devm_iio_channel_free_all(void *iio_channels)
492 iio_channel_release_all(iio_channels);
495 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
497 struct iio_channel *channels;
500 channels = iio_channel_get_all(dev);
501 if (IS_ERR(channels))
504 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
511 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
513 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
514 enum iio_chan_info_enum info)
517 int vals[INDIO_MAX_RAW_ELEMENTS];
524 if (!iio_channel_has_info(chan->channel, info))
527 if (chan->indio_dev->info->read_raw_multi) {
528 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
529 chan->channel, INDIO_MAX_RAW_ELEMENTS,
530 vals, &val_len, info);
534 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
535 chan->channel, val, val2, info);
540 int iio_read_channel_raw(struct iio_channel *chan, int *val)
542 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
545 mutex_lock(&iio_dev_opaque->info_exist_lock);
546 if (chan->indio_dev->info == NULL) {
551 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
553 mutex_unlock(&iio_dev_opaque->info_exist_lock);
557 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
559 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
561 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
564 mutex_lock(&iio_dev_opaque->info_exist_lock);
565 if (chan->indio_dev->info == NULL) {
570 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
572 mutex_unlock(&iio_dev_opaque->info_exist_lock);
576 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
578 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
579 int raw, int *processed, unsigned int scale)
581 int scale_type, scale_val, scale_val2;
582 int offset_type, offset_val, offset_val2;
585 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
586 IIO_CHAN_INFO_OFFSET);
587 if (offset_type >= 0) {
588 switch (offset_type) {
591 case IIO_VAL_INT_PLUS_MICRO:
592 case IIO_VAL_INT_PLUS_NANO:
594 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
595 * implicitely truncate the offset to it's integer form.
598 case IIO_VAL_FRACTIONAL:
599 offset_val /= offset_val2;
601 case IIO_VAL_FRACTIONAL_LOG2:
602 offset_val >>= offset_val2;
611 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
612 IIO_CHAN_INFO_SCALE);
613 if (scale_type < 0) {
615 * If no channel scaling is available apply consumer scale to
616 * raw value and return.
618 *processed = raw * scale;
622 switch (scale_type) {
624 *processed = raw64 * scale_val * scale;
626 case IIO_VAL_INT_PLUS_MICRO:
628 *processed = -raw64 * scale_val;
630 *processed = raw64 * scale_val;
631 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
634 case IIO_VAL_INT_PLUS_NANO:
636 *processed = -raw64 * scale_val;
638 *processed = raw64 * scale_val;
639 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
642 case IIO_VAL_FRACTIONAL:
643 *processed = div_s64(raw64 * (s64)scale_val * scale,
646 case IIO_VAL_FRACTIONAL_LOG2:
647 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
656 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
657 int *processed, unsigned int scale)
659 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
662 mutex_lock(&iio_dev_opaque->info_exist_lock);
663 if (chan->indio_dev->info == NULL) {
668 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
671 mutex_unlock(&iio_dev_opaque->info_exist_lock);
675 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
677 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
678 enum iio_chan_info_enum attribute)
680 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
683 mutex_lock(&iio_dev_opaque->info_exist_lock);
684 if (chan->indio_dev->info == NULL) {
689 ret = iio_channel_read(chan, val, val2, attribute);
691 mutex_unlock(&iio_dev_opaque->info_exist_lock);
695 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
697 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
699 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
701 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
703 int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
706 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
709 mutex_lock(&iio_dev_opaque->info_exist_lock);
710 if (chan->indio_dev->info == NULL) {
715 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
716 ret = iio_channel_read(chan, val, NULL,
717 IIO_CHAN_INFO_PROCESSED);
722 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
725 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
730 mutex_unlock(&iio_dev_opaque->info_exist_lock);
734 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
736 int iio_read_channel_processed(struct iio_channel *chan, int *val)
738 /* This is just a special case with scale factor 1 */
739 return iio_read_channel_processed_scale(chan, val, 1);
741 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
743 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
745 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
747 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
749 static int iio_channel_read_avail(struct iio_channel *chan,
750 const int **vals, int *type, int *length,
751 enum iio_chan_info_enum info)
753 if (!iio_channel_has_available(chan->channel, info))
756 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
757 vals, type, length, info);
760 int iio_read_avail_channel_attribute(struct iio_channel *chan,
761 const int **vals, int *type, int *length,
762 enum iio_chan_info_enum attribute)
764 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
767 mutex_lock(&iio_dev_opaque->info_exist_lock);
768 if (!chan->indio_dev->info) {
773 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
775 mutex_unlock(&iio_dev_opaque->info_exist_lock);
779 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
781 int iio_read_avail_channel_raw(struct iio_channel *chan,
782 const int **vals, int *length)
787 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
790 if (ret >= 0 && type != IIO_VAL_INT)
791 /* raw values are assumed to be IIO_VAL_INT */
796 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
798 static int iio_channel_read_max(struct iio_channel *chan,
799 int *val, int *val2, int *type,
800 enum iio_chan_info_enum info)
810 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
812 case IIO_AVAIL_RANGE:
828 *val = vals[--length];
830 if (vals[--length] > *val)
835 /* FIXME: learn about max for other iio values */
845 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
847 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
851 mutex_lock(&iio_dev_opaque->info_exist_lock);
852 if (!chan->indio_dev->info) {
857 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
859 mutex_unlock(&iio_dev_opaque->info_exist_lock);
863 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
865 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
867 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
869 /* Need to verify underlying driver has not gone away */
871 mutex_lock(&iio_dev_opaque->info_exist_lock);
872 if (chan->indio_dev->info == NULL) {
877 *type = chan->channel->type;
879 mutex_unlock(&iio_dev_opaque->info_exist_lock);
883 EXPORT_SYMBOL_GPL(iio_get_channel_type);
885 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
886 enum iio_chan_info_enum info)
888 return chan->indio_dev->info->write_raw(chan->indio_dev,
889 chan->channel, val, val2, info);
892 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
893 enum iio_chan_info_enum attribute)
895 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
898 mutex_lock(&iio_dev_opaque->info_exist_lock);
899 if (chan->indio_dev->info == NULL) {
904 ret = iio_channel_write(chan, val, val2, attribute);
906 mutex_unlock(&iio_dev_opaque->info_exist_lock);
910 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
912 int iio_write_channel_raw(struct iio_channel *chan, int val)
914 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
916 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
918 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
920 const struct iio_chan_spec_ext_info *ext_info;
923 if (!chan->channel->ext_info)
926 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
931 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
933 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
934 const struct iio_channel *chan,
937 const struct iio_chan_spec_ext_info *ext_info;
939 if (!chan->channel->ext_info)
942 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
943 if (!strcmp(attr, ext_info->name))
950 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
951 const char *attr, char *buf)
953 const struct iio_chan_spec_ext_info *ext_info;
955 ext_info = iio_lookup_ext_info(chan, attr);
959 return ext_info->read(chan->indio_dev, ext_info->private,
962 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
964 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
965 const char *buf, size_t len)
967 const struct iio_chan_spec_ext_info *ext_info;
969 ext_info = iio_lookup_ext_info(chan, attr);
973 return ext_info->write(chan->indio_dev, ext_info->private,
974 chan->channel, buf, len);
976 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);