]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blobdiff - drivers/regulator/tps65910-regulator.c
regulator: Fix s5m8767_set_voltage_time_sel calculation value
[mirror_ubuntu-hirsute-kernel.git] / drivers / regulator / tps65910-regulator.c
index b552aae55b417c7cd9aa97fba33bd61ad0b7e787..b0533c11157175201ec850673c7fadcd10a8e6d4 100644 (file)
 #include <linux/gpio.h>
 #include <linux/mfd/tps65910.h>
 
-#define TPS65910_REG_VRTC              0
-#define TPS65910_REG_VIO               1
-#define TPS65910_REG_VDD1              2
-#define TPS65910_REG_VDD2              3
-#define TPS65910_REG_VDD3              4
-#define TPS65910_REG_VDIG1             5
-#define TPS65910_REG_VDIG2             6
-#define TPS65910_REG_VPLL              7
-#define TPS65910_REG_VDAC              8
-#define TPS65910_REG_VAUX1             9
-#define TPS65910_REG_VAUX2             10
-#define TPS65910_REG_VAUX33            11
-#define TPS65910_REG_VMMC              12
-
-#define TPS65911_REG_VDDCTRL           4
-#define TPS65911_REG_LDO1              5
-#define TPS65911_REG_LDO2              6
-#define TPS65911_REG_LDO3              7
-#define TPS65911_REG_LDO4              8
-#define TPS65911_REG_LDO5              9
-#define TPS65911_REG_LDO6              10
-#define TPS65911_REG_LDO7              11
-#define TPS65911_REG_LDO8              12
-
 #define TPS65910_SUPPLY_STATE_ENABLED  0x1
