regulator: palmas: implement sleep mode configuration
[linux-2.6.git] / drivers / regulator / s5m8767.c
index b3d356f..4ca2db0 100644 (file)
@@ -120,16 +120,11 @@ static const struct s5m_voltage_desc *reg_voltage_map[] = {
        [S5M8767_BUCK9] = &buck_voltage_val3,
 };
 
-static inline int s5m8767_get_reg_id(struct regulator_dev *rdev)
-{
-       return rdev_get_id(rdev);
-}
-
 static int s5m8767_list_voltage(struct regulator_dev *rdev,
                                unsigned int selector)
 {
        const struct s5m_voltage_desc *desc;
-       int reg_id = s5m8767_get_reg_id(rdev);
+       int reg_id = rdev_get_id(rdev);
        int val;
 
        if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0)
@@ -148,7 +143,7 @@ static int s5m8767_list_voltage(struct regulator_dev *rdev,
 
 static int s5m8767_get_register(struct regulator_dev *rdev, int *reg)
 {
-       int reg_id = s5m8767_get_reg_id(rdev);
+       int reg_id = rdev_get_id(rdev);
 
        switch (reg_id) {
        case S5M8767_LDO1 ... S5M8767_LDO2:
@@ -224,7 +219,8 @@ static int s5m8767_reg_disable(struct regulator_dev *rdev)
 
 static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
 {
-       int reg_id = s5m8767_get_reg_id(rdev);
+       struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
+       int reg_id = rdev_get_id(rdev);
        int reg;
 
        switch (reg_id) {
@@ -239,12 +235,18 @@ static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
                break;
        case S5M8767_BUCK2:
                reg = S5M8767_REG_BUCK2DVS1;
+               if (s5m8767->buck2_gpiodvs)
+                       reg += s5m8767->buck_gpioindex;
                break;
        case S5M8767_BUCK3:
                reg = S5M8767_REG_BUCK3DVS1;
+               if (s5m8767->buck3_gpiodvs)
+                       reg += s5m8767->buck_gpioindex;
                break;
        case S5M8767_BUCK4:
                reg = S5M8767_REG_BUCK4DVS1;
+               if (s5m8767->buck4_gpiodvs)
+                       reg += s5m8767->buck_gpioindex;
                break;
        case S5M8767_BUCK5:
                reg = S5M8767_REG_BUCK5CTRL2;
@@ -264,31 +266,15 @@ static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
 static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
 {
        struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
-       int reg, mask = 0xff, ret;
-       int reg_id = s5m8767_get_reg_id(rdev);
+       int reg, mask, ret;
+       int reg_id = rdev_get_id(rdev);
        u8 val;
 
        ret = s5m8767_get_voltage_register(rdev, &reg);
        if (ret)
                return ret;
 
-       switch (reg_id) {
-       case S5M8767_LDO1 ... S5M8767_LDO28:
-               mask = 0x3f;
-               break;
-       case S5M8767_BUCK2:
-               if (s5m8767->buck2_gpiodvs)
-                       reg += s5m8767->buck_gpioindex;
-               break;
-       case S5M8767_BUCK3:
-               if (s5m8767->buck3_gpiodvs)
-                       reg += s5m8767->buck_gpioindex;
-               break;
-       case S5M8767_BUCK4:
-               if (s5m8767->buck4_gpiodvs)
-                       reg += s5m8767->buck_gpioindex;
-               break;
-       }
+       mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
 
        ret = s5m_reg_read(s5m8767->iodev, reg, &val);
        if (ret)
@@ -299,11 +285,11 @@ static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
        return val;
 }
 
-static inline int s5m8767_convert_voltage(
+static int s5m8767_convert_voltage_to_sel(
                const struct s5m_voltage_desc *desc,
                int min_vol, int max_vol)
 {
-       int out_vol = 0;
+       int selector = 0;
 
        if (desc == NULL)
                return -EINVAL;
@@ -311,23 +297,21 @@ static inline int s5m8767_convert_voltage(
        if (max_vol < desc->min || min_vol > desc->max)
                return -EINVAL;
 
-       out_vol = (min_vol - desc->min) / desc->step;
+       selector = (min_vol - desc->min) / desc->step;
 
-       if (desc->min + desc->step * out_vol > max_vol)
+       if (desc->min + desc->step * selector > max_vol)
                return -EINVAL;
 
-       return out_vol;
+       return selector;
 }
 
 static int s5m8767_set_voltage(struct regulator_dev *rdev,
                                int min_uV, int max_uV, unsigned *selector)
 {
        struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
-       int min_vol = min_uV, max_vol = max_uV;
        const struct s5m_voltage_desc *desc;
-       int reg_id = s5m8767_get_reg_id(rdev);
-       int reg, mask, ret;
-       int i;
+       int reg_id = rdev_get_id(rdev);
+       int sel, reg, mask, ret;
        u8 val;
 
        switch (reg_id) {
@@ -348,19 +332,20 @@ static int s5m8767_set_voltage(struct regulator_dev *rdev,
 
        desc = reg_voltage_map[reg_id];
 
-       i = s5m8767_convert_voltage(desc, min_vol, max_vol);
-       if (i < 0)
-               return i;
+       sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
+       if (sel < 0)
+               return sel;
 
        ret = s5m8767_get_voltage_register(rdev, &reg);
        if (ret)
                return ret;
 
        s5m_reg_read(s5m8767->iodev, reg, &val);
-       val = val & mask;
+       val &= ~mask;
+       val |= sel;
 
        ret = s5m_reg_write(s5m8767->iodev, reg, val);
-       *selector = i;
+       *selector = sel;
 
        return ret;
 }
@@ -387,10 +372,9 @@ static int s5m8767_set_voltage_buck(struct regulator_dev *rdev,
                                    int min_uV, int max_uV, unsigned *selector)
 {
        struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
-       int reg_id = s5m8767_get_reg_id(rdev);
+       int reg_id = rdev_get_id(rdev);
        const struct s5m_voltage_desc *desc;
        int new_val, old_val, i = 0;
-       int min_vol = min_uV, max_vol = max_uV;
 
        if (reg_id < S5M8767_BUCK1 || reg_id > S5M8767_BUCK6)
                return -EINVAL;
@@ -407,7 +391,7 @@ static int s5m8767_set_voltage_buck(struct regulator_dev *rdev,
        }
 
        desc = reg_voltage_map[reg_id];
-       new_val = s5m8767_convert_voltage(desc, min_vol, max_vol);
+       new_val = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
        if (new_val < 0)
                return new_val;
 
@@ -456,40 +440,14 @@ static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
 {
        struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
        const struct s5m_voltage_desc *desc;
-       int reg_id = s5m8767_get_reg_id(rdev);
-       int mask;
-       int new_val, old_val;
+       int reg_id = rdev_get_id(rdev);
 
-       switch (reg_id) {
-       case S5M8767_LDO1 ... S5M8767_LDO28:
-               mask = 0x3f;
-               break;
-       case S5M8767_BUCK1 ... S5M8767_BUCK6:
-               mask = 0xff;
-               break;
-       case S5M8767_BUCK7 ... S5M8767_BUCK8:
-               return -EINVAL;
-       case S5M8767_BUCK9:
-               mask = 0xff;
-               break;
-       default:
-               return -EINVAL;
-       }
        desc = reg_voltage_map[reg_id];
 
-       new_val = s5m8767_convert_voltage(desc, new_sel, new_sel);
-       if (new_val < 0)
-               return new_val;
-
-       old_val = s5m8767_convert_voltage(desc, old_sel, old_sel);
-       if (old_val < 0)
-               return old_val;
-
        if (old_sel < new_sel)
-               return DIV_ROUND_UP(desc->step * (new_val - old_val),
-                                       s5m8767->ramp_delay);
-       else
-               return 0;
+               return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
+                                       s5m8767->ramp_delay * 1000);
+       return 0;
 }
 
 static struct regulator_ops s5m8767_ldo_ops = {
@@ -573,14 +531,34 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
        struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev);
        struct regulator_dev **rdev;
        struct s5m8767_info *s5m8767;
-       struct i2c_client *i2c;
-       int i, ret, size, reg;
+       int i, ret, size;
 
        if (!pdata) {
                dev_err(pdev->dev.parent, "Platform data not supplied\n");
                return -ENODEV;
        }
 
+       if (pdata->buck2_gpiodvs) {
+               if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
+                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
+                       return -EINVAL;
+               }
+       }
+
+       if (pdata->buck3_gpiodvs) {
+               if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
+                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
+                       return -EINVAL;
+               }
+       }
+
+       if (pdata->buck4_gpiodvs) {
+               if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
+                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
+                       return -EINVAL;
+               }
+       }
+
        s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
                                GFP_KERNEL);
        if (!s5m8767)
@@ -596,7 +574,6 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
        s5m8767->iodev = iodev;
        s5m8767->num_regulators = S5M8767_REG_MAX - 2;
        platform_set_drvdata(pdev, s5m8767);
-       i2c = s5m8767->iodev->i2c;
 
        s5m8767->buck_gpioindex = pdata->buck_default_idx;
        s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
@@ -613,7 +590,7 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
        for (i = 0; i < 8; i++) {
                if (s5m8767->buck2_gpiodvs) {
                        s5m8767->buck2_vol[i] =
-                               s5m8767_convert_voltage(
+                               s5m8767_convert_voltage_to_sel(
                                                &buck_voltage_val2,
                                                pdata->buck2_voltage[i],
                                                pdata->buck2_voltage[i] +
@@ -622,7 +599,7 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
 
                if (s5m8767->buck3_gpiodvs) {
                        s5m8767->buck3_vol[i] =
-                               s5m8767_convert_voltage(
+                               s5m8767_convert_voltage_to_sel(
                                                &buck_voltage_val2,
                                                pdata->buck3_voltage[i],
                                                pdata->buck3_voltage[i] +
@@ -631,7 +608,7 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
 
                if (s5m8767->buck4_gpiodvs) {
                        s5m8767->buck4_vol[i] =
-                               s5m8767_convert_voltage(
+                               s5m8767_convert_voltage_to_sel(
                                                &buck_voltage_val2,
                                                pdata->buck4_voltage[i],
                                                pdata->buck4_voltage[i] +
@@ -675,30 +652,6 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
                }
        }
 
-       if (pdata->buck2_gpiodvs) {
-               if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
-                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
-                       ret = -EINVAL;
-                       return ret;
-               }
-       }
-
-       if (pdata->buck3_gpiodvs) {
-               if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
-                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
-                       ret = -EINVAL;
-                       return ret;
-               }
-       }
-
-       if (pdata->buck4_gpiodvs) {
-               if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
-                       dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
-                       ret = -EINVAL;
-                       return ret;
-               }
-       }
-
        s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
                        (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
        s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
@@ -742,15 +695,19 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
                case 15:
                        s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
                                        0xc0, 0xf0);
+                       break;
                case 25:
                        s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
                                        0xd0, 0xf0);
+                       break;
                case 50:
                        s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
                                        0xe0, 0xf0);
+                       break;
                case 100:
                        s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
                                        0xf0, 0xf0);
+                       break;
                default:
                        s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
                                        0x90, 0xf0);
@@ -767,7 +724,7 @@ static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
                                (desc->max - desc->min) / desc->step + 1;
 
                rdev[i] = regulator_register(&regulators[id], s5m8767->dev,
-                               pdata->regulators[i].initdata, s5m8767);
+                               pdata->regulators[i].initdata, s5m8767, NULL);
                if (IS_ERR(rdev[i])) {
                        ret = PTR_ERR(rdev[i]);
                        dev_err(s5m8767->dev, "regulator init failed for %d\n",
@@ -831,4 +788,3 @@ module_exit(s5m8767_pmic_exit);
 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
 MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
 MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:s5m8767-pmic");