tps65090-regulator.c 15 KB

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