]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
regulator: Move OF-specific regulator lookup code to of_regulator.c
authorChen-Yu Tsai <wenst@chromium.org>
Wed, 4 Sep 2024 09:00:05 +0000 (17:00 +0800)
committerMark Brown <broonie@kernel.org>
Wed, 4 Sep 2024 12:15:21 +0000 (13:15 +0100)
There's still a bit of OF-specific code in the regulator device lookup
function.

Move those bits of code over to of_regulator.c, and create a new
function of_regulator_dev_lookup() to encapsulate the code moved out of
regulator_dev_lookup().

Also mark of_find_regulator_by_node() as static, since there are no
other users in other compile units.

There are no functional changes. A line alignment was also fixed.

Signed-off-by: Chen-Yu Tsai <wenst@chromium.org>
Link: https://patch.msgid.link/20240904090016.2841572-4-wenst@chromium.org
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/regulator/core.c
drivers/regulator/internal.h
drivers/regulator/of_regulator.c

index c1d11924d8921265078d59f0a5171ac05875a48c..835a5531d045847eb75c1c5d5cfc1599a0b08b8d 100644 (file)
@@ -421,74 +421,6 @@ static void regulator_lock_dependent(struct regulator_dev *rdev,
        mutex_unlock(&regulator_list_mutex);
 }
 
-/**
- * of_get_child_regulator - get a child regulator device node
- * based on supply name
- * @parent: Parent device node
- * @prop_name: Combination regulator supply name and "-supply"
- *
- * Traverse all child nodes.
- * Extract the child regulator device node corresponding to the supply name.
- *
- * Return: Pointer to the &struct device_node corresponding to the regulator
- *        if found, or %NULL if not found.
- */
-static struct device_node *of_get_child_regulator(struct device_node *parent,
-                                                 const char *prop_name)
-{
-       struct device_node *regnode = NULL;
-       struct device_node *child = NULL;
-
-       for_each_child_of_node(parent, child) {
-               regnode = of_parse_phandle(child, prop_name, 0);
-
-               if (!regnode) {
-                       regnode = of_get_child_regulator(child, prop_name);
-                       if (regnode)
-                               goto err_node_put;
-               } else {
-                       goto err_node_put;
-               }
-       }
-       return NULL;
-
-err_node_put:
-       of_node_put(child);
-       return regnode;
-}
-
-/**
- * of_get_regulator - get a regulator device node based on supply name
- * @dev: Device pointer for the consumer (of regulator) device
- * @supply: regulator supply name
- *
- * Extract the regulator device node corresponding to the supply name.
- *
- * Return: Pointer to the &struct device_node corresponding to the regulator
- *        if found, or %NULL if not found.
- */
-static struct device_node *of_get_regulator(struct device *dev, const char *supply)
-{
-       struct device_node *regnode = NULL;
-       char prop_name[64]; /* 64 is max size of property name */
-
-       dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
-
-       snprintf(prop_name, 64, "%s-supply", supply);
-       regnode = of_parse_phandle(dev->of_node, prop_name, 0);
-
-       if (!regnode) {
-               regnode = of_get_child_regulator(dev->of_node, prop_name);
-               if (regnode)
-                       return regnode;
-
-               dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
-                               prop_name, dev->of_node);
-               return NULL;
-       }
-       return regnode;
-}
-
 /* Platform voltage constraint check */
 int regulator_check_voltage(struct regulator_dev *rdev,
                            int *min_uV, int *max_uV)
@@ -2021,7 +1953,6 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
                                                  const char *supply)
 {
        struct regulator_dev *r = NULL;
-       struct device_node *node;
        struct regulator_map *map;
        const char *devname = NULL;
 
@@ -2029,19 +1960,11 @@ static struct regulator_dev *regulator_dev_lookup(struct device *dev,
 
        /* first do a dt based lookup */
        if (dev && dev->of_node) {
-               node = of_get_regulator(dev, supply);
-               if (node) {
-                       r = of_find_regulator_by_node(node);
-                       of_node_put(node);
-                       if (r)
-                               return r;
-
-                       /*
-                        * We have a node, but there is no device.
-                        * assume it has not registered yet.
-                        */
-                       return ERR_PTR(-EPROBE_DEFER);
-               }
+               r = of_regulator_dev_lookup(dev, supply);
+               if (!IS_ERR(r))
+                       return r;
+               if (PTR_ERR(r) == -EPROBE_DEFER)
+                       return r;
        }
 
        /* if not found, try doing it non-dt way */
index 77a502141089638be260e3d3ad4d072f97c59ec2..8e5506c5ee9484ab49a2597bc859a0890f525137 100644 (file)
@@ -66,7 +66,8 @@ static inline struct regulator_dev *dev_to_rdev(struct device *dev)
 }
 
 #ifdef CONFIG_OF
-struct regulator_dev *of_find_regulator_by_node(struct device_node *np);
+struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+                                             const char *supply);
 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
                                 const struct regulator_desc *desc,
                                 struct regulator_config *config,
