regulator: da9055: Add DT support
authorAdam Thomson <Adam.Thomson.Opensource@diasemi.com>
Thu, 6 Feb 2014 18:03:13 +0000 (18:03 +0000)
committerMark Brown <broonie@linaro.org>
Fri, 7 Feb 2014 16:30:02 +0000 (16:30 +0000)
Signed-off-by: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
Signed-off-by: Mark Brown <broonie@linaro.org>
drivers/regulator/da9055-regulator.c

index 7f340206d329d452710a319deba4869a2d6c603b..d90f785829c45be3917d3f1c3fcadcdc335f8782 100644 (file)
@@ -19,6 +19,8 @@
 #include <linux/platform_device.h>
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
+#include <linux/of.h>
+#include <linux/regulator/of_regulator.h>
 
 #include <linux/mfd/da9055/core.h>
 #include <linux/mfd/da9055/reg.h>
@@ -446,6 +448,9 @@ static int da9055_gpio_init(struct da9055_regulator *regulator,
        struct da9055_regulator_info *info = regulator->info;
        int ret = 0;
 
+       if (!pdata)
+               return 0;
+
        if (pdata->gpio_ren && pdata->gpio_ren[id]) {
                char name[18];
                int gpio_mux = pdata->gpio_ren[id];
@@ -530,6 +535,59 @@ static inline struct da9055_regulator_info *find_regulator_info(int id)
        return NULL;
 }
 
+#ifdef CONFIG_OF
+static struct of_regulator_match da9055_reg_matches[] = {
+       { .name = "BUCK1", },
+       { .name = "BUCK2", },
+       { .name = "LDO1", },
+       { .name = "LDO2", },
+       { .name = "LDO3", },
+       { .name = "LDO4", },
+       { .name = "LDO5", },
+       { .name = "LDO6", },
+};
+
+static int da9055_regulator_dt_init(struct platform_device *pdev,
+                                   struct da9055_regulator *regulator,
+                                   struct regulator_config *config,
+                                   int regid)
+{
+       struct device_node *nproot, *np;
+       int ret;
+
+       nproot = of_node_get(pdev->dev.parent->of_node);
+       if (!nproot)
+               return -ENODEV;
+
+       np = of_find_node_by_name(nproot, "regulators");
+       if (!np)
+               return -ENODEV;
+
+       ret = of_regulator_match(&pdev->dev, np, &da9055_reg_matches[regid], 1);
+       of_node_put(nproot);
+       if (ret < 0) {
+               dev_err(&pdev->dev, "Error matching regulator: %d\n", ret);
+               return -ENODEV;
+       }
+
+       config->init_data = da9055_reg_matches[regid].init_data;
+       config->of_node = da9055_reg_matches[regid].of_node;
+
+       if (!config->of_node)
+               return -ENODEV;
+
+       return 0;
+}
+#else
+static inline da9055_regulator_dt_init(struct platform_device *pdev,
+                                      struct da9055_regulator *regulator,
+                                      struct regulator_config *config,
+                                      int regid)
+{
+       return -ENODEV;
+}
+#endif /* CONFIG_OF */
+
 static int da9055_regulator_probe(struct platform_device *pdev)
 {
        struct regulator_config config = { };
@@ -538,9 +596,6 @@ static int da9055_regulator_probe(struct platform_device *pdev)
        struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
        int ret, irq;
 
-       if (pdata == NULL || pdata->regulators[pdev->id] == NULL)
-               return -ENODEV;
-
        regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
                                 GFP_KERNEL);
        if (!regulator)
@@ -557,8 +612,14 @@ static int da9055_regulator_probe(struct platform_device *pdev)
        config.driver_data = regulator;
        config.regmap = da9055->regmap;
 
-       if (pdata && pdata->regulators)
+       if (pdata && pdata->regulators) {
                config.init_data = pdata->regulators[pdev->id];
+       } else {
+               ret = da9055_regulator_dt_init(pdev, regulator, &config,
+                                              pdev->id);
+               if (ret < 0)
+                       return ret;
+       }
 
        ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
        if (ret < 0)