+#define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 |      \
+                       TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 |          \
+                       TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 |          \
+                       TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
 
 /* supported VIO voltages in milivolts */
 static const u16 VIO_VSEL_table[] = {
@@ -107,8 +87,8 @@ struct tps_info {
        const char *name;
        unsigned min_uV;
        unsigned max_uV;
-       u8 table_len;
-       const u16 *table;
+       u8 n_voltages;
+       const u16 *voltage_table;
 };
 
 static struct tps_info tps65910_regs[] = {
@@ -119,8 +99,8 @@ static struct tps_info tps65910_regs[] = {
                .name = "VIO",
                .min_uV = 1500000,
                .max_uV = 3300000,
-               .table_len = ARRAY_SIZE(VIO_VSEL_table),
-               .table = VIO_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
+               .voltage_table = VIO_VSEL_table,
        },
        {
                .name = "VDD1",
@@ -136,132 +116,179 @@ static struct tps_info tps65910_regs[] = {
                .name = "VDD3",
                .min_uV = 5000000,
                .max_uV = 5000000,
-               .table_len = ARRAY_SIZE(VDD3_VSEL_table),
-               .table = VDD3_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
+               .voltage_table = VDD3_VSEL_table,
        },
        {
                .name = "VDIG1",
                .min_uV = 1200000,
                .max_uV = 2700000,
-               .table_len = ARRAY_SIZE(VDIG1_VSEL_table),
-               .table = VDIG1_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
+               .voltage_table = VDIG1_VSEL_table,
        },
        {
                .name = "VDIG2",
                .min_uV = 1000000,
                .max_uV = 1800000,
-               .table_len = ARRAY_SIZE(VDIG2_VSEL_table),
-               .table = VDIG2_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
+               .voltage_table = VDIG2_VSEL_table,
        },
        {
                .name = "VPLL",
                .min_uV = 1000000,
                .max_uV = 2500000,
-               .table_len = ARRAY_SIZE(VPLL_VSEL_table),
-               .table = VPLL_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
+               .voltage_table = VPLL_VSEL_table,
        },
        {
                .name = "VDAC",
                .min_uV = 1800000,
                .max_uV = 2850000,
-               .table_len = ARRAY_SIZE(VDAC_VSEL_table),
-               .table = VDAC_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
+               .voltage_table = VDAC_VSEL_table,
        },
        {
                .name = "VAUX1",
                .min_uV = 1800000,
                .max_uV = 2850000,
-               .table_len = ARRAY_SIZE(VAUX1_VSEL_table),
-               .table = VAUX1_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
+               .voltage_table = VAUX1_VSEL_table,
        },
        {
                .name = "VAUX2",
                .min_uV = 1800000,
                .max_uV = 3300000,
-               .table_len = ARRAY_SIZE(VAUX2_VSEL_table),
-               .table = VAUX2_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
+               .voltage_table = VAUX2_VSEL_table,
        },
        {
                .name = "VAUX33",
                .min_uV = 1800000,
                .max_uV = 3300000,
-               .table_len = ARRAY_SIZE(VAUX33_VSEL_table),
-               .table = VAUX33_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
+               .voltage_table = VAUX33_VSEL_table,
        },
        {
                .name = "VMMC",
                .min_uV = 1800000,
                .max_uV = 3300000,
-               .table_len = ARRAY_SIZE(VMMC_VSEL_table),
-               .table = VMMC_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
+               .voltage_table = VMMC_VSEL_table,
        },
 };
 
 static struct tps_info tps65911_regs[] = {
+       {
+               .name = "VRTC",
+       },
        {
                .name = "VIO",
                .min_uV = 1500000,
                .max_uV = 3300000,
-               .table_len = ARRAY_SIZE(VIO_VSEL_table),
-               .table = VIO_VSEL_table,
+               .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
+               .voltage_table = VIO_VSEL_table,
        },
        {
                .name = "VDD1",
                .min_uV = 600000,
                .max_uV = 4500000,
+               .n_voltages = 73,
        },
        {
                .name = "VDD2",
                .min_uV = 600000,
                .max_uV = 4500000,
+               .n_voltages = 73,
        },
        {
                .name = "VDDCTRL",
                .min_uV = 600000,
                .max_uV = 1400000,
+               .n_voltages = 65,
        },
        {
                .name = "LDO1",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 47,
        },
        {
                .name = "LDO2",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 47,
        },
        {
                .name = "LDO3",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 24,
        },
        {
                .name = "LDO4",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 47,
        },
        {
                .name = "LDO5",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 24,
        },
        {
                .name = "LDO6",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 24,
        },
        {
                .name = "LDO7",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 24,
        },
        {
                .name = "LDO8",
                .min_uV = 1000000,
                .max_uV = 3300000,
+               .n_voltages = 24,
        },
 };
 
+#define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
+static unsigned int tps65910_ext_sleep_control[] = {
+       0,
+       EXT_CONTROL_REG_BITS(VIO,    1, 0),
+       EXT_CONTROL_REG_BITS(VDD1,   1, 1),
+       EXT_CONTROL_REG_BITS(VDD2,   1, 2),
+       EXT_CONTROL_REG_BITS(VDD3,   1, 3),
+       EXT_CONTROL_REG_BITS(VDIG1,  0, 1),
+       EXT_CONTROL_REG_BITS(VDIG2,  0, 2),
+       EXT_CONTROL_REG_BITS(VPLL,   0, 6),
+       EXT_CONTROL_REG_BITS(VDAC,   0, 7),
+       EXT_CONTROL_REG_BITS(VAUX1,  0, 3),
+       EXT_CONTROL_REG_BITS(VAUX2,  0, 4),
+       EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
+       EXT_CONTROL_REG_BITS(VMMC,   0, 0),
+};
+
+static unsigned int tps65911_ext_sleep_control[] = {
+       0,
+       EXT_CONTROL_REG_BITS(VIO,     1, 0),
+       EXT_CONTROL_REG_BITS(VDD1,    1, 1),
+       EXT_CONTROL_REG_BITS(VDD2,    1, 2),
+       EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
+       EXT_CONTROL_REG_BITS(LDO1,    0, 1),
+       EXT_CONTROL_REG_BITS(LDO2,    0, 2),
+       EXT_CONTROL_REG_BITS(LDO3,    0, 7),
+       EXT_CONTROL_REG_BITS(LDO4,    0, 6),
+       EXT_CONTROL_REG_BITS(LDO5,    0, 3),
+       EXT_CONTROL_REG_BITS(LDO6,    0, 0),
+       EXT_CONTROL_REG_BITS(LDO7,    0, 5),
+       EXT_CONTROL_REG_BITS(LDO8,    0, 4),
+};
+
 struct tps65910_reg {
        struct regulator_desc *desc;
        struct tps65910 *mfd;
@@ -271,6 +298,8 @@ struct tps65910_reg {
        int num_regulators;
        int mode;
        int  (*get_ctrl_reg)(int);
+       unsigned int *ext_sleep_control;
+       unsigned int board_ext_control[TPS65910_NUM_REGS];
 };
 
 static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
@@ -596,7 +625,7 @@ static int tps65910_get_voltage(struct regulator_dev *dev)
                return -EINVAL;
        }
 
-       voltage = pmic->info[id]->table[value] * 1000;
+       voltage = pmic->info[id]->voltage_table[value] * 1000;
 
        return voltage;
 }
@@ -646,8 +675,9 @@ static int tps65911_get_voltage(struct regulator_dev *dev)
                step_mv = 100;
                break;
        case TPS65910_REG_VIO:
-               return pmic->info[id]->table[value] * 1000;
-               break;
+               value &= LDO_SEL_MASK;
+               value >>= LDO_SEL_SHIFT;
+               return pmic->info[id]->voltage_table[value] * 1000;
        default:
                return -EINVAL;
        }
