Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (c) 2016 MediaTek Inc.
0004 // Author: Chen Zhong <chen.zhong@mediatek.com>
0005 
0006 #include <linux/module.h>
0007 #include <linux/of.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/regmap.h>
0010 #include <linux/mfd/mt6397/core.h>
0011 #include <linux/mfd/mt6323/registers.h>
0012 #include <linux/regulator/driver.h>
0013 #include <linux/regulator/machine.h>
0014 #include <linux/regulator/mt6323-regulator.h>
0015 #include <linux/regulator/of_regulator.h>
0016 
0017 #define MT6323_LDO_MODE_NORMAL  0
0018 #define MT6323_LDO_MODE_LP  1
0019 
0020 /*
0021  * MT6323 regulators' information
0022  *
0023  * @desc: standard fields of regulator description.
0024  * @qi: Mask for query enable signal status of regulators
0025  * @vselon_reg: Register sections for hardware control mode of bucks
0026  * @vselctrl_reg: Register for controlling the buck control mode.
0027  * @vselctrl_mask: Mask for query buck's voltage control mode.
0028  */
0029 struct mt6323_regulator_info {
0030     struct regulator_desc desc;
0031     u32 qi;
0032     u32 vselon_reg;
0033     u32 vselctrl_reg;
0034     u32 vselctrl_mask;
0035     u32 modeset_reg;
0036     u32 modeset_mask;
0037 };
0038 
0039 #define MT6323_BUCK(match, vreg, min, max, step, volt_ranges, enreg,    \
0040         vosel, vosel_mask, voselon, vosel_ctrl)         \
0041 [MT6323_ID_##vreg] = {                          \
0042     .desc = {                           \
0043         .name = #vreg,                      \
0044         .of_match = of_match_ptr(match),            \
0045         .ops = &mt6323_volt_range_ops,              \
0046         .type = REGULATOR_VOLTAGE,              \
0047         .id = MT6323_ID_##vreg,                 \
0048         .owner = THIS_MODULE,                   \
0049         .n_voltages = (max - min)/step + 1,         \
0050         .linear_ranges = volt_ranges,               \
0051         .n_linear_ranges = ARRAY_SIZE(volt_ranges),     \
0052         .vsel_reg = vosel,                  \
0053         .vsel_mask = vosel_mask,                \
0054         .enable_reg = enreg,                    \
0055         .enable_mask = BIT(0),                  \
0056     },                              \
0057     .qi = BIT(13),                          \
0058     .vselon_reg = voselon,                      \
0059     .vselctrl_reg = vosel_ctrl,                 \
0060     .vselctrl_mask = BIT(1),                    \
0061 }
0062 
0063 #define MT6323_LDO(match, vreg, ldo_volt_table, enreg, enbit, vosel,    \
0064         vosel_mask, _modeset_reg, _modeset_mask)        \
0065 [MT6323_ID_##vreg] = {                          \
0066     .desc = {                           \
0067         .name = #vreg,                      \
0068         .of_match = of_match_ptr(match),            \
0069         .ops = &mt6323_volt_table_ops,              \
0070         .type = REGULATOR_VOLTAGE,              \
0071         .id = MT6323_ID_##vreg,                 \
0072         .owner = THIS_MODULE,                   \
0073         .n_voltages = ARRAY_SIZE(ldo_volt_table),       \
0074         .volt_table = ldo_volt_table,               \
0075         .vsel_reg = vosel,                  \
0076         .vsel_mask = vosel_mask,                \
0077         .enable_reg = enreg,                    \
0078         .enable_mask = BIT(enbit),              \
0079     },                              \
0080     .qi = BIT(15),                          \
0081     .modeset_reg = _modeset_reg,                    \
0082     .modeset_mask = _modeset_mask,                  \
0083 }
0084 
0085 #define MT6323_REG_FIXED(match, vreg, enreg, enbit, volt,       \
0086         _modeset_reg, _modeset_mask)                \
0087 [MT6323_ID_##vreg] = {                          \
0088     .desc = {                           \
0089         .name = #vreg,                      \
0090         .of_match = of_match_ptr(match),            \
0091         .ops = &mt6323_volt_fixed_ops,              \
0092         .type = REGULATOR_VOLTAGE,              \
0093         .id = MT6323_ID_##vreg,                 \
0094         .owner = THIS_MODULE,                   \
0095         .n_voltages = 1,                    \
0096         .enable_reg = enreg,                    \
0097         .enable_mask = BIT(enbit),              \
0098         .min_uV = volt,                     \
0099     },                              \
0100     .qi = BIT(15),                          \
0101     .modeset_reg = _modeset_reg,                    \
0102     .modeset_mask = _modeset_mask,                  \
0103 }
0104 
0105 static const struct linear_range buck_volt_range1[] = {
0106     REGULATOR_LINEAR_RANGE(700000, 0, 0x7f, 6250),
0107 };
0108 
0109 static const struct linear_range buck_volt_range2[] = {
0110     REGULATOR_LINEAR_RANGE(1400000, 0, 0x7f, 12500),
0111 };
0112 
0113 static const struct linear_range buck_volt_range3[] = {
0114     REGULATOR_LINEAR_RANGE(500000, 0, 0x3f, 50000),
0115 };
0116 
0117 static const unsigned int ldo_volt_table1[] = {
0118     3300000, 3400000, 3500000, 3600000,
0119 };
0120 
0121 static const unsigned int ldo_volt_table2[] = {
0122     1500000, 1800000, 2500000, 2800000,
0123 };
0124 
0125 static const unsigned int ldo_volt_table3[] = {
0126     1800000, 3300000,
0127 };
0128 
0129 static const unsigned int ldo_volt_table4[] = {
0130     3000000, 3300000,
0131 };
0132 
0133 static const unsigned int ldo_volt_table5[] = {
0134     1200000, 1300000, 1500000, 1800000, 2000000, 2800000, 3000000, 3300000,
0135 };
0136 
0137 static const unsigned int ldo_volt_table6[] = {
0138     1200000, 1300000, 1500000, 1800000, 2500000, 2800000, 3000000, 2000000,
0139 };
0140 
0141 static const unsigned int ldo_volt_table7[] = {
0142     1200000, 1300000, 1500000, 1800000,
0143 };
0144 
0145 static const unsigned int ldo_volt_table8[] = {
0146     1800000, 3000000,
0147 };
0148 
0149 static const unsigned int ldo_volt_table9[] = {
0150     1200000, 1350000, 1500000, 1800000,
0151 };
0152 
0153 static const unsigned int ldo_volt_table10[] = {
0154     1200000, 1300000, 1500000, 1800000,
0155 };
0156 
0157 static int mt6323_get_status(struct regulator_dev *rdev)
0158 {
0159     int ret;
0160     u32 regval;
0161     struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
0162 
0163     ret = regmap_read(rdev->regmap, info->desc.enable_reg, &regval);
0164     if (ret != 0) {
0165         dev_err(&rdev->dev, "Failed to get enable reg: %d\n", ret);
0166         return ret;
0167     }
0168 
0169     return (regval & info->qi) ? REGULATOR_STATUS_ON : REGULATOR_STATUS_OFF;
0170 }
0171 
0172 static int mt6323_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
0173 {
0174     int ret, val = 0;
0175     struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
0176 
0177     if (!info->modeset_mask) {
0178         dev_err(&rdev->dev, "regulator %s doesn't support set_mode\n",
0179             info->desc.name);
0180         return -EINVAL;
0181     }
0182 
0183     switch (mode) {
0184     case REGULATOR_MODE_STANDBY:
0185         val = MT6323_LDO_MODE_LP;
0186         break;
0187     case REGULATOR_MODE_NORMAL:
0188         val = MT6323_LDO_MODE_NORMAL;
0189         break;
0190     default:
0191         return -EINVAL;
0192     }
0193 
0194     val <<= ffs(info->modeset_mask) - 1;
0195 
0196     ret = regmap_update_bits(rdev->regmap, info->modeset_reg,
0197                   info->modeset_mask, val);
0198 
0199     return ret;
0200 }
0201 
0202 static unsigned int mt6323_ldo_get_mode(struct regulator_dev *rdev)
0203 {
0204     unsigned int val;
0205     unsigned int mode;
0206     int ret;
0207     struct mt6323_regulator_info *info = rdev_get_drvdata(rdev);
0208 
0209     if (!info->modeset_mask) {
0210         dev_err(&rdev->dev, "regulator %s doesn't support get_mode\n",
0211             info->desc.name);
0212         return -EINVAL;
0213     }
0214 
0215     ret = regmap_read(rdev->regmap, info->modeset_reg, &val);
0216     if (ret < 0)
0217         return ret;
0218 
0219     val &= info->modeset_mask;
0220     val >>= ffs(info->modeset_mask) - 1;
0221 
0222     if (val & 0x1)
0223         mode = REGULATOR_MODE_STANDBY;
0224     else
0225         mode = REGULATOR_MODE_NORMAL;
0226 
0227     return mode;
0228 }
0229 
0230 static const struct regulator_ops mt6323_volt_range_ops = {
0231     .list_voltage = regulator_list_voltage_linear_range,
0232     .map_voltage = regulator_map_voltage_linear_range,
0233     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0234     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0235     .set_voltage_time_sel = regulator_set_voltage_time_sel,
0236     .enable = regulator_enable_regmap,
0237     .disable = regulator_disable_regmap,
0238     .is_enabled = regulator_is_enabled_regmap,
0239     .get_status = mt6323_get_status,
0240 };
0241 
0242 static const struct regulator_ops mt6323_volt_table_ops = {
0243     .list_voltage = regulator_list_voltage_table,
0244     .map_voltage = regulator_map_voltage_iterate,
0245     .set_voltage_sel = regulator_set_voltage_sel_regmap,
0246     .get_voltage_sel = regulator_get_voltage_sel_regmap,
0247     .set_voltage_time_sel = regulator_set_voltage_time_sel,
0248     .enable = regulator_enable_regmap,
0249     .disable = regulator_disable_regmap,
0250     .is_enabled = regulator_is_enabled_regmap,
0251     .get_status = mt6323_get_status,
0252     .set_mode = mt6323_ldo_set_mode,
0253     .get_mode = mt6323_ldo_get_mode,
0254 };
0255 
0256 static const struct regulator_ops mt6323_volt_fixed_ops = {
0257     .list_voltage = regulator_list_voltage_linear,
0258     .enable = regulator_enable_regmap,
0259     .disable = regulator_disable_regmap,
0260     .is_enabled = regulator_is_enabled_regmap,
0261     .get_status = mt6323_get_status,
0262     .set_mode = mt6323_ldo_set_mode,
0263     .get_mode = mt6323_ldo_get_mode,
0264 };
0265 
0266 /* The array is indexed by id(MT6323_ID_XXX) */
0267 static struct mt6323_regulator_info mt6323_regulators[] = {
0268     MT6323_BUCK("buck_vproc", VPROC, 700000, 1493750, 6250,
0269         buck_volt_range1, MT6323_VPROC_CON7, MT6323_VPROC_CON9, 0x7f,
0270         MT6323_VPROC_CON10, MT6323_VPROC_CON5),
0271     MT6323_BUCK("buck_vsys", VSYS, 1400000, 2987500, 12500,
0272         buck_volt_range2, MT6323_VSYS_CON7, MT6323_VSYS_CON9, 0x7f,
0273         MT6323_VSYS_CON10, MT6323_VSYS_CON5),
0274     MT6323_BUCK("buck_vpa", VPA, 500000, 3650000, 50000,
0275         buck_volt_range3, MT6323_VPA_CON7, MT6323_VPA_CON9,
0276         0x3f, MT6323_VPA_CON10, MT6323_VPA_CON5),
0277     MT6323_REG_FIXED("ldo_vtcxo", VTCXO, MT6323_ANALDO_CON1, 10, 2800000,
0278         MT6323_ANALDO_CON1, 0x2),
0279     MT6323_REG_FIXED("ldo_vcn28", VCN28, MT6323_ANALDO_CON19, 12, 2800000,
0280         MT6323_ANALDO_CON20, 0x2),
0281     MT6323_LDO("ldo_vcn33_bt", VCN33_BT, ldo_volt_table1,
0282         MT6323_ANALDO_CON16, 7, MT6323_ANALDO_CON16, 0xC,
0283         MT6323_ANALDO_CON21, 0x2),
0284     MT6323_LDO("ldo_vcn33_wifi", VCN33_WIFI, ldo_volt_table1,
0285         MT6323_ANALDO_CON17, 12, MT6323_ANALDO_CON16, 0xC,
0286         MT6323_ANALDO_CON21, 0x2),
0287     MT6323_REG_FIXED("ldo_va", VA, MT6323_ANALDO_CON2, 14, 2800000,
0288         MT6323_ANALDO_CON2, 0x2),
0289     MT6323_LDO("ldo_vcama", VCAMA, ldo_volt_table2,
0290         MT6323_ANALDO_CON4, 15, MT6323_ANALDO_CON10, 0x60, -1, 0),
0291     MT6323_REG_FIXED("ldo_vio28", VIO28, MT6323_DIGLDO_CON0, 14, 2800000,
0292         MT6323_DIGLDO_CON0, 0x2),
0293     MT6323_REG_FIXED("ldo_vusb", VUSB, MT6323_DIGLDO_CON2, 14, 3300000,
0294         MT6323_DIGLDO_CON2, 0x2),
0295     MT6323_LDO("ldo_vmc", VMC, ldo_volt_table3,
0296         MT6323_DIGLDO_CON3, 12, MT6323_DIGLDO_CON24, 0x10,
0297         MT6323_DIGLDO_CON3, 0x2),
0298     MT6323_LDO("ldo_vmch", VMCH, ldo_volt_table4,
0299         MT6323_DIGLDO_CON5, 14, MT6323_DIGLDO_CON26, 0x80,
0300         MT6323_DIGLDO_CON5, 0x2),
0301     MT6323_LDO("ldo_vemc3v3", VEMC3V3, ldo_volt_table4,
0302         MT6323_DIGLDO_CON6, 14, MT6323_DIGLDO_CON27, 0x80,
0303         MT6323_DIGLDO_CON6, 0x2),
0304     MT6323_LDO("ldo_vgp1", VGP1, ldo_volt_table5,
0305         MT6323_DIGLDO_CON7, 15, MT6323_DIGLDO_CON28, 0xE0,
0306         MT6323_DIGLDO_CON7, 0x2),
0307     MT6323_LDO("ldo_vgp2", VGP2, ldo_volt_table6,
0308         MT6323_DIGLDO_CON8, 15, MT6323_DIGLDO_CON29, 0xE0,
0309         MT6323_DIGLDO_CON8, 0x2),
0310     MT6323_LDO("ldo_vgp3", VGP3, ldo_volt_table7,
0311         MT6323_DIGLDO_CON9, 15, MT6323_DIGLDO_CON30, 0x60,
0312         MT6323_DIGLDO_CON9, 0x2),
0313     MT6323_REG_FIXED("ldo_vcn18", VCN18, MT6323_DIGLDO_CON11, 14, 1800000,
0314         MT6323_DIGLDO_CON11, 0x2),
0315     MT6323_LDO("ldo_vsim1", VSIM1, ldo_volt_table8,
0316         MT6323_DIGLDO_CON13, 15, MT6323_DIGLDO_CON34, 0x20,
0317         MT6323_DIGLDO_CON13, 0x2),
0318     MT6323_LDO("ldo_vsim2", VSIM2, ldo_volt_table8,
0319         MT6323_DIGLDO_CON14, 15, MT6323_DIGLDO_CON35, 0x20,
0320         MT6323_DIGLDO_CON14, 0x2),
0321     MT6323_REG_FIXED("ldo_vrtc", VRTC, MT6323_DIGLDO_CON15, 8, 2800000,
0322         -1, 0),
0323     MT6323_LDO("ldo_vcamaf", VCAMAF, ldo_volt_table5,
0324         MT6323_DIGLDO_CON31, 15, MT6323_DIGLDO_CON32, 0xE0,
0325         MT6323_DIGLDO_CON31, 0x2),
0326     MT6323_LDO("ldo_vibr", VIBR, ldo_volt_table5,
0327         MT6323_DIGLDO_CON39, 15, MT6323_DIGLDO_CON40, 0xE0,
0328         MT6323_DIGLDO_CON39, 0x2),
0329     MT6323_REG_FIXED("ldo_vrf18", VRF18, MT6323_DIGLDO_CON45, 15, 1825000,
0330         MT6323_DIGLDO_CON45, 0x2),
0331     MT6323_LDO("ldo_vm", VM, ldo_volt_table9,
0332         MT6323_DIGLDO_CON47, 14, MT6323_DIGLDO_CON48, 0x30,
0333         MT6323_DIGLDO_CON47, 0x2),
0334     MT6323_REG_FIXED("ldo_vio18", VIO18, MT6323_DIGLDO_CON49, 14, 1800000,
0335         MT6323_DIGLDO_CON49, 0x2),
0336     MT6323_LDO("ldo_vcamd", VCAMD, ldo_volt_table10,
0337         MT6323_DIGLDO_CON51, 14, MT6323_DIGLDO_CON52, 0x60,
0338         MT6323_DIGLDO_CON51, 0x2),
0339     MT6323_REG_FIXED("ldo_vcamio", VCAMIO, MT6323_DIGLDO_CON53, 14, 1800000,
0340         MT6323_DIGLDO_CON53, 0x2),
0341 };
0342 
0343 static int mt6323_set_buck_vosel_reg(struct platform_device *pdev)
0344 {
0345     struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
0346     int i;
0347     u32 regval;
0348 
0349     for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
0350         if (mt6323_regulators[i].vselctrl_reg) {
0351             if (regmap_read(mt6323->regmap,
0352                 mt6323_regulators[i].vselctrl_reg,
0353                 &regval) < 0) {
0354                 dev_err(&pdev->dev,
0355                     "Failed to read buck ctrl\n");
0356                 return -EIO;
0357             }
0358 
0359             if (regval & mt6323_regulators[i].vselctrl_mask) {
0360                 mt6323_regulators[i].desc.vsel_reg =
0361                 mt6323_regulators[i].vselon_reg;
0362             }
0363         }
0364     }
0365 
0366     return 0;
0367 }
0368 
0369 static int mt6323_regulator_probe(struct platform_device *pdev)
0370 {
0371     struct mt6397_chip *mt6323 = dev_get_drvdata(pdev->dev.parent);
0372     struct regulator_config config = {};
0373     struct regulator_dev *rdev;
0374     int i;
0375     u32 reg_value;
0376 
0377     /* Query buck controller to select activated voltage register part */
0378     if (mt6323_set_buck_vosel_reg(pdev))
0379         return -EIO;
0380 
0381     /* Read PMIC chip revision to update constraints and voltage table */
0382     if (regmap_read(mt6323->regmap, MT6323_CID, &reg_value) < 0) {
0383         dev_err(&pdev->dev, "Failed to read Chip ID\n");
0384         return -EIO;
0385     }
0386     dev_info(&pdev->dev, "Chip ID = 0x%x\n", reg_value);
0387 
0388     for (i = 0; i < MT6323_MAX_REGULATOR; i++) {
0389         config.dev = &pdev->dev;
0390         config.driver_data = &mt6323_regulators[i];
0391         config.regmap = mt6323->regmap;
0392         rdev = devm_regulator_register(&pdev->dev,
0393                 &mt6323_regulators[i].desc, &config);
0394         if (IS_ERR(rdev)) {
0395             dev_err(&pdev->dev, "failed to register %s\n",
0396                 mt6323_regulators[i].desc.name);
0397             return PTR_ERR(rdev);
0398         }
0399     }
0400     return 0;
0401 }
0402 
0403 static const struct platform_device_id mt6323_platform_ids[] = {
0404     {"mt6323-regulator", 0},
0405     { /* sentinel */ },
0406 };
0407 MODULE_DEVICE_TABLE(platform, mt6323_platform_ids);
0408 
0409 static struct platform_driver mt6323_regulator_driver = {
0410     .driver = {
0411         .name = "mt6323-regulator",
0412     },
0413     .probe = mt6323_regulator_probe,
0414     .id_table = mt6323_platform_ids,
0415 };
0416 
0417 module_platform_driver(mt6323_regulator_driver);
0418 
0419 MODULE_AUTHOR("Chen Zhong <chen.zhong@mediatek.com>");
0420 MODULE_DESCRIPTION("Regulator Driver for MediaTek MT6323 PMIC");
0421 MODULE_LICENSE("GPL v2");