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,
139 of_node_put(iiospec.np);
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);
148 index = __of_iio_simple_xlate(indio_dev, &iiospec);
151 channel->channel = &indio_dev->channels[index];
156 iio_device_put(indio_dev);
160 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
162 struct iio_channel *channel;
166 return ERR_PTR(-EINVAL);
168 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
170 return ERR_PTR(-ENOMEM);
172 err = __of_iio_channel_get(channel, np, index);
174 goto err_free_channel;
183 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
186 struct iio_channel *chan = NULL;
188 /* Walk up the tree of devices looking for a matching iio channel */
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()
199 index = of_property_match_string(np, "io-channel-names",
201 chan = of_iio_channel_get(np, index);
202 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
204 else if (name && index >= 0) {
205 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
206 np, name ? name : "", index);
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.
216 if (np && !of_get_property(np, "io-channel-ranges", NULL))
223 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
225 struct iio_channel *chans;
226 int i, mapind, nummaps = 0;
230 ret = of_parse_phandle_with_args(dev->of_node,
238 if (nummaps == 0) /* no error, return NULL to search map table */
241 /* NULL terminated array to save passing size */
242 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
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,
251 goto error_free_chans;
256 for (i = 0; i < mapind; i++)
257 iio_device_put(chans[i].indio_dev);
262 #else /* CONFIG_OF */
264 static inline struct iio_channel *
265 of_iio_channel_get_by_name(struct device_node *np, const char *name)
270 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
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;
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) ||
292 strcmp(channel_name, c_i->map->consumer_channel) != 0))
295 iio_device_get(c->indio_dev);
298 mutex_unlock(&iio_map_list_lock);
300 return ERR_PTR(-ENODEV);
302 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
303 if (channel == NULL) {
308 channel->indio_dev = c->indio_dev;
310 if (c->map->adc_channel_label) {
312 iio_chan_spec_from_name(channel->indio_dev,
313 c->map->adc_channel_label);
315 if (channel->channel == NULL) {
326 iio_device_put(c->indio_dev);
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;
337 channel = of_iio_channel_get_by_name(dev->of_node,
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)
351 iio_device_put(channel->indio_dev);
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 struct iio_channel *devm_iio_channel_get(struct device *dev,
364 const char *channel_name)
366 struct iio_channel **ptr, *channel;
368 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
370 return ERR_PTR(-ENOMEM);
372 channel = iio_channel_get(dev, channel_name);
373 if (IS_ERR(channel)) {
379 devres_add(dev, ptr);
383 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
385 struct iio_channel *iio_channel_get_all(struct device *dev)
388 struct iio_channel *chans;
389 struct iio_map_internal *c = NULL;
395 return ERR_PTR(-EINVAL);
397 chans = of_iio_channel_get_all(dev);
401 name = dev_name(dev);
403 mutex_lock(&iio_map_list_lock);
404 /* first count the matching maps */
405 list_for_each_entry(c, &iio_map_list, l)
406 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
416 /* NULL terminated array to save passing size */
417 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
423 /* for each map fill in the chans element */
424 list_for_each_entry(c, &iio_map_list, l) {
425 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
427 chans[mapind].indio_dev = c->indio_dev;
428 chans[mapind].data = c->map->consumer_data;
429 chans[mapind].channel =
430 iio_chan_spec_from_name(chans[mapind].indio_dev,
431 c->map->adc_channel_label);
432 if (chans[mapind].channel == NULL) {
434 goto error_free_chans;
436 iio_device_get(chans[mapind].indio_dev);
441 goto error_free_chans;
443 mutex_unlock(&iio_map_list_lock);
448 for (i = 0; i < nummaps; i++)
449 iio_device_put(chans[i].indio_dev);
452 mutex_unlock(&iio_map_list_lock);
456 EXPORT_SYMBOL_GPL(iio_channel_get_all);
458 void iio_channel_release_all(struct iio_channel *channels)
460 struct iio_channel *chan = &channels[0];
462 while (chan->indio_dev) {
463 iio_device_put(chan->indio_dev);
468 EXPORT_SYMBOL_GPL(iio_channel_release_all);
470 static void devm_iio_channel_free_all(struct device *dev, void *res)
472 struct iio_channel *channels = *(struct iio_channel **)res;
474 iio_channel_release_all(channels);
477 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
479 struct iio_channel **ptr, *channels;
481 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
483 return ERR_PTR(-ENOMEM);
485 channels = iio_channel_get_all(dev);
486 if (IS_ERR(channels)) {
492 devres_add(dev, ptr);
496 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
498 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
499 enum iio_chan_info_enum info)
502 int vals[INDIO_MAX_RAW_ELEMENTS];
509 if (!iio_channel_has_info(chan->channel, info))
512 if (chan->indio_dev->info->read_raw_multi) {
513 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
514 chan->channel, INDIO_MAX_RAW_ELEMENTS,
515 vals, &val_len, info);
519 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
520 chan->channel, val, val2, info);
525 int iio_read_channel_raw(struct iio_channel *chan, int *val)
529 mutex_lock(&chan->indio_dev->info_exist_lock);
530 if (chan->indio_dev->info == NULL) {
535 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
537 mutex_unlock(&chan->indio_dev->info_exist_lock);
541 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
543 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
547 mutex_lock(&chan->indio_dev->info_exist_lock);
548 if (chan->indio_dev->info == NULL) {
553 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
555 mutex_unlock(&chan->indio_dev->info_exist_lock);
559 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
561 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
562 int raw, int *processed, unsigned int scale)
564 int scale_type, scale_val, scale_val2, offset;
568 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
572 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
573 IIO_CHAN_INFO_SCALE);
574 if (scale_type < 0) {
576 * Just pass raw values as processed if no scaling is
583 switch (scale_type) {
585 *processed = raw64 * scale_val;
587 case IIO_VAL_INT_PLUS_MICRO:
589 *processed = -raw64 * scale_val;
591 *processed = raw64 * scale_val;
592 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
595 case IIO_VAL_INT_PLUS_NANO:
597 *processed = -raw64 * scale_val;
599 *processed = raw64 * scale_val;
600 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
603 case IIO_VAL_FRACTIONAL:
604 *processed = div_s64(raw64 * (s64)scale_val * scale,
607 case IIO_VAL_FRACTIONAL_LOG2:
608 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
617 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
618 int *processed, unsigned int scale)
622 mutex_lock(&chan->indio_dev->info_exist_lock);
623 if (chan->indio_dev->info == NULL) {
628 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
631 mutex_unlock(&chan->indio_dev->info_exist_lock);
635 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
637 int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
638 enum iio_chan_info_enum attribute)
642 mutex_lock(&chan->indio_dev->info_exist_lock);
643 if (chan->indio_dev->info == NULL) {
648 ret = iio_channel_read(chan, val, val2, attribute);
650 mutex_unlock(&chan->indio_dev->info_exist_lock);
654 EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
656 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
658 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
660 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
662 int iio_read_channel_processed(struct iio_channel *chan, int *val)
666 mutex_lock(&chan->indio_dev->info_exist_lock);
667 if (chan->indio_dev->info == NULL) {
672 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
673 ret = iio_channel_read(chan, val, NULL,
674 IIO_CHAN_INFO_PROCESSED);
676 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
679 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
683 mutex_unlock(&chan->indio_dev->info_exist_lock);
687 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
689 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
691 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
693 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
695 static int iio_channel_read_avail(struct iio_channel *chan,
696 const int **vals, int *type, int *length,
697 enum iio_chan_info_enum info)
699 if (!iio_channel_has_available(chan->channel, info))
702 return chan->indio_dev->info->read_avail(chan->indio_dev, chan->channel,
703 vals, type, length, info);
706 int iio_read_avail_channel_attribute(struct iio_channel *chan,
707 const int **vals, int *type, int *length,
708 enum iio_chan_info_enum attribute)
712 mutex_lock(&chan->indio_dev->info_exist_lock);
713 if (!chan->indio_dev->info) {
718 ret = iio_channel_read_avail(chan, vals, type, length, attribute);
720 mutex_unlock(&chan->indio_dev->info_exist_lock);
724 EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
726 int iio_read_avail_channel_raw(struct iio_channel *chan,
727 const int **vals, int *length)
732 ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
735 if (ret >= 0 && type != IIO_VAL_INT)
736 /* raw values are assumed to be IIO_VAL_INT */
741 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
743 static int iio_channel_read_max(struct iio_channel *chan,
744 int *val, int *val2, int *type,
745 enum iio_chan_info_enum info)
755 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
757 case IIO_AVAIL_RANGE:
773 *val = vals[--length];
775 if (vals[--length] > *val)
780 /* FIXME: learn about max for other iio values */
790 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
795 mutex_lock(&chan->indio_dev->info_exist_lock);
796 if (!chan->indio_dev->info) {
801 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
803 mutex_unlock(&chan->indio_dev->info_exist_lock);
807 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
809 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
812 /* Need to verify underlying driver has not gone away */
814 mutex_lock(&chan->indio_dev->info_exist_lock);
815 if (chan->indio_dev->info == NULL) {
820 *type = chan->channel->type;
822 mutex_unlock(&chan->indio_dev->info_exist_lock);
826 EXPORT_SYMBOL_GPL(iio_get_channel_type);
828 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
829 enum iio_chan_info_enum info)
831 return chan->indio_dev->info->write_raw(chan->indio_dev,
832 chan->channel, val, val2, info);
835 int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
836 enum iio_chan_info_enum attribute)
840 mutex_lock(&chan->indio_dev->info_exist_lock);
841 if (chan->indio_dev->info == NULL) {
846 ret = iio_channel_write(chan, val, val2, attribute);
848 mutex_unlock(&chan->indio_dev->info_exist_lock);
852 EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
854 int iio_write_channel_raw(struct iio_channel *chan, int val)
856 return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
858 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
860 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
862 const struct iio_chan_spec_ext_info *ext_info;
865 if (!chan->channel->ext_info)
868 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
873 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
875 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
876 const struct iio_channel *chan,
879 const struct iio_chan_spec_ext_info *ext_info;
881 if (!chan->channel->ext_info)
884 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
885 if (!strcmp(attr, ext_info->name))
892 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
893 const char *attr, char *buf)
895 const struct iio_chan_spec_ext_info *ext_info;
897 ext_info = iio_lookup_ext_info(chan, attr);
901 return ext_info->read(chan->indio_dev, ext_info->private,
904 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
906 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
907 const char *buf, size_t len)
909 const struct iio_chan_spec_ext_info *ext_info;
911 ext_info = iio_lookup_ext_info(chan, attr);
915 return ext_info->write(chan->indio_dev, ext_info->private,
916 chan->channel, buf, len);
918 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);