2 * Copyright © 2013 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
28 #include <linux/kernel.h>
29 #include <linux/string_helpers.h>
33 #include "intel_display_types.h"
34 #include "intel_dsi.h"
35 #include "vlv_dsi_pll.h"
36 #include "vlv_dsi_pll_regs.h"
37 #include "vlv_sideband.h"
39 static const u16 lfsr_converts[] = {
40 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
41 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
42 106, 53, 282, 397, 454, 227, 113, 56, 284, 142, /* 81 - 90 */
43 71, 35, 273, 136, 324, 418, 465, 488, 500, 506 /* 91 - 100 */
46 /* Get DSI clock from pixel clock */
47 static u32 dsi_clk_from_pclk(u32 pclk, enum mipi_dsi_pixel_format fmt,
51 u32 bpp = mipi_dsi_pixel_format_to_bpp(fmt);
53 /* DSI data rate = pixel clock * bits per pixel / lane count
54 pixel clock is converted from KHz to Hz */
55 dsi_clk_khz = DIV_ROUND_CLOSEST(pclk * bpp, lane_count);
60 static int dsi_calc_mnp(struct drm_i915_private *dev_priv,
61 struct intel_crtc_state *config,
64 unsigned int m_min, m_max, p_min = 2, p_max = 6;
66 unsigned int calc_m, calc_p;
69 /* target_dsi_clk is expected in kHz */
70 if (target_dsi_clk < 300000 || target_dsi_clk > 1150000) {
71 drm_err(&dev_priv->drm, "DSI CLK Out of Range\n");
75 if (IS_CHERRYVIEW(dev_priv)) {
89 delta = abs(target_dsi_clk - (m_min * ref_clk) / (p_min * n));
91 for (m = m_min; m <= m_max && delta; m++) {
92 for (p = p_min; p <= p_max && delta; p++) {
94 * Find the optimal m and p divisors with minimal delta
95 * +/- the required clock
97 int calc_dsi_clk = (m * ref_clk) / (p * n);
98 int d = abs(target_dsi_clk - calc_dsi_clk);
107 /* register has log2(N1), this works fine for powers of two */
108 config->dsi_pll.ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
109 config->dsi_pll.div =
110 (ffs(n) - 1) << DSI_PLL_N1_DIV_SHIFT |
111 (u32)lfsr_converts[calc_m - 62] << DSI_PLL_M1_DIV_SHIFT;
116 static int vlv_dsi_pclk(struct intel_encoder *encoder,
117 struct intel_crtc_state *config)
119 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
120 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
121 int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
123 u32 pll_ctl, pll_div;
125 int refclk = IS_CHERRYVIEW(dev_priv) ? 100000 : 25000;
128 pll_ctl = config->dsi_pll.ctrl;
129 pll_div = config->dsi_pll.div;
131 /* mask out other bits and extract the P1 divisor */
132 pll_ctl &= DSI_PLL_P1_POST_DIV_MASK;
133 pll_ctl = pll_ctl >> (DSI_PLL_P1_POST_DIV_SHIFT - 2);
136 n = (pll_div & DSI_PLL_N1_DIV_MASK) >> DSI_PLL_N1_DIV_SHIFT;
137 n = 1 << n; /* register has log2(N1) */
139 /* mask out the other bits and extract the M1 divisor */
140 pll_div &= DSI_PLL_M1_DIV_MASK;
141 pll_div = pll_div >> DSI_PLL_M1_DIV_SHIFT;
144 pll_ctl = pll_ctl >> 1;
150 drm_err(&dev_priv->drm, "wrong P1 divisor\n");
154 for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
155 if (lfsr_converts[i] == pll_div)
159 if (i == ARRAY_SIZE(lfsr_converts)) {
160 drm_err(&dev_priv->drm, "wrong m_seed programmed\n");
166 dsi_clock = (m * refclk) / (p * n);
168 return DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, bpp);
172 * XXX: The muxing and gating is hard coded for now. Need to add support for
173 * sharing PLLs with two DSI outputs.
175 int vlv_dsi_pll_compute(struct intel_encoder *encoder,
176 struct intel_crtc_state *config)
178 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
179 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
180 int pclk, dsi_clk, ret;
182 dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
183 intel_dsi->lane_count);
185 ret = dsi_calc_mnp(dev_priv, config, dsi_clk);
187 drm_dbg_kms(&dev_priv->drm, "dsi_calc_mnp failed\n");
191 if (intel_dsi->ports & (1 << PORT_A))
192 config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
194 if (intel_dsi->ports & (1 << PORT_C))
195 config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI1_DSIPLL;
197 config->dsi_pll.ctrl |= DSI_PLL_VCO_EN;
199 drm_dbg_kms(&dev_priv->drm, "dsi pll div %08x, ctrl %08x\n",
200 config->dsi_pll.div, config->dsi_pll.ctrl);
202 pclk = vlv_dsi_pclk(encoder, config);
203 config->port_clock = pclk;
205 /* FIXME definitely not right for burst/cmd mode/pixel overlap */
206 config->hw.adjusted_mode.crtc_clock = pclk;
207 if (intel_dsi->dual_link)
208 config->hw.adjusted_mode.crtc_clock *= 2;
213 void vlv_dsi_pll_enable(struct intel_encoder *encoder,
214 const struct intel_crtc_state *config)
216 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
218 drm_dbg_kms(&dev_priv->drm, "\n");
220 vlv_cck_get(dev_priv);
222 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
223 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, config->dsi_pll.div);
224 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL,
225 config->dsi_pll.ctrl & ~DSI_PLL_VCO_EN);
227 /* wait at least 0.5 us after ungating before enabling VCO,
228 * allow hrtimer subsystem optimization by relaxing timing
230 usleep_range(10, 50);
232 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, config->dsi_pll.ctrl);
234 if (wait_for(vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL) &
237 vlv_cck_put(dev_priv);
238 drm_err(&dev_priv->drm, "DSI PLL lock failed\n");
241 vlv_cck_put(dev_priv);
243 drm_dbg_kms(&dev_priv->drm, "DSI PLL locked\n");
246 void vlv_dsi_pll_disable(struct intel_encoder *encoder)
248 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
251 drm_dbg_kms(&dev_priv->drm, "\n");
253 vlv_cck_get(dev_priv);
255 tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
256 tmp &= ~DSI_PLL_VCO_EN;
257 tmp |= DSI_PLL_LDO_GATE;
258 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
260 vlv_cck_put(dev_priv);
263 bool bxt_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
269 mask = BXT_DSI_PLL_DO_ENABLE | BXT_DSI_PLL_LOCKED;
270 val = intel_de_read(dev_priv, BXT_DSI_PLL_ENABLE);
271 enabled = (val & mask) == mask;
277 * Dividers must be programmed with valid values. As per BSEPC, for
278 * GEMINLAKE only PORT A divider values are checked while for BXT
279 * both divider values are validated. Check this here for
280 * paranoia, since BIOS is known to misconfigure PLLs in this way at
281 * times, and since accessing DSI registers with invalid dividers
282 * causes a system hang.
284 val = intel_de_read(dev_priv, BXT_DSI_PLL_CTL);
285 if (IS_GEMINILAKE(dev_priv)) {
286 if (!(val & BXT_DSIA_16X_MASK)) {
287 drm_dbg(&dev_priv->drm,
288 "Invalid PLL divider (%08x)\n", val);
292 if (!(val & BXT_DSIA_16X_MASK) || !(val & BXT_DSIC_16X_MASK)) {
293 drm_dbg(&dev_priv->drm,
294 "Invalid PLL divider (%08x)\n", val);
302 void bxt_dsi_pll_disable(struct intel_encoder *encoder)
304 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
306 drm_dbg_kms(&dev_priv->drm, "\n");
308 intel_de_rmw(dev_priv, BXT_DSI_PLL_ENABLE, BXT_DSI_PLL_DO_ENABLE, 0);
311 * PLL lock should deassert within 200us.
312 * Wait up to 1ms before timing out.
314 if (intel_de_wait_for_clear(dev_priv, BXT_DSI_PLL_ENABLE,
315 BXT_DSI_PLL_LOCKED, 1))
316 drm_err(&dev_priv->drm,
317 "Timeout waiting for PLL lock deassertion\n");
320 u32 vlv_dsi_get_pclk(struct intel_encoder *encoder,
321 struct intel_crtc_state *config)
323 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
324 u32 pll_ctl, pll_div;
326 drm_dbg_kms(&dev_priv->drm, "\n");
328 vlv_cck_get(dev_priv);
329 pll_ctl = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
330 pll_div = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_DIVIDER);
331 vlv_cck_put(dev_priv);
333 config->dsi_pll.ctrl = pll_ctl & ~DSI_PLL_LOCK;
334 config->dsi_pll.div = pll_div;
336 return vlv_dsi_pclk(encoder, config);
339 static int bxt_dsi_pclk(struct intel_encoder *encoder,
340 const struct intel_crtc_state *config)
342 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
343 int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
344 u32 dsi_ratio, dsi_clk;
346 dsi_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
347 dsi_clk = (dsi_ratio * BXT_REF_CLOCK_KHZ) / 2;
349 return DIV_ROUND_CLOSEST(dsi_clk * intel_dsi->lane_count, bpp);
352 u32 bxt_dsi_get_pclk(struct intel_encoder *encoder,
353 struct intel_crtc_state *config)
355 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
358 config->dsi_pll.ctrl = intel_de_read(dev_priv, BXT_DSI_PLL_CTL);
360 pclk = bxt_dsi_pclk(encoder, config);
362 drm_dbg(&dev_priv->drm, "Calculated pclk=%u\n", pclk);
366 void vlv_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
368 struct intel_display *display = to_intel_display(encoder);
369 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
372 temp = intel_de_read(display, MIPI_CTRL(display, port));
373 temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
374 intel_de_write(display, MIPI_CTRL(display, port),
375 temp | intel_dsi->escape_clk_div << ESCAPE_CLOCK_DIVIDER_SHIFT);
378 static void glk_dsi_program_esc_clock(struct drm_device *dev,
379 const struct intel_crtc_state *config)
381 struct drm_i915_private *dev_priv = to_i915(dev);
390 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
392 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
394 ddr_clk = dsi_rate / 2;
396 /* Variable divider value */
397 div1_value = DIV_ROUND_CLOSEST(ddr_clk, 20000);
399 /* Calculate TXESC1 divider */
400 if (div1_value <= 10)
401 txesc1_div = div1_value;
402 else if ((div1_value > 10) && (div1_value <= 20))
403 txesc1_div = DIV_ROUND_UP(div1_value, 2);
404 else if ((div1_value > 20) && (div1_value <= 30))
405 txesc1_div = DIV_ROUND_UP(div1_value, 4);
406 else if ((div1_value > 30) && (div1_value <= 40))
407 txesc1_div = DIV_ROUND_UP(div1_value, 6);
408 else if ((div1_value > 40) && (div1_value <= 50))
409 txesc1_div = DIV_ROUND_UP(div1_value, 8);
413 /* Calculate TXESC2 divider */
414 div2_value = DIV_ROUND_UP(div1_value, txesc1_div);
416 txesc2_div = min_t(u32, div2_value, 10);
418 intel_de_write(dev_priv, MIPIO_TXESC_CLK_DIV1,
419 (1 << (txesc1_div - 1)) & GLK_TX_ESC_CLK_DIV1_MASK);
420 intel_de_write(dev_priv, MIPIO_TXESC_CLK_DIV2,
421 (1 << (txesc2_div - 1)) & GLK_TX_ESC_CLK_DIV2_MASK);
424 /* Program BXT Mipi clocks and dividers */
425 static void bxt_dsi_program_clocks(struct drm_device *dev, enum port port,
426 const struct intel_crtc_state *config)
428 struct drm_i915_private *dev_priv = to_i915(dev);
436 u32 mipi_8by3_divider;
438 /* Clear old configurations */
439 tmp = intel_de_read(dev_priv, BXT_MIPI_CLOCK_CTL);
440 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
441 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
442 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
443 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
445 /* Get the current DSI rate(actual) */
446 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
447 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
450 * tx clock should be <= 20MHz and the div value must be
451 * subtracted by 1 as per bspec
453 tx_div = DIV_ROUND_UP(dsi_rate, 20000) - 1;
455 * rx clock should be <= 150MHz and the div value must be
456 * subtracted by 1 as per bspec
458 rx_div = DIV_ROUND_UP(dsi_rate, 150000) - 1;
461 * rx divider value needs to be updated in the
462 * two differnt bit fields in the register hence splitting the
463 * rx divider value accordingly
465 rx_div_lower = rx_div & RX_DIVIDER_BIT_1_2;
466 rx_div_upper = (rx_div & RX_DIVIDER_BIT_3_4) >> 2;
468 mipi_8by3_divider = 0x2;
470 tmp |= BXT_MIPI_8X_BY3_DIVIDER(port, mipi_8by3_divider);
471 tmp |= BXT_MIPI_TX_ESCLK_DIVIDER(port, tx_div);
472 tmp |= BXT_MIPI_RX_ESCLK_LOWER_DIVIDER(port, rx_div_lower);
473 tmp |= BXT_MIPI_RX_ESCLK_UPPER_DIVIDER(port, rx_div_upper);
475 intel_de_write(dev_priv, BXT_MIPI_CLOCK_CTL, tmp);
478 int bxt_dsi_pll_compute(struct intel_encoder *encoder,
479 struct intel_crtc_state *config)
481 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
482 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
483 u8 dsi_ratio, dsi_ratio_min, dsi_ratio_max;
487 dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
488 intel_dsi->lane_count);
491 * From clock diagram, to get PLL ratio divider, divide double of DSI
492 * link rate (i.e., 2*8x=16x frequency value) by ref clock. Make sure to
493 * round 'up' the result
495 dsi_ratio = DIV_ROUND_UP(dsi_clk * 2, BXT_REF_CLOCK_KHZ);
497 if (IS_BROXTON(dev_priv)) {
498 dsi_ratio_min = BXT_DSI_PLL_RATIO_MIN;
499 dsi_ratio_max = BXT_DSI_PLL_RATIO_MAX;
501 dsi_ratio_min = GLK_DSI_PLL_RATIO_MIN;
502 dsi_ratio_max = GLK_DSI_PLL_RATIO_MAX;
505 if (dsi_ratio < dsi_ratio_min || dsi_ratio > dsi_ratio_max) {
506 drm_err(&dev_priv->drm,
507 "Can't get a suitable ratio from DSI PLL ratios\n");
510 drm_dbg_kms(&dev_priv->drm, "DSI PLL calculation is Done!!\n");
513 * Program DSI ratio and Select MIPIC and MIPIA PLL output as 8x
514 * Spec says both have to be programmed, even if one is not getting
515 * used. Configure MIPI_CLOCK_CTL dividers in modeset
517 config->dsi_pll.ctrl = dsi_ratio | BXT_DSIA_16X_BY2 | BXT_DSIC_16X_BY2;
519 /* As per recommendation from hardware team,
520 * Prog PVD ratio =1 if dsi ratio <= 50
522 if (IS_BROXTON(dev_priv) && dsi_ratio <= 50)
523 config->dsi_pll.ctrl |= BXT_DSI_PLL_PVD_RATIO_1;
525 pclk = bxt_dsi_pclk(encoder, config);
526 config->port_clock = pclk;
528 /* FIXME definitely not right for burst/cmd mode/pixel overlap */
529 config->hw.adjusted_mode.crtc_clock = pclk;
530 if (intel_dsi->dual_link)
531 config->hw.adjusted_mode.crtc_clock *= 2;
536 void bxt_dsi_pll_enable(struct intel_encoder *encoder,
537 const struct intel_crtc_state *config)
539 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
540 struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
543 drm_dbg_kms(&dev_priv->drm, "\n");
545 /* Configure PLL vales */
546 intel_de_write(dev_priv, BXT_DSI_PLL_CTL, config->dsi_pll.ctrl);
547 intel_de_posting_read(dev_priv, BXT_DSI_PLL_CTL);
549 /* Program TX, RX, Dphy clocks */
550 if (IS_BROXTON(dev_priv)) {
551 for_each_dsi_port(port, intel_dsi->ports)
552 bxt_dsi_program_clocks(encoder->base.dev, port, config);
554 glk_dsi_program_esc_clock(encoder->base.dev, config);
558 intel_de_rmw(dev_priv, BXT_DSI_PLL_ENABLE, 0, BXT_DSI_PLL_DO_ENABLE);
560 /* Timeout and fail if PLL not locked */
561 if (intel_de_wait_for_set(dev_priv, BXT_DSI_PLL_ENABLE,
562 BXT_DSI_PLL_LOCKED, 1)) {
563 drm_err(&dev_priv->drm,
564 "Timed out waiting for DSI PLL to lock\n");
568 drm_dbg_kms(&dev_priv->drm, "DSI PLL locked\n");
571 void bxt_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
573 struct intel_display *display = to_intel_display(encoder);
574 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
577 /* Clear old configurations */
578 if (IS_BROXTON(dev_priv)) {
579 tmp = intel_de_read(display, BXT_MIPI_CLOCK_CTL);
580 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
581 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
582 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
583 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
584 intel_de_write(display, BXT_MIPI_CLOCK_CTL, tmp);
586 intel_de_rmw(display, MIPIO_TXESC_CLK_DIV1, GLK_TX_ESC_CLK_DIV1_MASK, 0);
588 intel_de_rmw(display, MIPIO_TXESC_CLK_DIV2, GLK_TX_ESC_CLK_DIV2_MASK, 0);
590 intel_de_write(display, MIPI_EOT_DISABLE(display, port), CLOCKSTOP);
593 static void assert_dsi_pll(struct drm_i915_private *i915, bool state)
595 struct intel_display *display = &i915->display;
599 cur_state = vlv_cck_read(i915, CCK_REG_DSI_PLL_CONTROL) & DSI_PLL_VCO_EN;
602 INTEL_DISPLAY_STATE_WARN(display, cur_state != state,
603 "DSI PLL state assertion failure (expected %s, current %s)\n",
604 str_on_off(state), str_on_off(cur_state));
607 void assert_dsi_pll_enabled(struct drm_i915_private *i915)
609 assert_dsi_pll(i915, true);
612 void assert_dsi_pll_disabled(struct drm_i915_private *i915)
614 assert_dsi_pll(i915, false);