2 * SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation
7 #include <linux/clk-provider.h>
8 #include <linux/iopoll.h>
14 * DSI PLL 7nm - clock diagram (eg: DSI0): TODO: updated CPHY diagram
16 * dsi0_pll_out_div_clk dsi0_pll_bit_clk
19 * +---------+ | +----------+ | +----+
20 * dsi0vco_clk ---| out_div |--o--| divl_3_0 |--o--| /8 |-- dsi0_phy_pll_out_byteclk
21 * +---------+ | +----------+ | +----+
23 * | | dsi0_pll_by_2_bit_clk
25 * | | +----+ | |\ dsi0_pclk_mux
26 * | |--| /2 |--o--| \ |
27 * | | +----+ | \ | +---------+
28 * | --------------| |--o--| div_7_4 |-- dsi0_phy_pll_out_dsiclk
29 * |------------------------------| / +---------+
31 * -----------| /4? |--o----------|/
35 * dsi0_pll_post_out_div_clk
38 #define VCO_REF_CLK_RATE 19200000
41 u32 pll_prop_gain_rate;
43 u32 decimal_div_start;
44 u32 frac_div_start_low;
45 u32 frac_div_start_mid;
46 u32 frac_div_start_high;
47 u32 pll_clock_inverters;
49 u32 ssc_stepsize_high;
57 /* Hardware is V4.1 */
58 #define DSI_PHY_7NM_QUIRK_V4_1 BIT(0)
60 struct dsi_pll_config {
65 bool disable_prescaler;
78 struct pll_7nm_cached_state {
79 unsigned long vco_rate;
89 struct msm_dsi_phy *phy;
94 /* protects REG_DSI_7nm_PHY_CMN_CLK_CFG0 register */
95 spinlock_t postdiv_lock;
97 struct dsi_pll_config pll_configuration;
98 struct dsi_pll_regs reg_setup;
100 struct pll_7nm_cached_state cached_state;
102 struct dsi_pll_7nm *slave;
105 #define to_pll_7nm(x) container_of(x, struct dsi_pll_7nm, clk_hw)
108 * Global list of private DSI PLL struct pointers. We need this for Dual DSI
109 * mode, where the master PLL's clk_ops needs access the slave's private data
111 static struct dsi_pll_7nm *pll_7nm_list[DSI_MAX];
113 static void dsi_pll_setup_config(struct dsi_pll_7nm *pll)
115 struct dsi_pll_config *config = &pll->pll_configuration;
117 config->ref_freq = pll->vco_ref_clk_rate;
118 config->output_div = 1;
119 config->dec_bits = 8;
120 config->frac_bits = 18;
121 config->lock_timer = 64;
122 config->ssc_freq = 31500;
123 config->ssc_offset = 4800;
124 config->ssc_adj_per = 2;
125 config->thresh_cycles = 32;
126 config->refclk_cycles = 256;
128 config->div_override = false;
129 config->ignore_frac = false;
130 config->disable_prescaler = false;
132 /* TODO: ssc enable */
133 config->enable_ssc = false;
134 config->ssc_center = 0;
137 static void dsi_pll_calc_dec_frac(struct dsi_pll_7nm *pll)
139 struct dsi_pll_config *config = &pll->pll_configuration;
140 struct dsi_pll_regs *regs = &pll->reg_setup;
141 u64 fref = pll->vco_ref_clk_rate;
144 u64 dec, dec_multiple;
148 pll_freq = pll->vco_current_rate;
150 if (config->disable_prescaler)
155 multiplier = 1 << config->frac_bits;
156 dec_multiple = div_u64(pll_freq * multiplier, divider);
157 div_u64_rem(dec_multiple, multiplier, &frac);
159 dec = div_u64(dec_multiple, multiplier);
161 if (!(pll->phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1))
162 regs->pll_clock_inverters = 0x28;
163 else if (pll_freq <= 1000000000ULL)
164 regs->pll_clock_inverters = 0xa0;
165 else if (pll_freq <= 2500000000ULL)
166 regs->pll_clock_inverters = 0x20;
167 else if (pll_freq <= 3020000000ULL)
168 regs->pll_clock_inverters = 0x00;
170 regs->pll_clock_inverters = 0x40;
172 regs->pll_lockdet_rate = config->lock_timer;
173 regs->decimal_div_start = dec;
174 regs->frac_div_start_low = (frac & 0xff);
175 regs->frac_div_start_mid = (frac & 0xff00) >> 8;
176 regs->frac_div_start_high = (frac & 0x30000) >> 16;
179 #define SSC_CENTER BIT(0)
180 #define SSC_EN BIT(1)
182 static void dsi_pll_calc_ssc(struct dsi_pll_7nm *pll)
184 struct dsi_pll_config *config = &pll->pll_configuration;
185 struct dsi_pll_regs *regs = &pll->reg_setup;
191 if (!config->enable_ssc) {
192 DBG("SSC not enabled\n");
196 ssc_per = DIV_ROUND_CLOSEST(config->ref_freq, config->ssc_freq) / 2 - 1;
197 ssc_mod = (ssc_per + 1) % (config->ssc_adj_per + 1);
200 frac = regs->frac_div_start_low |
201 (regs->frac_div_start_mid << 8) |
202 (regs->frac_div_start_high << 16);
203 ssc_step_size = regs->decimal_div_start;
204 ssc_step_size *= (1 << config->frac_bits);
205 ssc_step_size += frac;
206 ssc_step_size *= config->ssc_offset;
207 ssc_step_size *= (config->ssc_adj_per + 1);
208 ssc_step_size = div_u64(ssc_step_size, (ssc_per + 1));
209 ssc_step_size = DIV_ROUND_CLOSEST_ULL(ssc_step_size, 1000000);
211 regs->ssc_div_per_low = ssc_per & 0xFF;
212 regs->ssc_div_per_high = (ssc_per & 0xFF00) >> 8;
213 regs->ssc_stepsize_low = (u32)(ssc_step_size & 0xFF);
214 regs->ssc_stepsize_high = (u32)((ssc_step_size & 0xFF00) >> 8);
215 regs->ssc_adjper_low = config->ssc_adj_per & 0xFF;
216 regs->ssc_adjper_high = (config->ssc_adj_per & 0xFF00) >> 8;
218 regs->ssc_control = config->ssc_center ? SSC_CENTER : 0;
220 pr_debug("SCC: Dec:%d, frac:%llu, frac_bits:%d\n",
221 regs->decimal_div_start, frac, config->frac_bits);
222 pr_debug("SSC: div_per:0x%X, stepsize:0x%X, adjper:0x%X\n",
223 ssc_per, (u32)ssc_step_size, config->ssc_adj_per);
226 static void dsi_pll_ssc_commit(struct dsi_pll_7nm *pll)
228 void __iomem *base = pll->phy->pll_base;
229 struct dsi_pll_regs *regs = &pll->reg_setup;
231 if (pll->pll_configuration.enable_ssc) {
232 pr_debug("SSC is enabled\n");
234 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_STEPSIZE_LOW_1,
235 regs->ssc_stepsize_low);
236 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_STEPSIZE_HIGH_1,
237 regs->ssc_stepsize_high);
238 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_DIV_PER_LOW_1,
239 regs->ssc_div_per_low);
240 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_DIV_PER_HIGH_1,
241 regs->ssc_div_per_high);
242 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_ADJPER_LOW_1,
243 regs->ssc_adjper_low);
244 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_ADJPER_HIGH_1,
245 regs->ssc_adjper_high);
246 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_SSC_CONTROL,
247 SSC_EN | regs->ssc_control);
251 static void dsi_pll_config_hzindep_reg(struct dsi_pll_7nm *pll)
253 void __iomem *base = pll->phy->pll_base;
254 u8 analog_controls_five_1 = 0x01, vco_config_1 = 0x00;
256 if (pll->phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1) {
257 if (pll->vco_current_rate >= 3100000000ULL)
258 analog_controls_five_1 = 0x03;
260 if (pll->vco_current_rate < 1520000000ULL)
262 else if (pll->vco_current_rate < 2990000000ULL)
266 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_ANALOG_CONTROLS_FIVE_1,
267 analog_controls_five_1);
268 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_VCO_CONFIG_1, vco_config_1);
269 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_ANALOG_CONTROLS_FIVE, 0x01);
270 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_ANALOG_CONTROLS_TWO, 0x03);
271 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_ANALOG_CONTROLS_THREE, 0x00);
272 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_DSM_DIVIDER, 0x00);
273 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_FEEDBACK_DIVIDER, 0x4e);
274 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_CALIBRATION_SETTINGS, 0x40);
275 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_BAND_SEL_CAL_SETTINGS_THREE, 0xba);
276 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_FREQ_DETECT_SETTINGS_ONE, 0x0c);
277 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_OUTDIV, 0x00);
278 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_CORE_OVERRIDE, 0x00);
279 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_DIGITAL_TIMERS_TWO, 0x08);
280 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_PROP_GAIN_RATE_1, 0x0a);
281 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_BAND_SEL_RATE_1, 0xc0);
282 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_INT_GAIN_IFILT_BAND_1, 0x84);
283 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_INT_GAIN_IFILT_BAND_1, 0x82);
284 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_FL_INT_GAIN_PFILT_BAND_1, 0x4c);
285 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_LOCK_OVERRIDE, 0x80);
286 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PFILT, 0x29);
287 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PFILT, 0x2f);
288 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_IFILT, 0x2a);
289 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_IFILT,
290 pll->phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1 ? 0x3f : 0x22);
292 if (pll->phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1) {
293 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PERF_OPTIMIZE, 0x22);
295 dsi_phy_write(pll->slave->phy->pll_base + REG_DSI_7nm_PHY_PLL_PERF_OPTIMIZE, 0x22);
299 static void dsi_pll_commit(struct dsi_pll_7nm *pll)
301 void __iomem *base = pll->phy->pll_base;
302 struct dsi_pll_regs *reg = &pll->reg_setup;
304 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_CORE_INPUT_OVERRIDE, 0x12);
305 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_DECIMAL_DIV_START_1, reg->decimal_div_start);
306 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_LOW_1, reg->frac_div_start_low);
307 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_MID_1, reg->frac_div_start_mid);
308 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_HIGH_1, reg->frac_div_start_high);
309 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_LOCKDET_RATE_1, reg->pll_lockdet_rate);
310 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_PLL_LOCK_DELAY, 0x06);
311 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_CMODE_1, 0x10); /* TODO: 0x00 for CPHY */
312 dsi_phy_write(base + REG_DSI_7nm_PHY_PLL_CLOCK_INVERTERS, reg->pll_clock_inverters);
315 static int dsi_pll_7nm_vco_set_rate(struct clk_hw *hw, unsigned long rate,
316 unsigned long parent_rate)
318 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(hw);
320 DBG("DSI PLL%d rate=%lu, parent's=%lu", pll_7nm->phy->id, rate,
323 pll_7nm->vco_current_rate = rate;
324 pll_7nm->vco_ref_clk_rate = VCO_REF_CLK_RATE;
326 dsi_pll_setup_config(pll_7nm);
328 dsi_pll_calc_dec_frac(pll_7nm);
330 dsi_pll_calc_ssc(pll_7nm);
332 dsi_pll_commit(pll_7nm);
334 dsi_pll_config_hzindep_reg(pll_7nm);
336 dsi_pll_ssc_commit(pll_7nm);
338 /* flush, ensure all register writes are done*/
344 static int dsi_pll_7nm_lock_status(struct dsi_pll_7nm *pll)
348 u32 const delay_us = 100;
349 u32 const timeout_us = 5000;
351 rc = readl_poll_timeout_atomic(pll->phy->pll_base +
352 REG_DSI_7nm_PHY_PLL_COMMON_STATUS_ONE,
354 ((status & BIT(0)) > 0),
358 pr_err("DSI PLL(%d) lock failed, status=0x%08x\n",
359 pll->phy->id, status);
364 static void dsi_pll_disable_pll_bias(struct dsi_pll_7nm *pll)
366 u32 data = dsi_phy_read(pll->phy->base + REG_DSI_7nm_PHY_CMN_CTRL_0);
368 dsi_phy_write(pll->phy->pll_base + REG_DSI_7nm_PHY_PLL_SYSTEM_MUXES, 0);
369 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_CTRL_0, data & ~BIT(5));
373 static void dsi_pll_enable_pll_bias(struct dsi_pll_7nm *pll)
375 u32 data = dsi_phy_read(pll->phy->base + REG_DSI_7nm_PHY_CMN_CTRL_0);
377 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_CTRL_0, data | BIT(5));
378 dsi_phy_write(pll->phy->pll_base + REG_DSI_7nm_PHY_PLL_SYSTEM_MUXES, 0xc0);
382 static void dsi_pll_disable_global_clk(struct dsi_pll_7nm *pll)
386 data = dsi_phy_read(pll->phy->base + REG_DSI_7nm_PHY_CMN_CLK_CFG1);
387 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_CLK_CFG1, data & ~BIT(5));
390 static void dsi_pll_enable_global_clk(struct dsi_pll_7nm *pll)
394 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_CTRL_3, 0x04);
396 data = dsi_phy_read(pll->phy->base + REG_DSI_7nm_PHY_CMN_CLK_CFG1);
397 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_CLK_CFG1,
398 data | BIT(5) | BIT(4));
401 static void dsi_pll_phy_dig_reset(struct dsi_pll_7nm *pll)
404 * Reset the PHY digital domain. This would be needed when
405 * coming out of a CX or analog rail power collapse while
406 * ensuring that the pads maintain LP00 or LP11 state
408 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_GLBL_DIGTOP_SPARE4, BIT(0));
409 wmb(); /* Ensure that the reset is deasserted */
410 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_GLBL_DIGTOP_SPARE4, 0x0);
411 wmb(); /* Ensure that the reset is deasserted */
414 static int dsi_pll_7nm_vco_prepare(struct clk_hw *hw)
416 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(hw);
419 dsi_pll_enable_pll_bias(pll_7nm);
421 dsi_pll_enable_pll_bias(pll_7nm->slave);
424 dsi_phy_write(pll_7nm->phy->base + REG_DSI_7nm_PHY_CMN_PLL_CNTRL, 0x01);
427 * ensure all PLL configurations are written prior to checking
432 /* Check for PLL lock */
433 rc = dsi_pll_7nm_lock_status(pll_7nm);
435 pr_err("PLL(%d) lock failed\n", pll_7nm->phy->id);
439 pll_7nm->phy->pll_on = true;
442 * assert power on reset for PHY digital in case the PLL is
443 * enabled after CX of analog domain power collapse. This needs
444 * to be done before enabling the global clk.
446 dsi_pll_phy_dig_reset(pll_7nm);
448 dsi_pll_phy_dig_reset(pll_7nm->slave);
450 dsi_pll_enable_global_clk(pll_7nm);
452 dsi_pll_enable_global_clk(pll_7nm->slave);
458 static void dsi_pll_disable_sub(struct dsi_pll_7nm *pll)
460 dsi_phy_write(pll->phy->base + REG_DSI_7nm_PHY_CMN_RBUF_CTRL, 0);
461 dsi_pll_disable_pll_bias(pll);
464 static void dsi_pll_7nm_vco_unprepare(struct clk_hw *hw)
466 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(hw);
469 * To avoid any stray glitches while abruptly powering down the PLL
470 * make sure to gate the clock using the clock enable bit before
471 * powering down the PLL
473 dsi_pll_disable_global_clk(pll_7nm);
474 dsi_phy_write(pll_7nm->phy->base + REG_DSI_7nm_PHY_CMN_PLL_CNTRL, 0);
475 dsi_pll_disable_sub(pll_7nm);
476 if (pll_7nm->slave) {
477 dsi_pll_disable_global_clk(pll_7nm->slave);
478 dsi_pll_disable_sub(pll_7nm->slave);
480 /* flush, ensure all register writes are done */
482 pll_7nm->phy->pll_on = false;
485 static unsigned long dsi_pll_7nm_vco_recalc_rate(struct clk_hw *hw,
486 unsigned long parent_rate)
488 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(hw);
489 struct dsi_pll_config *config = &pll_7nm->pll_configuration;
490 void __iomem *base = pll_7nm->phy->pll_base;
491 u64 ref_clk = pll_7nm->vco_ref_clk_rate;
498 dec = dsi_phy_read(base + REG_DSI_7nm_PHY_PLL_DECIMAL_DIV_START_1);
501 frac = dsi_phy_read(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_LOW_1);
502 frac |= ((dsi_phy_read(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_MID_1) &
504 frac |= ((dsi_phy_read(base + REG_DSI_7nm_PHY_PLL_FRAC_DIV_START_HIGH_1) &
509 * 1. Assumes prescaler is disabled
511 multiplier = 1 << config->frac_bits;
512 pll_freq = dec * (ref_clk * 2);
513 tmp64 = (ref_clk * 2 * frac);
514 pll_freq += div_u64(tmp64, multiplier);
518 DBG("DSI PLL%d returning vco rate = %lu, dec = %x, frac = %x",
519 pll_7nm->phy->id, (unsigned long)vco_rate, dec, frac);
521 return (unsigned long)vco_rate;
524 static long dsi_pll_7nm_clk_round_rate(struct clk_hw *hw,
525 unsigned long rate, unsigned long *parent_rate)
527 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(hw);
529 if (rate < pll_7nm->phy->cfg->min_pll_rate)
530 return pll_7nm->phy->cfg->min_pll_rate;
531 else if (rate > pll_7nm->phy->cfg->max_pll_rate)
532 return pll_7nm->phy->cfg->max_pll_rate;
537 static const struct clk_ops clk_ops_dsi_pll_7nm_vco = {
538 .round_rate = dsi_pll_7nm_clk_round_rate,
539 .set_rate = dsi_pll_7nm_vco_set_rate,
540 .recalc_rate = dsi_pll_7nm_vco_recalc_rate,
541 .prepare = dsi_pll_7nm_vco_prepare,
542 .unprepare = dsi_pll_7nm_vco_unprepare,
549 static void dsi_7nm_pll_save_state(struct msm_dsi_phy *phy)
551 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(phy->vco_hw);
552 struct pll_7nm_cached_state *cached = &pll_7nm->cached_state;
553 void __iomem *phy_base = pll_7nm->phy->base;
554 u32 cmn_clk_cfg0, cmn_clk_cfg1;
556 cached->pll_out_div = dsi_phy_read(pll_7nm->phy->pll_base +
557 REG_DSI_7nm_PHY_PLL_PLL_OUTDIV_RATE);
558 cached->pll_out_div &= 0x3;
560 cmn_clk_cfg0 = dsi_phy_read(phy_base + REG_DSI_7nm_PHY_CMN_CLK_CFG0);
561 cached->bit_clk_div = cmn_clk_cfg0 & 0xf;
562 cached->pix_clk_div = (cmn_clk_cfg0 & 0xf0) >> 4;
564 cmn_clk_cfg1 = dsi_phy_read(phy_base + REG_DSI_7nm_PHY_CMN_CLK_CFG1);
565 cached->pll_mux = cmn_clk_cfg1 & 0x3;
567 DBG("DSI PLL%d outdiv %x bit_clk_div %x pix_clk_div %x pll_mux %x",
568 pll_7nm->phy->id, cached->pll_out_div, cached->bit_clk_div,
569 cached->pix_clk_div, cached->pll_mux);
572 static int dsi_7nm_pll_restore_state(struct msm_dsi_phy *phy)
574 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(phy->vco_hw);
575 struct pll_7nm_cached_state *cached = &pll_7nm->cached_state;
576 void __iomem *phy_base = pll_7nm->phy->base;
580 val = dsi_phy_read(pll_7nm->phy->pll_base + REG_DSI_7nm_PHY_PLL_PLL_OUTDIV_RATE);
582 val |= cached->pll_out_div;
583 dsi_phy_write(pll_7nm->phy->pll_base + REG_DSI_7nm_PHY_PLL_PLL_OUTDIV_RATE, val);
585 dsi_phy_write(phy_base + REG_DSI_7nm_PHY_CMN_CLK_CFG0,
586 cached->bit_clk_div | (cached->pix_clk_div << 4));
588 val = dsi_phy_read(phy_base + REG_DSI_7nm_PHY_CMN_CLK_CFG1);
590 val |= cached->pll_mux;
591 dsi_phy_write(phy_base + REG_DSI_7nm_PHY_CMN_CLK_CFG1, val);
593 ret = dsi_pll_7nm_vco_set_rate(phy->vco_hw,
594 pll_7nm->vco_current_rate,
595 pll_7nm->vco_ref_clk_rate);
597 DRM_DEV_ERROR(&pll_7nm->phy->pdev->dev,
598 "restore vco rate failed. ret=%d\n", ret);
602 DBG("DSI PLL%d", pll_7nm->phy->id);
607 static int dsi_7nm_set_usecase(struct msm_dsi_phy *phy)
609 struct dsi_pll_7nm *pll_7nm = to_pll_7nm(phy->vco_hw);
610 void __iomem *base = phy->base;
611 u32 data = 0x0; /* internal PLL */
613 DBG("DSI PLL%d", pll_7nm->phy->id);
615 switch (phy->usecase) {
616 case MSM_DSI_PHY_STANDALONE:
618 case MSM_DSI_PHY_MASTER:
619 pll_7nm->slave = pll_7nm_list[(pll_7nm->phy->id + 1) % DSI_MAX];
621 case MSM_DSI_PHY_SLAVE:
622 data = 0x1; /* external PLL */
629 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CLK_CFG1, (data << 2));
635 * The post dividers and mux clocks are created using the standard divider and
636 * mux API. Unlike the 14nm PHY, the slave PLL doesn't need its dividers/mux
637 * state to follow the master PLL's divider/mux state. Therefore, we don't
638 * require special clock ops that also configure the slave PLL registers
640 static int pll_7nm_register(struct dsi_pll_7nm *pll_7nm, struct clk_hw **provided_clocks)
642 char clk_name[32], parent[32], vco_name[32];
643 char parent2[32], parent3[32], parent4[32];
644 struct clk_init_data vco_init = {
645 .parent_names = (const char *[]){ "bi_tcxo" },
648 .flags = CLK_IGNORE_UNUSED,
649 .ops = &clk_ops_dsi_pll_7nm_vco,
651 struct device *dev = &pll_7nm->phy->pdev->dev;
655 DBG("DSI%d", pll_7nm->phy->id);
657 snprintf(vco_name, 32, "dsi%dvco_clk", pll_7nm->phy->id);
658 pll_7nm->clk_hw.init = &vco_init;
660 ret = devm_clk_hw_register(dev, &pll_7nm->clk_hw);
664 snprintf(clk_name, 32, "dsi%d_pll_out_div_clk", pll_7nm->phy->id);
665 snprintf(parent, 32, "dsi%dvco_clk", pll_7nm->phy->id);
667 hw = devm_clk_hw_register_divider(dev, clk_name,
668 parent, CLK_SET_RATE_PARENT,
669 pll_7nm->phy->pll_base +
670 REG_DSI_7nm_PHY_PLL_PLL_OUTDIV_RATE,
671 0, 2, CLK_DIVIDER_POWER_OF_TWO, NULL);
677 snprintf(clk_name, 32, "dsi%d_pll_bit_clk", pll_7nm->phy->id);
678 snprintf(parent, 32, "dsi%d_pll_out_div_clk", pll_7nm->phy->id);
680 /* BIT CLK: DIV_CTRL_3_0 */
681 hw = devm_clk_hw_register_divider(dev, clk_name, parent,
684 REG_DSI_7nm_PHY_CMN_CLK_CFG0,
685 0, 4, CLK_DIVIDER_ONE_BASED,
686 &pll_7nm->postdiv_lock);
692 snprintf(clk_name, 32, "dsi%d_phy_pll_out_byteclk", pll_7nm->phy->id);
693 snprintf(parent, 32, "dsi%d_pll_bit_clk", pll_7nm->phy->id);
695 /* DSI Byte clock = VCO_CLK / OUT_DIV / BIT_DIV / 8 */
696 hw = devm_clk_hw_register_fixed_factor(dev, clk_name, parent,
697 CLK_SET_RATE_PARENT, 1, 8);
703 provided_clocks[DSI_BYTE_PLL_CLK] = hw;
705 snprintf(clk_name, 32, "dsi%d_pll_by_2_bit_clk", pll_7nm->phy->id);
706 snprintf(parent, 32, "dsi%d_pll_bit_clk", pll_7nm->phy->id);
708 hw = devm_clk_hw_register_fixed_factor(dev, clk_name, parent,
715 snprintf(clk_name, 32, "dsi%d_pll_post_out_div_clk", pll_7nm->phy->id);
716 snprintf(parent, 32, "dsi%d_pll_out_div_clk", pll_7nm->phy->id);
718 hw = devm_clk_hw_register_fixed_factor(dev, clk_name, parent,
725 snprintf(clk_name, 32, "dsi%d_pclk_mux", pll_7nm->phy->id);
726 snprintf(parent, 32, "dsi%d_pll_bit_clk", pll_7nm->phy->id);
727 snprintf(parent2, 32, "dsi%d_pll_by_2_bit_clk", pll_7nm->phy->id);
728 snprintf(parent3, 32, "dsi%d_pll_out_div_clk", pll_7nm->phy->id);
729 snprintf(parent4, 32, "dsi%d_pll_post_out_div_clk", pll_7nm->phy->id);
731 hw = devm_clk_hw_register_mux(dev, clk_name,
733 parent, parent2, parent3, parent4
734 }), 4, 0, pll_7nm->phy->base +
735 REG_DSI_7nm_PHY_CMN_CLK_CFG1,
742 snprintf(clk_name, 32, "dsi%d_phy_pll_out_dsiclk", pll_7nm->phy->id);
743 snprintf(parent, 32, "dsi%d_pclk_mux", pll_7nm->phy->id);
745 /* PIX CLK DIV : DIV_CTRL_7_4*/
746 hw = devm_clk_hw_register_divider(dev, clk_name, parent,
747 0, pll_7nm->phy->base +
748 REG_DSI_7nm_PHY_CMN_CLK_CFG0,
749 4, 4, CLK_DIVIDER_ONE_BASED,
750 &pll_7nm->postdiv_lock);
756 provided_clocks[DSI_PIXEL_PLL_CLK] = hw;
765 static int dsi_pll_7nm_init(struct msm_dsi_phy *phy)
767 struct platform_device *pdev = phy->pdev;
768 struct dsi_pll_7nm *pll_7nm;
771 pll_7nm = devm_kzalloc(&pdev->dev, sizeof(*pll_7nm), GFP_KERNEL);
775 DBG("DSI PLL%d", phy->id);
777 pll_7nm_list[phy->id] = pll_7nm;
779 spin_lock_init(&pll_7nm->postdiv_lock);
783 ret = pll_7nm_register(pll_7nm, phy->provided_clocks->hws);
785 DRM_DEV_ERROR(&pdev->dev, "failed to register PLL: %d\n", ret);
789 phy->vco_hw = &pll_7nm->clk_hw;
791 /* TODO: Remove this when we have proper display handover support */
792 msm_dsi_phy_pll_save_state(phy);
797 static int dsi_phy_hw_v4_0_is_pll_on(struct msm_dsi_phy *phy)
799 void __iomem *base = phy->base;
802 data = dsi_phy_read(base + REG_DSI_7nm_PHY_CMN_PLL_CNTRL);
803 mb(); /* make sure read happened */
805 return (data & BIT(0));
808 static void dsi_phy_hw_v4_0_config_lpcdrx(struct msm_dsi_phy *phy, bool enable)
810 void __iomem *lane_base = phy->lane_base;
811 int phy_lane_0 = 0; /* TODO: Support all lane swap configs */
814 * LPRX and CDRX need to enabled only for physical data lane
815 * corresponding to the logical data lane 0
818 dsi_phy_write(lane_base +
819 REG_DSI_7nm_PHY_LN_LPRX_CTRL(phy_lane_0), 0x3);
821 dsi_phy_write(lane_base +
822 REG_DSI_7nm_PHY_LN_LPRX_CTRL(phy_lane_0), 0);
825 static void dsi_phy_hw_v4_0_lane_settings(struct msm_dsi_phy *phy)
828 const u8 tx_dctrl_0[] = { 0x00, 0x00, 0x00, 0x04, 0x01 };
829 const u8 tx_dctrl_1[] = { 0x40, 0x40, 0x40, 0x46, 0x41 };
830 const u8 *tx_dctrl = tx_dctrl_0;
831 void __iomem *lane_base = phy->lane_base;
833 if (phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1)
834 tx_dctrl = tx_dctrl_1;
836 /* Strength ctrl settings */
837 for (i = 0; i < 5; i++) {
839 * Disable LPRX and CDRX for all lanes. And later on, it will
840 * be only enabled for the physical data lane corresponding
841 * to the logical data lane 0
843 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_LPRX_CTRL(i), 0);
844 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_PIN_SWAP(i), 0x0);
847 dsi_phy_hw_v4_0_config_lpcdrx(phy, true);
850 for (i = 0; i < 5; i++) {
851 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_CFG0(i), 0x0);
852 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_CFG1(i), 0x0);
853 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_CFG2(i), i == 4 ? 0x8a : 0xa);
854 dsi_phy_write(lane_base + REG_DSI_7nm_PHY_LN_TX_DCTRL(i), tx_dctrl[i]);
858 static int dsi_7nm_phy_enable(struct msm_dsi_phy *phy, int src_pll_id,
859 struct msm_dsi_phy_clk_request *clk_req)
863 u32 const delay_us = 5;
864 u32 const timeout_us = 1000;
865 struct msm_dsi_dphy_timing *timing = &phy->timing;
866 void __iomem *base = phy->base;
867 bool less_than_1500_mhz;
868 u32 vreg_ctrl_0, glbl_str_swi_cal_sel_ctrl, glbl_hstx_str_ctrl_0;
869 u32 glbl_rescode_top_ctrl, glbl_rescode_bot_ctrl;
874 if (msm_dsi_dphy_timing_calc_v4(timing, clk_req)) {
875 DRM_DEV_ERROR(&phy->pdev->dev,
876 "%s: D-PHY timing calculation failed\n", __func__);
880 if (dsi_phy_hw_v4_0_is_pll_on(phy))
881 pr_warn("PLL turned on before configuring PHY\n");
883 /* wait for REFGEN READY */
884 ret = readl_poll_timeout_atomic(base + REG_DSI_7nm_PHY_CMN_PHY_STATUS,
885 status, (status & BIT(0)),
886 delay_us, timeout_us);
888 pr_err("Ref gen not ready. Aborting\n");
892 /* TODO: CPHY enable path (this is for DPHY only) */
894 /* Alter PHY configurations if data rate less than 1.5GHZ*/
895 less_than_1500_mhz = (clk_req->bitclk_rate <= 1500000000);
897 if (phy->cfg->quirks & DSI_PHY_7NM_QUIRK_V4_1) {
898 vreg_ctrl_0 = less_than_1500_mhz ? 0x53 : 0x52;
899 glbl_rescode_top_ctrl = less_than_1500_mhz ? 0x3d : 0x00;
900 glbl_rescode_bot_ctrl = less_than_1500_mhz ? 0x39 : 0x3c;
901 glbl_str_swi_cal_sel_ctrl = 0x00;
902 glbl_hstx_str_ctrl_0 = 0x88;
904 vreg_ctrl_0 = less_than_1500_mhz ? 0x5B : 0x59;
905 glbl_str_swi_cal_sel_ctrl = less_than_1500_mhz ? 0x03 : 0x00;
906 glbl_hstx_str_ctrl_0 = less_than_1500_mhz ? 0x66 : 0x88;
907 glbl_rescode_top_ctrl = 0x03;
908 glbl_rescode_bot_ctrl = 0x3c;
911 /* de-assert digital and pll power down */
912 data = BIT(6) | BIT(5);
913 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_0, data);
915 /* Assert PLL core reset */
916 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_PLL_CNTRL, 0x00);
918 /* turn off resync FIFO */
919 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_RBUF_CTRL, 0x00);
921 /* program CMN_CTRL_4 for minor_ver 2 chipsets*/
922 data = dsi_phy_read(base + REG_DSI_7nm_PHY_CMN_REVISION_ID0);
923 data = data & (0xf0);
925 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_4, 0x04);
927 /* Configure PHY lane swap (TODO: we need to calculate this) */
928 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_LANE_CFG0, 0x21);
929 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_LANE_CFG1, 0x84);
932 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_VREG_CTRL_0, vreg_ctrl_0);
933 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_VREG_CTRL_1, 0x5c);
934 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_3, 0x00);
935 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_STR_SWI_CAL_SEL_CTRL,
936 glbl_str_swi_cal_sel_ctrl);
937 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_HSTX_STR_CTRL_0,
938 glbl_hstx_str_ctrl_0);
939 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_PEMPH_CTRL_0, 0x00);
940 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_RESCODE_OFFSET_TOP_CTRL,
941 glbl_rescode_top_ctrl);
942 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_RESCODE_OFFSET_BOT_CTRL,
943 glbl_rescode_bot_ctrl);
944 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_GLBL_LPTX_STR_CTRL, 0x55);
946 /* Remove power down from all blocks */
947 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_0, 0x7f);
949 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_LANE_CTRL0, 0x1f);
951 /* Select full-rate mode */
952 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_2, 0x40);
954 ret = dsi_7nm_set_usecase(phy);
956 DRM_DEV_ERROR(&phy->pdev->dev, "%s: set pll usecase failed, %d\n",
961 /* DSI PHY timings */
962 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_0, 0x00);
963 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_1, timing->clk_zero);
964 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_2, timing->clk_prepare);
965 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_3, timing->clk_trail);
966 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_4, timing->hs_exit);
967 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_5, timing->hs_zero);
968 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_6, timing->hs_prepare);
969 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_7, timing->hs_trail);
970 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_8, timing->hs_rqst);
971 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_9, 0x02);
972 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_10, 0x04);
973 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_11, 0x00);
974 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_12,
975 timing->shared_timings.clk_pre);
976 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_TIMING_CTRL_13,
977 timing->shared_timings.clk_post);
979 /* DSI lane settings */
980 dsi_phy_hw_v4_0_lane_settings(phy);
982 DBG("DSI%d PHY enabled", phy->id);
987 static void dsi_7nm_phy_disable(struct msm_dsi_phy *phy)
989 void __iomem *base = phy->base;
994 if (dsi_phy_hw_v4_0_is_pll_on(phy))
995 pr_warn("Turning OFF PHY while PLL is on\n");
997 dsi_phy_hw_v4_0_config_lpcdrx(phy, false);
998 data = dsi_phy_read(base + REG_DSI_7nm_PHY_CMN_CTRL_0);
1000 /* disable all lanes */
1002 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_0, data);
1003 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_LANE_CTRL0, 0);
1005 /* Turn off all PHY blocks */
1006 dsi_phy_write(base + REG_DSI_7nm_PHY_CMN_CTRL_0, 0x00);
1007 /* make sure phy is turned off */
1010 DBG("DSI%d PHY disabled", phy->id);
1013 const struct msm_dsi_phy_cfg dsi_phy_7nm_cfgs = {
1014 .src_pll_truthtable = { {false, false}, {true, false} },
1015 .has_phy_lane = true,
1019 {"vdds", 36000, 32},
1023 .enable = dsi_7nm_phy_enable,
1024 .disable = dsi_7nm_phy_disable,
1025 .pll_init = dsi_pll_7nm_init,
1026 .save_pll_state = dsi_7nm_pll_save_state,
1027 .restore_pll_state = dsi_7nm_pll_restore_state,
1029 .min_pll_rate = 600000000UL,
1030 .max_pll_rate = (5000000000ULL < ULONG_MAX) ? 5000000000ULL : ULONG_MAX,
1031 .io_start = { 0xae94400, 0xae96400 },
1033 .quirks = DSI_PHY_7NM_QUIRK_V4_1,
1036 const struct msm_dsi_phy_cfg dsi_phy_7nm_8150_cfgs = {
1037 .src_pll_truthtable = { {false, false}, {true, false} },
1038 .has_phy_lane = true,
1042 {"vdds", 36000, 32},
1046 .enable = dsi_7nm_phy_enable,
1047 .disable = dsi_7nm_phy_disable,
1048 .pll_init = dsi_pll_7nm_init,
1049 .save_pll_state = dsi_7nm_pll_save_state,
1050 .restore_pll_state = dsi_7nm_pll_restore_state,
1052 .min_pll_rate = 1000000000UL,
1053 .max_pll_rate = 3500000000UL,
1054 .io_start = { 0xae94400, 0xae96400 },