0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
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
0054
0055
0056
0057 #define DFLL_CTRL 0x00
0058 #define DFLL_CTRL_MODE_MASK 0x03
0059
0060
0061 #define DFLL_CONFIG 0x04
0062 #define DFLL_CONFIG_DIV_MASK 0xff
0063 #define DFLL_CONFIG_DIV_PRESCALE 32
0064
0065
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
0078 #define DFLL_TUNE0 0x0c
0079
0080
0081 #define DFLL_TUNE1 0x10
0082
0083
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
0099 #define DFLL_DROOP_CTRL 0x1c
0100
0101
0102
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
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
0129 #define DFLL_MONITOR_CTRL 0x28
0130 #define DFLL_MONITOR_CTRL_FREQ 6
0131
0132
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
0140
0141
0142
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
0155 #define DFLL_I2C_VDD_REG_ADDR 0x44
0156
0157
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
0163 #define DFLL_INTR_STS 0x5c
0164
0165
0166 #define DFLL_INTR_EN 0x60
0167 #define DFLL_INTR_MIN_MASK 0x1
0168 #define DFLL_INTR_MAX_MASK 0x2
0169
0170
0171
0172
0173
0174
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
0184
0185
0186
0187 #define MAX_DFLL_VOLTAGES 33
0188
0189
0190
0191
0192
0193
0194 #define REF_CLK_CYC_PER_DVCO_SAMPLE 4
0195
0196
0197
0198
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
0207
0208
0209
0210
0211
0212
0213
0214
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
0225
0226
0227
0228
0229
0230
0231
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
0246
0247
0248
0249
0250
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
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
0298 u32 i2c_fs_rate;
0299 u32 i2c_reg;
0300 u32 i2c_slave_addr;
0301
0302
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
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
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
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
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
0366
0367
0368
0369
0370
0371 static bool dfll_is_running(struct tegra_dfll *td)
0372 {
0373 return td->mode >= DFLL_OPEN_LOOP;
0374 }
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
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
0421
0422
0423
0424
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
0440
0441
0442
0443
0444
0445
0446
0447
0448
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
0464
0465
0466
0467
0468
0469
0470
0471
0472
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
0482
0483
0484
0485
0486
0487
0488
0489
0490
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
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
0530
0531
0532
0533
0534
0535
0536
0537
0538
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
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
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
0609
0610
0611
0612
0613
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
0628
0629
0630
0631
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
0648
0649
0650
0651
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
0671
0672
0673
0674
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
0699
0700
0701
0702
0703
0704
0705
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
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
0735
0736
0737
0738
0739
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
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
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
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
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
0818
0819
0820
0821
0822
0823
0824
0825
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
0835
0836
0837
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
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
0874
0875
0876
0877
0878
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; ;
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
0902
0903
0904
0905
0906
0907
0908
0909
0910
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
0938
0939
0940
0941
0942
0943
0944
0945
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
0963
0964
0965
0966
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
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993 static void dfll_set_open_loop_config(struct tegra_dfll *td)
0994 {
0995 u32 val;
0996
0997
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
1010
1011
1012
1013
1014
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
1051
1052
1053
1054
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
1081
1082
1083
1084
1085
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
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
1143
1144
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
1174
1175
1176
1177
1178
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
1207
1208
1209
1210
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
1221
1222
1223 #ifdef CONFIG_DEBUG_FS
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
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
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
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
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
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
1418
1419
1420
1421
1422
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
1450
1451
1452
1453
1454
1455
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
1521
1522
1523
1524
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
1544
1545
1546
1547
1548
1549
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
1578
1579
1580
1581
1582
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
1610
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
1638
1639
1640
1641
1642
1643
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
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
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
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
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
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
1787
1788
1789
1790
1791
1792
1793
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
1810
1811
1812
1813
1814
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
1899
1900
1901
1902
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
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
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
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
2074
2075
2076
2077
2078
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
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);