vctrl-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Driver for voltage controller regulators
  3. *
  4. * Copyright (C) 2017 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_device.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/of_regulator.h>
  23. #include <linux/sort.h>
  24. struct vctrl_voltage_range {
  25. int min_uV;
  26. int max_uV;
  27. };
  28. struct vctrl_voltage_ranges {
  29. struct vctrl_voltage_range ctrl;
  30. struct vctrl_voltage_range out;
  31. };
  32. struct vctrl_voltage_table {
  33. int ctrl;
  34. int out;
  35. int ovp_min_sel;
  36. };
  37. struct vctrl_data {
  38. struct regulator_dev *rdev;
  39. struct regulator_desc desc;
  40. struct regulator *ctrl_reg;
  41. bool enabled;
  42. unsigned int min_slew_down_rate;
  43. unsigned int ovp_threshold;
  44. struct vctrl_voltage_ranges vrange;
  45. struct vctrl_voltage_table *vtable;
  46. unsigned int sel;
  47. };
  48. static int vctrl_calc_ctrl_voltage(struct vctrl_data *vctrl, int out_uV)
  49. {
  50. struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
  51. struct vctrl_voltage_range *out = &vctrl->vrange.out;
  52. return ctrl->min_uV +
  53. DIV_ROUND_CLOSEST_ULL((s64)(out_uV - out->min_uV) *
  54. (ctrl->max_uV - ctrl->min_uV),
  55. out->max_uV - out->min_uV);
  56. }
  57. static int vctrl_calc_output_voltage(struct vctrl_data *vctrl, int ctrl_uV)
  58. {
  59. struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
  60. struct vctrl_voltage_range *out = &vctrl->vrange.out;
  61. if (ctrl_uV < 0) {
  62. pr_err("vctrl: failed to get control voltage\n");
  63. return ctrl_uV;
  64. }
  65. if (ctrl_uV < ctrl->min_uV)
  66. return out->min_uV;
  67. if (ctrl_uV > ctrl->max_uV)
  68. return out->max_uV;
  69. return out->min_uV +
  70. DIV_ROUND_CLOSEST_ULL((s64)(ctrl_uV - ctrl->min_uV) *
  71. (out->max_uV - out->min_uV),
  72. ctrl->max_uV - ctrl->min_uV);
  73. }
  74. static int vctrl_get_voltage(struct regulator_dev *rdev)
  75. {
  76. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  77. int ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg);
  78. return vctrl_calc_output_voltage(vctrl, ctrl_uV);
  79. }
  80. static int vctrl_set_voltage(struct regulator_dev *rdev,
  81. int req_min_uV, int req_max_uV,
  82. unsigned int *selector)
  83. {
  84. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  85. struct regulator *ctrl_reg = vctrl->ctrl_reg;
  86. int orig_ctrl_uV = regulator_get_voltage(ctrl_reg);
  87. int uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV);
  88. int ret;
  89. if (req_min_uV >= uV || !vctrl->ovp_threshold)
  90. /* voltage rising or no OVP */
  91. return regulator_set_voltage(
  92. ctrl_reg,
  93. vctrl_calc_ctrl_voltage(vctrl, req_min_uV),
  94. vctrl_calc_ctrl_voltage(vctrl, req_max_uV));
  95. while (uV > req_min_uV) {
  96. int max_drop_uV = (uV * vctrl->ovp_threshold) / 100;
  97. int next_uV;
  98. int next_ctrl_uV;
  99. int delay;
  100. /* Make sure no infinite loop even in crazy cases */
  101. if (max_drop_uV == 0)
  102. max_drop_uV = 1;
  103. next_uV = max_t(int, req_min_uV, uV - max_drop_uV);
  104. next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV);
  105. ret = regulator_set_voltage(ctrl_reg,
  106. next_ctrl_uV,
  107. next_ctrl_uV);
  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(ctrl_reg, orig_ctrl_uV, orig_ctrl_uV);
  118. return ret;
  119. }
  120. static int vctrl_get_voltage_sel(struct regulator_dev *rdev)
  121. {
  122. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  123. return vctrl->sel;
  124. }
  125. static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
  126. unsigned int selector)
  127. {
  128. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  129. struct regulator *ctrl_reg = vctrl->ctrl_reg;
  130. unsigned int orig_sel = vctrl->sel;
  131. int ret;
  132. if (selector >= rdev->desc->n_voltages)
  133. return -EINVAL;
  134. if (selector >= vctrl->sel || !vctrl->ovp_threshold) {
  135. /* voltage rising or no OVP */
  136. ret = regulator_set_voltage(ctrl_reg,
  137. vctrl->vtable[selector].ctrl,
  138. vctrl->vtable[selector].ctrl);
  139. if (!ret)
  140. vctrl->sel = selector;
  141. return ret;
  142. }
  143. while (vctrl->sel != selector) {
  144. unsigned int next_sel;
  145. int delay;
  146. if (selector >= vctrl->vtable[vctrl->sel].ovp_min_sel)
  147. next_sel = selector;
  148. else
  149. next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel;
  150. ret = regulator_set_voltage(ctrl_reg,
  151. vctrl->vtable[next_sel].ctrl,
  152. vctrl->vtable[next_sel].ctrl);
  153. if (ret) {
  154. dev_err(&rdev->dev,
  155. "failed to set control voltage to %duV\n",
  156. vctrl->vtable[next_sel].ctrl);
  157. goto err;
  158. }
  159. vctrl->sel = next_sel;
  160. delay = DIV_ROUND_UP(vctrl->vtable[vctrl->sel].out -
  161. vctrl->vtable[next_sel].out,
  162. vctrl->min_slew_down_rate);
  163. usleep_range(delay, delay + DIV_ROUND_UP(delay, 10));
  164. }
  165. return 0;
  166. err:
  167. if (vctrl->sel != orig_sel) {
  168. /* Try to go back to original voltage */
  169. if (!regulator_set_voltage(ctrl_reg,
  170. vctrl->vtable[orig_sel].ctrl,
  171. vctrl->vtable[orig_sel].ctrl))
  172. vctrl->sel = orig_sel;
  173. else
  174. dev_warn(&rdev->dev,
  175. "failed to restore original voltage\n");
  176. }
  177. return ret;
  178. }
  179. static int vctrl_list_voltage(struct regulator_dev *rdev,
  180. unsigned int selector)
  181. {
  182. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  183. if (selector >= rdev->desc->n_voltages)
  184. return -EINVAL;
  185. return vctrl->vtable[selector].out;
  186. }
  187. static int vctrl_parse_dt(struct platform_device *pdev,
  188. struct vctrl_data *vctrl)
  189. {
  190. int ret;
  191. struct device_node *np = pdev->dev.of_node;
  192. u32 pval;
  193. u32 vrange_ctrl[2];
  194. vctrl->ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl");
  195. if (IS_ERR(vctrl->ctrl_reg))
  196. return PTR_ERR(vctrl->ctrl_reg);
  197. ret = of_property_read_u32(np, "ovp-threshold-percent", &pval);
  198. if (!ret) {
  199. vctrl->ovp_threshold = pval;
  200. if (vctrl->ovp_threshold > 100) {
  201. dev_err(&pdev->dev,
  202. "ovp-threshold-percent (%u) > 100\n",
  203. vctrl->ovp_threshold);
  204. return -EINVAL;
  205. }
  206. }
  207. ret = of_property_read_u32(np, "min-slew-down-rate", &pval);
  208. if (!ret) {
  209. vctrl->min_slew_down_rate = pval;
  210. /* We use the value as int and as divider; sanity check */
  211. if (vctrl->min_slew_down_rate == 0) {
  212. dev_err(&pdev->dev,
  213. "min-slew-down-rate must not be 0\n");
  214. return -EINVAL;
  215. } else if (vctrl->min_slew_down_rate > INT_MAX) {
  216. dev_err(&pdev->dev, "min-slew-down-rate (%u) too big\n",
  217. vctrl->min_slew_down_rate);
  218. return -EINVAL;
  219. }
  220. }
  221. if (vctrl->ovp_threshold && !vctrl->min_slew_down_rate) {
  222. dev_err(&pdev->dev,
  223. "ovp-threshold-percent requires min-slew-down-rate\n");
  224. return -EINVAL;
  225. }
  226. ret = of_property_read_u32(np, "regulator-min-microvolt", &pval);
  227. if (ret) {
  228. dev_err(&pdev->dev,
  229. "failed to read regulator-min-microvolt: %d\n", ret);
  230. return ret;
  231. }
  232. vctrl->vrange.out.min_uV = pval;
  233. ret = of_property_read_u32(np, "regulator-max-microvolt", &pval);
  234. if (ret) {
  235. dev_err(&pdev->dev,
  236. "failed to read regulator-max-microvolt: %d\n", ret);
  237. return ret;
  238. }
  239. vctrl->vrange.out.max_uV = pval;
  240. ret = of_property_read_u32_array(np, "ctrl-voltage-range", vrange_ctrl,
  241. 2);
  242. if (ret) {
  243. dev_err(&pdev->dev, "failed to read ctrl-voltage-range: %d\n",
  244. ret);
  245. return ret;
  246. }
  247. if (vrange_ctrl[0] >= vrange_ctrl[1]) {
  248. dev_err(&pdev->dev, "ctrl-voltage-range is invalid: %d-%d\n",
  249. vrange_ctrl[0], vrange_ctrl[1]);
  250. return -EINVAL;
  251. }
  252. vctrl->vrange.ctrl.min_uV = vrange_ctrl[0];
  253. vctrl->vrange.ctrl.max_uV = vrange_ctrl[1];
  254. return 0;
  255. }
  256. static int vctrl_cmp_ctrl_uV(const void *a, const void *b)
  257. {
  258. const struct vctrl_voltage_table *at = a;
  259. const struct vctrl_voltage_table *bt = b;
  260. return at->ctrl - bt->ctrl;
  261. }
  262. static int vctrl_init_vtable(struct platform_device *pdev)
  263. {
  264. struct vctrl_data *vctrl = platform_get_drvdata(pdev);
  265. struct regulator_desc *rdesc = &vctrl->desc;
  266. struct regulator *ctrl_reg = vctrl->ctrl_reg;
  267. struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl;
  268. int n_voltages;
  269. int ctrl_uV;
  270. int i, idx_vt;
  271. n_voltages = regulator_count_voltages(ctrl_reg);
  272. rdesc->n_voltages = n_voltages;
  273. /* determine number of steps within the range of the vctrl regulator */
  274. for (i = 0; i < n_voltages; i++) {
  275. ctrl_uV = regulator_list_voltage(ctrl_reg, i);
  276. if (ctrl_uV < vrange_ctrl->min_uV ||
  277. ctrl_uV > vrange_ctrl->max_uV) {
  278. rdesc->n_voltages--;
  279. continue;
  280. }
  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. int ret = regulator_enable(vctrl->ctrl_reg);
  330. if (!ret)
  331. vctrl->enabled = true;
  332. return ret;
  333. }
  334. static int vctrl_disable(struct regulator_dev *rdev)
  335. {
  336. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  337. int ret = regulator_disable(vctrl->ctrl_reg);
  338. if (!ret)
  339. vctrl->enabled = false;
  340. return ret;
  341. }
  342. static int vctrl_is_enabled(struct regulator_dev *rdev)
  343. {
  344. struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
  345. return vctrl->enabled;
  346. }
  347. static const struct regulator_ops vctrl_ops_cont = {
  348. .enable = vctrl_enable,
  349. .disable = vctrl_disable,
  350. .is_enabled = vctrl_is_enabled,
  351. .get_voltage = vctrl_get_voltage,
  352. .set_voltage = vctrl_set_voltage,
  353. };
  354. static const struct regulator_ops vctrl_ops_non_cont = {
  355. .enable = vctrl_enable,
  356. .disable = vctrl_disable,
  357. .is_enabled = vctrl_is_enabled,
  358. .set_voltage_sel = vctrl_set_voltage_sel,
  359. .get_voltage_sel = vctrl_get_voltage_sel,
  360. .list_voltage = vctrl_list_voltage,
  361. .map_voltage = regulator_map_voltage_iterate,
  362. };
  363. static int vctrl_probe(struct platform_device *pdev)
  364. {
  365. struct device_node *np = pdev->dev.of_node;
  366. struct vctrl_data *vctrl;
  367. const struct regulator_init_data *init_data;
  368. struct regulator_desc *rdesc;
  369. struct regulator_config cfg = { };
  370. struct vctrl_voltage_range *vrange_ctrl;
  371. int ctrl_uV;
  372. int ret;
  373. vctrl = devm_kzalloc(&pdev->dev, sizeof(struct vctrl_data),
  374. GFP_KERNEL);
  375. if (!vctrl)
  376. return -ENOMEM;
  377. platform_set_drvdata(pdev, vctrl);
  378. ret = vctrl_parse_dt(pdev, vctrl);
  379. if (ret)
  380. return ret;
  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. if ((regulator_get_linear_step(vctrl->ctrl_reg) == 1) ||
  387. (regulator_count_voltages(vctrl->ctrl_reg) == -EINVAL)) {
  388. rdesc->continuous_voltage_range = true;
  389. rdesc->ops = &vctrl_ops_cont;
  390. } else {
  391. rdesc->ops = &vctrl_ops_non_cont;
  392. }
  393. init_data = of_get_regulator_init_data(&pdev->dev, np, rdesc);
  394. if (!init_data)
  395. return -ENOMEM;
  396. cfg.of_node = np;
  397. cfg.dev = &pdev->dev;
  398. cfg.driver_data = vctrl;
  399. cfg.init_data = init_data;
  400. if (!rdesc->continuous_voltage_range) {
  401. ret = vctrl_init_vtable(pdev);
  402. if (ret)
  403. return ret;
  404. ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg);
  405. if (ctrl_uV < 0) {
  406. dev_err(&pdev->dev, "failed to get control voltage\n");
  407. return ctrl_uV;
  408. }
  409. /* determine current voltage selector from control voltage */
  410. if (ctrl_uV < vrange_ctrl->min_uV) {
  411. vctrl->sel = 0;
  412. } else if (ctrl_uV > vrange_ctrl->max_uV) {
  413. vctrl->sel = rdesc->n_voltages - 1;
  414. } else {
  415. int i;
  416. for (i = 0; i < rdesc->n_voltages; i++) {
  417. if (ctrl_uV == vctrl->vtable[i].ctrl) {
  418. vctrl->sel = i;
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg);
  425. if (IS_ERR(vctrl->rdev)) {
  426. ret = PTR_ERR(vctrl->rdev);
  427. dev_err(&pdev->dev, "failed to register regulator: %d\n", ret);
  428. return ret;
  429. }
  430. return 0;
  431. }
  432. static const struct of_device_id vctrl_of_match[] = {
  433. { .compatible = "vctrl-regulator", },
  434. {},
  435. };
  436. MODULE_DEVICE_TABLE(of, vctrl_of_match);
  437. static struct platform_driver vctrl_driver = {
  438. .probe = vctrl_probe,
  439. .driver = {
  440. .name = "vctrl-regulator",
  441. .of_match_table = of_match_ptr(vctrl_of_match),
  442. },
  443. };
  444. module_platform_driver(vctrl_driver);
  445. MODULE_DESCRIPTION("Voltage Controlled Regulator Driver");
  446. MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
  447. MODULE_LICENSE("GPL v2");