0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/err.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regulator/driver.h>
0016 #include <linux/regulator/machine.h>
0017 #include <linux/regulator/tps6507x.h>
0018 #include <linux/of.h>
0019 #include <linux/slab.h>
0020 #include <linux/mfd/tps6507x.h>
0021 #include <linux/regulator/of_regulator.h>
0022
0023
0024 #define TPS6507X_DCDC_1 0
0025 #define TPS6507X_DCDC_2 1
0026 #define TPS6507X_DCDC_3 2
0027
0028 #define TPS6507X_LDO_1 3
0029 #define TPS6507X_LDO_2 4
0030
0031 #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
0032
0033
0034 #define TPS6507X_NUM_DCDC 3
0035
0036 #define TPS6507X_NUM_LDO 2
0037
0038 #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
0039
0040
0041 static const unsigned int VDCDCx_VSEL_table[] = {
0042 725000, 750000, 775000, 800000,
0043 825000, 850000, 875000, 900000,
0044 925000, 950000, 975000, 1000000,
0045 1025000, 1050000, 1075000, 1100000,
0046 1125000, 1150000, 1175000, 1200000,
0047 1225000, 1250000, 1275000, 1300000,
0048 1325000, 1350000, 1375000, 1400000,
0049 1425000, 1450000, 1475000, 1500000,
0050 1550000, 1600000, 1650000, 1700000,
0051 1750000, 1800000, 1850000, 1900000,
0052 1950000, 2000000, 2050000, 2100000,
0053 2150000, 2200000, 2250000, 2300000,
0054 2350000, 2400000, 2450000, 2500000,
0055 2550000, 2600000, 2650000, 2700000,
0056 2750000, 2800000, 2850000, 2900000,
0057 3000000, 3100000, 3200000, 3300000,
0058 };
0059
0060 static const unsigned int LDO1_VSEL_table[] = {
0061 1000000, 1100000, 1200000, 1250000,
0062 1300000, 1350000, 1400000, 1500000,
0063 1600000, 1800000, 2500000, 2750000,
0064 2800000, 3000000, 3100000, 3300000,
0065 };
0066
0067
0068 #define LDO2_VSEL_table VDCDCx_VSEL_table
0069
0070 struct tps_info {
0071 const char *name;
0072 u8 table_len;
0073 const unsigned int *table;
0074
0075
0076 bool defdcdc_default;
0077 };
0078
0079 static struct tps_info tps6507x_pmic_regs[] = {
0080 {
0081 .name = "VDCDC1",
0082 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
0083 .table = VDCDCx_VSEL_table,
0084 },
0085 {
0086 .name = "VDCDC2",
0087 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
0088 .table = VDCDCx_VSEL_table,
0089 },
0090 {
0091 .name = "VDCDC3",
0092 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
0093 .table = VDCDCx_VSEL_table,
0094 },
0095 {
0096 .name = "LDO1",
0097 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
0098 .table = LDO1_VSEL_table,
0099 },
0100 {
0101 .name = "LDO2",
0102 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
0103 .table = LDO2_VSEL_table,
0104 },
0105 };
0106
0107 struct tps6507x_pmic {
0108 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
0109 struct tps6507x_dev *mfd;
0110 struct tps_info *info[TPS6507X_NUM_REGULATOR];
0111 struct mutex io_lock;
0112 };
0113 static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
0114 {
0115 u8 val;
0116 int err;
0117
0118 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
0119
0120 if (err)
0121 return err;
0122
0123 return val;
0124 }
0125
0126 static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
0127 {
0128 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
0129 }
0130
0131 static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
0132 {
0133 int err, data;
0134
0135 mutex_lock(&tps->io_lock);
0136
0137 data = tps6507x_pmic_read(tps, reg);
0138 if (data < 0) {
0139 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
0140 err = data;
0141 goto out;
0142 }
0143
0144 data |= mask;
0145 err = tps6507x_pmic_write(tps, reg, data);
0146 if (err)
0147 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
0148
0149 out:
0150 mutex_unlock(&tps->io_lock);
0151 return err;
0152 }
0153
0154 static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
0155 {
0156 int err, data;
0157
0158 mutex_lock(&tps->io_lock);
0159
0160 data = tps6507x_pmic_read(tps, reg);
0161 if (data < 0) {
0162 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
0163 err = data;
0164 goto out;
0165 }
0166
0167 data &= ~mask;
0168 err = tps6507x_pmic_write(tps, reg, data);
0169 if (err)
0170 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
0171
0172 out:
0173 mutex_unlock(&tps->io_lock);
0174 return err;
0175 }
0176
0177 static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
0178 {
0179 int data;
0180
0181 mutex_lock(&tps->io_lock);
0182
0183 data = tps6507x_pmic_read(tps, reg);
0184 if (data < 0)
0185 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
0186
0187 mutex_unlock(&tps->io_lock);
0188 return data;
0189 }
0190
0191 static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
0192 {
0193 int err;
0194
0195 mutex_lock(&tps->io_lock);
0196
0197 err = tps6507x_pmic_write(tps, reg, val);
0198 if (err < 0)
0199 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
0200
0201 mutex_unlock(&tps->io_lock);
0202 return err;
0203 }
0204
0205 static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
0206 {
0207 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
0208 int data, rid = rdev_get_id(dev);
0209 u8 shift;
0210
0211 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
0212 return -EINVAL;
0213
0214 shift = TPS6507X_MAX_REG_ID - rid;
0215 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
0216
0217 if (data < 0)
0218 return data;
0219 else
0220 return (data & 1<<shift) ? 1 : 0;
0221 }
0222
0223 static int tps6507x_pmic_enable(struct regulator_dev *dev)
0224 {
0225 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
0226 int rid = rdev_get_id(dev);
0227 u8 shift;
0228
0229 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
0230 return -EINVAL;
0231
0232 shift = TPS6507X_MAX_REG_ID - rid;
0233 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
0234 }
0235
0236 static int tps6507x_pmic_disable(struct regulator_dev *dev)
0237 {
0238 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
0239 int rid = rdev_get_id(dev);
0240 u8 shift;
0241
0242 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
0243 return -EINVAL;
0244
0245 shift = TPS6507X_MAX_REG_ID - rid;
0246 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
0247 1 << shift);
0248 }
0249
0250 static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
0251 {
0252 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
0253 int data, rid = rdev_get_id(dev);
0254 u8 reg, mask;
0255
0256 switch (rid) {
0257 case TPS6507X_DCDC_1:
0258 reg = TPS6507X_REG_DEFDCDC1;
0259 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0260 break;
0261 case TPS6507X_DCDC_2:
0262 if (tps->info[rid]->defdcdc_default)
0263 reg = TPS6507X_REG_DEFDCDC2_HIGH;
0264 else
0265 reg = TPS6507X_REG_DEFDCDC2_LOW;
0266 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0267 break;
0268 case TPS6507X_DCDC_3:
0269 if (tps->info[rid]->defdcdc_default)
0270 reg = TPS6507X_REG_DEFDCDC3_HIGH;
0271 else
0272 reg = TPS6507X_REG_DEFDCDC3_LOW;
0273 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0274 break;
0275 case TPS6507X_LDO_1:
0276 reg = TPS6507X_REG_LDO_CTRL1;
0277 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
0278 break;
0279 case TPS6507X_LDO_2:
0280 reg = TPS6507X_REG_DEFLDO2;
0281 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
0282 break;
0283 default:
0284 return -EINVAL;
0285 }
0286
0287 data = tps6507x_pmic_reg_read(tps, reg);
0288 if (data < 0)
0289 return data;
0290
0291 data &= mask;
0292 return data;
0293 }
0294
0295 static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
0296 unsigned selector)
0297 {
0298 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
0299 int data, rid = rdev_get_id(dev);
0300 u8 reg, mask;
0301
0302 switch (rid) {
0303 case TPS6507X_DCDC_1:
0304 reg = TPS6507X_REG_DEFDCDC1;
0305 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0306 break;
0307 case TPS6507X_DCDC_2:
0308 if (tps->info[rid]->defdcdc_default)
0309 reg = TPS6507X_REG_DEFDCDC2_HIGH;
0310 else
0311 reg = TPS6507X_REG_DEFDCDC2_LOW;
0312 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0313 break;
0314 case TPS6507X_DCDC_3:
0315 if (tps->info[rid]->defdcdc_default)
0316 reg = TPS6507X_REG_DEFDCDC3_HIGH;
0317 else
0318 reg = TPS6507X_REG_DEFDCDC3_LOW;
0319 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
0320 break;
0321 case TPS6507X_LDO_1:
0322 reg = TPS6507X_REG_LDO_CTRL1;
0323 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
0324 break;
0325 case TPS6507X_LDO_2:
0326 reg = TPS6507X_REG_DEFLDO2;
0327 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
0328 break;
0329 default:
0330 return -EINVAL;
0331 }
0332
0333 data = tps6507x_pmic_reg_read(tps, reg);
0334 if (data < 0)
0335 return data;
0336
0337 data &= ~mask;
0338 data |= selector;
0339
0340 return tps6507x_pmic_reg_write(tps, reg, data);
0341 }
0342
0343 static const struct regulator_ops tps6507x_pmic_ops = {
0344 .is_enabled = tps6507x_pmic_is_enabled,
0345 .enable = tps6507x_pmic_enable,
0346 .disable = tps6507x_pmic_disable,
0347 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
0348 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
0349 .list_voltage = regulator_list_voltage_table,
0350 .map_voltage = regulator_map_voltage_ascend,
0351 };
0352
0353 static int tps6507x_pmic_of_parse_cb(struct device_node *np,
0354 const struct regulator_desc *desc,
0355 struct regulator_config *config)
0356 {
0357 struct tps6507x_pmic *tps = config->driver_data;
0358 struct tps_info *info = tps->info[desc->id];
0359 u32 prop;
0360 int ret;
0361
0362 ret = of_property_read_u32(np, "ti,defdcdc_default", &prop);
0363 if (!ret)
0364 info->defdcdc_default = prop;
0365
0366 return 0;
0367 }
0368
0369 static int tps6507x_pmic_probe(struct platform_device *pdev)
0370 {
0371 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
0372 struct tps_info *info = &tps6507x_pmic_regs[0];
0373 struct regulator_config config = { };
0374 struct regulator_init_data *init_data = NULL;
0375 struct regulator_dev *rdev;
0376 struct tps6507x_pmic *tps;
0377 struct tps6507x_board *tps_board;
0378 int i;
0379
0380
0381
0382
0383
0384
0385 tps_board = dev_get_platdata(tps6507x_dev->dev);
0386 if (tps_board)
0387 init_data = tps_board->tps6507x_pmic_init_data;
0388
0389 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
0390 if (!tps)
0391 return -ENOMEM;
0392
0393 mutex_init(&tps->io_lock);
0394
0395
0396 tps->mfd = tps6507x_dev;
0397
0398 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++) {
0399
0400 tps->info[i] = info;
0401 if (init_data && init_data[i].driver_data) {
0402 struct tps6507x_reg_platform_data *data =
0403 init_data[i].driver_data;
0404 info->defdcdc_default = data->defdcdc_default;
0405 }
0406
0407 tps->desc[i].name = info->name;
0408 tps->desc[i].of_match = of_match_ptr(info->name);
0409 tps->desc[i].regulators_node = of_match_ptr("regulators");
0410 tps->desc[i].of_parse_cb = tps6507x_pmic_of_parse_cb;
0411 tps->desc[i].id = i;
0412 tps->desc[i].n_voltages = info->table_len;
0413 tps->desc[i].volt_table = info->table;
0414 tps->desc[i].ops = &tps6507x_pmic_ops;
0415 tps->desc[i].type = REGULATOR_VOLTAGE;
0416 tps->desc[i].owner = THIS_MODULE;
0417
0418 config.dev = tps6507x_dev->dev;
0419 config.init_data = init_data;
0420 config.driver_data = tps;
0421
0422 rdev = devm_regulator_register(&pdev->dev, &tps->desc[i],
0423 &config);
0424 if (IS_ERR(rdev)) {
0425 dev_err(tps6507x_dev->dev,
0426 "failed to register %s regulator\n",
0427 pdev->name);
0428 return PTR_ERR(rdev);
0429 }
0430 }
0431
0432 tps6507x_dev->pmic = tps;
0433 platform_set_drvdata(pdev, tps6507x_dev);
0434
0435 return 0;
0436 }
0437
0438 static struct platform_driver tps6507x_pmic_driver = {
0439 .driver = {
0440 .name = "tps6507x-pmic",
0441 },
0442 .probe = tps6507x_pmic_probe,
0443 };
0444
0445 static int __init tps6507x_pmic_init(void)
0446 {
0447 return platform_driver_register(&tps6507x_pmic_driver);
0448 }
0449 subsys_initcall(tps6507x_pmic_init);
0450
0451 static void __exit tps6507x_pmic_cleanup(void)
0452 {
0453 platform_driver_unregister(&tps6507x_pmic_driver);
0454 }
0455 module_exit(tps6507x_pmic_cleanup);
0456
0457 MODULE_AUTHOR("Texas Instruments");
0458 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
0459 MODULE_LICENSE("GPL v2");
0460 MODULE_ALIAS("platform:tps6507x-pmic");