Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * clk-dfll.c - Tegra DFLL clock source common code
0004  *
0005  * Copyright (C) 2012-2019 NVIDIA Corporation. All rights reserved.
0006  *
0007  * Aleksandr Frid <afrid@nvidia.com>
0008  * Paul Walmsley <pwalmsley@nvidia.com>
0009  *
0010  * This library is for the DVCO and DFLL IP blocks on the Tegra124
0011  * SoC. These IP blocks together are also known at NVIDIA as
0012  * "CL-DVFS". To try to avoid confusion, this code refers to them
0013  * collectively as the "DFLL."
0014  *
0015  * The DFLL is a root clocksource which tolerates some amount of
0016  * supply voltage noise. Tegra124 uses it to clock the fast CPU
0017  * complex when the target CPU speed is above a particular rate. The
0018  * DFLL can be operated in either open-loop mode or closed-loop mode.
0019  * In open-loop mode, the DFLL generates an output clock appropriate
0020  * to the supply voltage. In closed-loop mode, when configured with a
0021  * target frequency, the DFLL minimizes supply voltage while
0022  * delivering an average frequency equal to the target.
0023  *
0024  * Devices clocked by the DFLL must be able to tolerate frequency
0025  * variation. In the case of the CPU, it's important to note that the
0026  * CPU cycle time will vary. This has implications for
0027  * performance-measurement code and any code that relies on the CPU
0028  * cycle time to delay for a certain length of time.
0029  */
0030 
0031 #include <linux/clk.h>
0032 #include <linux/clk-provider.h>
0033 #include <linux/debugfs.h>
0034 #include <linux/device.h>
0035 #include <linux/err.h>
0036 #include <linux/i2c.h>
0037 #include <linux/io.h>
0038 #include <linux/kernel.h>
0039 #include <linux/module.h>
0040 #include <linux/of.h>
0041 #include <linux/pinctrl/consumer.h>
0042 #include <linux/pm_opp.h>
0043 #include <linux/pm_runtime.h>
0044 #include <linux/regmap.h>
0045 #include <linux/regulator/consumer.h>
0046 #include <linux/reset.h>
0047 #include <linux/seq_file.h>
0048 
0049 #include "clk-dfll.h"
0050 #include "cvb.h"
0051 
0052 /*
0053  * DFLL control registers - access via dfll_{readl,writel}
0054  */
0055 
0056 /* DFLL_CTRL: DFLL control register */
0057 #define DFLL_CTRL           0x00
0058 #define DFLL_CTRL_MODE_MASK     0x03
0059 
0060 /* DFLL_CONFIG: DFLL sample rate control */
0061 #define DFLL_CONFIG         0x04
0062 #define DFLL_CONFIG_DIV_MASK        0xff
0063 #define DFLL_CONFIG_DIV_PRESCALE    32
0064 
0065 /* DFLL_PARAMS: tuning coefficients for closed loop integrator */
0066 #define DFLL_PARAMS         0x08
0067 #define DFLL_PARAMS_CG_SCALE        (0x1 << 24)
0068 #define DFLL_PARAMS_FORCE_MODE_SHIFT    22
0069 #define DFLL_PARAMS_FORCE_MODE_MASK (0x3 << DFLL_PARAMS_FORCE_MODE_SHIFT)
0070 #define DFLL_PARAMS_CF_PARAM_SHIFT  16
0071 #define DFLL_PARAMS_CF_PARAM_MASK   (0x3f << DFLL_PARAMS_CF_PARAM_SHIFT)
0072 #define DFLL_PARAMS_CI_PARAM_SHIFT  8
0073 #define DFLL_PARAMS_CI_PARAM_MASK   (0x7 << DFLL_PARAMS_CI_PARAM_SHIFT)
0074 #define DFLL_PARAMS_CG_PARAM_SHIFT  0
0075 #define DFLL_PARAMS_CG_PARAM_MASK   (0xff << DFLL_PARAMS_CG_PARAM_SHIFT)
0076 
0077 /* DFLL_TUNE0: delay line configuration register 0 */
0078 #define DFLL_TUNE0          0x0c
0079 
0080 /* DFLL_TUNE1: delay line configuration register 1 */
0081 #define DFLL_TUNE1          0x10
0082 
0083 /* DFLL_FREQ_REQ: target DFLL frequency control */
0084 #define DFLL_FREQ_REQ           0x14
0085 #define DFLL_FREQ_REQ_FORCE_ENABLE  (0x1 << 28)
0086 #define DFLL_FREQ_REQ_FORCE_SHIFT   16
0087 #define DFLL_FREQ_REQ_FORCE_MASK    (0xfff << DFLL_FREQ_REQ_FORCE_SHIFT)
0088 #define FORCE_MAX           2047
0089 #define FORCE_MIN           -2048
0090 #define DFLL_FREQ_REQ_SCALE_SHIFT   8
0091 #define DFLL_FREQ_REQ_SCALE_MASK    (0xff << DFLL_FREQ_REQ_SCALE_SHIFT)
0092 #define DFLL_FREQ_REQ_SCALE_MAX     256
0093 #define DFLL_FREQ_REQ_FREQ_VALID    (0x1 << 7)
0094 #define DFLL_FREQ_REQ_MULT_SHIFT    0
0095 #define DFLL_FREQ_REG_MULT_MASK     (0x7f << DFLL_FREQ_REQ_MULT_SHIFT)
0096 #define FREQ_MAX            127
0097 
0098 /* DFLL_DROOP_CTRL: droop prevention control */
0099 #define DFLL_DROOP_CTRL         0x1c
0100 
0101 /* DFLL_OUTPUT_CFG: closed loop mode control registers */
0102 /* NOTE: access via dfll_i2c_{readl,writel} */
0103 #define DFLL_OUTPUT_CFG         0x20
0104 #define DFLL_OUTPUT_CFG_I2C_ENABLE  (0x1 << 30)
0105 #define OUT_MASK            0x3f
0106 #define DFLL_OUTPUT_CFG_SAFE_SHIFT  24
0107 #define DFLL_OUTPUT_CFG_SAFE_MASK   \
0108         (OUT_MASK << DFLL_OUTPUT_CFG_SAFE_SHIFT)
0109 #define DFLL_OUTPUT_CFG_MAX_SHIFT   16
0110 #define DFLL_OUTPUT_CFG_MAX_MASK    \
0111         (OUT_MASK << DFLL_OUTPUT_CFG_MAX_SHIFT)
0112 #define DFLL_OUTPUT_CFG_MIN_SHIFT   8
0113 #define DFLL_OUTPUT_CFG_MIN_MASK    \
0114         (OUT_MASK << DFLL_OUTPUT_CFG_MIN_SHIFT)
0115 #define DFLL_OUTPUT_CFG_PWM_DELTA   (0x1 << 7)
0116 #define DFLL_OUTPUT_CFG_PWM_ENABLE  (0x1 << 6)
0117 #define DFLL_OUTPUT_CFG_PWM_DIV_SHIFT   0
0118 #define DFLL_OUTPUT_CFG_PWM_DIV_MASK    \
0119         (OUT_MASK << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
0120 
0121 /* DFLL_OUTPUT_FORCE: closed loop mode voltage forcing control */
0122 #define DFLL_OUTPUT_FORCE       0x24
0123 #define DFLL_OUTPUT_FORCE_ENABLE    (0x1 << 6)
0124 #define DFLL_OUTPUT_FORCE_VALUE_SHIFT   0
0125 #define DFLL_OUTPUT_FORCE_VALUE_MASK    \
0126         (OUT_MASK << DFLL_OUTPUT_FORCE_VALUE_SHIFT)
0127 
0128 /* DFLL_MONITOR_CTRL: internal monitor data source control */
0129 #define DFLL_MONITOR_CTRL       0x28
0130 #define DFLL_MONITOR_CTRL_FREQ      6
0131 
0132 /* DFLL_MONITOR_DATA: internal monitor data output */
0133 #define DFLL_MONITOR_DATA       0x2c
0134 #define DFLL_MONITOR_DATA_NEW_MASK  (0x1 << 16)
0135 #define DFLL_MONITOR_DATA_VAL_SHIFT 0
0136 #define DFLL_MONITOR_DATA_VAL_MASK  (0xFFFF << DFLL_MONITOR_DATA_VAL_SHIFT)
0137 
0138 /*
0139  * I2C output control registers - access via dfll_i2c_{readl,writel}
0140  */
0141 
0142 /* DFLL_I2C_CFG: I2C controller configuration register */
0143 #define DFLL_I2C_CFG            0x40
0144 #define DFLL_I2C_CFG_ARB_ENABLE     (0x1 << 20)
0145 #define DFLL_I2C_CFG_HS_CODE_SHIFT  16
0146 #define DFLL_I2C_CFG_HS_CODE_MASK   (0x7 << DFLL_I2C_CFG_HS_CODE_SHIFT)
0147 #define DFLL_I2C_CFG_PACKET_ENABLE  (0x1 << 15)
0148 #define DFLL_I2C_CFG_SIZE_SHIFT     12
0149 #define DFLL_I2C_CFG_SIZE_MASK      (0x7 << DFLL_I2C_CFG_SIZE_SHIFT)
0150 #define DFLL_I2C_CFG_SLAVE_ADDR_10  (0x1 << 10)
0151 #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT  1
0152 #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT 0
0153 
0154 /* DFLL_I2C_VDD_REG_ADDR: PMIC I2C address for closed loop mode */
0155 #define DFLL_I2C_VDD_REG_ADDR       0x44
0156 
0157 /* DFLL_I2C_STS: I2C controller status */
0158 #define DFLL_I2C_STS            0x48
0159 #define DFLL_I2C_STS_I2C_LAST_SHIFT 1
0160 #define DFLL_I2C_STS_I2C_REQ_PENDING    0x1
0161 
0162 /* DFLL_INTR_STS: DFLL interrupt status register */
0163 #define DFLL_INTR_STS           0x5c
0164 
0165 /* DFLL_INTR_EN: DFLL interrupt enable register */
0166 #define DFLL_INTR_EN            0x60
0167 #define DFLL_INTR_MIN_MASK      0x1
0168 #define DFLL_INTR_MAX_MASK      0x2
0169 
0170 /*
0171  * Integrated I2C controller registers - relative to td->i2c_controller_base
0172  */
0173 
0174 /* DFLL_I2C_CLK_DIVISOR: I2C controller clock divisor */
0175 #define DFLL_I2C_CLK_DIVISOR        0x6c
0176 #define DFLL_I2C_CLK_DIVISOR_MASK   0xffff
0177 #define DFLL_I2C_CLK_DIVISOR_FS_SHIFT   16
0178 #define DFLL_I2C_CLK_DIVISOR_HS_SHIFT   0
0179 #define DFLL_I2C_CLK_DIVISOR_PREDIV 8
0180 #define DFLL_I2C_CLK_DIVISOR_HSMODE_PREDIV  12
0181 
0182 /*
0183  * Other constants
0184  */
0185 
0186 /* MAX_DFLL_VOLTAGES: number of LUT entries in the DFLL IP block */
0187 #define MAX_DFLL_VOLTAGES       33
0188 
0189 /*
0190  * REF_CLK_CYC_PER_DVCO_SAMPLE: the number of ref_clk cycles that the hardware
0191  *    integrates the DVCO counter over - used for debug rate monitoring and
0192  *    droop control
0193  */
0194 #define REF_CLK_CYC_PER_DVCO_SAMPLE 4
0195 
0196 /*
0197  * REF_CLOCK_RATE: the DFLL reference clock rate currently supported by this
0198  * driver, in Hz
0199  */
0200 #define REF_CLOCK_RATE          51000000UL
0201 
0202 #define DVCO_RATE_TO_MULT(rate, ref_rate)   ((rate) / ((ref_rate) / 2))
0203 #define MULT_TO_DVCO_RATE(mult, ref_rate)   ((mult) * ((ref_rate) / 2))
0204 
0205 /**
0206  * enum dfll_ctrl_mode - DFLL hardware operating mode
0207  * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield)
0208  * @DFLL_DISABLED: DFLL not generating an output clock
0209  * @DFLL_OPEN_LOOP: DVCO running, but DFLL not adjusting voltage
0210  * @DFLL_CLOSED_LOOP: DVCO running, and DFLL adjusting voltage to match
0211  *            the requested rate
0212  *
0213  * The integer corresponding to the last two states, minus one, is
0214  * written to the DFLL hardware to change operating modes.
0215  */
0216 enum dfll_ctrl_mode {
0217     DFLL_UNINITIALIZED = 0,
0218     DFLL_DISABLED = 1,
0219     DFLL_OPEN_LOOP = 2,
0220     DFLL_CLOSED_LOOP = 3,
0221 };
0222 
0223 /**
0224  * enum dfll_tune_range - voltage range that the driver believes it's in
0225  * @DFLL_TUNE_UNINITIALIZED: DFLL tuning not yet programmed
0226  * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode)
0227  *
0228  * Some DFLL tuning parameters may need to change depending on the
0229  * DVCO's voltage; these states represent the ranges that the driver
0230  * supports. These are software states; these values are never
0231  * written into registers.
0232  */
0233 enum dfll_tune_range {
0234     DFLL_TUNE_UNINITIALIZED = 0,
0235     DFLL_TUNE_LOW = 1,
0236 };
0237 
0238 
0239 enum tegra_dfll_pmu_if {
0240     TEGRA_DFLL_PMU_I2C = 0,
0241     TEGRA_DFLL_PMU_PWM = 1,
0242 };
0243 
0244 /**
0245  * struct dfll_rate_req - target DFLL rate request data
0246  * @rate: target frequency, after the postscaling
0247  * @dvco_target_rate: target frequency, after the postscaling
0248  * @lut_index: LUT index at which voltage the dvco_target_rate will be reached
0249  * @mult_bits: value to program to the MULT bits of the DFLL_FREQ_REQ register
0250  * @scale_bits: value to program to the SCALE bits of the DFLL_FREQ_REQ register
0251  */
0252 struct dfll_rate_req {
0253     unsigned long rate;
0254     unsigned long dvco_target_rate;
0255     int lut_index;
0256     u8 mult_bits;
0257     u8 scale_bits;
0258 };
0259 
0260 struct tegra_dfll {
0261     struct device           *dev;
0262     struct tegra_dfll_soc_data  *soc;
0263 
0264     void __iomem            *base;
0265     void __iomem            *i2c_base;
0266     void __iomem            *i2c_controller_base;
0267     void __iomem            *lut_base;
0268 
0269     struct regulator        *vdd_reg;
0270     struct clk          *soc_clk;
0271     struct clk          *ref_clk;
0272     struct clk          *i2c_clk;
0273     struct clk          *dfll_clk;
0274     struct reset_control        *dfll_rst;
0275     struct reset_control        *dvco_rst;
0276     unsigned long           ref_rate;
0277     unsigned long           i2c_clk_rate;
0278     unsigned long           dvco_rate_min;
0279 
0280     enum dfll_ctrl_mode     mode;
0281     enum dfll_tune_range        tune_range;
0282     struct dentry           *debugfs_dir;
0283     struct clk_hw           dfll_clk_hw;
0284     const char          *output_clock_name;
0285     struct dfll_rate_req        last_req;
0286     unsigned long           last_unrounded_rate;
0287 
0288     /* Parameters from DT */
0289     u32             droop_ctrl;
0290     u32             sample_rate;
0291     u32             force_mode;
0292     u32             cf;
0293     u32             ci;
0294     u32             cg;
0295     bool                cg_scale;
0296 
0297     /* I2C interface parameters */
0298     u32             i2c_fs_rate;
0299     u32             i2c_reg;
0300     u32             i2c_slave_addr;
0301 
0302     /* lut array entries are regulator framework selectors or PWM values*/
0303     unsigned            lut[MAX_DFLL_VOLTAGES];
0304     unsigned long           lut_uv[MAX_DFLL_VOLTAGES];
0305     int             lut_size;
0306     u8              lut_bottom, lut_min, lut_max, lut_safe;
0307 
0308     /* PWM interface */
0309     enum tegra_dfll_pmu_if      pmu_if;
0310     unsigned long           pwm_rate;
0311     struct pinctrl          *pwm_pin;
0312     struct pinctrl_state        *pwm_enable_state;
0313     struct pinctrl_state        *pwm_disable_state;
0314     u32             reg_init_uV;
0315 };
0316 
0317 #define clk_hw_to_dfll(_hw) container_of(_hw, struct tegra_dfll, dfll_clk_hw)
0318 
0319 /* mode_name: map numeric DFLL modes to names for friendly console messages */
0320 static const char * const mode_name[] = {
0321     [DFLL_UNINITIALIZED] = "uninitialized",
0322     [DFLL_DISABLED] = "disabled",
0323     [DFLL_OPEN_LOOP] = "open_loop",
0324     [DFLL_CLOSED_LOOP] = "closed_loop",
0325 };
0326 
0327 /*
0328  * Register accessors
0329  */
0330 
0331 static inline u32 dfll_readl(struct tegra_dfll *td, u32 offs)
0332 {
0333     return __raw_readl(td->base + offs);
0334 }
0335 
0336 static inline void dfll_writel(struct tegra_dfll *td, u32 val, u32 offs)
0337 {
0338     WARN_ON(offs >= DFLL_I2C_CFG);
0339     __raw_writel(val, td->base + offs);
0340 }
0341 
0342 static inline void dfll_wmb(struct tegra_dfll *td)
0343 {
0344     dfll_readl(td, DFLL_CTRL);
0345 }
0346 
0347 /* I2C output control registers - for addresses above DFLL_I2C_CFG */
0348 
0349 static inline u32 dfll_i2c_readl(struct tegra_dfll *td, u32 offs)
0350 {
0351     return __raw_readl(td->i2c_base + offs);
0352 }
0353 
0354 static inline void dfll_i2c_writel(struct tegra_dfll *td, u32 val, u32 offs)
0355 {
0356     __raw_writel(val, td->i2c_base + offs);
0357 }
0358 
0359 static inline void dfll_i2c_wmb(struct tegra_dfll *td)
0360 {
0361     dfll_i2c_readl(td, DFLL_I2C_CFG);
0362 }
0363 
0364 /**
0365  * dfll_is_running - is the DFLL currently generating a clock?
0366  * @td: DFLL instance
0367  *
0368  * If the DFLL is currently generating an output clock signal, return
0369  * true; otherwise return false.
0370  */
0371 static bool dfll_is_running(struct tegra_dfll *td)
0372 {
0373     return td->mode >= DFLL_OPEN_LOOP;
0374 }
0375 
0376 /*
0377  * Runtime PM suspend/resume callbacks
0378  */
0379 
0380 /**
0381  * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL
0382  * @dev: DFLL device *
0383  *
0384  * Enable all clocks needed by the DFLL. Assumes that clk_prepare()
0385  * has already been called on all the clocks.
0386  *
0387  * XXX Should also handle context restore when returning from off.
0388  */
0389 int tegra_dfll_runtime_resume(struct device *dev)
0390 {
0391     struct tegra_dfll *td = dev_get_drvdata(dev);
0392     int ret;
0393 
0394     ret = clk_enable(td->ref_clk);
0395     if (ret) {
0396         dev_err(dev, "could not enable ref clock: %d\n", ret);
0397         return ret;
0398     }
0399 
0400     ret = clk_enable(td->soc_clk);
0401     if (ret) {
0402         dev_err(dev, "could not enable register clock: %d\n", ret);
0403         clk_disable(td->ref_clk);
0404         return ret;
0405     }
0406 
0407     ret = clk_enable(td->i2c_clk);
0408     if (ret) {
0409         dev_err(dev, "could not enable i2c clock: %d\n", ret);
0410         clk_disable(td->soc_clk);
0411         clk_disable(td->ref_clk);
0412         return ret;
0413     }
0414 
0415     return 0;
0416 }
0417 EXPORT_SYMBOL(tegra_dfll_runtime_resume);
0418 
0419 /**
0420  * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL
0421  * @dev: DFLL device *
0422  *
0423  * Disable all clocks needed by the DFLL. Assumes that other code
0424  * will later call clk_unprepare().
0425  */
0426 int tegra_dfll_runtime_suspend(struct device *dev)
0427 {
0428     struct tegra_dfll *td = dev_get_drvdata(dev);
0429 
0430     clk_disable(td->ref_clk);
0431     clk_disable(td->soc_clk);
0432     clk_disable(td->i2c_clk);
0433 
0434     return 0;
0435 }
0436 EXPORT_SYMBOL(tegra_dfll_runtime_suspend);
0437 
0438 /*
0439  * DFLL tuning operations (per-voltage-range tuning settings)
0440  */
0441 
0442 /**
0443  * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage
0444  * @td: DFLL instance
0445  *
0446  * Tune the DFLL oscillator parameters and the CPU clock shaper for
0447  * the low-voltage range. These settings are valid for any voltage,
0448  * but may not be optimal.
0449  */
0450 static void dfll_tune_low(struct tegra_dfll *td)
0451 {
0452     td->tune_range = DFLL_TUNE_LOW;
0453 
0454     dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune0_low, DFLL_TUNE0);
0455     dfll_writel(td, td->soc->cvb->cpu_dfll_data.tune1, DFLL_TUNE1);
0456     dfll_wmb(td);
0457 
0458     if (td->soc->set_clock_trimmers_low)
0459         td->soc->set_clock_trimmers_low();
0460 }
0461 
0462 /*
0463  * Output clock scaler helpers
0464  */
0465 
0466 /**
0467  * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate
0468  * @scale_bits: clock scaler value (bits in the DFLL_FREQ_REQ_SCALE field)
0469  * @dvco_rate: the DVCO rate
0470  *
0471  * Apply the same scaling formula that the DFLL hardware uses to scale
0472  * the DVCO rate.
0473  */
0474 static unsigned long dfll_scale_dvco_rate(int scale_bits,
0475                       unsigned long dvco_rate)
0476 {
0477     return (u64)dvco_rate * (scale_bits + 1) / DFLL_FREQ_REQ_SCALE_MAX;
0478 }
0479 
0480 /*
0481  * DFLL mode switching
0482  */
0483 
0484 /**
0485  * dfll_set_mode - change the DFLL control mode
0486  * @td: DFLL instance
0487  * @mode: DFLL control mode (see enum dfll_ctrl_mode)
0488  *
0489  * Change the DFLL's operating mode between disabled, open-loop mode,
0490  * and closed-loop mode, or vice versa.
0491  */
0492 static void dfll_set_mode(struct tegra_dfll *td,
0493               enum dfll_ctrl_mode mode)
0494 {
0495     td->mode = mode;
0496     dfll_writel(td, mode - 1, DFLL_CTRL);
0497     dfll_wmb(td);
0498 }
0499 
0500 /*
0501  * DVCO rate control
0502  */
0503 
0504 static unsigned long get_dvco_rate_below(struct tegra_dfll *td, u8 out_min)
0505 {
0506     struct dev_pm_opp *opp;
0507     unsigned long rate, prev_rate;
0508     unsigned long uv, min_uv;
0509 
0510     min_uv = td->lut_uv[out_min];
0511     for (rate = 0, prev_rate = 0; ; rate++) {
0512         opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
0513         if (IS_ERR(opp))
0514             break;
0515 
0516         uv = dev_pm_opp_get_voltage(opp);
0517         dev_pm_opp_put(opp);
0518 
0519         if (uv && uv > min_uv)
0520             return prev_rate;
0521 
0522         prev_rate = rate;
0523     }
0524 
0525     return prev_rate;
0526 }
0527 
0528 /*
0529  * DFLL-to-I2C controller interface
0530  */
0531 
0532 /**
0533  * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests
0534  * @td: DFLL instance
0535  * @enable: whether to enable or disable the I2C voltage requests
0536  *
0537  * Set the master enable control for I2C control value updates. If disabled,
0538  * then I2C control messages are inhibited, regardless of the DFLL mode.
0539  */
0540 static int dfll_i2c_set_output_enabled(struct tegra_dfll *td, bool enable)
0541 {
0542     u32 val;
0543 
0544     val = dfll_i2c_readl(td, DFLL_OUTPUT_CFG);
0545 
0546     if (enable)
0547         val |= DFLL_OUTPUT_CFG_I2C_ENABLE;
0548     else
0549         val &= ~DFLL_OUTPUT_CFG_I2C_ENABLE;
0550 
0551     dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG);
0552     dfll_i2c_wmb(td);
0553 
0554     return 0;
0555 }
0556 
0557 
0558 /*
0559  * DFLL-to-PWM controller interface
0560  */
0561 
0562 /**
0563  * dfll_pwm_set_output_enabled - enable/disable PWM voltage requests
0564  * @td: DFLL instance
0565  * @enable: whether to enable or disable the PWM voltage requests
0566  *
0567  * Set the master enable control for PWM control value updates. If disabled,
0568  * then the PWM signal is not driven. Also configure the PWM output pad
0569  * to the appropriate state.
0570  */
0571 static int dfll_pwm_set_output_enabled(struct tegra_dfll *td, bool enable)
0572 {
0573     int ret;
0574     u32 val, div;
0575 
0576     if (enable) {
0577         ret = pinctrl_select_state(td->pwm_pin, td->pwm_enable_state);
0578         if (ret < 0) {
0579             dev_err(td->dev, "setting enable state failed\n");
0580             return -EINVAL;
0581         }
0582         val = dfll_readl(td, DFLL_OUTPUT_CFG);
0583         val &= ~DFLL_OUTPUT_CFG_PWM_DIV_MASK;
0584         div = DIV_ROUND_UP(td->ref_rate, td->pwm_rate);
0585         val |= (div << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT)
0586                 & DFLL_OUTPUT_CFG_PWM_DIV_MASK;
0587         dfll_writel(td, val, DFLL_OUTPUT_CFG);
0588         dfll_wmb(td);
0589 
0590         val |= DFLL_OUTPUT_CFG_PWM_ENABLE;
0591         dfll_writel(td, val, DFLL_OUTPUT_CFG);
0592         dfll_wmb(td);
0593     } else {
0594         ret = pinctrl_select_state(td->pwm_pin, td->pwm_disable_state);
0595         if (ret < 0)
0596             dev_warn(td->dev, "setting disable state failed\n");
0597 
0598         val = dfll_readl(td, DFLL_OUTPUT_CFG);
0599         val &= ~DFLL_OUTPUT_CFG_PWM_ENABLE;
0600         dfll_writel(td, val, DFLL_OUTPUT_CFG);
0601         dfll_wmb(td);
0602     }
0603 
0604     return 0;
0605 }
0606 
0607 /**
0608  * dfll_set_force_output_value - set fixed value for force output
0609  * @td: DFLL instance
0610  * @out_val: value to force output
0611  *
0612  * Set the fixed value for force output, DFLL will output this value when
0613  * force output is enabled.
0614  */
0615 static u32 dfll_set_force_output_value(struct tegra_dfll *td, u8 out_val)
0616 {
0617     u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
0618 
0619     val = (val & DFLL_OUTPUT_FORCE_ENABLE) | (out_val & OUT_MASK);
0620     dfll_writel(td, val, DFLL_OUTPUT_FORCE);
0621     dfll_wmb(td);
0622 
0623     return dfll_readl(td, DFLL_OUTPUT_FORCE);
0624 }
0625 
0626 /**
0627  * dfll_set_force_output_enabled - enable/disable force output
0628  * @td: DFLL instance
0629  * @enable: whether to enable or disable the force output
0630  *
0631  * Set the enable control for fouce output with fixed value.
0632  */
0633 static void dfll_set_force_output_enabled(struct tegra_dfll *td, bool enable)
0634 {
0635     u32 val = dfll_readl(td, DFLL_OUTPUT_FORCE);
0636 
0637     if (enable)
0638         val |= DFLL_OUTPUT_FORCE_ENABLE;
0639     else
0640         val &= ~DFLL_OUTPUT_FORCE_ENABLE;
0641 
0642     dfll_writel(td, val, DFLL_OUTPUT_FORCE);
0643     dfll_wmb(td);
0644 }
0645 
0646 /**
0647  * dfll_force_output - force output a fixed value
0648  * @td: DFLL instance
0649  * @out_sel: value to force output
0650  *
0651  * Set the fixed value for force output, DFLL will output this value.
0652  */
0653 static int dfll_force_output(struct tegra_dfll *td, unsigned int out_sel)
0654 {
0655     u32 val;
0656 
0657     if (out_sel > OUT_MASK)
0658         return -EINVAL;
0659 
0660     val = dfll_set_force_output_value(td, out_sel);
0661     if ((td->mode < DFLL_CLOSED_LOOP) &&
0662         !(val & DFLL_OUTPUT_FORCE_ENABLE)) {
0663         dfll_set_force_output_enabled(td, true);
0664     }
0665 
0666     return 0;
0667 }
0668 
0669 /**
0670  * dfll_load_i2c_lut - load the voltage lookup table
0671  * @td: struct tegra_dfll *
0672  *
0673  * Load the voltage-to-PMIC register value lookup table into the DFLL
0674  * IP block memory. Look-up tables can be loaded at any time.
0675  */
0676 static void dfll_load_i2c_lut(struct tegra_dfll *td)
0677 {
0678     int i, lut_index;
0679     u32 val;
0680 
0681     for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
0682         if (i < td->lut_min)
0683             lut_index = td->lut_min;
0684         else if (i > td->lut_max)
0685             lut_index = td->lut_max;
0686         else
0687             lut_index = i;
0688 
0689         val = regulator_list_hardware_vsel(td->vdd_reg,
0690                              td->lut[lut_index]);
0691         __raw_writel(val, td->lut_base + i * 4);
0692     }
0693 
0694     dfll_i2c_wmb(td);
0695 }
0696 
0697 /**
0698  * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface
0699  * @td: DFLL instance
0700  *
0701  * During DFLL driver initialization, program the DFLL-I2C interface
0702  * with the PMU slave address, vdd register offset, and transfer mode.
0703  * This data is used by the DFLL to automatically construct I2C
0704  * voltage-set commands, which are then passed to the DFLL's internal
0705  * I2C controller.
0706  */
0707 static void dfll_init_i2c_if(struct tegra_dfll *td)
0708 {
0709     u32 val;
0710 
0711     if (td->i2c_slave_addr > 0x7f) {
0712         val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT;
0713         val |= DFLL_I2C_CFG_SLAVE_ADDR_10;
0714     } else {
0715         val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT;
0716     }
0717     val |= DFLL_I2C_CFG_SIZE_MASK;
0718     val |= DFLL_I2C_CFG_ARB_ENABLE;
0719     dfll_i2c_writel(td, val, DFLL_I2C_CFG);
0720 
0721     dfll_i2c_writel(td, td->i2c_reg, DFLL_I2C_VDD_REG_ADDR);
0722 
0723     val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8);
0724     BUG_ON(!val || (val > DFLL_I2C_CLK_DIVISOR_MASK));
0725     val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT;
0726 
0727     /* default hs divisor just in case */
0728     val |= 1 << DFLL_I2C_CLK_DIVISOR_HS_SHIFT;
0729     __raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR);
0730     dfll_i2c_wmb(td);
0731 }
0732 
0733 /**
0734  * dfll_init_out_if - prepare DFLL-to-PMIC interface
0735  * @td: DFLL instance
0736  *
0737  * During DFLL driver initialization or resume from context loss,
0738  * disable the I2C command output to the PMIC, set safe voltage and
0739  * output limits, and disable and clear limit interrupts.
0740  */
0741 static void dfll_init_out_if(struct tegra_dfll *td)
0742 {
0743     u32 val;
0744 
0745     td->lut_min = td->lut_bottom;
0746     td->lut_max = td->lut_size - 1;
0747     td->lut_safe = td->lut_min + (td->lut_min < td->lut_max ? 1 : 0);
0748 
0749     /* clear DFLL_OUTPUT_CFG before setting new value */
0750     dfll_writel(td, 0, DFLL_OUTPUT_CFG);
0751     dfll_wmb(td);
0752 
0753     val = (td->lut_safe << DFLL_OUTPUT_CFG_SAFE_SHIFT) |
0754           (td->lut_max << DFLL_OUTPUT_CFG_MAX_SHIFT) |
0755           (td->lut_min << DFLL_OUTPUT_CFG_MIN_SHIFT);
0756     dfll_writel(td, val, DFLL_OUTPUT_CFG);
0757     dfll_wmb(td);
0758 
0759     dfll_writel(td, 0, DFLL_OUTPUT_FORCE);
0760     dfll_i2c_writel(td, 0, DFLL_INTR_EN);
0761     dfll_i2c_writel(td, DFLL_INTR_MAX_MASK | DFLL_INTR_MIN_MASK,
0762             DFLL_INTR_STS);
0763 
0764     if (td->pmu_if == TEGRA_DFLL_PMU_PWM) {
0765         u32 vinit = td->reg_init_uV;
0766         int vstep = td->soc->alignment.step_uv;
0767         unsigned long vmin = td->lut_uv[0];
0768 
0769         /* set initial voltage */
0770         if ((vinit >= vmin) && vstep) {
0771             unsigned int vsel;
0772 
0773             vsel = DIV_ROUND_UP((vinit - vmin), vstep);
0774             dfll_force_output(td, vsel);
0775         }
0776     } else {
0777         dfll_load_i2c_lut(td);
0778         dfll_init_i2c_if(td);
0779     }
0780 }
0781 
0782 /*
0783  * Set/get the DFLL's targeted output clock rate
0784  */
0785 
0786 /**
0787  * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate
0788  * @td: DFLL instance
0789  * @rate: clock rate
0790  *
0791  * Determines the index of a I2C LUT entry for a voltage that approximately
0792  * produces the given DFLL clock rate. This is used when forcing a value
0793  * to the integrator during rate changes. Returns -ENOENT if a suitable
0794  * LUT index is not found.
0795  */
0796 static int find_lut_index_for_rate(struct tegra_dfll *td, unsigned long rate)
0797 {
0798     struct dev_pm_opp *opp;
0799     int i, align_step;
0800 
0801     opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
0802     if (IS_ERR(opp))
0803         return PTR_ERR(opp);
0804 
0805     align_step = dev_pm_opp_get_voltage(opp) / td->soc->alignment.step_uv;
0806     dev_pm_opp_put(opp);
0807 
0808     for (i = td->lut_bottom; i < td->lut_size; i++) {
0809         if ((td->lut_uv[i] / td->soc->alignment.step_uv) >= align_step)
0810             return i;
0811     }
0812 
0813     return -ENOENT;
0814 }
0815 
0816 /**
0817  * dfll_calculate_rate_request - calculate DFLL parameters for a given rate
0818  * @td: DFLL instance
0819  * @req: DFLL-rate-request structure
0820  * @rate: the desired DFLL rate
0821  *
0822  * Populate the DFLL-rate-request record @req fields with the scale_bits
0823  * and mult_bits fields, based on the target input rate. Returns 0 upon
0824  * success, or -EINVAL if the requested rate in req->rate is too high
0825  * or low for the DFLL to generate.
0826  */
0827 static int dfll_calculate_rate_request(struct tegra_dfll *td,
0828                        struct dfll_rate_req *req,
0829                        unsigned long rate)
0830 {
0831     u32 val;
0832 
0833     /*
0834      * If requested rate is below the minimum DVCO rate, active the scaler.
0835      * In the future the DVCO minimum voltage should be selected based on
0836      * chip temperature and the actual minimum rate should be calibrated
0837      * at runtime.
0838      */
0839     req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1;
0840     if (rate < td->dvco_rate_min) {
0841         int scale;
0842 
0843         scale = DIV_ROUND_CLOSEST(rate / 1000 * DFLL_FREQ_REQ_SCALE_MAX,
0844                       td->dvco_rate_min / 1000);
0845         if (!scale) {
0846             dev_err(td->dev, "%s: Rate %lu is too low\n",
0847                 __func__, rate);
0848             return -EINVAL;
0849         }
0850         req->scale_bits = scale - 1;
0851         rate = td->dvco_rate_min;
0852     }
0853 
0854     /* Convert requested rate into frequency request and scale settings */
0855     val = DVCO_RATE_TO_MULT(rate, td->ref_rate);
0856     if (val > FREQ_MAX) {
0857         dev_err(td->dev, "%s: Rate %lu is above dfll range\n",
0858             __func__, rate);
0859         return -EINVAL;
0860     }
0861     req->mult_bits = val;
0862     req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate);
0863     req->rate = dfll_scale_dvco_rate(req->scale_bits,
0864                      req->dvco_target_rate);
0865     req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate);
0866     if (req->lut_index < 0)
0867         return req->lut_index;
0868 
0869     return 0;
0870 }
0871 
0872 /**
0873  * dfll_set_frequency_request - start the frequency change operation
0874  * @td: DFLL instance
0875  * @req: rate request structure
0876  *
0877  * Tell the DFLL to try to change its output frequency to the
0878  * frequency represented by @req. DFLL must be in closed-loop mode.
0879  */
0880 static void dfll_set_frequency_request(struct tegra_dfll *td,
0881                        struct dfll_rate_req *req)
0882 {
0883     u32 val = 0;
0884     int force_val;
0885     int coef = 128; /* FIXME: td->cg_scale? */;
0886 
0887     force_val = (req->lut_index - td->lut_safe) * coef / td->cg;
0888     force_val = clamp(force_val, FORCE_MIN, FORCE_MAX);
0889 
0890     val |= req->mult_bits << DFLL_FREQ_REQ_MULT_SHIFT;
0891     val |= req->scale_bits << DFLL_FREQ_REQ_SCALE_SHIFT;
0892     val |= ((u32)force_val << DFLL_FREQ_REQ_FORCE_SHIFT) &
0893         DFLL_FREQ_REQ_FORCE_MASK;
0894     val |= DFLL_FREQ_REQ_FREQ_VALID | DFLL_FREQ_REQ_FORCE_ENABLE;
0895 
0896     dfll_writel(td, val, DFLL_FREQ_REQ);
0897     dfll_wmb(td);
0898 }
0899 
0900 /**
0901  * dfll_request_rate - set the next rate for the DFLL to tune to
0902  * @td: DFLL instance
0903  * @rate: clock rate to target
0904  *
0905  * Convert the requested clock rate @rate into the DFLL control logic
0906  * settings. In closed-loop mode, update new settings immediately to
0907  * adjust DFLL output rate accordingly. Otherwise, just save them
0908  * until the next switch to closed loop. Returns 0 upon success,
0909  * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL
0910  * if @rate is outside the DFLL's tunable range.
0911  */
0912 static int dfll_request_rate(struct tegra_dfll *td, unsigned long rate)
0913 {
0914     int ret;
0915     struct dfll_rate_req req;
0916 
0917     if (td->mode == DFLL_UNINITIALIZED) {
0918         dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n",
0919             __func__, mode_name[td->mode]);
0920         return -EPERM;
0921     }
0922 
0923     ret = dfll_calculate_rate_request(td, &req, rate);
0924     if (ret)
0925         return ret;
0926 
0927     td->last_unrounded_rate = rate;
0928     td->last_req = req;
0929 
0930     if (td->mode == DFLL_CLOSED_LOOP)
0931         dfll_set_frequency_request(td, &td->last_req);
0932 
0933     return 0;
0934 }
0935 
0936 /*
0937  * DFLL enable/disable & open-loop <-> closed-loop transitions
0938  */
0939 
0940 /**
0941  * dfll_disable - switch from open-loop mode to disabled mode
0942  * @td: DFLL instance
0943  *
0944  * Switch from OPEN_LOOP state to DISABLED state. Returns 0 upon success
0945  * or -EPERM if the DFLL is not currently in open-loop mode.
0946  */
0947 static int dfll_disable(struct tegra_dfll *td)
0948 {
0949     if (td->mode != DFLL_OPEN_LOOP) {
0950         dev_err(td->dev, "cannot disable DFLL in %s mode\n",
0951             mode_name[td->mode]);
0952         return -EINVAL;
0953     }
0954 
0955     dfll_set_mode(td, DFLL_DISABLED);
0956     pm_runtime_put_sync(td->dev);
0957 
0958     return 0;
0959 }
0960 
0961 /**
0962  * dfll_enable - switch a disabled DFLL to open-loop mode
0963  * @td: DFLL instance
0964  *
0965  * Switch from DISABLED state to OPEN_LOOP state. Returns 0 upon success
0966  * or -EPERM if the DFLL is not currently disabled.
0967  */
0968 static int dfll_enable(struct tegra_dfll *td)
0969 {
0970     if (td->mode != DFLL_DISABLED) {
0971         dev_err(td->dev, "cannot enable DFLL in %s mode\n",
0972             mode_name[td->mode]);
0973         return -EPERM;
0974     }
0975 
0976     pm_runtime_get_sync(td->dev);
0977     dfll_set_mode(td, DFLL_OPEN_LOOP);
0978 
0979     return 0;
0980 }
0981 
0982 /**
0983  * dfll_set_open_loop_config - prepare to switch to open-loop mode
0984  * @td: DFLL instance
0985  *
0986  * Prepare to switch the DFLL to open-loop mode. This switches the
0987  * DFLL to the low-voltage tuning range, ensures that I2C output
0988  * forcing is disabled, and disables the output clock rate scaler.
0989  * The DFLL's low-voltage tuning range parameters must be
0990  * characterized to keep the downstream device stable at any DVCO
0991  * input voltage. No return value.
0992  */
0993 static void dfll_set_open_loop_config(struct tegra_dfll *td)
0994 {
0995     u32 val;
0996 
0997     /* always tune low (safe) in open loop */
0998     if (td->tune_range != DFLL_TUNE_LOW)
0999         dfll_tune_low(td);
1000 
1001     val = dfll_readl(td, DFLL_FREQ_REQ);
1002     val |= DFLL_FREQ_REQ_SCALE_MASK;
1003     val &= ~DFLL_FREQ_REQ_FORCE_ENABLE;
1004     dfll_writel(td, val, DFLL_FREQ_REQ);
1005     dfll_wmb(td);
1006 }
1007 
1008 /**
1009  * dfll_lock - switch from open-loop to closed-loop mode
1010  * @td: DFLL instance
1011  *
1012  * Switch from OPEN_LOOP state to CLOSED_LOOP state. Returns 0 upon success,
1013  * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the
1014  * DFLL is not currently in open-loop mode.
1015  */
1016 static int dfll_lock(struct tegra_dfll *td)
1017 {
1018     struct dfll_rate_req *req = &td->last_req;
1019 
1020     switch (td->mode) {
1021     case DFLL_CLOSED_LOOP:
1022         return 0;
1023 
1024     case DFLL_OPEN_LOOP:
1025         if (req->rate == 0) {
1026             dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n",
1027                 __func__);
1028             return -EINVAL;
1029         }
1030 
1031         if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1032             dfll_pwm_set_output_enabled(td, true);
1033         else
1034             dfll_i2c_set_output_enabled(td, true);
1035 
1036         dfll_set_mode(td, DFLL_CLOSED_LOOP);
1037         dfll_set_frequency_request(td, req);
1038         dfll_set_force_output_enabled(td, false);
1039         return 0;
1040 
1041     default:
1042         BUG_ON(td->mode > DFLL_CLOSED_LOOP);
1043         dev_err(td->dev, "%s: Cannot lock DFLL in %s mode\n",
1044             __func__, mode_name[td->mode]);
1045         return -EPERM;
1046     }
1047 }
1048 
1049 /**
1050  * dfll_unlock - switch from closed-loop to open-loop mode
1051  * @td: DFLL instance
1052  *
1053  * Switch from CLOSED_LOOP state to OPEN_LOOP state. Returns 0 upon success,
1054  * or -EPERM if the DFLL is not currently in open-loop mode.
1055  */
1056 static int dfll_unlock(struct tegra_dfll *td)
1057 {
1058     switch (td->mode) {
1059     case DFLL_CLOSED_LOOP:
1060         dfll_set_open_loop_config(td);
1061         dfll_set_mode(td, DFLL_OPEN_LOOP);
1062         if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1063             dfll_pwm_set_output_enabled(td, false);
1064         else
1065             dfll_i2c_set_output_enabled(td, false);
1066         return 0;
1067 
1068     case DFLL_OPEN_LOOP:
1069         return 0;
1070 
1071     default:
1072         BUG_ON(td->mode > DFLL_CLOSED_LOOP);
1073         dev_err(td->dev, "%s: Cannot unlock DFLL in %s mode\n",
1074             __func__, mode_name[td->mode]);
1075         return -EPERM;
1076     }
1077 }
1078 
1079 /*
1080  * Clock framework integration
1081  *
1082  * When the DFLL is being controlled by the CCF, always enter closed loop
1083  * mode when the clk is enabled. This requires that a DFLL rate request
1084  * has been set beforehand, which implies that a clk_set_rate() call is
1085  * always required before a clk_enable().
1086  */
1087 
1088 static int dfll_clk_is_enabled(struct clk_hw *hw)
1089 {
1090     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1091 
1092     return dfll_is_running(td);
1093 }
1094 
1095 static int dfll_clk_enable(struct clk_hw *hw)
1096 {
1097     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1098     int ret;
1099 
1100     ret = dfll_enable(td);
1101     if (ret)
1102         return ret;
1103 
1104     ret = dfll_lock(td);
1105     if (ret)
1106         dfll_disable(td);
1107 
1108     return ret;
1109 }
1110 
1111 static void dfll_clk_disable(struct clk_hw *hw)
1112 {
1113     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1114     int ret;
1115 
1116     ret = dfll_unlock(td);
1117     if (!ret)
1118         dfll_disable(td);
1119 }
1120 
1121 static unsigned long dfll_clk_recalc_rate(struct clk_hw *hw,
1122                       unsigned long parent_rate)
1123 {
1124     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1125 
1126     return td->last_unrounded_rate;
1127 }
1128 
1129 /* Must use determine_rate since it allows for rates exceeding 2^31-1 */
1130 static int dfll_clk_determine_rate(struct clk_hw *hw,
1131                    struct clk_rate_request *clk_req)
1132 {
1133     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1134     struct dfll_rate_req req;
1135     int ret;
1136 
1137     ret = dfll_calculate_rate_request(td, &req, clk_req->rate);
1138     if (ret)
1139         return ret;
1140 
1141     /*
1142      * Don't set the rounded rate, since it doesn't really matter as
1143      * the output rate will be voltage controlled anyway, and cpufreq
1144      * freaks out if any rounding happens.
1145      */
1146 
1147     return 0;
1148 }
1149 
1150 static int dfll_clk_set_rate(struct clk_hw *hw, unsigned long rate,
1151                  unsigned long parent_rate)
1152 {
1153     struct tegra_dfll *td = clk_hw_to_dfll(hw);
1154 
1155     return dfll_request_rate(td, rate);
1156 }
1157 
1158 static const struct clk_ops dfll_clk_ops = {
1159     .is_enabled = dfll_clk_is_enabled,
1160     .enable     = dfll_clk_enable,
1161     .disable    = dfll_clk_disable,
1162     .recalc_rate    = dfll_clk_recalc_rate,
1163     .determine_rate = dfll_clk_determine_rate,
1164     .set_rate   = dfll_clk_set_rate,
1165 };
1166 
1167 static struct clk_init_data dfll_clk_init_data = {
1168     .ops        = &dfll_clk_ops,
1169     .num_parents    = 0,
1170 };
1171 
1172 /**
1173  * dfll_register_clk - register the DFLL output clock with the clock framework
1174  * @td: DFLL instance
1175  *
1176  * Register the DFLL's output clock with the Linux clock framework and register
1177  * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL
1178  * or -ENOMEM upon failure.
1179  */
1180 static int dfll_register_clk(struct tegra_dfll *td)
1181 {
1182     int ret;
1183 
1184     dfll_clk_init_data.name = td->output_clock_name;
1185     td->dfll_clk_hw.init = &dfll_clk_init_data;
1186 
1187     td->dfll_clk = clk_register(td->dev, &td->dfll_clk_hw);
1188     if (IS_ERR(td->dfll_clk)) {
1189         dev_err(td->dev, "DFLL clock registration error\n");
1190         return -EINVAL;
1191     }
1192 
1193     ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get,
1194                   td->dfll_clk);
1195     if (ret) {
1196         dev_err(td->dev, "of_clk_add_provider() failed\n");
1197 
1198         clk_unregister(td->dfll_clk);
1199         return ret;
1200     }
1201 
1202     return 0;
1203 }
1204 
1205 /**
1206  * dfll_unregister_clk - unregister the DFLL output clock
1207  * @td: DFLL instance
1208  *
1209  * Unregister the DFLL's output clock from the Linux clock framework
1210  * and from clkdev. No return value.
1211  */
1212 static void dfll_unregister_clk(struct tegra_dfll *td)
1213 {
1214     of_clk_del_provider(td->dev->of_node);
1215     clk_unregister(td->dfll_clk);
1216     td->dfll_clk = NULL;
1217 }
1218 
1219 /*
1220  * Debugfs interface
1221  */
1222 
1223 #ifdef CONFIG_DEBUG_FS
1224 /*
1225  * Monitor control
1226  */
1227 
1228 /**
1229  * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq
1230  * @monitor_data: value read from the DFLL_MONITOR_DATA_VAL bitfield
1231  * @ref_rate: DFLL reference clock rate
1232  *
1233  * Convert @monitor_data from DFLL_MONITOR_DATA_VAL units into cycles
1234  * per second. Returns the converted value.
1235  */
1236 static u64 dfll_calc_monitored_rate(u32 monitor_data,
1237                     unsigned long ref_rate)
1238 {
1239     return monitor_data * (ref_rate / REF_CLK_CYC_PER_DVCO_SAMPLE);
1240 }
1241 
1242 /**
1243  * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor
1244  * @td: DFLL instance
1245  *
1246  * If the DFLL is enabled, return the last rate reported by the DFLL's
1247  * internal monitoring hardware. This works in both open-loop and
1248  * closed-loop mode, and takes the output scaler setting into account.
1249  * Assumes that the monitor was programmed to monitor frequency before
1250  * the sample period started. If the driver believes that the DFLL is
1251  * currently uninitialized or disabled, it will return 0, since
1252  * otherwise the DFLL monitor data register will return the last
1253  * measured rate from when the DFLL was active.
1254  */
1255 static u64 dfll_read_monitor_rate(struct tegra_dfll *td)
1256 {
1257     u32 v, s;
1258     u64 pre_scaler_rate, post_scaler_rate;
1259 
1260     if (!dfll_is_running(td))
1261         return 0;
1262 
1263     v = dfll_readl(td, DFLL_MONITOR_DATA);
1264     v = (v & DFLL_MONITOR_DATA_VAL_MASK) >> DFLL_MONITOR_DATA_VAL_SHIFT;
1265     pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate);
1266 
1267     s = dfll_readl(td, DFLL_FREQ_REQ);
1268     s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT;
1269     post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate);
1270 
1271     return post_scaler_rate;
1272 }
1273 
1274 static int attr_enable_get(void *data, u64 *val)
1275 {
1276     struct tegra_dfll *td = data;
1277 
1278     *val = dfll_is_running(td);
1279 
1280     return 0;
1281 }
1282 static int attr_enable_set(void *data, u64 val)
1283 {
1284     struct tegra_dfll *td = data;
1285 
1286     return val ? dfll_enable(td) : dfll_disable(td);
1287 }
1288 DEFINE_DEBUGFS_ATTRIBUTE(enable_fops, attr_enable_get, attr_enable_set,
1289              "%llu\n");
1290 
1291 static int attr_lock_get(void *data, u64 *val)
1292 {
1293     struct tegra_dfll *td = data;
1294 
1295     *val = (td->mode == DFLL_CLOSED_LOOP);
1296 
1297     return 0;
1298 }
1299 static int attr_lock_set(void *data, u64 val)
1300 {
1301     struct tegra_dfll *td = data;
1302 
1303     return val ? dfll_lock(td) :  dfll_unlock(td);
1304 }
1305 DEFINE_DEBUGFS_ATTRIBUTE(lock_fops, attr_lock_get, attr_lock_set, "%llu\n");
1306 
1307 static int attr_rate_get(void *data, u64 *val)
1308 {
1309     struct tegra_dfll *td = data;
1310 
1311     *val = dfll_read_monitor_rate(td);
1312 
1313     return 0;
1314 }
1315 
1316 static int attr_rate_set(void *data, u64 val)
1317 {
1318     struct tegra_dfll *td = data;
1319 
1320     return dfll_request_rate(td, val);
1321 }
1322 DEFINE_DEBUGFS_ATTRIBUTE(rate_fops, attr_rate_get, attr_rate_set, "%llu\n");
1323 
1324 static int attr_registers_show(struct seq_file *s, void *data)
1325 {
1326     u32 val, offs;
1327     struct tegra_dfll *td = s->private;
1328 
1329     seq_puts(s, "CONTROL REGISTERS:\n");
1330     for (offs = 0; offs <= DFLL_MONITOR_DATA; offs += 4) {
1331         if (offs == DFLL_OUTPUT_CFG)
1332             val = dfll_i2c_readl(td, offs);
1333         else
1334             val = dfll_readl(td, offs);
1335         seq_printf(s, "[0x%02x] = 0x%08x\n", offs, val);
1336     }
1337 
1338     seq_puts(s, "\nI2C and INTR REGISTERS:\n");
1339     for (offs = DFLL_I2C_CFG; offs <= DFLL_I2C_STS; offs += 4)
1340         seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1341                dfll_i2c_readl(td, offs));
1342     for (offs = DFLL_INTR_STS; offs <= DFLL_INTR_EN; offs += 4)
1343         seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1344                dfll_i2c_readl(td, offs));
1345 
1346     if (td->pmu_if == TEGRA_DFLL_PMU_I2C) {
1347         seq_puts(s, "\nINTEGRATED I2C CONTROLLER REGISTERS:\n");
1348         offs = DFLL_I2C_CLK_DIVISOR;
1349         seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1350                __raw_readl(td->i2c_controller_base + offs));
1351 
1352         seq_puts(s, "\nLUT:\n");
1353         for (offs = 0; offs <  4 * MAX_DFLL_VOLTAGES; offs += 4)
1354             seq_printf(s, "[0x%02x] = 0x%08x\n", offs,
1355                    __raw_readl(td->lut_base + offs));
1356     }
1357 
1358     return 0;
1359 }
1360 
1361 DEFINE_SHOW_ATTRIBUTE(attr_registers);
1362 
1363 static void dfll_debug_init(struct tegra_dfll *td)
1364 {
1365     struct dentry *root;
1366 
1367     if (!td || (td->mode == DFLL_UNINITIALIZED))
1368         return;
1369 
1370     root = debugfs_create_dir("tegra_dfll_fcpu", NULL);
1371     td->debugfs_dir = root;
1372 
1373     debugfs_create_file_unsafe("enable", 0644, root, td,
1374                    &enable_fops);
1375     debugfs_create_file_unsafe("lock", 0444, root, td, &lock_fops);
1376     debugfs_create_file_unsafe("rate", 0444, root, td, &rate_fops);
1377     debugfs_create_file("registers", 0444, root, td, &attr_registers_fops);
1378 }
1379 
1380 #else
1381 static inline void dfll_debug_init(struct tegra_dfll *td) { }
1382 #endif /* CONFIG_DEBUG_FS */
1383 
1384 /*
1385  * DFLL initialization
1386  */
1387 
1388 /**
1389  * dfll_set_default_params - program non-output related DFLL parameters
1390  * @td: DFLL instance
1391  *
1392  * During DFLL driver initialization or resume from context loss,
1393  * program parameters for the closed loop integrator, DVCO tuning,
1394  * voltage droop control and monitor control.
1395  */
1396 static void dfll_set_default_params(struct tegra_dfll *td)
1397 {
1398     u32 val;
1399 
1400     val = DIV_ROUND_UP(td->ref_rate, td->sample_rate * 32);
1401     BUG_ON(val > DFLL_CONFIG_DIV_MASK);
1402     dfll_writel(td, val, DFLL_CONFIG);
1403 
1404     val = (td->force_mode << DFLL_PARAMS_FORCE_MODE_SHIFT) |
1405         (td->cf << DFLL_PARAMS_CF_PARAM_SHIFT) |
1406         (td->ci << DFLL_PARAMS_CI_PARAM_SHIFT) |
1407         (td->cg << DFLL_PARAMS_CG_PARAM_SHIFT) |
1408         (td->cg_scale ? DFLL_PARAMS_CG_SCALE : 0);
1409     dfll_writel(td, val, DFLL_PARAMS);
1410 
1411     dfll_tune_low(td);
1412     dfll_writel(td, td->droop_ctrl, DFLL_DROOP_CTRL);
1413     dfll_writel(td, DFLL_MONITOR_CTRL_FREQ, DFLL_MONITOR_CTRL);
1414 }
1415 
1416 /**
1417  * dfll_init_clks - clk_get() the DFLL source clocks
1418  * @td: DFLL instance
1419  *
1420  * Call clk_get() on the DFLL source clocks and save the pointers for later
1421  * use. Returns 0 upon success or error (see devm_clk_get) if one or more
1422  * of the clocks couldn't be looked up.
1423  */
1424 static int dfll_init_clks(struct tegra_dfll *td)
1425 {
1426     td->ref_clk = devm_clk_get(td->dev, "ref");
1427     if (IS_ERR(td->ref_clk)) {
1428         dev_err(td->dev, "missing ref clock\n");
1429         return PTR_ERR(td->ref_clk);
1430     }
1431 
1432     td->soc_clk = devm_clk_get(td->dev, "soc");
1433     if (IS_ERR(td->soc_clk)) {
1434         dev_err(td->dev, "missing soc clock\n");
1435         return PTR_ERR(td->soc_clk);
1436     }
1437 
1438     td->i2c_clk = devm_clk_get(td->dev, "i2c");
1439     if (IS_ERR(td->i2c_clk)) {
1440         dev_err(td->dev, "missing i2c clock\n");
1441         return PTR_ERR(td->i2c_clk);
1442     }
1443     td->i2c_clk_rate = clk_get_rate(td->i2c_clk);
1444 
1445     return 0;
1446 }
1447 
1448 /**
1449  * dfll_init - Prepare the DFLL IP block for use
1450  * @td: DFLL instance
1451  *
1452  * Do everything necessary to prepare the DFLL IP block for use. The
1453  * DFLL will be left in DISABLED state. Called by dfll_probe().
1454  * Returns 0 upon success, or passes along the error from whatever
1455  * function returned it.
1456  */
1457 static int dfll_init(struct tegra_dfll *td)
1458 {
1459     int ret;
1460 
1461     td->ref_rate = clk_get_rate(td->ref_clk);
1462     if (td->ref_rate != REF_CLOCK_RATE) {
1463         dev_err(td->dev, "unexpected ref clk rate %lu, expecting %lu",
1464             td->ref_rate, REF_CLOCK_RATE);
1465         return -EINVAL;
1466     }
1467 
1468     reset_control_deassert(td->dfll_rst);
1469     reset_control_deassert(td->dvco_rst);
1470 
1471     ret = clk_prepare(td->ref_clk);
1472     if (ret) {
1473         dev_err(td->dev, "failed to prepare ref_clk\n");
1474         return ret;
1475     }
1476 
1477     ret = clk_prepare(td->soc_clk);
1478     if (ret) {
1479         dev_err(td->dev, "failed to prepare soc_clk\n");
1480         goto di_err1;
1481     }
1482 
1483     ret = clk_prepare(td->i2c_clk);
1484     if (ret) {
1485         dev_err(td->dev, "failed to prepare i2c_clk\n");
1486         goto di_err2;
1487     }
1488 
1489     td->last_unrounded_rate = 0;
1490 
1491     pm_runtime_enable(td->dev);
1492     pm_runtime_get_sync(td->dev);
1493 
1494     dfll_set_mode(td, DFLL_DISABLED);
1495     dfll_set_default_params(td);
1496 
1497     if (td->soc->init_clock_trimmers)
1498         td->soc->init_clock_trimmers();
1499 
1500     dfll_set_open_loop_config(td);
1501 
1502     dfll_init_out_if(td);
1503 
1504     pm_runtime_put_sync(td->dev);
1505 
1506     return 0;
1507 
1508 di_err2:
1509     clk_unprepare(td->soc_clk);
1510 di_err1:
1511     clk_unprepare(td->ref_clk);
1512 
1513     reset_control_assert(td->dvco_rst);
1514     reset_control_assert(td->dfll_rst);
1515 
1516     return ret;
1517 }
1518 
1519 /**
1520  * tegra_dfll_suspend - check DFLL is disabled
1521  * @dev: DFLL instance
1522  *
1523  * DFLL clock should be disabled by the CPUFreq driver. So, make
1524  * sure it is disabled and disable all clocks needed by the DFLL.
1525  */
1526 int tegra_dfll_suspend(struct device *dev)
1527 {
1528     struct tegra_dfll *td = dev_get_drvdata(dev);
1529 
1530     if (dfll_is_running(td)) {
1531         dev_err(td->dev, "DFLL still enabled while suspending\n");
1532         return -EBUSY;
1533     }
1534 
1535     reset_control_assert(td->dvco_rst);
1536     reset_control_assert(td->dfll_rst);
1537 
1538     return 0;
1539 }
1540 EXPORT_SYMBOL(tegra_dfll_suspend);
1541 
1542 /**
1543  * tegra_dfll_resume - reinitialize DFLL on resume
1544  * @dev: DFLL instance
1545  *
1546  * DFLL is disabled and reset during suspend and resume.
1547  * So, reinitialize the DFLL IP block back for use.
1548  * DFLL clock is enabled later in closed loop mode by CPUFreq
1549  * driver before switching its clock source to DFLL output.
1550  */
1551 int tegra_dfll_resume(struct device *dev)
1552 {
1553     struct tegra_dfll *td = dev_get_drvdata(dev);
1554 
1555     reset_control_deassert(td->dfll_rst);
1556     reset_control_deassert(td->dvco_rst);
1557 
1558     pm_runtime_get_sync(td->dev);
1559 
1560     dfll_set_mode(td, DFLL_DISABLED);
1561     dfll_set_default_params(td);
1562 
1563     if (td->soc->init_clock_trimmers)
1564         td->soc->init_clock_trimmers();
1565 
1566     dfll_set_open_loop_config(td);
1567 
1568     dfll_init_out_if(td);
1569 
1570     pm_runtime_put_sync(td->dev);
1571 
1572     return 0;
1573 }
1574 EXPORT_SYMBOL(tegra_dfll_resume);
1575 
1576 /*
1577  * DT data fetch
1578  */
1579 
1580 /*
1581  * Find a PMIC voltage register-to-voltage mapping for the given voltage.
1582  * An exact voltage match is required.
1583  */
1584 static int find_vdd_map_entry_exact(struct tegra_dfll *td, int uV)
1585 {
1586     int i, n_voltages, reg_uV,reg_volt_id, align_step;
1587 
1588     if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM))
1589         return -EINVAL;
1590 
1591     align_step = uV / td->soc->alignment.step_uv;
1592     n_voltages = regulator_count_voltages(td->vdd_reg);
1593     for (i = 0; i < n_voltages; i++) {
1594         reg_uV = regulator_list_voltage(td->vdd_reg, i);
1595         if (reg_uV < 0)
1596             break;
1597 
1598         reg_volt_id = reg_uV / td->soc->alignment.step_uv;
1599 
1600         if (align_step == reg_volt_id)
1601             return i;
1602     }
1603 
1604     dev_err(td->dev, "no voltage map entry for %d uV\n", uV);
1605     return -EINVAL;
1606 }
1607 
1608 /*
1609  * Find a PMIC voltage register-to-voltage mapping for the given voltage,
1610  * rounding up to the closest supported voltage.
1611  * */
1612 static int find_vdd_map_entry_min(struct tegra_dfll *td, int uV)
1613 {
1614     int i, n_voltages, reg_uV, reg_volt_id, align_step;
1615 
1616     if (WARN_ON(td->pmu_if == TEGRA_DFLL_PMU_PWM))
1617         return -EINVAL;
1618 
1619     align_step = uV / td->soc->alignment.step_uv;
1620     n_voltages = regulator_count_voltages(td->vdd_reg);
1621     for (i = 0; i < n_voltages; i++) {
1622         reg_uV = regulator_list_voltage(td->vdd_reg, i);
1623         if (reg_uV < 0)
1624             break;
1625 
1626         reg_volt_id = reg_uV / td->soc->alignment.step_uv;
1627 
1628         if (align_step <= reg_volt_id)
1629             return i;
1630     }
1631 
1632     dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV);
1633     return -EINVAL;
1634 }
1635 
1636 /*
1637  * dfll_build_pwm_lut - build the PWM regulator lookup table
1638  * @td: DFLL instance
1639  * @v_max: Vmax from OPP table
1640  *
1641  * Look-up table in h/w is ignored when PWM is used as DFLL interface to PMIC.
1642  * In this case closed loop output is controlling duty cycle directly. The s/w
1643  * look-up that maps PWM duty cycle to voltage is still built by this function.
1644  */
1645 static int dfll_build_pwm_lut(struct tegra_dfll *td, unsigned long v_max)
1646 {
1647     int i;
1648     unsigned long rate, reg_volt;
1649     u8 lut_bottom = MAX_DFLL_VOLTAGES;
1650     int v_min = td->soc->cvb->min_millivolts * 1000;
1651 
1652     for (i = 0; i < MAX_DFLL_VOLTAGES; i++) {
1653         reg_volt = td->lut_uv[i];
1654 
1655         /* since opp voltage is exact mv */
1656         reg_volt = (reg_volt / 1000) * 1000;
1657         if (reg_volt > v_max)
1658             break;
1659 
1660         td->lut[i] = i;
1661         if ((lut_bottom == MAX_DFLL_VOLTAGES) && (reg_volt >= v_min))
1662             lut_bottom = i;
1663     }
1664 
1665     /* determine voltage boundaries */
1666     td->lut_size = i;
1667     if ((lut_bottom == MAX_DFLL_VOLTAGES) ||
1668         (lut_bottom + 1 >= td->lut_size)) {
1669         dev_err(td->dev, "no voltage above DFLL minimum %d mV\n",
1670             td->soc->cvb->min_millivolts);
1671         return -EINVAL;
1672     }
1673     td->lut_bottom = lut_bottom;
1674 
1675     /* determine rate boundaries */
1676     rate = get_dvco_rate_below(td, td->lut_bottom);
1677     if (!rate) {
1678         dev_err(td->dev, "no opp below DFLL minimum voltage %d mV\n",
1679             td->soc->cvb->min_millivolts);
1680         return -EINVAL;
1681     }
1682     td->dvco_rate_min = rate;
1683 
1684     return 0;
1685 }
1686 
1687 /**
1688  * dfll_build_i2c_lut - build the I2C voltage register lookup table
1689  * @td: DFLL instance
1690  * @v_max: Vmax from OPP table
1691  *
1692  * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with
1693  * PMIC voltage register values that span the entire DFLL operating range.
1694  * This function builds the look-up table based on the OPP table provided by
1695  * the soc-specific platform driver (td->soc->opp_dev) and the PMIC
1696  * register-to-voltage mapping queried from the regulator framework.
1697  *
1698  * On success, fills in td->lut and returns 0, or -err on failure.
1699  */
1700 static int dfll_build_i2c_lut(struct tegra_dfll *td, unsigned long v_max)
1701 {
1702     unsigned long rate, v, v_opp;
1703     int ret = -EINVAL;
1704     int j, selector, lut;
1705 
1706     v = td->soc->cvb->min_millivolts * 1000;
1707     lut = find_vdd_map_entry_exact(td, v);
1708     if (lut < 0)
1709         goto out;
1710     td->lut[0] = lut;
1711     td->lut_bottom = 0;
1712 
1713     for (j = 1, rate = 0; ; rate++) {
1714         struct dev_pm_opp *opp;
1715 
1716         opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate);
1717         if (IS_ERR(opp))
1718             break;
1719         v_opp = dev_pm_opp_get_voltage(opp);
1720 
1721         if (v_opp <= td->soc->cvb->min_millivolts * 1000)
1722             td->dvco_rate_min = dev_pm_opp_get_freq(opp);
1723 
1724         dev_pm_opp_put(opp);
1725 
1726         for (;;) {
1727             v += max(1UL, (v_max - v) / (MAX_DFLL_VOLTAGES - j));
1728             if (v >= v_opp)
1729                 break;
1730 
1731             selector = find_vdd_map_entry_min(td, v);
1732             if (selector < 0)
1733                 goto out;
1734             if (selector != td->lut[j - 1])
1735                 td->lut[j++] = selector;
1736         }
1737 
1738         v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp;
1739         selector = find_vdd_map_entry_exact(td, v);
1740         if (selector < 0)
1741             goto out;
1742         if (selector != td->lut[j - 1])
1743             td->lut[j++] = selector;
1744 
1745         if (v >= v_max)
1746             break;
1747     }
1748     td->lut_size = j;
1749 
1750     if (!td->dvco_rate_min)
1751         dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n",
1752             td->soc->cvb->min_millivolts);
1753     else {
1754         ret = 0;
1755         for (j = 0; j < td->lut_size; j++)
1756             td->lut_uv[j] =
1757                 regulator_list_voltage(td->vdd_reg,
1758                                td->lut[j]);
1759     }
1760 
1761 out:
1762     return ret;
1763 }
1764 
1765 static int dfll_build_lut(struct tegra_dfll *td)
1766 {
1767     unsigned long rate, v_max;
1768     struct dev_pm_opp *opp;
1769 
1770     rate = ULONG_MAX;
1771     opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate);
1772     if (IS_ERR(opp)) {
1773         dev_err(td->dev, "couldn't get vmax opp, empty opp table?\n");
1774         return -EINVAL;
1775     }
1776     v_max = dev_pm_opp_get_voltage(opp);
1777     dev_pm_opp_put(opp);
1778 
1779     if (td->pmu_if == TEGRA_DFLL_PMU_PWM)
1780         return dfll_build_pwm_lut(td, v_max);
1781     else
1782         return dfll_build_i2c_lut(td, v_max);
1783 }
1784 
1785 /**
1786  * read_dt_param - helper function for reading required parameters from the DT
1787  * @td: DFLL instance
1788  * @param: DT property name
1789  * @dest: output pointer for the value read
1790  *
1791  * Read a required numeric parameter from the DFLL device node, or complain
1792  * if the property doesn't exist. Returns a boolean indicating success for
1793  * easy chaining of multiple calls to this function.
1794  */
1795 static bool read_dt_param(struct tegra_dfll *td, const char *param, u32 *dest)
1796 {
1797     int err = of_property_read_u32(td->dev->of_node, param, dest);
1798 
1799     if (err < 0) {
1800         dev_err(td->dev, "failed to read DT parameter %s: %d\n",
1801             param, err);
1802         return false;
1803     }
1804 
1805     return true;
1806 }
1807 
1808 /**
1809  * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem
1810  * @td: DFLL instance
1811  *
1812  * Read all the parameters required for operation in I2C mode. The parameters
1813  * can originate from the device tree or the regulator subsystem.
1814  * Returns 0 on success or -err on failure.
1815  */
1816 static int dfll_fetch_i2c_params(struct tegra_dfll *td)
1817 {
1818     struct regmap *regmap;
1819     struct device *i2c_dev;
1820     struct i2c_client *i2c_client;
1821     int vsel_reg, vsel_mask;
1822     int ret;
1823 
1824     if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate))
1825         return -EINVAL;
1826 
1827     regmap = regulator_get_regmap(td->vdd_reg);
1828     i2c_dev = regmap_get_device(regmap);
1829     i2c_client = to_i2c_client(i2c_dev);
1830 
1831     td->i2c_slave_addr = i2c_client->addr;
1832 
1833     ret = regulator_get_hardware_vsel_register(td->vdd_reg,
1834                            &vsel_reg,
1835                            &vsel_mask);
1836     if (ret < 0) {
1837         dev_err(td->dev,
1838             "regulator unsuitable for DFLL I2C operation\n");
1839         return -EINVAL;
1840     }
1841     td->i2c_reg = vsel_reg;
1842 
1843     return 0;
1844 }
1845 
1846 static int dfll_fetch_pwm_params(struct tegra_dfll *td)
1847 {
1848     int ret, i;
1849     u32 pwm_period;
1850 
1851     if (!td->soc->alignment.step_uv || !td->soc->alignment.offset_uv) {
1852         dev_err(td->dev,
1853             "Missing step or alignment info for PWM regulator");
1854         return -EINVAL;
1855     }
1856     for (i = 0; i < MAX_DFLL_VOLTAGES; i++)
1857         td->lut_uv[i] = td->soc->alignment.offset_uv +
1858                 i * td->soc->alignment.step_uv;
1859 
1860     ret = read_dt_param(td, "nvidia,pwm-tristate-microvolts",
1861                 &td->reg_init_uV);
1862     if (!ret) {
1863         dev_err(td->dev, "couldn't get initialized voltage\n");
1864         return -EINVAL;
1865     }
1866 
1867     ret = read_dt_param(td, "nvidia,pwm-period-nanoseconds", &pwm_period);
1868     if (!ret) {
1869         dev_err(td->dev, "couldn't get PWM period\n");
1870         return -EINVAL;
1871     }
1872     td->pwm_rate = (NSEC_PER_SEC / pwm_period) * (MAX_DFLL_VOLTAGES - 1);
1873 
1874     td->pwm_pin = devm_pinctrl_get(td->dev);
1875     if (IS_ERR(td->pwm_pin)) {
1876         dev_err(td->dev, "DT: missing pinctrl device\n");
1877         return PTR_ERR(td->pwm_pin);
1878     }
1879 
1880     td->pwm_enable_state = pinctrl_lookup_state(td->pwm_pin,
1881                             "dvfs_pwm_enable");
1882     if (IS_ERR(td->pwm_enable_state)) {
1883         dev_err(td->dev, "DT: missing pwm enabled state\n");
1884         return PTR_ERR(td->pwm_enable_state);
1885     }
1886 
1887     td->pwm_disable_state = pinctrl_lookup_state(td->pwm_pin,
1888                              "dvfs_pwm_disable");
1889     if (IS_ERR(td->pwm_disable_state)) {
1890         dev_err(td->dev, "DT: missing pwm disabled state\n");
1891         return PTR_ERR(td->pwm_disable_state);
1892     }
1893 
1894     return 0;
1895 }
1896 
1897 /**
1898  * dfll_fetch_common_params - read DFLL parameters from the device tree
1899  * @td: DFLL instance
1900  *
1901  * Read all the DT parameters that are common to both I2C and PWM operation.
1902  * Returns 0 on success or -EINVAL on any failure.
1903  */
1904 static int dfll_fetch_common_params(struct tegra_dfll *td)
1905 {
1906     bool ok = true;
1907 
1908     ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl);
1909     ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate);
1910     ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode);
1911     ok &= read_dt_param(td, "nvidia,cf", &td->cf);
1912     ok &= read_dt_param(td, "nvidia,ci", &td->ci);
1913     ok &= read_dt_param(td, "nvidia,cg", &td->cg);
1914     td->cg_scale = of_property_read_bool(td->dev->of_node,
1915                          "nvidia,cg-scale");
1916 
1917     if (of_property_read_string(td->dev->of_node, "clock-output-names",
1918                     &td->output_clock_name)) {
1919         dev_err(td->dev, "missing clock-output-names property\n");
1920         ok = false;
1921     }
1922 
1923     return ok ? 0 : -EINVAL;
1924 }
1925 
1926 /*
1927  * API exported to per-SoC platform drivers
1928  */
1929 
1930 /**
1931  * tegra_dfll_register - probe a Tegra DFLL device
1932  * @pdev: DFLL platform_device *
1933  * @soc: Per-SoC integration and characterization data for this DFLL instance
1934  *
1935  * Probe and initialize a DFLL device instance. Intended to be called
1936  * by a SoC-specific shim driver that passes in per-SoC integration
1937  * and configuration data via @soc. Returns 0 on success or -err on failure.
1938  */
1939 int tegra_dfll_register(struct platform_device *pdev,
1940             struct tegra_dfll_soc_data *soc)
1941 {
1942     struct resource *mem;
1943     struct tegra_dfll *td;
1944     int ret;
1945 
1946     if (!soc) {
1947         dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n");
1948         return -EINVAL;
1949     }
1950 
1951     td = devm_kzalloc(&pdev->dev, sizeof(*td), GFP_KERNEL);
1952     if (!td)
1953         return -ENOMEM;
1954     td->dev = &pdev->dev;
1955     platform_set_drvdata(pdev, td);
1956 
1957     td->soc = soc;
1958 
1959     td->dfll_rst = devm_reset_control_get_optional(td->dev, "dfll");
1960     if (IS_ERR(td->dfll_rst)) {
1961         dev_err(td->dev, "couldn't get dfll reset\n");
1962         return PTR_ERR(td->dfll_rst);
1963     }
1964 
1965     td->dvco_rst = devm_reset_control_get(td->dev, "dvco");
1966     if (IS_ERR(td->dvco_rst)) {
1967         dev_err(td->dev, "couldn't get dvco reset\n");
1968         return PTR_ERR(td->dvco_rst);
1969     }
1970 
1971     ret = dfll_fetch_common_params(td);
1972     if (ret) {
1973         dev_err(td->dev, "couldn't parse device tree parameters\n");
1974         return ret;
1975     }
1976 
1977     if (of_property_read_bool(td->dev->of_node, "nvidia,pwm-to-pmic")) {
1978         td->pmu_if = TEGRA_DFLL_PMU_PWM;
1979         ret = dfll_fetch_pwm_params(td);
1980     } else  {
1981         td->vdd_reg = devm_regulator_get(td->dev, "vdd-cpu");
1982         if (IS_ERR(td->vdd_reg)) {
1983             dev_err(td->dev, "couldn't get vdd_cpu regulator\n");
1984             return PTR_ERR(td->vdd_reg);
1985         }
1986         td->pmu_if = TEGRA_DFLL_PMU_I2C;
1987         ret = dfll_fetch_i2c_params(td);
1988     }
1989     if (ret)
1990         return ret;
1991 
1992     ret = dfll_build_lut(td);
1993     if (ret) {
1994         dev_err(td->dev, "couldn't build LUT\n");
1995         return ret;
1996     }
1997 
1998     mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1999     if (!mem) {
2000         dev_err(td->dev, "no control register resource\n");
2001         return -ENODEV;
2002     }
2003 
2004     td->base = devm_ioremap(td->dev, mem->start, resource_size(mem));
2005     if (!td->base) {
2006         dev_err(td->dev, "couldn't ioremap DFLL control registers\n");
2007         return -ENODEV;
2008     }
2009 
2010     mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2011     if (!mem) {
2012         dev_err(td->dev, "no i2c_base resource\n");
2013         return -ENODEV;
2014     }
2015 
2016     td->i2c_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
2017     if (!td->i2c_base) {
2018         dev_err(td->dev, "couldn't ioremap i2c_base resource\n");
2019         return -ENODEV;
2020     }
2021 
2022     mem = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2023     if (!mem) {
2024         dev_err(td->dev, "no i2c_controller_base resource\n");
2025         return -ENODEV;
2026     }
2027 
2028     td->i2c_controller_base = devm_ioremap(td->dev, mem->start,
2029                            resource_size(mem));
2030     if (!td->i2c_controller_base) {
2031         dev_err(td->dev,
2032             "couldn't ioremap i2c_controller_base resource\n");
2033         return -ENODEV;
2034     }
2035 
2036     mem = platform_get_resource(pdev, IORESOURCE_MEM, 3);
2037     if (!mem) {
2038         dev_err(td->dev, "no lut_base resource\n");
2039         return -ENODEV;
2040     }
2041 
2042     td->lut_base = devm_ioremap(td->dev, mem->start, resource_size(mem));
2043     if (!td->lut_base) {
2044         dev_err(td->dev,
2045             "couldn't ioremap lut_base resource\n");
2046         return -ENODEV;
2047     }
2048 
2049     ret = dfll_init_clks(td);
2050     if (ret) {
2051         dev_err(&pdev->dev, "DFLL clock init error\n");
2052         return ret;
2053     }
2054 
2055     /* Enable the clocks and set the device up */
2056     ret = dfll_init(td);
2057     if (ret)
2058         return ret;
2059 
2060     ret = dfll_register_clk(td);
2061     if (ret) {
2062         dev_err(&pdev->dev, "DFLL clk registration failed\n");
2063         return ret;
2064     }
2065 
2066     dfll_debug_init(td);
2067 
2068     return 0;
2069 }
2070 EXPORT_SYMBOL(tegra_dfll_register);
2071 
2072 /**
2073  * tegra_dfll_unregister - release all of the DFLL driver resources for a device
2074  * @pdev: DFLL platform_device *
2075  *
2076  * Unbind this driver from the DFLL hardware device represented by
2077  * @pdev. The DFLL must be disabled for this to succeed. Returns a
2078  * soc pointer upon success or -EBUSY if the DFLL is still active.
2079  */
2080 struct tegra_dfll_soc_data *tegra_dfll_unregister(struct platform_device *pdev)
2081 {
2082     struct tegra_dfll *td = platform_get_drvdata(pdev);
2083 
2084     /* Try to prevent removal while the DFLL is active */
2085     if (td->mode != DFLL_DISABLED) {
2086         dev_err(&pdev->dev,
2087             "must disable DFLL before removing driver\n");
2088         return ERR_PTR(-EBUSY);
2089     }
2090 
2091     debugfs_remove_recursive(td->debugfs_dir);
2092 
2093     dfll_unregister_clk(td);
2094     pm_runtime_disable(&pdev->dev);
2095 
2096     clk_unprepare(td->ref_clk);
2097     clk_unprepare(td->soc_clk);
2098     clk_unprepare(td->i2c_clk);
2099 
2100     reset_control_assert(td->dvco_rst);
2101     reset_control_assert(td->dfll_rst);
2102 
2103     return td->soc;
2104 }
2105 EXPORT_SYMBOL(tegra_dfll_unregister);