intel_dsi_pll.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  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. #include <linux/kernel.h>
  28. #include "intel_drv.h"
  29. #include "i915_drv.h"
  30. #include "intel_dsi.h"
  31. #define DSI_HSS_PACKET_SIZE 4
  32. #define DSI_HSE_PACKET_SIZE 4
  33. #define DSI_HSA_PACKET_EXTRA_SIZE 6
  34. #define DSI_HBP_PACKET_EXTRA_SIZE 6
  35. #define DSI_HACTIVE_PACKET_EXTRA_SIZE 6
  36. #define DSI_HFP_PACKET_EXTRA_SIZE 6
  37. #define DSI_EOTP_PACKET_SIZE 4
  38. static int dsi_pixel_format_bpp(int pixel_format)
  39. {
  40. int bpp;
  41. switch (pixel_format) {
  42. default:
  43. case VID_MODE_FORMAT_RGB888:
  44. case VID_MODE_FORMAT_RGB666_LOOSE:
  45. bpp = 24;
  46. break;
  47. case VID_MODE_FORMAT_RGB666:
  48. bpp = 18;
  49. break;
  50. case VID_MODE_FORMAT_RGB565:
  51. bpp = 16;
  52. break;
  53. }
  54. return bpp;
  55. }
  56. struct dsi_mnp {
  57. u32 dsi_pll_ctrl;
  58. u32 dsi_pll_div;
  59. };
  60. static const u32 lfsr_converts[] = {
  61. 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
  62. 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
  63. 106, 53, 282, 397, 454, 227, 113, 56, 284, 142, /* 81 - 90 */
  64. 71, 35, 273, 136, 324, 418, 465, 488, 500, 506 /* 91 - 100 */
  65. };
  66. #ifdef DSI_CLK_FROM_RR
  67. static u32 dsi_rr_formula(const struct drm_display_mode *mode,
  68. int pixel_format, int video_mode_format,
  69. int lane_count, bool eotp)
  70. {
  71. u32 bpp;
  72. u32 hactive, vactive, hfp, hsync, hbp, vfp, vsync, vbp;
  73. u32 hsync_bytes, hbp_bytes, hactive_bytes, hfp_bytes;
  74. u32 bytes_per_line, bytes_per_frame;
  75. u32 num_frames;
  76. u32 bytes_per_x_frames, bytes_per_x_frames_x_lanes;
  77. u32 dsi_bit_clock_hz;
  78. u32 dsi_clk;
  79. bpp = dsi_pixel_format_bpp(pixel_format);
  80. hactive = mode->hdisplay;
  81. vactive = mode->vdisplay;
  82. hfp = mode->hsync_start - mode->hdisplay;
  83. hsync = mode->hsync_end - mode->hsync_start;
  84. hbp = mode->htotal - mode->hsync_end;
  85. vfp = mode->vsync_start - mode->vdisplay;
  86. vsync = mode->vsync_end - mode->vsync_start;
  87. vbp = mode->vtotal - mode->vsync_end;
  88. hsync_bytes = DIV_ROUND_UP(hsync * bpp, 8);
  89. hbp_bytes = DIV_ROUND_UP(hbp * bpp, 8);
  90. hactive_bytes = DIV_ROUND_UP(hactive * bpp, 8);
  91. hfp_bytes = DIV_ROUND_UP(hfp * bpp, 8);
  92. bytes_per_line = DSI_HSS_PACKET_SIZE + hsync_bytes +
  93. DSI_HSA_PACKET_EXTRA_SIZE + DSI_HSE_PACKET_SIZE +
  94. hbp_bytes + DSI_HBP_PACKET_EXTRA_SIZE +
  95. hactive_bytes + DSI_HACTIVE_PACKET_EXTRA_SIZE +
  96. hfp_bytes + DSI_HFP_PACKET_EXTRA_SIZE;
  97. /*
  98. * XXX: Need to accurately calculate LP to HS transition timeout and add
  99. * it to bytes_per_line/bytes_per_frame.
  100. */
  101. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  102. bytes_per_line += DSI_EOTP_PACKET_SIZE;
  103. bytes_per_frame = vsync * bytes_per_line + vbp * bytes_per_line +
  104. vactive * bytes_per_line + vfp * bytes_per_line;
  105. if (eotp &&
  106. (video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ||
  107. video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS))
  108. bytes_per_frame += DSI_EOTP_PACKET_SIZE;
  109. num_frames = drm_mode_vrefresh(mode);
  110. bytes_per_x_frames = num_frames * bytes_per_frame;
  111. bytes_per_x_frames_x_lanes = bytes_per_x_frames / lane_count;
  112. /* the dsi clock is divided by 2 in the hardware to get dsi ddr clock */
  113. dsi_bit_clock_hz = bytes_per_x_frames_x_lanes * 8;
  114. dsi_clk = dsi_bit_clock_hz / 1000;
  115. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  116. dsi_clk *= 2;
  117. return dsi_clk;
  118. }
  119. #else
  120. /* Get DSI clock from pixel clock */
  121. static u32 dsi_clk_from_pclk(u32 pclk, int pixel_format, int lane_count)
  122. {
  123. u32 dsi_clk_khz;
  124. u32 bpp = dsi_pixel_format_bpp(pixel_format);
  125. /* DSI data rate = pixel clock * bits per pixel / lane count
  126. pixel clock is converted from KHz to Hz */
  127. dsi_clk_khz = DIV_ROUND_CLOSEST(pclk * bpp, lane_count);
  128. return dsi_clk_khz;
  129. }
  130. #endif
  131. static int dsi_calc_mnp(struct drm_i915_private *dev_priv,
  132. struct dsi_mnp *dsi_mnp, int target_dsi_clk)
  133. {
  134. unsigned int calc_m = 0, calc_p = 0;
  135. unsigned int m_min, m_max, p_min = 2, p_max = 6;
  136. unsigned int m, n, p;
  137. int ref_clk;
  138. int delta = target_dsi_clk;
  139. u32 m_seed;
  140. /* target_dsi_clk is expected in kHz */
  141. if (target_dsi_clk < 300000 || target_dsi_clk > 1150000) {
  142. DRM_ERROR("DSI CLK Out of Range\n");
  143. return -ECHRNG;
  144. }
  145. if (IS_CHERRYVIEW(dev_priv)) {
  146. ref_clk = 100000;
  147. n = 4;
  148. m_min = 70;
  149. m_max = 96;
  150. } else {
  151. ref_clk = 25000;
  152. n = 1;
  153. m_min = 62;
  154. m_max = 92;
  155. }
  156. for (m = m_min; m <= m_max && delta; m++) {
  157. for (p = p_min; p <= p_max && delta; p++) {
  158. /*
  159. * Find the optimal m and p divisors with minimal delta
  160. * +/- the required clock
  161. */
  162. int calc_dsi_clk = (m * ref_clk) / (p * n);
  163. int d = abs(target_dsi_clk - calc_dsi_clk);
  164. if (d < delta) {
  165. delta = d;
  166. calc_m = m;
  167. calc_p = p;
  168. }
  169. }
  170. }
  171. /* register has log2(N1), this works fine for powers of two */
  172. n = ffs(n) - 1;
  173. m_seed = lfsr_converts[calc_m - 62];
  174. dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
  175. dsi_mnp->dsi_pll_div = n << DSI_PLL_N1_DIV_SHIFT |
  176. m_seed << DSI_PLL_M1_DIV_SHIFT;
  177. return 0;
  178. }
  179. /*
  180. * XXX: The muxing and gating is hard coded for now. Need to add support for
  181. * sharing PLLs with two DSI outputs.
  182. */
  183. static void vlv_configure_dsi_pll(struct intel_encoder *encoder)
  184. {
  185. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  186. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  187. int ret;
  188. struct dsi_mnp dsi_mnp;
  189. u32 dsi_clk;
  190. dsi_clk = dsi_clk_from_pclk(intel_dsi->pclk, intel_dsi->pixel_format,
  191. intel_dsi->lane_count);
  192. ret = dsi_calc_mnp(dev_priv, &dsi_mnp, dsi_clk);
  193. if (ret) {
  194. DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
  195. return;
  196. }
  197. if (intel_dsi->ports & (1 << PORT_A))
  198. dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
  199. if (intel_dsi->ports & (1 << PORT_C))
  200. dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI1_DSIPLL;
  201. DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
  202. dsi_mnp.dsi_pll_div, dsi_mnp.dsi_pll_ctrl);
  203. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
  204. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, dsi_mnp.dsi_pll_div);
  205. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, dsi_mnp.dsi_pll_ctrl);
  206. }
  207. void vlv_enable_dsi_pll(struct intel_encoder *encoder)
  208. {
  209. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  210. u32 tmp;
  211. DRM_DEBUG_KMS("\n");
  212. mutex_lock(&dev_priv->sb_lock);
  213. vlv_configure_dsi_pll(encoder);
  214. /* wait at least 0.5 us after ungating before enabling VCO */
  215. usleep_range(1, 10);
  216. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  217. tmp |= DSI_PLL_VCO_EN;
  218. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  219. if (wait_for(vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL) &
  220. DSI_PLL_LOCK, 20)) {
  221. mutex_unlock(&dev_priv->sb_lock);
  222. DRM_ERROR("DSI PLL lock failed\n");
  223. return;
  224. }
  225. mutex_unlock(&dev_priv->sb_lock);
  226. DRM_DEBUG_KMS("DSI PLL locked\n");
  227. }
  228. void vlv_disable_dsi_pll(struct intel_encoder *encoder)
  229. {
  230. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  231. u32 tmp;
  232. DRM_DEBUG_KMS("\n");
  233. mutex_lock(&dev_priv->sb_lock);
  234. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  235. tmp &= ~DSI_PLL_VCO_EN;
  236. tmp |= DSI_PLL_LDO_GATE;
  237. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  238. mutex_unlock(&dev_priv->sb_lock);
  239. }
  240. static void assert_bpp_mismatch(int pixel_format, int pipe_bpp)
  241. {
  242. int bpp = dsi_pixel_format_bpp(pixel_format);
  243. WARN(bpp != pipe_bpp,
  244. "bpp match assertion failure (expected %d, current %d)\n",
  245. bpp, pipe_bpp);
  246. }
  247. u32 vlv_get_dsi_pclk(struct intel_encoder *encoder, int pipe_bpp)
  248. {
  249. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  250. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  251. u32 dsi_clock, pclk;
  252. u32 pll_ctl, pll_div;
  253. u32 m = 0, p = 0, n;
  254. int refclk = 25000;
  255. int i;
  256. DRM_DEBUG_KMS("\n");
  257. mutex_lock(&dev_priv->sb_lock);
  258. pll_ctl = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  259. pll_div = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_DIVIDER);
  260. mutex_unlock(&dev_priv->sb_lock);
  261. /* mask out other bits and extract the P1 divisor */
  262. pll_ctl &= DSI_PLL_P1_POST_DIV_MASK;
  263. pll_ctl = pll_ctl >> (DSI_PLL_P1_POST_DIV_SHIFT - 2);
  264. /* N1 divisor */
  265. n = (pll_div & DSI_PLL_N1_DIV_MASK) >> DSI_PLL_N1_DIV_SHIFT;
  266. n = 1 << n; /* register has log2(N1) */
  267. /* mask out the other bits and extract the M1 divisor */
  268. pll_div &= DSI_PLL_M1_DIV_MASK;
  269. pll_div = pll_div >> DSI_PLL_M1_DIV_SHIFT;
  270. while (pll_ctl) {
  271. pll_ctl = pll_ctl >> 1;
  272. p++;
  273. }
  274. p--;
  275. if (!p) {
  276. DRM_ERROR("wrong P1 divisor\n");
  277. return 0;
  278. }
  279. for (i = 0; i < ARRAY_SIZE(lfsr_converts); i++) {
  280. if (lfsr_converts[i] == pll_div)
  281. break;
  282. }
  283. if (i == ARRAY_SIZE(lfsr_converts)) {
  284. DRM_ERROR("wrong m_seed programmed\n");
  285. return 0;
  286. }
  287. m = i + 62;
  288. dsi_clock = (m * refclk) / (p * n);
  289. /* pixel_format and pipe_bpp should agree */
  290. assert_bpp_mismatch(intel_dsi->pixel_format, pipe_bpp);
  291. pclk = DIV_ROUND_CLOSEST(dsi_clock * intel_dsi->lane_count, pipe_bpp);
  292. return pclk;
  293. }