regulator: fan53555: add devicetree support
[cascardo/linux.git] / drivers / regulator / fan53555.c
index 714fd9a..fa29ba0 100644 (file)
@@ -18,6 +18,8 @@
 #include <linux/platform_device.h>
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
+#include <linux/regulator/of_regulator.h>
+#include <linux/of_device.h>
 #include <linux/i2c.h>
 #include <linux/slab.h>
 #include <linux/regmap.h>
@@ -135,6 +137,38 @@ static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
                return REGULATOR_MODE_NORMAL;
 }
 
+static int slew_rates[] = {
+       64000,
+       32000,
+       16000,
+        8000,
+        4000,
+        2000,
+        1000,
+         500,
+};
+
+static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
+{
+       struct fan53555_device_info *di = rdev_get_drvdata(rdev);
+       int regval = -1, i;
+
+       for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
+               if (ramp <= slew_rates[i])
+                       regval = i;
+               else
+                       break;
+       }
+
+       if (regval < 0) {
+               dev_err(di->dev, "unsupported ramp value %d\n", ramp);
+               return -EINVAL;
+       }
+
+       return regmap_update_bits(di->regmap, FAN53555_CONTROL,
+                                 CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
+}
+
 static struct regulator_ops fan53555_regulator_ops = {
        .set_voltage_sel = regulator_set_voltage_sel_regmap,
        .get_voltage_sel = regulator_get_voltage_sel_regmap,
@@ -146,6 +180,7 @@ static struct regulator_ops fan53555_regulator_ops = {
        .is_enabled = regulator_is_enabled_regmap,
        .set_mode = fan53555_set_mode,
        .get_mode = fan53555_get_mode,
+       .set_ramp_delay = fan53555_set_ramp,
 };
 
 /* For 00,01,03,05 options:
@@ -156,8 +191,6 @@ static struct regulator_ops fan53555_regulator_ops = {
 static int fan53555_device_setup(struct fan53555_device_info *di,
                                struct fan53555_platform_data *pdata)
 {
-       unsigned int reg, data, mask;
-
        /* Setup voltage control register */
        switch (pdata->sleep_vsel_id) {
        case FAN53555_VSEL_ID_0:
@@ -190,15 +223,8 @@ static int fan53555_device_setup(struct fan53555_device_info *di,
                        "Chip ID[%d]\n not supported!\n", di->chip_id);
                return -EINVAL;
        }
-       /* Init slew rate */
-       if (pdata->slew_rate & 0x7)
-               di->slew_rate = pdata->slew_rate;
-       else
-               di->slew_rate = FAN53555_SLEW_RATE_64MV;
-       reg = FAN53555_CONTROL;
-       data = di->slew_rate << CTL_SLEW_SHIFT;
-       mask = CTL_SLEW_MASK;
-       return regmap_update_bits(di->regmap, reg, mask, data);
+
+       return 0;
 }
 
 static int fan53555_regulator_register(struct fan53555_device_info *di,
@@ -207,6 +233,7 @@ static int fan53555_regulator_register(struct fan53555_device_info *di,
        struct regulator_desc *rdesc = &di->desc;
 
        rdesc->name = "fan53555-reg";
+       rdesc->supply_name = "vin";
        rdesc->ops = &fan53555_regulator_ops;
        rdesc->type = REGULATOR_VOLTAGE;
        rdesc->n_voltages = FAN53555_NVOLTAGES;
@@ -227,9 +254,39 @@ static struct regmap_config fan53555_regmap_config = {
        .val_bits = 8,
 };
 
+static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
+                                                       struct device_node *np)
+{
+       struct fan53555_platform_data *pdata;
+       int ret;
+       u32 tmp;
+
+       pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+       if (!pdata)
+               return NULL;
+
+       pdata->regulator = of_get_regulator_init_data(dev, np);
+
+       ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
+                                  &tmp);
+       if (!ret)
+               pdata->sleep_vsel_id = tmp;
+
+       return pdata;
+}
+
+static const struct of_device_id fan53555_dt_ids[] = {
+       {
+               .compatible = "fcs,fan53555",
+       },
+       { }
+};
+MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
+
 static int fan53555_regulator_probe(struct i2c_client *client,
                                const struct i2c_device_id *id)
 {
+       struct device_node *np = client->dev.of_node;
        struct fan53555_device_info *di;
        struct fan53555_platform_data *pdata;
        struct regulator_config config = { };
@@ -237,6 +294,9 @@ static int fan53555_regulator_probe(struct i2c_client *client,
        int ret;
 
        pdata = dev_get_platdata(&client->dev);
+       if (!pdata)
+               pdata = fan53555_parse_dt(&client->dev, np);
+
        if (!pdata || !pdata->regulator) {
                dev_err(&client->dev, "Platform data not found!\n");
                return -ENODEV;
@@ -247,6 +307,17 @@ static int fan53555_regulator_probe(struct i2c_client *client,
        if (!di)
                return -ENOMEM;
 
+       if (!client->dev.of_node) {
+               /* if no ramp constraint set, get the pdata ramp_delay */
+               if (!di->regulator->constraints.ramp_delay) {
+                       int slew_idx = (pdata->slew_rate & 0x7)
+                                               ? pdata->slew_rate : 0;
+
+                       di->regulator->constraints.ramp_delay
+                                               = slew_rates[slew_idx];
+               }
+       }
+
        di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
        if (IS_ERR(di->regmap)) {
                dev_err(&client->dev, "Failed to allocate regmap!\n");
@@ -282,6 +353,8 @@ static int fan53555_regulator_probe(struct i2c_client *client,
        config.init_data = di->regulator;
        config.regmap = di->regmap;
        config.driver_data = di;
+       config.of_node = np;
+
        ret = fan53555_regulator_register(di, &config);
        if (ret < 0)
                dev_err(&client->dev, "Failed to register regulator!\n");
@@ -297,6 +370,7 @@ static const struct i2c_device_id fan53555_id[] = {
 static struct i2c_driver fan53555_regulator_driver = {
        .driver = {
                .name = "fan53555-regulator",
+               .of_match_table = of_match_ptr(fan53555_dt_ids),
        },
        .probe = fan53555_regulator_probe,
        .id_table = fan53555_id,