vctrl-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for voltage controller regulators
  4. *
  5. * Copyright (C) 2017 Google, Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_device.h>
  13. #include <linux/regulator/coupler.h>
  14. #include <linux/regulator/driver.h>
  15. #include <linux/regulator/of_regulator.h>
  16. #include <linux/sort.h>
  17. #include "internal.h"
  18. struct vctrl_voltage_range {
  19. int min_uV;
  20. int max_uV;
  21. };
  22. struct vctrl_voltage_ranges {
  23. struct vctrl_voltage_range ctrl;
  24. struct vctrl_voltage_range out;
  25. };
  26. struct vctrl_voltage_table {
  27. int ctrl;
  28. int out;
  29. int ovp_min_sel;
  30. };
  31. struct vctrl_data {
  32. struct regulator_dev *rdev;
  33. struct regulator_desc desc;
  34. bool enabled;
  35. unsigned int min_slew_down_rate;
  36. unsigned int ovp_threshold;
  37. struct vctrl_voltage_ranges vrange;
  38. struct vctrl_voltage_table *vtable;
  39. unsigned int sel;
  40. };
  41. static int vctrl_calc_ctrl_voltage(struct vctrl_data *vctrl, int out_uV)
  42. {
  43. struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
  44. struct vctrl_voltage_range *out = &vctrl->vrange.out;
  45. return ctrl->min_uV +
  46. DIV_ROUND_CLOSEST_ULL((s64)(out_uV - out->min_uV) *
  47. (ctrl->max_uV - ctrl->min_uV),
  48. out->max_uV - out->min_uV);
  49. }
  50. static int vctrl_calc_output_voltage(struct vctrl_data *vctrl, int ctrl_uV)
  51. {
  52. struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
  53. struct vctrl_voltage_range *out = &vctrl->vrange.out;
  54. if (ctrl_uV < 0) {
  55. pr_err("vctrl: failed to get control voltage\n");
  56. return ctrl_uV;
  57. }
  58. if (ctrl_uV < ctrl->min_uV)
  59. return out->min_uV;
  60. if (ctrl_uV > ctrl->max_uV)
  61. return out->max_uV;
  62. return out->min_uV +
  63. DIV_ROUND_CLOSEST_ULL((s64)(ctrl_uV - ctrl->min_uV) *
  64. (out->max_uV - out->min_uV),
  65. ctrl->max_uV - ctrl->min_uV);
  66. }
  67. static int vctrl_get_voltage(struct regulator_dev *rdev)
  68. {
  69. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  70. int ctrl_uV;
  71. if (!rdev->supply)
  72. return -EPROBE_DEFER;
  73. ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
  74. return vctrl_calc_output_voltage(vctrl, ctrl_uV);
  75. }
  76. static int vctrl_set_voltage(struct regulator_dev *rdev,
  77. int req_min_uV, int req_max_uV,
  78. unsigned int *selector)
  79. {
  80. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  81. int orig_ctrl_uV;
  82. int uV;
  83. int ret;
  84. if (!rdev->supply)
  85. return -EPROBE_DEFER;
  86. orig_ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
  87. uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV);
  88. if (req_min_uV >= uV || !vctrl->ovp_threshold)
  89. /* voltage rising or no OVP */
  90. return regulator_set_voltage_rdev(rdev->supply->rdev,
  91. vctrl_calc_ctrl_voltage(vctrl, req_min_uV),
  92. vctrl_calc_ctrl_voltage(vctrl, req_max_uV),
  93. PM_SUSPEND_ON);
  94. while (uV > req_min_uV) {
  95. int max_drop_uV = (uV * vctrl->ovp_threshold) / 100;
  96. int next_uV;
  97. int next_ctrl_uV;
  98. int delay;
  99. /* Make sure no infinite loop even in crazy cases */
  100. if (max_drop_uV == 0)
  101. max_drop_uV = 1;
  102. next_uV = max_t(int, req_min_uV, uV - max_drop_uV);
  103. next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV);
  104. ret = regulator_set_voltage_rdev(rdev->supply->rdev,
  105. next_ctrl_uV,
  106. next_ctrl_uV,
  107. PM_SUSPEND_ON);
  108. if (ret)
  109. goto err;
  110. delay = DIV_ROUND_UP(uV - next_uV, vctrl->min_slew_down_rate);
  111. usleep_range(delay, delay + DIV_ROUND_UP(delay, 10));
  112. uV = next_uV;
  113. }
  114. return 0;
  115. err:
  116. /* Try to go back to original voltage */
  117. regulator_set_voltage_rdev(rdev->supply->rdev, orig_ctrl_uV, orig_ctrl_uV,
  118. PM_SUSPEND_ON);
  119. return ret;
  120. }
  121. static int vctrl_get_voltage_sel(struct regulator_dev *rdev)
  122. {
  123. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  124. return vctrl->sel;
  125. }
  126. static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
  127. unsigned int selector)
  128. {
  129. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  130. unsigned int orig_sel = vctrl->sel;
  131. int ret;
  132. if (!rdev->supply)
  133. return -EPROBE_DEFER;
  134. if (selector >= rdev->desc->n_voltages)
  135. return -EINVAL;
  136. if (selector >= vctrl->sel || !vctrl->ovp_threshold) {
  137. /* voltage rising or no OVP */
  138. ret = regulator_set_voltage_rdev(rdev->supply->rdev,
  139. vctrl->vtable[selector].ctrl,
  140. vctrl->vtable[selector].ctrl,
  141. PM_SUSPEND_ON);
  142. if (!ret)
  143. vctrl->sel = selector;
  144. return ret;
  145. }
  146. while (vctrl->sel != selector) {
  147. unsigned int next_sel;
  148. int delay;
  149. if (selector >= vctrl->vtable[vctrl->sel].ovp_min_sel)
  150. next_sel = selector;
  151. else
  152. next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel;
  153. ret = regulator_set_voltage_rdev(rdev->supply->rdev,
  154. vctrl->vtable[next_sel].ctrl,
  155. vctrl->vtable[next_sel].ctrl,
  156. PM_SUSPEND_ON);
  157. if (ret) {
  158. dev_err(&rdev->dev,
  159. "failed to set control voltage to %duV\n",
  160. vctrl->vtable[next_sel].ctrl);
  161. goto err;
  162. }
  163. vctrl->sel = next_sel;
  164. delay = DIV_ROUND_UP(vctrl->vtable[vctrl->sel].out -
  165. vctrl->vtable[next_sel].out,
  166. vctrl->min_slew_down_rate);
  167. usleep_range(delay, delay + DIV_ROUND_UP(delay, 10));
  168. }
  169. return 0;
  170. err:
  171. if (vctrl->sel != orig_sel) {
  172. /* Try to go back to original voltage */
  173. if (!regulator_set_voltage_rdev(rdev->supply->rdev,
  174. vctrl->vtable[orig_sel].ctrl,
  175. vctrl->vtable[orig_sel].ctrl,
  176. PM_SUSPEND_ON))
  177. vctrl->sel = orig_sel;
  178. else
  179. dev_warn(&rdev->dev,
  180. "failed to restore original voltage\n");
  181. }
  182. return ret;
  183. }
  184. static int vctrl_list_voltage(struct regulator_dev *rdev,
  185. unsigned int selector)
  186. {
  187. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  188. if (selector >= rdev->desc->n_voltages)
  189. return -EINVAL;
  190. return vctrl->vtable[selector].out;
  191. }
  192. static int vctrl_parse_dt(struct platform_device *pdev,
  193. struct vctrl_data *vctrl)
  194. {
  195. int ret;
  196. struct device_node *np = pdev->dev.of_node;
  197. u32 pval;
  198. u32 vrange_ctrl[2];
  199. ret = of_property_read_u32(np, "ovp-threshold-percent", &pval);
  200. if (!ret) {
  201. vctrl->ovp_threshold = pval;
  202. if (vctrl->ovp_threshold > 100) {
  203. dev_err(&pdev->dev,
  204. "ovp-threshold-percent (%u) > 100\n",
  205. vctrl->ovp_threshold);
  206. return -EINVAL;
  207. }
  208. }
  209. ret = of_property_read_u32(np, "min-slew-down-rate", &pval);
  210. if (!ret) {
  211. vctrl->min_slew_down_rate = pval;
  212. /* We use the value as int and as divider; sanity check */
  213. if (vctrl->min_slew_down_rate == 0) {
  214. dev_err(&pdev->dev,
  215. "min-slew-down-rate must not be 0\n");
  216. return -EINVAL;
  217. } else if (vctrl->min_slew_down_rate > INT_MAX) {
  218. dev_err(&pdev->dev, "min-slew-down-rate (%u) too big\n",
  219. vctrl->min_slew_down_rate);
  220. return -EINVAL;
  221. }
  222. }
  223. if (vctrl->ovp_threshold && !vctrl->min_slew_down_rate) {
  224. dev_err(&pdev->dev,
  225. "ovp-threshold-percent requires min-slew-down-rate\n");
  226. return -EINVAL;
  227. }
  228. ret = of_property_read_u32(np, "regulator-min-microvolt", &pval);
  229. if (ret) {
  230. dev_err(&pdev->dev,
  231. "failed to read regulator-min-microvolt: %d\n", ret);
  232. return ret;
  233. }
  234. vctrl->vrange.out.min_uV = pval;
  235. ret = of_property_read_u32(np, "regulator-max-microvolt", &pval);
  236. if (ret) {
  237. dev_err(&pdev->dev,
  238. "failed to read regulator-max-microvolt: %d\n", ret);
  239. return ret;
  240. }
  241. vctrl->vrange.out.max_uV = pval;
  242. ret = of_property_read_u32_array(np, "ctrl-voltage-range", vrange_ctrl,
  243. 2);
  244. if (ret) {
  245. dev_err(&pdev->dev, "failed to read ctrl-voltage-range: %d\n",
  246. ret);
  247. return ret;
  248. }
  249. if (vrange_ctrl[0] >= vrange_ctrl[1]) {
  250. dev_err(&pdev->dev, "ctrl-voltage-range is invalid: %d-%d\n",
  251. vrange_ctrl[0], vrange_ctrl[1]);
  252. return -EINVAL;
  253. }
  254. vctrl->vrange.ctrl.min_uV = vrange_ctrl[0];
  255. vctrl->vrange.ctrl.max_uV = vrange_ctrl[1];
  256. return 0;
  257. }
  258. static int vctrl_cmp_ctrl_uV(const void *a, const void *b)
  259. {
  260. const struct vctrl_voltage_table *at = a;
  261. const struct vctrl_voltage_table *bt = b;
  262. return at->ctrl - bt->ctrl;
  263. }
  264. static int vctrl_init_vtable(struct platform_device *pdev,
  265. struct regulator *ctrl_reg)
  266. {
  267. struct vctrl_data *vctrl = platform_get_drvdata(pdev);
  268. struct regulator_desc *rdesc = &vctrl->desc;
  269. struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl;
  270. int n_voltages;
  271. int ctrl_uV;
  272. int i, idx_vt;
  273. n_voltages = regulator_count_voltages(ctrl_reg);
  274. rdesc->n_voltages = n_voltages;
  275. /* determine number of steps within the range of the vctrl regulator */
  276. for (i = 0; i < n_voltages; i++) {
  277. ctrl_uV = regulator_list_voltage(ctrl_reg, i);
  278. if (ctrl_uV < vrange_ctrl->min_uV ||
  279. ctrl_uV > vrange_ctrl->max_uV)
  280. rdesc->n_voltages--;
  281. }
  282. if (rdesc->n_voltages == 0) {
  283. dev_err(&pdev->dev, "invalid configuration\n");
  284. return -EINVAL;
  285. }
  286. vctrl->vtable = devm_kcalloc(&pdev->dev, rdesc->n_voltages,
  287. sizeof(struct vctrl_voltage_table),
  288. GFP_KERNEL);
  289. if (!vctrl->vtable)
  290. return -ENOMEM;
  291. /* create mapping control <=> output voltage */
  292. for (i = 0, idx_vt = 0; i < n_voltages; i++) {
  293. ctrl_uV = regulator_list_voltage(ctrl_reg, i);
  294. if (ctrl_uV < vrange_ctrl->min_uV ||
  295. ctrl_uV > vrange_ctrl->max_uV)
  296. continue;
  297. vctrl->vtable[idx_vt].ctrl = ctrl_uV;
  298. vctrl->vtable[idx_vt].out =
  299. vctrl_calc_output_voltage(vctrl, ctrl_uV);
  300. idx_vt++;
  301. }
  302. /* we rely on the table to be ordered by ascending voltage */
  303. sort(vctrl->vtable, rdesc->n_voltages,
  304. sizeof(struct vctrl_voltage_table), vctrl_cmp_ctrl_uV,
  305. NULL);
  306. /* pre-calculate OVP-safe downward transitions */
  307. for (i = rdesc->n_voltages - 1; i > 0; i--) {
  308. int j;
  309. int ovp_min_uV = (vctrl->vtable[i].out *
  310. (100 - vctrl->ovp_threshold)) / 100;
  311. for (j = 0; j < i; j++) {
  312. if (vctrl->vtable[j].out >= ovp_min_uV) {
  313. vctrl->vtable[i].ovp_min_sel = j;
  314. break;
  315. }
  316. }
  317. if (j == i) {
  318. dev_warn(&pdev->dev, "switching down from %duV may cause OVP shutdown\n",
  319. vctrl->vtable[i].out);
  320. /* use next lowest voltage */
  321. vctrl->vtable[i].ovp_min_sel = i - 1;
  322. }
  323. }
  324. return 0;
  325. }
  326. static int vctrl_enable(struct regulator_dev *rdev)
  327. {
  328. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  329. vctrl->enabled = true;
  330. return 0;
  331. }
  332. static int vctrl_disable(struct regulator_dev *rdev)
  333. {
  334. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  335. vctrl->enabled = false;
  336. return 0;
  337. }
  338. static int vctrl_is_enabled(struct regulator_dev *rdev)
  339. {
  340. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  341. return vctrl->enabled;
  342. }
  343. static const struct regulator_ops vctrl_ops_cont = {
  344. .enable = vctrl_enable,
  345. .disable = vctrl_disable,
  346. .is_enabled = vctrl_is_enabled,
  347. .get_voltage = vctrl_get_voltage,
  348. .set_voltage = vctrl_set_voltage,
  349. };
  350. static const struct regulator_ops vctrl_ops_non_cont = {
  351. .enable = vctrl_enable,
  352. .disable = vctrl_disable,
  353. .is_enabled = vctrl_is_enabled,
  354. .set_voltage_sel = vctrl_set_voltage_sel,
  355. .get_voltage_sel = vctrl_get_voltage_sel,
  356. .list_voltage = vctrl_list_voltage,
  357. .map_voltage = regulator_map_voltage_iterate,
  358. };
  359. static int vctrl_probe(struct platform_device *pdev)
  360. {
  361. struct device_node *np = pdev->dev.of_node;
  362. struct vctrl_data *vctrl;
  363. const struct regulator_init_data *init_data;
  364. struct regulator_desc *rdesc;
  365. struct regulator_config cfg = { };
  366. struct vctrl_voltage_range *vrange_ctrl;
  367. struct regulator *ctrl_reg;
  368. int ctrl_uV;
  369. int ret;
  370. vctrl = devm_kzalloc(&pdev->dev, sizeof(struct vctrl_data),
  371. GFP_KERNEL);
  372. if (!vctrl)
  373. return -ENOMEM;
  374. platform_set_drvdata(pdev, vctrl);
  375. ret = vctrl_parse_dt(pdev, vctrl);
  376. if (ret)
  377. return ret;
  378. ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl");
  379. if (IS_ERR(ctrl_reg))
  380. return PTR_ERR(ctrl_reg);
  381. vrange_ctrl = &vctrl->vrange.ctrl;
  382. rdesc = &vctrl->desc;
  383. rdesc->name = "vctrl";
  384. rdesc->type = REGULATOR_VOLTAGE;
  385. rdesc->owner = THIS_MODULE;
  386. rdesc->supply_name = "ctrl";
  387. if ((regulator_get_linear_step(ctrl_reg) == 1) ||
  388. (regulator_count_voltages(ctrl_reg) == -EINVAL)) {
  389. rdesc->continuous_voltage_range = true;
  390. rdesc->ops = &vctrl_ops_cont;
  391. } else {
  392. rdesc->ops = &vctrl_ops_non_cont;
  393. }
  394. init_data = of_get_regulator_init_data(&pdev->dev, np, rdesc);
  395. if (!init_data)
  396. return -ENOMEM;
  397. cfg.of_node = np;
  398. cfg.dev = &pdev->dev;
  399. cfg.driver_data = vctrl;
  400. cfg.init_data = init_data;
  401. if (!rdesc->continuous_voltage_range) {
  402. ret = vctrl_init_vtable(pdev, ctrl_reg);
  403. if (ret)
  404. return ret;
  405. /* Use locked consumer API when not in regulator framework */
  406. ctrl_uV = regulator_get_voltage(ctrl_reg);
  407. if (ctrl_uV < 0) {
  408. dev_err(&pdev->dev, "failed to get control voltage\n");
  409. return ctrl_uV;
  410. }
  411. /* determine current voltage selector from control voltage */
  412. if (ctrl_uV < vrange_ctrl->min_uV) {
  413. vctrl->sel = 0;
  414. } else if (ctrl_uV > vrange_ctrl->max_uV) {
  415. vctrl->sel = rdesc->n_voltages - 1;
  416. } else {
  417. int i;
  418. for (i = 0; i < rdesc->n_voltages; i++) {
  419. if (ctrl_uV == vctrl->vtable[i].ctrl) {
  420. vctrl->sel = i;
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. /* Drop ctrl-supply here in favor of regulator core managed supply */
  427. devm_regulator_put(ctrl_reg);
  428. vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg);
  429. if (IS_ERR(vctrl->rdev)) {
  430. ret = PTR_ERR(vctrl->rdev);
  431. dev_err(&pdev->dev, "failed to register regulator: %d\n", ret);
  432. return ret;
  433. }
  434. return 0;
  435. }
  436. static const struct of_device_id vctrl_of_match[] = {
  437. { .compatible = "vctrl-regulator", },
  438. {},
  439. };
  440. MODULE_DEVICE_TABLE(of, vctrl_of_match);
  441. static struct platform_driver vctrl_driver = {
  442. .probe = vctrl_probe,
  443. .driver = {
  444. .name = "vctrl-regulator",
  445. .of_match_table = of_match_ptr(vctrl_of_match),
  446. },
  447. };
  448. module_platform_driver(vctrl_driver);
  449. MODULE_DESCRIPTION("Voltage Controlled Regulator Driver");
  450. MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
  451. MODULE_LICENSE("GPL v2");