@@ -739,9 +769,11 @@ static int tps65911_set_voltage(struct regulator_dev *dev, unsigned selector)
        case TPS65911_REG_LDO6:
        case TPS65911_REG_LDO7:
        case TPS65911_REG_LDO8:
-       case TPS65910_REG_VIO:
                return tps65910_modify_bits(pmic, reg,
                                (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
+       case TPS65910_REG_VIO:
+               return tps65910_modify_bits(pmic, reg,
+                               (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
        }
 
        return -EINVAL;
@@ -780,10 +812,10 @@ static int tps65910_list_voltage(struct regulator_dev *dev,
        if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
                return -EINVAL;
 
-       if (selector >= pmic->info[id]->table_len)
+       if (selector >= pmic->info[id]->n_voltages)
                return -EINVAL;
        else
-               voltage = pmic->info[id]->table[selector] * 1000;
+               voltage = pmic->info[id]->voltage_table[selector] * 1000;
 
        return voltage;
 }
@@ -819,7 +851,7 @@ static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
                step_mv = 100;
                break;
        case TPS65910_REG_VIO:
-               return pmic->info[id]->table[selector] * 1000;
+               return pmic->info[id]->voltage_table[selector] * 1000;
        default:
                return -EINVAL;
        }
@@ -871,6 +903,139 @@ static struct regulator_ops tps65911_ops = {
        .list_voltage           = tps65911_list_voltage,
 };
 
+static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic,
+               int id, int ext_sleep_config)
+{
+       struct tps65910 *mfd = pmic->mfd;
+       u8 regoffs = (pmic->ext_sleep_control[id] >> 8) & 0xFF;
+       u8 bit_pos = (1 << pmic->ext_sleep_control[id] & 0xFF);
+       int ret;
+
+       /*
+        * Regulator can not be control from multiple external input EN1, EN2
+        * and EN3 together.
+        */
+       if (ext_sleep_config & EXT_SLEEP_CONTROL) {
+               int en_count;
+               en_count = ((ext_sleep_config &
+                               TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1) != 0);
+               en_count += ((ext_sleep_config &
+                               TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2) != 0);
+               en_count += ((ext_sleep_config &
+                               TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3) != 0);
+               en_count += ((ext_sleep_config &
+                               TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP) != 0);
+               if (en_count > 1) {
+                       dev_err(mfd->dev,
+                               "External sleep control flag is not proper\n");
+                       return -EINVAL;
+               }
+       }
+
+       pmic->board_ext_control[id] = ext_sleep_config;
+
+       /* External EN1 control */
+       if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1)
+               ret = tps65910_set_bits(mfd,
+                               TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
+       else
+               ret = tps65910_clear_bits(mfd,
+                               TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
+       if (ret < 0) {
+               dev_err(mfd->dev,
+                       "Error in configuring external control EN1\n");
+               return ret;
+       }
+
+       /* External EN2 control */
+       if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2)
+               ret = tps65910_set_bits(mfd,
+                               TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
+       else
+               ret = tps65910_clear_bits(mfd,
+                               TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
+       if (ret < 0) {
+               dev_err(mfd->dev,
+                       "Error in configuring external control EN2\n");
+               return ret;
+       }
+
+       /* External EN3 control for TPS65910 LDO only */
+       if ((tps65910_chip_id(mfd) == TPS65910) &&
+                       (id >= TPS65910_REG_VDIG1)) {
+               if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3)
+                       ret = tps65910_set_bits(mfd,
+                               TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
+               else
+                       ret = tps65910_clear_bits(mfd,
+                               TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
+               if (ret < 0) {
+                       dev_err(mfd->dev,
+                               "Error in configuring external control EN3\n");
+                       return ret;
+               }
+       }
+
+       /* Return if no external control is selected */
+       if (!(ext_sleep_config & EXT_SLEEP_CONTROL)) {
+               /* Clear all sleep controls */
+               ret = tps65910_clear_bits(mfd,
+                       TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
+               if (!ret)
+                       ret = tps65910_clear_bits(mfd,
+                               TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
+               if (ret < 0)
+                       dev_err(mfd->dev,
+                               "Error in configuring SLEEP register\n");
+               return ret;
+       }
+
+       /*
+        * For regulator that has separate operational and sleep register make
+        * sure that operational is used and clear sleep register to turn
+        * regulator off when external control is inactive
+        */
+       if ((id == TPS65910_REG_VDD1) ||
+               (id == TPS65910_REG_VDD2) ||
+                       ((id == TPS65911_REG_VDDCTRL) &&
+                               (tps65910_chip_id(mfd) == TPS65911))) {
+               int op_reg_add = pmic->get_ctrl_reg(id) + 1;
+               int sr_reg_add = pmic->get_ctrl_reg(id) + 2;
+               int opvsel = tps65910_reg_read(pmic, op_reg_add);
+               int srvsel = tps65910_reg_read(pmic, sr_reg_add);
+               if (opvsel & VDD1_OP_CMD_MASK) {
+                       u8 reg_val = srvsel & VDD1_OP_SEL_MASK;
+                       ret = tps65910_reg_write(pmic, op_reg_add, reg_val);
+                       if (ret < 0) {
+                               dev_err(mfd->dev,
+                                       "Error in configuring op register\n");
+                               return ret;
+                       }
+               }
+               ret = tps65910_reg_write(pmic, sr_reg_add, 0);
+               if (ret < 0) {
+                       dev_err(mfd->dev, "Error in settting sr register\n");
+                       return ret;
+               }
+       }
+
+       ret = tps65910_clear_bits(mfd,
+                       TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
+       if (!ret) {
+               if (ext_sleep_config & TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
+                       ret = tps65910_set_bits(mfd,
+                               TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
+               else
+                       ret = tps65910_clear_bits(mfd,
+                               TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
+       }
+       if (ret < 0)
+               dev_err(mfd->dev,
+                       "Error in configuring SLEEP register\n");
+
+       return ret;
+}
+
 static __devinit int tps65910_probe(struct platform_device *pdev)
 {
        struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
@@ -885,8 +1050,6 @@ static __devinit int tps65910_probe(struct platform_device *pdev)
        if (!pmic_plat_data)
                return -EINVAL;
 
-       reg_data = pmic_plat_data->tps65910_pmic_init_data;
-
        pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
        if (!pmic)
                return -ENOMEM;
@@ -903,11 +1066,13 @@ static __devinit int tps65910_probe(struct platform_device *pdev)
        case TPS65910:
                pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
                pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
+               pmic->ext_sleep_control = tps65910_ext_sleep_control;
                info = tps65910_regs;
                break;
        case TPS65911:
                pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
                pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
+               pmic->ext_sleep_control = tps65911_ext_sleep_control;
                info = tps65911_regs;
                break;
        default:
@@ -937,13 +1102,22 @@ static __devinit int tps65910_probe(struct platform_device *pdev)
                goto err_free_info;
        }
 
-       for (i = 0; i < pmic->num_regulators; i++, info++, reg_data++) {
+       for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
+                       i++, info++) {
+
+               reg_data = pmic_plat_data->tps65910_pmic_init_data[i];
+
+               /* Regulator API handles empty constraints but not NULL
+                * constraints */
+               if (!reg_data)
+                       continue;
+
                /* Register the regulators */
                pmic->info[i] = info;
 
                pmic->desc[i].name = info->name;
                pmic->desc[i].id = i;
-               pmic->desc[i].n_voltages = info->table_len;
+               pmic->desc[i].n_voltages = info->n_voltages;
 
                if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
                        pmic->desc[i].ops = &tps65910_ops_dcdc;
@@ -961,11 +1135,21 @@ static __devinit int tps65910_probe(struct platform_device *pdev)
                                pmic->desc[i].ops = &tps65911_ops;
                }
 
+               err = tps65910_set_ext_sleep_config(pmic, i,
+                               pmic_plat_data->regulator_ext_sleep_control[i]);
+               /*
+                * Failing on regulator for configuring externally control
+                * is not a serious issue, just throw warning.
+                */
+               if (err < 0)
+                       dev_warn(tps65910->dev,
+                               "Failed to initialise ext control config\n");
+
                pmic->desc[i].type = REGULATOR_VOLTAGE;
                pmic->desc[i].owner = THIS_MODULE;
 
                rdev = regulator_register(&pmic->desc[i],
-                               tps65910->dev, reg_data, pmic);
+                               tps65910->dev, reg_data, pmic, NULL);
                if (IS_ERR(rdev)) {
                        dev_err(tps65910->dev,
                                "failed to register %s regulator\n",
@@ -1007,6 +1191,36 @@ static int __devexit tps65910_remove(struct platform_device *pdev)
        return 0;
 }
 
+static void tps65910_shutdown(struct platform_device *pdev)
+{
+       struct tps65910_reg *pmic = platform_get_drvdata(pdev);
+       int i;
+
+       /*
+        * Before bootloader jumps to kernel, it makes sure that required
+        * external control signals are in desired state so that given rails
+        * can be configure accordingly.
+        * If rails are configured to be controlled from external control
+        * then before shutting down/rebooting the system, the external
+        * control configuration need to be remove from the rails so that
+        * its output will be available as per register programming even
+        * if external controls are removed. This is require when the POR
+        * value of the control signals are not in active state and before
+        * bootloader initializes it, the system requires the rail output
+        * to be active for booting.
+        */
+       for (i = 0; i < pmic->num_regulators; i++) {
+               int err;
+               if (!pmic->rdev[i])
+                       continue;
+
+               err = tps65910_set_ext_sleep_config(pmic, i, 0);
+               if (err < 0)
+                       dev_err(&pdev->dev,
+                               "Error in clearing external control\n");
+       }
+}
+
 static struct platform_driver tps65910_driver = {
        .driver = {
                .name = "tps65910-pmic",
@@ -1014,6 +1228,7 @@ static struct platform_driver tps65910_driver = {
        },
        .probe = tps65910_probe,
        .remove = __devexit_p(tps65910_remove),
+       .shutdown = tps65910_shutdown,
 };
 
 static int __init tps65910_init(void)
@@ -1029,6 +1244,6 @@ static void __exit tps65910_cleanup(void)
 module_exit(tps65910_cleanup);
 
 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
-MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
+MODULE_DESCRIPTION("TPS65910/TPS65911 voltage regulator driver");
 MODULE_LICENSE("GPL v2");
 MODULE_ALIAS("platform:tps65910-pmic");