.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * Driver for voltage controller regulators |
---|
3 | 4 | * |
---|
4 | 5 | * 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 | 6 | */ |
---|
15 | 7 | |
---|
16 | 8 | #include <linux/delay.h> |
---|
.. | .. |
---|
19 | 11 | #include <linux/module.h> |
---|
20 | 12 | #include <linux/of.h> |
---|
21 | 13 | #include <linux/of_device.h> |
---|
| 14 | +#include <linux/regulator/coupler.h> |
---|
22 | 15 | #include <linux/regulator/driver.h> |
---|
23 | 16 | #include <linux/regulator/of_regulator.h> |
---|
24 | 17 | #include <linux/sort.h> |
---|
| 18 | + |
---|
| 19 | +#include "internal.h" |
---|
25 | 20 | |
---|
26 | 21 | struct vctrl_voltage_range { |
---|
27 | 22 | int min_uV; |
---|
.. | .. |
---|
42 | 37 | struct vctrl_data { |
---|
43 | 38 | struct regulator_dev *rdev; |
---|
44 | 39 | struct regulator_desc desc; |
---|
45 | | - struct regulator *ctrl_reg; |
---|
46 | 40 | bool enabled; |
---|
47 | 41 | unsigned int min_slew_down_rate; |
---|
48 | 42 | unsigned int ovp_threshold; |
---|
.. | .. |
---|
87 | 81 | static int vctrl_get_voltage(struct regulator_dev *rdev) |
---|
88 | 82 | { |
---|
89 | 83 | struct vctrl_data *vctrl = rdev_get_drvdata(rdev); |
---|
90 | | - int ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg); |
---|
| 84 | + int ctrl_uV; |
---|
| 85 | + |
---|
| 86 | + if (!rdev->supply) |
---|
| 87 | + return -EPROBE_DEFER; |
---|
| 88 | + |
---|
| 89 | + ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); |
---|
91 | 90 | |
---|
92 | 91 | return vctrl_calc_output_voltage(vctrl, ctrl_uV); |
---|
93 | 92 | } |
---|
.. | .. |
---|
97 | 96 | unsigned int *selector) |
---|
98 | 97 | { |
---|
99 | 98 | struct vctrl_data *vctrl = rdev_get_drvdata(rdev); |
---|
100 | | - struct regulator *ctrl_reg = vctrl->ctrl_reg; |
---|
101 | | - int orig_ctrl_uV = regulator_get_voltage(ctrl_reg); |
---|
102 | | - int uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV); |
---|
| 99 | + int orig_ctrl_uV; |
---|
| 100 | + int uV; |
---|
103 | 101 | int ret; |
---|
| 102 | + |
---|
| 103 | + if (!rdev->supply) |
---|
| 104 | + return -EPROBE_DEFER; |
---|
| 105 | + |
---|
| 106 | + orig_ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); |
---|
| 107 | + uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV); |
---|
104 | 108 | |
---|
105 | 109 | if (req_min_uV >= uV || !vctrl->ovp_threshold) |
---|
106 | 110 | /* voltage rising or no OVP */ |
---|
107 | | - return regulator_set_voltage( |
---|
108 | | - ctrl_reg, |
---|
| 111 | + return regulator_set_voltage_rdev(rdev->supply->rdev, |
---|
109 | 112 | vctrl_calc_ctrl_voltage(vctrl, req_min_uV), |
---|
110 | | - vctrl_calc_ctrl_voltage(vctrl, req_max_uV)); |
---|
| 113 | + vctrl_calc_ctrl_voltage(vctrl, req_max_uV), |
---|
| 114 | + PM_SUSPEND_ON); |
---|
111 | 115 | |
---|
112 | 116 | while (uV > req_min_uV) { |
---|
113 | 117 | int max_drop_uV = (uV * vctrl->ovp_threshold) / 100; |
---|
.. | .. |
---|
122 | 126 | next_uV = max_t(int, req_min_uV, uV - max_drop_uV); |
---|
123 | 127 | next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV); |
---|
124 | 128 | |
---|
125 | | - ret = regulator_set_voltage(ctrl_reg, |
---|
| 129 | + ret = regulator_set_voltage_rdev(rdev->supply->rdev, |
---|
126 | 130 | next_ctrl_uV, |
---|
127 | | - next_ctrl_uV); |
---|
| 131 | + next_ctrl_uV, |
---|
| 132 | + PM_SUSPEND_ON); |
---|
128 | 133 | if (ret) |
---|
129 | 134 | goto err; |
---|
130 | 135 | |
---|
.. | .. |
---|
138 | 143 | |
---|
139 | 144 | err: |
---|
140 | 145 | /* Try to go back to original voltage */ |
---|
141 | | - regulator_set_voltage(ctrl_reg, orig_ctrl_uV, orig_ctrl_uV); |
---|
| 146 | + regulator_set_voltage_rdev(rdev->supply->rdev, orig_ctrl_uV, orig_ctrl_uV, |
---|
| 147 | + PM_SUSPEND_ON); |
---|
142 | 148 | |
---|
143 | 149 | return ret; |
---|
144 | 150 | } |
---|
.. | .. |
---|
154 | 160 | unsigned int selector) |
---|
155 | 161 | { |
---|
156 | 162 | struct vctrl_data *vctrl = rdev_get_drvdata(rdev); |
---|
157 | | - struct regulator *ctrl_reg = vctrl->ctrl_reg; |
---|
158 | 163 | unsigned int orig_sel = vctrl->sel; |
---|
159 | 164 | int ret; |
---|
| 165 | + |
---|
| 166 | + if (!rdev->supply) |
---|
| 167 | + return -EPROBE_DEFER; |
---|
160 | 168 | |
---|
161 | 169 | if (selector >= rdev->desc->n_voltages) |
---|
162 | 170 | return -EINVAL; |
---|
163 | 171 | |
---|
164 | 172 | if (selector >= vctrl->sel || !vctrl->ovp_threshold) { |
---|
165 | 173 | /* voltage rising or no OVP */ |
---|
166 | | - ret = regulator_set_voltage(ctrl_reg, |
---|
| 174 | + ret = regulator_set_voltage_rdev(rdev->supply->rdev, |
---|
167 | 175 | vctrl->vtable[selector].ctrl, |
---|
168 | | - vctrl->vtable[selector].ctrl); |
---|
| 176 | + vctrl->vtable[selector].ctrl, |
---|
| 177 | + PM_SUSPEND_ON); |
---|
169 | 178 | if (!ret) |
---|
170 | 179 | vctrl->sel = selector; |
---|
171 | 180 | |
---|
.. | .. |
---|
181 | 190 | else |
---|
182 | 191 | next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel; |
---|
183 | 192 | |
---|
184 | | - ret = regulator_set_voltage(ctrl_reg, |
---|
| 193 | + ret = regulator_set_voltage_rdev(rdev->supply->rdev, |
---|
185 | 194 | vctrl->vtable[next_sel].ctrl, |
---|
186 | | - vctrl->vtable[next_sel].ctrl); |
---|
| 195 | + vctrl->vtable[next_sel].ctrl, |
---|
| 196 | + PM_SUSPEND_ON); |
---|
187 | 197 | if (ret) { |
---|
188 | 198 | dev_err(&rdev->dev, |
---|
189 | 199 | "failed to set control voltage to %duV\n", |
---|
.. | .. |
---|
203 | 213 | err: |
---|
204 | 214 | if (vctrl->sel != orig_sel) { |
---|
205 | 215 | /* Try to go back to original voltage */ |
---|
206 | | - if (!regulator_set_voltage(ctrl_reg, |
---|
| 216 | + if (!regulator_set_voltage_rdev(rdev->supply->rdev, |
---|
207 | 217 | vctrl->vtable[orig_sel].ctrl, |
---|
208 | | - vctrl->vtable[orig_sel].ctrl)) |
---|
| 218 | + vctrl->vtable[orig_sel].ctrl, |
---|
| 219 | + PM_SUSPEND_ON)) |
---|
209 | 220 | vctrl->sel = orig_sel; |
---|
210 | 221 | else |
---|
211 | 222 | dev_warn(&rdev->dev, |
---|
.. | .. |
---|
233 | 244 | struct device_node *np = pdev->dev.of_node; |
---|
234 | 245 | u32 pval; |
---|
235 | 246 | u32 vrange_ctrl[2]; |
---|
236 | | - |
---|
237 | | - vctrl->ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl"); |
---|
238 | | - if (IS_ERR(vctrl->ctrl_reg)) |
---|
239 | | - return PTR_ERR(vctrl->ctrl_reg); |
---|
240 | 247 | |
---|
241 | 248 | ret = of_property_read_u32(np, "ovp-threshold-percent", &pval); |
---|
242 | 249 | if (!ret) { |
---|
.. | .. |
---|
315 | 322 | return at->ctrl - bt->ctrl; |
---|
316 | 323 | } |
---|
317 | 324 | |
---|
318 | | -static int vctrl_init_vtable(struct platform_device *pdev) |
---|
| 325 | +static int vctrl_init_vtable(struct platform_device *pdev, |
---|
| 326 | + struct regulator *ctrl_reg) |
---|
319 | 327 | { |
---|
320 | 328 | struct vctrl_data *vctrl = platform_get_drvdata(pdev); |
---|
321 | 329 | struct regulator_desc *rdesc = &vctrl->desc; |
---|
322 | | - struct regulator *ctrl_reg = vctrl->ctrl_reg; |
---|
323 | 330 | struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl; |
---|
324 | 331 | int n_voltages; |
---|
325 | 332 | int ctrl_uV; |
---|
.. | .. |
---|
334 | 341 | ctrl_uV = regulator_list_voltage(ctrl_reg, i); |
---|
335 | 342 | |
---|
336 | 343 | if (ctrl_uV < vrange_ctrl->min_uV || |
---|
337 | | - ctrl_uV > vrange_ctrl->max_uV) { |
---|
| 344 | + ctrl_uV > vrange_ctrl->max_uV) |
---|
338 | 345 | rdesc->n_voltages--; |
---|
339 | | - continue; |
---|
340 | | - } |
---|
341 | 346 | } |
---|
342 | 347 | |
---|
343 | 348 | if (rdesc->n_voltages == 0) { |
---|
.. | .. |
---|
397 | 402 | static int vctrl_enable(struct regulator_dev *rdev) |
---|
398 | 403 | { |
---|
399 | 404 | struct vctrl_data *vctrl = rdev_get_drvdata(rdev); |
---|
400 | | - int ret = regulator_enable(vctrl->ctrl_reg); |
---|
401 | 405 | |
---|
402 | | - if (!ret) |
---|
403 | | - vctrl->enabled = true; |
---|
| 406 | + vctrl->enabled = true; |
---|
404 | 407 | |
---|
405 | | - return ret; |
---|
| 408 | + return 0; |
---|
406 | 409 | } |
---|
407 | 410 | |
---|
408 | 411 | static int vctrl_disable(struct regulator_dev *rdev) |
---|
409 | 412 | { |
---|
410 | 413 | struct vctrl_data *vctrl = rdev_get_drvdata(rdev); |
---|
411 | | - int ret = regulator_disable(vctrl->ctrl_reg); |
---|
412 | 414 | |
---|
413 | | - if (!ret) |
---|
414 | | - vctrl->enabled = false; |
---|
| 415 | + vctrl->enabled = false; |
---|
415 | 416 | |
---|
416 | | - return ret; |
---|
| 417 | + return 0; |
---|
417 | 418 | } |
---|
418 | 419 | |
---|
419 | 420 | static int vctrl_is_enabled(struct regulator_dev *rdev) |
---|
.. | .. |
---|
449 | 450 | struct regulator_desc *rdesc; |
---|
450 | 451 | struct regulator_config cfg = { }; |
---|
451 | 452 | struct vctrl_voltage_range *vrange_ctrl; |
---|
| 453 | + struct regulator *ctrl_reg; |
---|
452 | 454 | int ctrl_uV; |
---|
453 | 455 | int ret; |
---|
454 | 456 | |
---|
.. | .. |
---|
463 | 465 | if (ret) |
---|
464 | 466 | return ret; |
---|
465 | 467 | |
---|
| 468 | + ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl"); |
---|
| 469 | + if (IS_ERR(ctrl_reg)) |
---|
| 470 | + return PTR_ERR(ctrl_reg); |
---|
| 471 | + |
---|
466 | 472 | vrange_ctrl = &vctrl->vrange.ctrl; |
---|
467 | 473 | |
---|
468 | 474 | rdesc = &vctrl->desc; |
---|
469 | 475 | rdesc->name = "vctrl"; |
---|
470 | 476 | rdesc->type = REGULATOR_VOLTAGE; |
---|
471 | 477 | rdesc->owner = THIS_MODULE; |
---|
| 478 | + rdesc->supply_name = "ctrl"; |
---|
472 | 479 | |
---|
473 | | - if ((regulator_get_linear_step(vctrl->ctrl_reg) == 1) || |
---|
474 | | - (regulator_count_voltages(vctrl->ctrl_reg) == -EINVAL)) { |
---|
| 480 | + if ((regulator_get_linear_step(ctrl_reg) == 1) || |
---|
| 481 | + (regulator_count_voltages(ctrl_reg) == -EINVAL)) { |
---|
475 | 482 | rdesc->continuous_voltage_range = true; |
---|
476 | 483 | rdesc->ops = &vctrl_ops_cont; |
---|
477 | 484 | } else { |
---|
.. | .. |
---|
488 | 495 | cfg.init_data = init_data; |
---|
489 | 496 | |
---|
490 | 497 | if (!rdesc->continuous_voltage_range) { |
---|
491 | | - ret = vctrl_init_vtable(pdev); |
---|
| 498 | + ret = vctrl_init_vtable(pdev, ctrl_reg); |
---|
492 | 499 | if (ret) |
---|
493 | 500 | return ret; |
---|
494 | 501 | |
---|
495 | | - ctrl_uV = regulator_get_voltage(vctrl->ctrl_reg); |
---|
| 502 | + /* Use locked consumer API when not in regulator framework */ |
---|
| 503 | + ctrl_uV = regulator_get_voltage(ctrl_reg); |
---|
496 | 504 | if (ctrl_uV < 0) { |
---|
497 | 505 | dev_err(&pdev->dev, "failed to get control voltage\n"); |
---|
498 | 506 | return ctrl_uV; |
---|
.. | .. |
---|
515 | 523 | } |
---|
516 | 524 | } |
---|
517 | 525 | |
---|
| 526 | + /* Drop ctrl-supply here in favor of regulator core managed supply */ |
---|
| 527 | + devm_regulator_put(ctrl_reg); |
---|
| 528 | + |
---|
518 | 529 | vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg); |
---|
519 | 530 | if (IS_ERR(vctrl->rdev)) { |
---|
520 | 531 | ret = PTR_ERR(vctrl->rdev); |
---|