@@ -80,10 +81,10 @@ int of_get_n_coupled(struct regulator_dev *rdev);
 bool of_check_coupling_data(struct regulator_dev *rdev);
 
 #else
-static inline struct regulator_dev *
-of_find_regulator_by_node(struct device_node *np)
+static inline struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+                                                           const char *supply)
 {
-       return NULL;
+       return ERR_PTR(-ENODEV);
 }
 
 static inline struct regulator_init_data *
index cec8c3647a00a1fb0861e0c8080f9f2bf6c8926b..d5dd7a9e577b60aa0d3c903b296f689591330529 100644 (file)
@@ -552,7 +552,75 @@ error:
        return NULL;
 }
 
-struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
+/**
+ * of_get_child_regulator - get a child regulator device node
+ * based on supply name
+ * @parent: Parent device node
+ * @prop_name: Combination regulator supply name and "-supply"
+ *
+ * Traverse all child nodes.
+ * Extract the child regulator device node corresponding to the supply name.
+ *
+ * Return: Pointer to the &struct device_node corresponding to the regulator
+ *        if found, or %NULL if not found.
+ */
+static struct device_node *of_get_child_regulator(struct device_node *parent,
+                                                 const char *prop_name)
+{
+       struct device_node *regnode = NULL;
+       struct device_node *child = NULL;
+
+       for_each_child_of_node(parent, child) {
+               regnode = of_parse_phandle(child, prop_name, 0);
+
+               if (!regnode) {
+                       regnode = of_get_child_regulator(child, prop_name);
+                       if (regnode)
+                               goto err_node_put;
+               } else {
+                       goto err_node_put;
+               }
+       }
+       return NULL;
+
+err_node_put:
+       of_node_put(child);
+       return regnode;
+}
+
+/**
+ * of_get_regulator - get a regulator device node based on supply name
+ * @dev: Device pointer for the consumer (of regulator) device
+ * @supply: regulator supply name
+ *
+ * Extract the regulator device node corresponding to the supply name.
+ *
+ * Return: Pointer to the &struct device_node corresponding to the regulator
+ *        if found, or %NULL if not found.
+ */
+static struct device_node *of_get_regulator(struct device *dev, const char *supply)
+{
+       struct device_node *regnode = NULL;
+       char prop_name[64]; /* 64 is max size of property name */
+
+       dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
+
+       snprintf(prop_name, 64, "%s-supply", supply);
+       regnode = of_parse_phandle(dev->of_node, prop_name, 0);
+
+       if (!regnode) {
+               regnode = of_get_child_regulator(dev->of_node, prop_name);
+               if (regnode)
+                       return regnode;
+
+               dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
+                       prop_name, dev->of_node);
+               return NULL;
+       }
+       return regnode;
+}
+
+static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
 {
        struct device *dev;
 
@@ -561,6 +629,46 @@ struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
        return dev ? dev_to_rdev(dev) : NULL;
 }
 
+/**
+ * of_regulator_dev_lookup - lookup a regulator device with device tree only
+ * @dev: Device pointer for regulator supply lookup.
+ * @supply: Supply name or regulator ID.
+ *
+ * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR()
+ *        encoded value on error.
+ *
+ * If successful, returns a pointer to the &struct regulator_dev that
+ * corresponds to the name @supply and with the embedded &struct device
+ * refcount incremented by one. The refcount must be dropped by calling
+ * put_device().
+ *
+ * On failure one of the following ERR_PTR() encoded values is returned:
+ * * -%ENODEV if lookup fails permanently.
+ * * -%EPROBE_DEFER if lookup could succeed in the future.
+ */
+struct regulator_dev *of_regulator_dev_lookup(struct device *dev,
+                                             const char *supply)
+{
+       struct regulator_dev *r;
+       struct device_node *node;
+
+       node = of_get_regulator(dev, supply);
+       if (node) {
+               r = of_find_regulator_by_node(node);
+               of_node_put(node);
+               if (r)
+                       return r;
+
+               /*
+                * We have a node, but there is no device.
+                * assume it has not registered yet.
+                */
+               return ERR_PTR(-EPROBE_DEFER);
+       }
+
+       return ERR_PTR(-ENODEV);
+}
+
 /*
  * Returns number of regulators coupled with rdev.
  */