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,
152 of_node_put(iiospec.np);
153 return -EPROBE_DEFER;
156 indio_dev = dev_to_iio_dev(idev);
157 channel->indio_dev = indio_dev;
158 if (indio_dev->info->of_xlate)
159 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
161 index = __of_iio_simple_xlate(indio_dev, &iiospec);
162 of_node_put(iiospec.np);
165 channel->channel = &indio_dev->channels[index];
170 iio_device_put(indio_dev);
174 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
176 struct iio_channel *channel;
180 return ERR_PTR(-EINVAL);
182 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
184 return ERR_PTR(-ENOMEM);
186 err = __of_iio_channel_get(channel, np, index);
188 goto err_free_channel;
197 struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
200 struct iio_channel *chan = NULL;
202 /* Walk up the tree of devices looking for a matching iio channel */
207 * For named iio channels, first look up the name in the
208 * "io-channel-names" property. If it cannot be found, the
209 * index will be an error code, and of_iio_channel_get()
213 index = of_property_match_string(np, "io-channel-names",
215 chan = of_iio_channel_get(np, index);
216 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
218 else if (name && index >= 0) {
219 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
220 np, name ? name : "", index);
225 * No matching IIO channel found on this node.
226 * If the parent node has a "io-channel-ranges" property,
227 * then we can try one of its channels.
230 if (np && !of_get_property(np, "io-channel-ranges", NULL))
236 EXPORT_SYMBOL_GPL(of_iio_channel_get_by_name);
238 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
240 struct iio_channel *chans;
241 int i, mapind, nummaps = 0;
245 ret = of_parse_phandle_with_args(dev->of_node,
253 if (nummaps == 0) /* no error, return NULL to search map table */
256 /* NULL terminated array to save passing size */
257 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
259 return ERR_PTR(-ENOMEM);
261 /* Search for OF matches */
262 for (mapind = 0; mapind < nummaps; mapind++) {
263 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
266 goto error_free_chans;
271 for (i = 0; i < mapind; i++)
272 iio_device_put(chans[i].indio_dev);
277 #else /* CONFIG_OF */
279 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
284 #endif /* CONFIG_OF */
286 static struct iio_channel *iio_channel_get_sys(const char *name,
287 const char *channel_name)
289 struct iio_map_internal *c_i = NULL, *c = NULL;
290 struct iio_channel *channel;
293 if (name == NULL && channel_name == NULL)
294 return ERR_PTR(-ENODEV);
296 /* first find matching entry the channel map */
297 mutex_lock(&iio_map_list_lock);
298 list_for_each_entry(c_i, &iio_map_list, l) {
299 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
301 strcmp(channel_name, c_i->map->consumer_channel) != 0))
304 iio_device_get(c->indio_dev);
307 mutex_unlock(&iio_map_list_lock);
309 return ERR_PTR(-ENODEV);
311 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
312 if (channel == NULL) {
317 channel->indio_dev = c->indio_dev;
319 if (c->map->adc_channel_label) {
321 iio_chan_spec_from_name(channel->indio_dev,
322 c->map->adc_channel_label);
324 if (channel->channel == NULL) {
335 iio_device_put(c->indio_dev);
339 struct iio_channel *iio_channel_get(struct device *dev,
340 const char *channel_name)
342 const char *name = dev ? dev_name(dev) : NULL;
343 struct iio_channel *channel;
346 channel = of_iio_channel_get_by_name(dev->of_node,
352 return iio_channel_get_sys(name, channel_name);
354 EXPORT_SYMBOL_GPL(iio_channel_get);
356 void iio_channel_release(struct iio_channel *channel)
360 iio_device_put(channel->indio_dev);
363 EXPORT_SYMBOL_GPL(iio_channel_release);
365 static void devm_iio_channel_free(void *iio_channel)
367 iio_channel_release(iio_channel);
370 struct iio_channel *devm_iio_channel_get(struct device *dev,
371 const char *channel_name)
373 struct iio_channel *channel;
376 channel = iio_channel_get(dev, channel_name);
380 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
386 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
388 struct iio_channel *devm_of_iio_channel_get_by_name(struct device *dev,
389 struct device_node *np,
390 const char *channel_name)
392 struct iio_channel *channel;
395 channel = of_iio_channel_get_by_name(np, channel_name);
399 return ERR_PTR(-ENODEV);
401 ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
407 EXPORT_SYMBOL_GPL(devm_of_iio_channel_get_by_name);
409 struct iio_channel *iio_channel_get_all(struct device *dev)
412 struct iio_channel *chans;
413 struct iio_map_internal *c = NULL;
419 return ERR_PTR(-EINVAL);
421 chans = of_iio_channel_get_all(dev);
425 name = dev_name(dev);
427 mutex_lock(&iio_map_list_lock);
428 /* first count the matching maps */
429 list_for_each_entry(c, &iio_map_list, l)
430 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
440 /* NULL terminated array to save passing size */
441 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
447 /* for each map fill in the chans element */
448 list_for_each_entry(c, &iio_map_list, l) {
449 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
451 chans[mapind].indio_dev = c->indio_dev;
452 chans[mapind].data = c->map->consumer_data;
453 chans[mapind].channel =
454 iio_chan_spec_from_name(chans[mapind].indio_dev,
455 c->map->adc_channel_label);
456 if (chans[mapind].channel == NULL) {
458 goto error_free_chans;
460 iio_device_get(chans[mapind].indio_dev);
465 goto error_free_chans;
467 mutex_unlock(&iio_map_list_lock);
472 for (i = 0; i < nummaps; i++)
473 iio_device_put(chans[i].indio_dev);
476 mutex_unlock(&iio_map_list_lock);
480 EXPORT_SYMBOL_GPL(iio_channel_get_all);
482 void iio_channel_release_all(struct iio_channel *channels)
484 struct iio_channel *chan = &channels[0];
486 while (chan->indio_dev) {
487 iio_device_put(chan->indio_dev);
492 EXPORT_SYMBOL_GPL(iio_channel_release_all);
494 static void devm_iio_channel_free_all(void *iio_channels)
496 iio_channel_release_all(iio_channels);
499 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
501 struct iio_channel *channels;
504 channels = iio_channel_get_all(dev);
505 if (IS_ERR(channels))
508 ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
515 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
517 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
518 enum iio_chan_info_enum info)
521 int vals[INDIO_MAX_RAW_ELEMENTS];
528 if (!iio_channel_has_info(chan->channel, info))
531 if (chan->indio_dev->info->read_raw_multi) {
532 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
533 chan->channel, INDIO_MAX_RAW_ELEMENTS,
534 vals, &val_len, info);
538 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
539 chan->channel, val, val2, info);
544 int iio_read_channel_raw(struct iio_channel *chan, int *val)
546 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
549 mutex_lock(&iio_dev_opaque->info_exist_lock);
550 if (chan->indio_dev->info == NULL) {
555 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
557 mutex_unlock(&iio_dev_opaque->info_exist_lock);
561 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
563 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
565 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
568 mutex_lock(&iio_dev_opaque->info_exist_lock);
569 if (chan->indio_dev->info == NULL) {
574 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
576 mutex_unlock(&iio_dev_opaque->info_exist_lock);
580 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
582 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
583 int raw, int *processed, unsigned int scale)
585 int scale_type, scale_val, scale_val2;
586 int offset_type, offset_val, offset_val2;
589 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
590 IIO_CHAN_INFO_OFFSET);
591 if (offset_type >= 0) {
592 switch (offset_type) {
595 case IIO_VAL_INT_PLUS_MICRO:
596 case IIO_VAL_INT_PLUS_NANO:
598 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
599 * implicitely truncate the offset to it's integer form.
602 case IIO_VAL_FRACTIONAL:
603 offset_val /= offset_val2;
605 case IIO_VAL_FRACTIONAL_LOG2:
606 offset_val >>= offset_val2;
615 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
616 IIO_CHAN_INFO_SCALE);
617 if (scale_type < 0) {
619 * If no channel scaling is available apply consumer scale to
620 * raw value and return.
622 *processed = raw * scale;
626 switch (scale_type) {
628 *processed = raw64 * scale_val * scale;
630 case IIO_VAL_INT_PLUS_MICRO:
632 *processed = -raw64 * scale_val;
634 *processed = raw64 * scale_val;
635 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
638 case IIO_VAL_INT_PLUS_NANO:
640 *processed = -raw64 * scale_val;
642 *processed = raw64 * scale_val;
643 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
646 case IIO_VAL_FRACTIONAL:
647 *processed = div_s64(raw64 * (s64)scale_val * scale,
650 case IIO_VAL_FRACTIONAL_LOG2:
651 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
660 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
661 int *processed, unsigned int scale)
663 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
666 mutex_lock(&iio_dev_opaque->info_exist_lock);
667 if (chan->indio_dev->info == NULL) {
672 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
675 mutex_unlock(&iio_dev_opaque->info_exist_lock);
679 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
681 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
682 enum iio_chan_info_enum attribute)
684 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
687 mutex_lock(&iio_dev_opaque->info_exist_lock);
688 if (chan->indio_dev->info == NULL) {
693 ret = iio_channel_read(chan, val, val2, attribute);
695 mutex_unlock(&iio_dev_opaque->info_exist_lock);
699 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
701 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
703 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
705 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
707 int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
710 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
713 mutex_lock(&iio_dev_opaque->info_exist_lock);
714 if (chan->indio_dev->info == NULL) {
719 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
720 ret = iio_channel_read(chan, val, NULL,
721 IIO_CHAN_INFO_PROCESSED);
726 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
729 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val,
734 mutex_unlock(&iio_dev_opaque->info_exist_lock);
738 EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
740 int iio_read_channel_processed(struct iio_channel *chan, int *val)
742 /* This is just a special case with scale factor 1 */
743 return iio_read_channel_processed_scale(chan, val, 1);
745 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
747 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
749 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
751 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
753 static int iio_channel_read_avail(struct iio_channel *chan,
754 const int **vals, int *type, int *length,
755 enum iio_chan_info_enum info)
757 if (!iio_channel_has_available(chan->channel, info))
760 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
761 vals, type, length, info);
764 int iio_read_avail_channel_attribute(struct iio_channel *chan,
765 const int **vals, int *type, int *length,
766 enum iio_chan_info_enum attribute)
768 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
771 mutex_lock(&iio_dev_opaque->info_exist_lock);
772 if (!chan->indio_dev->info) {
777 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
779 mutex_unlock(&iio_dev_opaque->info_exist_lock);
783 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
785 int iio_read_avail_channel_raw(struct iio_channel *chan,
786 const int **vals, int *length)
791 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
794 if (ret >= 0 && type != IIO_VAL_INT)
795 /* raw values are assumed to be IIO_VAL_INT */
800 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
802 static int iio_channel_read_max(struct iio_channel *chan,
803 int *val, int *val2, int *type,
804 enum iio_chan_info_enum info)
814 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
816 case IIO_AVAIL_RANGE:
832 *val = vals[--length];
834 if (vals[--length] > *val)
839 /* FIXME: learn about max for other iio values */
849 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
851 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
855 mutex_lock(&iio_dev_opaque->info_exist_lock);
856 if (!chan->indio_dev->info) {
861 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
863 mutex_unlock(&iio_dev_opaque->info_exist_lock);
867 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
869 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
871 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
873 /* Need to verify underlying driver has not gone away */
875 mutex_lock(&iio_dev_opaque->info_exist_lock);
876 if (chan->indio_dev->info == NULL) {
881 *type = chan->channel->type;
883 mutex_unlock(&iio_dev_opaque->info_exist_lock);
887 EXPORT_SYMBOL_GPL(iio_get_channel_type);
889 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
890 enum iio_chan_info_enum info)
892 return chan->indio_dev->info->write_raw(chan->indio_dev,
893 chan->channel, val, val2, info);
896 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
897 enum iio_chan_info_enum attribute)
899 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
902 mutex_lock(&iio_dev_opaque->info_exist_lock);
903 if (chan->indio_dev->info == NULL) {
908 ret = iio_channel_write(chan, val, val2, attribute);
910 mutex_unlock(&iio_dev_opaque->info_exist_lock);
914 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
916 int iio_write_channel_raw(struct iio_channel *chan, int val)
918 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
920 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
922 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
924 const struct iio_chan_spec_ext_info *ext_info;
927 if (!chan->channel->ext_info)
930 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
935 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
937 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
938 const struct iio_channel *chan,
941 const struct iio_chan_spec_ext_info *ext_info;
943 if (!chan->channel->ext_info)
946 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
947 if (!strcmp(attr, ext_info->name))
954 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
955 const char *attr, char *buf)
957 const struct iio_chan_spec_ext_info *ext_info;
959 ext_info = iio_lookup_ext_info(chan, attr);
963 return ext_info->read(chan->indio_dev, ext_info->private,
966 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
968 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
969 const char *buf, size_t len)
971 const struct iio_chan_spec_ext_info *ext_info;
973 ext_info = iio_lookup_ext_info(chan, attr);
977 return ext_info->write(chan->indio_dev, ext_info->private,
978 chan->channel, buf, len);
980 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);