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>
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;
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)
30 struct iio_map_internal *mapi;
35 mutex_lock(&iio_map_list_lock);
36 while (maps[i].consumer_dev_name != NULL) {
37 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
43 mapi->indio_dev = indio_dev;
44 list_add_tail(&mapi->l, &iio_map_list);
48 mutex_unlock(&iio_map_list_lock);
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)
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) {
71 mutex_unlock(&iio_map_list_lock);
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)
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];
93 static int iio_dev_node_match(struct device *dev, const void *data)
95 return dev->of_node == data && dev->type == &iio_device_type;
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
108 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
109 const struct of_phandle_args *iiospec)
111 if (!iiospec->args_count)
114 if (iiospec->args[0] >= indio_dev->num_channels) {
115 dev_err(&indio_dev->dev, "invalid channel index %u\n",
120 return iiospec->args[0];
123 static int __of_iio_channel_get(struct iio_channel *channel,
124 struct device_node *np, int index)
127 struct iio_dev *indio_dev;
129 struct of_phandle_args iiospec;
131 err = of_parse_phandle_with_args(np, "io-channels",
137 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
140 of_node_put(iiospec.np);
141 return -EPROBE_DEFER;
144 indio_dev = dev_to_iio_dev(idev);
145 channel->indio_dev = indio_dev;
146 if (indio_dev->info->of_xlate)
147 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
149 index = __of_iio_simple_xlate(indio_dev, &iiospec);
150 of_node_put(iiospec.np);
153 channel->channel = &indio_dev->channels[index];
158 iio_device_put(indio_dev);
162 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
164 struct iio_channel *channel;
168 return ERR_PTR(-EINVAL);
170 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 err = __of_iio_channel_get(channel, np, index);
176 goto err_free_channel;
185 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
188 struct iio_channel *chan = NULL;
190 /* Walk up the tree of devices looking for a matching iio channel */
195 * For named iio channels, first look up the name in the
196 * "io-channel-names" property. If it cannot be found, the
197 * index will be an error code, and of_iio_channel_get()
201 index = of_property_match_string(np, "io-channel-names",
203 chan = of_iio_channel_get(np, index);
204 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
206 else if (name && index >= 0) {
207 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
208 np, name ? name : "", index);
213 * No matching IIO channel found on this node.
214 * If the parent node has a "io-channel-ranges" property,
215 * then we can try one of its channels.
218 if (np && !of_get_property(np, "io-channel-ranges", NULL))
225 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
227 struct iio_channel *chans;
228 int i, mapind, nummaps = 0;
232 ret = of_parse_phandle_with_args(dev->of_node,
240 if (nummaps == 0) /* no error, return NULL to search map table */
243 /* NULL terminated array to save passing size */
244 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
246 return ERR_PTR(-ENOMEM);
248 /* Search for OF matches */
249 for (mapind = 0; mapind < nummaps; mapind++) {
250 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
253 goto error_free_chans;
258 for (i = 0; i < mapind; i++)
259 iio_device_put(chans[i].indio_dev);
264 #else /* CONFIG_OF */
266 static inline struct iio_channel *
267 of_iio_channel_get_by_name(struct device_node *np, const char *name)
272 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
277 #endif /* CONFIG_OF */
279 static struct iio_channel *iio_channel_get_sys(const char *name,
280 const char *channel_name)
282 struct iio_map_internal *c_i = NULL, *c = NULL;
283 struct iio_channel *channel;
286 if (name == NULL && channel_name == NULL)
287 return ERR_PTR(-ENODEV);
289 /* first find matching entry the channel map */
290 mutex_lock(&iio_map_list_lock);
291 list_for_each_entry(c_i, &iio_map_list, l) {
292 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
294 strcmp(channel_name, c_i->map->consumer_channel) != 0))
297 iio_device_get(c->indio_dev);
300 mutex_unlock(&iio_map_list_lock);
302 return ERR_PTR(-ENODEV);
304 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
305 if (channel == NULL) {
310 channel->indio_dev = c->indio_dev;
312 if (c->map->adc_channel_label) {
314 iio_chan_spec_from_name(channel->indio_dev,
315 c->map->adc_channel_label);
317 if (channel->channel == NULL) {
328 iio_device_put(c->indio_dev);
332 struct iio_channel *iio_channel_get(struct device *dev,
333 const char *channel_name)
335 const char *name = dev ? dev_name(dev) : NULL;
336 struct iio_channel *channel;
339 channel = of_iio_channel_get_by_name(dev->of_node,
345 return iio_channel_get_sys(name, channel_name);
347 EXPORT_SYMBOL_GPL(iio_channel_get);
349 void iio_channel_release(struct iio_channel *channel)
353 iio_device_put(channel->indio_dev);
356 EXPORT_SYMBOL_GPL(iio_channel_release);
358 static void devm_iio_channel_free(struct device *dev, void *res)
360 struct iio_channel *channel = *(struct iio_channel **)res;
362 iio_channel_release(channel);
365 struct iio_channel *devm_iio_channel_get(struct device *dev,
366 const char *channel_name)
368 struct iio_channel **ptr, *channel;
370 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
372 return ERR_PTR(-ENOMEM);
374 channel = iio_channel_get(dev, channel_name);
375 if (IS_ERR(channel)) {
381 devres_add(dev, ptr);
385 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
387 struct iio_channel *iio_channel_get_all(struct device *dev)
390 struct iio_channel *chans;
391 struct iio_map_internal *c = NULL;
397 return ERR_PTR(-EINVAL);
399 chans = of_iio_channel_get_all(dev);
403 name = dev_name(dev);
405 mutex_lock(&iio_map_list_lock);
406 /* first count the matching maps */
407 list_for_each_entry(c, &iio_map_list, l)
408 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
418 /* NULL terminated array to save passing size */
419 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
425 /* for each map fill in the chans element */
426 list_for_each_entry(c, &iio_map_list, l) {
427 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
429 chans[mapind].indio_dev = c->indio_dev;
430 chans[mapind].data = c->map->consumer_data;
431 chans[mapind].channel =
432 iio_chan_spec_from_name(chans[mapind].indio_dev,
433 c->map->adc_channel_label);
434 if (chans[mapind].channel == NULL) {
436 goto error_free_chans;
438 iio_device_get(chans[mapind].indio_dev);
443 goto error_free_chans;
445 mutex_unlock(&iio_map_list_lock);
450 for (i = 0; i < nummaps; i++)
451 iio_device_put(chans[i].indio_dev);
454 mutex_unlock(&iio_map_list_lock);
458 EXPORT_SYMBOL_GPL(iio_channel_get_all);
460 void iio_channel_release_all(struct iio_channel *channels)
462 struct iio_channel *chan = &channels[0];
464 while (chan->indio_dev) {
465 iio_device_put(chan->indio_dev);
470 EXPORT_SYMBOL_GPL(iio_channel_release_all);
472 static void devm_iio_channel_free_all(struct device *dev, void *res)
474 struct iio_channel *channels = *(struct iio_channel **)res;
476 iio_channel_release_all(channels);
479 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
481 struct iio_channel **ptr, *channels;
483 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
485 return ERR_PTR(-ENOMEM);
487 channels = iio_channel_get_all(dev);
488 if (IS_ERR(channels)) {
494 devres_add(dev, ptr);
498 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
500 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
501 enum iio_chan_info_enum info)
504 int vals[INDIO_MAX_RAW_ELEMENTS];
511 if (!iio_channel_has_info(chan->channel, info))
514 if (chan->indio_dev->info->read_raw_multi) {
515 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
516 chan->channel, INDIO_MAX_RAW_ELEMENTS,
517 vals, &val_len, info);
521 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
522 chan->channel, val, val2, info);
527 int iio_read_channel_raw(struct iio_channel *chan, int *val)
531 mutex_lock(&chan->indio_dev->info_exist_lock);
532 if (chan->indio_dev->info == NULL) {
537 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
539 mutex_unlock(&chan->indio_dev->info_exist_lock);
543 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
545 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
549 mutex_lock(&chan->indio_dev->info_exist_lock);
550 if (chan->indio_dev->info == NULL) {
555 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
557 mutex_unlock(&chan->indio_dev->info_exist_lock);
561 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
563 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
564 int raw, int *processed, unsigned int scale)
566 int scale_type, scale_val, scale_val2;
567 int offset_type, offset_val, offset_val2;
570 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
571 IIO_CHAN_INFO_OFFSET);
572 if (offset_type >= 0) {
573 switch (offset_type) {
576 case IIO_VAL_INT_PLUS_MICRO:
577 case IIO_VAL_INT_PLUS_NANO:
579 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
580 * implicitely truncate the offset to it's integer form.
583 case IIO_VAL_FRACTIONAL:
584 offset_val /= offset_val2;
586 case IIO_VAL_FRACTIONAL_LOG2:
587 offset_val >>= offset_val2;
596 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
597 IIO_CHAN_INFO_SCALE);
598 if (scale_type < 0) {
600 * If no channel scaling is available apply consumer scale to
601 * raw value and return.
603 *processed = raw * scale;
607 switch (scale_type) {
609 *processed = raw64 * scale_val * scale;
611 case IIO_VAL_INT_PLUS_MICRO:
613 *processed = -raw64 * scale_val;
615 *processed = raw64 * scale_val;
616 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
619 case IIO_VAL_INT_PLUS_NANO:
621 *processed = -raw64 * scale_val;
623 *processed = raw64 * scale_val;
624 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
627 case IIO_VAL_FRACTIONAL:
628 *processed = div_s64(raw64 * (s64)scale_val * scale,
631 case IIO_VAL_FRACTIONAL_LOG2:
632 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
641 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
642 int *processed, unsigned int scale)
646 mutex_lock(&chan->indio_dev->info_exist_lock);
647 if (chan->indio_dev->info == NULL) {
652 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
655 mutex_unlock(&chan->indio_dev->info_exist_lock);
659 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
661 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
662 enum iio_chan_info_enum attribute)
666 mutex_lock(&chan->indio_dev->info_exist_lock);
667 if (chan->indio_dev->info == NULL) {
672 ret = iio_channel_read(chan, val, val2, attribute);
674 mutex_unlock(&chan->indio_dev->info_exist_lock);
678 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
680 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
682 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
684 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
686 int iio_read_channel_processed(struct iio_channel *chan, int *val)
690 mutex_lock(&chan->indio_dev->info_exist_lock);
691 if (chan->indio_dev->info == NULL) {
696 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
697 ret = iio_channel_read(chan, val, NULL,
698 IIO_CHAN_INFO_PROCESSED);
700 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
703 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
707 mutex_unlock(&chan->indio_dev->info_exist_lock);
711 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
713 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
715 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
717 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
719 static int iio_channel_read_avail(struct iio_channel *chan,
720 const int **vals, int *type, int *length,
721 enum iio_chan_info_enum info)
723 if (!iio_channel_has_available(chan->channel, info))
726 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
727 vals, type, length, info);
730 int iio_read_avail_channel_attribute(struct iio_channel *chan,
731 const int **vals, int *type, int *length,
732 enum iio_chan_info_enum attribute)
736 mutex_lock(&chan->indio_dev->info_exist_lock);
737 if (!chan->indio_dev->info) {
742 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
744 mutex_unlock(&chan->indio_dev->info_exist_lock);
748 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
750 int iio_read_avail_channel_raw(struct iio_channel *chan,
751 const int **vals, int *length)
756 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
759 if (ret >= 0 && type != IIO_VAL_INT)
760 /* raw values are assumed to be IIO_VAL_INT */
765 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
767 static int iio_channel_read_max(struct iio_channel *chan,
768 int *val, int *val2, int *type,
769 enum iio_chan_info_enum info)
779 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
781 case IIO_AVAIL_RANGE:
797 *val = vals[--length];
799 if (vals[--length] > *val)
804 /* FIXME: learn about max for other iio values */
814 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
819 mutex_lock(&chan->indio_dev->info_exist_lock);
820 if (!chan->indio_dev->info) {
825 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
827 mutex_unlock(&chan->indio_dev->info_exist_lock);
831 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
833 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
836 /* Need to verify underlying driver has not gone away */
838 mutex_lock(&chan->indio_dev->info_exist_lock);
839 if (chan->indio_dev->info == NULL) {
844 *type = chan->channel->type;
846 mutex_unlock(&chan->indio_dev->info_exist_lock);
850 EXPORT_SYMBOL_GPL(iio_get_channel_type);
852 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
853 enum iio_chan_info_enum info)
855 return chan->indio_dev->info->write_raw(chan->indio_dev,
856 chan->channel, val, val2, info);
859 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
860 enum iio_chan_info_enum attribute)
864 mutex_lock(&chan->indio_dev->info_exist_lock);
865 if (chan->indio_dev->info == NULL) {
870 ret = iio_channel_write(chan, val, val2, attribute);
872 mutex_unlock(&chan->indio_dev->info_exist_lock);
876 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
878 int iio_write_channel_raw(struct iio_channel *chan, int val)
880 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
882 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
884 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
886 const struct iio_chan_spec_ext_info *ext_info;
889 if (!chan->channel->ext_info)
892 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
897 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
899 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
900 const struct iio_channel *chan,
903 const struct iio_chan_spec_ext_info *ext_info;
905 if (!chan->channel->ext_info)
908 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
909 if (!strcmp(attr, ext_info->name))
916 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
917 const char *attr, char *buf)
919 const struct iio_chan_spec_ext_info *ext_info;
921 ext_info = iio_lookup_ext_info(chan, attr);
925 return ext_info->read(chan->indio_dev, ext_info->private,
928 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
930 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
931 const char *buf, size_t len)
933 const struct iio_chan_spec_ext_info *ext_info;
935 ext_info = iio_lookup_ext_info(chan, attr);
939 return ext_info->write(chan->indio_dev, ext_info->private,
940 chan->channel, buf, len);
942 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);