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(&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;
65 struct list_head *pos, *tmp;
67 mutex_lock(&iio_map_list_lock);
68 list_for_each_safe(pos, tmp, &iio_map_list) {
69 mapi = list_entry(pos, struct iio_map_internal, l);
70 if (indio_dev == mapi->indio_dev) {
76 mutex_unlock(&iio_map_list_lock);
79 EXPORT_SYMBOL_GPL(iio_map_array_unregister);
81 static const struct iio_chan_spec
82 *iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
85 const struct iio_chan_spec *chan = NULL;
87 for (i = 0; i < indio_dev->num_channels; i++)
88 if (indio_dev->channels[i].datasheet_name &&
89 strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
90 chan = &indio_dev->channels[i];
98 static int iio_dev_node_match(struct device *dev, void *data)
100 return dev->of_node == data && dev->type == &iio_device_type;
104 * __of_iio_simple_xlate - translate iiospec to the IIO channel index
105 * @indio_dev: pointer to the iio_dev structure
106 * @iiospec: IIO specifier as found in the device tree
108 * This is simple translation function, suitable for the most 1:1 mapped
109 * channels in IIO chips. This function performs only one sanity check:
110 * whether IIO index is less than num_channels (that is specified in the
113 static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
114 const struct of_phandle_args *iiospec)
116 if (!iiospec->args_count)
119 if (iiospec->args[0] >= indio_dev->num_channels) {
120 dev_err(&indio_dev->dev, "invalid channel index %u\n",
125 return iiospec->args[0];
128 static int __of_iio_channel_get(struct iio_channel *channel,
129 struct device_node *np, int index)
132 struct iio_dev *indio_dev;
134 struct of_phandle_args iiospec;
136 err = of_parse_phandle_with_args(np, "io-channels",
142 idev = bus_find_device(&iio_bus_type, NULL, iiospec.np,
144 of_node_put(iiospec.np);
146 return -EPROBE_DEFER;
148 indio_dev = dev_to_iio_dev(idev);
149 channel->indio_dev = indio_dev;
150 if (indio_dev->info->of_xlate)
151 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
153 index = __of_iio_simple_xlate(indio_dev, &iiospec);
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 %s:%s(%i)\n",
211 np->full_name, 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 int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
495 enum iio_chan_info_enum info)
498 int vals[INDIO_MAX_RAW_ELEMENTS];
505 if(!iio_channel_has_info(chan->channel, info))
508 if (chan->indio_dev->info->read_raw_multi) {
509 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
510 chan->channel, INDIO_MAX_RAW_ELEMENTS,
511 vals, &val_len, info);
515 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
516 chan->channel, val, val2, info);
521 int iio_read_channel_raw(struct iio_channel *chan, int *val)
525 mutex_lock(&chan->indio_dev->info_exist_lock);
526 if (chan->indio_dev->info == NULL) {
531 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
533 mutex_unlock(&chan->indio_dev->info_exist_lock);
537 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
539 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
543 mutex_lock(&chan->indio_dev->info_exist_lock);
544 if (chan->indio_dev->info == NULL) {
549 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
551 mutex_unlock(&chan->indio_dev->info_exist_lock);
555 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
557 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
558 int raw, int *processed, unsigned int scale)
560 int scale_type, scale_val, scale_val2, offset;
564 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
568 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
569 IIO_CHAN_INFO_SCALE);
573 switch (scale_type) {
575 *processed = raw64 * scale_val;
577 case IIO_VAL_INT_PLUS_MICRO:
579 *processed = -raw64 * scale_val;
581 *processed = raw64 * scale_val;
582 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
585 case IIO_VAL_INT_PLUS_NANO:
587 *processed = -raw64 * scale_val;
589 *processed = raw64 * scale_val;
590 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
593 case IIO_VAL_FRACTIONAL:
594 *processed = div_s64(raw64 * (s64)scale_val * scale,
597 case IIO_VAL_FRACTIONAL_LOG2:
598 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
607 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
608 int *processed, unsigned int scale)
612 mutex_lock(&chan->indio_dev->info_exist_lock);
613 if (chan->indio_dev->info == NULL) {
618 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
621 mutex_unlock(&chan->indio_dev->info_exist_lock);
625 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
627 int iio_read_channel_processed(struct iio_channel *chan, int *val)
631 mutex_lock(&chan->indio_dev->info_exist_lock);
632 if (chan->indio_dev->info == NULL) {
637 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
638 ret = iio_channel_read(chan, val, NULL,
639 IIO_CHAN_INFO_PROCESSED);
641 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
644 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
648 mutex_unlock(&chan->indio_dev->info_exist_lock);
652 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
654 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
658 mutex_lock(&chan->indio_dev->info_exist_lock);
659 if (chan->indio_dev->info == NULL) {
664 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
666 mutex_unlock(&chan->indio_dev->info_exist_lock);
670 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
672 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
675 /* Need to verify underlying driver has not gone away */
677 mutex_lock(&chan->indio_dev->info_exist_lock);
678 if (chan->indio_dev->info == NULL) {
683 *type = chan->channel->type;
685 mutex_unlock(&chan->indio_dev->info_exist_lock);
689 EXPORT_SYMBOL_GPL(iio_get_channel_type);
691 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
692 enum iio_chan_info_enum info)
694 return chan->indio_dev->info->write_raw(chan->indio_dev,
695 chan->channel, val, val2, info);
698 int iio_write_channel_raw(struct iio_channel *chan, int val)
702 mutex_lock(&chan->indio_dev->info_exist_lock);
703 if (chan->indio_dev->info == NULL) {
708 ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
710 mutex_unlock(&chan->indio_dev->info_exist_lock);
714 EXPORT_SYMBOL_GPL(iio_write_channel_raw);