blob: 2ff2ee7f3b78cfdb6f75f76811151dbc6f1fa24f [file] [log] [blame]
ymohanmabe4fc042013-08-27 23:40:56 +03001/*
2 * Copyright © 2013 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * Shobhit Kumar <shobhit.kumar@intel.com>
25 * Yogesh Mohan Marimuthu <yogesh.mohan.marimuthu@intel.com>
26 */
27
28#include <linux/kernel.h>
29#include "intel_drv.h"
30#include "i915_drv.h"
31#include "intel_dsi.h"
32
Ville Syrjälä50dd63a2016-03-15 16:40:02 +020033static const u16 lfsr_converts[] = {
ymohanmabe4fc042013-08-27 23:40:56 +030034 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
35 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
Gaurav K Singh3c5c6d82015-07-01 15:58:51 +030036 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 */
ymohanmabe4fc042013-08-27 23:40:56 +030038};
39
Shobhit Kumar44d4c6e2013-12-10 12:14:56 +053040/* Get DSI clock from pixel clock */
Jani Nikula1e78aa02016-03-16 12:21:40 +020041static u32 dsi_clk_from_pclk(u32 pclk, enum mipi_dsi_pixel_format fmt,
42 int lane_count)
ymohanmabe4fc042013-08-27 23:40:56 +030043{
Shobhit Kumar44d4c6e2013-12-10 12:14:56 +053044 u32 dsi_clk_khz;
Jani Nikula1e78aa02016-03-16 12:21:40 +020045 u32 bpp = mipi_dsi_pixel_format_to_bpp(fmt);
ymohanmabe4fc042013-08-27 23:40:56 +030046
Shobhit Kumar44d4c6e2013-12-10 12:14:56 +053047 /* DSI data rate = pixel clock * bits per pixel / lane count
48 pixel clock is converted from KHz to Hz */
Shobhit Kumar7f0c8602014-07-30 20:34:57 +053049 dsi_clk_khz = DIV_ROUND_CLOSEST(pclk * bpp, lane_count);
ymohanmabe4fc042013-08-27 23:40:56 +030050
Shobhit Kumar44d4c6e2013-12-10 12:14:56 +053051 return dsi_clk_khz;
ymohanmabe4fc042013-08-27 23:40:56 +030052}
53
Gaurav K Singh20dbe1a2015-07-01 15:58:52 +030054static int dsi_calc_mnp(struct drm_i915_private *dev_priv,
Ville Syrjälä47eacba2016-04-12 22:14:35 +030055 struct intel_crtc_state *config,
56 int target_dsi_clk)
ymohanmabe4fc042013-08-27 23:40:56 +030057{
Gaurav K Singh20dbe1a2015-07-01 15:58:52 +030058 unsigned int m_min, m_max, p_min = 2, p_max = 6;
59 unsigned int m, n, p;
Chris Wilson1bbea162016-07-02 15:36:04 +010060 unsigned int calc_m, calc_p;
61 int delta, ref_clk;
ymohanmabe4fc042013-08-27 23:40:56 +030062
Jani Nikula7471bf42015-05-12 15:23:09 +030063 /* target_dsi_clk is expected in kHz */
64 if (target_dsi_clk < 300000 || target_dsi_clk > 1150000) {
ymohanmabe4fc042013-08-27 23:40:56 +030065 DRM_ERROR("DSI CLK Out of Range\n");
66 return -ECHRNG;
67 }
68
Gaurav K Singh20dbe1a2015-07-01 15:58:52 +030069 if (IS_CHERRYVIEW(dev_priv)) {
70 ref_clk = 100000;
71 n = 4;
72 m_min = 70;
73 m_max = 96;
74 } else {
75 ref_clk = 25000;
76 n = 1;
77 m_min = 62;
78 m_max = 92;
79 }
80
Chris Wilson1bbea162016-07-02 15:36:04 +010081 calc_p = p_min;
82 calc_m = m_min;
83 delta = abs(target_dsi_clk - (m_min * ref_clk) / (p_min * n));
84
Gaurav K Singh20dbe1a2015-07-01 15:58:52 +030085 for (m = m_min; m <= m_max && delta; m++) {
86 for (p = p_min; p <= p_max && delta; p++) {
Jani Nikula7471bf42015-05-12 15:23:09 +030087 /*
88 * Find the optimal m and p divisors with minimal delta
89 * +/- the required clock
90 */
Jani Nikulaa856c5b2015-05-13 10:35:25 +030091 int calc_dsi_clk = (m * ref_clk) / (p * n);
Jani Nikula7471bf42015-05-12 15:23:09 +030092 int d = abs(target_dsi_clk - calc_dsi_clk);
93 if (d < delta) {
94 delta = d;
Shobhit Kumar8e1eed52013-12-10 12:14:57 +053095 calc_m = m;
96 calc_p = p;
ymohanmabe4fc042013-08-27 23:40:56 +030097 }
98 }
99 }
100
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300101 /* register has log2(N1), this works fine for powers of two */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300102 config->dsi_pll.ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
Chris Wilson1bbea162016-07-02 15:36:04 +0100103 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;
ymohanmabe4fc042013-08-27 23:40:56 +0300106
107 return 0;
108}
109
ymohanmabe4fc042013-08-27 23:40:56 +0300110/*
111 * XXX: The muxing and gating is hard coded for now. Need to add support for
112 * sharing PLLs with two DSI outputs.
113 */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300114static int vlv_compute_dsi_pll(struct intel_encoder *encoder,
115 struct intel_crtc_state *config)
ymohanmabe4fc042013-08-27 23:40:56 +0300116{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100117 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
ymohanmabe4fc042013-08-27 23:40:56 +0300118 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
119 int ret;
ymohanmabe4fc042013-08-27 23:40:56 +0300120 u32 dsi_clk;
121
Shobhit Kumar7f0c8602014-07-30 20:34:57 +0530122 dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200123 intel_dsi->lane_count);
ymohanmabe4fc042013-08-27 23:40:56 +0300124
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300125 ret = dsi_calc_mnp(dev_priv, config, dsi_clk);
ymohanmabe4fc042013-08-27 23:40:56 +0300126 if (ret) {
127 DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300128 return ret;
ymohanmabe4fc042013-08-27 23:40:56 +0300129 }
130
Gaurav K Singh3c860ab2014-12-09 10:57:00 +0530131 if (intel_dsi->ports & (1 << PORT_A))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300132 config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
ymohanmabe4fc042013-08-27 23:40:56 +0300133
Gaurav K Singh3c860ab2014-12-09 10:57:00 +0530134 if (intel_dsi->ports & (1 << PORT_C))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300135 config->dsi_pll.ctrl |= DSI_PLL_CLK_GATE_DSI1_DSIPLL;
136
137 config->dsi_pll.ctrl |= DSI_PLL_VCO_EN;
Gaurav K Singh58cf8882014-12-04 10:58:52 +0530138
ymohanmabe4fc042013-08-27 23:40:56 +0300139 DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300140 config->dsi_pll.div, config->dsi_pll.ctrl);
ymohanmabe4fc042013-08-27 23:40:56 +0300141
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300142 return 0;
ymohanmabe4fc042013-08-27 23:40:56 +0300143}
144
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300145static void vlv_enable_dsi_pll(struct intel_encoder *encoder,
146 const struct intel_crtc_state *config)
147{
148 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
ymohanmabe4fc042013-08-27 23:40:56 +0300149
150 DRM_DEBUG_KMS("\n");
151
Ville Syrjäläa5805162015-05-26 20:42:30 +0300152 mutex_lock(&dev_priv->sb_lock);
ymohanmabe4fc042013-08-27 23:40:56 +0300153
Ville Syrjälä062efa52016-04-12 22:14:36 +0300154 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);
ymohanmabe4fc042013-08-27 23:40:56 +0300158
Nicholas Mc Guire15a43cb2016-12-16 02:59:38 +0100159 /* wait at least 0.5 us after ungating before enabling VCO,
160 * allow hrtimer subsystem optimization by relaxing timing
161 */
162 usleep_range(10, 50);
ymohanmabe4fc042013-08-27 23:40:56 +0300163
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300164 vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, config->dsi_pll.ctrl);
ymohanmabe4fc042013-08-27 23:40:56 +0300165
Gaurav K Singh3770f0e2014-12-05 14:16:58 +0530166 if (wait_for(vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL) &
167 DSI_PLL_LOCK, 20)) {
ymohanmabe4fc042013-08-27 23:40:56 +0300168
Ville Syrjäläa5805162015-05-26 20:42:30 +0300169 mutex_unlock(&dev_priv->sb_lock);
ymohanmabe4fc042013-08-27 23:40:56 +0300170 DRM_ERROR("DSI PLL lock failed\n");
171 return;
172 }
Ville Syrjäläa5805162015-05-26 20:42:30 +0300173 mutex_unlock(&dev_priv->sb_lock);
ymohanmabe4fc042013-08-27 23:40:56 +0300174
175 DRM_DEBUG_KMS("DSI PLL locked\n");
176}
177
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530178static void vlv_disable_dsi_pll(struct intel_encoder *encoder)
ymohanmabe4fc042013-08-27 23:40:56 +0300179{
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300180 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
ymohanmabe4fc042013-08-27 23:40:56 +0300181 u32 tmp;
182
183 DRM_DEBUG_KMS("\n");
184
Ville Syrjäläa5805162015-05-26 20:42:30 +0300185 mutex_lock(&dev_priv->sb_lock);
ymohanmabe4fc042013-08-27 23:40:56 +0300186
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);
191
Ville Syrjäläa5805162015-05-26 20:42:30 +0300192 mutex_unlock(&dev_priv->sb_lock);
ymohanmabe4fc042013-08-27 23:40:56 +0300193}
Shobhit Kumarf573de52014-07-30 20:32:37 +0530194
Imre Deakdb18b6a2016-03-24 12:41:40 +0200195static bool bxt_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
196{
197 bool enabled;
198 u32 val;
199 u32 mask;
200
201 mask = BXT_DSI_PLL_DO_ENABLE | BXT_DSI_PLL_LOCKED;
202 val = I915_READ(BXT_DSI_PLL_ENABLE);
203 enabled = (val & mask) == mask;
204
205 if (!enabled)
206 return false;
207
208 /*
Madhav Chauhanebeac382017-02-17 18:13:35 +0530209 * 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
Imre Deakdb18b6a2016-03-24 12:41:40 +0200212 * 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.
215 */
216 val = I915_READ(BXT_DSI_PLL_CTL);
Madhav Chauhanebeac382017-02-17 18:13:35 +0530217 if (IS_GEMINILAKE(dev_priv)) {
218 if (!(val & BXT_DSIA_16X_MASK)) {
219 DRM_DEBUG_DRIVER("Invalid PLL divider (%08x)\n", val);
220 enabled = false;
221 }
222 } else {
223 if (!(val & BXT_DSIA_16X_MASK) || !(val & BXT_DSIC_16X_MASK)) {
224 DRM_DEBUG_DRIVER("Invalid PLL divider (%08x)\n", val);
225 enabled = false;
226 }
Imre Deakdb18b6a2016-03-24 12:41:40 +0200227 }
228
229 return enabled;
230}
231
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530232static void bxt_disable_dsi_pll(struct intel_encoder *encoder)
233{
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300234 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530235 u32 val;
236
237 DRM_DEBUG_KMS("\n");
238
239 val = I915_READ(BXT_DSI_PLL_ENABLE);
240 val &= ~BXT_DSI_PLL_DO_ENABLE;
241 I915_WRITE(BXT_DSI_PLL_ENABLE, val);
242
243 /*
244 * PLL lock should deassert within 200us.
245 * Wait up to 1ms before timing out.
246 */
Chris Wilson90a392c2016-06-30 15:33:19 +0100247 if (intel_wait_for_register(dev_priv,
248 BXT_DSI_PLL_ENABLE,
249 BXT_DSI_PLL_LOCKED,
250 0,
251 1))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530252 DRM_ERROR("Timeout waiting for PLL lock deassertion\n");
253}
254
Jani Nikula1e78aa02016-03-16 12:21:40 +0200255static void assert_bpp_mismatch(enum mipi_dsi_pixel_format fmt, int pipe_bpp)
Shobhit Kumarf573de52014-07-30 20:32:37 +0530256{
Jani Nikula1e78aa02016-03-16 12:21:40 +0200257 int bpp = mipi_dsi_pixel_format_to_bpp(fmt);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530258
259 WARN(bpp != pipe_bpp,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200260 "bpp match assertion failure (expected %d, current %d)\n",
261 bpp, pipe_bpp);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530262}
263
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300264static u32 vlv_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
265 struct intel_crtc_state *config)
Shobhit Kumarf573de52014-07-30 20:32:37 +0530266{
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300267 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530268 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
269 u32 dsi_clock, pclk;
270 u32 pll_ctl, pll_div;
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300271 u32 m = 0, p = 0, n;
Ville Syrjäläae9ec622016-03-15 16:40:05 +0200272 int refclk = IS_CHERRYVIEW(dev_priv) ? 100000 : 25000;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530273 int i;
274
275 DRM_DEBUG_KMS("\n");
276
Ville Syrjäläa5805162015-05-26 20:42:30 +0300277 mutex_lock(&dev_priv->sb_lock);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530278 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);
Ville Syrjäläa5805162015-05-26 20:42:30 +0300280 mutex_unlock(&dev_priv->sb_lock);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530281
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300282 config->dsi_pll.ctrl = pll_ctl & ~DSI_PLL_LOCK;
283 config->dsi_pll.div = pll_div;
284
Shobhit Kumarf573de52014-07-30 20:32:37 +0530285 /* 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);
288
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300289 /* N1 divisor */
290 n = (pll_div & DSI_PLL_N1_DIV_MASK) >> DSI_PLL_N1_DIV_SHIFT;
291 n = 1 << n; /* register has log2(N1) */
292
Shobhit Kumarf573de52014-07-30 20:32:37 +0530293 /* 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;
296
297 while (pll_ctl) {
298 pll_ctl = pll_ctl >> 1;
299 p++;
300 }
301 p--;
302
303 if (!p) {
304 DRM_ERROR("wrong P1 divisor\n");
305 return 0;
306 }
307
308 for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
309 if (lfsr_converts[i] == pll_div)
310 break;
311 }
312
313 if (i == ARRAY_SIZE(lfsr_converts)) {
314 DRM_ERROR("wrong m_seed programmed\n");
315 return 0;
316 }
317
318 m = i + 62;
319
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300320 dsi_clock = (m * refclk) / (p * n);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530321
322 /* pixel_format and pipe_bpp should agree */
323 assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
324
325 pclk = DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, pipe_bpp);
326
327 return pclk;
328}
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530329
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300330static u32 bxt_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
331 struct intel_crtc_state *config)
Shashank Sharmace0c9822015-09-01 19:41:46 +0530332{
333 u32 pclk;
334 u32 dsi_clk;
335 u32 dsi_ratio;
336 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Chris Wilsonfac5e232016-07-04 11:34:36 +0100337 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmace0c9822015-09-01 19:41:46 +0530338
339 /* Divide by zero */
340 if (!pipe_bpp) {
341 DRM_ERROR("Invalid BPP(0)\n");
342 return 0;
343 }
344
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300345 config->dsi_pll.ctrl = I915_READ(BXT_DSI_PLL_CTL);
Shashank Sharmace0c9822015-09-01 19:41:46 +0530346
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300347 dsi_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
Shashank Sharmace0c9822015-09-01 19:41:46 +0530348
349 dsi_clk = (dsi_ratio * BXT_REF_CLOCK_KHZ) / 2;
350
351 /* pixel_format and pipe_bpp should agree */
352 assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
353
354 pclk = DIV_ROUND_CLOSEST(dsi_clk * intel_dsi->lane_count, pipe_bpp);
355
356 DRM_DEBUG_DRIVER("Calculated pclk=%u\n", pclk);
357 return pclk;
358}
359
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300360u32 intel_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
361 struct intel_crtc_state *config)
Jani Nikulad7d85d82016-01-08 12:45:39 +0200362{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200363 if (IS_GEN9_LP(to_i915(encoder->base.dev)))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300364 return bxt_dsi_get_pclk(encoder, pipe_bpp, config);
Jani Nikulad7d85d82016-01-08 12:45:39 +0200365 else
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300366 return vlv_dsi_get_pclk(encoder, pipe_bpp, config);
Jani Nikulad7d85d82016-01-08 12:45:39 +0200367}
368
kbuild test robotb248e652015-10-05 16:21:11 +0800369static void vlv_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
Shashank Sharmab389a452015-09-01 19:41:44 +0530370{
371 u32 temp;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100372 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530373 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
374
375 temp = I915_READ(MIPI_CTRL(port));
376 temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
377 I915_WRITE(MIPI_CTRL(port), temp |
378 intel_dsi->escape_clk_div <<
379 ESCAPE_CLOCK_DIVIDER_SHIFT);
380}
381
Deepak Mbcc65702017-02-17 18:13:34 +0530382static void glk_dsi_program_esc_clock(struct drm_device *dev,
383 const struct intel_crtc_state *config)
384{
385 struct drm_i915_private *dev_priv = to_i915(dev);
386 u32 dsi_rate = 0;
387 u32 pll_ratio = 0;
388 u32 ddr_clk = 0;
389 u32 div1_value = 0;
390 u32 div2_value = 0;
391 u32 txesc1_div = 0;
392 u32 txesc2_div = 0;
393
394 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
395
396 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
397
398 ddr_clk = dsi_rate / 2;
399
400 /* Variable divider value */
401 div1_value = DIV_ROUND_CLOSEST(ddr_clk, 20000);
402
403 /* Calculate TXESC1 divider */
404 if (div1_value <= 10)
405 txesc1_div = div1_value;
406 else if ((div1_value > 10) && (div1_value <= 20))
407 txesc1_div = DIV_ROUND_UP(div1_value, 2);
408 else if ((div1_value > 20) && (div1_value <= 30))
409 txesc1_div = DIV_ROUND_UP(div1_value, 4);
410 else if ((div1_value > 30) && (div1_value <= 40))
411 txesc1_div = DIV_ROUND_UP(div1_value, 6);
412 else if ((div1_value > 40) && (div1_value <= 50))
413 txesc1_div = DIV_ROUND_UP(div1_value, 8);
414 else
415 txesc1_div = 10;
416
417 /* Calculate TXESC2 divider */
418 div2_value = DIV_ROUND_UP(div1_value, txesc1_div);
419
420 if (div2_value < 10)
421 txesc2_div = div2_value;
422 else
423 txesc2_div = 10;
424
425 I915_WRITE(MIPIO_TXESC_CLK_DIV1, txesc1_div & GLK_TX_ESC_CLK_DIV1_MASK);
426 I915_WRITE(MIPIO_TXESC_CLK_DIV2, txesc2_div & GLK_TX_ESC_CLK_DIV2_MASK);
427}
428
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530429/* Program BXT Mipi clocks and dividers */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300430static void bxt_dsi_program_clocks(struct drm_device *dev, enum port port,
431 const struct intel_crtc_state *config)
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530432{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100433 struct drm_i915_private *dev_priv = to_i915(dev);
Deepak M782d25c2016-02-15 22:43:57 +0530434 u32 tmp;
435 u32 dsi_rate = 0;
436 u32 pll_ratio = 0;
437 u32 rx_div;
438 u32 tx_div;
439 u32 rx_div_upper;
440 u32 rx_div_lower;
441 u32 mipi_8by3_divider;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530442
443 /* Clear old configurations */
444 tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
445 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
Deepak M782d25c2016-02-15 22:43:57 +0530446 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
447 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
448 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530449
450 /* Get the current DSI rate(actual) */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300451 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530452 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
453
Deepak M782d25c2016-02-15 22:43:57 +0530454 /*
455 * tx clock should be <= 20MHz and the div value must be
456 * subtracted by 1 as per bspec
457 */
458 tx_div = DIV_ROUND_UP(dsi_rate, 20000) - 1;
459 /*
460 * rx clock should be <= 150MHz and the div value must be
461 * subtracted by 1 as per bspec
462 */
463 rx_div = DIV_ROUND_UP(dsi_rate, 150000) - 1;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530464
465 /*
Deepak M782d25c2016-02-15 22:43:57 +0530466 * rx divider value needs to be updated in the
467 * two differnt bit fields in the register hence splitting the
468 * rx divider value accordingly
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530469 */
Deepak M782d25c2016-02-15 22:43:57 +0530470 rx_div_lower = rx_div & RX_DIVIDER_BIT_1_2;
471 rx_div_upper = (rx_div & RX_DIVIDER_BIT_3_4) >> 2;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530472
Uma Shankar06a20d22017-02-08 16:20:51 +0530473 mipi_8by3_divider = 0x2;
Deepak M782d25c2016-02-15 22:43:57 +0530474
475 tmp |= BXT_MIPI_8X_BY3_DIVIDER(port, mipi_8by3_divider);
476 tmp |= BXT_MIPI_TX_ESCLK_DIVIDER(port, tx_div);
477 tmp |= BXT_MIPI_RX_ESCLK_LOWER_DIVIDER(port, rx_div_lower);
478 tmp |= BXT_MIPI_RX_ESCLK_UPPER_DIVIDER(port, rx_div_upper);
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530479
480 I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
481}
482
Deepak Mf340c2f2017-02-17 18:13:32 +0530483static int gen9lp_compute_dsi_pll(struct intel_encoder *encoder,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300484 struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530485{
Deepak Mf340c2f2017-02-17 18:13:32 +0530486 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530487 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Deepak Mf340c2f2017-02-17 18:13:32 +0530488 u8 dsi_ratio, dsi_ratio_min, dsi_ratio_max;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530489 u32 dsi_clk;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530490
491 dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300492 intel_dsi->lane_count);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530493
494 /*
495 * From clock diagram, to get PLL ratio divider, divide double of DSI
496 * link rate (i.e., 2*8x=16x frequency value) by ref clock. Make sure to
497 * round 'up' the result
498 */
499 dsi_ratio = DIV_ROUND_UP(dsi_clk * 2, BXT_REF_CLOCK_KHZ);
Deepak Mf340c2f2017-02-17 18:13:32 +0530500
501 if (IS_BROXTON(dev_priv)) {
502 dsi_ratio_min = BXT_DSI_PLL_RATIO_MIN;
503 dsi_ratio_max = BXT_DSI_PLL_RATIO_MAX;
504 } else {
505 dsi_ratio_min = GLK_DSI_PLL_RATIO_MIN;
506 dsi_ratio_max = GLK_DSI_PLL_RATIO_MAX;
507 }
508
509 if (dsi_ratio < dsi_ratio_min || dsi_ratio > dsi_ratio_max) {
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530510 DRM_ERROR("Cant get a suitable ratio from DSI PLL ratios\n");
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300511 return -ECHRNG;
Deepak Mf340c2f2017-02-17 18:13:32 +0530512 } else
513 DRM_DEBUG_KMS("DSI PLL calculation is Done!!\n");
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530514
515 /*
516 * Program DSI ratio and Select MIPIC and MIPIA PLL output as 8x
517 * Spec says both have to be programmed, even if one is not getting
518 * used. Configure MIPI_CLOCK_CTL dividers in modeset
519 */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300520 config->dsi_pll.ctrl = dsi_ratio | BXT_DSIA_16X_BY2 | BXT_DSIC_16X_BY2;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530521
522 /* As per recommendation from hardware team,
523 * Prog PVD ratio =1 if dsi ratio <= 50
524 */
Deepak Mf340c2f2017-02-17 18:13:32 +0530525 if (IS_BROXTON(dev_priv) && dsi_ratio <= 50)
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300526 config->dsi_pll.ctrl |= BXT_DSI_PLL_PVD_RATIO_1;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530527
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300528 return 0;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530529}
530
Deepak Mbcc65702017-02-17 18:13:34 +0530531static void gen9lp_enable_dsi_pll(struct intel_encoder *encoder,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300532 const struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530533{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100534 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530535 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
536 enum port port;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530537 u32 val;
538
539 DRM_DEBUG_KMS("\n");
540
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530541 /* Configure PLL vales */
Ville Syrjälä062efa52016-04-12 22:14:36 +0300542 I915_WRITE(BXT_DSI_PLL_CTL, config->dsi_pll.ctrl);
543 POSTING_READ(BXT_DSI_PLL_CTL);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530544
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530545 /* Program TX, RX, Dphy clocks */
Deepak M09a568e2017-02-17 18:13:33 +0530546 if (IS_BROXTON(dev_priv)) {
547 for_each_dsi_port(port, intel_dsi->ports)
548 bxt_dsi_program_clocks(encoder->base.dev, port, config);
Deepak Mbcc65702017-02-17 18:13:34 +0530549 } else {
550 glk_dsi_program_esc_clock(encoder->base.dev, config);
Deepak M09a568e2017-02-17 18:13:33 +0530551 }
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530552
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530553 /* Enable DSI PLL */
554 val = I915_READ(BXT_DSI_PLL_ENABLE);
555 val |= BXT_DSI_PLL_DO_ENABLE;
556 I915_WRITE(BXT_DSI_PLL_ENABLE, val);
557
558 /* Timeout and fail if PLL not locked */
Chris Wilson186f1c52016-06-30 15:33:20 +0100559 if (intel_wait_for_register(dev_priv,
560 BXT_DSI_PLL_ENABLE,
561 BXT_DSI_PLL_LOCKED,
562 BXT_DSI_PLL_LOCKED,
563 1)) {
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530564 DRM_ERROR("Timed out waiting for DSI PLL to lock\n");
565 return;
566 }
567
568 DRM_DEBUG_KMS("DSI PLL locked\n");
569}
570
Imre Deakdb18b6a2016-03-24 12:41:40 +0200571bool intel_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
572{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200573 if (IS_GEN9_LP(dev_priv))
Imre Deakdb18b6a2016-03-24 12:41:40 +0200574 return bxt_dsi_pll_is_enabled(dev_priv);
575
576 MISSING_CASE(INTEL_DEVID(dev_priv));
577
578 return false;
579}
580
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300581int intel_compute_dsi_pll(struct intel_encoder *encoder,
582 struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530583{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100584 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530585
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100586 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300587 return vlv_compute_dsi_pll(encoder, config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200588 else if (IS_GEN9_LP(dev_priv))
Deepak Mf340c2f2017-02-17 18:13:32 +0530589 return gen9lp_compute_dsi_pll(encoder, config);
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300590
591 return -ENODEV;
592}
593
594void intel_enable_dsi_pll(struct intel_encoder *encoder,
595 const struct intel_crtc_state *config)
596{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100597 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300598
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100599 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300600 vlv_enable_dsi_pll(encoder, config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200601 else if (IS_GEN9_LP(dev_priv))
Deepak Mbcc65702017-02-17 18:13:34 +0530602 gen9lp_enable_dsi_pll(encoder, config);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530603}
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530604
605void intel_disable_dsi_pll(struct intel_encoder *encoder)
606{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100607 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530608
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100609 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530610 vlv_disable_dsi_pll(encoder);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200611 else if (IS_GEN9_LP(dev_priv))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530612 bxt_disable_dsi_pll(encoder);
613}
Shashank Sharmab389a452015-09-01 19:41:44 +0530614
Deepak M09a568e2017-02-17 18:13:33 +0530615static void gen9lp_dsi_reset_clocks(struct intel_encoder *encoder,
616 enum port port)
Shashank Sharmab389a452015-09-01 19:41:44 +0530617{
618 u32 tmp;
619 struct drm_device *dev = encoder->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100620 struct drm_i915_private *dev_priv = to_i915(dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530621
622 /* Clear old configurations */
Deepak M09a568e2017-02-17 18:13:33 +0530623 if (IS_BROXTON(dev_priv)) {
624 tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
625 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
626 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
627 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
628 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
629 I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
Deepak Mbcc65702017-02-17 18:13:34 +0530630 } else {
631 tmp = I915_READ(MIPIO_TXESC_CLK_DIV1);
632 tmp &= ~GLK_TX_ESC_CLK_DIV1_MASK;
633 I915_WRITE(MIPIO_TXESC_CLK_DIV1, tmp);
634
635 tmp = I915_READ(MIPIO_TXESC_CLK_DIV2);
636 tmp &= ~GLK_TX_ESC_CLK_DIV2_MASK;
637 I915_WRITE(MIPIO_TXESC_CLK_DIV2, tmp);
Deepak M09a568e2017-02-17 18:13:33 +0530638 }
Shashank Sharmab389a452015-09-01 19:41:44 +0530639 I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
640}
641
642void intel_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
643{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100644 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530645
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200646 if (IS_GEN9_LP(dev_priv))
Deepak M09a568e2017-02-17 18:13:33 +0530647 gen9lp_dsi_reset_clocks(encoder, port);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100648 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Shashank Sharmab389a452015-09-01 19:41:44 +0530649 vlv_dsi_reset_clocks(encoder, port);
650}