2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2012-02-15 19:48:01 +00:00
|
|
|
/* The industrial I/O core in kernel channel mapping
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011 Jonathan Cameron
|
|
|
|
*/
|
2024-02-29 15:10:28 +00:00
|
|
|
#include <linux/cleanup.h>
|
2012-02-15 19:48:01 +00:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/export.h>
|
2023-06-23 08:58:25 +00:00
|
|
|
#include <linux/minmax.h>
|
2023-06-23 08:58:23 +00:00
|
|
|
#include <linux/mutex.h>
|
2022-07-15 12:28:53 +00:00
|
|
|
#include <linux/property.h>
|
2012-02-15 19:48:01 +00:00
|
|
|
#include <linux/slab.h>
|
|
|
|
|
2012-04-25 14:54:58 +00:00
|
|
|
#include <linux/iio/iio.h>
|
2021-04-26 17:49:08 +00:00
|
|
|
#include <linux/iio/iio-opaque.h>
|
2012-02-15 19:48:01 +00:00
|
|
|
#include "iio_core.h"
|
2012-04-25 14:54:58 +00:00
|
|
|
#include <linux/iio/machine.h>
|
|
|
|
#include <linux/iio/driver.h>
|
|
|
|
#include <linux/iio/consumer.h>
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
struct iio_map_internal {
|
|
|
|
struct iio_dev *indio_dev;
|
|
|
|
struct iio_map *map;
|
|
|
|
struct list_head l;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(iio_map_list);
|
|
|
|
static DEFINE_MUTEX(iio_map_list_lock);
|
|
|
|
|
2020-11-28 13:44:18 +00:00
|
|
|
static int iio_map_array_unregister_locked(struct iio_dev *indio_dev)
|
|
|
|
{
|
|
|
|
int ret = -ENODEV;
|
|
|
|
struct iio_map_internal *mapi, *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(mapi, next, &iio_map_list, l) {
|
|
|
|
if (indio_dev == mapi->indio_dev) {
|
|
|
|
list_del(&mapi->l);
|
|
|
|
kfree(mapi);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-15 19:48:01 +00:00
|
|
|
int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
|
|
|
|
{
|
|
|
|
struct iio_map_internal *mapi;
|
2024-02-29 15:10:28 +00:00
|
|
|
int i = 0;
|
|
|
|
int ret;
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!maps)
|
2012-02-15 19:48:01 +00:00
|
|
|
return 0;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_map_list_lock);
|
2022-07-15 12:29:03 +00:00
|
|
|
while (maps[i].consumer_dev_name) {
|
2012-02-15 19:48:01 +00:00
|
|
|
mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!mapi) {
|
2012-02-15 19:48:01 +00:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error_ret;
|
|
|
|
}
|
|
|
|
mapi->map = &maps[i];
|
|
|
|
mapi->indio_dev = indio_dev;
|
2017-06-27 16:46:01 +00:00
|
|
|
list_add_tail(&mapi->l, &iio_map_list);
|
2012-02-15 19:48:01 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return 0;
|
|
|
|
error_ret:
|
|
|
|
iio_map_array_unregister_locked(indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_map_array_register);
|
|
|
|
|
2013-01-31 21:43:00 +00:00
|
|
|
/*
|
|
|
|
* Remove all map entries associated with the given iio device
|
2012-02-15 19:48:01 +00:00
|
|
|
*/
|
2013-01-31 21:43:00 +00:00
|
|
|
int iio_map_array_unregister(struct iio_dev *indio_dev)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_map_list_lock);
|
|
|
|
return iio_map_array_unregister_locked(indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_map_array_unregister);
|
|
|
|
|
2021-09-03 07:29:13 +00:00
|
|
|
static void iio_map_array_unregister_cb(void *indio_dev)
|
|
|
|
{
|
|
|
|
iio_map_array_unregister(indio_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int devm_iio_map_array_register(struct device *dev, struct iio_dev *indio_dev, struct iio_map *maps)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = iio_map_array_register(indio_dev, maps);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return devm_add_action_or_reset(dev, iio_map_array_unregister_cb, indio_dev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_iio_map_array_register);
|
|
|
|
|
2012-02-15 19:48:01 +00:00
|
|
|
static const struct iio_chan_spec
|
2012-05-01 20:04:24 +00:00
|
|
|
*iio_chan_spec_from_name(const struct iio_dev *indio_dev, const char *name)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const struct iio_chan_spec *chan = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < indio_dev->num_channels; i++)
|
|
|
|
if (indio_dev->channels[i].datasheet_name &&
|
|
|
|
strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
|
|
|
|
chan = &indio_dev->channels[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:29:43 +00:00
|
|
|
/**
|
2022-07-15 12:28:53 +00:00
|
|
|
* __fwnode_iio_simple_xlate - translate iiospec to the IIO channel index
|
2014-10-22 15:29:43 +00:00
|
|
|
* @indio_dev: pointer to the iio_dev structure
|
|
|
|
* @iiospec: IIO specifier as found in the device tree
|
|
|
|
*
|
|
|
|
* This is simple translation function, suitable for the most 1:1 mapped
|
|
|
|
* channels in IIO chips. This function performs only one sanity check:
|
|
|
|
* whether IIO index is less than num_channels (that is specified in the
|
|
|
|
* iio_dev).
|
|
|
|
*/
|
2022-07-15 12:28:53 +00:00
|
|
|
static int __fwnode_iio_simple_xlate(struct iio_dev *indio_dev,
|
|
|
|
const struct fwnode_reference_args *iiospec)
|
2014-10-22 15:29:43 +00:00
|
|
|
{
|
2022-07-15 12:28:53 +00:00
|
|
|
if (!iiospec->nargs)
|
2014-10-22 15:29:43 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-01-01 18:13:24 +00:00
|
|
|
if (iiospec->args[0] >= indio_dev->num_channels) {
|
2022-07-15 12:28:53 +00:00
|
|
|
dev_err(&indio_dev->dev, "invalid channel index %llu\n",
|
2015-01-01 18:13:24 +00:00
|
|
|
iiospec->args[0]);
|
2014-10-22 15:29:43 +00:00
|
|
|
return -EINVAL;
|
2015-01-01 18:13:24 +00:00
|
|
|
}
|
2014-10-22 15:29:43 +00:00
|
|
|
|
|
|
|
return iiospec->args[0];
|
|
|
|
}
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
static int __fwnode_iio_channel_get(struct iio_channel *channel,
|
|
|
|
struct fwnode_handle *fwnode, int index)
|
|
|
|
{
|
|
|
|
struct fwnode_reference_args iiospec;
|
2013-02-07 17:09:00 +00:00
|
|
|
struct device *idev;
|
|
|
|
struct iio_dev *indio_dev;
|
|
|
|
int err;
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
err = fwnode_property_get_reference_args(fwnode, "io-channels",
|
|
|
|
"#io-channel-cells", 0,
|
|
|
|
index, &iiospec);
|
2013-02-07 17:09:00 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
idev = bus_find_device_by_fwnode(&iio_bus_type, iiospec.fwnode);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!idev) {
|
2022-07-15 12:28:53 +00:00
|
|
|
fwnode_handle_put(iiospec.fwnode);
|
2013-02-07 17:09:00 +00:00
|
|
|
return -EPROBE_DEFER;
|
2022-07-15 12:28:49 +00:00
|
|
|
}
|
2013-02-07 17:09:00 +00:00
|
|
|
|
|
|
|
indio_dev = dev_to_iio_dev(idev);
|
|
|
|
channel->indio_dev = indio_dev;
|
2022-07-15 12:29:02 +00:00
|
|
|
if (indio_dev->info->fwnode_xlate)
|
2022-07-15 12:28:53 +00:00
|
|
|
index = indio_dev->info->fwnode_xlate(indio_dev, &iiospec);
|
2014-10-22 15:29:43 +00:00
|
|
|
else
|
2022-07-15 12:28:53 +00:00
|
|
|
index = __fwnode_iio_simple_xlate(indio_dev, &iiospec);
|
|
|
|
fwnode_handle_put(iiospec.fwnode);
|
2014-10-22 15:29:43 +00:00
|
|
|
if (index < 0)
|
2013-02-07 17:09:00 +00:00
|
|
|
goto err_put;
|
|
|
|
channel->channel = &indio_dev->channels[index];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_put:
|
|
|
|
iio_device_put(indio_dev);
|
2014-10-22 15:29:43 +00:00
|
|
|
return index;
|
2013-02-07 17:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
static struct iio_channel *fwnode_iio_channel_get(struct fwnode_handle *fwnode,
|
|
|
|
int index)
|
2013-02-07 17:09:00 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
struct iio_channel *channel __free(kfree) =
|
|
|
|
kzalloc(sizeof(*channel), GFP_KERNEL);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!channel)
|
2013-02-07 17:09:00 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
err = __fwnode_iio_channel_get(channel, fwnode, index);
|
2013-02-07 17:09:00 +00:00
|
|
|
if (err)
|
2024-02-29 15:10:28 +00:00
|
|
|
return ERR_PTR(err);
|
2013-02-07 17:09:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return_ptr(channel);
|
2013-02-07 17:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
static struct iio_channel *
|
|
|
|
__fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode, const char *name)
|
2022-07-15 12:28:52 +00:00
|
|
|
{
|
|
|
|
struct iio_channel *chan;
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For named iio channels, first look up the name in the
|
|
|
|
* "io-channel-names" property. If it cannot be found, the
|
2022-07-15 12:28:53 +00:00
|
|
|
* index will be an error code, and fwnode_iio_channel_get()
|
2022-07-15 12:28:52 +00:00
|
|
|
* will fail.
|
|
|
|
*/
|
|
|
|
if (name)
|
2022-07-15 12:28:53 +00:00
|
|
|
index = fwnode_property_match_string(fwnode, "io-channel-names",
|
|
|
|
name);
|
2022-07-15 12:28:52 +00:00
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
chan = fwnode_iio_channel_get(fwnode, index);
|
2022-07-15 12:28:52 +00:00
|
|
|
if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
|
|
|
|
return chan;
|
|
|
|
if (name) {
|
|
|
|
if (index >= 0) {
|
2022-07-15 12:28:53 +00:00
|
|
|
pr_err("ERROR: could not get IIO channel %pfw:%s(%i)\n",
|
|
|
|
fwnode, name, index);
|
2022-07-15 12:28:52 +00:00
|
|
|
/*
|
|
|
|
* In this case, we found 'name' in 'io-channel-names'
|
|
|
|
* but somehow we still fail so that we should not proceed
|
|
|
|
* with any other lookup. Hence, explicitly return -EINVAL
|
|
|
|
* (maybe not the better error code) so that the caller
|
|
|
|
* won't do a system lookup.
|
|
|
|
*/
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
/*
|
2022-07-15 12:28:53 +00:00
|
|
|
* If index < 0, then fwnode_property_get_reference_args() fails
|
|
|
|
* with -EINVAL or -ENOENT (ACPI case) which is expected. We
|
|
|
|
* should not proceed if we get any other error.
|
2022-07-15 12:28:52 +00:00
|
|
|
*/
|
2022-07-15 12:28:53 +00:00
|
|
|
if (PTR_ERR(chan) != -EINVAL && PTR_ERR(chan) != -ENOENT)
|
2022-07-15 12:28:52 +00:00
|
|
|
return chan;
|
|
|
|
} else if (PTR_ERR(chan) != -ENOENT) {
|
|
|
|
/*
|
|
|
|
* if !name, then we should only proceed the lookup if
|
2022-07-15 12:28:53 +00:00
|
|
|
* fwnode_property_get_reference_args() returns -ENOENT.
|
2022-07-15 12:28:52 +00:00
|
|
|
*/
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* so we continue the lookup */
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
}
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
struct iio_channel *fwnode_iio_channel_get_by_name(struct fwnode_handle *fwnode,
|
|
|
|
const char *name)
|
2013-02-07 17:09:00 +00:00
|
|
|
{
|
2022-07-15 12:28:53 +00:00
|
|
|
struct fwnode_handle *parent;
|
2022-07-15 12:28:51 +00:00
|
|
|
struct iio_channel *chan;
|
2013-02-07 17:09:00 +00:00
|
|
|
|
|
|
|
/* Walk up the tree of devices looking for a matching iio channel */
|
2022-07-15 12:28:53 +00:00
|
|
|
chan = __fwnode_iio_channel_get_by_name(fwnode, name);
|
2022-07-15 12:28:52 +00:00
|
|
|
if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV)
|
|
|
|
return chan;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No matching IIO channel found on this node.
|
|
|
|
* If the parent node has a "io-channel-ranges" property,
|
|
|
|
* then we can try one of its channels.
|
|
|
|
*/
|
2022-07-15 12:28:53 +00:00
|
|
|
fwnode_for_each_parent_node(fwnode, parent) {
|
|
|
|
if (!fwnode_property_present(parent, "io-channel-ranges")) {
|
|
|
|
fwnode_handle_put(parent);
|
2022-07-15 12:28:52 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
2022-07-15 12:28:53 +00:00
|
|
|
}
|
2013-02-07 17:09:00 +00:00
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
chan = __fwnode_iio_channel_get_by_name(fwnode, name);
|
|
|
|
if (!IS_ERR(chan) || PTR_ERR(chan) != -ENODEV) {
|
|
|
|
fwnode_handle_put(parent);
|
2022-07-15 12:28:52 +00:00
|
|
|
return chan;
|
2022-07-15 12:28:53 +00:00
|
|
|
}
|
2013-02-07 17:09:00 +00:00
|
|
|
}
|
2014-11-06 12:11:00 +00:00
|
|
|
|
2022-07-15 12:28:51 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
2013-02-07 17:09:00 +00:00
|
|
|
}
|
2022-07-15 12:28:53 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fwnode_iio_channel_get_by_name);
|
2013-02-07 17:09:00 +00:00
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
static struct iio_channel *fwnode_iio_channel_get_all(struct device *dev)
|
2013-02-07 17:09:00 +00:00
|
|
|
{
|
2022-07-15 12:28:53 +00:00
|
|
|
struct fwnode_handle *fwnode = dev_fwnode(dev);
|
2013-02-07 17:09:00 +00:00
|
|
|
int i, mapind, nummaps = 0;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do {
|
2022-07-15 12:28:53 +00:00
|
|
|
ret = fwnode_property_get_reference_args(fwnode, "io-channels",
|
|
|
|
"#io-channel-cells", 0,
|
|
|
|
nummaps, NULL);
|
2013-02-07 17:09:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
} while (++nummaps);
|
|
|
|
|
2022-07-15 12:28:51 +00:00
|
|
|
if (nummaps == 0)
|
|
|
|
return ERR_PTR(-ENODEV);
|
2013-02-07 17:09:00 +00:00
|
|
|
|
|
|
|
/* NULL terminated array to save passing size */
|
2024-02-29 15:10:28 +00:00
|
|
|
struct iio_channel *chans __free(kfree) =
|
|
|
|
kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!chans)
|
2013-02-07 17:09:00 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
/* Search for FW matches */
|
2013-02-07 17:09:00 +00:00
|
|
|
for (mapind = 0; mapind < nummaps; mapind++) {
|
2022-07-15 12:28:53 +00:00
|
|
|
ret = __fwnode_iio_channel_get(&chans[mapind], fwnode, mapind);
|
2013-02-07 17:09:00 +00:00
|
|
|
if (ret)
|
|
|
|
goto error_free_chans;
|
|
|
|
}
|
2024-02-29 15:10:28 +00:00
|
|
|
return_ptr(chans);
|
2013-02-07 17:09:00 +00:00
|
|
|
|
|
|
|
error_free_chans:
|
|
|
|
for (i = 0; i < mapind; i++)
|
|
|
|
iio_device_put(chans[i].indio_dev);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
2013-02-04 20:26:00 +00:00
|
|
|
static struct iio_channel *iio_channel_get_sys(const char *name,
|
|
|
|
const char *channel_name)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
|
|
|
struct iio_map_internal *c_i = NULL, *c = NULL;
|
2012-09-18 04:56:00 +00:00
|
|
|
int err;
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!(name || channel_name))
|
2012-02-15 19:48:01 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
|
|
|
/* first find matching entry the channel map */
|
2024-02-29 15:10:28 +00:00
|
|
|
scoped_guard(mutex, &iio_map_list_lock) {
|
|
|
|
list_for_each_entry(c_i, &iio_map_list, l) {
|
|
|
|
if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
|
|
|
|
(channel_name &&
|
|
|
|
strcmp(channel_name, c_i->map->consumer_channel) != 0))
|
|
|
|
continue;
|
|
|
|
c = c_i;
|
|
|
|
iio_device_get(c->indio_dev);
|
|
|
|
break;
|
|
|
|
}
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!c)
|
2012-02-15 19:48:01 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
struct iio_channel *channel __free(kfree) =
|
|
|
|
kzalloc(sizeof(*channel), GFP_KERNEL);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!channel) {
|
2012-09-18 04:56:00 +00:00
|
|
|
err = -ENOMEM;
|
2012-09-18 04:55:00 +00:00
|
|
|
goto error_no_mem;
|
2012-09-18 04:56:00 +00:00
|
|
|
}
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
channel->indio_dev = c->indio_dev;
|
|
|
|
|
2012-09-17 08:44:00 +00:00
|
|
|
if (c->map->adc_channel_label) {
|
2012-02-15 19:48:01 +00:00
|
|
|
channel->channel =
|
|
|
|
iio_chan_spec_from_name(channel->indio_dev,
|
|
|
|
c->map->adc_channel_label);
|
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!channel->channel) {
|
2012-09-18 04:56:00 +00:00
|
|
|
err = -EINVAL;
|
2024-02-29 15:10:28 +00:00
|
|
|
goto error_no_mem;
|
2012-09-18 04:56:00 +00:00
|
|
|
}
|
2012-09-17 08:44:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return_ptr(channel);
|
2012-09-17 08:44:00 +00:00
|
|
|
|
2012-09-18 04:55:00 +00:00
|
|
|
error_no_mem:
|
|
|
|
iio_device_put(c->indio_dev);
|
2012-09-18 04:56:00 +00:00
|
|
|
return ERR_PTR(err);
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
2013-02-04 20:26:00 +00:00
|
|
|
|
|
|
|
struct iio_channel *iio_channel_get(struct device *dev,
|
|
|
|
const char *channel_name)
|
|
|
|
{
|
|
|
|
const char *name = dev ? dev_name(dev) : NULL;
|
2013-02-07 17:09:00 +00:00
|
|
|
struct iio_channel *channel;
|
2013-02-04 20:26:00 +00:00
|
|
|
|
2013-02-07 17:09:00 +00:00
|
|
|
if (dev) {
|
2022-07-15 12:28:53 +00:00
|
|
|
channel = fwnode_iio_channel_get_by_name(dev_fwnode(dev),
|
|
|
|
channel_name);
|
2022-07-15 12:28:51 +00:00
|
|
|
if (!IS_ERR(channel) || PTR_ERR(channel) != -ENODEV)
|
2013-02-07 17:09:00 +00:00
|
|
|
return channel;
|
|
|
|
}
|
2014-11-06 12:11:00 +00:00
|
|
|
|
2013-02-04 20:26:00 +00:00
|
|
|
return iio_channel_get_sys(name, channel_name);
|
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_channel_get);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2012-05-01 20:04:24 +00:00
|
|
|
void iio_channel_release(struct iio_channel *channel)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2016-01-26 09:25:21 +00:00
|
|
|
if (!channel)
|
|
|
|
return;
|
2012-06-04 08:50:03 +00:00
|
|
|
iio_device_put(channel->indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
kfree(channel);
|
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_channel_release);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2021-04-08 11:38:16 +00:00
|
|
|
static void devm_iio_channel_free(void *iio_channel)
|
2016-04-06 10:31:06 +00:00
|
|
|
{
|
2021-04-08 11:38:16 +00:00
|
|
|
iio_channel_release(iio_channel);
|
2016-04-06 10:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct iio_channel *devm_iio_channel_get(struct device *dev,
|
|
|
|
const char *channel_name)
|
|
|
|
{
|
2021-04-08 11:38:16 +00:00
|
|
|
struct iio_channel *channel;
|
|
|
|
int ret;
|
2016-04-06 10:31:06 +00:00
|
|
|
|
|
|
|
channel = iio_channel_get(dev, channel_name);
|
2021-04-08 11:38:16 +00:00
|
|
|
if (IS_ERR(channel))
|
2016-04-06 10:31:06 +00:00
|
|
|
return channel;
|
|
|
|
|
2021-04-08 11:38:16 +00:00
|
|
|
ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2016-04-06 10:31:06 +00:00
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_get);
|
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
struct iio_channel *devm_fwnode_iio_channel_get_by_name(struct device *dev,
|
|
|
|
struct fwnode_handle *fwnode,
|
|
|
|
const char *channel_name)
|
2020-12-04 02:55:01 +00:00
|
|
|
{
|
2021-04-08 11:38:16 +00:00
|
|
|
struct iio_channel *channel;
|
|
|
|
int ret;
|
2020-12-04 02:55:01 +00:00
|
|
|
|
2022-07-15 12:28:53 +00:00
|
|
|
channel = fwnode_iio_channel_get_by_name(fwnode, channel_name);
|
2021-04-08 11:38:16 +00:00
|
|
|
if (IS_ERR(channel))
|
2020-12-04 02:55:01 +00:00
|
|
|
return channel;
|
|
|
|
|
2021-04-08 11:38:16 +00:00
|
|
|
ret = devm_add_action_or_reset(dev, devm_iio_channel_free, channel);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2020-12-04 02:55:01 +00:00
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
2022-07-15 12:28:53 +00:00
|
|
|
EXPORT_SYMBOL_GPL(devm_fwnode_iio_channel_get_by_name);
|
2020-12-04 02:55:01 +00:00
|
|
|
|
2013-01-31 21:43:00 +00:00
|
|
|
struct iio_channel *iio_channel_get_all(struct device *dev)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2013-01-31 21:43:00 +00:00
|
|
|
const char *name;
|
2012-02-15 19:48:01 +00:00
|
|
|
struct iio_map_internal *c = NULL;
|
2024-02-29 15:10:28 +00:00
|
|
|
struct iio_channel *fw_chans;
|
2012-02-15 19:48:01 +00:00
|
|
|
int nummaps = 0;
|
|
|
|
int mapind = 0;
|
|
|
|
int i, ret;
|
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!dev)
|
2012-02-15 19:48:01 +00:00
|
|
|
return ERR_PTR(-EINVAL);
|
2013-02-07 17:09:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
fw_chans = fwnode_iio_channel_get_all(dev);
|
2022-07-15 12:28:51 +00:00
|
|
|
/*
|
|
|
|
* We only want to carry on if the error is -ENODEV. Anything else
|
|
|
|
* should be reported up the stack.
|
|
|
|
*/
|
2024-02-29 15:10:28 +00:00
|
|
|
if (!IS_ERR(fw_chans) || PTR_ERR(fw_chans) != -ENODEV)
|
|
|
|
return fw_chans;
|
2013-02-07 17:09:00 +00:00
|
|
|
|
2013-01-31 21:43:00 +00:00
|
|
|
name = dev_name(dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_map_list_lock);
|
2012-02-15 19:48:01 +00:00
|
|
|
/* first count the matching maps */
|
|
|
|
list_for_each_entry(c, &iio_map_list, l)
|
|
|
|
if (name && strcmp(name, c->map->consumer_dev_name) != 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
nummaps++;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
if (nummaps == 0)
|
|
|
|
return ERR_PTR(-ENODEV);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
/* NULL terminated array to save passing size */
|
2024-02-29 15:10:28 +00:00
|
|
|
struct iio_channel *chans __free(kfree) =
|
|
|
|
kcalloc(nummaps + 1, sizeof(*chans), GFP_KERNEL);
|
|
|
|
if (!chans)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
/* for each map fill in the chans element */
|
|
|
|
list_for_each_entry(c, &iio_map_list, l) {
|
|
|
|
if (name && strcmp(name, c->map->consumer_dev_name) != 0)
|
|
|
|
continue;
|
|
|
|
chans[mapind].indio_dev = c->indio_dev;
|
2012-06-30 19:06:00 +00:00
|
|
|
chans[mapind].data = c->map->consumer_data;
|
2012-02-15 19:48:01 +00:00
|
|
|
chans[mapind].channel =
|
|
|
|
iio_chan_spec_from_name(chans[mapind].indio_dev,
|
|
|
|
c->map->adc_channel_label);
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!chans[mapind].channel) {
|
2012-02-15 19:48:01 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto error_free_chans;
|
|
|
|
}
|
2012-06-04 08:50:03 +00:00
|
|
|
iio_device_get(chans[mapind].indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
mapind++;
|
|
|
|
}
|
|
|
|
if (mapind == 0) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto error_free_chans;
|
|
|
|
}
|
2012-07-11 06:34:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return_ptr(chans);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
error_free_chans:
|
|
|
|
for (i = 0; i < nummaps; i++)
|
2012-06-04 08:50:03 +00:00
|
|
|
iio_device_put(chans[i].indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_channel_get_all);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2012-05-01 20:04:24 +00:00
|
|
|
void iio_channel_release_all(struct iio_channel *channels)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
|
|
|
struct iio_channel *chan = &channels[0];
|
|
|
|
|
|
|
|
while (chan->indio_dev) {
|
2012-06-04 08:50:03 +00:00
|
|
|
iio_device_put(chan->indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
chan++;
|
|
|
|
}
|
|
|
|
kfree(channels);
|
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_channel_release_all);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2021-04-08 11:38:16 +00:00
|
|
|
static void devm_iio_channel_free_all(void *iio_channels)
|
2016-04-06 10:31:07 +00:00
|
|
|
{
|
2021-04-08 11:38:16 +00:00
|
|
|
iio_channel_release_all(iio_channels);
|
2016-04-06 10:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct iio_channel *devm_iio_channel_get_all(struct device *dev)
|
|
|
|
{
|
2021-04-08 11:38:16 +00:00
|
|
|
struct iio_channel *channels;
|
|
|
|
int ret;
|
2016-04-06 10:31:07 +00:00
|
|
|
|
|
|
|
channels = iio_channel_get_all(dev);
|
2021-04-08 11:38:16 +00:00
|
|
|
if (IS_ERR(channels))
|
2016-04-06 10:31:07 +00:00
|
|
|
return channels;
|
|
|
|
|
2021-04-08 11:38:16 +00:00
|
|
|
ret = devm_add_action_or_reset(dev, devm_iio_channel_free_all,
|
|
|
|
channels);
|
|
|
|
if (ret)
|
|
|
|
return ERR_PTR(ret);
|
2016-04-06 10:31:07 +00:00
|
|
|
|
|
|
|
return channels;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_iio_channel_get_all);
|
|
|
|
|
2012-09-17 12:17:00 +00:00
|
|
|
static int iio_channel_read(struct iio_channel *chan, int *val, int *val2,
|
2022-07-15 12:29:03 +00:00
|
|
|
enum iio_chan_info_enum info)
|
2012-09-17 12:17:00 +00:00
|
|
|
{
|
2024-05-30 09:22:46 +00:00
|
|
|
const struct iio_info *iio_info = chan->indio_dev->info;
|
2012-09-17 12:17:00 +00:00
|
|
|
int unused;
|
2014-04-28 23:51:00 +00:00
|
|
|
int vals[INDIO_MAX_RAW_ELEMENTS];
|
|
|
|
int ret;
|
|
|
|
int val_len = 2;
|
2012-09-17 12:17:00 +00:00
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
if (!val2)
|
2012-09-17 12:17:00 +00:00
|
|
|
val2 = &unused;
|
|
|
|
|
2016-04-14 19:36:34 +00:00
|
|
|
if (!iio_channel_has_info(chan->channel, info))
|
2015-01-01 12:46:48 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2024-05-30 09:22:46 +00:00
|
|
|
if (iio_info->read_raw_multi) {
|
|
|
|
ret = iio_info->read_raw_multi(chan->indio_dev,
|
|
|
|
chan->channel,
|
|
|
|
INDIO_MAX_RAW_ELEMENTS,
|
|
|
|
vals, &val_len, info);
|
2014-04-28 23:51:00 +00:00
|
|
|
*val = vals[0];
|
|
|
|
*val2 = vals[1];
|
2024-05-30 09:22:46 +00:00
|
|
|
} else if (iio_info->read_raw) {
|
|
|
|
ret = iio_info->read_raw(chan->indio_dev,
|
|
|
|
chan->channel, val, val2, info);
|
2022-07-15 12:29:03 +00:00
|
|
|
} else {
|
2024-05-30 09:22:46 +00:00
|
|
|
return -EINVAL;
|
2022-07-15 12:29:03 +00:00
|
|
|
}
|
2014-04-28 23:51:00 +00:00
|
|
|
|
|
|
|
return ret;
|
2012-09-17 12:17:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:04:24 +00:00
|
|
|
int iio_read_channel_raw(struct iio_channel *chan, int *val)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_raw);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2014-10-03 16:25:00 +00:00
|
|
|
int iio_read_channel_average_raw(struct iio_channel *chan, int *val)
|
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2014-10-03 16:25:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2014-10-03 16:25:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_AVERAGE_RAW);
|
2014-10-03 16:25:00 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
|
|
|
|
|
2012-09-17 12:17:00 +00:00
|
|
|
static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
|
2022-07-15 12:29:03 +00:00
|
|
|
int raw, int *processed,
|
|
|
|
unsigned int scale)
|
2012-09-17 12:17:00 +00:00
|
|
|
{
|
2022-01-08 20:53:06 +00:00
|
|
|
int scale_type, scale_val, scale_val2;
|
|
|
|
int offset_type, offset_val, offset_val2;
|
2012-09-17 12:17:00 +00:00
|
|
|
s64 raw64 = raw;
|
|
|
|
|
2022-01-08 20:53:06 +00:00
|
|
|
offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
|
|
|
|
IIO_CHAN_INFO_OFFSET);
|
|
|
|
if (offset_type >= 0) {
|
|
|
|
switch (offset_type) {
|
|
|
|
case IIO_VAL_INT:
|
|
|
|
break;
|
|
|
|
case IIO_VAL_INT_PLUS_MICRO:
|
|
|
|
case IIO_VAL_INT_PLUS_NANO:
|
|
|
|
/*
|
|
|
|
* Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
|
|
|
|
* implicitely truncate the offset to it's integer form.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case IIO_VAL_FRACTIONAL:
|
|
|
|
offset_val /= offset_val2;
|
|
|
|
break;
|
|
|
|
case IIO_VAL_FRACTIONAL_LOG2:
|
|
|
|
offset_val >>= offset_val2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw64 += offset_val;
|
|
|
|
}
|
2012-09-17 12:17:00 +00:00
|
|
|
|
|
|
|
scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
|
2022-07-15 12:29:03 +00:00
|
|
|
IIO_CHAN_INFO_SCALE);
|
2017-01-11 10:48:39 +00:00
|
|
|
if (scale_type < 0) {
|
|
|
|
/*
|
2022-01-08 20:53:05 +00:00
|
|
|
* If no channel scaling is available apply consumer scale to
|
|
|
|
* raw value and return.
|
2017-01-11 10:48:39 +00:00
|
|
|
*/
|
2022-01-08 20:53:05 +00:00
|
|
|
*processed = raw * scale;
|
2017-01-11 10:48:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-09-17 12:17:00 +00:00
|
|
|
|
|
|
|
switch (scale_type) {
|
|
|
|
case IIO_VAL_INT:
|
2022-01-08 20:53:04 +00:00
|
|
|
*processed = raw64 * scale_val * scale;
|
2012-09-17 12:17:00 +00:00
|
|
|
break;
|
|
|
|
case IIO_VAL_INT_PLUS_MICRO:
|
|
|
|
if (scale_val2 < 0)
|
2024-07-30 08:11:53 +00:00
|
|
|
*processed = -raw64 * scale_val * scale;
|
2012-09-17 12:17:00 +00:00
|
|
|
else
|
2024-07-30 08:11:53 +00:00
|
|
|
*processed = raw64 * scale_val * scale;
|
2012-09-17 12:17:00 +00:00
|
|
|
*processed += div_s64(raw64 * (s64)scale_val2 * scale,
|
|
|
|
1000000LL);
|
|
|
|
break;
|
|
|
|
case IIO_VAL_INT_PLUS_NANO:
|
|
|
|
if (scale_val2 < 0)
|
2024-07-30 08:11:53 +00:00
|
|
|
*processed = -raw64 * scale_val * scale;
|
2012-09-17 12:17:00 +00:00
|
|
|
else
|
2024-07-30 08:11:53 +00:00
|
|
|
*processed = raw64 * scale_val * scale;
|
2012-09-17 12:17:00 +00:00
|
|
|
*processed += div_s64(raw64 * (s64)scale_val2 * scale,
|
|
|
|
1000000000LL);
|
|
|
|
break;
|
|
|
|
case IIO_VAL_FRACTIONAL:
|
|
|
|
*processed = div_s64(raw64 * (s64)scale_val * scale,
|
|
|
|
scale_val2);
|
|
|
|
break;
|
2012-10-16 16:29:00 +00:00
|
|
|
case IIO_VAL_FRACTIONAL_LOG2:
|
|
|
|
*processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
|
|
|
|
break;
|
2012-09-17 12:17:00 +00:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int iio_convert_raw_to_processed(struct iio_channel *chan, int raw,
|
2022-07-15 12:29:03 +00:00
|
|
|
int *processed, unsigned int scale)
|
2012-09-17 12:17:00 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2012-09-17 12:17:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2012-09-17 12:17:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_convert_raw_to_processed_unlocked(chan, raw, processed,
|
|
|
|
scale);
|
2012-09-17 12:17:00 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_convert_raw_to_processed);
|
|
|
|
|
2018-01-10 10:13:06 +00:00
|
|
|
int iio_read_channel_attribute(struct iio_channel *chan, int *val, int *val2,
|
|
|
|
enum iio_chan_info_enum attribute)
|
2016-09-24 06:04:07 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2016-09-24 06:04:07 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2016-09-24 06:04:07 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read(chan, val, val2, attribute);
|
2016-09-24 06:04:07 +00:00
|
|
|
}
|
2018-01-10 10:13:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_attribute);
|
2016-09-24 06:04:07 +00:00
|
|
|
|
|
|
|
int iio_read_channel_offset(struct iio_channel *chan, int *val, int *val2)
|
|
|
|
{
|
|
|
|
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_OFFSET);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_offset);
|
|
|
|
|
2021-03-08 10:02:18 +00:00
|
|
|
int iio_read_channel_processed_scale(struct iio_channel *chan, int *val,
|
|
|
|
unsigned int scale)
|
2012-09-17 12:17:00 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2012-09-17 12:17:00 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2012-09-17 12:17:00 +00:00
|
|
|
|
|
|
|
if (iio_channel_has_info(chan->channel, IIO_CHAN_INFO_PROCESSED)) {
|
|
|
|
ret = iio_channel_read(chan, val, NULL,
|
|
|
|
IIO_CHAN_INFO_PROCESSED);
|
2021-03-23 12:27:05 +00:00
|
|
|
if (ret < 0)
|
2024-02-29 15:10:28 +00:00
|
|
|
return ret;
|
2021-03-08 10:02:18 +00:00
|
|
|
*val *= scale;
|
2024-02-29 15:10:28 +00:00
|
|
|
|
2024-05-30 07:44:16 +00:00
|
|
|
return ret;
|
2012-09-17 12:17:00 +00:00
|
|
|
} else {
|
|
|
|
ret = iio_channel_read(chan, val, NULL, IIO_CHAN_INFO_RAW);
|
|
|
|
if (ret < 0)
|
2024-02-29 15:10:28 +00:00
|
|
|
return ret;
|
2012-09-17 12:17:00 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_convert_raw_to_processed_unlocked(chan, *val, val,
|
|
|
|
scale);
|
|
|
|
}
|
2012-09-17 12:17:00 +00:00
|
|
|
}
|
2021-03-08 10:02:18 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_processed_scale);
|
|
|
|
|
|
|
|
int iio_read_channel_processed(struct iio_channel *chan, int *val)
|
|
|
|
{
|
|
|
|
/* This is just a special case with scale factor 1 */
|
|
|
|
return iio_read_channel_processed_scale(chan, val, 1);
|
|
|
|
}
|
2012-09-17 12:17:00 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_processed);
|
|
|
|
|
2012-05-01 20:04:24 +00:00
|
|
|
int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2016-09-24 06:04:07 +00:00
|
|
|
return iio_read_channel_attribute(chan, val, val2, IIO_CHAN_INFO_SCALE);
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_scale);
|
2016-11-08 11:58:52 +00:00
|
|
|
|
|
|
|
static int iio_channel_read_avail(struct iio_channel *chan,
|
|
|
|
const int **vals, int *type, int *length,
|
|
|
|
enum iio_chan_info_enum info)
|
|
|
|
{
|
2024-05-30 09:22:46 +00:00
|
|
|
const struct iio_info *iio_info = chan->indio_dev->info;
|
|
|
|
|
2016-11-08 11:58:52 +00:00
|
|
|
if (!iio_channel_has_available(chan->channel, info))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2024-05-30 09:22:46 +00:00
|
|
|
if (iio_info->read_avail)
|
|
|
|
return iio_info->read_avail(chan->indio_dev, chan->channel,
|
|
|
|
vals, type, length, info);
|
|
|
|
return -EINVAL;
|
2016-11-08 11:58:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 17:28:06 +00:00
|
|
|
int iio_read_avail_channel_attribute(struct iio_channel *chan,
|
|
|
|
const int **vals, int *type, int *length,
|
|
|
|
enum iio_chan_info_enum attribute)
|
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2019-03-23 17:28:06 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2019-03-23 17:28:06 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read_avail(chan, vals, type, length, attribute);
|
2019-03-23 17:28:06 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_avail_channel_attribute);
|
|
|
|
|
2016-11-08 11:58:52 +00:00
|
|
|
int iio_read_avail_channel_raw(struct iio_channel *chan,
|
|
|
|
const int **vals, int *length)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int type;
|
|
|
|
|
2019-03-23 17:28:07 +00:00
|
|
|
ret = iio_read_avail_channel_attribute(chan, vals, &type, length,
|
2022-07-15 12:29:03 +00:00
|
|
|
IIO_CHAN_INFO_RAW);
|
2016-11-08 11:58:52 +00:00
|
|
|
|
2017-04-20 21:01:57 +00:00
|
|
|
if (ret >= 0 && type != IIO_VAL_INT)
|
2016-11-08 11:58:52 +00:00
|
|
|
/* raw values are assumed to be IIO_VAL_INT */
|
|
|
|
ret = -EINVAL;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_avail_channel_raw);
|
|
|
|
|
|
|
|
static int iio_channel_read_max(struct iio_channel *chan,
|
|
|
|
int *val, int *val2, int *type,
|
|
|
|
enum iio_chan_info_enum info)
|
|
|
|
{
|
|
|
|
const int *vals;
|
|
|
|
int length;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = iio_channel_read_avail(chan, &vals, type, &length, info);
|
2023-06-23 08:58:20 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2016-11-08 11:58:52 +00:00
|
|
|
switch (ret) {
|
|
|
|
case IIO_AVAIL_RANGE:
|
|
|
|
switch (*type) {
|
|
|
|
case IIO_VAL_INT:
|
|
|
|
*val = vals[2];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*val = vals[4];
|
2023-06-23 08:58:22 +00:00
|
|
|
if (val2)
|
|
|
|
*val2 = vals[5];
|
2016-11-08 11:58:52 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IIO_AVAIL_LIST:
|
|
|
|
if (length <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
switch (*type) {
|
|
|
|
case IIO_VAL_INT:
|
2023-06-23 08:58:25 +00:00
|
|
|
*val = max_array(vals, length);
|
2016-11-08 11:58:52 +00:00
|
|
|
break;
|
|
|
|
default:
|
2023-06-23 08:58:26 +00:00
|
|
|
/* TODO: learn about max for other iio values */
|
2016-11-08 11:58:52 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
2023-06-23 08:58:20 +00:00
|
|
|
return -EINVAL;
|
2016-11-08 11:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iio_read_max_channel_raw(struct iio_channel *chan, int *val)
|
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2016-11-08 11:58:52 +00:00
|
|
|
int type;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2016-11-08 11:58:52 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read_max(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
|
2016-11-08 11:58:52 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_max_channel_raw);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2023-06-23 08:58:27 +00:00
|
|
|
static int iio_channel_read_min(struct iio_channel *chan,
|
|
|
|
int *val, int *val2, int *type,
|
|
|
|
enum iio_chan_info_enum info)
|
|
|
|
{
|
|
|
|
const int *vals;
|
|
|
|
int length;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = iio_channel_read_avail(chan, &vals, type, &length, info);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case IIO_AVAIL_RANGE:
|
|
|
|
switch (*type) {
|
|
|
|
case IIO_VAL_INT:
|
|
|
|
*val = vals[0];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*val = vals[0];
|
|
|
|
if (val2)
|
|
|
|
*val2 = vals[1];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IIO_AVAIL_LIST:
|
|
|
|
if (length <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
switch (*type) {
|
|
|
|
case IIO_VAL_INT:
|
|
|
|
*val = min_array(vals, length);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TODO: learn about min for other iio values */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iio_read_min_channel_raw(struct iio_channel *chan, int *val)
|
|
|
|
{
|
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
|
|
|
int type;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2023-06-23 08:58:27 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_read_min(chan, val, NULL, &type, IIO_CHAN_INFO_RAW);
|
2023-06-23 08:58:27 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_min_channel_raw);
|
|
|
|
|
2012-05-01 20:04:24 +00:00
|
|
|
int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
|
2012-02-15 19:48:01 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2012-02-15 19:48:01 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2012-02-15 19:48:01 +00:00
|
|
|
|
|
|
|
*type = chan->channel->type;
|
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return 0;
|
2012-02-15 19:48:01 +00:00
|
|
|
}
|
2012-05-01 20:04:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_get_channel_type);
|
2014-11-26 22:42:45 +00:00
|
|
|
|
|
|
|
static int iio_channel_write(struct iio_channel *chan, int val, int val2,
|
|
|
|
enum iio_chan_info_enum info)
|
|
|
|
{
|
2024-05-30 09:22:46 +00:00
|
|
|
const struct iio_info *iio_info = chan->indio_dev->info;
|
|
|
|
|
|
|
|
if (iio_info->write_raw)
|
|
|
|
return iio_info->write_raw(chan->indio_dev,
|
|
|
|
chan->channel, val, val2, info);
|
|
|
|
return -EINVAL;
|
2014-11-26 22:42:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 10:13:06 +00:00
|
|
|
int iio_write_channel_attribute(struct iio_channel *chan, int val, int val2,
|
|
|
|
enum iio_chan_info_enum attribute)
|
2014-11-26 22:42:45 +00:00
|
|
|
{
|
2021-04-26 17:49:08 +00:00
|
|
|
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(chan->indio_dev);
|
2014-11-26 22:42:45 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
guard(mutex)(&iio_dev_opaque->info_exist_lock);
|
|
|
|
if (!chan->indio_dev->info)
|
|
|
|
return -ENODEV;
|
2014-11-26 22:42:45 +00:00
|
|
|
|
2024-02-29 15:10:28 +00:00
|
|
|
return iio_channel_write(chan, val, val2, attribute);
|
2014-11-26 22:42:45 +00:00
|
|
|
}
|
2018-01-10 10:13:06 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_write_channel_attribute);
|
|
|
|
|
|
|
|
int iio_write_channel_raw(struct iio_channel *chan, int val)
|
|
|
|
{
|
|
|
|
return iio_write_channel_attribute(chan, val, 0, IIO_CHAN_INFO_RAW);
|
|
|
|
}
|
2014-11-26 22:42:45 +00:00
|
|
|
EXPORT_SYMBOL_GPL(iio_write_channel_raw);
|
2017-05-14 19:51:08 +00:00
|
|
|
|
|
|
|
unsigned int iio_get_channel_ext_info_count(struct iio_channel *chan)
|
|
|
|
{
|
|
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
if (!chan->channel->ext_info)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
for (ext_info = chan->channel->ext_info; ext_info->name; ext_info++)
|
|
|
|
++i;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_get_channel_ext_info_count);
|
|
|
|
|
2022-07-15 12:29:03 +00:00
|
|
|
static const struct iio_chan_spec_ext_info *
|
|
|
|
iio_lookup_ext_info(const struct iio_channel *chan, const char *attr)
|
2017-05-14 19:51:08 +00:00
|
|
|
{
|
|
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
|
|
|
|
if (!chan->channel->ext_info)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (ext_info = chan->channel->ext_info; ext_info->name; ++ext_info) {
|
|
|
|
if (!strcmp(attr, ext_info->name))
|
|
|
|
return ext_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t iio_read_channel_ext_info(struct iio_channel *chan,
|
|
|
|
const char *attr, char *buf)
|
|
|
|
{
|
|
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
|
|
|
|
ext_info = iio_lookup_ext_info(chan, attr);
|
|
|
|
if (!ext_info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return ext_info->read(chan->indio_dev, ext_info->private,
|
|
|
|
chan->channel, buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_ext_info);
|
|
|
|
|
|
|
|
ssize_t iio_write_channel_ext_info(struct iio_channel *chan, const char *attr,
|
|
|
|
const char *buf, size_t len)
|
|
|
|
{
|
|
|
|
const struct iio_chan_spec_ext_info *ext_info;
|
|
|
|
|
|
|
|
ext_info = iio_lookup_ext_info(chan, attr);
|
|
|
|
if (!ext_info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return ext_info->write(chan->indio_dev, ext_info->private,
|
|
|
|
chan->channel, buf, len);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_write_channel_ext_info);
|
2024-06-24 17:46:00 +00:00
|
|
|
|
|
|
|
ssize_t iio_read_channel_label(struct iio_channel *chan, char *buf)
|
|
|
|
{
|
|
|
|
return do_iio_read_channel_label(chan->indio_dev, chan->channel, buf);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(iio_read_channel_label);
|