Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Driver for voltage controller regulators
0004  *
0005  * Copyright (C) 2017 Google, Inc.
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/err.h>
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/of_device.h>
0014 #include <linux/regulator/coupler.h>
0015 #include <linux/regulator/driver.h>
0016 #include <linux/regulator/of_regulator.h>
0017 #include <linux/sort.h>
0018 
0019 #include "internal.h"
0020 
0021 struct vctrl_voltage_range {
0022     int min_uV;
0023     int max_uV;
0024 };
0025 
0026 struct vctrl_voltage_ranges {
0027     struct vctrl_voltage_range ctrl;
0028     struct vctrl_voltage_range out;
0029 };
0030 
0031 struct vctrl_voltage_table {
0032     int ctrl;
0033     int out;
0034     int ovp_min_sel;
0035 };
0036 
0037 struct vctrl_data {
0038     struct regulator_dev *rdev;
0039     struct regulator_desc desc;
0040     bool enabled;
0041     unsigned int min_slew_down_rate;
0042     unsigned int ovp_threshold;
0043     struct vctrl_voltage_ranges vrange;
0044     struct vctrl_voltage_table *vtable;
0045     unsigned int sel;
0046 };
0047 
0048 static int vctrl_calc_ctrl_voltage(struct vctrl_data *vctrl, int out_uV)
0049 {
0050     struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
0051     struct vctrl_voltage_range *out = &vctrl->vrange.out;
0052 
0053     return ctrl->min_uV +
0054         DIV_ROUND_CLOSEST_ULL((s64)(out_uV - out->min_uV) *
0055                       (ctrl->max_uV - ctrl->min_uV),
0056                       out->max_uV - out->min_uV);
0057 }
0058 
0059 static int vctrl_calc_output_voltage(struct vctrl_data *vctrl, int ctrl_uV)
0060 {
0061     struct vctrl_voltage_range *ctrl = &vctrl->vrange.ctrl;
0062     struct vctrl_voltage_range *out = &vctrl->vrange.out;
0063 
0064     if (ctrl_uV < 0) {
0065         pr_err("vctrl: failed to get control voltage\n");
0066         return ctrl_uV;
0067     }
0068 
0069     if (ctrl_uV < ctrl->min_uV)
0070         return out->min_uV;
0071 
0072     if (ctrl_uV > ctrl->max_uV)
0073         return out->max_uV;
0074 
0075     return out->min_uV +
0076         DIV_ROUND_CLOSEST_ULL((s64)(ctrl_uV - ctrl->min_uV) *
0077                       (out->max_uV - out->min_uV),
0078                       ctrl->max_uV - ctrl->min_uV);
0079 }
0080 
0081 static int vctrl_get_voltage(struct regulator_dev *rdev)
0082 {
0083     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0084     int ctrl_uV;
0085 
0086     if (!rdev->supply)
0087         return -EPROBE_DEFER;
0088 
0089     ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
0090 
0091     return vctrl_calc_output_voltage(vctrl, ctrl_uV);
0092 }
0093 
0094 static int vctrl_set_voltage(struct regulator_dev *rdev,
0095                  int req_min_uV, int req_max_uV,
0096                  unsigned int *selector)
0097 {
0098     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0099     int orig_ctrl_uV;
0100     int uV;
0101     int ret;
0102 
0103     if (!rdev->supply)
0104         return -EPROBE_DEFER;
0105 
0106     orig_ctrl_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
0107     uV = vctrl_calc_output_voltage(vctrl, orig_ctrl_uV);
0108 
0109     if (req_min_uV >= uV || !vctrl->ovp_threshold)
0110         /* voltage rising or no OVP */
0111         return regulator_set_voltage_rdev(rdev->supply->rdev,
0112             vctrl_calc_ctrl_voltage(vctrl, req_min_uV),
0113             vctrl_calc_ctrl_voltage(vctrl, req_max_uV),
0114             PM_SUSPEND_ON);
0115 
0116     while (uV > req_min_uV) {
0117         int max_drop_uV = (uV * vctrl->ovp_threshold) / 100;
0118         int next_uV;
0119         int next_ctrl_uV;
0120         int delay;
0121 
0122         /* Make sure no infinite loop even in crazy cases */
0123         if (max_drop_uV == 0)
0124             max_drop_uV = 1;
0125 
0126         next_uV = max_t(int, req_min_uV, uV - max_drop_uV);
0127         next_ctrl_uV = vctrl_calc_ctrl_voltage(vctrl, next_uV);
0128 
0129         ret = regulator_set_voltage_rdev(rdev->supply->rdev,
0130                         next_ctrl_uV,
0131                         next_ctrl_uV,
0132                         PM_SUSPEND_ON);
0133         if (ret)
0134             goto err;
0135 
0136         delay = DIV_ROUND_UP(uV - next_uV, vctrl->min_slew_down_rate);
0137         usleep_range(delay, delay + DIV_ROUND_UP(delay, 10));
0138 
0139         uV = next_uV;
0140     }
0141 
0142     return 0;
0143 
0144 err:
0145     /* Try to go back to original voltage */
0146     regulator_set_voltage_rdev(rdev->supply->rdev, orig_ctrl_uV, orig_ctrl_uV,
0147                    PM_SUSPEND_ON);
0148 
0149     return ret;
0150 }
0151 
0152 static int vctrl_get_voltage_sel(struct regulator_dev *rdev)
0153 {
0154     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0155 
0156     return vctrl->sel;
0157 }
0158 
0159 static int vctrl_set_voltage_sel(struct regulator_dev *rdev,
0160                  unsigned int selector)
0161 {
0162     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0163     unsigned int orig_sel = vctrl->sel;
0164     int ret;
0165 
0166     if (!rdev->supply)
0167         return -EPROBE_DEFER;
0168 
0169     if (selector >= rdev->desc->n_voltages)
0170         return -EINVAL;
0171 
0172     if (selector >= vctrl->sel || !vctrl->ovp_threshold) {
0173         /* voltage rising or no OVP */
0174         ret = regulator_set_voltage_rdev(rdev->supply->rdev,
0175                         vctrl->vtable[selector].ctrl,
0176                         vctrl->vtable[selector].ctrl,
0177                         PM_SUSPEND_ON);
0178         if (!ret)
0179             vctrl->sel = selector;
0180 
0181         return ret;
0182     }
0183 
0184     while (vctrl->sel != selector) {
0185         unsigned int next_sel;
0186         int delay;
0187 
0188         next_sel = max_t(unsigned int, selector, vctrl->vtable[vctrl->sel].ovp_min_sel);
0189 
0190         ret = regulator_set_voltage_rdev(rdev->supply->rdev,
0191                         vctrl->vtable[next_sel].ctrl,
0192                         vctrl->vtable[next_sel].ctrl,
0193                         PM_SUSPEND_ON);
0194         if (ret) {
0195             dev_err(&rdev->dev,
0196                 "failed to set control voltage to %duV\n",
0197                 vctrl->vtable[next_sel].ctrl);
0198             goto err;
0199         }
0200         vctrl->sel = next_sel;
0201 
0202         delay = DIV_ROUND_UP(vctrl->vtable[vctrl->sel].out -
0203                      vctrl->vtable[next_sel].out,
0204                      vctrl->min_slew_down_rate);
0205         usleep_range(delay, delay + DIV_ROUND_UP(delay, 10));
0206     }
0207 
0208     return 0;
0209 
0210 err:
0211     if (vctrl->sel != orig_sel) {
0212         /* Try to go back to original voltage */
0213         if (!regulator_set_voltage_rdev(rdev->supply->rdev,
0214                        vctrl->vtable[orig_sel].ctrl,
0215                        vctrl->vtable[orig_sel].ctrl,
0216                        PM_SUSPEND_ON))
0217             vctrl->sel = orig_sel;
0218         else
0219             dev_warn(&rdev->dev,
0220                  "failed to restore original voltage\n");
0221     }
0222 
0223     return ret;
0224 }
0225 
0226 static int vctrl_list_voltage(struct regulator_dev *rdev,
0227                   unsigned int selector)
0228 {
0229     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0230 
0231     if (selector >= rdev->desc->n_voltages)
0232         return -EINVAL;
0233 
0234     return vctrl->vtable[selector].out;
0235 }
0236 
0237 static int vctrl_parse_dt(struct platform_device *pdev,
0238               struct vctrl_data *vctrl)
0239 {
0240     int ret;
0241     struct device_node *np = pdev->dev.of_node;
0242     u32 pval;
0243     u32 vrange_ctrl[2];
0244 
0245     ret = of_property_read_u32(np, "ovp-threshold-percent", &pval);
0246     if (!ret) {
0247         vctrl->ovp_threshold = pval;
0248         if (vctrl->ovp_threshold > 100) {
0249             dev_err(&pdev->dev,
0250                 "ovp-threshold-percent (%u) > 100\n",
0251                 vctrl->ovp_threshold);
0252             return -EINVAL;
0253         }
0254     }
0255 
0256     ret = of_property_read_u32(np, "min-slew-down-rate", &pval);
0257     if (!ret) {
0258         vctrl->min_slew_down_rate = pval;
0259 
0260         /* We use the value as int and as divider; sanity check */
0261         if (vctrl->min_slew_down_rate == 0) {
0262             dev_err(&pdev->dev,
0263                 "min-slew-down-rate must not be 0\n");
0264             return -EINVAL;
0265         } else if (vctrl->min_slew_down_rate > INT_MAX) {
0266             dev_err(&pdev->dev, "min-slew-down-rate (%u) too big\n",
0267                 vctrl->min_slew_down_rate);
0268             return -EINVAL;
0269         }
0270     }
0271 
0272     if (vctrl->ovp_threshold && !vctrl->min_slew_down_rate) {
0273         dev_err(&pdev->dev,
0274             "ovp-threshold-percent requires min-slew-down-rate\n");
0275         return -EINVAL;
0276     }
0277 
0278     ret = of_property_read_u32(np, "regulator-min-microvolt", &pval);
0279     if (ret) {
0280         dev_err(&pdev->dev,
0281             "failed to read regulator-min-microvolt: %d\n", ret);
0282         return ret;
0283     }
0284     vctrl->vrange.out.min_uV = pval;
0285 
0286     ret = of_property_read_u32(np, "regulator-max-microvolt", &pval);
0287     if (ret) {
0288         dev_err(&pdev->dev,
0289             "failed to read regulator-max-microvolt: %d\n", ret);
0290         return ret;
0291     }
0292     vctrl->vrange.out.max_uV = pval;
0293 
0294     ret = of_property_read_u32_array(np, "ctrl-voltage-range", vrange_ctrl,
0295                      2);
0296     if (ret) {
0297         dev_err(&pdev->dev, "failed to read ctrl-voltage-range: %d\n",
0298             ret);
0299         return ret;
0300     }
0301 
0302     if (vrange_ctrl[0] >= vrange_ctrl[1]) {
0303         dev_err(&pdev->dev, "ctrl-voltage-range is invalid: %d-%d\n",
0304             vrange_ctrl[0], vrange_ctrl[1]);
0305         return -EINVAL;
0306     }
0307 
0308     vctrl->vrange.ctrl.min_uV = vrange_ctrl[0];
0309     vctrl->vrange.ctrl.max_uV = vrange_ctrl[1];
0310 
0311     return 0;
0312 }
0313 
0314 static int vctrl_cmp_ctrl_uV(const void *a, const void *b)
0315 {
0316     const struct vctrl_voltage_table *at = a;
0317     const struct vctrl_voltage_table *bt = b;
0318 
0319     return at->ctrl - bt->ctrl;
0320 }
0321 
0322 static int vctrl_init_vtable(struct platform_device *pdev,
0323                  struct regulator *ctrl_reg)
0324 {
0325     struct vctrl_data *vctrl = platform_get_drvdata(pdev);
0326     struct regulator_desc *rdesc = &vctrl->desc;
0327     struct vctrl_voltage_range *vrange_ctrl = &vctrl->vrange.ctrl;
0328     int n_voltages;
0329     int ctrl_uV;
0330     int i, idx_vt;
0331 
0332     n_voltages = regulator_count_voltages(ctrl_reg);
0333 
0334     rdesc->n_voltages = n_voltages;
0335 
0336     /* determine number of steps within the range of the vctrl regulator */
0337     for (i = 0; i < n_voltages; i++) {
0338         ctrl_uV = regulator_list_voltage(ctrl_reg, i);
0339 
0340         if (ctrl_uV < vrange_ctrl->min_uV ||
0341             ctrl_uV > vrange_ctrl->max_uV)
0342             rdesc->n_voltages--;
0343     }
0344 
0345     if (rdesc->n_voltages == 0) {
0346         dev_err(&pdev->dev, "invalid configuration\n");
0347         return -EINVAL;
0348     }
0349 
0350     vctrl->vtable = devm_kcalloc(&pdev->dev, rdesc->n_voltages,
0351                      sizeof(struct vctrl_voltage_table),
0352                      GFP_KERNEL);
0353     if (!vctrl->vtable)
0354         return -ENOMEM;
0355 
0356     /* create mapping control <=> output voltage */
0357     for (i = 0, idx_vt = 0; i < n_voltages; i++) {
0358         ctrl_uV = regulator_list_voltage(ctrl_reg, i);
0359 
0360         if (ctrl_uV < vrange_ctrl->min_uV ||
0361             ctrl_uV > vrange_ctrl->max_uV)
0362             continue;
0363 
0364         vctrl->vtable[idx_vt].ctrl = ctrl_uV;
0365         vctrl->vtable[idx_vt].out =
0366             vctrl_calc_output_voltage(vctrl, ctrl_uV);
0367         idx_vt++;
0368     }
0369 
0370     /* we rely on the table to be ordered by ascending voltage */
0371     sort(vctrl->vtable, rdesc->n_voltages,
0372          sizeof(struct vctrl_voltage_table), vctrl_cmp_ctrl_uV,
0373          NULL);
0374 
0375     /* pre-calculate OVP-safe downward transitions */
0376     for (i = rdesc->n_voltages - 1; i > 0; i--) {
0377         int j;
0378         int ovp_min_uV = (vctrl->vtable[i].out *
0379                   (100 - vctrl->ovp_threshold)) / 100;
0380 
0381         for (j = 0; j < i; j++) {
0382             if (vctrl->vtable[j].out >= ovp_min_uV) {
0383                 vctrl->vtable[i].ovp_min_sel = j;
0384                 break;
0385             }
0386         }
0387 
0388         if (j == i) {
0389             dev_warn(&pdev->dev, "switching down from %duV may cause OVP shutdown\n",
0390                 vctrl->vtable[i].out);
0391             /* use next lowest voltage */
0392             vctrl->vtable[i].ovp_min_sel = i - 1;
0393         }
0394     }
0395 
0396     return 0;
0397 }
0398 
0399 static int vctrl_enable(struct regulator_dev *rdev)
0400 {
0401     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0402 
0403     vctrl->enabled = true;
0404 
0405     return 0;
0406 }
0407 
0408 static int vctrl_disable(struct regulator_dev *rdev)
0409 {
0410     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0411 
0412     vctrl->enabled = false;
0413 
0414     return 0;
0415 }
0416 
0417 static int vctrl_is_enabled(struct regulator_dev *rdev)
0418 {
0419     struct vctrl_data *vctrl = rdev_get_drvdata(rdev);
0420 
0421     return vctrl->enabled;
0422 }
0423 
0424 static const struct regulator_ops vctrl_ops_cont = {
0425     .enable       = vctrl_enable,
0426     .disable      = vctrl_disable,
0427     .is_enabled   = vctrl_is_enabled,
0428     .get_voltage      = vctrl_get_voltage,
0429     .set_voltage      = vctrl_set_voltage,
0430 };
0431 
0432 static const struct regulator_ops vctrl_ops_non_cont = {
0433     .enable       = vctrl_enable,
0434     .disable      = vctrl_disable,
0435     .is_enabled   = vctrl_is_enabled,
0436     .set_voltage_sel = vctrl_set_voltage_sel,
0437     .get_voltage_sel = vctrl_get_voltage_sel,
0438     .list_voltage    = vctrl_list_voltage,
0439     .map_voltage     = regulator_map_voltage_iterate,
0440 };
0441 
0442 static int vctrl_probe(struct platform_device *pdev)
0443 {
0444     struct device_node *np = pdev->dev.of_node;
0445     struct vctrl_data *vctrl;
0446     const struct regulator_init_data *init_data;
0447     struct regulator_desc *rdesc;
0448     struct regulator_config cfg = { };
0449     struct vctrl_voltage_range *vrange_ctrl;
0450     struct regulator *ctrl_reg;
0451     int ctrl_uV;
0452     int ret;
0453 
0454     vctrl = devm_kzalloc(&pdev->dev, sizeof(struct vctrl_data),
0455                  GFP_KERNEL);
0456     if (!vctrl)
0457         return -ENOMEM;
0458 
0459     platform_set_drvdata(pdev, vctrl);
0460 
0461     ret = vctrl_parse_dt(pdev, vctrl);
0462     if (ret)
0463         return ret;
0464 
0465     ctrl_reg = devm_regulator_get(&pdev->dev, "ctrl");
0466     if (IS_ERR(ctrl_reg))
0467         return PTR_ERR(ctrl_reg);
0468 
0469     vrange_ctrl = &vctrl->vrange.ctrl;
0470 
0471     rdesc = &vctrl->desc;
0472     rdesc->name = "vctrl";
0473     rdesc->type = REGULATOR_VOLTAGE;
0474     rdesc->owner = THIS_MODULE;
0475     rdesc->supply_name = "ctrl";
0476 
0477     if ((regulator_get_linear_step(ctrl_reg) == 1) ||
0478         (regulator_count_voltages(ctrl_reg) == -EINVAL)) {
0479         rdesc->continuous_voltage_range = true;
0480         rdesc->ops = &vctrl_ops_cont;
0481     } else {
0482         rdesc->ops = &vctrl_ops_non_cont;
0483     }
0484 
0485     init_data = of_get_regulator_init_data(&pdev->dev, np, rdesc);
0486     if (!init_data)
0487         return -ENOMEM;
0488 
0489     cfg.of_node = np;
0490     cfg.dev = &pdev->dev;
0491     cfg.driver_data = vctrl;
0492     cfg.init_data = init_data;
0493 
0494     if (!rdesc->continuous_voltage_range) {
0495         ret = vctrl_init_vtable(pdev, ctrl_reg);
0496         if (ret)
0497             return ret;
0498 
0499         /* Use locked consumer API when not in regulator framework */
0500         ctrl_uV = regulator_get_voltage(ctrl_reg);
0501         if (ctrl_uV < 0) {
0502             dev_err(&pdev->dev, "failed to get control voltage\n");
0503             return ctrl_uV;
0504         }
0505 
0506         /* determine current voltage selector from control voltage */
0507         if (ctrl_uV < vrange_ctrl->min_uV) {
0508             vctrl->sel = 0;
0509         } else if (ctrl_uV > vrange_ctrl->max_uV) {
0510             vctrl->sel = rdesc->n_voltages - 1;
0511         } else {
0512             int i;
0513 
0514             for (i = 0; i < rdesc->n_voltages; i++) {
0515                 if (ctrl_uV == vctrl->vtable[i].ctrl) {
0516                     vctrl->sel = i;
0517                     break;
0518                 }
0519             }
0520         }
0521     }
0522 
0523     /* Drop ctrl-supply here in favor of regulator core managed supply */
0524     devm_regulator_put(ctrl_reg);
0525 
0526     vctrl->rdev = devm_regulator_register(&pdev->dev, rdesc, &cfg);
0527     if (IS_ERR(vctrl->rdev)) {
0528         ret = PTR_ERR(vctrl->rdev);
0529         dev_err(&pdev->dev, "failed to register regulator: %d\n", ret);
0530         return ret;
0531     }
0532 
0533     return 0;
0534 }
0535 
0536 static const struct of_device_id vctrl_of_match[] = {
0537     { .compatible = "vctrl-regulator", },
0538     {},
0539 };
0540 MODULE_DEVICE_TABLE(of, vctrl_of_match);
0541 
0542 static struct platform_driver vctrl_driver = {
0543     .probe      = vctrl_probe,
0544     .driver     = {
0545         .name       = "vctrl-regulator",
0546         .of_match_table = of_match_ptr(vctrl_of_match),
0547     },
0548 };
0549 
0550 module_platform_driver(vctrl_driver);
0551 
0552 MODULE_DESCRIPTION("Voltage Controlled Regulator Driver");
0553 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
0554 MODULE_LICENSE("GPL v2");