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.
24 * Shobhit Kumar <shobhit.kumar@intel.com>
25 * Yogesh Mohan Marimuthu <yogesh.mohan.marimuthu@intel.com>
28 #include <linux/kernel.h>
29 #include "intel_drv.h"
31 #include "intel_dsi.h"
33 static const u16 lfsr_converts
[] = {
34 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
35 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
36 106, 53, 282, 397, 454, 227, 113, 56, 284, 142, /* 81 - 90 */
37 71, 35, 273, 136, 324, 418, 465, 488, 500, 506 /* 91 - 100 */
40 /* Get DSI clock from pixel clock */
41 static u32
dsi_clk_from_pclk(u32 pclk
, enum mipi_dsi_pixel_format fmt
,
45 u32 bpp
= mipi_dsi_pixel_format_to_bpp(fmt
);
47 /* DSI data rate = pixel clock * bits per pixel / lane count
48 pixel clock is converted from KHz to Hz */
49 dsi_clk_khz
= DIV_ROUND_CLOSEST(pclk
* bpp
, lane_count
);
54 static int dsi_calc_mnp(struct drm_i915_private
*dev_priv
,
55 struct intel_crtc_state
*config
,
58 unsigned int m_min
, m_max
, p_min
= 2, p_max
= 6;
60 unsigned int calc_m
, calc_p
;
63 /* target_dsi_clk is expected in kHz */
64 if (target_dsi_clk
< 300000 || target_dsi_clk
> 1150000) {
65 DRM_ERROR("DSI CLK Out of Range\n");
69 if (IS_CHERRYVIEW(dev_priv
)) {
83 delta
= abs(target_dsi_clk
- (m_min
* ref_clk
) / (p_min
* n
));
85 for (m
= m_min
; m
<= m_max
&& delta
; m
++) {
86 for (p
= p_min
; p
<= p_max
&& delta
; p
++) {
88 * Find the optimal m and p divisors with minimal delta
89 * +/- the required clock
91 int calc_dsi_clk
= (m
* ref_clk
) / (p
* n
);
92 int d
= abs(target_dsi_clk
- calc_dsi_clk
);
101 /* register has log2(N1), this works fine for powers of two */
102 config
->dsi_pll
.ctrl
= 1 << (DSI_PLL_P1_POST_DIV_SHIFT
+ calc_p
- 2);
103 config
->dsi_pll
.div
=
104 (ffs(n
) - 1) << DSI_PLL_N1_DIV_SHIFT
|
105 (u32
)lfsr_converts
[calc_m
- 62] << DSI_PLL_M1_DIV_SHIFT
;
111 * XXX: The muxing and gating is hard coded for now. Need to add support for
112 * sharing PLLs with two DSI outputs.
114 int vlv_dsi_pll_compute(struct intel_encoder
*encoder
,
115 struct intel_crtc_state
*config
)
117 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
118 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
122 dsi_clk
= dsi_clk_from_pclk(intel_dsi
->pclk
, intel_dsi
->pixel_format
,
123 intel_dsi
->lane_count
);
125 ret
= dsi_calc_mnp(dev_priv
, config
, dsi_clk
);
127 DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
131 if (intel_dsi
->ports
& (1 << PORT_A
))
132 config
->dsi_pll
.ctrl
|= DSI_PLL_CLK_GATE_DSI0_DSIPLL
;
134 if (intel_dsi
->ports
& (1 << PORT_C
))
135 config
->dsi_pll
.ctrl
|= DSI_PLL_CLK_GATE_DSI1_DSIPLL
;
137 config
->dsi_pll
.ctrl
|= DSI_PLL_VCO_EN
;
139 DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
140 config
->dsi_pll
.div
, config
->dsi_pll
.ctrl
);
145 void vlv_dsi_pll_enable(struct intel_encoder
*encoder
,
146 const struct intel_crtc_state
*config
)
148 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
152 mutex_lock(&dev_priv
->sb_lock
);
154 vlv_cck_write(dev_priv
, CCK_REG_DSI_PLL_CONTROL
, 0);
155 vlv_cck_write(dev_priv
, CCK_REG_DSI_PLL_DIVIDER
, config
->dsi_pll
.div
);
156 vlv_cck_write(dev_priv
, CCK_REG_DSI_PLL_CONTROL
,
157 config
->dsi_pll
.ctrl
& ~DSI_PLL_VCO_EN
);
159 /* wait at least 0.5 us after ungating before enabling VCO,
160 * allow hrtimer subsystem optimization by relaxing timing
162 usleep_range(10, 50);
164 vlv_cck_write(dev_priv
, CCK_REG_DSI_PLL_CONTROL
, config
->dsi_pll
.ctrl
);
166 if (wait_for(vlv_cck_read(dev_priv
, CCK_REG_DSI_PLL_CONTROL
) &
169 mutex_unlock(&dev_priv
->sb_lock
);
170 DRM_ERROR("DSI PLL lock failed\n");
173 mutex_unlock(&dev_priv
->sb_lock
);
175 DRM_DEBUG_KMS("DSI PLL locked\n");
178 void vlv_dsi_pll_disable(struct intel_encoder
*encoder
)
180 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
185 mutex_lock(&dev_priv
->sb_lock
);
187 tmp
= vlv_cck_read(dev_priv
, CCK_REG_DSI_PLL_CONTROL
);
188 tmp
&= ~DSI_PLL_VCO_EN
;
189 tmp
|= DSI_PLL_LDO_GATE
;
190 vlv_cck_write(dev_priv
, CCK_REG_DSI_PLL_CONTROL
, tmp
);
192 mutex_unlock(&dev_priv
->sb_lock
);
195 bool bxt_dsi_pll_is_enabled(struct drm_i915_private
*dev_priv
)
201 mask
= BXT_DSI_PLL_DO_ENABLE
| BXT_DSI_PLL_LOCKED
;
202 val
= I915_READ(BXT_DSI_PLL_ENABLE
);
203 enabled
= (val
& mask
) == mask
;
209 * Dividers must be programmed with valid values. As per BSEPC, for
210 * GEMINLAKE only PORT A divider values are checked while for BXT
211 * both divider values are validated. Check this here for
212 * paranoia, since BIOS is known to misconfigure PLLs in this way at
213 * times, and since accessing DSI registers with invalid dividers
214 * causes a system hang.
216 val
= I915_READ(BXT_DSI_PLL_CTL
);
217 if (IS_GEMINILAKE(dev_priv
)) {
218 if (!(val
& BXT_DSIA_16X_MASK
)) {
219 DRM_DEBUG_DRIVER("Invalid PLL divider (%08x)\n", val
);
223 if (!(val
& BXT_DSIA_16X_MASK
) || !(val
& BXT_DSIC_16X_MASK
)) {
224 DRM_DEBUG_DRIVER("Invalid PLL divider (%08x)\n", val
);
232 void bxt_dsi_pll_disable(struct intel_encoder
*encoder
)
234 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
239 val
= I915_READ(BXT_DSI_PLL_ENABLE
);
240 val
&= ~BXT_DSI_PLL_DO_ENABLE
;
241 I915_WRITE(BXT_DSI_PLL_ENABLE
, val
);
244 * PLL lock should deassert within 200us.
245 * Wait up to 1ms before timing out.
247 if (intel_wait_for_register(dev_priv
,
252 DRM_ERROR("Timeout waiting for PLL lock deassertion\n");
255 static void assert_bpp_mismatch(enum mipi_dsi_pixel_format fmt
, int pipe_bpp
)
257 int bpp
= mipi_dsi_pixel_format_to_bpp(fmt
);
259 WARN(bpp
!= pipe_bpp
,
260 "bpp match assertion failure (expected %d, current %d)\n",
264 u32
vlv_dsi_get_pclk(struct intel_encoder
*encoder
, int pipe_bpp
,
265 struct intel_crtc_state
*config
)
267 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
268 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
270 u32 pll_ctl
, pll_div
;
272 int refclk
= IS_CHERRYVIEW(dev_priv
) ? 100000 : 25000;
277 mutex_lock(&dev_priv
->sb_lock
);
278 pll_ctl
= vlv_cck_read(dev_priv
, CCK_REG_DSI_PLL_CONTROL
);
279 pll_div
= vlv_cck_read(dev_priv
, CCK_REG_DSI_PLL_DIVIDER
);
280 mutex_unlock(&dev_priv
->sb_lock
);
282 config
->dsi_pll
.ctrl
= pll_ctl
& ~DSI_PLL_LOCK
;
283 config
->dsi_pll
.div
= pll_div
;
285 /* mask out other bits and extract the P1 divisor */
286 pll_ctl
&= DSI_PLL_P1_POST_DIV_MASK
;
287 pll_ctl
= pll_ctl
>> (DSI_PLL_P1_POST_DIV_SHIFT
- 2);
290 n
= (pll_div
& DSI_PLL_N1_DIV_MASK
) >> DSI_PLL_N1_DIV_SHIFT
;
291 n
= 1 << n
; /* register has log2(N1) */
293 /* mask out the other bits and extract the M1 divisor */
294 pll_div
&= DSI_PLL_M1_DIV_MASK
;
295 pll_div
= pll_div
>> DSI_PLL_M1_DIV_SHIFT
;
298 pll_ctl
= pll_ctl
>> 1;
304 DRM_ERROR("wrong P1 divisor\n");
308 for (i
= 0; i
< ARRAY_SIZE(lfsr_converts
); i
++) {
309 if (lfsr_converts
[i
] == pll_div
)
313 if (i
== ARRAY_SIZE(lfsr_converts
)) {
314 DRM_ERROR("wrong m_seed programmed\n");
320 dsi_clock
= (m
* refclk
) / (p
* n
);
322 /* pixel_format and pipe_bpp should agree */
323 assert_bpp_mismatch(intel_dsi
->pixel_format
, pipe_bpp
);
325 pclk
= DIV_ROUND_CLOSEST(dsi_clock
* intel_dsi
->lane_count
, pipe_bpp
);
330 u32
bxt_dsi_get_pclk(struct intel_encoder
*encoder
, int pipe_bpp
,
331 struct intel_crtc_state
*config
)
336 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
337 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
341 DRM_ERROR("Invalid BPP(0)\n");
345 config
->dsi_pll
.ctrl
= I915_READ(BXT_DSI_PLL_CTL
);
347 dsi_ratio
= config
->dsi_pll
.ctrl
& BXT_DSI_PLL_RATIO_MASK
;
349 dsi_clk
= (dsi_ratio
* BXT_REF_CLOCK_KHZ
) / 2;
351 /* pixel_format and pipe_bpp should agree */
352 assert_bpp_mismatch(intel_dsi
->pixel_format
, pipe_bpp
);
354 pclk
= DIV_ROUND_CLOSEST(dsi_clk
* intel_dsi
->lane_count
, pipe_bpp
);
356 DRM_DEBUG_DRIVER("Calculated pclk=%u\n", pclk
);
360 void vlv_dsi_reset_clocks(struct intel_encoder
*encoder
, enum port port
)
363 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
364 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
366 temp
= I915_READ(MIPI_CTRL(port
));
367 temp
&= ~ESCAPE_CLOCK_DIVIDER_MASK
;
368 I915_WRITE(MIPI_CTRL(port
), temp
|
369 intel_dsi
->escape_clk_div
<<
370 ESCAPE_CLOCK_DIVIDER_SHIFT
);
373 static void glk_dsi_program_esc_clock(struct drm_device
*dev
,
374 const struct intel_crtc_state
*config
)
376 struct drm_i915_private
*dev_priv
= to_i915(dev
);
385 pll_ratio
= config
->dsi_pll
.ctrl
& BXT_DSI_PLL_RATIO_MASK
;
387 dsi_rate
= (BXT_REF_CLOCK_KHZ
* pll_ratio
) / 2;
389 ddr_clk
= dsi_rate
/ 2;
391 /* Variable divider value */
392 div1_value
= DIV_ROUND_CLOSEST(ddr_clk
, 20000);
394 /* Calculate TXESC1 divider */
395 if (div1_value
<= 10)
396 txesc1_div
= div1_value
;
397 else if ((div1_value
> 10) && (div1_value
<= 20))
398 txesc1_div
= DIV_ROUND_UP(div1_value
, 2);
399 else if ((div1_value
> 20) && (div1_value
<= 30))
400 txesc1_div
= DIV_ROUND_UP(div1_value
, 4);
401 else if ((div1_value
> 30) && (div1_value
<= 40))
402 txesc1_div
= DIV_ROUND_UP(div1_value
, 6);
403 else if ((div1_value
> 40) && (div1_value
<= 50))
404 txesc1_div
= DIV_ROUND_UP(div1_value
, 8);
408 /* Calculate TXESC2 divider */
409 div2_value
= DIV_ROUND_UP(div1_value
, txesc1_div
);
412 txesc2_div
= div2_value
;
416 I915_WRITE(MIPIO_TXESC_CLK_DIV1
, (1 << (txesc1_div
- 1)) & GLK_TX_ESC_CLK_DIV1_MASK
);
417 I915_WRITE(MIPIO_TXESC_CLK_DIV2
, (1 << (txesc2_div
- 1)) & GLK_TX_ESC_CLK_DIV2_MASK
);
420 /* Program BXT Mipi clocks and dividers */
421 static void bxt_dsi_program_clocks(struct drm_device
*dev
, enum port port
,
422 const struct intel_crtc_state
*config
)
424 struct drm_i915_private
*dev_priv
= to_i915(dev
);
432 u32 mipi_8by3_divider
;
434 /* Clear old configurations */
435 tmp
= I915_READ(BXT_MIPI_CLOCK_CTL
);
436 tmp
&= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port
));
437 tmp
&= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port
));
438 tmp
&= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port
));
439 tmp
&= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port
));
441 /* Get the current DSI rate(actual) */
442 pll_ratio
= config
->dsi_pll
.ctrl
& BXT_DSI_PLL_RATIO_MASK
;
443 dsi_rate
= (BXT_REF_CLOCK_KHZ
* pll_ratio
) / 2;
446 * tx clock should be <= 20MHz and the div value must be
447 * subtracted by 1 as per bspec
449 tx_div
= DIV_ROUND_UP(dsi_rate
, 20000) - 1;
451 * rx clock should be <= 150MHz and the div value must be
452 * subtracted by 1 as per bspec
454 rx_div
= DIV_ROUND_UP(dsi_rate
, 150000) - 1;
457 * rx divider value needs to be updated in the
458 * two differnt bit fields in the register hence splitting the
459 * rx divider value accordingly
461 rx_div_lower
= rx_div
& RX_DIVIDER_BIT_1_2
;
462 rx_div_upper
= (rx_div
& RX_DIVIDER_BIT_3_4
) >> 2;
464 mipi_8by3_divider
= 0x2;
466 tmp
|= BXT_MIPI_8X_BY3_DIVIDER(port
, mipi_8by3_divider
);
467 tmp
|= BXT_MIPI_TX_ESCLK_DIVIDER(port
, tx_div
);
468 tmp
|= BXT_MIPI_RX_ESCLK_LOWER_DIVIDER(port
, rx_div_lower
);
469 tmp
|= BXT_MIPI_RX_ESCLK_UPPER_DIVIDER(port
, rx_div_upper
);
471 I915_WRITE(BXT_MIPI_CLOCK_CTL
, tmp
);
474 int bxt_dsi_pll_compute(struct intel_encoder
*encoder
,
475 struct intel_crtc_state
*config
)
477 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
478 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
479 u8 dsi_ratio
, dsi_ratio_min
, dsi_ratio_max
;
482 dsi_clk
= dsi_clk_from_pclk(intel_dsi
->pclk
, intel_dsi
->pixel_format
,
483 intel_dsi
->lane_count
);
486 * From clock diagram, to get PLL ratio divider, divide double of DSI
487 * link rate (i.e., 2*8x=16x frequency value) by ref clock. Make sure to
488 * round 'up' the result
490 dsi_ratio
= DIV_ROUND_UP(dsi_clk
* 2, BXT_REF_CLOCK_KHZ
);
492 if (IS_BROXTON(dev_priv
)) {
493 dsi_ratio_min
= BXT_DSI_PLL_RATIO_MIN
;
494 dsi_ratio_max
= BXT_DSI_PLL_RATIO_MAX
;
496 dsi_ratio_min
= GLK_DSI_PLL_RATIO_MIN
;
497 dsi_ratio_max
= GLK_DSI_PLL_RATIO_MAX
;
500 if (dsi_ratio
< dsi_ratio_min
|| dsi_ratio
> dsi_ratio_max
) {
501 DRM_ERROR("Cant get a suitable ratio from DSI PLL ratios\n");
504 DRM_DEBUG_KMS("DSI PLL calculation is Done!!\n");
507 * Program DSI ratio and Select MIPIC and MIPIA PLL output as 8x
508 * Spec says both have to be programmed, even if one is not getting
509 * used. Configure MIPI_CLOCK_CTL dividers in modeset
511 config
->dsi_pll
.ctrl
= dsi_ratio
| BXT_DSIA_16X_BY2
| BXT_DSIC_16X_BY2
;
513 /* As per recommendation from hardware team,
514 * Prog PVD ratio =1 if dsi ratio <= 50
516 if (IS_BROXTON(dev_priv
) && dsi_ratio
<= 50)
517 config
->dsi_pll
.ctrl
|= BXT_DSI_PLL_PVD_RATIO_1
;
522 void bxt_dsi_pll_enable(struct intel_encoder
*encoder
,
523 const struct intel_crtc_state
*config
)
525 struct drm_i915_private
*dev_priv
= to_i915(encoder
->base
.dev
);
526 struct intel_dsi
*intel_dsi
= enc_to_intel_dsi(&encoder
->base
);
532 /* Configure PLL vales */
533 I915_WRITE(BXT_DSI_PLL_CTL
, config
->dsi_pll
.ctrl
);
534 POSTING_READ(BXT_DSI_PLL_CTL
);
536 /* Program TX, RX, Dphy clocks */
537 if (IS_BROXTON(dev_priv
)) {
538 for_each_dsi_port(port
, intel_dsi
->ports
)
539 bxt_dsi_program_clocks(encoder
->base
.dev
, port
, config
);
541 glk_dsi_program_esc_clock(encoder
->base
.dev
, config
);
545 val
= I915_READ(BXT_DSI_PLL_ENABLE
);
546 val
|= BXT_DSI_PLL_DO_ENABLE
;
547 I915_WRITE(BXT_DSI_PLL_ENABLE
, val
);
549 /* Timeout and fail if PLL not locked */
550 if (intel_wait_for_register(dev_priv
,
555 DRM_ERROR("Timed out waiting for DSI PLL to lock\n");
559 DRM_DEBUG_KMS("DSI PLL locked\n");
562 void bxt_dsi_reset_clocks(struct intel_encoder
*encoder
, enum port port
)
565 struct drm_device
*dev
= encoder
->base
.dev
;
566 struct drm_i915_private
*dev_priv
= to_i915(dev
);
568 /* Clear old configurations */
569 if (IS_BROXTON(dev_priv
)) {
570 tmp
= I915_READ(BXT_MIPI_CLOCK_CTL
);
571 tmp
&= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port
));
572 tmp
&= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port
));
573 tmp
&= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port
));
574 tmp
&= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port
));
575 I915_WRITE(BXT_MIPI_CLOCK_CTL
, tmp
);
577 tmp
= I915_READ(MIPIO_TXESC_CLK_DIV1
);
578 tmp
&= ~GLK_TX_ESC_CLK_DIV1_MASK
;
579 I915_WRITE(MIPIO_TXESC_CLK_DIV1
, tmp
);
581 tmp
= I915_READ(MIPIO_TXESC_CLK_DIV2
);
582 tmp
&= ~GLK_TX_ESC_CLK_DIV2_MASK
;
583 I915_WRITE(MIPIO_TXESC_CLK_DIV2
, tmp
);
585 I915_WRITE(MIPI_EOT_DISABLE(port
), CLOCKSTOP
);