1 /* 2 * clk-dfll.c - Tegra DFLL clock source common code 3 * 4 * Copyright (C) 2012-2014 NVIDIA Corporation. All rights reserved. 5 * 6 * Aleksandr Frid <afrid@nvidia.com> 7 * Paul Walmsley <pwalmsley@nvidia.com> 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 * 13 * This program is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 16 * more details. 17 * 18 * This library is for the DVCO and DFLL IP blocks on the Tegra124 19 * SoC. These IP blocks together are also known at NVIDIA as 20 * "CL-DVFS". To try to avoid confusion, this code refers to them 21 * collectively as the "DFLL." 22 * 23 * The DFLL is a root clocksource which tolerates some amount of 24 * supply voltage noise. Tegra124 uses it to clock the fast CPU 25 * complex when the target CPU speed is above a particular rate. The 26 * DFLL can be operated in either open-loop mode or closed-loop mode. 27 * In open-loop mode, the DFLL generates an output clock appropriate 28 * to the supply voltage. In closed-loop mode, when configured with a 29 * target frequency, the DFLL minimizes supply voltage while 30 * delivering an average frequency equal to the target. 31 * 32 * Devices clocked by the DFLL must be able to tolerate frequency 33 * variation. In the case of the CPU, it's important to note that the 34 * CPU cycle time will vary. This has implications for 35 * performance-measurement code and any code that relies on the CPU 36 * cycle time to delay for a certain length of time. 37 * 38 */ 39 40 #include <linux/clk.h> 41 #include <linux/clk-provider.h> 42 #include <linux/debugfs.h> 43 #include <linux/device.h> 44 #include <linux/err.h> 45 #include <linux/i2c.h> 46 #include <linux/io.h> 47 #include <linux/kernel.h> 48 #include <linux/module.h> 49 #include <linux/of.h> 50 #include <linux/pm_opp.h> 51 #include <linux/pm_runtime.h> 52 #include <linux/regmap.h> 53 #include <linux/regulator/consumer.h> 54 #include <linux/reset.h> 55 #include <linux/seq_file.h> 56 57 #include "clk-dfll.h" 58 59 /* 60 * DFLL control registers - access via dfll_{readl,writel} 61 */ 62 63 /* DFLL_CTRL: DFLL control register */ 64 #define DFLL_CTRL 0x00 65 #define DFLL_CTRL_MODE_MASK 0x03 66 67 /* DFLL_CONFIG: DFLL sample rate control */ 68 #define DFLL_CONFIG 0x04 69 #define DFLL_CONFIG_DIV_MASK 0xff 70 #define DFLL_CONFIG_DIV_PRESCALE 32 71 72 /* DFLL_PARAMS: tuning coefficients for closed loop integrator */ 73 #define DFLL_PARAMS 0x08 74 #define DFLL_PARAMS_CG_SCALE (0x1 << 24) 75 #define DFLL_PARAMS_FORCE_MODE_SHIFT 22 76 #define DFLL_PARAMS_FORCE_MODE_MASK (0x3 << DFLL_PARAMS_FORCE_MODE_SHIFT) 77 #define DFLL_PARAMS_CF_PARAM_SHIFT 16 78 #define DFLL_PARAMS_CF_PARAM_MASK (0x3f << DFLL_PARAMS_CF_PARAM_SHIFT) 79 #define DFLL_PARAMS_CI_PARAM_SHIFT 8 80 #define DFLL_PARAMS_CI_PARAM_MASK (0x7 << DFLL_PARAMS_CI_PARAM_SHIFT) 81 #define DFLL_PARAMS_CG_PARAM_SHIFT 0 82 #define DFLL_PARAMS_CG_PARAM_MASK (0xff << DFLL_PARAMS_CG_PARAM_SHIFT) 83 84 /* DFLL_TUNE0: delay line configuration register 0 */ 85 #define DFLL_TUNE0 0x0c 86 87 /* DFLL_TUNE1: delay line configuration register 1 */ 88 #define DFLL_TUNE1 0x10 89 90 /* DFLL_FREQ_REQ: target DFLL frequency control */ 91 #define DFLL_FREQ_REQ 0x14 92 #define DFLL_FREQ_REQ_FORCE_ENABLE (0x1 << 28) 93 #define DFLL_FREQ_REQ_FORCE_SHIFT 16 94 #define DFLL_FREQ_REQ_FORCE_MASK (0xfff << DFLL_FREQ_REQ_FORCE_SHIFT) 95 #define FORCE_MAX 2047 96 #define FORCE_MIN -2048 97 #define DFLL_FREQ_REQ_SCALE_SHIFT 8 98 #define DFLL_FREQ_REQ_SCALE_MASK (0xff << DFLL_FREQ_REQ_SCALE_SHIFT) 99 #define DFLL_FREQ_REQ_SCALE_MAX 256 100 #define DFLL_FREQ_REQ_FREQ_VALID (0x1 << 7) 101 #define DFLL_FREQ_REQ_MULT_SHIFT 0 102 #define DFLL_FREQ_REG_MULT_MASK (0x7f << DFLL_FREQ_REQ_MULT_SHIFT) 103 #define FREQ_MAX 127 104 105 /* DFLL_DROOP_CTRL: droop prevention control */ 106 #define DFLL_DROOP_CTRL 0x1c 107 108 /* DFLL_OUTPUT_CFG: closed loop mode control registers */ 109 /* NOTE: access via dfll_i2c_{readl,writel} */ 110 #define DFLL_OUTPUT_CFG 0x20 111 #define DFLL_OUTPUT_CFG_I2C_ENABLE (0x1 << 30) 112 #define OUT_MASK 0x3f 113 #define DFLL_OUTPUT_CFG_SAFE_SHIFT 24 114 #define DFLL_OUTPUT_CFG_SAFE_MASK \ 115 (OUT_MASK << DFLL_OUTPUT_CFG_SAFE_SHIFT) 116 #define DFLL_OUTPUT_CFG_MAX_SHIFT 16 117 #define DFLL_OUTPUT_CFG_MAX_MASK \ 118 (OUT_MASK << DFLL_OUTPUT_CFG_MAX_SHIFT) 119 #define DFLL_OUTPUT_CFG_MIN_SHIFT 8 120 #define DFLL_OUTPUT_CFG_MIN_MASK \ 121 (OUT_MASK << DFLL_OUTPUT_CFG_MIN_SHIFT) 122 #define DFLL_OUTPUT_CFG_PWM_DELTA (0x1 << 7) 123 #define DFLL_OUTPUT_CFG_PWM_ENABLE (0x1 << 6) 124 #define DFLL_OUTPUT_CFG_PWM_DIV_SHIFT 0 125 #define DFLL_OUTPUT_CFG_PWM_DIV_MASK \ 126 (OUT_MASK << DFLL_OUTPUT_CFG_PWM_DIV_SHIFT) 127 128 /* DFLL_OUTPUT_FORCE: closed loop mode voltage forcing control */ 129 #define DFLL_OUTPUT_FORCE 0x24 130 #define DFLL_OUTPUT_FORCE_ENABLE (0x1 << 6) 131 #define DFLL_OUTPUT_FORCE_VALUE_SHIFT 0 132 #define DFLL_OUTPUT_FORCE_VALUE_MASK \ 133 (OUT_MASK << DFLL_OUTPUT_FORCE_VALUE_SHIFT) 134 135 /* DFLL_MONITOR_CTRL: internal monitor data source control */ 136 #define DFLL_MONITOR_CTRL 0x28 137 #define DFLL_MONITOR_CTRL_FREQ 6 138 139 /* DFLL_MONITOR_DATA: internal monitor data output */ 140 #define DFLL_MONITOR_DATA 0x2c 141 #define DFLL_MONITOR_DATA_NEW_MASK (0x1 << 16) 142 #define DFLL_MONITOR_DATA_VAL_SHIFT 0 143 #define DFLL_MONITOR_DATA_VAL_MASK (0xFFFF << DFLL_MONITOR_DATA_VAL_SHIFT) 144 145 /* 146 * I2C output control registers - access via dfll_i2c_{readl,writel} 147 */ 148 149 /* DFLL_I2C_CFG: I2C controller configuration register */ 150 #define DFLL_I2C_CFG 0x40 151 #define DFLL_I2C_CFG_ARB_ENABLE (0x1 << 20) 152 #define DFLL_I2C_CFG_HS_CODE_SHIFT 16 153 #define DFLL_I2C_CFG_HS_CODE_MASK (0x7 << DFLL_I2C_CFG_HS_CODE_SHIFT) 154 #define DFLL_I2C_CFG_PACKET_ENABLE (0x1 << 15) 155 #define DFLL_I2C_CFG_SIZE_SHIFT 12 156 #define DFLL_I2C_CFG_SIZE_MASK (0x7 << DFLL_I2C_CFG_SIZE_SHIFT) 157 #define DFLL_I2C_CFG_SLAVE_ADDR_10 (0x1 << 10) 158 #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT 1 159 #define DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT 0 160 161 /* DFLL_I2C_VDD_REG_ADDR: PMIC I2C address for closed loop mode */ 162 #define DFLL_I2C_VDD_REG_ADDR 0x44 163 164 /* DFLL_I2C_STS: I2C controller status */ 165 #define DFLL_I2C_STS 0x48 166 #define DFLL_I2C_STS_I2C_LAST_SHIFT 1 167 #define DFLL_I2C_STS_I2C_REQ_PENDING 0x1 168 169 /* DFLL_INTR_STS: DFLL interrupt status register */ 170 #define DFLL_INTR_STS 0x5c 171 172 /* DFLL_INTR_EN: DFLL interrupt enable register */ 173 #define DFLL_INTR_EN 0x60 174 #define DFLL_INTR_MIN_MASK 0x1 175 #define DFLL_INTR_MAX_MASK 0x2 176 177 /* 178 * Integrated I2C controller registers - relative to td->i2c_controller_base 179 */ 180 181 /* DFLL_I2C_CLK_DIVISOR: I2C controller clock divisor */ 182 #define DFLL_I2C_CLK_DIVISOR 0x6c 183 #define DFLL_I2C_CLK_DIVISOR_MASK 0xffff 184 #define DFLL_I2C_CLK_DIVISOR_FS_SHIFT 16 185 #define DFLL_I2C_CLK_DIVISOR_HS_SHIFT 0 186 #define DFLL_I2C_CLK_DIVISOR_PREDIV 8 187 #define DFLL_I2C_CLK_DIVISOR_HSMODE_PREDIV 12 188 189 /* 190 * Other constants 191 */ 192 193 /* MAX_DFLL_VOLTAGES: number of LUT entries in the DFLL IP block */ 194 #define MAX_DFLL_VOLTAGES 33 195 196 /* 197 * REF_CLK_CYC_PER_DVCO_SAMPLE: the number of ref_clk cycles that the hardware 198 * integrates the DVCO counter over - used for debug rate monitoring and 199 * droop control 200 */ 201 #define REF_CLK_CYC_PER_DVCO_SAMPLE 4 202 203 /* 204 * REF_CLOCK_RATE: the DFLL reference clock rate currently supported by this 205 * driver, in Hz 206 */ 207 #define REF_CLOCK_RATE 51000000UL 208 209 #define DVCO_RATE_TO_MULT(rate, ref_rate) ((rate) / ((ref_rate) / 2)) 210 #define MULT_TO_DVCO_RATE(mult, ref_rate) ((mult) * ((ref_rate) / 2)) 211 212 /** 213 * enum dfll_ctrl_mode - DFLL hardware operating mode 214 * @DFLL_UNINITIALIZED: (uninitialized state - not in hardware bitfield) 215 * @DFLL_DISABLED: DFLL not generating an output clock 216 * @DFLL_OPEN_LOOP: DVCO running, but DFLL not adjusting voltage 217 * @DFLL_CLOSED_LOOP: DVCO running, and DFLL adjusting voltage to match 218 * the requested rate 219 * 220 * The integer corresponding to the last two states, minus one, is 221 * written to the DFLL hardware to change operating modes. 222 */ 223 enum dfll_ctrl_mode { 224 DFLL_UNINITIALIZED = 0, 225 DFLL_DISABLED = 1, 226 DFLL_OPEN_LOOP = 2, 227 DFLL_CLOSED_LOOP = 3, 228 }; 229 230 /** 231 * enum dfll_tune_range - voltage range that the driver believes it's in 232 * @DFLL_TUNE_UNINITIALIZED: DFLL tuning not yet programmed 233 * @DFLL_TUNE_LOW: DFLL in the low-voltage range (or open-loop mode) 234 * 235 * Some DFLL tuning parameters may need to change depending on the 236 * DVCO's voltage; these states represent the ranges that the driver 237 * supports. These are software states; these values are never 238 * written into registers. 239 */ 240 enum dfll_tune_range { 241 DFLL_TUNE_UNINITIALIZED = 0, 242 DFLL_TUNE_LOW = 1, 243 }; 244 245 /** 246 * struct dfll_rate_req - target DFLL rate request data 247 * @rate: target frequency, after the postscaling 248 * @dvco_target_rate: target frequency, after the postscaling 249 * @lut_index: LUT index at which voltage the dvco_target_rate will be reached 250 * @mult_bits: value to program to the MULT bits of the DFLL_FREQ_REQ register 251 * @scale_bits: value to program to the SCALE bits of the DFLL_FREQ_REQ register 252 */ 253 struct dfll_rate_req { 254 unsigned long rate; 255 unsigned long dvco_target_rate; 256 int lut_index; 257 u8 mult_bits; 258 u8 scale_bits; 259 }; 260 261 struct tegra_dfll { 262 struct device *dev; 263 struct tegra_dfll_soc_data *soc; 264 265 void __iomem *base; 266 void __iomem *i2c_base; 267 void __iomem *i2c_controller_base; 268 void __iomem *lut_base; 269 270 struct regulator *vdd_reg; 271 struct clk *soc_clk; 272 struct clk *ref_clk; 273 struct clk *i2c_clk; 274 struct clk *dfll_clk; 275 struct reset_control *dvco_rst; 276 unsigned long ref_rate; 277 unsigned long i2c_clk_rate; 278 unsigned long dvco_rate_min; 279 280 enum dfll_ctrl_mode mode; 281 enum dfll_tune_range tune_range; 282 struct dentry *debugfs_dir; 283 struct clk_hw dfll_clk_hw; 284 const char *output_clock_name; 285 struct dfll_rate_req last_req; 286 unsigned long last_unrounded_rate; 287 288 /* Parameters from DT */ 289 u32 droop_ctrl; 290 u32 sample_rate; 291 u32 force_mode; 292 u32 cf; 293 u32 ci; 294 u32 cg; 295 bool cg_scale; 296 297 /* I2C interface parameters */ 298 u32 i2c_fs_rate; 299 u32 i2c_reg; 300 u32 i2c_slave_addr; 301 302 /* i2c_lut array entries are regulator framework selectors */ 303 unsigned i2c_lut[MAX_DFLL_VOLTAGES]; 304 int i2c_lut_size; 305 u8 lut_min, lut_max, lut_safe; 306 }; 307 308 #define clk_hw_to_dfll(_hw) container_of(_hw, struct tegra_dfll, dfll_clk_hw) 309 310 /* mode_name: map numeric DFLL modes to names for friendly console messages */ 311 static const char * const mode_name[] = { 312 [DFLL_UNINITIALIZED] = "uninitialized", 313 [DFLL_DISABLED] = "disabled", 314 [DFLL_OPEN_LOOP] = "open_loop", 315 [DFLL_CLOSED_LOOP] = "closed_loop", 316 }; 317 318 /* 319 * Register accessors 320 */ 321 322 static inline u32 dfll_readl(struct tegra_dfll *td, u32 offs) 323 { 324 return __raw_readl(td->base + offs); 325 } 326 327 static inline void dfll_writel(struct tegra_dfll *td, u32 val, u32 offs) 328 { 329 WARN_ON(offs >= DFLL_I2C_CFG); 330 __raw_writel(val, td->base + offs); 331 } 332 333 static inline void dfll_wmb(struct tegra_dfll *td) 334 { 335 dfll_readl(td, DFLL_CTRL); 336 } 337 338 /* I2C output control registers - for addresses above DFLL_I2C_CFG */ 339 340 static inline u32 dfll_i2c_readl(struct tegra_dfll *td, u32 offs) 341 { 342 return __raw_readl(td->i2c_base + offs); 343 } 344 345 static inline void dfll_i2c_writel(struct tegra_dfll *td, u32 val, u32 offs) 346 { 347 __raw_writel(val, td->i2c_base + offs); 348 } 349 350 static inline void dfll_i2c_wmb(struct tegra_dfll *td) 351 { 352 dfll_i2c_readl(td, DFLL_I2C_CFG); 353 } 354 355 /** 356 * dfll_is_running - is the DFLL currently generating a clock? 357 * @td: DFLL instance 358 * 359 * If the DFLL is currently generating an output clock signal, return 360 * true; otherwise return false. 361 */ 362 static bool dfll_is_running(struct tegra_dfll *td) 363 { 364 return td->mode >= DFLL_OPEN_LOOP; 365 } 366 367 /* 368 * Runtime PM suspend/resume callbacks 369 */ 370 371 /** 372 * tegra_dfll_runtime_resume - enable all clocks needed by the DFLL 373 * @dev: DFLL device * 374 * 375 * Enable all clocks needed by the DFLL. Assumes that clk_prepare() 376 * has already been called on all the clocks. 377 * 378 * XXX Should also handle context restore when returning from off. 379 */ 380 int tegra_dfll_runtime_resume(struct device *dev) 381 { 382 struct tegra_dfll *td = dev_get_drvdata(dev); 383 int ret; 384 385 ret = clk_enable(td->ref_clk); 386 if (ret) { 387 dev_err(dev, "could not enable ref clock: %d\n", ret); 388 return ret; 389 } 390 391 ret = clk_enable(td->soc_clk); 392 if (ret) { 393 dev_err(dev, "could not enable register clock: %d\n", ret); 394 clk_disable(td->ref_clk); 395 return ret; 396 } 397 398 ret = clk_enable(td->i2c_clk); 399 if (ret) { 400 dev_err(dev, "could not enable i2c clock: %d\n", ret); 401 clk_disable(td->soc_clk); 402 clk_disable(td->ref_clk); 403 return ret; 404 } 405 406 return 0; 407 } 408 EXPORT_SYMBOL(tegra_dfll_runtime_resume); 409 410 /** 411 * tegra_dfll_runtime_suspend - disable all clocks needed by the DFLL 412 * @dev: DFLL device * 413 * 414 * Disable all clocks needed by the DFLL. Assumes that other code 415 * will later call clk_unprepare(). 416 */ 417 int tegra_dfll_runtime_suspend(struct device *dev) 418 { 419 struct tegra_dfll *td = dev_get_drvdata(dev); 420 421 clk_disable(td->ref_clk); 422 clk_disable(td->soc_clk); 423 clk_disable(td->i2c_clk); 424 425 return 0; 426 } 427 EXPORT_SYMBOL(tegra_dfll_runtime_suspend); 428 429 /* 430 * DFLL tuning operations (per-voltage-range tuning settings) 431 */ 432 433 /** 434 * dfll_tune_low - tune to DFLL and CPU settings valid for any voltage 435 * @td: DFLL instance 436 * 437 * Tune the DFLL oscillator parameters and the CPU clock shaper for 438 * the low-voltage range. These settings are valid for any voltage, 439 * but may not be optimal. 440 */ 441 static void dfll_tune_low(struct tegra_dfll *td) 442 { 443 td->tune_range = DFLL_TUNE_LOW; 444 445 dfll_writel(td, td->soc->tune0_low, DFLL_TUNE0); 446 dfll_writel(td, td->soc->tune1, DFLL_TUNE1); 447 dfll_wmb(td); 448 449 if (td->soc->set_clock_trimmers_low) 450 td->soc->set_clock_trimmers_low(); 451 } 452 453 /* 454 * Output clock scaler helpers 455 */ 456 457 /** 458 * dfll_scale_dvco_rate - calculate scaled rate from the DVCO rate 459 * @scale_bits: clock scaler value (bits in the DFLL_FREQ_REQ_SCALE field) 460 * @dvco_rate: the DVCO rate 461 * 462 * Apply the same scaling formula that the DFLL hardware uses to scale 463 * the DVCO rate. 464 */ 465 static unsigned long dfll_scale_dvco_rate(int scale_bits, 466 unsigned long dvco_rate) 467 { 468 return (u64)dvco_rate * (scale_bits + 1) / DFLL_FREQ_REQ_SCALE_MAX; 469 } 470 471 /* 472 * DFLL mode switching 473 */ 474 475 /** 476 * dfll_set_mode - change the DFLL control mode 477 * @td: DFLL instance 478 * @mode: DFLL control mode (see enum dfll_ctrl_mode) 479 * 480 * Change the DFLL's operating mode between disabled, open-loop mode, 481 * and closed-loop mode, or vice versa. 482 */ 483 static void dfll_set_mode(struct tegra_dfll *td, 484 enum dfll_ctrl_mode mode) 485 { 486 td->mode = mode; 487 dfll_writel(td, mode - 1, DFLL_CTRL); 488 dfll_wmb(td); 489 } 490 491 /* 492 * DFLL-to-I2C controller interface 493 */ 494 495 /** 496 * dfll_i2c_set_output_enabled - enable/disable I2C PMIC voltage requests 497 * @td: DFLL instance 498 * @enable: whether to enable or disable the I2C voltage requests 499 * 500 * Set the master enable control for I2C control value updates. If disabled, 501 * then I2C control messages are inhibited, regardless of the DFLL mode. 502 */ 503 static int dfll_i2c_set_output_enabled(struct tegra_dfll *td, bool enable) 504 { 505 u32 val; 506 507 val = dfll_i2c_readl(td, DFLL_OUTPUT_CFG); 508 509 if (enable) 510 val |= DFLL_OUTPUT_CFG_I2C_ENABLE; 511 else 512 val &= ~DFLL_OUTPUT_CFG_I2C_ENABLE; 513 514 dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG); 515 dfll_i2c_wmb(td); 516 517 return 0; 518 } 519 520 /** 521 * dfll_load_lut - load the voltage lookup table 522 * @td: struct tegra_dfll * 523 * 524 * Load the voltage-to-PMIC register value lookup table into the DFLL 525 * IP block memory. Look-up tables can be loaded at any time. 526 */ 527 static void dfll_load_i2c_lut(struct tegra_dfll *td) 528 { 529 int i, lut_index; 530 u32 val; 531 532 for (i = 0; i < MAX_DFLL_VOLTAGES; i++) { 533 if (i < td->lut_min) 534 lut_index = td->lut_min; 535 else if (i > td->lut_max) 536 lut_index = td->lut_max; 537 else 538 lut_index = i; 539 540 val = regulator_list_hardware_vsel(td->vdd_reg, 541 td->i2c_lut[lut_index]); 542 __raw_writel(val, td->lut_base + i * 4); 543 } 544 545 dfll_i2c_wmb(td); 546 } 547 548 /** 549 * dfll_init_i2c_if - set up the DFLL's DFLL-I2C interface 550 * @td: DFLL instance 551 * 552 * During DFLL driver initialization, program the DFLL-I2C interface 553 * with the PMU slave address, vdd register offset, and transfer mode. 554 * This data is used by the DFLL to automatically construct I2C 555 * voltage-set commands, which are then passed to the DFLL's internal 556 * I2C controller. 557 */ 558 static void dfll_init_i2c_if(struct tegra_dfll *td) 559 { 560 u32 val; 561 562 if (td->i2c_slave_addr > 0x7f) { 563 val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_10BIT; 564 val |= DFLL_I2C_CFG_SLAVE_ADDR_10; 565 } else { 566 val = td->i2c_slave_addr << DFLL_I2C_CFG_SLAVE_ADDR_SHIFT_7BIT; 567 } 568 val |= DFLL_I2C_CFG_SIZE_MASK; 569 val |= DFLL_I2C_CFG_ARB_ENABLE; 570 dfll_i2c_writel(td, val, DFLL_I2C_CFG); 571 572 dfll_i2c_writel(td, td->i2c_reg, DFLL_I2C_VDD_REG_ADDR); 573 574 val = DIV_ROUND_UP(td->i2c_clk_rate, td->i2c_fs_rate * 8); 575 BUG_ON(!val || (val > DFLL_I2C_CLK_DIVISOR_MASK)); 576 val = (val - 1) << DFLL_I2C_CLK_DIVISOR_FS_SHIFT; 577 578 /* default hs divisor just in case */ 579 val |= 1 << DFLL_I2C_CLK_DIVISOR_HS_SHIFT; 580 __raw_writel(val, td->i2c_controller_base + DFLL_I2C_CLK_DIVISOR); 581 dfll_i2c_wmb(td); 582 } 583 584 /** 585 * dfll_init_out_if - prepare DFLL-to-PMIC interface 586 * @td: DFLL instance 587 * 588 * During DFLL driver initialization or resume from context loss, 589 * disable the I2C command output to the PMIC, set safe voltage and 590 * output limits, and disable and clear limit interrupts. 591 */ 592 static void dfll_init_out_if(struct tegra_dfll *td) 593 { 594 u32 val; 595 596 td->lut_min = 0; 597 td->lut_max = td->i2c_lut_size - 1; 598 td->lut_safe = td->lut_min + 1; 599 600 dfll_i2c_writel(td, 0, DFLL_OUTPUT_CFG); 601 val = (td->lut_safe << DFLL_OUTPUT_CFG_SAFE_SHIFT) | 602 (td->lut_max << DFLL_OUTPUT_CFG_MAX_SHIFT) | 603 (td->lut_min << DFLL_OUTPUT_CFG_MIN_SHIFT); 604 dfll_i2c_writel(td, val, DFLL_OUTPUT_CFG); 605 dfll_i2c_wmb(td); 606 607 dfll_writel(td, 0, DFLL_OUTPUT_FORCE); 608 dfll_i2c_writel(td, 0, DFLL_INTR_EN); 609 dfll_i2c_writel(td, DFLL_INTR_MAX_MASK | DFLL_INTR_MIN_MASK, 610 DFLL_INTR_STS); 611 612 dfll_load_i2c_lut(td); 613 dfll_init_i2c_if(td); 614 } 615 616 /* 617 * Set/get the DFLL's targeted output clock rate 618 */ 619 620 /** 621 * find_lut_index_for_rate - determine I2C LUT index for given DFLL rate 622 * @td: DFLL instance 623 * @rate: clock rate 624 * 625 * Determines the index of a I2C LUT entry for a voltage that approximately 626 * produces the given DFLL clock rate. This is used when forcing a value 627 * to the integrator during rate changes. Returns -ENOENT if a suitable 628 * LUT index is not found. 629 */ 630 static int find_lut_index_for_rate(struct tegra_dfll *td, unsigned long rate) 631 { 632 struct dev_pm_opp *opp; 633 int i, uv; 634 635 rcu_read_lock(); 636 637 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); 638 if (IS_ERR(opp)) { 639 rcu_read_unlock(); 640 return PTR_ERR(opp); 641 } 642 uv = dev_pm_opp_get_voltage(opp); 643 644 rcu_read_unlock(); 645 646 for (i = 0; i < td->i2c_lut_size; i++) { 647 if (regulator_list_voltage(td->vdd_reg, td->i2c_lut[i]) == uv) 648 return i; 649 } 650 651 return -ENOENT; 652 } 653 654 /** 655 * dfll_calculate_rate_request - calculate DFLL parameters for a given rate 656 * @td: DFLL instance 657 * @req: DFLL-rate-request structure 658 * @rate: the desired DFLL rate 659 * 660 * Populate the DFLL-rate-request record @req fields with the scale_bits 661 * and mult_bits fields, based on the target input rate. Returns 0 upon 662 * success, or -EINVAL if the requested rate in req->rate is too high 663 * or low for the DFLL to generate. 664 */ 665 static int dfll_calculate_rate_request(struct tegra_dfll *td, 666 struct dfll_rate_req *req, 667 unsigned long rate) 668 { 669 u32 val; 670 671 /* 672 * If requested rate is below the minimum DVCO rate, active the scaler. 673 * In the future the DVCO minimum voltage should be selected based on 674 * chip temperature and the actual minimum rate should be calibrated 675 * at runtime. 676 */ 677 req->scale_bits = DFLL_FREQ_REQ_SCALE_MAX - 1; 678 if (rate < td->dvco_rate_min) { 679 int scale; 680 681 scale = DIV_ROUND_CLOSEST(rate / 1000 * DFLL_FREQ_REQ_SCALE_MAX, 682 td->dvco_rate_min / 1000); 683 if (!scale) { 684 dev_err(td->dev, "%s: Rate %lu is too low\n", 685 __func__, rate); 686 return -EINVAL; 687 } 688 req->scale_bits = scale - 1; 689 rate = td->dvco_rate_min; 690 } 691 692 /* Convert requested rate into frequency request and scale settings */ 693 val = DVCO_RATE_TO_MULT(rate, td->ref_rate); 694 if (val > FREQ_MAX) { 695 dev_err(td->dev, "%s: Rate %lu is above dfll range\n", 696 __func__, rate); 697 return -EINVAL; 698 } 699 req->mult_bits = val; 700 req->dvco_target_rate = MULT_TO_DVCO_RATE(req->mult_bits, td->ref_rate); 701 req->rate = dfll_scale_dvco_rate(req->scale_bits, 702 req->dvco_target_rate); 703 req->lut_index = find_lut_index_for_rate(td, req->dvco_target_rate); 704 if (req->lut_index < 0) 705 return req->lut_index; 706 707 return 0; 708 } 709 710 /** 711 * dfll_set_frequency_request - start the frequency change operation 712 * @td: DFLL instance 713 * @req: rate request structure 714 * 715 * Tell the DFLL to try to change its output frequency to the 716 * frequency represented by @req. DFLL must be in closed-loop mode. 717 */ 718 static void dfll_set_frequency_request(struct tegra_dfll *td, 719 struct dfll_rate_req *req) 720 { 721 u32 val = 0; 722 int force_val; 723 int coef = 128; /* FIXME: td->cg_scale? */; 724 725 force_val = (req->lut_index - td->lut_safe) * coef / td->cg; 726 force_val = clamp(force_val, FORCE_MIN, FORCE_MAX); 727 728 val |= req->mult_bits << DFLL_FREQ_REQ_MULT_SHIFT; 729 val |= req->scale_bits << DFLL_FREQ_REQ_SCALE_SHIFT; 730 val |= ((u32)force_val << DFLL_FREQ_REQ_FORCE_SHIFT) & 731 DFLL_FREQ_REQ_FORCE_MASK; 732 val |= DFLL_FREQ_REQ_FREQ_VALID | DFLL_FREQ_REQ_FORCE_ENABLE; 733 734 dfll_writel(td, val, DFLL_FREQ_REQ); 735 dfll_wmb(td); 736 } 737 738 /** 739 * tegra_dfll_request_rate - set the next rate for the DFLL to tune to 740 * @td: DFLL instance 741 * @rate: clock rate to target 742 * 743 * Convert the requested clock rate @rate into the DFLL control logic 744 * settings. In closed-loop mode, update new settings immediately to 745 * adjust DFLL output rate accordingly. Otherwise, just save them 746 * until the next switch to closed loop. Returns 0 upon success, 747 * -EPERM if the DFLL driver has not yet been initialized, or -EINVAL 748 * if @rate is outside the DFLL's tunable range. 749 */ 750 static int dfll_request_rate(struct tegra_dfll *td, unsigned long rate) 751 { 752 int ret; 753 struct dfll_rate_req req; 754 755 if (td->mode == DFLL_UNINITIALIZED) { 756 dev_err(td->dev, "%s: Cannot set DFLL rate in %s mode\n", 757 __func__, mode_name[td->mode]); 758 return -EPERM; 759 } 760 761 ret = dfll_calculate_rate_request(td, &req, rate); 762 if (ret) 763 return ret; 764 765 td->last_unrounded_rate = rate; 766 td->last_req = req; 767 768 if (td->mode == DFLL_CLOSED_LOOP) 769 dfll_set_frequency_request(td, &td->last_req); 770 771 return 0; 772 } 773 774 /* 775 * DFLL enable/disable & open-loop <-> closed-loop transitions 776 */ 777 778 /** 779 * dfll_disable - switch from open-loop mode to disabled mode 780 * @td: DFLL instance 781 * 782 * Switch from OPEN_LOOP state to DISABLED state. Returns 0 upon success 783 * or -EPERM if the DFLL is not currently in open-loop mode. 784 */ 785 static int dfll_disable(struct tegra_dfll *td) 786 { 787 if (td->mode != DFLL_OPEN_LOOP) { 788 dev_err(td->dev, "cannot disable DFLL in %s mode\n", 789 mode_name[td->mode]); 790 return -EINVAL; 791 } 792 793 dfll_set_mode(td, DFLL_DISABLED); 794 pm_runtime_put_sync(td->dev); 795 796 return 0; 797 } 798 799 /** 800 * dfll_enable - switch a disabled DFLL to open-loop mode 801 * @td: DFLL instance 802 * 803 * Switch from DISABLED state to OPEN_LOOP state. Returns 0 upon success 804 * or -EPERM if the DFLL is not currently disabled. 805 */ 806 static int dfll_enable(struct tegra_dfll *td) 807 { 808 if (td->mode != DFLL_DISABLED) { 809 dev_err(td->dev, "cannot enable DFLL in %s mode\n", 810 mode_name[td->mode]); 811 return -EPERM; 812 } 813 814 pm_runtime_get_sync(td->dev); 815 dfll_set_mode(td, DFLL_OPEN_LOOP); 816 817 return 0; 818 } 819 820 /** 821 * dfll_set_open_loop_config - prepare to switch to open-loop mode 822 * @td: DFLL instance 823 * 824 * Prepare to switch the DFLL to open-loop mode. This switches the 825 * DFLL to the low-voltage tuning range, ensures that I2C output 826 * forcing is disabled, and disables the output clock rate scaler. 827 * The DFLL's low-voltage tuning range parameters must be 828 * characterized to keep the downstream device stable at any DVCO 829 * input voltage. No return value. 830 */ 831 static void dfll_set_open_loop_config(struct tegra_dfll *td) 832 { 833 u32 val; 834 835 /* always tune low (safe) in open loop */ 836 if (td->tune_range != DFLL_TUNE_LOW) 837 dfll_tune_low(td); 838 839 val = dfll_readl(td, DFLL_FREQ_REQ); 840 val |= DFLL_FREQ_REQ_SCALE_MASK; 841 val &= ~DFLL_FREQ_REQ_FORCE_ENABLE; 842 dfll_writel(td, val, DFLL_FREQ_REQ); 843 dfll_wmb(td); 844 } 845 846 /** 847 * tegra_dfll_lock - switch from open-loop to closed-loop mode 848 * @td: DFLL instance 849 * 850 * Switch from OPEN_LOOP state to CLOSED_LOOP state. Returns 0 upon success, 851 * -EINVAL if the DFLL's target rate hasn't been set yet, or -EPERM if the 852 * DFLL is not currently in open-loop mode. 853 */ 854 static int dfll_lock(struct tegra_dfll *td) 855 { 856 struct dfll_rate_req *req = &td->last_req; 857 858 switch (td->mode) { 859 case DFLL_CLOSED_LOOP: 860 return 0; 861 862 case DFLL_OPEN_LOOP: 863 if (req->rate == 0) { 864 dev_err(td->dev, "%s: Cannot lock DFLL at rate 0\n", 865 __func__); 866 return -EINVAL; 867 } 868 869 dfll_i2c_set_output_enabled(td, true); 870 dfll_set_mode(td, DFLL_CLOSED_LOOP); 871 dfll_set_frequency_request(td, req); 872 return 0; 873 874 default: 875 BUG_ON(td->mode > DFLL_CLOSED_LOOP); 876 dev_err(td->dev, "%s: Cannot lock DFLL in %s mode\n", 877 __func__, mode_name[td->mode]); 878 return -EPERM; 879 } 880 } 881 882 /** 883 * tegra_dfll_unlock - switch from closed-loop to open-loop mode 884 * @td: DFLL instance 885 * 886 * Switch from CLOSED_LOOP state to OPEN_LOOP state. Returns 0 upon success, 887 * or -EPERM if the DFLL is not currently in open-loop mode. 888 */ 889 static int dfll_unlock(struct tegra_dfll *td) 890 { 891 switch (td->mode) { 892 case DFLL_CLOSED_LOOP: 893 dfll_set_open_loop_config(td); 894 dfll_set_mode(td, DFLL_OPEN_LOOP); 895 dfll_i2c_set_output_enabled(td, false); 896 return 0; 897 898 case DFLL_OPEN_LOOP: 899 return 0; 900 901 default: 902 BUG_ON(td->mode > DFLL_CLOSED_LOOP); 903 dev_err(td->dev, "%s: Cannot unlock DFLL in %s mode\n", 904 __func__, mode_name[td->mode]); 905 return -EPERM; 906 } 907 } 908 909 /* 910 * Clock framework integration 911 * 912 * When the DFLL is being controlled by the CCF, always enter closed loop 913 * mode when the clk is enabled. This requires that a DFLL rate request 914 * has been set beforehand, which implies that a clk_set_rate() call is 915 * always required before a clk_enable(). 916 */ 917 918 static int dfll_clk_is_enabled(struct clk_hw *hw) 919 { 920 struct tegra_dfll *td = clk_hw_to_dfll(hw); 921 922 return dfll_is_running(td); 923 } 924 925 static int dfll_clk_enable(struct clk_hw *hw) 926 { 927 struct tegra_dfll *td = clk_hw_to_dfll(hw); 928 int ret; 929 930 ret = dfll_enable(td); 931 if (ret) 932 return ret; 933 934 ret = dfll_lock(td); 935 if (ret) 936 dfll_disable(td); 937 938 return ret; 939 } 940 941 static void dfll_clk_disable(struct clk_hw *hw) 942 { 943 struct tegra_dfll *td = clk_hw_to_dfll(hw); 944 int ret; 945 946 ret = dfll_unlock(td); 947 if (!ret) 948 dfll_disable(td); 949 } 950 951 static unsigned long dfll_clk_recalc_rate(struct clk_hw *hw, 952 unsigned long parent_rate) 953 { 954 struct tegra_dfll *td = clk_hw_to_dfll(hw); 955 956 return td->last_unrounded_rate; 957 } 958 959 /* Must use determine_rate since it allows for rates exceeding 2^31-1 */ 960 static int dfll_clk_determine_rate(struct clk_hw *hw, 961 struct clk_rate_request *clk_req) 962 { 963 struct tegra_dfll *td = clk_hw_to_dfll(hw); 964 struct dfll_rate_req req; 965 int ret; 966 967 ret = dfll_calculate_rate_request(td, &req, clk_req->rate); 968 if (ret) 969 return ret; 970 971 /* 972 * Don't set the rounded rate, since it doesn't really matter as 973 * the output rate will be voltage controlled anyway, and cpufreq 974 * freaks out if any rounding happens. 975 */ 976 977 return 0; 978 } 979 980 static int dfll_clk_set_rate(struct clk_hw *hw, unsigned long rate, 981 unsigned long parent_rate) 982 { 983 struct tegra_dfll *td = clk_hw_to_dfll(hw); 984 985 return dfll_request_rate(td, rate); 986 } 987 988 static const struct clk_ops dfll_clk_ops = { 989 .is_enabled = dfll_clk_is_enabled, 990 .enable = dfll_clk_enable, 991 .disable = dfll_clk_disable, 992 .recalc_rate = dfll_clk_recalc_rate, 993 .determine_rate = dfll_clk_determine_rate, 994 .set_rate = dfll_clk_set_rate, 995 }; 996 997 static struct clk_init_data dfll_clk_init_data = { 998 .ops = &dfll_clk_ops, 999 .num_parents = 0, 1000 }; 1001 1002 /** 1003 * dfll_register_clk - register the DFLL output clock with the clock framework 1004 * @td: DFLL instance 1005 * 1006 * Register the DFLL's output clock with the Linux clock framework and register 1007 * the DFLL driver as an OF clock provider. Returns 0 upon success or -EINVAL 1008 * or -ENOMEM upon failure. 1009 */ 1010 static int dfll_register_clk(struct tegra_dfll *td) 1011 { 1012 int ret; 1013 1014 dfll_clk_init_data.name = td->output_clock_name; 1015 td->dfll_clk_hw.init = &dfll_clk_init_data; 1016 1017 td->dfll_clk = clk_register(td->dev, &td->dfll_clk_hw); 1018 if (IS_ERR(td->dfll_clk)) { 1019 dev_err(td->dev, "DFLL clock registration error\n"); 1020 return -EINVAL; 1021 } 1022 1023 ret = of_clk_add_provider(td->dev->of_node, of_clk_src_simple_get, 1024 td->dfll_clk); 1025 if (ret) { 1026 dev_err(td->dev, "of_clk_add_provider() failed\n"); 1027 1028 clk_unregister(td->dfll_clk); 1029 return ret; 1030 } 1031 1032 return 0; 1033 } 1034 1035 /** 1036 * dfll_unregister_clk - unregister the DFLL output clock 1037 * @td: DFLL instance 1038 * 1039 * Unregister the DFLL's output clock from the Linux clock framework 1040 * and from clkdev. No return value. 1041 */ 1042 static void dfll_unregister_clk(struct tegra_dfll *td) 1043 { 1044 of_clk_del_provider(td->dev->of_node); 1045 clk_unregister(td->dfll_clk); 1046 td->dfll_clk = NULL; 1047 } 1048 1049 /* 1050 * Debugfs interface 1051 */ 1052 1053 #ifdef CONFIG_DEBUG_FS 1054 /* 1055 * Monitor control 1056 */ 1057 1058 /** 1059 * dfll_calc_monitored_rate - convert DFLL_MONITOR_DATA_VAL rate into real freq 1060 * @monitor_data: value read from the DFLL_MONITOR_DATA_VAL bitfield 1061 * @ref_rate: DFLL reference clock rate 1062 * 1063 * Convert @monitor_data from DFLL_MONITOR_DATA_VAL units into cycles 1064 * per second. Returns the converted value. 1065 */ 1066 static u64 dfll_calc_monitored_rate(u32 monitor_data, 1067 unsigned long ref_rate) 1068 { 1069 return monitor_data * (ref_rate / REF_CLK_CYC_PER_DVCO_SAMPLE); 1070 } 1071 1072 /** 1073 * dfll_read_monitor_rate - return the DFLL's output rate from internal monitor 1074 * @td: DFLL instance 1075 * 1076 * If the DFLL is enabled, return the last rate reported by the DFLL's 1077 * internal monitoring hardware. This works in both open-loop and 1078 * closed-loop mode, and takes the output scaler setting into account. 1079 * Assumes that the monitor was programmed to monitor frequency before 1080 * the sample period started. If the driver believes that the DFLL is 1081 * currently uninitialized or disabled, it will return 0, since 1082 * otherwise the DFLL monitor data register will return the last 1083 * measured rate from when the DFLL was active. 1084 */ 1085 static u64 dfll_read_monitor_rate(struct tegra_dfll *td) 1086 { 1087 u32 v, s; 1088 u64 pre_scaler_rate, post_scaler_rate; 1089 1090 if (!dfll_is_running(td)) 1091 return 0; 1092 1093 v = dfll_readl(td, DFLL_MONITOR_DATA); 1094 v = (v & DFLL_MONITOR_DATA_VAL_MASK) >> DFLL_MONITOR_DATA_VAL_SHIFT; 1095 pre_scaler_rate = dfll_calc_monitored_rate(v, td->ref_rate); 1096 1097 s = dfll_readl(td, DFLL_FREQ_REQ); 1098 s = (s & DFLL_FREQ_REQ_SCALE_MASK) >> DFLL_FREQ_REQ_SCALE_SHIFT; 1099 post_scaler_rate = dfll_scale_dvco_rate(s, pre_scaler_rate); 1100 1101 return post_scaler_rate; 1102 } 1103 1104 static int attr_enable_get(void *data, u64 *val) 1105 { 1106 struct tegra_dfll *td = data; 1107 1108 *val = dfll_is_running(td); 1109 1110 return 0; 1111 } 1112 static int attr_enable_set(void *data, u64 val) 1113 { 1114 struct tegra_dfll *td = data; 1115 1116 return val ? dfll_enable(td) : dfll_disable(td); 1117 } 1118 DEFINE_SIMPLE_ATTRIBUTE(enable_fops, attr_enable_get, attr_enable_set, 1119 "%llu\n"); 1120 1121 static int attr_lock_get(void *data, u64 *val) 1122 { 1123 struct tegra_dfll *td = data; 1124 1125 *val = (td->mode == DFLL_CLOSED_LOOP); 1126 1127 return 0; 1128 } 1129 static int attr_lock_set(void *data, u64 val) 1130 { 1131 struct tegra_dfll *td = data; 1132 1133 return val ? dfll_lock(td) : dfll_unlock(td); 1134 } 1135 DEFINE_SIMPLE_ATTRIBUTE(lock_fops, attr_lock_get, attr_lock_set, 1136 "%llu\n"); 1137 1138 static int attr_rate_get(void *data, u64 *val) 1139 { 1140 struct tegra_dfll *td = data; 1141 1142 *val = dfll_read_monitor_rate(td); 1143 1144 return 0; 1145 } 1146 1147 static int attr_rate_set(void *data, u64 val) 1148 { 1149 struct tegra_dfll *td = data; 1150 1151 return dfll_request_rate(td, val); 1152 } 1153 DEFINE_SIMPLE_ATTRIBUTE(rate_fops, attr_rate_get, attr_rate_set, "%llu\n"); 1154 1155 static int attr_registers_show(struct seq_file *s, void *data) 1156 { 1157 u32 val, offs; 1158 struct tegra_dfll *td = s->private; 1159 1160 seq_puts(s, "CONTROL REGISTERS:\n"); 1161 for (offs = 0; offs <= DFLL_MONITOR_DATA; offs += 4) { 1162 if (offs == DFLL_OUTPUT_CFG) 1163 val = dfll_i2c_readl(td, offs); 1164 else 1165 val = dfll_readl(td, offs); 1166 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, val); 1167 } 1168 1169 seq_puts(s, "\nI2C and INTR REGISTERS:\n"); 1170 for (offs = DFLL_I2C_CFG; offs <= DFLL_I2C_STS; offs += 4) 1171 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, 1172 dfll_i2c_readl(td, offs)); 1173 for (offs = DFLL_INTR_STS; offs <= DFLL_INTR_EN; offs += 4) 1174 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, 1175 dfll_i2c_readl(td, offs)); 1176 1177 seq_puts(s, "\nINTEGRATED I2C CONTROLLER REGISTERS:\n"); 1178 offs = DFLL_I2C_CLK_DIVISOR; 1179 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, 1180 __raw_readl(td->i2c_controller_base + offs)); 1181 1182 seq_puts(s, "\nLUT:\n"); 1183 for (offs = 0; offs < 4 * MAX_DFLL_VOLTAGES; offs += 4) 1184 seq_printf(s, "[0x%02x] = 0x%08x\n", offs, 1185 __raw_readl(td->lut_base + offs)); 1186 1187 return 0; 1188 } 1189 1190 static int attr_registers_open(struct inode *inode, struct file *file) 1191 { 1192 return single_open(file, attr_registers_show, inode->i_private); 1193 } 1194 1195 static const struct file_operations attr_registers_fops = { 1196 .open = attr_registers_open, 1197 .read = seq_read, 1198 .llseek = seq_lseek, 1199 .release = single_release, 1200 }; 1201 1202 static int dfll_debug_init(struct tegra_dfll *td) 1203 { 1204 int ret; 1205 1206 if (!td || (td->mode == DFLL_UNINITIALIZED)) 1207 return 0; 1208 1209 td->debugfs_dir = debugfs_create_dir("tegra_dfll_fcpu", NULL); 1210 if (!td->debugfs_dir) 1211 return -ENOMEM; 1212 1213 ret = -ENOMEM; 1214 1215 if (!debugfs_create_file("enable", S_IRUGO | S_IWUSR, 1216 td->debugfs_dir, td, &enable_fops)) 1217 goto err_out; 1218 1219 if (!debugfs_create_file("lock", S_IRUGO, 1220 td->debugfs_dir, td, &lock_fops)) 1221 goto err_out; 1222 1223 if (!debugfs_create_file("rate", S_IRUGO, 1224 td->debugfs_dir, td, &rate_fops)) 1225 goto err_out; 1226 1227 if (!debugfs_create_file("registers", S_IRUGO, 1228 td->debugfs_dir, td, &attr_registers_fops)) 1229 goto err_out; 1230 1231 return 0; 1232 1233 err_out: 1234 debugfs_remove_recursive(td->debugfs_dir); 1235 return ret; 1236 } 1237 1238 #endif /* CONFIG_DEBUG_FS */ 1239 1240 /* 1241 * DFLL initialization 1242 */ 1243 1244 /** 1245 * dfll_set_default_params - program non-output related DFLL parameters 1246 * @td: DFLL instance 1247 * 1248 * During DFLL driver initialization or resume from context loss, 1249 * program parameters for the closed loop integrator, DVCO tuning, 1250 * voltage droop control and monitor control. 1251 */ 1252 static void dfll_set_default_params(struct tegra_dfll *td) 1253 { 1254 u32 val; 1255 1256 val = DIV_ROUND_UP(td->ref_rate, td->sample_rate * 32); 1257 BUG_ON(val > DFLL_CONFIG_DIV_MASK); 1258 dfll_writel(td, val, DFLL_CONFIG); 1259 1260 val = (td->force_mode << DFLL_PARAMS_FORCE_MODE_SHIFT) | 1261 (td->cf << DFLL_PARAMS_CF_PARAM_SHIFT) | 1262 (td->ci << DFLL_PARAMS_CI_PARAM_SHIFT) | 1263 (td->cg << DFLL_PARAMS_CG_PARAM_SHIFT) | 1264 (td->cg_scale ? DFLL_PARAMS_CG_SCALE : 0); 1265 dfll_writel(td, val, DFLL_PARAMS); 1266 1267 dfll_tune_low(td); 1268 dfll_writel(td, td->droop_ctrl, DFLL_DROOP_CTRL); 1269 dfll_writel(td, DFLL_MONITOR_CTRL_FREQ, DFLL_MONITOR_CTRL); 1270 } 1271 1272 /** 1273 * dfll_init_clks - clk_get() the DFLL source clocks 1274 * @td: DFLL instance 1275 * 1276 * Call clk_get() on the DFLL source clocks and save the pointers for later 1277 * use. Returns 0 upon success or error (see devm_clk_get) if one or more 1278 * of the clocks couldn't be looked up. 1279 */ 1280 static int dfll_init_clks(struct tegra_dfll *td) 1281 { 1282 td->ref_clk = devm_clk_get(td->dev, "ref"); 1283 if (IS_ERR(td->ref_clk)) { 1284 dev_err(td->dev, "missing ref clock\n"); 1285 return PTR_ERR(td->ref_clk); 1286 } 1287 1288 td->soc_clk = devm_clk_get(td->dev, "soc"); 1289 if (IS_ERR(td->soc_clk)) { 1290 dev_err(td->dev, "missing soc clock\n"); 1291 return PTR_ERR(td->soc_clk); 1292 } 1293 1294 td->i2c_clk = devm_clk_get(td->dev, "i2c"); 1295 if (IS_ERR(td->i2c_clk)) { 1296 dev_err(td->dev, "missing i2c clock\n"); 1297 return PTR_ERR(td->i2c_clk); 1298 } 1299 td->i2c_clk_rate = clk_get_rate(td->i2c_clk); 1300 1301 return 0; 1302 } 1303 1304 /** 1305 * dfll_init - Prepare the DFLL IP block for use 1306 * @td: DFLL instance 1307 * 1308 * Do everything necessary to prepare the DFLL IP block for use. The 1309 * DFLL will be left in DISABLED state. Called by dfll_probe(). 1310 * Returns 0 upon success, or passes along the error from whatever 1311 * function returned it. 1312 */ 1313 static int dfll_init(struct tegra_dfll *td) 1314 { 1315 int ret; 1316 1317 td->ref_rate = clk_get_rate(td->ref_clk); 1318 if (td->ref_rate != REF_CLOCK_RATE) { 1319 dev_err(td->dev, "unexpected ref clk rate %lu, expecting %lu", 1320 td->ref_rate, REF_CLOCK_RATE); 1321 return -EINVAL; 1322 } 1323 1324 reset_control_deassert(td->dvco_rst); 1325 1326 ret = clk_prepare(td->ref_clk); 1327 if (ret) { 1328 dev_err(td->dev, "failed to prepare ref_clk\n"); 1329 return ret; 1330 } 1331 1332 ret = clk_prepare(td->soc_clk); 1333 if (ret) { 1334 dev_err(td->dev, "failed to prepare soc_clk\n"); 1335 goto di_err1; 1336 } 1337 1338 ret = clk_prepare(td->i2c_clk); 1339 if (ret) { 1340 dev_err(td->dev, "failed to prepare i2c_clk\n"); 1341 goto di_err2; 1342 } 1343 1344 td->last_unrounded_rate = 0; 1345 1346 pm_runtime_enable(td->dev); 1347 pm_runtime_get_sync(td->dev); 1348 1349 dfll_set_mode(td, DFLL_DISABLED); 1350 dfll_set_default_params(td); 1351 1352 if (td->soc->init_clock_trimmers) 1353 td->soc->init_clock_trimmers(); 1354 1355 dfll_set_open_loop_config(td); 1356 1357 dfll_init_out_if(td); 1358 1359 pm_runtime_put_sync(td->dev); 1360 1361 return 0; 1362 1363 di_err2: 1364 clk_unprepare(td->soc_clk); 1365 di_err1: 1366 clk_unprepare(td->ref_clk); 1367 1368 reset_control_assert(td->dvco_rst); 1369 1370 return ret; 1371 } 1372 1373 /* 1374 * DT data fetch 1375 */ 1376 1377 /* 1378 * Find a PMIC voltage register-to-voltage mapping for the given voltage. 1379 * An exact voltage match is required. 1380 */ 1381 static int find_vdd_map_entry_exact(struct tegra_dfll *td, int uV) 1382 { 1383 int i, n_voltages, reg_uV; 1384 1385 n_voltages = regulator_count_voltages(td->vdd_reg); 1386 for (i = 0; i < n_voltages; i++) { 1387 reg_uV = regulator_list_voltage(td->vdd_reg, i); 1388 if (reg_uV < 0) 1389 break; 1390 1391 if (uV == reg_uV) 1392 return i; 1393 } 1394 1395 dev_err(td->dev, "no voltage map entry for %d uV\n", uV); 1396 return -EINVAL; 1397 } 1398 1399 /* 1400 * Find a PMIC voltage register-to-voltage mapping for the given voltage, 1401 * rounding up to the closest supported voltage. 1402 * */ 1403 static int find_vdd_map_entry_min(struct tegra_dfll *td, int uV) 1404 { 1405 int i, n_voltages, reg_uV; 1406 1407 n_voltages = regulator_count_voltages(td->vdd_reg); 1408 for (i = 0; i < n_voltages; i++) { 1409 reg_uV = regulator_list_voltage(td->vdd_reg, i); 1410 if (reg_uV < 0) 1411 break; 1412 1413 if (uV <= reg_uV) 1414 return i; 1415 } 1416 1417 dev_err(td->dev, "no voltage map entry rounding to %d uV\n", uV); 1418 return -EINVAL; 1419 } 1420 1421 /** 1422 * dfll_build_i2c_lut - build the I2C voltage register lookup table 1423 * @td: DFLL instance 1424 * 1425 * The DFLL hardware has 33 bytes of look-up table RAM that must be filled with 1426 * PMIC voltage register values that span the entire DFLL operating range. 1427 * This function builds the look-up table based on the OPP table provided by 1428 * the soc-specific platform driver (td->soc->opp_dev) and the PMIC 1429 * register-to-voltage mapping queried from the regulator framework. 1430 * 1431 * On success, fills in td->i2c_lut and returns 0, or -err on failure. 1432 */ 1433 static int dfll_build_i2c_lut(struct tegra_dfll *td) 1434 { 1435 int ret = -EINVAL; 1436 int j, v, v_max, v_opp; 1437 int selector; 1438 unsigned long rate; 1439 struct dev_pm_opp *opp; 1440 int lut; 1441 1442 rcu_read_lock(); 1443 1444 rate = ULONG_MAX; 1445 opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate); 1446 if (IS_ERR(opp)) { 1447 dev_err(td->dev, "couldn't get vmax opp, empty opp table?\n"); 1448 goto out; 1449 } 1450 v_max = dev_pm_opp_get_voltage(opp); 1451 1452 v = td->soc->min_millivolts * 1000; 1453 lut = find_vdd_map_entry_exact(td, v); 1454 if (lut < 0) 1455 goto out; 1456 td->i2c_lut[0] = lut; 1457 1458 for (j = 1, rate = 0; ; rate++) { 1459 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); 1460 if (IS_ERR(opp)) 1461 break; 1462 v_opp = dev_pm_opp_get_voltage(opp); 1463 1464 if (v_opp <= td->soc->min_millivolts * 1000) 1465 td->dvco_rate_min = dev_pm_opp_get_freq(opp); 1466 1467 for (;;) { 1468 v += max(1, (v_max - v) / (MAX_DFLL_VOLTAGES - j)); 1469 if (v >= v_opp) 1470 break; 1471 1472 selector = find_vdd_map_entry_min(td, v); 1473 if (selector < 0) 1474 goto out; 1475 if (selector != td->i2c_lut[j - 1]) 1476 td->i2c_lut[j++] = selector; 1477 } 1478 1479 v = (j == MAX_DFLL_VOLTAGES - 1) ? v_max : v_opp; 1480 selector = find_vdd_map_entry_exact(td, v); 1481 if (selector < 0) 1482 goto out; 1483 if (selector != td->i2c_lut[j - 1]) 1484 td->i2c_lut[j++] = selector; 1485 1486 if (v >= v_max) 1487 break; 1488 } 1489 td->i2c_lut_size = j; 1490 1491 if (!td->dvco_rate_min) 1492 dev_err(td->dev, "no opp above DFLL minimum voltage %d mV\n", 1493 td->soc->min_millivolts); 1494 else 1495 ret = 0; 1496 1497 out: 1498 rcu_read_unlock(); 1499 1500 return ret; 1501 } 1502 1503 /** 1504 * read_dt_param - helper function for reading required parameters from the DT 1505 * @td: DFLL instance 1506 * @param: DT property name 1507 * @dest: output pointer for the value read 1508 * 1509 * Read a required numeric parameter from the DFLL device node, or complain 1510 * if the property doesn't exist. Returns a boolean indicating success for 1511 * easy chaining of multiple calls to this function. 1512 */ 1513 static bool read_dt_param(struct tegra_dfll *td, const char *param, u32 *dest) 1514 { 1515 int err = of_property_read_u32(td->dev->of_node, param, dest); 1516 1517 if (err < 0) { 1518 dev_err(td->dev, "failed to read DT parameter %s: %d\n", 1519 param, err); 1520 return false; 1521 } 1522 1523 return true; 1524 } 1525 1526 /** 1527 * dfll_fetch_i2c_params - query PMIC I2C params from DT & regulator subsystem 1528 * @td: DFLL instance 1529 * 1530 * Read all the parameters required for operation in I2C mode. The parameters 1531 * can originate from the device tree or the regulator subsystem. 1532 * Returns 0 on success or -err on failure. 1533 */ 1534 static int dfll_fetch_i2c_params(struct tegra_dfll *td) 1535 { 1536 struct regmap *regmap; 1537 struct device *i2c_dev; 1538 struct i2c_client *i2c_client; 1539 int vsel_reg, vsel_mask; 1540 int ret; 1541 1542 if (!read_dt_param(td, "nvidia,i2c-fs-rate", &td->i2c_fs_rate)) 1543 return -EINVAL; 1544 1545 regmap = regulator_get_regmap(td->vdd_reg); 1546 i2c_dev = regmap_get_device(regmap); 1547 i2c_client = to_i2c_client(i2c_dev); 1548 1549 td->i2c_slave_addr = i2c_client->addr; 1550 1551 ret = regulator_get_hardware_vsel_register(td->vdd_reg, 1552 &vsel_reg, 1553 &vsel_mask); 1554 if (ret < 0) { 1555 dev_err(td->dev, 1556 "regulator unsuitable for DFLL I2C operation\n"); 1557 return -EINVAL; 1558 } 1559 td->i2c_reg = vsel_reg; 1560 1561 ret = dfll_build_i2c_lut(td); 1562 if (ret) { 1563 dev_err(td->dev, "couldn't build I2C LUT\n"); 1564 return ret; 1565 } 1566 1567 return 0; 1568 } 1569 1570 /** 1571 * dfll_fetch_common_params - read DFLL parameters from the device tree 1572 * @td: DFLL instance 1573 * 1574 * Read all the DT parameters that are common to both I2C and PWM operation. 1575 * Returns 0 on success or -EINVAL on any failure. 1576 */ 1577 static int dfll_fetch_common_params(struct tegra_dfll *td) 1578 { 1579 bool ok = true; 1580 1581 ok &= read_dt_param(td, "nvidia,droop-ctrl", &td->droop_ctrl); 1582 ok &= read_dt_param(td, "nvidia,sample-rate", &td->sample_rate); 1583 ok &= read_dt_param(td, "nvidia,force-mode", &td->force_mode); 1584 ok &= read_dt_param(td, "nvidia,cf", &td->cf); 1585 ok &= read_dt_param(td, "nvidia,ci", &td->ci); 1586 ok &= read_dt_param(td, "nvidia,cg", &td->cg); 1587 td->cg_scale = of_property_read_bool(td->dev->of_node, 1588 "nvidia,cg-scale"); 1589 1590 if (of_property_read_string(td->dev->of_node, "clock-output-names", 1591 &td->output_clock_name)) { 1592 dev_err(td->dev, "missing clock-output-names property\n"); 1593 ok = false; 1594 } 1595 1596 return ok ? 0 : -EINVAL; 1597 } 1598 1599 /* 1600 * API exported to per-SoC platform drivers 1601 */ 1602 1603 /** 1604 * tegra_dfll_register - probe a Tegra DFLL device 1605 * @pdev: DFLL platform_device * 1606 * @soc: Per-SoC integration and characterization data for this DFLL instance 1607 * 1608 * Probe and initialize a DFLL device instance. Intended to be called 1609 * by a SoC-specific shim driver that passes in per-SoC integration 1610 * and configuration data via @soc. Returns 0 on success or -err on failure. 1611 */ 1612 int tegra_dfll_register(struct platform_device *pdev, 1613 struct tegra_dfll_soc_data *soc) 1614 { 1615 struct resource *mem; 1616 struct tegra_dfll *td; 1617 int ret; 1618 1619 if (!soc) { 1620 dev_err(&pdev->dev, "no tegra_dfll_soc_data provided\n"); 1621 return -EINVAL; 1622 } 1623 1624 td = devm_kzalloc(&pdev->dev, sizeof(*td), GFP_KERNEL); 1625 if (!td) 1626 return -ENOMEM; 1627 td->dev = &pdev->dev; 1628 platform_set_drvdata(pdev, td); 1629 1630 td->soc = soc; 1631 1632 td->vdd_reg = devm_regulator_get(td->dev, "vdd-cpu"); 1633 if (IS_ERR(td->vdd_reg)) { 1634 dev_err(td->dev, "couldn't get vdd_cpu regulator\n"); 1635 return PTR_ERR(td->vdd_reg); 1636 } 1637 1638 td->dvco_rst = devm_reset_control_get(td->dev, "dvco"); 1639 if (IS_ERR(td->dvco_rst)) { 1640 dev_err(td->dev, "couldn't get dvco reset\n"); 1641 return PTR_ERR(td->dvco_rst); 1642 } 1643 1644 ret = dfll_fetch_common_params(td); 1645 if (ret) { 1646 dev_err(td->dev, "couldn't parse device tree parameters\n"); 1647 return ret; 1648 } 1649 1650 ret = dfll_fetch_i2c_params(td); 1651 if (ret) 1652 return ret; 1653 1654 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1655 if (!mem) { 1656 dev_err(td->dev, "no control register resource\n"); 1657 return -ENODEV; 1658 } 1659 1660 td->base = devm_ioremap(td->dev, mem->start, resource_size(mem)); 1661 if (!td->base) { 1662 dev_err(td->dev, "couldn't ioremap DFLL control registers\n"); 1663 return -ENODEV; 1664 } 1665 1666 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1667 if (!mem) { 1668 dev_err(td->dev, "no i2c_base resource\n"); 1669 return -ENODEV; 1670 } 1671 1672 td->i2c_base = devm_ioremap(td->dev, mem->start, resource_size(mem)); 1673 if (!td->i2c_base) { 1674 dev_err(td->dev, "couldn't ioremap i2c_base resource\n"); 1675 return -ENODEV; 1676 } 1677 1678 mem = platform_get_resource(pdev, IORESOURCE_MEM, 2); 1679 if (!mem) { 1680 dev_err(td->dev, "no i2c_controller_base resource\n"); 1681 return -ENODEV; 1682 } 1683 1684 td->i2c_controller_base = devm_ioremap(td->dev, mem->start, 1685 resource_size(mem)); 1686 if (!td->i2c_controller_base) { 1687 dev_err(td->dev, 1688 "couldn't ioremap i2c_controller_base resource\n"); 1689 return -ENODEV; 1690 } 1691 1692 mem = platform_get_resource(pdev, IORESOURCE_MEM, 3); 1693 if (!mem) { 1694 dev_err(td->dev, "no lut_base resource\n"); 1695 return -ENODEV; 1696 } 1697 1698 td->lut_base = devm_ioremap(td->dev, mem->start, resource_size(mem)); 1699 if (!td->lut_base) { 1700 dev_err(td->dev, 1701 "couldn't ioremap lut_base resource\n"); 1702 return -ENODEV; 1703 } 1704 1705 ret = dfll_init_clks(td); 1706 if (ret) { 1707 dev_err(&pdev->dev, "DFLL clock init error\n"); 1708 return ret; 1709 } 1710 1711 /* Enable the clocks and set the device up */ 1712 ret = dfll_init(td); 1713 if (ret) 1714 return ret; 1715 1716 ret = dfll_register_clk(td); 1717 if (ret) { 1718 dev_err(&pdev->dev, "DFLL clk registration failed\n"); 1719 return ret; 1720 } 1721 1722 #ifdef CONFIG_DEBUG_FS 1723 dfll_debug_init(td); 1724 #endif 1725 1726 return 0; 1727 } 1728 EXPORT_SYMBOL(tegra_dfll_register); 1729 1730 /** 1731 * tegra_dfll_unregister - release all of the DFLL driver resources for a device 1732 * @pdev: DFLL platform_device * 1733 * 1734 * Unbind this driver from the DFLL hardware device represented by 1735 * @pdev. The DFLL must be disabled for this to succeed. Returns 0 1736 * upon success or -EBUSY if the DFLL is still active. 1737 */ 1738 int tegra_dfll_unregister(struct platform_device *pdev) 1739 { 1740 struct tegra_dfll *td = platform_get_drvdata(pdev); 1741 1742 /* Try to prevent removal while the DFLL is active */ 1743 if (td->mode != DFLL_DISABLED) { 1744 dev_err(&pdev->dev, 1745 "must disable DFLL before removing driver\n"); 1746 return -EBUSY; 1747 } 1748 1749 debugfs_remove_recursive(td->debugfs_dir); 1750 1751 dfll_unregister_clk(td); 1752 pm_runtime_disable(&pdev->dev); 1753 1754 clk_unprepare(td->ref_clk); 1755 clk_unprepare(td->soc_clk); 1756 clk_unprepare(td->i2c_clk); 1757 1758 reset_control_assert(td->dvco_rst); 1759 1760 return 0; 1761 } 1762 EXPORT_SYMBOL(tegra_dfll_unregister); 1763