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, *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,
142 of_node_put(iiospec.np);
144 return -EPROBE_DEFER;
146 indio_dev = dev_to_iio_dev(idev);
147 channel->indio_dev = indio_dev;
148 if (indio_dev->info->of_xlate)
149 index = indio_dev->info->of_xlate(indio_dev, &iiospec);
151 index = __of_iio_simple_xlate(indio_dev, &iiospec);
154 channel->channel = &indio_dev->channels[index];
159 iio_device_put(indio_dev);
163 static struct iio_channel *of_iio_channel_get(struct device_node *np, int index)
165 struct iio_channel *channel;
169 return ERR_PTR(-EINVAL);
171 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
173 return ERR_PTR(-ENOMEM);
175 err = __of_iio_channel_get(channel, np, index);
177 goto err_free_channel;
186 static struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
189 struct iio_channel *chan = NULL;
191 /* Walk up the tree of devices looking for a matching iio channel */
196 * For named iio channels, first look up the name in the
197 * "io-channel-names" property. If it cannot be found, the
198 * index will be an error code, and of_iio_channel_get()
202 index = of_property_match_string(np, "io-channel-names",
204 chan = of_iio_channel_get(np, index);
205 if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
207 else if (name && index >= 0) {
208 pr_err("ERROR: could not get IIO channel %s:%s(%i)\n",
209 np->full_name, name ? name : "", index);
214 * No matching IIO channel found on this node.
215 * If the parent node has a "io-channel-ranges" property,
216 * then we can try one of its channels.
219 if (np && !of_get_property(np, "io-channel-ranges", NULL))
226 static struct iio_channel *of_iio_channel_get_all(struct device *dev)
228 struct iio_channel *chans;
229 int i, mapind, nummaps = 0;
233 ret = of_parse_phandle_with_args(dev->of_node,
241 if (nummaps == 0) /* no error, return NULL to search map table */
244 /* NULL terminated array to save passing size */
245 chans = kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
247 return ERR_PTR(-ENOMEM);
249 /* Search for OF matches */
250 for (mapind = 0; mapind < nummaps; mapind++) {
251 ret = __of_iio_channel_get(&chans[mapind], dev->of_node,
254 goto error_free_chans;
259 for (i = 0; i < mapind; i++)
260 iio_device_put(chans[i].indio_dev);
265 #else /* CONFIG_OF */
267 static inline struct iio_channel *
268 of_iio_channel_get_by_name(struct device_node *np, const char *name)
273 static inline struct iio_channel *of_iio_channel_get_all(struct device *dev)
278 #endif /* CONFIG_OF */
280 static struct iio_channel *iio_channel_get_sys(const char *name,
281 const char *channel_name)
283 struct iio_map_internal *c_i = NULL, *c = NULL;
284 struct iio_channel *channel;
287 if (name == NULL && channel_name == NULL)
288 return ERR_PTR(-ENODEV);
290 /* first find matching entry the channel map */
291 mutex_lock(&iio_map_list_lock);
292 list_for_each_entry(c_i, &iio_map_list, l) {
293 if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
295 strcmp(channel_name, c_i->map->consumer_channel) != 0))
298 iio_device_get(c->indio_dev);
301 mutex_unlock(&iio_map_list_lock);
303 return ERR_PTR(-ENODEV);
305 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
306 if (channel == NULL) {
311 channel->indio_dev = c->indio_dev;
313 if (c->map->adc_channel_label) {
315 iio_chan_spec_from_name(channel->indio_dev,
316 c->map->adc_channel_label);
318 if (channel->channel == NULL) {
329 iio_device_put(c->indio_dev);
333 struct iio_channel *iio_channel_get(struct device *dev,
334 const char *channel_name)
336 const char *name = dev ? dev_name(dev) : NULL;
337 struct iio_channel *channel;
340 channel = of_iio_channel_get_by_name(dev->of_node,
346 return iio_channel_get_sys(name, channel_name);
348 EXPORT_SYMBOL_GPL(iio_channel_get);
350 void iio_channel_release(struct iio_channel *channel)
354 iio_device_put(channel->indio_dev);
357 EXPORT_SYMBOL_GPL(iio_channel_release);
359 static void devm_iio_channel_free(struct device *dev, void *res)
361 struct iio_channel *channel = *(struct iio_channel **)res;
363 iio_channel_release(channel);
366 static int devm_iio_channel_match(struct device *dev, void *res, void *data)
368 struct iio_channel **r = res;
378 struct iio_channel *devm_iio_channel_get(struct device *dev,
379 const char *channel_name)
381 struct iio_channel **ptr, *channel;
383 ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL);
385 return ERR_PTR(-ENOMEM);
387 channel = iio_channel_get(dev, channel_name);
388 if (IS_ERR(channel)) {
394 devres_add(dev, ptr);
398 EXPORT_SYMBOL_GPL(devm_iio_channel_get);
400 void devm_iio_channel_release(struct device *dev, struct iio_channel *channel)
402 WARN_ON(devres_release(dev, devm_iio_channel_free,
403 devm_iio_channel_match, channel));
405 EXPORT_SYMBOL_GPL(devm_iio_channel_release);
407 struct iio_channel *iio_channel_get_all(struct device *dev)
410 struct iio_channel *chans;
411 struct iio_map_internal *c = NULL;
417 return ERR_PTR(-EINVAL);
419 chans = of_iio_channel_get_all(dev);
423 name = dev_name(dev);
425 mutex_lock(&iio_map_list_lock);
426 /* first count the matching maps */
427 list_for_each_entry(c, &iio_map_list, l)
428 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
438 /* NULL terminated array to save passing size */
439 chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
445 /* for each map fill in the chans element */
446 list_for_each_entry(c, &iio_map_list, l) {
447 if (name && strcmp(name, c->map->consumer_dev_name) != 0)
449 chans[mapind].indio_dev = c->indio_dev;
450 chans[mapind].data = c->map->consumer_data;
451 chans[mapind].channel =
452 iio_chan_spec_from_name(chans[mapind].indio_dev,
453 c->map->adc_channel_label);
454 if (chans[mapind].channel == NULL) {
456 goto error_free_chans;
458 iio_device_get(chans[mapind].indio_dev);
463 goto error_free_chans;
465 mutex_unlock(&iio_map_list_lock);
470 for (i = 0; i < nummaps; i++)
471 iio_device_put(chans[i].indio_dev);
474 mutex_unlock(&iio_map_list_lock);
478 EXPORT_SYMBOL_GPL(iio_channel_get_all);
480 void iio_channel_release_all(struct iio_channel *channels)
482 struct iio_channel *chan = &channels[0];
484 while (chan->indio_dev) {
485 iio_device_put(chan->indio_dev);
490 EXPORT_SYMBOL_GPL(iio_channel_release_all);
492 static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
493 enum iio_chan_info_enum info)
496 int vals[INDIO_MAX_RAW_ELEMENTS];
503 if (!iio_channel_has_info(chan->channel, info))
506 if (chan->indio_dev->info->read_raw_multi) {
507 ret = chan->indio_dev->info->read_raw_multi(chan->indio_dev,
508 chan->channel, INDIO_MAX_RAW_ELEMENTS,
509 vals, &val_len, info);
513 ret = chan->indio_dev->info->read_raw(chan->indio_dev,
514 chan->channel, val, val2, info);
519 int iio_read_channel_raw(struct iio_channel *chan, int *val)
523 mutex_lock(&chan->indio_dev->info_exist_lock);
524 if (chan->indio_dev->info == NULL) {
529 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
531 mutex_unlock(&chan->indio_dev->info_exist_lock);
535 EXPORT_SYMBOL_GPL(iio_read_channel_raw);
537 int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
541 mutex_lock(&chan->indio_dev->info_exist_lock);
542 if (chan->indio_dev->info == NULL) {
547 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
549 mutex_unlock(&chan->indio_dev->info_exist_lock);
553 EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
555 static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
556 int raw, int *processed, unsigned int scale)
558 int scale_type, scale_val, scale_val2, offset;
562 ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
566 scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
567 IIO_CHAN_INFO_SCALE);
571 switch (scale_type) {
573 *processed = raw64 * scale_val;
575 case IIO_VAL_INT_PLUS_MICRO:
577 *processed = -raw64 * scale_val;
579 *processed = raw64 * scale_val;
580 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
583 case IIO_VAL_INT_PLUS_NANO:
585 *processed = -raw64 * scale_val;
587 *processed = raw64 * scale_val;
588 *processed += div_s64(raw64 * (s64)scale_val2 * scale,
591 case IIO_VAL_FRACTIONAL:
592 *processed = div_s64(raw64 * (s64)scale_val * scale,
595 case IIO_VAL_FRACTIONAL_LOG2:
596 *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
605 int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
606 int *processed, unsigned int scale)
610 mutex_lock(&chan->indio_dev->info_exist_lock);
611 if (chan->indio_dev->info == NULL) {
616 ret = iio_convert_raw_to_processed_unlocked(chan, raw, processed,
619 mutex_unlock(&chan->indio_dev->info_exist_lock);
623 EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
625 int iio_read_channel_processed(struct iio_channel *chan, int *val)
629 mutex_lock(&chan->indio_dev->info_exist_lock);
630 if (chan->indio_dev->info == NULL) {
635 if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
636 ret = iio_channel_read(chan, val, NULL,
637 IIO_CHAN_INFO_PROCESSED);
639 ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
642 ret = iio_convert_raw_to_processed_unlocked(chan, *val, val, 1);
646 mutex_unlock(&chan->indio_dev->info_exist_lock);
650 EXPORT_SYMBOL_GPL(iio_read_channel_processed);
652 int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
656 mutex_lock(&chan->indio_dev->info_exist_lock);
657 if (chan->indio_dev->info == NULL) {
662 ret = iio_channel_read(chan, val, val2, IIO_CHAN_INFO_SCALE);
664 mutex_unlock(&chan->indio_dev->info_exist_lock);
668 EXPORT_SYMBOL_GPL(iio_read_channel_scale);
670 int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
673 /* Need to verify underlying driver has not gone away */
675 mutex_lock(&chan->indio_dev->info_exist_lock);
676 if (chan->indio_dev->info == NULL) {
681 *type = chan->channel->type;
683 mutex_unlock(&chan->indio_dev->info_exist_lock);
687 EXPORT_SYMBOL_GPL(iio_get_channel_type);
689 static int iio_channel_write(struct iio_channel *chan, int val, int val2,
690 enum iio_chan_info_enum info)
692 return chan->indio_dev->info->write_raw(chan->indio_dev,
693 chan->channel, val, val2, info);
696 int iio_write_channel_raw(struct iio_channel *chan, int val)
700 mutex_lock(&chan->indio_dev->info_exist_lock);
701 if (chan->indio_dev->info == NULL) {
706 ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
708 mutex_unlock(&chan->indio_dev->info_exist_lock);
712 EXPORT_SYMBOL_GPL(iio_write_channel_raw);