Lines Matching +full:slew +full:- +full:percent

1 // SPDX-License-Identifier: GPL-2.0-only
50 struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl; in vctrl_calc_ctrl_voltage()
51 struct vctrl_voltage_range *out = &vctrl->vrange.out; in vctrl_calc_ctrl_voltage()
53 return ctrl->min_uV + in vctrl_calc_ctrl_voltage()
54 DIV_ROUND_CLOSEST_ULL((s64)(out_uV - out->min_uV) * in vctrl_calc_ctrl_voltage()
55 (ctrl->max_uV - ctrl->min_uV), in vctrl_calc_ctrl_voltage()
56 out->max_uV - out->min_uV); in vctrl_calc_ctrl_voltage()
61 struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl; in vctrl_calc_output_voltage()
62 struct vctrl_voltage_range *out = &vctrl->vrange.out; in vctrl_calc_output_voltage()
69 if (ctrl_uV < ctrl->min_uV) in vctrl_calc_output_voltage()
70 return out->min_uV; in vctrl_calc_output_voltage()
72 if (ctrl_uV > ctrl->max_uV) in vctrl_calc_output_voltage()
73 return out->max_uV; in vctrl_calc_output_voltage()
75 return out->min_uV + in vctrl_calc_output_voltage()
76 DIV_ROUND_CLOSEST_ULL((s64)(ctrl_uV - ctrl->min_uV) * in vctrl_calc_output_voltage()
77 (out->max_uV - out->min_uV), in vctrl_calc_output_voltage()
78 ctrl->max_uV - ctrl->min_uV); in vctrl_calc_output_voltage()
86 if (!rdev->supply) in vctrl_get_voltage()
87 return -EPROBE_DEFER; in vctrl_get_voltage()
89 ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in vctrl_get_voltage()
103 if (!rdev->supply) in vctrl_set_voltage()
104 return -EPROBE_DEFER; in vctrl_set_voltage()
106 orig_ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev); in vctrl_set_voltage()
109 if (req_min_uV >= uV || !vctrl->ovp_threshold) in vctrl_set_voltage()
111 return regulator_set_voltage_rdev(rdev->supply->rdev, in vctrl_set_voltage()
117 int max_drop_uV = (uV * vctrl->ovp_threshold) / 100; in vctrl_set_voltage()
126 next_uV = max_t(int, req_min_uV, uV - max_drop_uV); in vctrl_set_voltage()
129 ret = regulator_set_voltage_rdev(rdev->supply->rdev, in vctrl_set_voltage()
136 delay = DIV_ROUND_UP(uV - next_uV, vctrl->min_slew_down_rate); in vctrl_set_voltage()
146 regulator_set_voltage_rdev(rdev->supply->rdev, orig_ctrl_uV, orig_ctrl_uV, in vctrl_set_voltage()
156 return vctrl->sel; in vctrl_get_voltage_sel()
163 unsigned int orig_sel = vctrl->sel; in vctrl_set_voltage_sel()
166 if (!rdev->supply) in vctrl_set_voltage_sel()
167 return -EPROBE_DEFER; in vctrl_set_voltage_sel()
169 if (selector >= rdev->desc->n_voltages) in vctrl_set_voltage_sel()
170 return -EINVAL; in vctrl_set_voltage_sel()
172 if (selector >= vctrl->sel || !vctrl->ovp_threshold) { in vctrl_set_voltage_sel()
174 ret = regulator_set_voltage_rdev(rdev->supply->rdev, in vctrl_set_voltage_sel()
175 vctrl->vtable[selector].ctrl, in vctrl_set_voltage_sel()
176 vctrl->vtable[selector].ctrl, in vctrl_set_voltage_sel()
179 vctrl->sel = selector; in vctrl_set_voltage_sel()
184 while (vctrl->sel != selector) { in vctrl_set_voltage_sel()
188 next_sel = max_t(unsigned int, selector, vctrl->vtable[vctrl->sel].ovp_min_sel); in vctrl_set_voltage_sel()
190 ret = regulator_set_voltage_rdev(rdev->supply->rdev, in vctrl_set_voltage_sel()
191 vctrl->vtable[next_sel].ctrl, in vctrl_set_voltage_sel()
192 vctrl->vtable[next_sel].ctrl, in vctrl_set_voltage_sel()
195 dev_err(&rdev->dev, in vctrl_set_voltage_sel()
197 vctrl->vtable[next_sel].ctrl); in vctrl_set_voltage_sel()
200 vctrl->sel = next_sel; in vctrl_set_voltage_sel()
202 delay = DIV_ROUND_UP(vctrl->vtable[vctrl->sel].out - in vctrl_set_voltage_sel()
203 vctrl->vtable[next_sel].out, in vctrl_set_voltage_sel()
204 vctrl->min_slew_down_rate); in vctrl_set_voltage_sel()
211 if (vctrl->sel != orig_sel) { in vctrl_set_voltage_sel()
213 if (!regulator_set_voltage_rdev(rdev->supply->rdev, in vctrl_set_voltage_sel()
214 vctrl->vtable[orig_sel].ctrl, in vctrl_set_voltage_sel()
215 vctrl->vtable[orig_sel].ctrl, in vctrl_set_voltage_sel()
217 vctrl->sel = orig_sel; in vctrl_set_voltage_sel()
219 dev_warn(&rdev->dev, in vctrl_set_voltage_sel()
231 if (selector >= rdev->desc->n_voltages) in vctrl_list_voltage()
232 return -EINVAL; in vctrl_list_voltage()
234 return vctrl->vtable[selector].out; in vctrl_list_voltage()
241 struct device_node *np = pdev->dev.of_node; in vctrl_parse_dt()
245 ret = of_property_read_u32(np, "ovp-threshold-percent", &pval); in vctrl_parse_dt()
247 vctrl->ovp_threshold = pval; in vctrl_parse_dt()
248 if (vctrl->ovp_threshold > 100) { in vctrl_parse_dt()
249 dev_err(&pdev->dev, in vctrl_parse_dt()
250 "ovp-threshold-percent (%u) > 100\n", in vctrl_parse_dt()
251 vctrl->ovp_threshold); in vctrl_parse_dt()
252 return -EINVAL; in vctrl_parse_dt()
256 ret = of_property_read_u32(np, "min-slew-down-rate", &pval); in vctrl_parse_dt()
258 vctrl->min_slew_down_rate = pval; in vctrl_parse_dt()
261 if (vctrl->min_slew_down_rate == 0) { in vctrl_parse_dt()
262 dev_err(&pdev->dev, in vctrl_parse_dt()
263 "min-slew-down-rate must not be 0\n"); in vctrl_parse_dt()
264 return -EINVAL; in vctrl_parse_dt()
265 } else if (vctrl->min_slew_down_rate > INT_MAX) { in vctrl_parse_dt()
266 dev_err(&pdev->dev, "min-slew-down-rate (%u) too big\n", in vctrl_parse_dt()
267 vctrl->min_slew_down_rate); in vctrl_parse_dt()
268 return -EINVAL; in vctrl_parse_dt()
272 if (vctrl->ovp_threshold && !vctrl->min_slew_down_rate) { in vctrl_parse_dt()
273 dev_err(&pdev->dev, in vctrl_parse_dt()
274 "ovp-threshold-percent requires min-slew-down-rate\n"); in vctrl_parse_dt()
275 return -EINVAL; in vctrl_parse_dt()
278 ret = of_property_read_u32(np, "regulator-min-microvolt", &pval); in vctrl_parse_dt()
280 dev_err(&pdev->dev, in vctrl_parse_dt()
281 "failed to read regulator-min-microvolt: %d\n", ret); in vctrl_parse_dt()
284 vctrl->vrange.out.min_uV = pval; in vctrl_parse_dt()
286 ret = of_property_read_u32(np, "regulator-max-microvolt", &pval); in vctrl_parse_dt()
288 dev_err(&pdev->dev, in vctrl_parse_dt()
289 "failed to read regulator-max-microvolt: %d\n", ret); in vctrl_parse_dt()
292 vctrl->vrange.out.max_uV = pval; in vctrl_parse_dt()
294 ret = of_property_read_u32_array(np, "ctrl-voltage-range", vrange_ctrl, in vctrl_parse_dt()
297 dev_err(&pdev->dev, "failed to read ctrl-voltage-range: %d\n", in vctrl_parse_dt()
303 dev_err(&pdev->dev, "ctrl-voltage-range is invalid: %d-%d\n", in vctrl_parse_dt()
305 return -EINVAL; in vctrl_parse_dt()
308 vctrl->vrange.ctrl.min_uV = vrange_ctrl[0]; in vctrl_parse_dt()
309 vctrl->vrange.ctrl.max_uV = vrange_ctrl[1]; in vctrl_parse_dt()
319 return at->ctrl - bt->ctrl; in vctrl_cmp_ctrl_uV()
326 struct regulator_desc *rdesc = &vctrl->desc; in vctrl_init_vtable()
327 struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl; in vctrl_init_vtable()
334 rdesc->n_voltages = n_voltages; in vctrl_init_vtable()
340 if (ctrl_uV < vrange_ctrl->min_uV || in vctrl_init_vtable()
341 ctrl_uV > vrange_ctrl->max_uV) in vctrl_init_vtable()
342 rdesc->n_voltages--; in vctrl_init_vtable()
345 if (rdesc->n_voltages == 0) { in vctrl_init_vtable()
346 dev_err(&pdev->dev, "invalid configuration\n"); in vctrl_init_vtable()
347 return -EINVAL; in vctrl_init_vtable()
350 vctrl->vtable = devm_kcalloc(&pdev->dev, rdesc->n_voltages, in vctrl_init_vtable()
353 if (!vctrl->vtable) in vctrl_init_vtable()
354 return -ENOMEM; in vctrl_init_vtable()
360 if (ctrl_uV < vrange_ctrl->min_uV || in vctrl_init_vtable()
361 ctrl_uV > vrange_ctrl->max_uV) in vctrl_init_vtable()
364 vctrl->vtable[idx_vt].ctrl = ctrl_uV; in vctrl_init_vtable()
365 vctrl->vtable[idx_vt].out = in vctrl_init_vtable()
371 sort(vctrl->vtable, rdesc->n_voltages, in vctrl_init_vtable()
375 /* pre-calculate OVP-safe downward transitions */ in vctrl_init_vtable()
376 for (i = rdesc->n_voltages - 1; i > 0; i--) { in vctrl_init_vtable()
378 int ovp_min_uV = (vctrl->vtable[i].out * in vctrl_init_vtable()
379 (100 - vctrl->ovp_threshold)) / 100; in vctrl_init_vtable()
382 if (vctrl->vtable[j].out >= ovp_min_uV) { in vctrl_init_vtable()
383 vctrl->vtable[i].ovp_min_sel = j; in vctrl_init_vtable()
389 dev_warn(&pdev->dev, "switching down from %duV may cause OVP shutdown\n", in vctrl_init_vtable()
390 vctrl->vtable[i].out); in vctrl_init_vtable()
392 vctrl->vtable[i].ovp_min_sel = i - 1; in vctrl_init_vtable()
403 vctrl->enabled = true; in vctrl_enable()
412 vctrl->enabled = false; in vctrl_disable()
421 return vctrl->enabled; in vctrl_is_enabled()
444 struct device_node *np = pdev->dev.of_node; in vctrl_probe()
454 vctrl = devm_kzalloc(&pdev->dev, sizeof(struct vctrl_data), in vctrl_probe()
457 return -ENOMEM; in vctrl_probe()
465 ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl"); in vctrl_probe()
469 vrange_ctrl = &vctrl->vrange.ctrl; in vctrl_probe()
471 rdesc = &vctrl->desc; in vctrl_probe()
472 rdesc->name = "vctrl"; in vctrl_probe()
473 rdesc->type = REGULATOR_VOLTAGE; in vctrl_probe()
474 rdesc->owner = THIS_MODULE; in vctrl_probe()
475 rdesc->supply_name = "ctrl"; in vctrl_probe()
478 (regulator_count_voltages(ctrl_reg) == -EINVAL)) { in vctrl_probe()
479 rdesc->continuous_voltage_range = true; in vctrl_probe()
480 rdesc->ops = &vctrl_ops_cont; in vctrl_probe()
482 rdesc->ops = &vctrl_ops_non_cont; in vctrl_probe()
485 init_data = of_get_regulator_init_data(&pdev->dev, np, rdesc); in vctrl_probe()
487 return -ENOMEM; in vctrl_probe()
490 cfg.dev = &pdev->dev; in vctrl_probe()
494 if (!rdesc->continuous_voltage_range) { in vctrl_probe()
502 dev_err(&pdev->dev, "failed to get control voltage\n"); in vctrl_probe()
507 if (ctrl_uV < vrange_ctrl->min_uV) { in vctrl_probe()
508 vctrl->sel = 0; in vctrl_probe()
509 } else if (ctrl_uV > vrange_ctrl->max_uV) { in vctrl_probe()
510 vctrl->sel = rdesc->n_voltages - 1; in vctrl_probe()
514 for (i = 0; i < rdesc->n_voltages; i++) { in vctrl_probe()
515 if (ctrl_uV == vctrl->vtable[i].ctrl) { in vctrl_probe()
516 vctrl->sel = i; in vctrl_probe()
523 /* Drop ctrl-supply here in favor of regulator core managed supply */ in vctrl_probe()
526 vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg); in vctrl_probe()
527 if (IS_ERR(vctrl->rdev)) { in vctrl_probe()
528 ret = PTR_ERR(vctrl->rdev); in vctrl_probe()
529 dev_err(&pdev->dev, "failed to register regulator: %d\n", ret); in vctrl_probe()
537 { .compatible = "vctrl-regulator", },
545 .name = "vctrl-regulator",