xref: /dflybsd-src/sys/dev/drm/i915/intel_dsi_pll.c (revision 24edb8848e2499ece59b84a04f554a7a897feeab)
19edbd4a0SFrançois Tigeot /*
29edbd4a0SFrançois Tigeot  * Copyright © 2013 Intel Corporation
39edbd4a0SFrançois Tigeot  *
49edbd4a0SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
59edbd4a0SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
69edbd4a0SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
79edbd4a0SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
89edbd4a0SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
99edbd4a0SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
109edbd4a0SFrançois Tigeot  *
119edbd4a0SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
129edbd4a0SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
139edbd4a0SFrançois Tigeot  * Software.
149edbd4a0SFrançois Tigeot  *
159edbd4a0SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169edbd4a0SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
179edbd4a0SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
189edbd4a0SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
199edbd4a0SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
209edbd4a0SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
219edbd4a0SFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
229edbd4a0SFrançois Tigeot  *
239edbd4a0SFrançois Tigeot  * Authors:
249edbd4a0SFrançois Tigeot  *	Shobhit Kumar <shobhit.kumar@intel.com>
259edbd4a0SFrançois Tigeot  *	Yogesh Mohan Marimuthu <yogesh.mohan.marimuthu@intel.com>
269edbd4a0SFrançois Tigeot  */
279edbd4a0SFrançois Tigeot 
289edbd4a0SFrançois Tigeot #include <linux/kernel.h>
299edbd4a0SFrançois Tigeot #include "intel_drv.h"
309edbd4a0SFrançois Tigeot #include "i915_drv.h"
319edbd4a0SFrançois Tigeot #include "intel_dsi.h"
329edbd4a0SFrançois Tigeot 
339edbd4a0SFrançois Tigeot #define DSI_HSS_PACKET_SIZE		4
349edbd4a0SFrançois Tigeot #define DSI_HSE_PACKET_SIZE		4
359edbd4a0SFrançois Tigeot #define DSI_HSA_PACKET_EXTRA_SIZE	6
369edbd4a0SFrançois Tigeot #define DSI_HBP_PACKET_EXTRA_SIZE	6
379edbd4a0SFrançois Tigeot #define DSI_HACTIVE_PACKET_EXTRA_SIZE	6
389edbd4a0SFrançois Tigeot #define DSI_HFP_PACKET_EXTRA_SIZE	6
399edbd4a0SFrançois Tigeot #define DSI_EOTP_PACKET_SIZE		4
409edbd4a0SFrançois Tigeot 
419edbd4a0SFrançois Tigeot struct dsi_mnp {
429edbd4a0SFrançois Tigeot 	u32 dsi_pll_ctrl;
439edbd4a0SFrançois Tigeot 	u32 dsi_pll_div;
449edbd4a0SFrançois Tigeot };
459edbd4a0SFrançois Tigeot 
469edbd4a0SFrançois Tigeot static const u32 lfsr_converts[] = {
479edbd4a0SFrançois Tigeot 	426, 469, 234, 373, 442, 221, 110, 311, 411,		/* 62 - 70 */
489edbd4a0SFrançois Tigeot 	461, 486, 243, 377, 188, 350, 175, 343, 427, 213,	/* 71 - 80 */
499edbd4a0SFrançois Tigeot 	106, 53, 282, 397, 354, 227, 113, 56, 284, 142,		/* 81 - 90 */
509edbd4a0SFrançois Tigeot 	71, 35							/* 91 - 92 */
519edbd4a0SFrançois Tigeot };
529edbd4a0SFrançois Tigeot 
539edbd4a0SFrançois Tigeot #ifdef DSI_CLK_FROM_RR
549edbd4a0SFrançois Tigeot 
559edbd4a0SFrançois Tigeot static u32 dsi_rr_formula(const struct drm_display_mode *mode,
569edbd4a0SFrançois Tigeot 			  int pixel_format, int video_mode_format,
579edbd4a0SFrançois Tigeot 			  int lane_count, bool eotp)
589edbd4a0SFrançois Tigeot {
599edbd4a0SFrançois Tigeot 	u32 bpp;
609edbd4a0SFrançois Tigeot 	u32 hactive, vactive, hfp, hsync, hbp, vfp, vsync, vbp;
619edbd4a0SFrançois Tigeot 	u32 hsync_bytes, hbp_bytes, hactive_bytes, hfp_bytes;
629edbd4a0SFrançois Tigeot 	u32 bytes_per_line, bytes_per_frame;
639edbd4a0SFrançois Tigeot 	u32 num_frames;
649edbd4a0SFrançois Tigeot 	u32 bytes_per_x_frames, bytes_per_x_frames_x_lanes;
659edbd4a0SFrançois Tigeot 	u32 dsi_bit_clock_hz;
669edbd4a0SFrançois Tigeot 	u32 dsi_clk;
679edbd4a0SFrançois Tigeot 
689edbd4a0SFrançois Tigeot 	switch (pixel_format) {
699edbd4a0SFrançois Tigeot 	default:
709edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB888:
719edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666_LOOSE:
729edbd4a0SFrançois Tigeot 		bpp = 24;
739edbd4a0SFrançois Tigeot 		break;
749edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666:
759edbd4a0SFrançois Tigeot 		bpp = 18;
769edbd4a0SFrançois Tigeot 		break;
779edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB565:
789edbd4a0SFrançois Tigeot 		bpp = 16;
799edbd4a0SFrançois Tigeot 		break;
809edbd4a0SFrançois Tigeot 	}
819edbd4a0SFrançois Tigeot 
829edbd4a0SFrançois Tigeot 	hactive = mode->hdisplay;
839edbd4a0SFrançois Tigeot 	vactive = mode->vdisplay;
849edbd4a0SFrançois Tigeot 	hfp = mode->hsync_start - mode->hdisplay;
859edbd4a0SFrançois Tigeot 	hsync = mode->hsync_end - mode->hsync_start;
869edbd4a0SFrançois Tigeot 	hbp = mode->htotal - mode->hsync_end;
879edbd4a0SFrançois Tigeot 
889edbd4a0SFrançois Tigeot 	vfp = mode->vsync_start - mode->vdisplay;
899edbd4a0SFrançois Tigeot 	vsync = mode->vsync_end - mode->vsync_start;
909edbd4a0SFrançois Tigeot 	vbp = mode->vtotal - mode->vsync_end;
919edbd4a0SFrançois Tigeot 
929edbd4a0SFrançois Tigeot 	hsync_bytes = DIV_ROUND_UP(hsync * bpp, 8);
939edbd4a0SFrançois Tigeot 	hbp_bytes = DIV_ROUND_UP(hbp * bpp, 8);
949edbd4a0SFrançois Tigeot 	hactive_bytes = DIV_ROUND_UP(hactive * bpp, 8);
959edbd4a0SFrançois Tigeot 	hfp_bytes = DIV_ROUND_UP(hfp * bpp, 8);
969edbd4a0SFrançois Tigeot 
979edbd4a0SFrançois Tigeot 	bytes_per_line = DSI_HSS_PACKET_SIZE + hsync_bytes +
989edbd4a0SFrançois Tigeot 		DSI_HSA_PACKET_EXTRA_SIZE + DSI_HSE_PACKET_SIZE +
999edbd4a0SFrançois Tigeot 		hbp_bytes + DSI_HBP_PACKET_EXTRA_SIZE +
1009edbd4a0SFrançois Tigeot 		hactive_bytes + DSI_HACTIVE_PACKET_EXTRA_SIZE +
1019edbd4a0SFrançois Tigeot 		hfp_bytes + DSI_HFP_PACKET_EXTRA_SIZE;
1029edbd4a0SFrançois Tigeot 
1039edbd4a0SFrançois Tigeot 	/*
1049edbd4a0SFrançois Tigeot 	 * XXX: Need to accurately calculate LP to HS transition timeout and add
1059edbd4a0SFrançois Tigeot 	 * it to bytes_per_line/bytes_per_frame.
1069edbd4a0SFrançois Tigeot 	 */
1079edbd4a0SFrançois Tigeot 
1089edbd4a0SFrançois Tigeot 	if (eotp && video_mode_format == VIDEO_MODE_BURST)
1099edbd4a0SFrançois Tigeot 		bytes_per_line += DSI_EOTP_PACKET_SIZE;
1109edbd4a0SFrançois Tigeot 
1119edbd4a0SFrançois Tigeot 	bytes_per_frame = vsync * bytes_per_line + vbp * bytes_per_line +
1129edbd4a0SFrançois Tigeot 		vactive * bytes_per_line + vfp * bytes_per_line;
1139edbd4a0SFrançois Tigeot 
1149edbd4a0SFrançois Tigeot 	if (eotp &&
1159edbd4a0SFrançois Tigeot 	    (video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ||
1169edbd4a0SFrançois Tigeot 	     video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS))
1179edbd4a0SFrançois Tigeot 		bytes_per_frame += DSI_EOTP_PACKET_SIZE;
1189edbd4a0SFrançois Tigeot 
1199edbd4a0SFrançois Tigeot 	num_frames = drm_mode_vrefresh(mode);
1209edbd4a0SFrançois Tigeot 	bytes_per_x_frames = num_frames * bytes_per_frame;
1219edbd4a0SFrançois Tigeot 
1229edbd4a0SFrançois Tigeot 	bytes_per_x_frames_x_lanes = bytes_per_x_frames / lane_count;
1239edbd4a0SFrançois Tigeot 
1249edbd4a0SFrançois Tigeot 	/* the dsi clock is divided by 2 in the hardware to get dsi ddr clock */
1259edbd4a0SFrançois Tigeot 	dsi_bit_clock_hz = bytes_per_x_frames_x_lanes * 8;
1269edbd4a0SFrançois Tigeot 	dsi_clk = dsi_bit_clock_hz / 1000;
1279edbd4a0SFrançois Tigeot 
1289edbd4a0SFrançois Tigeot 	if (eotp && video_mode_format == VIDEO_MODE_BURST)
1299edbd4a0SFrançois Tigeot 		dsi_clk *= 2;
1309edbd4a0SFrançois Tigeot 
1319edbd4a0SFrançois Tigeot 	return dsi_clk;
1329edbd4a0SFrançois Tigeot }
1339edbd4a0SFrançois Tigeot 
1349edbd4a0SFrançois Tigeot #else
1359edbd4a0SFrançois Tigeot 
1369edbd4a0SFrançois Tigeot /* Get DSI clock from pixel clock */
1379edbd4a0SFrançois Tigeot static u32 dsi_clk_from_pclk(const struct drm_display_mode *mode,
1389edbd4a0SFrançois Tigeot 			  int pixel_format, int lane_count)
1399edbd4a0SFrançois Tigeot {
1409edbd4a0SFrançois Tigeot 	u32 dsi_clk_khz;
1419edbd4a0SFrançois Tigeot 	u32 bpp;
1429edbd4a0SFrançois Tigeot 
1439edbd4a0SFrançois Tigeot 	switch (pixel_format) {
1449edbd4a0SFrançois Tigeot 	default:
1459edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB888:
1469edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666_LOOSE:
1479edbd4a0SFrançois Tigeot 		bpp = 24;
1489edbd4a0SFrançois Tigeot 		break;
1499edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666:
1509edbd4a0SFrançois Tigeot 		bpp = 18;
1519edbd4a0SFrançois Tigeot 		break;
1529edbd4a0SFrançois Tigeot 	case VID_MODE_FORMAT_RGB565:
1539edbd4a0SFrançois Tigeot 		bpp = 16;
1549edbd4a0SFrançois Tigeot 		break;
1559edbd4a0SFrançois Tigeot 	}
1569edbd4a0SFrançois Tigeot 
1579edbd4a0SFrançois Tigeot 	/* DSI data rate = pixel clock * bits per pixel / lane count
1589edbd4a0SFrançois Tigeot 	   pixel clock is converted from KHz to Hz */
1599edbd4a0SFrançois Tigeot 	dsi_clk_khz = DIV_ROUND_CLOSEST(mode->clock * bpp, lane_count);
1609edbd4a0SFrançois Tigeot 
1619edbd4a0SFrançois Tigeot 	return dsi_clk_khz;
1629edbd4a0SFrançois Tigeot }
1639edbd4a0SFrançois Tigeot 
1649edbd4a0SFrançois Tigeot #endif
1659edbd4a0SFrançois Tigeot 
1669edbd4a0SFrançois Tigeot static int dsi_calc_mnp(u32 dsi_clk, struct dsi_mnp *dsi_mnp)
1679edbd4a0SFrançois Tigeot {
1689edbd4a0SFrançois Tigeot 	u32 m, n, p;
1699edbd4a0SFrançois Tigeot 	u32 ref_clk;
1709edbd4a0SFrançois Tigeot 	u32 error;
1719edbd4a0SFrançois Tigeot 	u32 tmp_error;
1729edbd4a0SFrançois Tigeot 	int target_dsi_clk;
1739edbd4a0SFrançois Tigeot 	int calc_dsi_clk;
1749edbd4a0SFrançois Tigeot 	u32 calc_m;
1759edbd4a0SFrançois Tigeot 	u32 calc_p;
1769edbd4a0SFrançois Tigeot 	u32 m_seed;
1779edbd4a0SFrançois Tigeot 
1789edbd4a0SFrançois Tigeot 	/* dsi_clk is expected in KHZ */
1799edbd4a0SFrançois Tigeot 	if (dsi_clk < 300000 || dsi_clk > 1150000) {
1809edbd4a0SFrançois Tigeot 		DRM_ERROR("DSI CLK Out of Range\n");
1819edbd4a0SFrançois Tigeot 		return -ECHRNG;
1829edbd4a0SFrançois Tigeot 	}
1839edbd4a0SFrançois Tigeot 
1849edbd4a0SFrançois Tigeot 	ref_clk = 25000;
1859edbd4a0SFrançois Tigeot 	target_dsi_clk = dsi_clk;
1869edbd4a0SFrançois Tigeot 	error = 0xFFFFFFFF;
1879edbd4a0SFrançois Tigeot 	tmp_error = 0xFFFFFFFF;
1889edbd4a0SFrançois Tigeot 	calc_m = 0;
1899edbd4a0SFrançois Tigeot 	calc_p = 0;
1909edbd4a0SFrançois Tigeot 
1919edbd4a0SFrançois Tigeot 	for (m = 62; m <= 92; m++) {
1929edbd4a0SFrançois Tigeot 		for (p = 2; p <= 6; p++) {
1939edbd4a0SFrançois Tigeot 			/* Find the optimal m and p divisors
1949edbd4a0SFrançois Tigeot 			with minimal error +/- the required clock */
1959edbd4a0SFrançois Tigeot 			calc_dsi_clk = (m * ref_clk) / p;
1969edbd4a0SFrançois Tigeot 			if (calc_dsi_clk == target_dsi_clk) {
1979edbd4a0SFrançois Tigeot 				calc_m = m;
1989edbd4a0SFrançois Tigeot 				calc_p = p;
1999edbd4a0SFrançois Tigeot 				error = 0;
2009edbd4a0SFrançois Tigeot 				break;
2019edbd4a0SFrançois Tigeot 			} else
2029edbd4a0SFrançois Tigeot 				tmp_error = abs(target_dsi_clk - calc_dsi_clk);
2039edbd4a0SFrançois Tigeot 
2049edbd4a0SFrançois Tigeot 			if (tmp_error < error) {
2059edbd4a0SFrançois Tigeot 				error = tmp_error;
2069edbd4a0SFrançois Tigeot 				calc_m = m;
2079edbd4a0SFrançois Tigeot 				calc_p = p;
2089edbd4a0SFrançois Tigeot 			}
2099edbd4a0SFrançois Tigeot 		}
2109edbd4a0SFrançois Tigeot 
2119edbd4a0SFrançois Tigeot 		if (error == 0)
2129edbd4a0SFrançois Tigeot 			break;
2139edbd4a0SFrançois Tigeot 	}
2149edbd4a0SFrançois Tigeot 
2159edbd4a0SFrançois Tigeot 	m_seed = lfsr_converts[calc_m - 62];
2169edbd4a0SFrançois Tigeot 	n = 1;
2179edbd4a0SFrançois Tigeot 	dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
2189edbd4a0SFrançois Tigeot 	dsi_mnp->dsi_pll_div = (n - 1) << DSI_PLL_N1_DIV_SHIFT |
2199edbd4a0SFrançois Tigeot 		m_seed << DSI_PLL_M1_DIV_SHIFT;
2209edbd4a0SFrançois Tigeot 
2219edbd4a0SFrançois Tigeot 	return 0;
2229edbd4a0SFrançois Tigeot }
2239edbd4a0SFrançois Tigeot 
2249edbd4a0SFrançois Tigeot /*
2259edbd4a0SFrançois Tigeot  * XXX: The muxing and gating is hard coded for now. Need to add support for
2269edbd4a0SFrançois Tigeot  * sharing PLLs with two DSI outputs.
2279edbd4a0SFrançois Tigeot  */
2289edbd4a0SFrançois Tigeot static void vlv_configure_dsi_pll(struct intel_encoder *encoder)
2299edbd4a0SFrançois Tigeot {
2309edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
2319edbd4a0SFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
2329edbd4a0SFrançois Tigeot 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
2339edbd4a0SFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
2349edbd4a0SFrançois Tigeot 	int ret;
2359edbd4a0SFrançois Tigeot 	struct dsi_mnp dsi_mnp;
2369edbd4a0SFrançois Tigeot 	u32 dsi_clk;
2379edbd4a0SFrançois Tigeot 
2389edbd4a0SFrançois Tigeot 	dsi_clk = dsi_clk_from_pclk(mode, intel_dsi->pixel_format,
2399edbd4a0SFrançois Tigeot 						intel_dsi->lane_count);
2409edbd4a0SFrançois Tigeot 
2419edbd4a0SFrançois Tigeot 	ret = dsi_calc_mnp(dsi_clk, &dsi_mnp);
2429edbd4a0SFrançois Tigeot 	if (ret) {
2439edbd4a0SFrançois Tigeot 		DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
2449edbd4a0SFrançois Tigeot 		return;
2459edbd4a0SFrançois Tigeot 	}
2469edbd4a0SFrançois Tigeot 
2479edbd4a0SFrançois Tigeot 	dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
2489edbd4a0SFrançois Tigeot 
2499edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
2509edbd4a0SFrançois Tigeot 		      dsi_mnp.dsi_pll_div, dsi_mnp.dsi_pll_ctrl);
2519edbd4a0SFrançois Tigeot 
2529edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
2539edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, dsi_mnp.dsi_pll_div);
2549edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, dsi_mnp.dsi_pll_ctrl);
2559edbd4a0SFrançois Tigeot }
2569edbd4a0SFrançois Tigeot 
2579edbd4a0SFrançois Tigeot void vlv_enable_dsi_pll(struct intel_encoder *encoder)
2589edbd4a0SFrançois Tigeot {
2599edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
2609edbd4a0SFrançois Tigeot 	u32 tmp;
2619edbd4a0SFrançois Tigeot 
2629edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
2639edbd4a0SFrançois Tigeot 
2649edbd4a0SFrançois Tigeot 	mutex_lock(&dev_priv->dpio_lock);
2659edbd4a0SFrançois Tigeot 
2669edbd4a0SFrançois Tigeot 	vlv_configure_dsi_pll(encoder);
2679edbd4a0SFrançois Tigeot 
2689edbd4a0SFrançois Tigeot 	/* wait at least 0.5 us after ungating before enabling VCO */
2699edbd4a0SFrançois Tigeot 	usleep_range(1, 10);
2709edbd4a0SFrançois Tigeot 
2719edbd4a0SFrançois Tigeot 	tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
2729edbd4a0SFrançois Tigeot 	tmp |= DSI_PLL_VCO_EN;
2739edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
2749edbd4a0SFrançois Tigeot 
2759edbd4a0SFrançois Tigeot 	mutex_unlock(&dev_priv->dpio_lock);
2769edbd4a0SFrançois Tigeot 
2779edbd4a0SFrançois Tigeot 	if (wait_for(I915_READ(PIPECONF(PIPE_A)) & PIPECONF_DSI_PLL_LOCKED, 20)) {
2789edbd4a0SFrançois Tigeot 		DRM_ERROR("DSI PLL lock failed\n");
2799edbd4a0SFrançois Tigeot 		return;
2809edbd4a0SFrançois Tigeot 	}
2819edbd4a0SFrançois Tigeot 
2829edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("DSI PLL locked\n");
2839edbd4a0SFrançois Tigeot }
2849edbd4a0SFrançois Tigeot 
2859edbd4a0SFrançois Tigeot void vlv_disable_dsi_pll(struct intel_encoder *encoder)
2869edbd4a0SFrançois Tigeot {
2879edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
2889edbd4a0SFrançois Tigeot 	u32 tmp;
2899edbd4a0SFrançois Tigeot 
2909edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
2919edbd4a0SFrançois Tigeot 
2929edbd4a0SFrançois Tigeot 	mutex_lock(&dev_priv->dpio_lock);
2939edbd4a0SFrançois Tigeot 
2949edbd4a0SFrançois Tigeot 	tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
2959edbd4a0SFrançois Tigeot 	tmp &= ~DSI_PLL_VCO_EN;
2969edbd4a0SFrançois Tigeot 	tmp |= DSI_PLL_LDO_GATE;
2979edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
2989edbd4a0SFrançois Tigeot 
2999edbd4a0SFrançois Tigeot 	mutex_unlock(&dev_priv->dpio_lock);
3009edbd4a0SFrançois Tigeot }
301*24edb884SFrançois Tigeot 
302*24edb884SFrançois Tigeot static void assert_bpp_mismatch(int pixel_format, int pipe_bpp)
303*24edb884SFrançois Tigeot {
304*24edb884SFrançois Tigeot 	int bpp;
305*24edb884SFrançois Tigeot 
306*24edb884SFrançois Tigeot 	switch (pixel_format) {
307*24edb884SFrançois Tigeot 	default:
308*24edb884SFrançois Tigeot 	case VID_MODE_FORMAT_RGB888:
309*24edb884SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666_LOOSE:
310*24edb884SFrançois Tigeot 		bpp = 24;
311*24edb884SFrançois Tigeot 		break;
312*24edb884SFrançois Tigeot 	case VID_MODE_FORMAT_RGB666:
313*24edb884SFrançois Tigeot 		bpp = 18;
314*24edb884SFrançois Tigeot 		break;
315*24edb884SFrançois Tigeot 	case VID_MODE_FORMAT_RGB565:
316*24edb884SFrançois Tigeot 		bpp = 16;
317*24edb884SFrançois Tigeot 		break;
318*24edb884SFrançois Tigeot 	}
319*24edb884SFrançois Tigeot 
320*24edb884SFrançois Tigeot 	WARN(bpp != pipe_bpp,
321*24edb884SFrançois Tigeot 		"bpp match assertion failure (expected %d, current %d)\n",
322*24edb884SFrançois Tigeot 		bpp, pipe_bpp);
323*24edb884SFrançois Tigeot }
324*24edb884SFrançois Tigeot 
325*24edb884SFrançois Tigeot u32 vlv_get_dsi_pclk(struct intel_encoder *encoder, int pipe_bpp)
326*24edb884SFrançois Tigeot {
327*24edb884SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
328*24edb884SFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
329*24edb884SFrançois Tigeot 	u32 dsi_clock, pclk;
330*24edb884SFrançois Tigeot 	u32 pll_ctl, pll_div;
331*24edb884SFrançois Tigeot 	u32 m = 0, p = 0;
332*24edb884SFrançois Tigeot 	int refclk = 25000;
333*24edb884SFrançois Tigeot 	int i;
334*24edb884SFrançois Tigeot 
335*24edb884SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
336*24edb884SFrançois Tigeot 
337*24edb884SFrançois Tigeot 	mutex_lock(&dev_priv->dpio_lock);
338*24edb884SFrançois Tigeot 	pll_ctl = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
339*24edb884SFrançois Tigeot 	pll_div = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_DIVIDER);
340*24edb884SFrançois Tigeot 	mutex_unlock(&dev_priv->dpio_lock);
341*24edb884SFrançois Tigeot 
342*24edb884SFrançois Tigeot 	/* mask out other bits and extract the P1 divisor */
343*24edb884SFrançois Tigeot 	pll_ctl &= DSI_PLL_P1_POST_DIV_MASK;
344*24edb884SFrançois Tigeot 	pll_ctl = pll_ctl >> (DSI_PLL_P1_POST_DIV_SHIFT - 2);
345*24edb884SFrançois Tigeot 
346*24edb884SFrançois Tigeot 	/* mask out the other bits and extract the M1 divisor */
347*24edb884SFrançois Tigeot 	pll_div &= DSI_PLL_M1_DIV_MASK;
348*24edb884SFrançois Tigeot 	pll_div = pll_div >> DSI_PLL_M1_DIV_SHIFT;
349*24edb884SFrançois Tigeot 
350*24edb884SFrançois Tigeot 	while (pll_ctl) {
351*24edb884SFrançois Tigeot 		pll_ctl = pll_ctl >> 1;
352*24edb884SFrançois Tigeot 		p++;
353*24edb884SFrançois Tigeot 	}
354*24edb884SFrançois Tigeot 	p--;
355*24edb884SFrançois Tigeot 
356*24edb884SFrançois Tigeot 	if (!p) {
357*24edb884SFrançois Tigeot 		DRM_ERROR("wrong P1 divisor\n");
358*24edb884SFrançois Tigeot 		return 0;
359*24edb884SFrançois Tigeot 	}
360*24edb884SFrançois Tigeot 
361*24edb884SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
362*24edb884SFrançois Tigeot 		if (lfsr_converts[i] == pll_div)
363*24edb884SFrançois Tigeot 			break;
364*24edb884SFrançois Tigeot 	}
365*24edb884SFrançois Tigeot 
366*24edb884SFrançois Tigeot 	if (i == ARRAY_SIZE(lfsr_converts)) {
367*24edb884SFrançois Tigeot 		DRM_ERROR("wrong m_seed programmed\n");
368*24edb884SFrançois Tigeot 		return 0;
369*24edb884SFrançois Tigeot 	}
370*24edb884SFrançois Tigeot 
371*24edb884SFrançois Tigeot 	m = i + 62;
372*24edb884SFrançois Tigeot 
373*24edb884SFrançois Tigeot 	dsi_clock = (m * refclk) / p;
374*24edb884SFrançois Tigeot 
375*24edb884SFrançois Tigeot 	/* pixel_format and pipe_bpp should agree */
376*24edb884SFrançois Tigeot 	assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
377*24edb884SFrançois Tigeot 
378*24edb884SFrançois Tigeot 	pclk = DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, pipe_bpp);
379*24edb884SFrançois Tigeot 
380*24edb884SFrançois Tigeot 	return pclk;
381*24edb884SFrançois Tigeot }
382