summaryrefslogtreecommitdiff
path: root/drivers/iio/inkern.c
diff options
context:
space:
mode:
authorNuno Sá <nuno.sa@analog.com>2022-07-15 14:28:52 +0200
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2022-08-15 22:29:59 +0100
commitd6bb09eab2b3c4c55e5f6006cf8e759439e3e741 (patch)
treed0a60f1f8174d741f24d9fd37d6057979fdacd47 /drivers/iio/inkern.c
parented5e5ed4e377599825d32162a6ff3e9d2d89d200 (diff)
iio: inkern: split of_iio_channel_get_by_name()
This change splits of_iio_channel_get_by_name() so that it decouples looking for channels in the current node from looking in it's parents nodes. This will be helpful when moving to fwnode properties where we need to release the handles when looking for channels in parent's nodes. No functional change intended... Signed-off-by: Nuno Sá <nuno.sa@analog.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Link: https://lore.kernel.org/r/20220715122903.332535-5-nuno.sa@analog.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Diffstat (limited to 'drivers/iio/inkern.c')
-rw-r--r--drivers/iio/inkern.c112
1 files changed, 65 insertions, 47 deletions
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 49913660e087..9e0c7cc3a093 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -211,63 +211,81 @@ err_free_channel:
return ERR_PTR(err);
}
+static struct iio_channel *__of_iio_channel_get_by_name(struct device_node *np,
+ const char *name)
+{
+ 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
+ * index will be an error code, and of_iio_channel_get()
+ * will fail.
+ */
+ if (name)
+ index = of_property_match_string(np, "io-channel-names", name);
+
+ chan = of_iio_channel_get(np, index);
+ if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
+ return chan;
+ if (name) {
+ if (index >= 0) {
+ pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
+ np, name, index);
+ /*
+ * 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);
+ }
+ /*
+ * If index < 0, then of_parse_phandle_with_args() fails
+ * with -EINVAL which is expected. We should not proceed
+ * if we get any other error.
+ */
+ if (PTR_ERR(chan) != -EINVAL)
+ return chan;
+ } else if (PTR_ERR(chan) != -ENOENT) {
+ /*
+ * if !name, then we should only proceed the lookup if
+ * of_parse_phandle_with_args() returns -ENOENT.
+ */
+ return chan;
+ }
+
+ /* so we continue the lookup */
+ return ERR_PTR(-ENODEV);
+}
+
struct iio_channel *of_iio_channel_get_by_name(struct device_node *np,
const char *name)
{
struct iio_channel *chan;
/* Walk up the tree of devices looking for a matching iio channel */
+ chan = __of_iio_channel_get_by_name(np, name);
+ 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.
+ */
+ np = np->parent;
while (np) {
- int index = 0;
+ if (!of_get_property(np, "io-channel-ranges", NULL))
+ return ERR_PTR(-ENODEV);
- /*
- * For named iio channels, first look up the name in the
- * "io-channel-names" property. If it cannot be found, the
- * index will be an error code, and of_iio_channel_get()
- * will fail.
- */
- if (name)
- index = of_property_match_string(np, "io-channel-names",
- name);
- chan = of_iio_channel_get(np, index);
- if (!IS_ERR(chan) || PTR_ERR(chan) == -EPROBE_DEFER)
- return chan;
- if (name) {
- if (index >= 0) {
- pr_err("ERROR: could not get IIO channel %pOF:%s(%i)\n",
- np, name, index);
- /*
- * 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);
- }
- /*
- * If index < 0, then of_parse_phandle_with_args() fails
- * with -EINVAL which is expected. We should not proceed
- * if we get any other error.
- */
- if (PTR_ERR(chan) != -EINVAL)
- return chan;
- } else if (PTR_ERR(chan) != -ENOENT) {
- /*
- * if !name, then we should only proceed the lookup if
- * of_parse_phandle_with_args() returns -ENOENT.
- */
- return chan;
- }
+ chan = __of_iio_channel_get_by_name(np, name);
+ 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.
- */
np = np->parent;
- if (np && !of_get_property(np, "io-channel-ranges", NULL))
- return ERR_PTR(-ENODEV);
}
return ERR_PTR(-ENODEV);