Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/kernel.h>
0003 #include <linux/init.h>
0004 
0005 #include "common.h"
0006 
0007 #include "voltage.h"
0008 #include "vp.h"
0009 #include "prm-regbits-34xx.h"
0010 #include "prm-regbits-44xx.h"
0011 #include "prm44xx.h"
0012 
0013 static u32 _vp_set_init_voltage(struct voltagedomain *voltdm, u32 volt)
0014 {
0015     struct omap_vp_instance *vp = voltdm->vp;
0016     u32 vpconfig;
0017     char vsel;
0018 
0019     vsel = voltdm->pmic->uv_to_vsel(volt);
0020 
0021     vpconfig = voltdm->read(vp->vpconfig);
0022     vpconfig &= ~(vp->common->vpconfig_initvoltage_mask |
0023               vp->common->vpconfig_forceupdate |
0024               vp->common->vpconfig_initvdd);
0025     vpconfig |= vsel << __ffs(vp->common->vpconfig_initvoltage_mask);
0026     voltdm->write(vpconfig, vp->vpconfig);
0027 
0028     /* Trigger initVDD value copy to voltage processor */
0029     voltdm->write((vpconfig | vp->common->vpconfig_initvdd),
0030                vp->vpconfig);
0031 
0032     /* Clear initVDD copy trigger bit */
0033     voltdm->write(vpconfig, vp->vpconfig);
0034 
0035     return vpconfig;
0036 }
0037 
0038 /* Generic voltage init functions */
0039 void __init omap_vp_init(struct voltagedomain *voltdm)
0040 {
0041     struct omap_vp_instance *vp = voltdm->vp;
0042     u32 val, sys_clk_rate, timeout, waittime;
0043     u32 vddmin, vddmax, vstepmin, vstepmax;
0044 
0045     if (!voltdm->pmic || !voltdm->pmic->uv_to_vsel) {
0046         pr_err("%s: No PMIC info for vdd_%s\n", __func__, voltdm->name);
0047         return;
0048     }
0049 
0050     if (!voltdm->read || !voltdm->write) {
0051         pr_err("%s: No read/write API for accessing vdd_%s regs\n",
0052             __func__, voltdm->name);
0053         return;
0054     }
0055 
0056     vp->enabled = false;
0057 
0058     /* Divide to avoid overflow */
0059     sys_clk_rate = voltdm->sys_clk.rate / 1000;
0060 
0061     timeout = (sys_clk_rate * voltdm->pmic->vp_timeout_us) / 1000;
0062     vddmin = max(voltdm->vp_param->vddmin, voltdm->pmic->vddmin);
0063     vddmax = min(voltdm->vp_param->vddmax, voltdm->pmic->vddmax);
0064     vddmin = voltdm->pmic->uv_to_vsel(vddmin);
0065     vddmax = voltdm->pmic->uv_to_vsel(vddmax);
0066 
0067     waittime = DIV_ROUND_UP(voltdm->pmic->step_size * sys_clk_rate,
0068                 1000 * voltdm->pmic->slew_rate);
0069     vstepmin = voltdm->pmic->vp_vstepmin;
0070     vstepmax = voltdm->pmic->vp_vstepmax;
0071 
0072     /*
0073      * VP_CONFIG: error gain is not set here, it will be updated
0074      * on each scale, based on OPP.
0075      */
0076     val = (voltdm->pmic->vp_erroroffset <<
0077            __ffs(voltdm->vp->common->vpconfig_erroroffset_mask)) |
0078         vp->common->vpconfig_timeouten;
0079     voltdm->write(val, vp->vpconfig);
0080 
0081     /* VSTEPMIN */
0082     val = (waittime << vp->common->vstepmin_smpswaittimemin_shift) |
0083         (vstepmin <<  vp->common->vstepmin_stepmin_shift);
0084     voltdm->write(val, vp->vstepmin);
0085 
0086     /* VSTEPMAX */
0087     val = (vstepmax << vp->common->vstepmax_stepmax_shift) |
0088         (waittime << vp->common->vstepmax_smpswaittimemax_shift);
0089     voltdm->write(val, vp->vstepmax);
0090 
0091     /* VLIMITTO */
0092     val = (vddmax << vp->common->vlimitto_vddmax_shift) |
0093         (vddmin << vp->common->vlimitto_vddmin_shift) |
0094         (timeout <<  vp->common->vlimitto_timeout_shift);
0095     voltdm->write(val, vp->vlimitto);
0096 }
0097 
0098 int omap_vp_update_errorgain(struct voltagedomain *voltdm,
0099                  unsigned long target_volt)
0100 {
0101     struct omap_volt_data *volt_data;
0102 
0103     if (!voltdm->vp)
0104         return -EINVAL;
0105 
0106     /* Get volt_data corresponding to target_volt */
0107     volt_data = omap_voltage_get_voltdata(voltdm, target_volt);
0108     if (IS_ERR(volt_data))
0109         return -EINVAL;
0110 
0111     /* Setting vp errorgain based on the voltage */
0112     voltdm->rmw(voltdm->vp->common->vpconfig_errorgain_mask,
0113             volt_data->vp_errgain <<
0114             __ffs(voltdm->vp->common->vpconfig_errorgain_mask),
0115             voltdm->vp->vpconfig);
0116 
0117     return 0;
0118 }
0119 
0120 /* VP force update method of voltage scaling */
0121 int omap_vp_forceupdate_scale(struct voltagedomain *voltdm,
0122                   unsigned long target_volt)
0123 {
0124     struct omap_vp_instance *vp = voltdm->vp;
0125     u32 vpconfig;
0126     u8 target_vsel, current_vsel;
0127     int ret, timeout = 0;
0128 
0129     ret = omap_vc_pre_scale(voltdm, target_volt, &target_vsel, &current_vsel);
0130     if (ret)
0131         return ret;
0132 
0133     /*
0134      * Clear all pending TransactionDone interrupt/status. Typical latency
0135      * is <3us
0136      */
0137     while (timeout++ < VP_TRANXDONE_TIMEOUT) {
0138         vp->common->ops->clear_txdone(vp->id);
0139         if (!vp->common->ops->check_txdone(vp->id))
0140             break;
0141         udelay(1);
0142     }
0143     if (timeout >= VP_TRANXDONE_TIMEOUT) {
0144         pr_warn("%s: vdd_%s TRANXDONE timeout exceeded. Voltage change aborted\n",
0145             __func__, voltdm->name);
0146         return -ETIMEDOUT;
0147     }
0148 
0149     vpconfig = _vp_set_init_voltage(voltdm, target_volt);
0150 
0151     /* Force update of voltage */
0152     voltdm->write(vpconfig | vp->common->vpconfig_forceupdate,
0153               voltdm->vp->vpconfig);
0154 
0155     /*
0156      * Wait for TransactionDone. Typical latency is <200us.
0157      * Depends on SMPSWAITTIMEMIN/MAX and voltage change
0158      */
0159     timeout = 0;
0160     omap_test_timeout(vp->common->ops->check_txdone(vp->id),
0161               VP_TRANXDONE_TIMEOUT, timeout);
0162     if (timeout >= VP_TRANXDONE_TIMEOUT)
0163         pr_err("%s: vdd_%s TRANXDONE timeout exceeded. TRANXDONE never got set after the voltage update\n",
0164                __func__, voltdm->name);
0165 
0166     omap_vc_post_scale(voltdm, target_volt, target_vsel, current_vsel);
0167 
0168     /*
0169      * Disable TransactionDone interrupt , clear all status, clear
0170      * control registers
0171      */
0172     timeout = 0;
0173     while (timeout++ < VP_TRANXDONE_TIMEOUT) {
0174         vp->common->ops->clear_txdone(vp->id);
0175         if (!vp->common->ops->check_txdone(vp->id))
0176             break;
0177         udelay(1);
0178     }
0179 
0180     if (timeout >= VP_TRANXDONE_TIMEOUT)
0181         pr_warn("%s: vdd_%s TRANXDONE timeout exceeded while trying to clear the TRANXDONE status\n",
0182             __func__, voltdm->name);
0183 
0184     /* Clear force bit */
0185     voltdm->write(vpconfig, vp->vpconfig);
0186 
0187     return 0;
0188 }
0189 
0190 /**
0191  * omap_vp_enable() - API to enable a particular VP
0192  * @voltdm: pointer to the VDD whose VP is to be enabled.
0193  *
0194  * This API enables a particular voltage processor. Needed by the smartreflex
0195  * class drivers.
0196  */
0197 void omap_vp_enable(struct voltagedomain *voltdm)
0198 {
0199     struct omap_vp_instance *vp;
0200     u32 vpconfig, volt;
0201 
0202     if (!voltdm || IS_ERR(voltdm)) {
0203         pr_warn("%s: VDD specified does not exist!\n", __func__);
0204         return;
0205     }
0206 
0207     vp = voltdm->vp;
0208     if (!voltdm->read || !voltdm->write) {
0209         pr_err("%s: No read/write API for accessing vdd_%s regs\n",
0210             __func__, voltdm->name);
0211         return;
0212     }
0213 
0214     /* If VP is already enabled, do nothing. Return */
0215     if (vp->enabled)
0216         return;
0217 
0218     volt = voltdm_get_voltage(voltdm);
0219     if (!volt) {
0220         pr_warn("%s: unable to find current voltage for %s\n",
0221             __func__, voltdm->name);
0222         return;
0223     }
0224 
0225     vpconfig = _vp_set_init_voltage(voltdm, volt);
0226 
0227     /* Enable VP */
0228     vpconfig |= vp->common->vpconfig_vpenable;
0229     voltdm->write(vpconfig, vp->vpconfig);
0230 
0231     vp->enabled = true;
0232 }
0233 
0234 /**
0235  * omap_vp_disable() - API to disable a particular VP
0236  * @voltdm: pointer to the VDD whose VP is to be disabled.
0237  *
0238  * This API disables a particular voltage processor. Needed by the smartreflex
0239  * class drivers.
0240  */
0241 void omap_vp_disable(struct voltagedomain *voltdm)
0242 {
0243     struct omap_vp_instance *vp;
0244     u32 vpconfig;
0245     int timeout;
0246 
0247     if (!voltdm || IS_ERR(voltdm)) {
0248         pr_warn("%s: VDD specified does not exist!\n", __func__);
0249         return;
0250     }
0251 
0252     vp = voltdm->vp;
0253     if (!voltdm->read || !voltdm->write) {
0254         pr_err("%s: No read/write API for accessing vdd_%s regs\n",
0255             __func__, voltdm->name);
0256         return;
0257     }
0258 
0259     /* If VP is already disabled, do nothing. Return */
0260     if (!vp->enabled) {
0261         pr_warn("%s: Trying to disable VP for vdd_%s when it is already disabled\n",
0262             __func__, voltdm->name);
0263         return;
0264     }
0265 
0266     /* Disable VP */
0267     vpconfig = voltdm->read(vp->vpconfig);
0268     vpconfig &= ~vp->common->vpconfig_vpenable;
0269     voltdm->write(vpconfig, vp->vpconfig);
0270 
0271     /*
0272      * Wait for VP idle Typical latency is <2us. Maximum latency is ~100us
0273      */
0274     omap_test_timeout((voltdm->read(vp->vstatus)),
0275               VP_IDLE_TIMEOUT, timeout);
0276 
0277     if (timeout >= VP_IDLE_TIMEOUT)
0278         pr_warn("%s: vdd_%s idle timedout\n", __func__, voltdm->name);
0279 
0280     vp->enabled = false;
0281 
0282     return;
0283 }