0001
0002
0003
0004
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/regulator/driver.h>
0011 #include <linux/regulator/of_regulator.h>
0012
0013
0014
0015
0016 #define SC2731_PWR_WR_PROT 0xf0c
0017 #define SC2731_WR_UNLOCK_VALUE 0x6e7f
0018
0019
0020
0021
0022 #define SC2731_POWER_PD_SW 0xc28
0023 #define SC2731_LDO_CAMA0_PD 0xcfc
0024 #define SC2731_LDO_CAMA1_PD 0xd04
0025 #define SC2731_LDO_CAMMOT_PD 0xd0c
0026 #define SC2731_LDO_VLDO_PD 0xd6c
0027 #define SC2731_LDO_EMMCCORE_PD 0xd2c
0028 #define SC2731_LDO_SDCORE_PD 0xd74
0029 #define SC2731_LDO_SDIO_PD 0xd70
0030 #define SC2731_LDO_WIFIPA_PD 0xd4c
0031 #define SC2731_LDO_USB33_PD 0xd5c
0032 #define SC2731_LDO_CAMD0_PD 0xd7c
0033 #define SC2731_LDO_CAMD1_PD 0xd84
0034 #define SC2731_LDO_CON_PD 0xd8c
0035 #define SC2731_LDO_CAMIO_PD 0xd94
0036 #define SC2731_LDO_SRAM_PD 0xd78
0037
0038
0039
0040
0041 #define SC2731_DCDC_CPU0_PD_MASK BIT(4)
0042 #define SC2731_DCDC_CPU1_PD_MASK BIT(3)
0043 #define SC2731_DCDC_RF_PD_MASK BIT(11)
0044 #define SC2731_LDO_CAMA0_PD_MASK BIT(0)
0045 #define SC2731_LDO_CAMA1_PD_MASK BIT(0)
0046 #define SC2731_LDO_CAMMOT_PD_MASK BIT(0)
0047 #define SC2731_LDO_VLDO_PD_MASK BIT(0)
0048 #define SC2731_LDO_EMMCCORE_PD_MASK BIT(0)
0049 #define SC2731_LDO_SDCORE_PD_MASK BIT(0)
0050 #define SC2731_LDO_SDIO_PD_MASK BIT(0)
0051 #define SC2731_LDO_WIFIPA_PD_MASK BIT(0)
0052 #define SC2731_LDO_USB33_PD_MASK BIT(0)
0053 #define SC2731_LDO_CAMD0_PD_MASK BIT(0)
0054 #define SC2731_LDO_CAMD1_PD_MASK BIT(0)
0055 #define SC2731_LDO_CON_PD_MASK BIT(0)
0056 #define SC2731_LDO_CAMIO_PD_MASK BIT(0)
0057 #define SC2731_LDO_SRAM_PD_MASK BIT(0)
0058
0059
0060
0061
0062 #define SC2731_DCDC_CPU0_VOL 0xc54
0063 #define SC2731_DCDC_CPU1_VOL 0xc64
0064 #define SC2731_DCDC_RF_VOL 0xcb8
0065 #define SC2731_LDO_CAMA0_VOL 0xd00
0066 #define SC2731_LDO_CAMA1_VOL 0xd08
0067 #define SC2731_LDO_CAMMOT_VOL 0xd10
0068 #define SC2731_LDO_VLDO_VOL 0xd28
0069 #define SC2731_LDO_EMMCCORE_VOL 0xd30
0070 #define SC2731_LDO_SDCORE_VOL 0xd38
0071 #define SC2731_LDO_SDIO_VOL 0xd40
0072 #define SC2731_LDO_WIFIPA_VOL 0xd50
0073 #define SC2731_LDO_USB33_VOL 0xd60
0074 #define SC2731_LDO_CAMD0_VOL 0xd80
0075 #define SC2731_LDO_CAMD1_VOL 0xd88
0076 #define SC2731_LDO_CON_VOL 0xd90
0077 #define SC2731_LDO_CAMIO_VOL 0xd98
0078 #define SC2731_LDO_SRAM_VOL 0xdB0
0079
0080
0081
0082
0083 #define SC2731_DCDC_CPU0_VOL_MASK GENMASK(8, 0)
0084 #define SC2731_DCDC_CPU1_VOL_MASK GENMASK(8, 0)
0085 #define SC2731_DCDC_RF_VOL_MASK GENMASK(8, 0)
0086 #define SC2731_LDO_CAMA0_VOL_MASK GENMASK(7, 0)
0087 #define SC2731_LDO_CAMA1_VOL_MASK GENMASK(7, 0)
0088 #define SC2731_LDO_CAMMOT_VOL_MASK GENMASK(7, 0)
0089 #define SC2731_LDO_VLDO_VOL_MASK GENMASK(7, 0)
0090 #define SC2731_LDO_EMMCCORE_VOL_MASK GENMASK(7, 0)
0091 #define SC2731_LDO_SDCORE_VOL_MASK GENMASK(7, 0)
0092 #define SC2731_LDO_SDIO_VOL_MASK GENMASK(7, 0)
0093 #define SC2731_LDO_WIFIPA_VOL_MASK GENMASK(7, 0)
0094 #define SC2731_LDO_USB33_VOL_MASK GENMASK(7, 0)
0095 #define SC2731_LDO_CAMD0_VOL_MASK GENMASK(6, 0)
0096 #define SC2731_LDO_CAMD1_VOL_MASK GENMASK(6, 0)
0097 #define SC2731_LDO_CON_VOL_MASK GENMASK(6, 0)
0098 #define SC2731_LDO_CAMIO_VOL_MASK GENMASK(6, 0)
0099 #define SC2731_LDO_SRAM_VOL_MASK GENMASK(6, 0)
0100
0101 enum sc2731_regulator_id {
0102 SC2731_BUCK_CPU0,
0103 SC2731_BUCK_CPU1,
0104 SC2731_BUCK_RF,
0105 SC2731_LDO_CAMA0,
0106 SC2731_LDO_CAMA1,
0107 SC2731_LDO_CAMMOT,
0108 SC2731_LDO_VLDO,
0109 SC2731_LDO_EMMCCORE,
0110 SC2731_LDO_SDCORE,
0111 SC2731_LDO_SDIO,
0112 SC2731_LDO_WIFIPA,
0113 SC2731_LDO_USB33,
0114 SC2731_LDO_CAMD0,
0115 SC2731_LDO_CAMD1,
0116 SC2731_LDO_CON,
0117 SC2731_LDO_CAMIO,
0118 SC2731_LDO_SRAM,
0119 };
0120
0121 static const struct regulator_ops sc2731_regu_linear_ops = {
0122 .enable = regulator_enable_regmap,
0123 .disable = regulator_disable_regmap,
0124 .is_enabled = regulator_is_enabled_regmap,
0125 .list_voltage = regulator_list_voltage_linear,
0126 .get_voltage_sel = regulator_get_voltage_sel_regmap,
0127 .set_voltage_sel = regulator_set_voltage_sel_regmap,
0128 };
0129
0130 #define SC2731_REGU_LINEAR(_id, en_reg, en_mask, vreg, vmask, \
0131 vstep, vmin, vmax) { \
0132 .name = #_id, \
0133 .of_match = of_match_ptr(#_id), \
0134 .ops = &sc2731_regu_linear_ops, \
0135 .type = REGULATOR_VOLTAGE, \
0136 .id = SC2731_##_id, \
0137 .owner = THIS_MODULE, \
0138 .min_uV = vmin, \
0139 .n_voltages = ((vmax) - (vmin)) / (vstep) + 1, \
0140 .uV_step = vstep, \
0141 .enable_is_inverted = true, \
0142 .enable_val = 0, \
0143 .enable_reg = en_reg, \
0144 .enable_mask = en_mask, \
0145 .vsel_reg = vreg, \
0146 .vsel_mask = vmask, \
0147 }
0148
0149 static const struct regulator_desc regulators[] = {
0150 SC2731_REGU_LINEAR(BUCK_CPU0, SC2731_POWER_PD_SW,
0151 SC2731_DCDC_CPU0_PD_MASK, SC2731_DCDC_CPU0_VOL,
0152 SC2731_DCDC_CPU0_VOL_MASK, 3125, 400000, 1996875),
0153 SC2731_REGU_LINEAR(BUCK_CPU1, SC2731_POWER_PD_SW,
0154 SC2731_DCDC_CPU1_PD_MASK, SC2731_DCDC_CPU1_VOL,
0155 SC2731_DCDC_CPU1_VOL_MASK, 3125, 400000, 1996875),
0156 SC2731_REGU_LINEAR(BUCK_RF, SC2731_POWER_PD_SW, SC2731_DCDC_RF_PD_MASK,
0157 SC2731_DCDC_RF_VOL, SC2731_DCDC_RF_VOL_MASK,
0158 3125, 600000, 2196875),
0159 SC2731_REGU_LINEAR(LDO_CAMA0, SC2731_LDO_CAMA0_PD,
0160 SC2731_LDO_CAMA0_PD_MASK, SC2731_LDO_CAMA0_VOL,
0161 SC2731_LDO_CAMA0_VOL_MASK, 10000, 1200000, 3750000),
0162 SC2731_REGU_LINEAR(LDO_CAMA1, SC2731_LDO_CAMA1_PD,
0163 SC2731_LDO_CAMA1_PD_MASK, SC2731_LDO_CAMA1_VOL,
0164 SC2731_LDO_CAMA1_VOL_MASK, 10000, 1200000, 3750000),
0165 SC2731_REGU_LINEAR(LDO_CAMMOT, SC2731_LDO_CAMMOT_PD,
0166 SC2731_LDO_CAMMOT_PD_MASK, SC2731_LDO_CAMMOT_VOL,
0167 SC2731_LDO_CAMMOT_VOL_MASK, 10000, 1200000, 3750000),
0168 SC2731_REGU_LINEAR(LDO_VLDO, SC2731_LDO_VLDO_PD,
0169 SC2731_LDO_VLDO_PD_MASK, SC2731_LDO_VLDO_VOL,
0170 SC2731_LDO_VLDO_VOL_MASK, 10000, 1200000, 3750000),
0171 SC2731_REGU_LINEAR(LDO_EMMCCORE, SC2731_LDO_EMMCCORE_PD,
0172 SC2731_LDO_EMMCCORE_PD_MASK, SC2731_LDO_EMMCCORE_VOL,
0173 SC2731_LDO_EMMCCORE_VOL_MASK, 10000, 1200000,
0174 3750000),
0175 SC2731_REGU_LINEAR(LDO_SDCORE, SC2731_LDO_SDCORE_PD,
0176 SC2731_LDO_SDCORE_PD_MASK, SC2731_LDO_SDCORE_VOL,
0177 SC2731_LDO_SDCORE_VOL_MASK, 10000, 1200000, 3750000),
0178 SC2731_REGU_LINEAR(LDO_SDIO, SC2731_LDO_SDIO_PD,
0179 SC2731_LDO_SDIO_PD_MASK, SC2731_LDO_SDIO_VOL,
0180 SC2731_LDO_SDIO_VOL_MASK, 10000, 1200000, 3750000),
0181 SC2731_REGU_LINEAR(LDO_WIFIPA, SC2731_LDO_WIFIPA_PD,
0182 SC2731_LDO_WIFIPA_PD_MASK, SC2731_LDO_WIFIPA_VOL,
0183 SC2731_LDO_WIFIPA_VOL_MASK, 10000, 1200000, 3750000),
0184 SC2731_REGU_LINEAR(LDO_USB33, SC2731_LDO_USB33_PD,
0185 SC2731_LDO_USB33_PD_MASK, SC2731_LDO_USB33_VOL,
0186 SC2731_LDO_USB33_VOL_MASK, 10000, 1200000, 3750000),
0187 SC2731_REGU_LINEAR(LDO_CAMD0, SC2731_LDO_CAMD0_PD,
0188 SC2731_LDO_CAMD0_PD_MASK, SC2731_LDO_CAMD0_VOL,
0189 SC2731_LDO_CAMD0_VOL_MASK, 6250, 1000000, 1793750),
0190 SC2731_REGU_LINEAR(LDO_CAMD1, SC2731_LDO_CAMD1_PD,
0191 SC2731_LDO_CAMD1_PD_MASK, SC2731_LDO_CAMD1_VOL,
0192 SC2731_LDO_CAMD1_VOL_MASK, 6250, 1000000, 1793750),
0193 SC2731_REGU_LINEAR(LDO_CON, SC2731_LDO_CON_PD,
0194 SC2731_LDO_CON_PD_MASK, SC2731_LDO_CON_VOL,
0195 SC2731_LDO_CON_VOL_MASK, 6250, 1000000, 1793750),
0196 SC2731_REGU_LINEAR(LDO_CAMIO, SC2731_LDO_CAMIO_PD,
0197 SC2731_LDO_CAMIO_PD_MASK, SC2731_LDO_CAMIO_VOL,
0198 SC2731_LDO_CAMIO_VOL_MASK, 6250, 1000000, 1793750),
0199 SC2731_REGU_LINEAR(LDO_SRAM, SC2731_LDO_SRAM_PD,
0200 SC2731_LDO_SRAM_PD_MASK, SC2731_LDO_SRAM_VOL,
0201 SC2731_LDO_SRAM_VOL_MASK, 6250, 1000000, 1793750),
0202 };
0203
0204 static int sc2731_regulator_unlock(struct regmap *regmap)
0205 {
0206 return regmap_write(regmap, SC2731_PWR_WR_PROT,
0207 SC2731_WR_UNLOCK_VALUE);
0208 }
0209
0210 static int sc2731_regulator_probe(struct platform_device *pdev)
0211 {
0212 int i, ret;
0213 struct regmap *regmap;
0214 struct regulator_config config = { };
0215 struct regulator_dev *rdev;
0216
0217 regmap = dev_get_regmap(pdev->dev.parent, NULL);
0218 if (!regmap) {
0219 dev_err(&pdev->dev, "failed to get regmap.\n");
0220 return -ENODEV;
0221 }
0222
0223 ret = sc2731_regulator_unlock(regmap);
0224 if (ret) {
0225 dev_err(&pdev->dev, "failed to release regulator lock\n");
0226 return ret;
0227 }
0228
0229 config.dev = &pdev->dev;
0230 config.regmap = regmap;
0231
0232 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
0233 rdev = devm_regulator_register(&pdev->dev, ®ulators[i],
0234 &config);
0235 if (IS_ERR(rdev)) {
0236 dev_err(&pdev->dev, "failed to register regulator %s\n",
0237 regulators[i].name);
0238 return PTR_ERR(rdev);
0239 }
0240 }
0241
0242 return 0;
0243 }
0244
0245 static struct platform_driver sc2731_regulator_driver = {
0246 .driver = {
0247 .name = "sc27xx-regulator",
0248 },
0249 .probe = sc2731_regulator_probe,
0250 };
0251
0252 module_platform_driver(sc2731_regulator_driver);
0253
0254 MODULE_AUTHOR("Chen Junhui <erick.chen@spreadtrum.com>");
0255 MODULE_DESCRIPTION("Spreadtrum SC2731 regulator driver");
0256 MODULE_LICENSE("GPL v2");