xref: /dflybsd-src/sys/dev/drm/i915/intel_dsi_pll.c (revision 8621f4070e7cb342161183c980ae7fce2fd8124e)
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 
33*8621f407SFrançois Tigeot static const u16 lfsr_converts[] = {
349edbd4a0SFrançois Tigeot 	426, 469, 234, 373, 442, 221, 110, 311, 411,		/* 62 - 70 */
359edbd4a0SFrançois Tigeot 	461, 486, 243, 377, 188, 350, 175, 343, 427, 213,	/* 71 - 80 */
36a05eeebfSFrançois Tigeot 	106, 53, 282, 397, 454, 227, 113, 56, 284, 142,		/* 81 - 90 */
37a05eeebfSFrançois Tigeot 	71, 35, 273, 136, 324, 418, 465, 488, 500, 506		/* 91 - 100 */
389edbd4a0SFrançois Tigeot };
399edbd4a0SFrançois Tigeot 
409edbd4a0SFrançois Tigeot /* Get DSI clock from pixel clock */
41*8621f407SFrançois Tigeot static u32 dsi_clk_from_pclk(u32 pclk, enum mipi_dsi_pixel_format fmt,
42*8621f407SFrançois Tigeot 			     int lane_count)
439edbd4a0SFrançois Tigeot {
449edbd4a0SFrançois Tigeot 	u32 dsi_clk_khz;
45*8621f407SFrançois Tigeot 	u32 bpp = mipi_dsi_pixel_format_to_bpp(fmt);
469edbd4a0SFrançois Tigeot 
479edbd4a0SFrançois Tigeot 	/* DSI data rate = pixel clock * bits per pixel / lane count
489edbd4a0SFrançois Tigeot 	   pixel clock is converted from KHz to Hz */
491b13d190SFrançois Tigeot 	dsi_clk_khz = DIV_ROUND_CLOSEST(pclk * bpp, lane_count);
509edbd4a0SFrançois Tigeot 
519edbd4a0SFrançois Tigeot 	return dsi_clk_khz;
529edbd4a0SFrançois Tigeot }
539edbd4a0SFrançois Tigeot 
54a05eeebfSFrançois Tigeot static int dsi_calc_mnp(struct drm_i915_private *dev_priv,
55*8621f407SFrançois Tigeot 			struct intel_crtc_state *config,
56*8621f407SFrançois Tigeot 			int target_dsi_clk)
579edbd4a0SFrançois Tigeot {
5819c468b4SFrançois Tigeot 	unsigned int calc_m = 0, calc_p = 0;
59a05eeebfSFrançois Tigeot 	unsigned int m_min, m_max, p_min = 2, p_max = 6;
60a05eeebfSFrançois Tigeot 	unsigned int m, n, p;
61a05eeebfSFrançois Tigeot 	int ref_clk;
6219c468b4SFrançois Tigeot 	int delta = target_dsi_clk;
639edbd4a0SFrançois Tigeot 	u32 m_seed;
649edbd4a0SFrançois Tigeot 
6519c468b4SFrançois Tigeot 	/* target_dsi_clk is expected in kHz */
6619c468b4SFrançois Tigeot 	if (target_dsi_clk < 300000 || target_dsi_clk > 1150000) {
679edbd4a0SFrançois Tigeot 		DRM_ERROR("DSI CLK Out of Range\n");
689edbd4a0SFrançois Tigeot 		return -ECHRNG;
699edbd4a0SFrançois Tigeot 	}
709edbd4a0SFrançois Tigeot 
71a05eeebfSFrançois Tigeot 	if (IS_CHERRYVIEW(dev_priv)) {
72a05eeebfSFrançois Tigeot 		ref_clk = 100000;
73a05eeebfSFrançois Tigeot 		n = 4;
74a05eeebfSFrançois Tigeot 		m_min = 70;
75a05eeebfSFrançois Tigeot 		m_max = 96;
76a05eeebfSFrançois Tigeot 	} else {
77a05eeebfSFrançois Tigeot 		ref_clk = 25000;
78a05eeebfSFrançois Tigeot 		n = 1;
79a05eeebfSFrançois Tigeot 		m_min = 62;
80a05eeebfSFrançois Tigeot 		m_max = 92;
81a05eeebfSFrançois Tigeot 	}
82a05eeebfSFrançois Tigeot 
83a05eeebfSFrançois Tigeot 	for (m = m_min; m <= m_max && delta; m++) {
84a05eeebfSFrançois Tigeot 		for (p = p_min; p <= p_max && delta; p++) {
8519c468b4SFrançois Tigeot 			/*
8619c468b4SFrançois Tigeot 			 * Find the optimal m and p divisors with minimal delta
8719c468b4SFrançois Tigeot 			 * +/- the required clock
8819c468b4SFrançois Tigeot 			 */
8919c468b4SFrançois Tigeot 			int calc_dsi_clk = (m * ref_clk) / (p * n);
9019c468b4SFrançois Tigeot 			int d = abs(target_dsi_clk - calc_dsi_clk);
9119c468b4SFrançois Tigeot 			if (d < delta) {
9219c468b4SFrançois Tigeot 				delta = d;
939edbd4a0SFrançois Tigeot 				calc_m = m;
949edbd4a0SFrançois Tigeot 				calc_p = p;
959edbd4a0SFrançois Tigeot 			}
969edbd4a0SFrançois Tigeot 		}
979edbd4a0SFrançois Tigeot 	}
989edbd4a0SFrançois Tigeot 
9919c468b4SFrançois Tigeot 	/* register has log2(N1), this works fine for powers of two */
10019c468b4SFrançois Tigeot 	n = ffs(n) - 1;
1019edbd4a0SFrançois Tigeot 	m_seed = lfsr_converts[calc_m - 62];
102*8621f407SFrançois Tigeot 	config->dsi_pll.ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
103*8621f407SFrançois Tigeot 	config->dsi_pll.div = n << DSI_PLL_N1_DIV_SHIFT |
1049edbd4a0SFrançois Tigeot 		m_seed << DSI_PLL_M1_DIV_SHIFT;
1059edbd4a0SFrançois Tigeot 
1069edbd4a0SFrançois Tigeot 	return 0;
1079edbd4a0SFrançois Tigeot }
1089edbd4a0SFrançois Tigeot 
1099edbd4a0SFrançois Tigeot /*
1109edbd4a0SFrançois Tigeot  * XXX: The muxing and gating is hard coded for now. Need to add support for
1119edbd4a0SFrançois Tigeot  * sharing PLLs with two DSI outputs.
1129edbd4a0SFrançois Tigeot  */
113*8621f407SFrançois Tigeot static int vlv_compute_dsi_pll(struct intel_encoder *encoder,
114*8621f407SFrançois Tigeot 			       struct intel_crtc_state *config)
1159edbd4a0SFrançois Tigeot {
1169edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
1179edbd4a0SFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
1189edbd4a0SFrançois Tigeot 	int ret;
1199edbd4a0SFrançois Tigeot 	u32 dsi_clk;
1209edbd4a0SFrançois Tigeot 
1211b13d190SFrançois Tigeot 	dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
1229edbd4a0SFrançois Tigeot 				    intel_dsi->lane_count);
1239edbd4a0SFrançois Tigeot 
124*8621f407SFrançois Tigeot 	ret = dsi_calc_mnp(dev_priv, config, dsi_clk);
1259edbd4a0SFrançois Tigeot 	if (ret) {
1269edbd4a0SFrançois Tigeot 		DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
127*8621f407SFrançois Tigeot 		return ret;
1289edbd4a0SFrançois Tigeot 	}
1299edbd4a0SFrançois Tigeot 
1302c9916cdSFrançois Tigeot 	if (intel_dsi->ports & (1 << PORT_A))
131*8621f407SFrançois Tigeot 		config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
1329edbd4a0SFrançois Tigeot 
1332c9916cdSFrançois Tigeot 	if (intel_dsi->ports & (1 << PORT_C))
134*8621f407SFrançois Tigeot 		config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI1_DSIPLL;
135*8621f407SFrançois Tigeot 
136*8621f407SFrançois Tigeot 	config->dsi_pll.ctrl |= DSI_PLL_VCO_EN;
1372c9916cdSFrançois Tigeot 
1389edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
139*8621f407SFrançois Tigeot 		      config->dsi_pll.div, config->dsi_pll.ctrl);
1409edbd4a0SFrançois Tigeot 
141*8621f407SFrançois Tigeot 	return 0;
1429edbd4a0SFrançois Tigeot }
1439edbd4a0SFrançois Tigeot 
144*8621f407SFrançois Tigeot static void vlv_enable_dsi_pll(struct intel_encoder *encoder,
145*8621f407SFrançois Tigeot 			       const struct intel_crtc_state *config)
1469edbd4a0SFrançois Tigeot {
147*8621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1489edbd4a0SFrançois Tigeot 
1499edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
1509edbd4a0SFrançois Tigeot 
15119c468b4SFrançois Tigeot 	mutex_lock(&dev_priv->sb_lock);
1529edbd4a0SFrançois Tigeot 
153*8621f407SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
154*8621f407SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, config->dsi_pll.div);
155*8621f407SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL,
156*8621f407SFrançois Tigeot 		      config->dsi_pll.ctrl & ~DSI_PLL_VCO_EN);
1579edbd4a0SFrançois Tigeot 
1589edbd4a0SFrançois Tigeot 	/* wait at least 0.5 us after ungating before enabling VCO */
1599edbd4a0SFrançois Tigeot 	usleep_range(1, 10);
1609edbd4a0SFrançois Tigeot 
161*8621f407SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, config->dsi_pll.ctrl);
1629edbd4a0SFrançois Tigeot 
1632c9916cdSFrançois Tigeot 	if (wait_for(vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL) &
1642c9916cdSFrançois Tigeot 						DSI_PLL_LOCK, 20)) {
1659edbd4a0SFrançois Tigeot 
16619c468b4SFrançois Tigeot 		mutex_unlock(&dev_priv->sb_lock);
1679edbd4a0SFrançois Tigeot 		DRM_ERROR("DSI PLL lock failed\n");
1689edbd4a0SFrançois Tigeot 		return;
1699edbd4a0SFrançois Tigeot 	}
17019c468b4SFrançois Tigeot 	mutex_unlock(&dev_priv->sb_lock);
1719edbd4a0SFrançois Tigeot 
1729edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("DSI PLL locked\n");
1739edbd4a0SFrançois Tigeot }
1749edbd4a0SFrançois Tigeot 
175352ff8bdSFrançois Tigeot static void vlv_disable_dsi_pll(struct intel_encoder *encoder)
1769edbd4a0SFrançois Tigeot {
177*8621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1789edbd4a0SFrançois Tigeot 	u32 tmp;
1799edbd4a0SFrançois Tigeot 
1809edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
1819edbd4a0SFrançois Tigeot 
18219c468b4SFrançois Tigeot 	mutex_lock(&dev_priv->sb_lock);
1839edbd4a0SFrançois Tigeot 
1849edbd4a0SFrançois Tigeot 	tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1859edbd4a0SFrançois Tigeot 	tmp &= ~DSI_PLL_VCO_EN;
1869edbd4a0SFrançois Tigeot 	tmp |= DSI_PLL_LDO_GATE;
1879edbd4a0SFrançois Tigeot 	vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
1889edbd4a0SFrançois Tigeot 
18919c468b4SFrançois Tigeot 	mutex_unlock(&dev_priv->sb_lock);
1909edbd4a0SFrançois Tigeot }
19124edb884SFrançois Tigeot 
192*8621f407SFrançois Tigeot static bool bxt_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
193*8621f407SFrançois Tigeot {
194*8621f407SFrançois Tigeot 	bool enabled;
195*8621f407SFrançois Tigeot 	u32 val;
196*8621f407SFrançois Tigeot 	u32 mask;
197*8621f407SFrançois Tigeot 
198*8621f407SFrançois Tigeot 	mask = BXT_DSI_PLL_DO_ENABLE | BXT_DSI_PLL_LOCKED;
199*8621f407SFrançois Tigeot 	val = I915_READ(BXT_DSI_PLL_ENABLE);
200*8621f407SFrançois Tigeot 	enabled = (val & mask) == mask;
201*8621f407SFrançois Tigeot 
202*8621f407SFrançois Tigeot 	if (!enabled)
203*8621f407SFrançois Tigeot 		return false;
204*8621f407SFrançois Tigeot 
205*8621f407SFrançois Tigeot 	/*
206*8621f407SFrançois Tigeot 	 * Both dividers must be programmed with valid values even if only one
207*8621f407SFrançois Tigeot 	 * of the PLL is used, see BSpec/Broxton Clocks. Check this here for
208*8621f407SFrançois Tigeot 	 * paranoia, since BIOS is known to misconfigure PLLs in this way at
209*8621f407SFrançois Tigeot 	 * times, and since accessing DSI registers with invalid dividers
210*8621f407SFrançois Tigeot 	 * causes a system hang.
211*8621f407SFrançois Tigeot 	 */
212*8621f407SFrançois Tigeot 	val = I915_READ(BXT_DSI_PLL_CTL);
213*8621f407SFrançois Tigeot 	if (!(val & BXT_DSIA_16X_MASK) || !(val & BXT_DSIC_16X_MASK)) {
214*8621f407SFrançois Tigeot 		DRM_DEBUG_DRIVER("PLL is enabled with invalid divider settings (%08x)\n",
215*8621f407SFrançois Tigeot 				 val);
216*8621f407SFrançois Tigeot 		enabled = false;
217*8621f407SFrançois Tigeot 	}
218*8621f407SFrançois Tigeot 
219*8621f407SFrançois Tigeot 	return enabled;
220*8621f407SFrançois Tigeot }
221*8621f407SFrançois Tigeot 
222352ff8bdSFrançois Tigeot static void bxt_disable_dsi_pll(struct intel_encoder *encoder)
223352ff8bdSFrançois Tigeot {
224*8621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
225352ff8bdSFrançois Tigeot 	u32 val;
226352ff8bdSFrançois Tigeot 
227352ff8bdSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
228352ff8bdSFrançois Tigeot 
229352ff8bdSFrançois Tigeot 	val = I915_READ(BXT_DSI_PLL_ENABLE);
230352ff8bdSFrançois Tigeot 	val &= ~BXT_DSI_PLL_DO_ENABLE;
231352ff8bdSFrançois Tigeot 	I915_WRITE(BXT_DSI_PLL_ENABLE, val);
232352ff8bdSFrançois Tigeot 
233352ff8bdSFrançois Tigeot 	/*
234352ff8bdSFrançois Tigeot 	 * PLL lock should deassert within 200us.
235352ff8bdSFrançois Tigeot 	 * Wait up to 1ms before timing out.
236352ff8bdSFrançois Tigeot 	 */
237352ff8bdSFrançois Tigeot 	if (wait_for((I915_READ(BXT_DSI_PLL_ENABLE)
238352ff8bdSFrançois Tigeot 					& BXT_DSI_PLL_LOCKED) == 0, 1))
239352ff8bdSFrançois Tigeot 		DRM_ERROR("Timeout waiting for PLL lock deassertion\n");
240352ff8bdSFrançois Tigeot }
241352ff8bdSFrançois Tigeot 
242*8621f407SFrançois Tigeot static void assert_bpp_mismatch(enum mipi_dsi_pixel_format fmt, int pipe_bpp)
24324edb884SFrançois Tigeot {
244*8621f407SFrançois Tigeot 	int bpp = mipi_dsi_pixel_format_to_bpp(fmt);
24524edb884SFrançois Tigeot 
24624edb884SFrançois Tigeot 	WARN(bpp != pipe_bpp,
24724edb884SFrançois Tigeot 	     "bpp match assertion failure (expected %d, current %d)\n",
24824edb884SFrançois Tigeot 	     bpp, pipe_bpp);
24924edb884SFrançois Tigeot }
25024edb884SFrançois Tigeot 
251*8621f407SFrançois Tigeot static u32 vlv_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
252*8621f407SFrançois Tigeot 			    struct intel_crtc_state *config)
25324edb884SFrançois Tigeot {
254*8621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
25524edb884SFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
25624edb884SFrançois Tigeot 	u32 dsi_clock, pclk;
25724edb884SFrançois Tigeot 	u32 pll_ctl, pll_div;
25819c468b4SFrançois Tigeot 	u32 m = 0, p = 0, n;
259*8621f407SFrançois Tigeot 	int refclk = IS_CHERRYVIEW(dev_priv) ? 100000 : 25000;
26024edb884SFrançois Tigeot 	int i;
26124edb884SFrançois Tigeot 
26224edb884SFrançois Tigeot 	DRM_DEBUG_KMS("\n");
26324edb884SFrançois Tigeot 
26419c468b4SFrançois Tigeot 	mutex_lock(&dev_priv->sb_lock);
26524edb884SFrançois Tigeot 	pll_ctl = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
26624edb884SFrançois Tigeot 	pll_div = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_DIVIDER);
26719c468b4SFrançois Tigeot 	mutex_unlock(&dev_priv->sb_lock);
26824edb884SFrançois Tigeot 
269*8621f407SFrançois Tigeot 	config->dsi_pll.ctrl = pll_ctl & ~DSI_PLL_LOCK;
270*8621f407SFrançois Tigeot 	config->dsi_pll.div = pll_div;
271*8621f407SFrançois Tigeot 
27224edb884SFrançois Tigeot 	/* mask out other bits and extract the P1 divisor */
27324edb884SFrançois Tigeot 	pll_ctl &= DSI_PLL_P1_POST_DIV_MASK;
27424edb884SFrançois Tigeot 	pll_ctl = pll_ctl >> (DSI_PLL_P1_POST_DIV_SHIFT - 2);
27524edb884SFrançois Tigeot 
27619c468b4SFrançois Tigeot 	/* N1 divisor */
27719c468b4SFrançois Tigeot 	n = (pll_div & DSI_PLL_N1_DIV_MASK) >> DSI_PLL_N1_DIV_SHIFT;
27819c468b4SFrançois Tigeot 	n = 1 << n; /* register has log2(N1) */
27919c468b4SFrançois Tigeot 
28024edb884SFrançois Tigeot 	/* mask out the other bits and extract the M1 divisor */
28124edb884SFrançois Tigeot 	pll_div &= DSI_PLL_M1_DIV_MASK;
28224edb884SFrançois Tigeot 	pll_div = pll_div >> DSI_PLL_M1_DIV_SHIFT;
28324edb884SFrançois Tigeot 
28424edb884SFrançois Tigeot 	while (pll_ctl) {
28524edb884SFrançois Tigeot 		pll_ctl = pll_ctl >> 1;
28624edb884SFrançois Tigeot 		p++;
28724edb884SFrançois Tigeot 	}
28824edb884SFrançois Tigeot 	p--;
28924edb884SFrançois Tigeot 
29024edb884SFrançois Tigeot 	if (!p) {
29124edb884SFrançois Tigeot 		DRM_ERROR("wrong P1 divisor\n");
29224edb884SFrançois Tigeot 		return 0;
29324edb884SFrançois Tigeot 	}
29424edb884SFrançois Tigeot 
29524edb884SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
29624edb884SFrançois Tigeot 		if (lfsr_converts[i] == pll_div)
29724edb884SFrançois Tigeot 			break;
29824edb884SFrançois Tigeot 	}
29924edb884SFrançois Tigeot 
30024edb884SFrançois Tigeot 	if (i == ARRAY_SIZE(lfsr_converts)) {
30124edb884SFrançois Tigeot 		DRM_ERROR("wrong m_seed programmed\n");
30224edb884SFrançois Tigeot 		return 0;
30324edb884SFrançois Tigeot 	}
30424edb884SFrançois Tigeot 
30524edb884SFrançois Tigeot 	m = i + 62;
30624edb884SFrançois Tigeot 
30719c468b4SFrançois Tigeot 	dsi_clock = (m * refclk) / (p * n);
30824edb884SFrançois Tigeot 
30924edb884SFrançois Tigeot 	/* pixel_format and pipe_bpp should agree */
31024edb884SFrançois Tigeot 	assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
31124edb884SFrançois Tigeot 
31224edb884SFrançois Tigeot 	pclk = DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, pipe_bpp);
31324edb884SFrançois Tigeot 
31424edb884SFrançois Tigeot 	return pclk;
31524edb884SFrançois Tigeot }
316352ff8bdSFrançois Tigeot 
317*8621f407SFrançois Tigeot static u32 bxt_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
318*8621f407SFrançois Tigeot 			    struct intel_crtc_state *config)
319352ff8bdSFrançois Tigeot {
320352ff8bdSFrançois Tigeot 	u32 pclk;
321352ff8bdSFrançois Tigeot 	u32 dsi_clk;
322352ff8bdSFrançois Tigeot 	u32 dsi_ratio;
323352ff8bdSFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
324352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
325352ff8bdSFrançois Tigeot 
326352ff8bdSFrançois Tigeot 	/* Divide by zero */
327352ff8bdSFrançois Tigeot 	if (!pipe_bpp) {
328352ff8bdSFrançois Tigeot 		DRM_ERROR("Invalid BPP(0)\n");
329352ff8bdSFrançois Tigeot 		return 0;
330352ff8bdSFrançois Tigeot 	}
331352ff8bdSFrançois Tigeot 
332*8621f407SFrançois Tigeot 	config->dsi_pll.ctrl = I915_READ(BXT_DSI_PLL_CTL);
333352ff8bdSFrançois Tigeot 
334*8621f407SFrançois Tigeot 	dsi_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
335352ff8bdSFrançois Tigeot 
336352ff8bdSFrançois Tigeot 	dsi_clk = (dsi_ratio * BXT_REF_CLOCK_KHZ) / 2;
337352ff8bdSFrançois Tigeot 
338352ff8bdSFrançois Tigeot 	/* pixel_format and pipe_bpp should agree */
339352ff8bdSFrançois Tigeot 	assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
340352ff8bdSFrançois Tigeot 
341352ff8bdSFrançois Tigeot 	pclk = DIV_ROUND_CLOSEST(dsi_clk * intel_dsi->lane_count, pipe_bpp);
342352ff8bdSFrançois Tigeot 
343352ff8bdSFrançois Tigeot 	DRM_DEBUG_DRIVER("Calculated pclk=%u\n", pclk);
344352ff8bdSFrançois Tigeot 	return pclk;
345352ff8bdSFrançois Tigeot }
346352ff8bdSFrançois Tigeot 
347*8621f407SFrançois Tigeot u32 intel_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
348*8621f407SFrançois Tigeot 		       struct intel_crtc_state *config)
349c0e85e96SFrançois Tigeot {
350c0e85e96SFrançois Tigeot 	if (IS_BROXTON(encoder->base.dev))
351*8621f407SFrançois Tigeot 		return bxt_dsi_get_pclk(encoder, pipe_bpp, config);
352c0e85e96SFrançois Tigeot 	else
353*8621f407SFrançois Tigeot 		return vlv_dsi_get_pclk(encoder, pipe_bpp, config);
354c0e85e96SFrançois Tigeot }
355c0e85e96SFrançois Tigeot 
356352ff8bdSFrançois Tigeot static void vlv_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
357352ff8bdSFrançois Tigeot {
358352ff8bdSFrançois Tigeot 	u32 temp;
359352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
360352ff8bdSFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
361352ff8bdSFrançois Tigeot 
362352ff8bdSFrançois Tigeot 	temp = I915_READ(MIPI_CTRL(port));
363352ff8bdSFrançois Tigeot 	temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
364352ff8bdSFrançois Tigeot 	I915_WRITE(MIPI_CTRL(port), temp |
365352ff8bdSFrançois Tigeot 			intel_dsi->escape_clk_div <<
366352ff8bdSFrançois Tigeot 			ESCAPE_CLOCK_DIVIDER_SHIFT);
367352ff8bdSFrançois Tigeot }
368352ff8bdSFrançois Tigeot 
369352ff8bdSFrançois Tigeot /* Program BXT Mipi clocks and dividers */
370*8621f407SFrançois Tigeot static void bxt_dsi_program_clocks(struct drm_device *dev, enum port port,
371*8621f407SFrançois Tigeot 				   const struct intel_crtc_state *config)
372352ff8bdSFrançois Tigeot {
373352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
374*8621f407SFrançois Tigeot 	u32 tmp;
375*8621f407SFrançois Tigeot 	u32 dsi_rate = 0;
376*8621f407SFrançois Tigeot 	u32 pll_ratio = 0;
377*8621f407SFrançois Tigeot 	u32 rx_div;
378*8621f407SFrançois Tigeot 	u32 tx_div;
379*8621f407SFrançois Tigeot 	u32 rx_div_upper;
380*8621f407SFrançois Tigeot 	u32 rx_div_lower;
381*8621f407SFrançois Tigeot 	u32 mipi_8by3_divider;
382352ff8bdSFrançois Tigeot 
383352ff8bdSFrançois Tigeot 	/* Clear old configurations */
384352ff8bdSFrançois Tigeot 	tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
385352ff8bdSFrançois Tigeot 	tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
386*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
387*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
388*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
389352ff8bdSFrançois Tigeot 
390352ff8bdSFrançois Tigeot 	/* Get the current DSI rate(actual) */
391*8621f407SFrançois Tigeot 	pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
392352ff8bdSFrançois Tigeot 	dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
393352ff8bdSFrançois Tigeot 
394*8621f407SFrançois Tigeot 	/*
395*8621f407SFrançois Tigeot 	 * tx clock should be <= 20MHz and the div value must be
396*8621f407SFrançois Tigeot 	 * subtracted by 1 as per bspec
397*8621f407SFrançois Tigeot 	 */
398*8621f407SFrançois Tigeot 	tx_div = DIV_ROUND_UP(dsi_rate, 20000) - 1;
399*8621f407SFrançois Tigeot 	/*
400*8621f407SFrançois Tigeot 	 * rx clock should be <= 150MHz and the div value must be
401*8621f407SFrançois Tigeot 	 * subtracted by 1 as per bspec
402*8621f407SFrançois Tigeot 	 */
403*8621f407SFrançois Tigeot 	rx_div = DIV_ROUND_UP(dsi_rate, 150000) - 1;
404352ff8bdSFrançois Tigeot 
405352ff8bdSFrançois Tigeot 	/*
406*8621f407SFrançois Tigeot 	 * rx divider value needs to be updated in the
407*8621f407SFrançois Tigeot 	 * two differnt bit fields in the register hence splitting the
408*8621f407SFrançois Tigeot 	 * rx divider value accordingly
409352ff8bdSFrançois Tigeot 	 */
410*8621f407SFrançois Tigeot 	rx_div_lower = rx_div & RX_DIVIDER_BIT_1_2;
411*8621f407SFrançois Tigeot 	rx_div_upper = (rx_div & RX_DIVIDER_BIT_3_4) >> 2;
412352ff8bdSFrançois Tigeot 
413*8621f407SFrançois Tigeot 	/* As per bpsec program the 8/3X clock divider to the below value */
414*8621f407SFrançois Tigeot 	if (dev_priv->vbt.dsi.config->is_cmd_mode)
415*8621f407SFrançois Tigeot 		mipi_8by3_divider = 0x2;
416*8621f407SFrançois Tigeot 	else
417*8621f407SFrançois Tigeot 		mipi_8by3_divider = 0x3;
418*8621f407SFrançois Tigeot 
419*8621f407SFrançois Tigeot 	tmp |= BXT_MIPI_8X_BY3_DIVIDER(port, mipi_8by3_divider);
420*8621f407SFrançois Tigeot 	tmp |= BXT_MIPI_TX_ESCLK_DIVIDER(port, tx_div);
421*8621f407SFrançois Tigeot 	tmp |= BXT_MIPI_RX_ESCLK_LOWER_DIVIDER(port, rx_div_lower);
422*8621f407SFrançois Tigeot 	tmp |= BXT_MIPI_RX_ESCLK_UPPER_DIVIDER(port, rx_div_upper);
423352ff8bdSFrançois Tigeot 
424352ff8bdSFrançois Tigeot 	I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
425352ff8bdSFrançois Tigeot }
426352ff8bdSFrançois Tigeot 
427*8621f407SFrançois Tigeot static int bxt_compute_dsi_pll(struct intel_encoder *encoder,
428*8621f407SFrançois Tigeot 			       struct intel_crtc_state *config)
429352ff8bdSFrançois Tigeot {
430352ff8bdSFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
431352ff8bdSFrançois Tigeot 	u8 dsi_ratio;
432352ff8bdSFrançois Tigeot 	u32 dsi_clk;
433352ff8bdSFrançois Tigeot 
434352ff8bdSFrançois Tigeot 	dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
435352ff8bdSFrançois Tigeot 				    intel_dsi->lane_count);
436352ff8bdSFrançois Tigeot 
437352ff8bdSFrançois Tigeot 	/*
438352ff8bdSFrançois Tigeot 	 * From clock diagram, to get PLL ratio divider, divide double of DSI
439352ff8bdSFrançois Tigeot 	 * link rate (i.e., 2*8x=16x frequency value) by ref clock. Make sure to
440352ff8bdSFrançois Tigeot 	 * round 'up' the result
441352ff8bdSFrançois Tigeot 	 */
442352ff8bdSFrançois Tigeot 	dsi_ratio = DIV_ROUND_UP(dsi_clk * 2, BXT_REF_CLOCK_KHZ);
443352ff8bdSFrançois Tigeot 	if (dsi_ratio < BXT_DSI_PLL_RATIO_MIN ||
444352ff8bdSFrançois Tigeot 	    dsi_ratio > BXT_DSI_PLL_RATIO_MAX) {
445352ff8bdSFrançois Tigeot 		DRM_ERROR("Cant get a suitable ratio from DSI PLL ratios\n");
446*8621f407SFrançois Tigeot 		return -ECHRNG;
447352ff8bdSFrançois Tigeot 	}
448352ff8bdSFrançois Tigeot 
449352ff8bdSFrançois Tigeot 	/*
450352ff8bdSFrançois Tigeot 	 * Program DSI ratio and Select MIPIC and MIPIA PLL output as 8x
451352ff8bdSFrançois Tigeot 	 * Spec says both have to be programmed, even if one is not getting
452352ff8bdSFrançois Tigeot 	 * used. Configure MIPI_CLOCK_CTL dividers in modeset
453352ff8bdSFrançois Tigeot 	 */
454*8621f407SFrançois Tigeot 	config->dsi_pll.ctrl = dsi_ratio | BXT_DSIA_16X_BY2 | BXT_DSIC_16X_BY2;
455352ff8bdSFrançois Tigeot 
456352ff8bdSFrançois Tigeot 	/* As per recommendation from hardware team,
457352ff8bdSFrançois Tigeot 	 * Prog PVD ratio =1 if dsi ratio <= 50
458352ff8bdSFrançois Tigeot 	 */
459*8621f407SFrançois Tigeot 	if (dsi_ratio <= 50)
460*8621f407SFrançois Tigeot 		config->dsi_pll.ctrl |= BXT_DSI_PLL_PVD_RATIO_1;
461*8621f407SFrançois Tigeot 
462*8621f407SFrançois Tigeot 	return 0;
463352ff8bdSFrançois Tigeot }
464352ff8bdSFrançois Tigeot 
465*8621f407SFrançois Tigeot static void bxt_enable_dsi_pll(struct intel_encoder *encoder,
466*8621f407SFrançois Tigeot 			       const struct intel_crtc_state *config)
467352ff8bdSFrançois Tigeot {
468352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
469352ff8bdSFrançois Tigeot 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
470352ff8bdSFrançois Tigeot 	enum port port;
471352ff8bdSFrançois Tigeot 	u32 val;
472352ff8bdSFrançois Tigeot 
473352ff8bdSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
474352ff8bdSFrançois Tigeot 
475352ff8bdSFrançois Tigeot 	/* Configure PLL vales */
476*8621f407SFrançois Tigeot 	I915_WRITE(BXT_DSI_PLL_CTL, config->dsi_pll.ctrl);
477*8621f407SFrançois Tigeot 	POSTING_READ(BXT_DSI_PLL_CTL);
478352ff8bdSFrançois Tigeot 
479352ff8bdSFrançois Tigeot 	/* Program TX, RX, Dphy clocks */
480352ff8bdSFrançois Tigeot 	for_each_dsi_port(port, intel_dsi->ports)
481*8621f407SFrançois Tigeot 		bxt_dsi_program_clocks(encoder->base.dev, port, config);
482352ff8bdSFrançois Tigeot 
483352ff8bdSFrançois Tigeot 	/* Enable DSI PLL */
484352ff8bdSFrançois Tigeot 	val = I915_READ(BXT_DSI_PLL_ENABLE);
485352ff8bdSFrançois Tigeot 	val |= BXT_DSI_PLL_DO_ENABLE;
486352ff8bdSFrançois Tigeot 	I915_WRITE(BXT_DSI_PLL_ENABLE, val);
487352ff8bdSFrançois Tigeot 
488352ff8bdSFrançois Tigeot 	/* Timeout and fail if PLL not locked */
489352ff8bdSFrançois Tigeot 	if (wait_for(I915_READ(BXT_DSI_PLL_ENABLE) & BXT_DSI_PLL_LOCKED, 1)) {
490352ff8bdSFrançois Tigeot 		DRM_ERROR("Timed out waiting for DSI PLL to lock\n");
491352ff8bdSFrançois Tigeot 		return;
492352ff8bdSFrançois Tigeot 	}
493352ff8bdSFrançois Tigeot 
494352ff8bdSFrançois Tigeot 	DRM_DEBUG_KMS("DSI PLL locked\n");
495352ff8bdSFrançois Tigeot }
496352ff8bdSFrançois Tigeot 
497*8621f407SFrançois Tigeot bool intel_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
498*8621f407SFrançois Tigeot {
499*8621f407SFrançois Tigeot 	if (IS_BROXTON(dev_priv))
500*8621f407SFrançois Tigeot 		return bxt_dsi_pll_is_enabled(dev_priv);
501*8621f407SFrançois Tigeot 
502*8621f407SFrançois Tigeot 	MISSING_CASE(INTEL_DEVID(dev_priv));
503*8621f407SFrançois Tigeot 
504*8621f407SFrançois Tigeot 	return false;
505*8621f407SFrançois Tigeot }
506*8621f407SFrançois Tigeot 
507*8621f407SFrançois Tigeot int intel_compute_dsi_pll(struct intel_encoder *encoder,
508*8621f407SFrançois Tigeot 			  struct intel_crtc_state *config)
509352ff8bdSFrançois Tigeot {
510352ff8bdSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
511352ff8bdSFrançois Tigeot 
512aee94f86SFrançois Tigeot 	if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
513*8621f407SFrançois Tigeot 		return vlv_compute_dsi_pll(encoder, config);
514352ff8bdSFrançois Tigeot 	else if (IS_BROXTON(dev))
515*8621f407SFrançois Tigeot 		return bxt_compute_dsi_pll(encoder, config);
516*8621f407SFrançois Tigeot 
517*8621f407SFrançois Tigeot 	return -ENODEV;
518*8621f407SFrançois Tigeot }
519*8621f407SFrançois Tigeot 
520*8621f407SFrançois Tigeot void intel_enable_dsi_pll(struct intel_encoder *encoder,
521*8621f407SFrançois Tigeot 			  const struct intel_crtc_state *config)
522*8621f407SFrançois Tigeot {
523*8621f407SFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
524*8621f407SFrançois Tigeot 
525*8621f407SFrançois Tigeot 	if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
526*8621f407SFrançois Tigeot 		vlv_enable_dsi_pll(encoder, config);
527*8621f407SFrançois Tigeot 	else if (IS_BROXTON(dev))
528*8621f407SFrançois Tigeot 		bxt_enable_dsi_pll(encoder, config);
529352ff8bdSFrançois Tigeot }
530352ff8bdSFrançois Tigeot 
531352ff8bdSFrançois Tigeot void intel_disable_dsi_pll(struct intel_encoder *encoder)
532352ff8bdSFrançois Tigeot {
533352ff8bdSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
534352ff8bdSFrançois Tigeot 
535aee94f86SFrançois Tigeot 	if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
536352ff8bdSFrançois Tigeot 		vlv_disable_dsi_pll(encoder);
537352ff8bdSFrançois Tigeot 	else if (IS_BROXTON(dev))
538352ff8bdSFrançois Tigeot 		bxt_disable_dsi_pll(encoder);
539352ff8bdSFrançois Tigeot }
540352ff8bdSFrançois Tigeot 
541352ff8bdSFrançois Tigeot static void bxt_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
542352ff8bdSFrançois Tigeot {
543352ff8bdSFrançois Tigeot 	u32 tmp;
544352ff8bdSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
545352ff8bdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
546352ff8bdSFrançois Tigeot 
547352ff8bdSFrançois Tigeot 	/* Clear old configurations */
548352ff8bdSFrançois Tigeot 	tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
549352ff8bdSFrançois Tigeot 	tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
550*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
551*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
552*8621f407SFrançois Tigeot 	tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
553352ff8bdSFrançois Tigeot 	I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
554352ff8bdSFrançois Tigeot 	I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
555352ff8bdSFrançois Tigeot }
556352ff8bdSFrançois Tigeot 
557352ff8bdSFrançois Tigeot void intel_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
558352ff8bdSFrançois Tigeot {
559352ff8bdSFrançois Tigeot 	struct drm_device *dev = encoder->base.dev;
560352ff8bdSFrançois Tigeot 
561352ff8bdSFrançois Tigeot 	if (IS_BROXTON(dev))
562352ff8bdSFrançois Tigeot 		bxt_dsi_reset_clocks(encoder, port);
563aee94f86SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
564352ff8bdSFrançois Tigeot 		vlv_dsi_reset_clocks(encoder, port);
565352ff8bdSFrançois Tigeot }
566