0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/delay.h>
0011 #include <linux/init.h>
0012 #include <linux/of.h>
0013 #include <linux/gpio/consumer.h>
0014 #include <linux/slab.h>
0015 #include <linux/err.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/regulator/driver.h>
0018 #include <linux/regulator/machine.h>
0019 #include <linux/regulator/of_regulator.h>
0020 #include <linux/mfd/tps65090.h>
0021
0022 #define MAX_CTRL_READ_TRIES 5
0023 #define MAX_FET_ENABLE_TRIES 1000
0024
0025 #define CTRL_EN_BIT 0
0026 #define CTRL_WT_BIT 2
0027 #define CTRL_PG_BIT 4
0028 #define CTRL_TO_BIT 7
0029
0030 #define MAX_OVERCURRENT_WAIT 3
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 struct tps65090_regulator {
0043 struct device *dev;
0044 struct regulator_desc *desc;
0045 struct regulator_dev *rdev;
0046 bool overcurrent_wait_valid;
0047 int overcurrent_wait;
0048 };
0049
0050 static const struct regulator_ops tps65090_ext_control_ops = {
0051 };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
0065 struct regulator_dev *rdev)
0066 {
0067 int ret;
0068
0069 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0070 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
0071 ri->overcurrent_wait << CTRL_WT_BIT);
0072 if (ret) {
0073 dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
0074 rdev->desc->enable_reg);
0075 }
0076
0077 return ret;
0078 }
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 static int tps65090_try_enable_fet(struct regulator_dev *rdev)
0089 {
0090 unsigned int control;
0091 int ret, i;
0092
0093 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0094 rdev->desc->enable_mask,
0095 rdev->desc->enable_mask);
0096 if (ret < 0) {
0097 dev_err(&rdev->dev, "Error in updating reg %#x\n",
0098 rdev->desc->enable_reg);
0099 return ret;
0100 }
0101
0102 for (i = 0; i < MAX_CTRL_READ_TRIES; i++) {
0103 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg,
0104 &control);
0105 if (ret < 0)
0106 return ret;
0107
0108 if (!(control & BIT(CTRL_TO_BIT)))
0109 break;
0110
0111 usleep_range(1000, 1500);
0112 }
0113 if (!(control & BIT(CTRL_PG_BIT)))
0114 return -ENOTRECOVERABLE;
0115
0116 return 0;
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133 static int tps65090_fet_enable(struct regulator_dev *rdev)
0134 {
0135 int ret, tries;
0136
0137
0138
0139
0140
0141 tries = 0;
0142 while (true) {
0143 ret = tps65090_try_enable_fet(rdev);
0144 if (!ret)
0145 break;
0146 if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES)
0147 goto err;
0148
0149
0150 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
0151 rdev->desc->enable_mask, 0);
0152 if (ret)
0153 goto err;
0154
0155 tries++;
0156 }
0157
0158 if (tries)
0159 dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n",
0160 rdev->desc->enable_reg, tries);
0161
0162 return 0;
0163 err:
0164 dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
0165 WARN_ON(1);
0166
0167 return ret;
0168 }
0169
0170 static const struct regulator_ops tps65090_reg_control_ops = {
0171 .enable = regulator_enable_regmap,
0172 .disable = regulator_disable_regmap,
0173 .is_enabled = regulator_is_enabled_regmap,
0174 };
0175
0176 static const struct regulator_ops tps65090_fet_control_ops = {
0177 .enable = tps65090_fet_enable,
0178 .disable = regulator_disable_regmap,
0179 .is_enabled = regulator_is_enabled_regmap,
0180 };
0181
0182 static const struct regulator_ops tps65090_ldo_ops = {
0183 };
0184
0185 #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
0186 { \
0187 .name = "TPS65090_RAILS"#_id, \
0188 .supply_name = _sname, \
0189 .id = TPS65090_REGULATOR_##_id, \
0190 .n_voltages = _nvolt, \
0191 .ops = &_ops, \
0192 .fixed_uV = _volt, \
0193 .enable_reg = _en_reg, \
0194 .enable_val = _en_bits, \
0195 .enable_mask = _en_bits, \
0196 .type = REGULATOR_VOLTAGE, \
0197 .owner = THIS_MODULE, \
0198 }
0199
0200 #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
0201 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
0202
0203 #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
0204 tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
0205
0206 static struct regulator_desc tps65090_regulator_desc[] = {
0207 tps65090_REG_FIXEDV(DCDC1, "vsys1", 0x0C, BIT(CTRL_EN_BIT), 5000000,
0208 tps65090_reg_control_ops),
0209 tps65090_REG_FIXEDV(DCDC2, "vsys2", 0x0D, BIT(CTRL_EN_BIT), 3300000,
0210 tps65090_reg_control_ops),
0211 tps65090_REG_SWITCH(DCDC3, "vsys3", 0x0E, BIT(CTRL_EN_BIT),
0212 tps65090_reg_control_ops),
0213
0214 tps65090_REG_SWITCH(FET1, "infet1", 0x0F,
0215 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0216 tps65090_fet_control_ops),
0217 tps65090_REG_SWITCH(FET2, "infet2", 0x10,
0218 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0219 tps65090_fet_control_ops),
0220 tps65090_REG_SWITCH(FET3, "infet3", 0x11,
0221 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0222 tps65090_fet_control_ops),
0223 tps65090_REG_SWITCH(FET4, "infet4", 0x12,
0224 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0225 tps65090_fet_control_ops),
0226 tps65090_REG_SWITCH(FET5, "infet5", 0x13,
0227 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0228 tps65090_fet_control_ops),
0229 tps65090_REG_SWITCH(FET6, "infet6", 0x14,
0230 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0231 tps65090_fet_control_ops),
0232 tps65090_REG_SWITCH(FET7, "infet7", 0x15,
0233 BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
0234 tps65090_fet_control_ops),
0235
0236 tps65090_REG_FIXEDV(LDO1, "vsys-l1", 0, 0, 5000000,
0237 tps65090_ldo_ops),
0238 tps65090_REG_FIXEDV(LDO2, "vsys-l2", 0, 0, 3300000,
0239 tps65090_ldo_ops),
0240 };
0241
0242 static inline bool is_dcdc(int id)
0243 {
0244 switch (id) {
0245 case TPS65090_REGULATOR_DCDC1:
0246 case TPS65090_REGULATOR_DCDC2:
0247 case TPS65090_REGULATOR_DCDC3:
0248 return true;
0249 default:
0250 return false;
0251 }
0252 }
0253
0254 static int tps65090_config_ext_control(
0255 struct tps65090_regulator *ri, bool enable)
0256 {
0257 int ret;
0258 struct device *parent = ri->dev->parent;
0259 unsigned int reg_en_reg = ri->desc->enable_reg;
0260
0261 if (enable)
0262 ret = tps65090_set_bits(parent, reg_en_reg, 1);
0263 else
0264 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
0265 if (ret < 0)
0266 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
0267 return ret;
0268 }
0269
0270 static int tps65090_regulator_disable_ext_control(
0271 struct tps65090_regulator *ri,
0272 struct tps65090_regulator_plat_data *tps_pdata)
0273 {
0274 int ret = 0;
0275 struct device *parent = ri->dev->parent;
0276 unsigned int reg_en_reg = ri->desc->enable_reg;
0277
0278
0279
0280
0281
0282 if (tps_pdata->reg_init_data->constraints.always_on ||
0283 tps_pdata->reg_init_data->constraints.boot_on) {
0284 ret = tps65090_set_bits(parent, reg_en_reg, 0);
0285 if (ret < 0) {
0286 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
0287 return ret;
0288 }
0289 }
0290 return tps65090_config_ext_control(ri, false);
0291 }
0292
0293 #ifdef CONFIG_OF
0294 static struct of_regulator_match tps65090_matches[] = {
0295 { .name = "dcdc1", },
0296 { .name = "dcdc2", },
0297 { .name = "dcdc3", },
0298 { .name = "fet1", },
0299 { .name = "fet2", },
0300 { .name = "fet3", },
0301 { .name = "fet4", },
0302 { .name = "fet5", },
0303 { .name = "fet6", },
0304 { .name = "fet7", },
0305 { .name = "ldo1", },
0306 { .name = "ldo2", },
0307 };
0308
0309 static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
0310 struct platform_device *pdev,
0311 struct of_regulator_match **tps65090_reg_matches)
0312 {
0313 struct tps65090_platform_data *tps65090_pdata;
0314 struct device_node *np = pdev->dev.parent->of_node;
0315 struct device_node *regulators;
0316 int idx = 0, ret;
0317 struct tps65090_regulator_plat_data *reg_pdata;
0318
0319 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
0320 GFP_KERNEL);
0321 if (!tps65090_pdata)
0322 return ERR_PTR(-ENOMEM);
0323
0324 reg_pdata = devm_kcalloc(&pdev->dev,
0325 TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
0326 GFP_KERNEL);
0327 if (!reg_pdata)
0328 return ERR_PTR(-ENOMEM);
0329
0330 regulators = of_get_child_by_name(np, "regulators");
0331 if (!regulators) {
0332 dev_err(&pdev->dev, "regulator node not found\n");
0333 return ERR_PTR(-ENODEV);
0334 }
0335
0336 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
0337 ARRAY_SIZE(tps65090_matches));
0338 of_node_put(regulators);
0339 if (ret < 0) {
0340 dev_err(&pdev->dev,
0341 "Error parsing regulator init data: %d\n", ret);
0342 return ERR_PTR(ret);
0343 }
0344
0345 *tps65090_reg_matches = tps65090_matches;
0346 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
0347 struct regulator_init_data *ri_data;
0348 struct tps65090_regulator_plat_data *rpdata;
0349 struct device_node *np;
0350
0351 rpdata = ®_pdata[idx];
0352 ri_data = tps65090_matches[idx].init_data;
0353 if (!ri_data)
0354 continue;
0355
0356 np = tps65090_matches[idx].of_node;
0357 if (!np)
0358 continue;
0359
0360 rpdata->reg_init_data = ri_data;
0361 rpdata->enable_ext_control = of_property_read_bool(np,
0362 "ti,enable-ext-control");
0363 if (rpdata->enable_ext_control) {
0364 enum gpiod_flags gflags;
0365
0366 if (ri_data->constraints.always_on ||
0367 ri_data->constraints.boot_on)
0368 gflags = GPIOD_OUT_HIGH;
0369 else
0370 gflags = GPIOD_OUT_LOW;
0371 gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
0372
0373 rpdata->gpiod = devm_fwnode_gpiod_get(
0374 &pdev->dev,
0375 of_fwnode_handle(np),
0376 "dcdc-ext-control",
0377 gflags,
0378 "tps65090");
0379 if (PTR_ERR(rpdata->gpiod) == -ENOENT) {
0380 dev_err(&pdev->dev,
0381 "could not find DCDC external control GPIO\n");
0382 rpdata->gpiod = NULL;
0383 } else if (IS_ERR(rpdata->gpiod))
0384 return ERR_CAST(rpdata->gpiod);
0385 }
0386
0387 if (of_property_read_u32(np, "ti,overcurrent-wait",
0388 &rpdata->overcurrent_wait) == 0)
0389 rpdata->overcurrent_wait_valid = true;
0390
0391 tps65090_pdata->reg_pdata[idx] = rpdata;
0392 }
0393 return tps65090_pdata;
0394 }
0395 #else
0396 static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
0397 struct platform_device *pdev,
0398 struct of_regulator_match **tps65090_reg_matches)
0399 {
0400 *tps65090_reg_matches = NULL;
0401 return NULL;
0402 }
0403 #endif
0404
0405 static int tps65090_regulator_probe(struct platform_device *pdev)
0406 {
0407 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
0408 struct tps65090_regulator *ri = NULL;
0409 struct regulator_config config = { };
0410 struct regulator_dev *rdev;
0411 struct tps65090_regulator_plat_data *tps_pdata;
0412 struct tps65090_regulator *pmic;
0413 struct tps65090_platform_data *tps65090_pdata;
0414 struct of_regulator_match *tps65090_reg_matches = NULL;
0415 int num;
0416 int ret;
0417
0418 dev_dbg(&pdev->dev, "Probing regulator\n");
0419
0420 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
0421 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
0422 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
0423 &tps65090_reg_matches);
0424 if (IS_ERR_OR_NULL(tps65090_pdata)) {
0425 dev_err(&pdev->dev, "Platform data missing\n");
0426 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
0427 }
0428
0429 pmic = devm_kcalloc(&pdev->dev,
0430 TPS65090_REGULATOR_MAX, sizeof(*pmic),
0431 GFP_KERNEL);
0432 if (!pmic)
0433 return -ENOMEM;
0434
0435 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
0436 tps_pdata = tps65090_pdata->reg_pdata[num];
0437
0438 ri = &pmic[num];
0439 ri->dev = &pdev->dev;
0440 ri->desc = &tps65090_regulator_desc[num];
0441 if (tps_pdata) {
0442 ri->overcurrent_wait_valid =
0443 tps_pdata->overcurrent_wait_valid;
0444 ri->overcurrent_wait = tps_pdata->overcurrent_wait;
0445 }
0446
0447
0448
0449
0450
0451 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
0452 if (tps_pdata->enable_ext_control) {
0453 config.ena_gpiod = tps_pdata->gpiod;
0454 ri->desc->ops = &tps65090_ext_control_ops;
0455 } else {
0456 ret = tps65090_regulator_disable_ext_control(
0457 ri, tps_pdata);
0458 if (ret < 0) {
0459 dev_err(&pdev->dev,
0460 "failed disable ext control\n");
0461 return ret;
0462 }
0463 }
0464 }
0465
0466 config.dev = pdev->dev.parent;
0467 config.driver_data = ri;
0468 config.regmap = tps65090_mfd->rmap;
0469 if (tps_pdata)
0470 config.init_data = tps_pdata->reg_init_data;
0471 else
0472 config.init_data = NULL;
0473 if (tps65090_reg_matches)
0474 config.of_node = tps65090_reg_matches[num].of_node;
0475 else
0476 config.of_node = NULL;
0477
0478
0479
0480
0481
0482 if (config.ena_gpiod)
0483 devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
0484 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
0485 if (IS_ERR(rdev)) {
0486 dev_err(&pdev->dev, "failed to register regulator %s\n",
0487 ri->desc->name);
0488 return PTR_ERR(rdev);
0489 }
0490 ri->rdev = rdev;
0491
0492 if (ri->overcurrent_wait_valid) {
0493 ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
0494 if (ret < 0)
0495 return ret;
0496 }
0497
0498
0499 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
0500 tps_pdata->enable_ext_control) {
0501 ret = tps65090_config_ext_control(ri, true);
0502 if (ret < 0)
0503 return ret;
0504 }
0505 }
0506
0507 platform_set_drvdata(pdev, pmic);
0508 return 0;
0509 }
0510
0511 static struct platform_driver tps65090_regulator_driver = {
0512 .driver = {
0513 .name = "tps65090-pmic",
0514 },
0515 .probe = tps65090_regulator_probe,
0516 };
0517
0518 static int __init tps65090_regulator_init(void)
0519 {
0520 return platform_driver_register(&tps65090_regulator_driver);
0521 }
0522 subsys_initcall(tps65090_regulator_init);
0523
0524 static void __exit tps65090_regulator_exit(void)
0525 {
0526 platform_driver_unregister(&tps65090_regulator_driver);
0527 }
0528 module_exit(tps65090_regulator_exit);
0529
0530 MODULE_DESCRIPTION("tps65090 regulator driver");
0531 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
0532 MODULE_LICENSE("GPL v2");
0533 MODULE_ALIAS("platform:tps65090-pmic");