hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/drivers/regulator/vctrl-regulator.c
....@@ -1,16 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Driver for voltage controller regulators
34 *
45 * 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.
146 */
157
168 #include <linux/delay.h>
....@@ -19,9 +11,12 @@
1911 #include <linux/module.h>
2012 #include <linux/of.h>
2113 #include <linux/of_device.h>
14
+#include <linux/regulator/coupler.h>
2215 #include <linux/regulator/driver.h>
2316 #include <linux/regulator/of_regulator.h>
2417 #include <linux/sort.h>
18
+
19
+#include "internal.h"
2520
2621 struct vctrl_voltage_range {
2722 int min_uV;
....@@ -42,7 +37,6 @@
4237 struct vctrl_data {
4338 struct regulator_dev *rdev;
4439 struct regulator_desc desc;
45
- struct regulator *ctrl_reg;
4640 bool enabled;
4741 unsigned int min_slew_down_rate;
4842 unsigned int ovp_threshold;
....@@ -87,7 +81,12 @@
8781 static int vctrl_get_voltage(struct regulator_dev *rdev)
8882 {
8983 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);
9190
9291 return vctrl_calc_output_voltage(vctrl, ctrl_uV);
9392 }
....@@ -97,17 +96,22 @@
9796 unsigned int *selector)
9897 {
9998 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;
103101 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);
104108
105109 if (req_min_uV >= uV || !vctrl->ovp_threshold)
106110 /* voltage rising or no OVP */
107
- return regulator_set_voltage(
108
- ctrl_reg,
111
+ return regulator_set_voltage_rdev(rdev->supply->rdev,
109112 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);
111115
112116 while (uV > req_min_uV) {
113117 int max_drop_uV = (uV * vctrl->ovp_threshold) / 100;
....@@ -122,9 +126,10 @@
122126 next_uV = max_t(int, req_min_uV, uV - max_drop_uV);
123127 next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV);
124128
125
- ret = regulator_set_voltage(ctrl_reg,
129
+ ret = regulator_set_voltage_rdev(rdev->supply->rdev,
126130 next_ctrl_uV,
127
- next_ctrl_uV);
131
+ next_ctrl_uV,
132
+ PM_SUSPEND_ON);
128133 if (ret)
129134 goto err;
130135
....@@ -138,7 +143,8 @@
138143
139144 err:
140145 /* 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);
142148
143149 return ret;
144150 }
....@@ -154,18 +160,21 @@
154160 unsigned int selector)
155161 {
156162 struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
157
- struct regulator *ctrl_reg = vctrl->ctrl_reg;
158163 unsigned int orig_sel = vctrl->sel;
159164 int ret;
165
+
166
+ if (!rdev->supply)
167
+ return -EPROBE_DEFER;
160168
161169 if (selector >= rdev->desc->n_voltages)
162170 return -EINVAL;
163171
164172 if (selector >= vctrl->sel || !vctrl->ovp_threshold) {
165173 /* voltage rising or no OVP */
166
- ret = regulator_set_voltage(ctrl_reg,
174
+ ret = regulator_set_voltage_rdev(rdev->supply->rdev,
167175 vctrl->vtable[selector].ctrl,
168
- vctrl->vtable[selector].ctrl);
176
+ vctrl->vtable[selector].ctrl,
177
+ PM_SUSPEND_ON);
169178 if (!ret)
170179 vctrl->sel = selector;
171180
....@@ -181,9 +190,10 @@
181190 else
182191 next_sel = vctrl->vtable[vctrl->sel].ovp_min_sel;
183192
184
- ret = regulator_set_voltage(ctrl_reg,
193
+ ret = regulator_set_voltage_rdev(rdev->supply->rdev,
185194 vctrl->vtable[next_sel].ctrl,
186
- vctrl->vtable[next_sel].ctrl);
195
+ vctrl->vtable[next_sel].ctrl,
196
+ PM_SUSPEND_ON);
187197 if (ret) {
188198 dev_err(&rdev->dev,
189199 "failed to set control voltage to %duV\n",
....@@ -203,9 +213,10 @@
203213 err:
204214 if (vctrl->sel != orig_sel) {
205215 /* Try to go back to original voltage */
206
- if (!regulator_set_voltage(ctrl_reg,
216
+ if (!regulator_set_voltage_rdev(rdev->supply->rdev,
207217 vctrl->vtable[orig_sel].ctrl,
208
- vctrl->vtable[orig_sel].ctrl))
218
+ vctrl->vtable[orig_sel].ctrl,
219
+ PM_SUSPEND_ON))
209220 vctrl->sel = orig_sel;
210221 else
211222 dev_warn(&rdev->dev,
....@@ -233,10 +244,6 @@
233244 struct device_node *np = pdev->dev.of_node;
234245 u32 pval;
235246 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);
240247
241248 ret = of_property_read_u32(np, "ovp-threshold-percent", &pval);
242249 if (!ret) {
....@@ -315,11 +322,11 @@
315322 return at->ctrl - bt->ctrl;
316323 }
317324
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)
319327 {
320328 struct vctrl_data *vctrl = platform_get_drvdata(pdev);
321329 struct regulator_desc *rdesc = &vctrl->desc;
322
- struct regulator *ctrl_reg = vctrl->ctrl_reg;
323330 struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl;
324331 int n_voltages;
325332 int ctrl_uV;
....@@ -334,10 +341,8 @@
334341 ctrl_uV = regulator_list_voltage(ctrl_reg, i);
335342
336343 if (ctrl_uV < vrange_ctrl->min_uV ||
337
- ctrl_uV > vrange_ctrl->max_uV) {
344
+ ctrl_uV > vrange_ctrl->max_uV)
338345 rdesc->n_voltages--;
339
- continue;
340
- }
341346 }
342347
343348 if (rdesc->n_voltages == 0) {
....@@ -397,23 +402,19 @@
397402 static int vctrl_enable(struct regulator_dev *rdev)
398403 {
399404 struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
400
- int ret = regulator_enable(vctrl->ctrl_reg);
401405
402
- if (!ret)
403
- vctrl->enabled = true;
406
+ vctrl->enabled = true;
404407
405
- return ret;
408
+ return 0;
406409 }
407410
408411 static int vctrl_disable(struct regulator_dev *rdev)
409412 {
410413 struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
411
- int ret = regulator_disable(vctrl->ctrl_reg);
412414
413
- if (!ret)
414
- vctrl->enabled = false;
415
+ vctrl->enabled = false;
415416
416
- return ret;
417
+ return 0;
417418 }
418419
419420 static int vctrl_is_enabled(struct regulator_dev *rdev)
....@@ -449,6 +450,7 @@
449450 struct regulator_desc *rdesc;
450451 struct regulator_config cfg = { };
451452 struct vctrl_voltage_range *vrange_ctrl;
453
+ struct regulator *ctrl_reg;
452454 int ctrl_uV;
453455 int ret;
454456
....@@ -463,15 +465,20 @@
463465 if (ret)
464466 return ret;
465467
468
+ ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl");
469
+ if (IS_ERR(ctrl_reg))
470
+ return PTR_ERR(ctrl_reg);
471
+
466472 vrange_ctrl = &vctrl->vrange.ctrl;
467473
468474 rdesc = &vctrl->desc;
469475 rdesc->name = "vctrl";
470476 rdesc->type = REGULATOR_VOLTAGE;
471477 rdesc->owner = THIS_MODULE;
478
+ rdesc->supply_name = "ctrl";
472479
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)) {
475482 rdesc->continuous_voltage_range = true;
476483 rdesc->ops = &vctrl_ops_cont;
477484 } else {
....@@ -488,11 +495,12 @@
488495 cfg.init_data = init_data;
489496
490497 if (!rdesc->continuous_voltage_range) {
491
- ret = vctrl_init_vtable(pdev);
498
+ ret = vctrl_init_vtable(pdev, ctrl_reg);
492499 if (ret)
493500 return ret;
494501
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);
496504 if (ctrl_uV < 0) {
497505 dev_err(&pdev->dev, "failed to get control voltage\n");
498506 return ctrl_uV;
....@@ -515,6 +523,9 @@
515523 }
516524 }
517525
526
+ /* Drop ctrl-supply here in favor of regulator core managed supply */
527
+ devm_regulator_put(ctrl_reg);
528
+
518529 vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg);
519530 if (IS_ERR(vctrl->rdev)) {
520531 ret = PTR_ERR(vctrl->rdev);