blob: 7a7617b24cb280cbc6d7c5909a0dae79109f5f84 [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 /*
209 * Both dividers must be programmed with valid values even if only one
210 * of the PLL is used, see BSpec/Broxton Clocks. Check this here for
211 * paranoia, since BIOS is known to misconfigure PLLs in this way at
212 * times, and since accessing DSI registers with invalid dividers
213 * causes a system hang.
214 */
215 val = I915_READ(BXT_DSI_PLL_CTL);
216 if (!(val & BXT_DSIA_16X_MASK) || !(val & BXT_DSIC_16X_MASK)) {
217 DRM_DEBUG_DRIVER("PLL is enabled with invalid divider settings (%08x)\n",
218 val);
219 enabled = false;
220 }
221
222 return enabled;
223}
224
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530225static void bxt_disable_dsi_pll(struct intel_encoder *encoder)
226{
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300227 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530228 u32 val;
229
230 DRM_DEBUG_KMS("\n");
231
232 val = I915_READ(BXT_DSI_PLL_ENABLE);
233 val &= ~BXT_DSI_PLL_DO_ENABLE;
234 I915_WRITE(BXT_DSI_PLL_ENABLE, val);
235
236 /*
237 * PLL lock should deassert within 200us.
238 * Wait up to 1ms before timing out.
239 */
Chris Wilson90a392c2016-06-30 15:33:19 +0100240 if (intel_wait_for_register(dev_priv,
241 BXT_DSI_PLL_ENABLE,
242 BXT_DSI_PLL_LOCKED,
243 0,
244 1))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530245 DRM_ERROR("Timeout waiting for PLL lock deassertion\n");
246}
247
Jani Nikula1e78aa02016-03-16 12:21:40 +0200248static void assert_bpp_mismatch(enum mipi_dsi_pixel_format fmt, int pipe_bpp)
Shobhit Kumarf573de52014-07-30 20:32:37 +0530249{
Jani Nikula1e78aa02016-03-16 12:21:40 +0200250 int bpp = mipi_dsi_pixel_format_to_bpp(fmt);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530251
252 WARN(bpp != pipe_bpp,
Daniel Vetter7f3de832014-07-30 22:34:27 +0200253 "bpp match assertion failure (expected %d, current %d)\n",
254 bpp, pipe_bpp);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530255}
256
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300257static u32 vlv_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
258 struct intel_crtc_state *config)
Shobhit Kumarf573de52014-07-30 20:32:37 +0530259{
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300260 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530261 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
262 u32 dsi_clock, pclk;
263 u32 pll_ctl, pll_div;
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300264 u32 m = 0, p = 0, n;
Ville Syrjäläae9ec622016-03-15 16:40:05 +0200265 int refclk = IS_CHERRYVIEW(dev_priv) ? 100000 : 25000;
Shobhit Kumarf573de52014-07-30 20:32:37 +0530266 int i;
267
268 DRM_DEBUG_KMS("\n");
269
Ville Syrjäläa5805162015-05-26 20:42:30 +0300270 mutex_lock(&dev_priv->sb_lock);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530271 pll_ctl = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
272 pll_div = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_DIVIDER);
Ville Syrjäläa5805162015-05-26 20:42:30 +0300273 mutex_unlock(&dev_priv->sb_lock);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530274
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300275 config->dsi_pll.ctrl = pll_ctl & ~DSI_PLL_LOCK;
276 config->dsi_pll.div = pll_div;
277
Shobhit Kumarf573de52014-07-30 20:32:37 +0530278 /* mask out other bits and extract the P1 divisor */
279 pll_ctl &= DSI_PLL_P1_POST_DIV_MASK;
280 pll_ctl = pll_ctl >> (DSI_PLL_P1_POST_DIV_SHIFT - 2);
281
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300282 /* N1 divisor */
283 n = (pll_div & DSI_PLL_N1_DIV_MASK) >> DSI_PLL_N1_DIV_SHIFT;
284 n = 1 << n; /* register has log2(N1) */
285
Shobhit Kumarf573de52014-07-30 20:32:37 +0530286 /* mask out the other bits and extract the M1 divisor */
287 pll_div &= DSI_PLL_M1_DIV_MASK;
288 pll_div = pll_div >> DSI_PLL_M1_DIV_SHIFT;
289
290 while (pll_ctl) {
291 pll_ctl = pll_ctl >> 1;
292 p++;
293 }
294 p--;
295
296 if (!p) {
297 DRM_ERROR("wrong P1 divisor\n");
298 return 0;
299 }
300
301 for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
302 if (lfsr_converts[i] == pll_div)
303 break;
304 }
305
306 if (i == ARRAY_SIZE(lfsr_converts)) {
307 DRM_ERROR("wrong m_seed programmed\n");
308 return 0;
309 }
310
311 m = i + 62;
312
Jani Nikulaa856c5b2015-05-13 10:35:25 +0300313 dsi_clock = (m * refclk) / (p * n);
Shobhit Kumarf573de52014-07-30 20:32:37 +0530314
315 /* pixel_format and pipe_bpp should agree */
316 assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
317
318 pclk = DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, pipe_bpp);
319
320 return pclk;
321}
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530322
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300323static u32 bxt_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
324 struct intel_crtc_state *config)
Shashank Sharmace0c9822015-09-01 19:41:46 +0530325{
326 u32 pclk;
327 u32 dsi_clk;
328 u32 dsi_ratio;
329 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Chris Wilsonfac5e232016-07-04 11:34:36 +0100330 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmace0c9822015-09-01 19:41:46 +0530331
332 /* Divide by zero */
333 if (!pipe_bpp) {
334 DRM_ERROR("Invalid BPP(0)\n");
335 return 0;
336 }
337
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300338 config->dsi_pll.ctrl = I915_READ(BXT_DSI_PLL_CTL);
Shashank Sharmace0c9822015-09-01 19:41:46 +0530339
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300340 dsi_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
Shashank Sharmace0c9822015-09-01 19:41:46 +0530341
342 dsi_clk = (dsi_ratio * BXT_REF_CLOCK_KHZ) / 2;
343
344 /* pixel_format and pipe_bpp should agree */
345 assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
346
347 pclk = DIV_ROUND_CLOSEST(dsi_clk * intel_dsi->lane_count, pipe_bpp);
348
349 DRM_DEBUG_DRIVER("Calculated pclk=%u\n", pclk);
350 return pclk;
351}
352
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300353u32 intel_dsi_get_pclk(struct intel_encoder *encoder, int pipe_bpp,
354 struct intel_crtc_state *config)
Jani Nikulad7d85d82016-01-08 12:45:39 +0200355{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200356 if (IS_GEN9_LP(to_i915(encoder->base.dev)))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300357 return bxt_dsi_get_pclk(encoder, pipe_bpp, config);
Jani Nikulad7d85d82016-01-08 12:45:39 +0200358 else
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300359 return vlv_dsi_get_pclk(encoder, pipe_bpp, config);
Jani Nikulad7d85d82016-01-08 12:45:39 +0200360}
361
kbuild test robotb248e652015-10-05 16:21:11 +0800362static void vlv_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
Shashank Sharmab389a452015-09-01 19:41:44 +0530363{
364 u32 temp;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100365 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530366 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
367
368 temp = I915_READ(MIPI_CTRL(port));
369 temp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
370 I915_WRITE(MIPI_CTRL(port), temp |
371 intel_dsi->escape_clk_div <<
372 ESCAPE_CLOCK_DIVIDER_SHIFT);
373}
374
Deepak Mbcc65702017-02-17 18:13:34 +0530375static void glk_dsi_program_esc_clock(struct drm_device *dev,
376 const struct intel_crtc_state *config)
377{
378 struct drm_i915_private *dev_priv = to_i915(dev);
379 u32 dsi_rate = 0;
380 u32 pll_ratio = 0;
381 u32 ddr_clk = 0;
382 u32 div1_value = 0;
383 u32 div2_value = 0;
384 u32 txesc1_div = 0;
385 u32 txesc2_div = 0;
386
387 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
388
389 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
390
391 ddr_clk = dsi_rate / 2;
392
393 /* Variable divider value */
394 div1_value = DIV_ROUND_CLOSEST(ddr_clk, 20000);
395
396 /* Calculate TXESC1 divider */
397 if (div1_value <= 10)
398 txesc1_div = div1_value;
399 else if ((div1_value > 10) && (div1_value <= 20))
400 txesc1_div = DIV_ROUND_UP(div1_value, 2);
401 else if ((div1_value > 20) && (div1_value <= 30))
402 txesc1_div = DIV_ROUND_UP(div1_value, 4);
403 else if ((div1_value > 30) && (div1_value <= 40))
404 txesc1_div = DIV_ROUND_UP(div1_value, 6);
405 else if ((div1_value > 40) && (div1_value <= 50))
406 txesc1_div = DIV_ROUND_UP(div1_value, 8);
407 else
408 txesc1_div = 10;
409
410 /* Calculate TXESC2 divider */
411 div2_value = DIV_ROUND_UP(div1_value, txesc1_div);
412
413 if (div2_value < 10)
414 txesc2_div = div2_value;
415 else
416 txesc2_div = 10;
417
418 I915_WRITE(MIPIO_TXESC_CLK_DIV1, txesc1_div & GLK_TX_ESC_CLK_DIV1_MASK);
419 I915_WRITE(MIPIO_TXESC_CLK_DIV2, txesc2_div & GLK_TX_ESC_CLK_DIV2_MASK);
420}
421
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530422/* Program BXT Mipi clocks and dividers */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300423static void bxt_dsi_program_clocks(struct drm_device *dev, enum port port,
424 const struct intel_crtc_state *config)
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530425{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100426 struct drm_i915_private *dev_priv = to_i915(dev);
Deepak M782d25c2016-02-15 22:43:57 +0530427 u32 tmp;
428 u32 dsi_rate = 0;
429 u32 pll_ratio = 0;
430 u32 rx_div;
431 u32 tx_div;
432 u32 rx_div_upper;
433 u32 rx_div_lower;
434 u32 mipi_8by3_divider;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530435
436 /* Clear old configurations */
437 tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
438 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
Deepak M782d25c2016-02-15 22:43:57 +0530439 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
440 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
441 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530442
443 /* Get the current DSI rate(actual) */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300444 pll_ratio = config->dsi_pll.ctrl & BXT_DSI_PLL_RATIO_MASK;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530445 dsi_rate = (BXT_REF_CLOCK_KHZ * pll_ratio) / 2;
446
Deepak M782d25c2016-02-15 22:43:57 +0530447 /*
448 * tx clock should be <= 20MHz and the div value must be
449 * subtracted by 1 as per bspec
450 */
451 tx_div = DIV_ROUND_UP(dsi_rate, 20000) - 1;
452 /*
453 * rx clock should be <= 150MHz and the div value must be
454 * subtracted by 1 as per bspec
455 */
456 rx_div = DIV_ROUND_UP(dsi_rate, 150000) - 1;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530457
458 /*
Deepak M782d25c2016-02-15 22:43:57 +0530459 * rx divider value needs to be updated in the
460 * two differnt bit fields in the register hence splitting the
461 * rx divider value accordingly
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530462 */
Deepak M782d25c2016-02-15 22:43:57 +0530463 rx_div_lower = rx_div & RX_DIVIDER_BIT_1_2;
464 rx_div_upper = (rx_div & RX_DIVIDER_BIT_3_4) >> 2;
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530465
Uma Shankar06a20d22017-02-08 16:20:51 +0530466 mipi_8by3_divider = 0x2;
Deepak M782d25c2016-02-15 22:43:57 +0530467
468 tmp |= BXT_MIPI_8X_BY3_DIVIDER(port, mipi_8by3_divider);
469 tmp |= BXT_MIPI_TX_ESCLK_DIVIDER(port, tx_div);
470 tmp |= BXT_MIPI_RX_ESCLK_LOWER_DIVIDER(port, rx_div_lower);
471 tmp |= BXT_MIPI_RX_ESCLK_UPPER_DIVIDER(port, rx_div_upper);
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530472
473 I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
474}
475
Deepak Mf340c2f2017-02-17 18:13:32 +0530476static int gen9lp_compute_dsi_pll(struct intel_encoder *encoder,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300477 struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530478{
Deepak Mf340c2f2017-02-17 18:13:32 +0530479 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530480 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
Deepak Mf340c2f2017-02-17 18:13:32 +0530481 u8 dsi_ratio, dsi_ratio_min, dsi_ratio_max;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530482 u32 dsi_clk;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530483
484 dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300485 intel_dsi->lane_count);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530486
487 /*
488 * From clock diagram, to get PLL ratio divider, divide double of DSI
489 * link rate (i.e., 2*8x=16x frequency value) by ref clock. Make sure to
490 * round 'up' the result
491 */
492 dsi_ratio = DIV_ROUND_UP(dsi_clk * 2, BXT_REF_CLOCK_KHZ);
Deepak Mf340c2f2017-02-17 18:13:32 +0530493
494 if (IS_BROXTON(dev_priv)) {
495 dsi_ratio_min = BXT_DSI_PLL_RATIO_MIN;
496 dsi_ratio_max = BXT_DSI_PLL_RATIO_MAX;
497 } else {
498 dsi_ratio_min = GLK_DSI_PLL_RATIO_MIN;
499 dsi_ratio_max = GLK_DSI_PLL_RATIO_MAX;
500 }
501
502 if (dsi_ratio < dsi_ratio_min || dsi_ratio > dsi_ratio_max) {
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530503 DRM_ERROR("Cant get a suitable ratio from DSI PLL ratios\n");
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300504 return -ECHRNG;
Deepak Mf340c2f2017-02-17 18:13:32 +0530505 } else
506 DRM_DEBUG_KMS("DSI PLL calculation is Done!!\n");
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530507
508 /*
509 * Program DSI ratio and Select MIPIC and MIPIA PLL output as 8x
510 * Spec says both have to be programmed, even if one is not getting
511 * used. Configure MIPI_CLOCK_CTL dividers in modeset
512 */
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300513 config->dsi_pll.ctrl = dsi_ratio | BXT_DSIA_16X_BY2 | BXT_DSIC_16X_BY2;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530514
515 /* As per recommendation from hardware team,
516 * Prog PVD ratio =1 if dsi ratio <= 50
517 */
Deepak Mf340c2f2017-02-17 18:13:32 +0530518 if (IS_BROXTON(dev_priv) && dsi_ratio <= 50)
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300519 config->dsi_pll.ctrl |= BXT_DSI_PLL_PVD_RATIO_1;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530520
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300521 return 0;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530522}
523
Deepak Mbcc65702017-02-17 18:13:34 +0530524static void gen9lp_enable_dsi_pll(struct intel_encoder *encoder,
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300525 const struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530526{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100527 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530528 struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
529 enum port port;
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530530 u32 val;
531
532 DRM_DEBUG_KMS("\n");
533
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530534 /* Configure PLL vales */
Ville Syrjälä062efa52016-04-12 22:14:36 +0300535 I915_WRITE(BXT_DSI_PLL_CTL, config->dsi_pll.ctrl);
536 POSTING_READ(BXT_DSI_PLL_CTL);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530537
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530538 /* Program TX, RX, Dphy clocks */
Deepak M09a568e2017-02-17 18:13:33 +0530539 if (IS_BROXTON(dev_priv)) {
540 for_each_dsi_port(port, intel_dsi->ports)
541 bxt_dsi_program_clocks(encoder->base.dev, port, config);
Deepak Mbcc65702017-02-17 18:13:34 +0530542 } else {
543 glk_dsi_program_esc_clock(encoder->base.dev, config);
Deepak M09a568e2017-02-17 18:13:33 +0530544 }
Shashank Sharma11b8e4f2015-09-23 23:27:17 +0530545
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530546 /* Enable DSI PLL */
547 val = I915_READ(BXT_DSI_PLL_ENABLE);
548 val |= BXT_DSI_PLL_DO_ENABLE;
549 I915_WRITE(BXT_DSI_PLL_ENABLE, val);
550
551 /* Timeout and fail if PLL not locked */
Chris Wilson186f1c52016-06-30 15:33:20 +0100552 if (intel_wait_for_register(dev_priv,
553 BXT_DSI_PLL_ENABLE,
554 BXT_DSI_PLL_LOCKED,
555 BXT_DSI_PLL_LOCKED,
556 1)) {
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530557 DRM_ERROR("Timed out waiting for DSI PLL to lock\n");
558 return;
559 }
560
561 DRM_DEBUG_KMS("DSI PLL locked\n");
562}
563
Imre Deakdb18b6a2016-03-24 12:41:40 +0200564bool intel_dsi_pll_is_enabled(struct drm_i915_private *dev_priv)
565{
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200566 if (IS_GEN9_LP(dev_priv))
Imre Deakdb18b6a2016-03-24 12:41:40 +0200567 return bxt_dsi_pll_is_enabled(dev_priv);
568
569 MISSING_CASE(INTEL_DEVID(dev_priv));
570
571 return false;
572}
573
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300574int intel_compute_dsi_pll(struct intel_encoder *encoder,
575 struct intel_crtc_state *config)
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530576{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100577 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530578
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100579 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300580 return vlv_compute_dsi_pll(encoder, config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200581 else if (IS_GEN9_LP(dev_priv))
Deepak Mf340c2f2017-02-17 18:13:32 +0530582 return gen9lp_compute_dsi_pll(encoder, config);
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300583
584 return -ENODEV;
585}
586
587void intel_enable_dsi_pll(struct intel_encoder *encoder,
588 const struct intel_crtc_state *config)
589{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100590 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300591
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100592 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Ville Syrjälä47eacba2016-04-12 22:14:35 +0300593 vlv_enable_dsi_pll(encoder, config);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200594 else if (IS_GEN9_LP(dev_priv))
Deepak Mbcc65702017-02-17 18:13:34 +0530595 gen9lp_enable_dsi_pll(encoder, config);
Shashank Sharmacfe01a52015-09-01 19:41:38 +0530596}
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530597
598void intel_disable_dsi_pll(struct intel_encoder *encoder)
599{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100600 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530601
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100602 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530603 vlv_disable_dsi_pll(encoder);
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200604 else if (IS_GEN9_LP(dev_priv))
Shashank Sharmafe88fc62015-09-01 19:41:39 +0530605 bxt_disable_dsi_pll(encoder);
606}
Shashank Sharmab389a452015-09-01 19:41:44 +0530607
Deepak M09a568e2017-02-17 18:13:33 +0530608static void gen9lp_dsi_reset_clocks(struct intel_encoder *encoder,
609 enum port port)
Shashank Sharmab389a452015-09-01 19:41:44 +0530610{
611 u32 tmp;
612 struct drm_device *dev = encoder->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100613 struct drm_i915_private *dev_priv = to_i915(dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530614
615 /* Clear old configurations */
Deepak M09a568e2017-02-17 18:13:33 +0530616 if (IS_BROXTON(dev_priv)) {
617 tmp = I915_READ(BXT_MIPI_CLOCK_CTL);
618 tmp &= ~(BXT_MIPI_TX_ESCLK_FIXDIV_MASK(port));
619 tmp &= ~(BXT_MIPI_RX_ESCLK_UPPER_FIXDIV_MASK(port));
620 tmp &= ~(BXT_MIPI_8X_BY3_DIVIDER_MASK(port));
621 tmp &= ~(BXT_MIPI_RX_ESCLK_LOWER_FIXDIV_MASK(port));
622 I915_WRITE(BXT_MIPI_CLOCK_CTL, tmp);
Deepak Mbcc65702017-02-17 18:13:34 +0530623 } else {
624 tmp = I915_READ(MIPIO_TXESC_CLK_DIV1);
625 tmp &= ~GLK_TX_ESC_CLK_DIV1_MASK;
626 I915_WRITE(MIPIO_TXESC_CLK_DIV1, tmp);
627
628 tmp = I915_READ(MIPIO_TXESC_CLK_DIV2);
629 tmp &= ~GLK_TX_ESC_CLK_DIV2_MASK;
630 I915_WRITE(MIPIO_TXESC_CLK_DIV2, tmp);
Deepak M09a568e2017-02-17 18:13:33 +0530631 }
Shashank Sharmab389a452015-09-01 19:41:44 +0530632 I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
633}
634
635void intel_dsi_reset_clocks(struct intel_encoder *encoder, enum port port)
636{
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100637 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Shashank Sharmab389a452015-09-01 19:41:44 +0530638
Ander Conselvan de Oliveiracc3f90f2016-12-02 10:23:49 +0200639 if (IS_GEN9_LP(dev_priv))
Deepak M09a568e2017-02-17 18:13:33 +0530640 gen9lp_dsi_reset_clocks(encoder, port);
Tvrtko Ursuline2d214a2016-10-13 11:03:04 +0100641 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
Shashank Sharmab389a452015-09-01 19:41:44 +0530642 vlv_dsi_reset_clocks(encoder, port);
643}