// SPDX-License-Identifier: GPL-2.0-only /* * clk-dfll.c - Tegra DFLL clock source common code * * Copyright (C) 2012-2019 NVIDIA Corporation. All rights reserved. * * Aleksandr Frid <afrid@nvidia.com> * Paul Walmsley <pwalmsley@nvidia.com> * * This library is for the DVCO and DFLL IP blocks on the Tegra124 * SoC. These IP blocks together are also known at NVIDIA as * "CL-DVFS". To try to avoid confusion, this code refers to them * collectively as the "DFLL." * * The DFLL is a root clocksource which tolerates some amount of * supply voltage noise. Tegra124 uses it to clock the fast CPU * complex when the target CPU speed is above a particular rate. The * DFLL can be operated in either open-loop mode or closed-loop mode. * In open-loop mode, the DFLL generates an output clock appropriate * to the supply voltage. In closed-loop mode, when configured with a * target frequency, the DFLL minimizes supply voltage while * delivering an average frequency equal to the target. * * Devices clocked by the DFLL must be able to tolerate frequency * variation. In the case of the CPU, it's important to note that the * CPU cycle time will vary. This has implications for * performance-measurement code and any code that relies on the CPU * cycle time to delay for a certain length of time.
*/
/* MAX_DFLL_VOLTAGES: number of LUT entries in the DFLL IP block */ #define MAX_DFLL_VOLTAGES 33
/* * REF_CLK_CYC_PER_DVCO_SAMPLE: the number of ref_clk cycles that the hardware * integrates the DVCO counter over - used for debug rate monitoring and * droop control
*/ #define REF_CLK_CYC_PER_DVCO_SAMPLE 4
/* * REF_CLOCK_RATE: the DFLL reference clock rate currently supported by this * driver, in Hz
*/ #define REF_CLOCK_RATE 51000000UL
/** * enum dfll_ctrl_mode - DFLL hardware operating mode * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield) * @DFLL_DISABLED: DFLL not generating an output clock * @DFLL_OPEN_LOOP: DVCO running, but DFLL not adjusting voltage * @DFLL_CLOSED_LOOP: DVCO running, and DFLL adjusting voltage to match * the requested rate * * The integer corresponding to the last two states, minus one, is * written to the DFLL hardware to change operating modes.
*/ enum dfll_ctrl_mode {
DFLL_UNINITIALIZED = 0,
DFLL_DISABLED = 1,
DFLL_OPEN_LOOP = 2,
DFLL_CLOSED_LOOP = 3,
};
/** * enum dfll_tune_range - voltage range that the driver believes it's in * @DFLL_TUNE_UNINITIALIZED: DFLL tuning not yet programmed * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode) * * Some DFLL tuning parameters may need to change depending on the * DVCO's voltage; these states represent the ranges that the driver * supports. These are software states; these values are never * written into registers.
*/ enum dfll_tune_range {
DFLL_TUNE_UNINITIALIZED = 0,
DFLL_TUNE_LOW = 1,
};
/** * struct dfll_rate_req - target DFLL rate request data * @rate: target frequency, after the postscaling * @dvco_target_rate: target frequency, after the postscaling * @lut_index: LUT index at which voltage the dvco_target_rate will be reached * @mult_bits: value to program to the MULT bits of the DFLL_FREQ_REQ register * @scale_bits: value to program to the SCALE bits of the DFLL_FREQ_REQ register
*/ struct dfll_rate_req { unsignedlong rate; unsignedlong dvco_target_rate; int lut_index;
u8 mult_bits;
u8 scale_bits;
};
/** * dfll_is_running - is the DFLL currently generating a clock? * @td: DFLL instance * * If the DFLL is currently generating an output clock signal, return * true; otherwise return false.
*/ staticbool dfll_is_running(struct tegra_dfll *td)
{ return td->mode >= DFLL_OPEN_LOOP;
}
/* * Runtime PM suspend/resume callbacks
*/
/** * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL * @dev: DFLL device * * * Enable all clocks needed by the DFLL. Assumes that clk_prepare() * has already been called on all the clocks. * * XXX Should also handle context restore when returning from off.
*/ int tegra_dfll_runtime_resume(struct device *dev)
{ struct tegra_dfll *td = dev_get_drvdata(dev); int ret;
ret = clk_enable(td->ref_clk); if (ret) {
dev_err(dev, "could not enable ref clock: %d\n", ret); return ret;
}
ret = clk_enable(td->soc_clk); if (ret) {
dev_err(dev, "could not enable register clock: %d\n", ret);
clk_disable(td->ref_clk); return ret;
}
ret = clk_enable(td->i2c_clk); if (ret) {
dev_err(dev, "could not enable i2c clock: %d\n", ret);
clk_disable(td->soc_clk);
clk_disable(td->ref_clk); return ret;
}
/** * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL * @dev: DFLL device * * * Disable all clocks needed by the DFLL. Assumes that other code * will later call clk_unprepare().
*/ int tegra_dfll_runtime_suspend(struct device *dev)
{ struct tegra_dfll *td = dev_get_drvdata(dev);
/** * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage * @td: DFLL instance * * Tune the DFLL oscillator parameters and the CPU clock shaper for * the low-voltage range. These settings are valid for any voltage, * but may not be optimal.
*/ staticvoid dfll_tune_low(struct tegra_dfll *td)
{
td->tune_range = DFLL_TUNE_LOW;
if (td->soc->set_clock_trimmers_low)
td->soc->set_clock_trimmers_low();
}
/* * Output clock scaler helpers
*/
/** * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate * @scale_bits: clock scaler value (bits in the DFLL_FREQ_REQ_SCALE field) * @dvco_rate: the DVCO rate * * Apply the same scaling formula that the DFLL hardware uses to scale * the DVCO rate.
*/ staticunsignedlong dfll_scale_dvco_rate(int scale_bits, unsignedlong dvco_rate)
{ return (u64)dvco_rate * (scale_bits + 1) / DFLL_FREQ_REQ_SCALE_MAX;
}
/* * DFLL mode switching
*/
/** * dfll_set_mode - change the DFLL control mode * @td: DFLL instance * @mode: DFLL control mode (see enum dfll_ctrl_mode) * * Change the DFLL's operating mode between disabled, open-loop mode, * and closed-loop mode, or vice versa.
*/ staticvoid dfll_set_mode(struct tegra_dfll *td, enum dfll_ctrl_mode mode)
{
td->mode = mode;
dfll_writel(td, mode - 1, DFLL_CTRL);
dfll_wmb(td);
}
/** * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests * @td: DFLL instance * @enable: whether to enable or disable the I2C voltage requests * * Set the master enable control for I2C control value updates. If disabled, * then I2C control messages are inhibited, regardless of the DFLL mode.
*/ staticint dfll_i2c_set_output_enabled(struct tegra_dfll *td, bool enable)
{
u32 val;
val = dfll_i2c_readl(td, DFLL_OUTPUT_CFG);
if (enable)
val |= DFLL_OUTPUT_CFG_I2C_ENABLE; else
val &= ~DFLL_OUTPUT_CFG_I2C_ENABLE;
/** * dfll_pwm_set_output_enabled - enable/disable PWM voltage requests * @td: DFLL instance * @enable: whether to enable or disable the PWM voltage requests * * Set the master enable control for PWM control value updates. If disabled, * then the PWM signal is not driven. Also configure the PWM output pad * to the appropriate state.
*/ staticint dfll_pwm_set_output_enabled(struct tegra_dfll *td, bool enable)
{ int ret;
u32 val, div;
if (enable) {
ret = pinctrl_select_state(td->pwm_pin, td->pwm_enable_state); if (ret < 0) {
dev_err(td->dev, "setting enable state failed\n"); return -EINVAL;
}
val = dfll_readl(td, DFLL_OUTPUT_CFG);
val &= ~DFLL_OUTPUT_CFG_PWM_DIV_MASK;
div = DIV_ROUND_UP(td->ref_rate, td->pwm_rate);
val |= (div << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
& DFLL_OUTPUT_CFG_PWM_DIV_MASK;
dfll_writel(td, val, DFLL_OUTPUT_CFG);
dfll_wmb(td);
val |= DFLL_OUTPUT_CFG_PWM_ENABLE;
dfll_writel(td, val, DFLL_OUTPUT_CFG);
dfll_wmb(td);
} else {
ret = pinctrl_select_state(td->pwm_pin, td->pwm_disable_state); if (ret < 0)
dev_warn(td->dev, "setting disable state failed\n");
val = dfll_readl(td, DFLL_OUTPUT_CFG);
val &= ~DFLL_OUTPUT_CFG_PWM_ENABLE;
dfll_writel(td, val, DFLL_OUTPUT_CFG);
dfll_wmb(td);
}
return 0;
}
/** * dfll_set_force_output_value - set fixed value for force output * @td: DFLL instance * @out_val: value to force output * * Set the fixed value for force output, DFLL will output this value when * force output is enabled.
*/ static u32 dfll_set_force_output_value(struct tegra_dfll *td, u8 out_val)
{
u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
/** * dfll_set_force_output_enabled - enable/disable force output * @td: DFLL instance * @enable: whether to enable or disable the force output * * Set the enable control for fouce output with fixed value.
*/ staticvoid dfll_set_force_output_enabled(struct tegra_dfll *td, bool enable)
{
u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
if (enable)
val |= DFLL_OUTPUT_FORCE_ENABLE; else
val &= ~DFLL_OUTPUT_FORCE_ENABLE;
/** * dfll_force_output - force output a fixed value * @td: DFLL instance * @out_sel: value to force output * * Set the fixed value for force output, DFLL will output this value.
*/ staticint dfll_force_output(struct tegra_dfll *td, unsignedint out_sel)
{
u32 val;
if (out_sel > OUT_MASK) return -EINVAL;
val = dfll_set_force_output_value(td, out_sel); if ((td->mode < DFLL_CLOSED_LOOP) &&
!(val & DFLL_OUTPUT_FORCE_ENABLE)) {
dfll_set_force_output_enabled(td, true);
}
return 0;
}
/** * dfll_load_i2c_lut - load the voltage lookup table * @td: struct tegra_dfll * * * Load the voltage-to-PMIC register value lookup table into the DFLL * IP block memory. Look-up tables can be loaded at any time.
*/ staticvoid dfll_load_i2c_lut(struct tegra_dfll *td)
{ int i, lut_index;
u32 val;
for (i = 0; i < MAX_DFLL_VOLTAGES; i++) { if (i < td->lut_min)
lut_index = td->lut_min; elseif (i > td->lut_max)
lut_index = td->lut_max; else
lut_index = i;
val = regulator_list_hardware_vsel(td->vdd_reg,
td->lut[lut_index]);
__raw_writel(val, td->lut_base + i * 4);
}
dfll_i2c_wmb(td);
}
/** * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface * @td: DFLL instance * * During DFLL driver initialization, program the DFLL-I2C interface * with the PMU slave address, vdd register offset, and transfer mode. * This data is used by the DFLL to automatically construct I2C * voltage-set commands, which are then passed to the DFLL's internal * I2C controller.
*/ staticvoid dfll_init_i2c_if(struct tegra_dfll *td)
{
u32 val;
if (td->i2c_slave_addr > 0x7f) {
val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT;
val |= DFLL_I2C_CFG_SLAVE_ADDR_10;
} else {
val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT;
}
val |= DFLL_I2C_CFG_SIZE_MASK;
val |= DFLL_I2C_CFG_ARB_ENABLE;
dfll_i2c_writel(td, val, DFLL_I2C_CFG);
val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8);
BUG_ON(!val || (val > DFLL_I2C_CLK_DIVISOR_MASK));
val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT;
/* default hs divisor just in case */
val |= 1 << DFLL_I2C_CLK_DIVISOR_HS_SHIFT;
__raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR);
dfll_i2c_wmb(td);
}
/** * dfll_init_out_if - prepare DFLL-to-PMIC interface * @td: DFLL instance * * During DFLL driver initialization or resume from context loss, * disable the I2C command output to the PMIC, set safe voltage and * output limits, and disable and clear limit interrupts.
*/ staticvoid dfll_init_out_if(struct tegra_dfll *td)
{
u32 val;
/* * Set/get the DFLL's targeted output clock rate
*/
/** * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate * @td: DFLL instance * @rate: clock rate * * Determines the index of a I2C LUT entry for a voltage that approximately * produces the given DFLL clock rate. This is used when forcing a value * to the integrator during rate changes. Returns -ENOENT if a suitable * LUT index is not found.
*/ staticint find_lut_index_for_rate(struct tegra_dfll *td, unsignedlong rate)
{ struct dev_pm_opp *opp; int i, align_step;
opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); if (IS_ERR(opp)) return PTR_ERR(opp);
for (i = td->lut_bottom; i < td->lut_size; i++) { if ((td->lut_uv[i] / td->soc->alignment.step_uv) >= align_step) return i;
}
return -ENOENT;
}
/** * dfll_calculate_rate_request - calculate DFLL parameters for a given rate * @td: DFLL instance * @req: DFLL-rate-request structure * @rate: the desired DFLL rate * * Populate the DFLL-rate-request record @req fields with the scale_bits * and mult_bits fields, based on the target input rate. Returns 0 upon * success, or -EINVAL if the requested rate in req->rate is too high * or low for the DFLL to generate.
*/ staticint dfll_calculate_rate_request(struct tegra_dfll *td, struct dfll_rate_req *req, unsignedlong rate)
{
u32 val;
/* * If requested rate is below the minimum DVCO rate, active the scaler. * In the future the DVCO minimum voltage should be selected based on * chip temperature and the actual minimum rate should be calibrated * at runtime.
*/
req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1; if (rate < td->dvco_rate_min) { int scale;
/* Convert requested rate into frequency request and scale settings */
val = DVCO_RATE_TO_MULT(rate, td->ref_rate); if (val > FREQ_MAX) {
dev_err(td->dev, "%s: Rate %lu is above dfll range\n",
__func__, rate); return -EINVAL;
}
req->mult_bits = val;
req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate);
req->rate = dfll_scale_dvco_rate(req->scale_bits,
req->dvco_target_rate);
req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate); if (req->lut_index < 0) return req->lut_index;
return 0;
}
/** * dfll_set_frequency_request - start the frequency change operation * @td: DFLL instance * @req: rate request structure * * Tell the DFLL to try to change its output frequency to the * frequency represented by @req. DFLL must be in closed-loop mode.
*/ staticvoid dfll_set_frequency_request(struct tegra_dfll *td, struct dfll_rate_req *req)
{
u32 val = 0; int force_val; int coef = 128; /* FIXME: td->cg_scale? */;
/** * dfll_request_rate - set the next rate for the DFLL to tune to * @td: DFLL instance * @rate: clock rate to target * * Convert the requested clock rate @rate into the DFLL control logic * settings. In closed-loop mode, update new settings immediately to * adjust DFLL output rate accordingly. Otherwise, just save them * until the next switch to closed loop. Returns 0 upon success, * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL * if @rate is outside the DFLL's tunable range.
*/ staticint dfll_request_rate(struct tegra_dfll *td, unsignedlong rate)
{ int ret; struct dfll_rate_req req;
if (td->mode == DFLL_UNINITIALIZED) {
dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n",
__func__, mode_name[td->mode]); return -EPERM;
}
ret = dfll_calculate_rate_request(td, &req, rate); if (ret) return ret;
/** * dfll_disable - switch from open-loop mode to disabled mode * @td: DFLL instance * * Switch from OPEN_LOOP state to DISABLED state. Returns 0 upon success * or -EPERM if the DFLL is not currently in open-loop mode.
*/ staticint dfll_disable(struct tegra_dfll *td)
{ if (td->mode != DFLL_OPEN_LOOP) {
dev_err(td->dev, "cannot disable DFLL in %s mode\n",
mode_name[td->mode]); return -EINVAL;
}
/** * dfll_enable - switch a disabled DFLL to open-loop mode * @td: DFLL instance * * Switch from DISABLED state to OPEN_LOOP state. Returns 0 upon success * or -EPERM if the DFLL is not currently disabled.
*/ staticint dfll_enable(struct tegra_dfll *td)
{ if (td->mode != DFLL_DISABLED) {
dev_err(td->dev, "cannot enable DFLL in %s mode\n",
mode_name[td->mode]); return -EPERM;
}
/** * dfll_set_open_loop_config - prepare to switch to open-loop mode * @td: DFLL instance * * Prepare to switch the DFLL to open-loop mode. This switches the * DFLL to the low-voltage tuning range, ensures that I2C output * forcing is disabled, and disables the output clock rate scaler. * The DFLL's low-voltage tuning range parameters must be * characterized to keep the downstream device stable at any DVCO * input voltage. No return value.
*/ staticvoid dfll_set_open_loop_config(struct tegra_dfll *td)
{
u32 val;
/* always tune low (safe) in open loop */ if (td->tune_range != DFLL_TUNE_LOW)
dfll_tune_low(td);
val = dfll_readl(td, DFLL_FREQ_REQ);
val |= DFLL_FREQ_REQ_SCALE_MASK;
val &= ~DFLL_FREQ_REQ_FORCE_ENABLE;
dfll_writel(td, val, DFLL_FREQ_REQ);
dfll_wmb(td);
}
/** * dfll_lock - switch from open-loop to closed-loop mode * @td: DFLL instance * * Switch from OPEN_LOOP state to CLOSED_LOOP state. Returns 0 upon success, * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the * DFLL is not currently in open-loop mode.
*/ staticint dfll_lock(struct tegra_dfll *td)
{ struct dfll_rate_req *req = &td->last_req;
switch (td->mode) { case DFLL_CLOSED_LOOP: return 0;
case DFLL_OPEN_LOOP: if (req->rate == 0) {
dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n",
__func__); return -EINVAL;
}
if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
dfll_pwm_set_output_enabled(td, true); else
dfll_i2c_set_output_enabled(td, true);
/** * dfll_unlock - switch from closed-loop to open-loop mode * @td: DFLL instance * * Switch from CLOSED_LOOP state to OPEN_LOOP state. Returns 0 upon success, * or -EPERM if the DFLL is not currently in open-loop mode.
*/ staticint dfll_unlock(struct tegra_dfll *td)
{ switch (td->mode) { case DFLL_CLOSED_LOOP:
dfll_set_open_loop_config(td);
dfll_set_mode(td, DFLL_OPEN_LOOP); if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
dfll_pwm_set_output_enabled(td, false); else
dfll_i2c_set_output_enabled(td, false); return 0;
/* * Clock framework integration * * When the DFLL is being controlled by the CCF, always enter closed loop * mode when the clk is enabled. This requires that a DFLL rate request * has been set beforehand, which implies that a clk_set_rate() call is * always required before a clk_enable().
*/
/* Must use determine_rate since it allows for rates exceeding 2^31-1 */ staticint dfll_clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *clk_req)
{ struct tegra_dfll *td = clk_hw_to_dfll(hw); struct dfll_rate_req req; int ret;
ret = dfll_calculate_rate_request(td, &req, clk_req->rate); if (ret) return ret;
/* * Don't set the rounded rate, since it doesn't really matter as * the output rate will be voltage controlled anyway, and cpufreq * freaks out if any rounding happens.
*/
/** * dfll_register_clk - register the DFLL output clock with the clock framework * @td: DFLL instance * * Register the DFLL's output clock with the Linux clock framework and register * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL * or -ENOMEM upon failure.
*/ staticint dfll_register_clk(struct tegra_dfll *td)
{ int ret;
ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get,
td->dfll_clk); if (ret) {
dev_err(td->dev, "of_clk_add_provider() failed\n");
clk_unregister(td->dfll_clk); return ret;
}
return 0;
}
/** * dfll_unregister_clk - unregister the DFLL output clock * @td: DFLL instance * * Unregister the DFLL's output clock from the Linux clock framework * and from clkdev. No return value.
*/ staticvoid dfll_unregister_clk(struct tegra_dfll *td)
{
of_clk_del_provider(td->dev->of_node);
clk_unregister(td->dfll_clk);
td->dfll_clk = NULL;
}
/* * Debugfs interface
*/
#ifdef CONFIG_DEBUG_FS /* * Monitor control
*/
/** * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq * @monitor_data: value read from the DFLL_MONITOR_DATA_VAL bitfield * @ref_rate: DFLL reference clock rate * * Convert @monitor_data from DFLL_MONITOR_DATA_VAL units into cycles * per second. Returns the converted value.
*/ static u64 dfll_calc_monitored_rate(u32 monitor_data, unsignedlong ref_rate)
{ return monitor_data * (ref_rate / REF_CLK_CYC_PER_DVCO_SAMPLE);
}
/** * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor * @td: DFLL instance * * If the DFLL is enabled, return the last rate reported by the DFLL's * internal monitoring hardware. This works in both open-loop and * closed-loop mode, and takes the output scaler setting into account. * Assumes that the monitor was programmed to monitor frequency before * the sample period started. If the driver believes that the DFLL is * currently uninitialized or disabled, it will return 0, since * otherwise the DFLL monitor data register will return the last * measured rate from when the DFLL was active.
*/ static u64 dfll_read_monitor_rate(struct tegra_dfll *td)
{
u32 v, s;
u64 pre_scaler_rate, post_scaler_rate;
if (!dfll_is_running(td)) return 0;
v = dfll_readl(td, DFLL_MONITOR_DATA);
v = (v & DFLL_MONITOR_DATA_VAL_MASK) >> DFLL_MONITOR_DATA_VAL_SHIFT;
pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate);
s = dfll_readl(td, DFLL_FREQ_REQ);
s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT;
post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate);
/** * dfll_set_default_params - program non-output related DFLL parameters * @td: DFLL instance * * During DFLL driver initialization or resume from context loss, * program parameters for the closed loop integrator, DVCO tuning, * voltage droop control and monitor control.
*/ staticvoid dfll_set_default_params(struct tegra_dfll *td)
{
u32 val;
/** * dfll_init_clks - clk_get() the DFLL source clocks * @td: DFLL instance * * Call clk_get() on the DFLL source clocks and save the pointers for later * use. Returns 0 upon success or error (see devm_clk_get) if one or more * of the clocks couldn't be looked up.
*/ staticint dfll_init_clks(struct tegra_dfll *td)
{
td->ref_clk = devm_clk_get(td->dev, "ref"); if (IS_ERR(td->ref_clk)) {
dev_err(td->dev, "missing ref clock\n"); return PTR_ERR(td->ref_clk);
}
/** * dfll_init - Prepare the DFLL IP block for use * @td: DFLL instance * * Do everything necessary to prepare the DFLL IP block for use. The * DFLL will be left in DISABLED state. Called by dfll_probe(). * Returns 0 upon success, or passes along the error from whatever * function returned it.
*/ staticint dfll_init(struct tegra_dfll *td)
{ int ret;
/** * tegra_dfll_suspend - check DFLL is disabled * @dev: DFLL instance * * DFLL clock should be disabled by the CPUFreq driver. So, make * sure it is disabled and disable all clocks needed by the DFLL.
*/ int tegra_dfll_suspend(struct device *dev)
{ struct tegra_dfll *td = dev_get_drvdata(dev);
if (dfll_is_running(td)) {
dev_err(td->dev, "DFLL still enabled while suspending\n"); return -EBUSY;
}
/** * tegra_dfll_resume - reinitialize DFLL on resume * @dev: DFLL instance * * DFLL is disabled and reset during suspend and resume. * So, reinitialize the DFLL IP block back for use. * DFLL clock is enabled later in closed loop mode by CPUFreq * driver before switching its clock source to DFLL output.
*/ int tegra_dfll_resume(struct device *dev)
{ struct tegra_dfll *td = dev_get_drvdata(dev);
if (td->soc->init_clock_trimmers)
td->soc->init_clock_trimmers();
dfll_set_open_loop_config(td);
dfll_init_out_if(td);
pm_runtime_put_sync(td->dev);
return 0;
}
EXPORT_SYMBOL(tegra_dfll_resume);
/* * DT data fetch
*/
/* * Find a PMIC voltage register-to-voltage mapping for the given voltage. * An exact voltage match is required.
*/ staticint find_vdd_map_entry_exact(struct tegra_dfll *td, int uV)
{ int i, n_voltages, reg_uV,reg_volt_id, align_step;
if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM)) return -EINVAL;
align_step = uV / td->soc->alignment.step_uv;
n_voltages = regulator_count_voltages(td->vdd_reg); for (i = 0; i < n_voltages; i++) {
reg_uV = regulator_list_voltage(td->vdd_reg, i); if (reg_uV < 0) break;
dev_err(td->dev, "no voltage map entry for %d uV\n", uV); return -EINVAL;
}
/* * Find a PMIC voltage register-to-voltage mapping for the given voltage, * rounding up to the closest supported voltage.
* */ staticint find_vdd_map_entry_min(struct tegra_dfll *td, int uV)
{ int i, n_voltages, reg_uV, reg_volt_id, align_step;
if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM)) return -EINVAL;
align_step = uV / td->soc->alignment.step_uv;
n_voltages = regulator_count_voltages(td->vdd_reg); for (i = 0; i < n_voltages; i++) {
reg_uV = regulator_list_voltage(td->vdd_reg, i); if (reg_uV < 0) break;
dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV); return -EINVAL;
}
/* * dfll_build_pwm_lut - build the PWM regulator lookup table * @td: DFLL instance * @v_max: Vmax from OPP table * * Look-up table in h/w is ignored when PWM is used as DFLL interface to PMIC. * In this case closed loop output is controlling duty cycle directly. The s/w * look-up that maps PWM duty cycle to voltage is still built by this function.
*/ staticint dfll_build_pwm_lut(struct tegra_dfll *td, unsignedlong v_max)
{ int i; unsignedlong rate, reg_volt;
u8 lut_bottom = MAX_DFLL_VOLTAGES; int v_min = td->soc->cvb->min_millivolts * 1000;
for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
reg_volt = td->lut_uv[i];
/* since opp voltage is exact mv */
reg_volt = (reg_volt / 1000) * 1000; if (reg_volt > v_max) break;
/* determine voltage boundaries */
td->lut_size = i; if ((lut_bottom == MAX_DFLL_VOLTAGES) ||
(lut_bottom + 1 >= td->lut_size)) {
dev_err(td->dev, "no voltage above DFLL minimum %d mV\n",
td->soc->cvb->min_millivolts); return -EINVAL;
}
td->lut_bottom = lut_bottom;
/* determine rate boundaries */
rate = get_dvco_rate_below(td, td->lut_bottom); if (!rate) {
dev_err(td->dev, "no opp below DFLL minimum voltage %d mV\n",
td->soc->cvb->min_millivolts); return -EINVAL;
}
td->dvco_rate_min = rate;
return 0;
}
/** * dfll_build_i2c_lut - build the I2C voltage register lookup table * @td: DFLL instance * @v_max: Vmax from OPP table * * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with * PMIC voltage register values that span the entire DFLL operating range. * This function builds the look-up table based on the OPP table provided by * the soc-specific platform driver (td->soc->opp_dev) and the PMIC * register-to-voltage mapping queried from the regulator framework. * * On success, fills in td->lut and returns 0, or -err on failure.
*/ staticint dfll_build_i2c_lut(struct tegra_dfll *td, unsignedlong v_max)
{ unsignedlong rate, v, v_opp; int ret = -EINVAL; int j, selector, lut;
opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); if (IS_ERR(opp)) break;
v_opp = dev_pm_opp_get_voltage(opp);
if (v_opp <= td->soc->cvb->min_millivolts * 1000)
td->dvco_rate_min = dev_pm_opp_get_freq(opp);
dev_pm_opp_put(opp);
for (;;) {
v += max(1UL, (v_max - v) / (MAX_DFLL_VOLTAGES - j)); if (v >= v_opp) break;
selector = find_vdd_map_entry_min(td, v); if (selector < 0) goto out; if (selector != td->lut[j - 1])
td->lut[j++] = selector;
}
v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp;
selector = find_vdd_map_entry_exact(td, v); if (selector < 0) goto out; if (selector != td->lut[j - 1])
td->lut[j++] = selector;
if (v >= v_max) break;
}
td->lut_size = j;
if (!td->dvco_rate_min)
dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n",
td->soc->cvb->min_millivolts); else {
ret = 0; for (j = 0; j < td->lut_size; j++)
td->lut_uv[j] =
regulator_list_voltage(td->vdd_reg,
td->lut[j]);
}
/** * read_dt_param - helper function for reading required parameters from the DT * @td: DFLL instance * @param: DT property name * @dest: output pointer for the value read * * Read a required numeric parameter from the DFLL device node, or complain * if the property doesn't exist. Returns a boolean indicating success for * easy chaining of multiple calls to this function.
*/ staticbool read_dt_param(struct tegra_dfll *td, constchar *param, u32 *dest)
{ int err = of_property_read_u32(td->dev->of_node, param, dest);
if (err < 0) {
dev_err(td->dev, "failed to read DT parameter %s: %d\n",
param, err); returnfalse;
}
returntrue;
}
/** * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem * @td: DFLL instance * * Read all the parameters required for operation in I2C mode. The parameters * can originate from the device tree or the regulator subsystem. * Returns 0 on success or -err on failure.
*/ staticint dfll_fetch_i2c_params(struct tegra_dfll *td)
{ struct regmap *regmap; struct device *i2c_dev; struct i2c_client *i2c_client; int vsel_reg, vsel_mask; int ret;
if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate)) return -EINVAL;
ret = regulator_get_hardware_vsel_register(td->vdd_reg,
&vsel_reg,
&vsel_mask); if (ret < 0) {
dev_err(td->dev, "regulator unsuitable for DFLL I2C operation\n"); return -EINVAL;
}
td->i2c_reg = vsel_reg;
return 0;
}
staticint dfll_fetch_pwm_params(struct tegra_dfll *td)
{ int ret, i;
u32 pwm_period;
if (!td->soc->alignment.step_uv || !td->soc->alignment.offset_uv) {
dev_err(td->dev, "Missing step or alignment info for PWM regulator"); return -EINVAL;
} for (i = 0; i < MAX_DFLL_VOLTAGES; i++)
td->lut_uv[i] = td->soc->alignment.offset_uv +
i * td->soc->alignment.step_uv;
ret = read_dt_param(td, "nvidia,pwm-tristate-microvolts",
&td->reg_init_uV); if (!ret) {
dev_err(td->dev, "couldn't get initialized voltage\n"); return -EINVAL;
}
ret = read_dt_param(td, "nvidia,pwm-period-nanoseconds", &pwm_period); if (!ret) {
dev_err(td->dev, "couldn't get PWM period\n"); return -EINVAL;
}
td->pwm_rate = (NSEC_PER_SEC / pwm_period) * (MAX_DFLL_VOLTAGES - 1);
/** * dfll_fetch_common_params - read DFLL parameters from the device tree * @td: DFLL instance * * Read all the DT parameters that are common to both I2C and PWM operation. * Returns 0 on success or -EINVAL on any failure.
*/ staticint dfll_fetch_common_params(struct tegra_dfll *td)
{ bool ok = true;
ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl);
ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate);
ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode);
ok &= read_dt_param(td, "nvidia,cf", &td->cf);
ok &= read_dt_param(td, "nvidia,ci", &td->ci);
ok &= read_dt_param(td, "nvidia,cg", &td->cg);
td->cg_scale = of_property_read_bool(td->dev->of_node, "nvidia,cg-scale");
if (of_property_read_string(td->dev->of_node, "clock-output-names",
&td->output_clock_name)) {
dev_err(td->dev, "missing clock-output-names property\n");
ok = false;
}
return ok ? 0 : -EINVAL;
}
/* * API exported to per-SoC platform drivers
*/
/** * tegra_dfll_register - probe a Tegra DFLL device * @pdev: DFLL platform_device * * @soc: Per-SoC integration and characterization data for this DFLL instance * * Probe and initialize a DFLL device instance. Intended to be called * by a SoC-specific shim driver that passes in per-SoC integration * and configuration data via @soc. Returns 0 on success or -err on failure.
*/ int tegra_dfll_register(struct platform_device *pdev, struct tegra_dfll_soc_data *soc)
{ struct resource *mem; struct tegra_dfll *td; int ret;
if (!soc) {
dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n"); return -EINVAL;
}
ret = dfll_init_clks(td); if (ret) {
dev_err(&pdev->dev, "DFLL clock init error\n"); return ret;
}
/* Enable the clocks and set the device up */
ret = dfll_init(td); if (ret) return ret;
ret = dfll_register_clk(td); if (ret) {
dev_err(&pdev->dev, "DFLL clk registration failed\n"); return ret;
}
dfll_debug_init(td);
return 0;
}
EXPORT_SYMBOL(tegra_dfll_register);
/** * tegra_dfll_unregister - release all of the DFLL driver resources for a device * @pdev: DFLL platform_device * * * Unbind this driver from the DFLL hardware device represented by * @pdev. The DFLL must be disabled for this to succeed. Returns a * soc pointer upon success or -EBUSY if the DFLL is still active.
*/ struct tegra_dfll_soc_data *tegra_dfll_unregister(struct platform_device *pdev)
{ struct tegra_dfll *td = platform_get_drvdata(pdev);
/* * Note that exiting early here doesn't prevent unbinding the driver. * Exiting early here only leaks some resources.
*/ if (td->mode != DFLL_DISABLED) {
dev_err(&pdev->dev, "must disable DFLL before removing driver\n"); return ERR_PTR(-EBUSY);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.