Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * tps65218-regulator.c
0004  *
0005  * Regulator driver for TPS65218 PMIC
0006  *
0007  * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/device.h>
0013 #include <linux/init.h>
0014 #include <linux/err.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/of_device.h>
0017 #include <linux/regmap.h>
0018 #include <linux/regulator/of_regulator.h>
0019 #include <linux/regulator/driver.h>
0020 #include <linux/regulator/machine.h>
0021 #include <linux/mfd/tps65218.h>
0022 
0023 #define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
0024                _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm, \
0025                _ct, _ncl) \
0026     {                           \
0027         .name           = _name,        \
0028         .of_match       = _of,          \
0029         .id         = _id,          \
0030         .ops            = &_ops,        \
0031         .n_voltages     = _n,           \
0032         .type           = _type,    \
0033         .owner          = THIS_MODULE,      \
0034         .vsel_reg       = _vr,          \
0035         .vsel_mask      = _vm,          \
0036         .csel_reg       = _cr,          \
0037         .csel_mask      = _cm,          \
0038         .curr_table     = _ct,          \
0039         .n_current_limits   = _ncl,         \
0040         .enable_reg     = _er,          \
0041         .enable_mask        = _em,          \
0042         .volt_table     = NULL,         \
0043         .linear_ranges      = _lr,          \
0044         .n_linear_ranges    = _nlr,         \
0045         .ramp_delay     = _delay,       \
0046         .fixed_uV       = _fuv,         \
0047         .bypass_reg = _sr,              \
0048         .bypass_mask    = _sm,              \
0049     }                           \
0050 
0051 static const struct linear_range dcdc1_dcdc2_ranges[] = {
0052     REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
0053     REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
0054 };
0055 
0056 static const struct linear_range ldo1_dcdc3_ranges[] = {
0057     REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
0058     REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
0059 };
0060 
0061 static const struct linear_range dcdc4_ranges[] = {
0062     REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
0063     REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
0064 };
0065 
0066 static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
0067                      unsigned selector)
0068 {
0069     int ret;
0070     struct tps65218 *tps = rdev_get_drvdata(dev);
0071     unsigned int rid = rdev_get_id(dev);
0072 
0073     /* Set the voltage based on vsel value and write protect level is 2 */
0074     ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
0075                 selector, TPS65218_PROTECT_L1);
0076 
0077     /* Set GO bit for DCDC1/2 to initiate voltage transistion */
0078     switch (rid) {
0079     case TPS65218_DCDC_1:
0080     case TPS65218_DCDC_2:
0081         ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
0082                     TPS65218_SLEW_RATE_GO,
0083                     TPS65218_SLEW_RATE_GO,
0084                     TPS65218_PROTECT_L1);
0085         break;
0086     }
0087 
0088     return ret;
0089 }
0090 
0091 static int tps65218_pmic_enable(struct regulator_dev *dev)
0092 {
0093     struct tps65218 *tps = rdev_get_drvdata(dev);
0094     int rid = rdev_get_id(dev);
0095 
0096     if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
0097         return -EINVAL;
0098 
0099     /* Enable the regulator and password protection is level 1 */
0100     return tps65218_set_bits(tps, dev->desc->enable_reg,
0101                  dev->desc->enable_mask, dev->desc->enable_mask,
0102                  TPS65218_PROTECT_L1);
0103 }
0104 
0105 static int tps65218_pmic_disable(struct regulator_dev *dev)
0106 {
0107     struct tps65218 *tps = rdev_get_drvdata(dev);
0108     int rid = rdev_get_id(dev);
0109 
0110     if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
0111         return -EINVAL;
0112 
0113     /* Disable the regulator and password protection is level 1 */
0114     return tps65218_clear_bits(tps, dev->desc->enable_reg,
0115                    dev->desc->enable_mask, TPS65218_PROTECT_L1);
0116 }
0117 
0118 static int tps65218_pmic_set_suspend_enable(struct regulator_dev *dev)
0119 {
0120     struct tps65218 *tps = rdev_get_drvdata(dev);
0121     unsigned int rid = rdev_get_id(dev);
0122 
0123     if (rid > TPS65218_LDO_1)
0124         return -EINVAL;
0125 
0126     return tps65218_clear_bits(tps, dev->desc->bypass_reg,
0127                    dev->desc->bypass_mask,
0128                    TPS65218_PROTECT_L1);
0129 }
0130 
0131 static int tps65218_pmic_set_suspend_disable(struct regulator_dev *dev)
0132 {
0133     struct tps65218 *tps = rdev_get_drvdata(dev);
0134     unsigned int rid = rdev_get_id(dev);
0135 
0136     if (rid > TPS65218_LDO_1)
0137         return -EINVAL;
0138 
0139     /*
0140      * Certain revisions of TPS65218 will need to have DCDC3 regulator
0141      * enabled always, otherwise an immediate system reboot will occur
0142      * during poweroff.
0143      */
0144     if (rid == TPS65218_DCDC_3 && tps->rev == TPS65218_REV_2_1)
0145         return 0;
0146 
0147     if (!tps->strobes[rid]) {
0148         if (rid == TPS65218_DCDC_3)
0149             tps->strobes[rid] = 3;
0150         else
0151             return -EINVAL;
0152     }
0153 
0154     return tps65218_set_bits(tps, dev->desc->bypass_reg,
0155                  dev->desc->bypass_mask,
0156                  tps->strobes[rid], TPS65218_PROTECT_L1);
0157 }
0158 
0159 /* Operations permitted on DCDC1, DCDC2 */
0160 static const struct regulator_ops tps65218_dcdc12_ops = {
0161     .is_enabled     = regulator_is_enabled_regmap,
0162     .enable         = tps65218_pmic_enable,
0163     .disable        = tps65218_pmic_disable,
0164     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0165     .set_voltage_sel    = tps65218_pmic_set_voltage_sel,
0166     .list_voltage       = regulator_list_voltage_linear_range,
0167     .map_voltage        = regulator_map_voltage_linear_range,
0168     .set_voltage_time_sel   = regulator_set_voltage_time_sel,
0169     .set_suspend_enable = tps65218_pmic_set_suspend_enable,
0170     .set_suspend_disable    = tps65218_pmic_set_suspend_disable,
0171 };
0172 
0173 /* Operations permitted on DCDC3, DCDC4 and LDO1 */
0174 static const struct regulator_ops tps65218_ldo1_dcdc34_ops = {
0175     .is_enabled     = regulator_is_enabled_regmap,
0176     .enable         = tps65218_pmic_enable,
0177     .disable        = tps65218_pmic_disable,
0178     .get_voltage_sel    = regulator_get_voltage_sel_regmap,
0179     .set_voltage_sel    = tps65218_pmic_set_voltage_sel,
0180     .list_voltage       = regulator_list_voltage_linear_range,
0181     .map_voltage        = regulator_map_voltage_linear_range,
0182     .set_suspend_enable = tps65218_pmic_set_suspend_enable,
0183     .set_suspend_disable    = tps65218_pmic_set_suspend_disable,
0184 };
0185 
0186 static const unsigned int ls3_currents[] = { 100000, 200000, 500000, 1000000 };
0187 
0188 static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
0189                            int lim_uA)
0190 {
0191     unsigned int index = 0;
0192     unsigned int num_currents = ARRAY_SIZE(ls3_currents);
0193     struct tps65218 *tps = rdev_get_drvdata(dev);
0194 
0195     while (index < num_currents && ls3_currents[index] != lim_uA)
0196         index++;
0197 
0198     if (index == num_currents)
0199         return -EINVAL;
0200 
0201     return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
0202                  index << __builtin_ctz(dev->desc->csel_mask),
0203                  TPS65218_PROTECT_L1);
0204 }
0205 
0206 static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
0207                        int min_uA, int max_uA)
0208 {
0209     int index = 0;
0210     unsigned int num_currents = ARRAY_SIZE(ls3_currents);
0211     struct tps65218 *tps = rdev_get_drvdata(dev);
0212 
0213     while (index < num_currents && ls3_currents[index] <= max_uA)
0214         index++;
0215 
0216     index--;
0217 
0218     if (index < 0 || ls3_currents[index] < min_uA)
0219         return -EINVAL;
0220 
0221     return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
0222                  index << __builtin_ctz(dev->desc->csel_mask),
0223                  TPS65218_PROTECT_L1);
0224 }
0225 
0226 static const struct regulator_ops tps65218_ls23_ops = {
0227     .is_enabled     = regulator_is_enabled_regmap,
0228     .enable         = tps65218_pmic_enable,
0229     .disable        = tps65218_pmic_disable,
0230     .set_input_current_limit = tps65218_pmic_set_input_current_lim,
0231     .set_current_limit  = tps65218_pmic_set_current_limit,
0232     .get_current_limit  = regulator_get_current_limit_regmap,
0233 };
0234 
0235 /* Operations permitted on DCDC5, DCDC6 */
0236 static const struct regulator_ops tps65218_dcdc56_pmic_ops = {
0237     .is_enabled     = regulator_is_enabled_regmap,
0238     .enable         = tps65218_pmic_enable,
0239     .disable        = tps65218_pmic_disable,
0240     .set_suspend_enable = tps65218_pmic_set_suspend_enable,
0241     .set_suspend_disable    = tps65218_pmic_set_suspend_disable,
0242 };
0243 
0244 static const struct regulator_desc regulators[] = {
0245     TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1,
0246                REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
0247                TPS65218_REG_CONTROL_DCDC1,
0248                TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
0249                TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
0250                2, 4000, 0, TPS65218_REG_SEQ3,
0251                TPS65218_SEQ3_DC1_SEQ_MASK, NULL, 0),
0252     TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2,
0253                REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
0254                TPS65218_REG_CONTROL_DCDC2,
0255                TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
0256                TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
0257                2, 4000, 0, TPS65218_REG_SEQ3,
0258                TPS65218_SEQ3_DC2_SEQ_MASK, NULL, 0),
0259     TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3,
0260                REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
0261                TPS65218_REG_CONTROL_DCDC3,
0262                TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
0263                TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
0264                0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK,
0265                NULL, 0),
0266     TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4,
0267                REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 53,
0268                TPS65218_REG_CONTROL_DCDC4,
0269                TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
0270                TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
0271                0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK,
0272                NULL, 0),
0273     TPS65218_REGULATOR("DCDC5", "regulator-dcdc5", TPS65218_DCDC_5,
0274                REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
0275                -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0,
0276                0, NULL, 0, 0, 1000000, TPS65218_REG_SEQ5,
0277                TPS65218_SEQ5_DC5_SEQ_MASK, NULL, 0),
0278     TPS65218_REGULATOR("DCDC6", "regulator-dcdc6", TPS65218_DCDC_6,
0279                REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
0280                -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0,
0281                0, NULL, 0, 0, 1800000, TPS65218_REG_SEQ5,
0282                TPS65218_SEQ5_DC6_SEQ_MASK, NULL, 0),
0283     TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1,
0284                REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
0285                TPS65218_REG_CONTROL_LDO1,
0286                TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
0287                TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
0288                2, 0, 0, TPS65218_REG_SEQ6,
0289                TPS65218_SEQ6_LDO1_SEQ_MASK, NULL, 0),
0290     TPS65218_REGULATOR("LS2", "regulator-ls2", TPS65218_LS_2,
0291                REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0,
0292                TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS2_EN,
0293                TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS2ILIM_MASK,
0294                NULL, 0, 0, 0, 0, 0, ls3_currents,
0295                ARRAY_SIZE(ls3_currents)),
0296     TPS65218_REGULATOR("LS3", "regulator-ls3", TPS65218_LS_3,
0297                REGULATOR_CURRENT, tps65218_ls23_ops, 0, 0, 0,
0298                TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS3_EN,
0299                TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS3ILIM_MASK,
0300                NULL, 0, 0, 0, 0, 0, ls3_currents,
0301                ARRAY_SIZE(ls3_currents)),
0302 };
0303 
0304 static int tps65218_regulator_probe(struct platform_device *pdev)
0305 {
0306     struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
0307     struct regulator_dev *rdev;
0308     struct regulator_config config = { };
0309     int i, ret;
0310     unsigned int val;
0311 
0312     config.dev = &pdev->dev;
0313     config.dev->of_node = tps->dev->of_node;
0314     config.driver_data = tps;
0315     config.regmap = tps->regmap;
0316 
0317     /* Allocate memory for strobes */
0318     tps->strobes = devm_kcalloc(&pdev->dev,
0319                     TPS65218_NUM_REGULATOR, sizeof(u8),
0320                     GFP_KERNEL);
0321     if (!tps->strobes)
0322         return -ENOMEM;
0323 
0324     for (i = 0; i < ARRAY_SIZE(regulators); i++) {
0325         rdev = devm_regulator_register(&pdev->dev, &regulators[i],
0326                            &config);
0327         if (IS_ERR(rdev)) {
0328             dev_err(tps->dev, "failed to register %s regulator\n",
0329                 pdev->name);
0330             return PTR_ERR(rdev);
0331         }
0332 
0333         ret = regmap_read(tps->regmap, regulators[i].bypass_reg, &val);
0334         if (ret)
0335             return ret;
0336 
0337         tps->strobes[i] = val & regulators[i].bypass_mask;
0338     }
0339 
0340     return 0;
0341 }
0342 
0343 static const struct platform_device_id tps65218_regulator_id_table[] = {
0344     { "tps65218-regulator", },
0345     { /* sentinel */ }
0346 };
0347 MODULE_DEVICE_TABLE(platform, tps65218_regulator_id_table);
0348 
0349 static struct platform_driver tps65218_regulator_driver = {
0350     .driver = {
0351         .name = "tps65218-pmic",
0352     },
0353     .probe = tps65218_regulator_probe,
0354     .id_table = tps65218_regulator_id_table,
0355 };
0356 
0357 module_platform_driver(tps65218_regulator_driver);
0358 
0359 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
0360 MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
0361 MODULE_ALIAS("platform:tps65218-pmic");
0362 MODULE_LICENSE("GPL v2");