1 /* The industrial I/O core in kernel channel mapping
3 * Copyright (c) 2011 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/mutex.h>
15 #include <linux/iio/iio.h>
17 #include <linux/iio/machine.h>
18 #include <linux/iio/driver.h>
19 #include <linux/iio/consumer.h>
21 struct iio_map_internal {
22 struct iio_dev *indio_dev;
27 static LIST_HEAD(iio_map_list);
28 static DEFINE_MUTEX(iio_map_list_lock);
30 int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
33 struct iio_map_internal *mapi;
38 mutex_lock(&iio_map_list_lock);
39 while (maps[i].consumer_dev_name != NULL) {
40 mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
46 mapi->indio_dev = indio_dev;
47 list_add_tail(&mapi->l, &iio_map_list);
51 mutex_unlock(&iio_map_list_lock);
55 EXPORT_SYMBOL_GPL(iio_map_array_register);
59 * Remove all map entries associated with the given iio device
61 int iio_map_array_unregister(struct iio_dev *indio_dev)
64 struct iio_map_internal *mapi, *next;
66 mutex_lock(&iio_map_list_lock);
67 list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
68 if (indio_dev == mapi->indio_dev) {
74 mutex_unlock(&iio_map_list_lock);
77 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
79 static const struct iio_chan_spec
80 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
83 const struct iio_chan_spec *chan = NULL;
85 for (i = 0; i < indio_dev->num_channels; i++)
86 if (indio_dev->channels[i].datasheet_name &&
87 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
88 chan = &indio_dev->channels[i];
96 static int iio_dev_node_match(struct device *dev, void *data)
98 return dev->of_node == data && dev->type == &iio_device_type;
102 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
103 * @indio_dev: pointer to the iio_dev structure
104 * @iiospec: IIO specifier as found in the device tree
106 * This is simple translation function, suitable for the most 1:1 mapped
107 * channels in IIO chips. This function performs only one sanity check:
108 * whether IIO index is less than num_channels (that is specified in the
111 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
112 const struct of_phandle_args *iiospec)
114 if (!iiospec->args_count)
117 if (iiospec->args[0] >= indio_dev->num_channels) {
118 dev_err(&indio_dev->dev, "invalid channel index %u\n",
123 return iiospec->args[0];
126 static int __of_iio_channel_get(struct iio_channel *channel,
127 struct device_node *np, int index)
130 struct iio_dev *indio_dev;
132 struct of_phandle_args iiospec;
134 err = of_parse_phandle_with_args(np, "io-channels",
140 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
143 of_node_put(iiospec.np);
144 return -EPROBE_DEFER;
147 indio_dev = dev_to_iio_dev(idev);
148 channel->indio_dev = indio_dev;
149 if (indio_dev->info->of_xlate)
150 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
152 index = __of_iio_simple_xlate(indio_dev, &iiospec);
153 of_node_put(iiospec.np);
156 channel->channel = &indio_dev->channels[index];
161 iio_device_put(indio_dev);
165 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
167 struct iio_channel *channel;
171 return ERR_PTR(-EINVAL);
173 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
175 return ERR_PTR(-ENOMEM);
177 err = __of_iio_channel_get(channel, np, index);
179 goto err_free_channel;
188 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
191 struct iio_channel *chan = NULL;
193 /* Walk up the tree of devices looking for a matching iio channel */
198 * For named iio channels, first look up the name in the
199 * "io-channel-names" property. If it cannot be found, the
200 * index will be an error code, and of_iio_channel_get()
204 index = of_property_match_string(np, "io-channel-names",
206 chan = of_iio_channel_get(np, index);
207 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
209 else if (name && index >= 0) {
210 pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
211 np, name ? name : "", index);
216 * No matching IIO channel found on this node.
217 * If the parent node has a "io-channel-ranges" property,
218 * then we can try one of its channels.
221 if (np && !of_get_property(np, "io-channel-ranges", NULL))
228 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
230 struct iio_channel *chans;
231 int i, mapind, nummaps = 0;
235 ret = of_parse_phandle_with_args(dev->of_node,
243 if (nummaps == 0) /* no error, return NULL to search map table */
246 /* NULL terminated array to save passing size */
247 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
249 return ERR_PTR(-ENOMEM);
251 /* Search for OF matches */
252 for (mapind = 0; mapind < nummaps; mapind++) {
253 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
256 goto error_free_chans;
261 for (i = 0; i < mapind; i++)
262 iio_device_put(chans[i].indio_dev);
267 #else /* CONFIG_OF */
269 static inline struct iio_channel *
270 of_iio_channel_get_by_name(struct device_node *np, const char *name)
275 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
280 #endif /* CONFIG_OF */
282 static struct iio_channel *iio_channel_get_sys(const char *name,
283 const char *channel_name)
285 struct iio_map_internal *c_i = NULL, *c = NULL;
286 struct iio_channel *channel;
289 if (name == NULL && channel_name == NULL)
290 return ERR_PTR(-ENODEV);
292 /* first find matching entry the channel map */
293 mutex_lock(&iio_map_list_lock);
294 list_for_each_entry(c_i, &iio_map_list, l) {
295 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
297 strcmp(channel_name, c_i->map->consumer_channel) != 0))
300 iio_device_get(c->indio_dev);
303 mutex_unlock(&iio_map_list_lock);
305 return ERR_PTR(-ENODEV);
307 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
308 if (channel == NULL) {
313 channel->indio_dev = c->indio_dev;
315 if (c->map->adc_channel_label) {
317 iio_chan_spec_from_name(channel->indio_dev,
318 c->map->adc_channel_label);
320 if (channel->channel == NULL) {
331 iio_device_put(c->indio_dev);
335 struct iio_channel *iio_channel_get(struct device *dev,
336 const char *channel_name)
338 const char *name = dev ? dev_name(dev) : NULL;
339 struct iio_channel *channel;
342 channel = of_iio_channel_get_by_name(dev->of_node,
348 return iio_channel_get_sys(name, channel_name);
350 EXPORT_SYMBOL_GPL(iio_channel_get);
352 void iio_channel_release(struct iio_channel *channel)
356 iio_device_put(channel->indio_dev);
359 EXPORT_SYMBOL_GPL(iio_channel_release);
361 static void devm_iio_channel_free(struct device *dev, void *res)
363 struct iio_channel *channel = *(struct iio_channel **)res;
365 iio_channel_release(channel);
368 static int devm_iio_channel_match(struct device *dev, void *res, void *data)
370 struct iio_channel **r = res;
380 struct iio_channel *devm_iio_channel_get(struct device *dev,
381 const char *channel_name)
383 struct iio_channel **ptr, *channel;
385 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
387 return ERR_PTR(-ENOMEM);
389 channel = iio_channel_get(dev, channel_name);
390 if (IS_ERR(channel)) {
396 devres_add(dev, ptr);
400 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
402 void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
404 WARN_ON(devres_release(dev, devm_iio_channel_free,
405 devm_iio_channel_match, channel));
407 EXPORT_SYMBOL_GPL(devm_iio_channel_release);
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 = kzalloc(sizeof(*chans)*(nummaps + 1), 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(struct device *dev, void *res)
496 struct iio_channel *channels = *(struct iio_channel **)res;
498 iio_channel_release_all(channels);
501 struct iio_channel *devm_iio_channel_get_all(struct device *dev)
503 struct iio_channel **ptr, *channels;
505 ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL);
507 return ERR_PTR(-ENOMEM);
509 channels = iio_channel_get_all(dev);
510 if (IS_ERR(channels)) {
516 devres_add(dev, ptr);
520 EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
522 void devm_iio_channel_release_all(struct device *dev,
523 struct iio_channel *channels)
525 WARN_ON(devres_release(dev, devm_iio_channel_free_all,
526 devm_iio_channel_match, channels));
528 EXPORT_SYMBOL_GPL(devm_iio_channel_release_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)
561 mutex_lock(&chan->indio_dev->info_exist_lock);
562 if (chan->indio_dev->info == NULL) {
567 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
569 mutex_unlock(&chan->indio_dev->info_exist_lock);
573 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
575 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
579 mutex_lock(&chan->indio_dev->info_exist_lock);
580 if (chan->indio_dev->info == NULL) {
585 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
587 mutex_unlock(&chan->indio_dev->info_exist_lock);
591 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
593 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
594 int raw, int *processed, unsigned int scale)
596 int scale_type, scale_val, scale_val2;
597 int offset_type, offset_val, offset_val2;
600 offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
601 IIO_CHAN_INFO_OFFSET);
602 if (offset_type >= 0) {
603 switch (offset_type) {
606 case IIO_VAL_INT_PLUS_MICRO:
607 case IIO_VAL_INT_PLUS_NANO:
609 * Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
610 * implicitely truncate the offset to it's integer form.
613 case IIO_VAL_FRACTIONAL:
614 offset_val /= offset_val2;
616 case IIO_VAL_FRACTIONAL_LOG2:
617 offset_val >>= offset_val2;
626 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
627 IIO_CHAN_INFO_SCALE);
628 if (scale_type < 0) {
630 * If no channel scaling is available apply consumer scale to
631 * raw value and return.
633 *processed = raw * scale;
637 switch (scale_type) {
639 *processed = raw64 * scale_val * scale;
641 case IIO_VAL_INT_PLUS_MICRO:
643 *processed = -raw64 * scale_val;
645 *processed = raw64 * scale_val;
646 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
649 case IIO_VAL_INT_PLUS_NANO:
651 *processed = -raw64 * scale_val;
653 *processed = raw64 * scale_val;
654 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
657 case IIO_VAL_FRACTIONAL:
658 *processed = div_s64(raw64 * (s64)scale_val * scale,
661 case IIO_VAL_FRACTIONAL_LOG2:
662 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
671 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
672 int *processed, unsigned int scale)
676 mutex_lock(&chan->indio_dev->info_exist_lock);
677 if (chan->indio_dev->info == NULL) {
682 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
685 mutex_unlock(&chan->indio_dev->info_exist_lock);
689 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
691 static int iio_read_channel_attribute(struct iio_channel *chan,
693 enum iio_chan_info_enum attribute)
697 mutex_lock(&chan->indio_dev->info_exist_lock);
698 if (chan->indio_dev->info == NULL) {
703 ret = iio_channel_read(chan, val, val2, attribute);
705 mutex_unlock(&chan->indio_dev->info_exist_lock);
710 int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
712 return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
714 EXPORT_SYMBOL_GPL(iio_read_channel_offset);
716 int iio_read_channel_processed(struct iio_channel *chan, int *val)
720 mutex_lock(&chan->indio_dev->info_exist_lock);
721 if (chan->indio_dev->info == NULL) {
726 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
727 ret = iio_channel_read(chan, val, NULL,
728 IIO_CHAN_INFO_PROCESSED);
730 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
733 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
737 mutex_unlock(&chan->indio_dev->info_exist_lock);
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_raw(struct iio_channel *chan,
761 const int **vals, int *length)
766 mutex_lock(&chan->indio_dev->info_exist_lock);
767 if (!chan->indio_dev->info) {
772 ret = iio_channel_read_avail(chan,
773 vals, &type, length, IIO_CHAN_INFO_RAW);
775 mutex_unlock(&chan->indio_dev->info_exist_lock);
777 if (ret >= 0 && type != IIO_VAL_INT)
778 /* raw values are assumed to be IIO_VAL_INT */
783 EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
785 static int iio_channel_read_max(struct iio_channel *chan,
786 int *val, int *val2, int *type,
787 enum iio_chan_info_enum info)
797 ret = iio_channel_read_avail(chan, &vals, type, &length, info);
799 case IIO_AVAIL_RANGE:
815 *val = vals[--length];
817 if (vals[--length] > *val)
822 /* FIXME: learn about max for other iio values */
832 int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
837 mutex_lock(&chan->indio_dev->info_exist_lock);
838 if (!chan->indio_dev->info) {
843 ret = iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
845 mutex_unlock(&chan->indio_dev->info_exist_lock);
849 EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
851 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
854 /* Need to verify underlying driver has not gone away */
856 mutex_lock(&chan->indio_dev->info_exist_lock);
857 if (chan->indio_dev->info == NULL) {
862 *type = chan->channel->type;
864 mutex_unlock(&chan->indio_dev->info_exist_lock);
868 EXPORT_SYMBOL_GPL(iio_get_channel_type);
870 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
871 enum iio_chan_info_enum info)
873 return chan->indio_dev->info->write_raw(chan->indio_dev,
874 chan->channel, val, val2, info);
877 int iio_write_channel_raw(struct iio_channel *chan, int val)
881 mutex_lock(&chan->indio_dev->info_exist_lock);
882 if (chan->indio_dev->info == NULL) {
887 ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
889 mutex_unlock(&chan->indio_dev->info_exist_lock);
893 EXPORT_SYMBOL_GPL(iio_write_channel_raw);
895 unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
897 const struct iio_chan_spec_ext_info *ext_info;
900 if (!chan->channel->ext_info)
903 for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
908 EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
910 static const struct iio_chan_spec_ext_info *iio_lookup_ext_info(
911 const struct iio_channel *chan,
914 const struct iio_chan_spec_ext_info *ext_info;
916 if (!chan->channel->ext_info)
919 for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
920 if (!strcmp(attr, ext_info->name))
927 ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
928 const char *attr, char *buf)
930 const struct iio_chan_spec_ext_info *ext_info;
932 ext_info = iio_lookup_ext_info(chan, attr);
936 return ext_info->read(chan->indio_dev, ext_info->private,
939 EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
941 ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
942 const char *buf, size_t len)
944 const struct iio_chan_spec_ext_info *ext_info;
946 ext_info = iio_lookup_ext_info(chan, attr);
950 return ext_info->write(chan->indio_dev, ext_info->private,
951 chan->channel, buf, len);
953 EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);