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 #define DSI_HSS_PACKET_SIZE 4
34 #define DSI_HSE_PACKET_SIZE 4
35 #define DSI_HSA_PACKET_EXTRA_SIZE 6
36 #define DSI_HBP_PACKET_EXTRA_SIZE 6
37 #define DSI_HACTIVE_PACKET_EXTRA_SIZE 6
38 #define DSI_HFP_PACKET_EXTRA_SIZE 6
39 #define DSI_EOTP_PACKET_SIZE 4
46 static const u32 lfsr_converts[] = {
47 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
48 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
49 106, 53, 282, 397, 354, 227, 113, 56, 284, 142, /* 81 - 90 */
53 #ifdef DSI_CLK_FROM_RR
55 static u32 dsi_rr_formula(const struct drm_display_mode *mode,
56 int pixel_format, int video_mode_format,
57 int lane_count, bool eotp)
60 u32 hactive, vactive, hfp, hsync, hbp, vfp, vsync, vbp;
61 u32 hsync_bytes, hbp_bytes, hactive_bytes, hfp_bytes;
62 u32 bytes_per_line, bytes_per_frame;
64 u32 bytes_per_x_frames, bytes_per_x_frames_x_lanes;
68 switch (pixel_format) {
70 case VID_MODE_FORMAT_RGB888:
71 case VID_MODE_FORMAT_RGB666_LOOSE:
74 case VID_MODE_FORMAT_RGB666:
77 case VID_MODE_FORMAT_RGB565:
82 hactive = mode->hdisplay;
83 vactive = mode->vdisplay;
84 hfp = mode->hsync_start - mode->hdisplay;
85 hsync = mode->hsync_end - mode->hsync_start;
86 hbp = mode->htotal - mode->hsync_end;
88 vfp = mode->vsync_start - mode->vdisplay;
89 vsync = mode->vsync_end - mode->vsync_start;
90 vbp = mode->vtotal - mode->vsync_end;
92 hsync_bytes = DIV_ROUND_UP(hsync * bpp, 8);
93 hbp_bytes = DIV_ROUND_UP(hbp * bpp, 8);
94 hactive_bytes = DIV_ROUND_UP(hactive * bpp, 8);
95 hfp_bytes = DIV_ROUND_UP(hfp * bpp, 8);
97 bytes_per_line = DSI_HSS_PACKET_SIZE + hsync_bytes +
98 DSI_HSA_PACKET_EXTRA_SIZE + DSI_HSE_PACKET_SIZE +
99 hbp_bytes + DSI_HBP_PACKET_EXTRA_SIZE +
100 hactive_bytes + DSI_HACTIVE_PACKET_EXTRA_SIZE +
101 hfp_bytes + DSI_HFP_PACKET_EXTRA_SIZE;
104 * XXX: Need to accurately calculate LP to HS transition timeout and add
105 * it to bytes_per_line/bytes_per_frame.
108 if (eotp && video_mode_format == VIDEO_MODE_BURST)
109 bytes_per_line += DSI_EOTP_PACKET_SIZE;
111 bytes_per_frame = vsync * bytes_per_line + vbp * bytes_per_line +
112 vactive * bytes_per_line + vfp * bytes_per_line;
115 (video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ||
116 video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS))
117 bytes_per_frame += DSI_EOTP_PACKET_SIZE;
119 num_frames = drm_mode_vrefresh(mode);
120 bytes_per_x_frames = num_frames * bytes_per_frame;
122 bytes_per_x_frames_x_lanes = bytes_per_x_frames / lane_count;
124 /* the dsi clock is divided by 2 in the hardware to get dsi ddr clock */
125 dsi_bit_clock_hz = bytes_per_x_frames_x_lanes * 8;
126 dsi_clk = dsi_bit_clock_hz / 1000;
128 if (eotp && video_mode_format == VIDEO_MODE_BURST)
136 /* Get DSI clock from pixel clock */
137 static u32 dsi_clk_from_pclk(const struct drm_display_mode *mode,
138 int pixel_format, int lane_count)
143 switch (pixel_format) {
145 case VID_MODE_FORMAT_RGB888:
146 case VID_MODE_FORMAT_RGB666_LOOSE:
149 case VID_MODE_FORMAT_RGB666:
152 case VID_MODE_FORMAT_RGB565:
157 /* DSI data rate = pixel clock * bits per pixel / lane count
158 pixel clock is converted from KHz to Hz */
159 dsi_clk_khz = DIV_ROUND_CLOSEST(mode->clock * bpp, lane_count);
166 static int dsi_calc_mnp(u32 dsi_clk, struct dsi_mnp *dsi_mnp)
178 /* dsi_clk is expected in KHZ */
179 if (dsi_clk < 300000 || dsi_clk > 1150000) {
180 DRM_ERROR("DSI CLK Out of Range\n");
185 target_dsi_clk = dsi_clk;
187 tmp_error = 0xFFFFFFFF;
191 for (m = 62; m <= 92; m++) {
192 for (p = 2; p <= 6; p++) {
193 /* Find the optimal m and p divisors
194 with minimal error +/- the required clock */
195 calc_dsi_clk = (m * ref_clk) / p;
196 if (calc_dsi_clk == target_dsi_clk) {
202 tmp_error = abs(target_dsi_clk - calc_dsi_clk);
204 if (tmp_error < error) {
215 m_seed = lfsr_converts[calc_m - 62];
217 dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
218 dsi_mnp->dsi_pll_div = (n - 1) << DSI_PLL_N1_DIV_SHIFT |
219 m_seed << DSI_PLL_M1_DIV_SHIFT;
225 * XXX: The muxing and gating is hard coded for now. Need to add support for
226 * sharing PLLs with two DSI outputs.
228 static void vlv_configure_dsi_pll(struct intel_encoder *encoder)
230 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
231 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
232 const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
233 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
235 struct dsi_mnp dsi_mnp;
238 dsi_clk = dsi_clk_from_pclk(mode, intel_dsi->pixel_format,
239 intel_dsi->lane_count);
241 ret = dsi_calc_mnp(dsi_clk, &dsi_mnp);
243 DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
247 dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
249 DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
250 dsi_mnp.dsi_pll_div, dsi_mnp.dsi_pll_ctrl);
252 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
253 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, dsi_mnp.dsi_pll_div);
254 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, dsi_mnp.dsi_pll_ctrl);
257 void vlv_enable_dsi_pll(struct intel_encoder *encoder)
259 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
264 mutex_lock(&dev_priv->dpio_lock);
266 vlv_configure_dsi_pll(encoder);
268 /* wait at least 0.5 us after ungating before enabling VCO */
271 tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
272 tmp |= DSI_PLL_VCO_EN;
273 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
275 mutex_unlock(&dev_priv->dpio_lock);
277 if (wait_for(I915_READ(PIPECONF(PIPE_A)) & PIPECONF_DSI_PLL_LOCKED, 20)) {
278 DRM_ERROR("DSI PLL lock failed\n");
282 DRM_DEBUG_KMS("DSI PLL locked\n");
285 void vlv_disable_dsi_pll(struct intel_encoder *encoder)
287 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
292 mutex_lock(&dev_priv->dpio_lock);
294 tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
295 tmp &= ~DSI_PLL_VCO_EN;
296 tmp |= DSI_PLL_LDO_GATE;
297 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
299 mutex_unlock(&dev_priv->dpio_lock);