tps65090-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Regulator driver for tps65090 power management chip.
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/of.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/slab.h>
  13. #include <linux/err.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/regulator/of_regulator.h>
  18. #include <linux/mfd/tps65090.h>
  19. #define MAX_CTRL_READ_TRIES 5
  20. #define MAX_FET_ENABLE_TRIES 1000
  21. #define CTRL_EN_BIT 0 /* Regulator enable bit, active high */
  22. #define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */
  23. #define CTRL_PG_BIT 4 /* Regulator power good bit, 1=good */
  24. #define CTRL_TO_BIT 7 /* Regulator timeout bit, 1=wait */
  25. #define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */
  26. /**
  27. * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
  28. *
  29. * @dev: Pointer to our device.
  30. * @desc: The struct regulator_desc for the regulator.
  31. * @rdev: The struct regulator_dev for the regulator.
  32. * @overcurrent_wait_valid: True if overcurrent_wait is valid.
  33. * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
  34. */
  35. struct tps65090_regulator {
  36. struct device *dev;
  37. struct regulator_desc *desc;
  38. struct regulator_dev *rdev;
  39. bool overcurrent_wait_valid;
  40. int overcurrent_wait;
  41. };
  42. static const struct regulator_ops tps65090_ext_control_ops = {
  43. };
  44. /**
  45. * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
  46. *
  47. * This will set the overcurrent wait time based on what's in the regulator
  48. * info.
  49. *
  50. * @ri: Overall regulator data
  51. * @rdev: Regulator device
  52. *
  53. * Return: 0 if no error, non-zero if there was an error writing the register.
  54. */
  55. static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
  56. struct regulator_dev *rdev)
  57. {
  58. int ret;
  59. ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  60. MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
  61. ri->overcurrent_wait << CTRL_WT_BIT);
  62. if (ret) {
  63. dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
  64. rdev->desc->enable_reg);
  65. }
  66. return ret;
  67. }
  68. /**
  69. * tps65090_try_enable_fet - Try to enable a FET
  70. *
  71. * @rdev: Regulator device
  72. *
  73. * Return: 0 if ok, -ENOTRECOVERABLE if the FET power good bit did not get
  74. * set, or some other -ve value if another error occurred (e.g. i2c error)
  75. */
  76. static int tps65090_try_enable_fet(struct regulator_dev *rdev)
  77. {
  78. unsigned int control;
  79. int ret, i;
  80. ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  81. rdev->desc->enable_mask,
  82. rdev->desc->enable_mask);
  83. if (ret < 0) {
  84. dev_err(&rdev->dev, "Error in updating reg %#x\n",
  85. rdev->desc->enable_reg);
  86. return ret;
  87. }
  88. for (i = 0; i < MAX_CTRL_READ_TRIES; i++) {
  89. ret = regmap_read(rdev->regmap, rdev->desc->enable_reg,
  90. &control);
  91. if (ret < 0)
  92. return ret;
  93. if (!(control & BIT(CTRL_TO_BIT)))
  94. break;
  95. usleep_range(1000, 1500);
  96. }
  97. if (!(control & BIT(CTRL_PG_BIT)))
  98. return -ENOTRECOVERABLE;
  99. return 0;
  100. }
  101. /**
  102. * tps65090_fet_enable - Enable a FET, trying a few times if it fails
  103. *
  104. * Some versions of the tps65090 have issues when turning on the FETs.
  105. * This function goes through several steps to ensure the best chance of the
  106. * FET going on. Specifically:
  107. * - We'll make sure that we bump the "overcurrent wait" to the maximum, which
  108. * increases the chances that we'll turn on properly.
  109. * - We'll retry turning the FET on multiple times (turning off in between).
  110. *
  111. * @rdev: Regulator device
  112. *
  113. * Return: 0 if ok, non-zero if it fails.
  114. */
  115. static int tps65090_fet_enable(struct regulator_dev *rdev)
  116. {
  117. int ret, tries;
  118. /*
  119. * Try enabling multiple times until we succeed since sometimes the
  120. * first try times out.
  121. */
  122. tries = 0;
  123. while (true) {
  124. ret = tps65090_try_enable_fet(rdev);
  125. if (!ret)
  126. break;
  127. if (ret != -ENOTRECOVERABLE || tries == MAX_FET_ENABLE_TRIES)
  128. goto err;
  129. /* Try turning the FET off (and then on again) */
  130. ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  131. rdev->desc->enable_mask, 0);
  132. if (ret)
  133. goto err;
  134. tries++;
  135. }
  136. if (tries)
  137. dev_warn(&rdev->dev, "reg %#x enable ok after %d tries\n",
  138. rdev->desc->enable_reg, tries);
  139. return 0;
  140. err:
  141. dev_warn(&rdev->dev, "reg %#x enable failed\n", rdev->desc->enable_reg);
  142. WARN_ON(1);
  143. return ret;
  144. }
  145. static const struct regulator_ops tps65090_reg_control_ops = {
  146. .enable = regulator_enable_regmap,
  147. .disable = regulator_disable_regmap,
  148. .is_enabled = regulator_is_enabled_regmap,
  149. };
  150. static const struct regulator_ops tps65090_fet_control_ops = {
  151. .enable = tps65090_fet_enable,
  152. .disable = regulator_disable_regmap,
  153. .is_enabled = regulator_is_enabled_regmap,
  154. };
  155. static const struct regulator_ops tps65090_ldo_ops = {
  156. };
  157. #define tps65090_REG_DESC(_id, _sname, _en_reg, _en_bits, _nvolt, _volt, _ops) \
  158. { \
  159. .name = "TPS65090_RAILS"#_id, \
  160. .supply_name = _sname, \
  161. .id = TPS65090_REGULATOR_##_id, \
  162. .n_voltages = _nvolt, \
  163. .ops = &_ops, \
  164. .fixed_uV = _volt, \
  165. .enable_reg = _en_reg, \
  166. .enable_val = _en_bits, \
  167. .enable_mask = _en_bits, \
  168. .type = REGULATOR_VOLTAGE, \
  169. .owner = THIS_MODULE, \
  170. }
  171. #define tps65090_REG_FIXEDV(_id, _sname, en_reg, _en_bits, _volt, _ops) \
  172. tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 1, _volt, _ops)
  173. #define tps65090_REG_SWITCH(_id, _sname, en_reg, _en_bits, _ops) \
  174. tps65090_REG_DESC(_id, _sname, en_reg, _en_bits, 0, 0, _ops)
  175. static struct regulator_desc tps65090_regulator_desc[] = {
  176. tps65090_REG_FIXEDV(DCDC1, "vsys1", 0x0C, BIT(CTRL_EN_BIT), 5000000,
  177. tps65090_reg_control_ops),
  178. tps65090_REG_FIXEDV(DCDC2, "vsys2", 0x0D, BIT(CTRL_EN_BIT), 3300000,
  179. tps65090_reg_control_ops),
  180. tps65090_REG_SWITCH(DCDC3, "vsys3", 0x0E, BIT(CTRL_EN_BIT),
  181. tps65090_reg_control_ops),
  182. tps65090_REG_SWITCH(FET1, "infet1", 0x0F,
  183. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  184. tps65090_fet_control_ops),
  185. tps65090_REG_SWITCH(FET2, "infet2", 0x10,
  186. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  187. tps65090_fet_control_ops),
  188. tps65090_REG_SWITCH(FET3, "infet3", 0x11,
  189. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  190. tps65090_fet_control_ops),
  191. tps65090_REG_SWITCH(FET4, "infet4", 0x12,
  192. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  193. tps65090_fet_control_ops),
  194. tps65090_REG_SWITCH(FET5, "infet5", 0x13,
  195. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  196. tps65090_fet_control_ops),
  197. tps65090_REG_SWITCH(FET6, "infet6", 0x14,
  198. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  199. tps65090_fet_control_ops),
  200. tps65090_REG_SWITCH(FET7, "infet7", 0x15,
  201. BIT(CTRL_EN_BIT) | BIT(CTRL_PG_BIT),
  202. tps65090_fet_control_ops),
  203. tps65090_REG_FIXEDV(LDO1, "vsys-l1", 0, 0, 5000000,
  204. tps65090_ldo_ops),
  205. tps65090_REG_FIXEDV(LDO2, "vsys-l2", 0, 0, 3300000,
  206. tps65090_ldo_ops),
  207. };
  208. static inline bool is_dcdc(int id)
  209. {
  210. switch (id) {
  211. case TPS65090_REGULATOR_DCDC1:
  212. case TPS65090_REGULATOR_DCDC2:
  213. case TPS65090_REGULATOR_DCDC3:
  214. return true;
  215. default:
  216. return false;
  217. }
  218. }
  219. static int tps65090_config_ext_control(
  220. struct tps65090_regulator *ri, bool enable)
  221. {
  222. int ret;
  223. struct device *parent = ri->dev->parent;
  224. unsigned int reg_en_reg = ri->desc->enable_reg;
  225. if (enable)
  226. ret = tps65090_set_bits(parent, reg_en_reg, 1);
  227. else
  228. ret = tps65090_clr_bits(parent, reg_en_reg, 1);
  229. if (ret < 0)
  230. dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
  231. return ret;
  232. }
  233. static int tps65090_regulator_disable_ext_control(
  234. struct tps65090_regulator *ri,
  235. struct tps65090_regulator_plat_data *tps_pdata)
  236. {
  237. int ret = 0;
  238. struct device *parent = ri->dev->parent;
  239. unsigned int reg_en_reg = ri->desc->enable_reg;
  240. /*
  241. * First enable output for internal control if require.
  242. * And then disable external control.
  243. */
  244. if (tps_pdata->reg_init_data->constraints.always_on ||
  245. tps_pdata->reg_init_data->constraints.boot_on) {
  246. ret = tps65090_set_bits(parent, reg_en_reg, 0);
  247. if (ret < 0) {
  248. dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
  249. return ret;
  250. }
  251. }
  252. return tps65090_config_ext_control(ri, false);
  253. }
  254. #ifdef CONFIG_OF
  255. static struct of_regulator_match tps65090_matches[] = {
  256. { .name = "dcdc1", },
  257. { .name = "dcdc2", },
  258. { .name = "dcdc3", },
  259. { .name = "fet1", },
  260. { .name = "fet2", },
  261. { .name = "fet3", },
  262. { .name = "fet4", },
  263. { .name = "fet5", },
  264. { .name = "fet6", },
  265. { .name = "fet7", },
  266. { .name = "ldo1", },
  267. { .name = "ldo2", },
  268. };
  269. static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
  270. struct platform_device *pdev,
  271. struct of_regulator_match **tps65090_reg_matches)
  272. {
  273. struct tps65090_platform_data *tps65090_pdata;
  274. struct device_node *np = pdev->dev.parent->of_node;
  275. struct device_node *regulators;
  276. int idx = 0, ret;
  277. struct tps65090_regulator_plat_data *reg_pdata;
  278. tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
  279. GFP_KERNEL);
  280. if (!tps65090_pdata)
  281. return ERR_PTR(-ENOMEM);
  282. reg_pdata = devm_kcalloc(&pdev->dev,
  283. TPS65090_REGULATOR_MAX, sizeof(*reg_pdata),
  284. GFP_KERNEL);
  285. if (!reg_pdata)
  286. return ERR_PTR(-ENOMEM);
  287. regulators = of_get_child_by_name(np, "regulators");
  288. if (!regulators) {
  289. dev_err(&pdev->dev, "regulator node not found\n");
  290. return ERR_PTR(-ENODEV);
  291. }
  292. ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
  293. ARRAY_SIZE(tps65090_matches));
  294. of_node_put(regulators);
  295. if (ret < 0) {
  296. dev_err(&pdev->dev,
  297. "Error parsing regulator init data: %d\n", ret);
  298. return ERR_PTR(ret);
  299. }
  300. *tps65090_reg_matches = tps65090_matches;
  301. for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
  302. struct regulator_init_data *ri_data;
  303. struct tps65090_regulator_plat_data *rpdata;
  304. struct device_node *np;
  305. rpdata = &reg_pdata[idx];
  306. ri_data = tps65090_matches[idx].init_data;
  307. if (!ri_data)
  308. continue;
  309. np = tps65090_matches[idx].of_node;
  310. if (!np)
  311. continue;
  312. rpdata->reg_init_data = ri_data;
  313. rpdata->enable_ext_control = of_property_read_bool(np,
  314. "ti,enable-ext-control");
  315. if (rpdata->enable_ext_control) {
  316. enum gpiod_flags gflags;
  317. if (ri_data->constraints.always_on ||
  318. ri_data->constraints.boot_on)
  319. gflags = GPIOD_OUT_HIGH;
  320. else
  321. gflags = GPIOD_OUT_LOW;
  322. gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  323. rpdata->gpiod = devm_fwnode_gpiod_get(
  324. &pdev->dev,
  325. of_fwnode_handle(np),
  326. "dcdc-ext-control",
  327. gflags,
  328. "tps65090");
  329. if (PTR_ERR(rpdata->gpiod) == -ENOENT) {
  330. dev_err(&pdev->dev,
  331. "could not find DCDC external control GPIO\n");
  332. rpdata->gpiod = NULL;
  333. } else if (IS_ERR(rpdata->gpiod))
  334. return ERR_CAST(rpdata->gpiod);
  335. }
  336. if (of_property_read_u32(np, "ti,overcurrent-wait",
  337. &rpdata->overcurrent_wait) == 0)
  338. rpdata->overcurrent_wait_valid = true;
  339. tps65090_pdata->reg_pdata[idx] = rpdata;
  340. }
  341. return tps65090_pdata;
  342. }
  343. #else
  344. static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
  345. struct platform_device *pdev,
  346. struct of_regulator_match **tps65090_reg_matches)
  347. {
  348. *tps65090_reg_matches = NULL;
  349. return NULL;
  350. }
  351. #endif
  352. static int tps65090_regulator_probe(struct platform_device *pdev)
  353. {
  354. struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
  355. struct tps65090_regulator *ri = NULL;
  356. struct regulator_config config = { };
  357. struct regulator_dev *rdev;
  358. struct tps65090_regulator_plat_data *tps_pdata;
  359. struct tps65090_regulator *pmic;
  360. struct tps65090_platform_data *tps65090_pdata;
  361. struct of_regulator_match *tps65090_reg_matches = NULL;
  362. int num;
  363. int ret;
  364. dev_dbg(&pdev->dev, "Probing regulator\n");
  365. tps65090_pdata = dev_get_platdata(pdev->dev.parent);
  366. if (!tps65090_pdata && tps65090_mfd->dev->of_node)
  367. tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
  368. &tps65090_reg_matches);
  369. if (IS_ERR_OR_NULL(tps65090_pdata)) {
  370. dev_err(&pdev->dev, "Platform data missing\n");
  371. return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
  372. }
  373. pmic = devm_kcalloc(&pdev->dev,
  374. TPS65090_REGULATOR_MAX, sizeof(*pmic),
  375. GFP_KERNEL);
  376. if (!pmic)
  377. return -ENOMEM;
  378. for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
  379. tps_pdata = tps65090_pdata->reg_pdata[num];
  380. ri = &pmic[num];
  381. ri->dev = &pdev->dev;
  382. ri->desc = &tps65090_regulator_desc[num];
  383. if (tps_pdata) {
  384. ri->overcurrent_wait_valid =
  385. tps_pdata->overcurrent_wait_valid;
  386. ri->overcurrent_wait = tps_pdata->overcurrent_wait;
  387. }
  388. /*
  389. * TPS5090 DCDC support the control from external digital input.
  390. * Configure it as per platform data.
  391. */
  392. if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
  393. if (tps_pdata->enable_ext_control) {
  394. config.ena_gpiod = tps_pdata->gpiod;
  395. ri->desc->ops = &tps65090_ext_control_ops;
  396. } else {
  397. ret = tps65090_regulator_disable_ext_control(
  398. ri, tps_pdata);
  399. if (ret < 0) {
  400. dev_err(&pdev->dev,
  401. "failed disable ext control\n");
  402. return ret;
  403. }
  404. }
  405. }
  406. config.dev = pdev->dev.parent;
  407. config.driver_data = ri;
  408. config.regmap = tps65090_mfd->rmap;
  409. if (tps_pdata)
  410. config.init_data = tps_pdata->reg_init_data;
  411. else
  412. config.init_data = NULL;
  413. if (tps65090_reg_matches)
  414. config.of_node = tps65090_reg_matches[num].of_node;
  415. else
  416. config.of_node = NULL;
  417. /*
  418. * Hand the GPIO descriptor management over to the regulator
  419. * core, remove it from devres management.
  420. */
  421. if (config.ena_gpiod)
  422. devm_gpiod_unhinge(&pdev->dev, config.ena_gpiod);
  423. rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
  424. if (IS_ERR(rdev)) {
  425. dev_err(&pdev->dev, "failed to register regulator %s\n",
  426. ri->desc->name);
  427. return PTR_ERR(rdev);
  428. }
  429. ri->rdev = rdev;
  430. if (ri->overcurrent_wait_valid) {
  431. ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
  432. if (ret < 0)
  433. return ret;
  434. }
  435. /* Enable external control if it is require */
  436. if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
  437. tps_pdata->enable_ext_control) {
  438. ret = tps65090_config_ext_control(ri, true);
  439. if (ret < 0)
  440. return ret;
  441. }
  442. }
  443. platform_set_drvdata(pdev, pmic);
  444. return 0;
  445. }
  446. static struct platform_driver tps65090_regulator_driver = {
  447. .driver = {
  448. .name = "tps65090-pmic",
  449. },
  450. .probe = tps65090_regulator_probe,
  451. };
  452. static int __init tps65090_regulator_init(void)
  453. {
  454. return platform_driver_register(&tps65090_regulator_driver);
  455. }
  456. subsys_initcall(tps65090_regulator_init);
  457. static void __exit tps65090_regulator_exit(void)
  458. {
  459. platform_driver_unregister(&tps65090_regulator_driver);
  460. }
  461. module_exit(tps65090_regulator_exit);
  462. MODULE_DESCRIPTION("tps65090 regulator driver");
  463. MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
  464. MODULE_LICENSE("GPL v2");
  465. MODULE_ALIAS("platform:tps65090-pmic");