Merge branch 'x86-fpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / drivers / regulator / fan53555.c
index 5808435cfae0a07187879773fe8264b0540b2bb0..f8e4257aef923d16f32f969e3e74e2e1fb55c6ff 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>
 
 #define FAN53555_NVOLTAGES     64      /* Numbers of voltages */
 
+enum fan53555_vendor {
+       FAN53555_VENDOR_FAIRCHILD = 0,
+       FAN53555_VENDOR_SILERGY,
+};
+
 /* IC Type */
 enum {
        FAN53555_CHIP_ID_00 = 0,
@@ -60,7 +67,12 @@ enum {
        FAN53555_CHIP_ID_05,
 };
 
+enum {
+       SILERGY_SYR82X = 8,
+};
+
 struct fan53555_device_info {
+       enum fan53555_vendor vendor;
        struct regmap *regmap;
        struct device *dev;
        struct regulator_desc desc;
@@ -181,6 +193,47 @@ static struct regulator_ops fan53555_regulator_ops = {
        .set_ramp_delay = fan53555_set_ramp,
 };
 
+static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
+{
+       /* Init voltage range and step */
+       switch (di->chip_id) {
+       case FAN53555_CHIP_ID_00:
+       case FAN53555_CHIP_ID_01:
+       case FAN53555_CHIP_ID_03:
+       case FAN53555_CHIP_ID_05:
+               di->vsel_min = 600000;
+               di->vsel_step = 10000;
+               break;
+       case FAN53555_CHIP_ID_04:
+               di->vsel_min = 603000;
+               di->vsel_step = 12826;
+               break;
+       default:
+               dev_err(di->dev,
+                       "Chip ID %d not supported!\n", di->chip_id);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
+{
+       /* Init voltage range and step */
+       switch (di->chip_id) {
+       case SILERGY_SYR82X:
+               di->vsel_min = 712500;
+               di->vsel_step = 12500;
+               break;
+       default:
+               dev_err(di->dev,
+                       "Chip ID %d not supported!\n", di->chip_id);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
 /* For 00,01,03,05 options:
  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
  * For 04 option:
@@ -189,6 +242,8 @@ static struct regulator_ops fan53555_regulator_ops = {
 static int fan53555_device_setup(struct fan53555_device_info *di,
                                struct fan53555_platform_data *pdata)
 {
+       int ret = 0;
+
        /* Setup voltage control register */
        switch (pdata->sleep_vsel_id) {
        case FAN53555_VSEL_ID_0:
@@ -203,26 +258,20 @@ static int fan53555_device_setup(struct fan53555_device_info *di,
                dev_err(di->dev, "Invalid VSEL ID!\n");
                return -EINVAL;
        }
-       /* Init voltage range and step */
-       switch (di->chip_id) {
-       case FAN53555_CHIP_ID_00:
-       case FAN53555_CHIP_ID_01:
-       case FAN53555_CHIP_ID_03:
-       case FAN53555_CHIP_ID_05:
-               di->vsel_min = 600000;
-               di->vsel_step = 10000;
+
+       switch (di->vendor) {
+       case FAN53555_VENDOR_FAIRCHILD:
+               ret = fan53555_voltages_setup_fairchild(di);
                break;
-       case FAN53555_CHIP_ID_04:
-               di->vsel_min = 603000;
-               di->vsel_step = 12826;
+       case FAN53555_VENDOR_SILERGY:
+               ret = fan53555_voltages_setup_silergy(di);
                break;
        default:
-               dev_err(di->dev,
-                       "Chip ID[%d]\n not supported!\n", di->chip_id);
+               dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
                return -EINVAL;
        }
 
-       return 0;
+       return ret;
 }
 
 static int fan53555_regulator_register(struct fan53555_device_info *di,
@@ -252,9 +301,46 @@ 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",
+               .data = (void *)FAN53555_VENDOR_FAIRCHILD
+       }, {
+               .compatible = "silergy,syr827",
+               .data = (void *)FAN53555_VENDOR_SILERGY,
+       }, {
+               .compatible = "silergy,syr828",
+               .data = (void *)FAN53555_VENDOR_SILERGY,
+       },
+       { }
+};
+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 = { };
@@ -262,6 +348,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;
@@ -272,11 +361,27 @@ static int fan53555_regulator_probe(struct i2c_client *client,
        if (!di)
                return -ENOMEM;
 
-       /* 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->regulator = pdata->regulator;
+       if (client->dev.of_node) {
+               const struct of_device_id *match;
+
+               match = of_match_device(of_match_ptr(fan53555_dt_ids),
+                                       &client->dev);
+               if (!match)
+                       return -ENODEV;
+
+               di->vendor = (unsigned long) match->data;
+       } else {
+               /* 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->vendor = id->driver_data;
        }
 
        di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
@@ -285,7 +390,6 @@ static int fan53555_regulator_probe(struct i2c_client *client,
                return PTR_ERR(di->regmap);
        }
        di->dev = &client->dev;
-       di->regulator = pdata->regulator;
        i2c_set_clientdata(client, di);
        /* Get chip ID */
        ret = regmap_read(di->regmap, FAN53555_ID1, &val);
@@ -314,6 +418,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");
@@ -322,13 +428,20 @@ static int fan53555_regulator_probe(struct i2c_client *client,
 }
 
 static const struct i2c_device_id fan53555_id[] = {
-       {"fan53555", -1},
+       {
+               .name = "fan53555",
+               .driver_data = FAN53555_VENDOR_FAIRCHILD
+       }, {
+               .name = "syr82x",
+               .driver_data = FAN53555_VENDOR_SILERGY
+       },
        { },
 };
 
 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,