intel_dsi_pll.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. struct dsi_mnp {
  39. u32 dsi_pll_ctrl;
  40. u32 dsi_pll_div;
  41. };
  42. static const u32 lfsr_converts[] = {
  43. 426, 469, 234, 373, 442, 221, 110, 311, 411, /* 62 - 70 */
  44. 461, 486, 243, 377, 188, 350, 175, 343, 427, 213, /* 71 - 80 */
  45. 106, 53, 282, 397, 354, 227, 113, 56, 284, 142, /* 81 - 90 */
  46. 71, 35 /* 91 - 92 */
  47. };
  48. #ifdef DSI_CLK_FROM_RR
  49. static u32 dsi_rr_formula(const struct drm_display_mode *mode,
  50. int pixel_format, int video_mode_format,
  51. int lane_count, bool eotp)
  52. {
  53. u32 bpp;
  54. u32 hactive, vactive, hfp, hsync, hbp, vfp, vsync, vbp;
  55. u32 hsync_bytes, hbp_bytes, hactive_bytes, hfp_bytes;
  56. u32 bytes_per_line, bytes_per_frame;
  57. u32 num_frames;
  58. u32 bytes_per_x_frames, bytes_per_x_frames_x_lanes;
  59. u32 dsi_bit_clock_hz;
  60. u32 dsi_clk;
  61. switch (pixel_format) {
  62. default:
  63. case VID_MODE_FORMAT_RGB888:
  64. case VID_MODE_FORMAT_RGB666_LOOSE:
  65. bpp = 24;
  66. break;
  67. case VID_MODE_FORMAT_RGB666:
  68. bpp = 18;
  69. break;
  70. case VID_MODE_FORMAT_RGB565:
  71. bpp = 16;
  72. break;
  73. }
  74. hactive = mode->hdisplay;
  75. vactive = mode->vdisplay;
  76. hfp = mode->hsync_start - mode->hdisplay;
  77. hsync = mode->hsync_end - mode->hsync_start;
  78. hbp = mode->htotal - mode->hsync_end;
  79. vfp = mode->vsync_start - mode->vdisplay;
  80. vsync = mode->vsync_end - mode->vsync_start;
  81. vbp = mode->vtotal - mode->vsync_end;
  82. hsync_bytes = DIV_ROUND_UP(hsync * bpp, 8);
  83. hbp_bytes = DIV_ROUND_UP(hbp * bpp, 8);
  84. hactive_bytes = DIV_ROUND_UP(hactive * bpp, 8);
  85. hfp_bytes = DIV_ROUND_UP(hfp * bpp, 8);
  86. bytes_per_line = DSI_HSS_PACKET_SIZE + hsync_bytes +
  87. DSI_HSA_PACKET_EXTRA_SIZE + DSI_HSE_PACKET_SIZE +
  88. hbp_bytes + DSI_HBP_PACKET_EXTRA_SIZE +
  89. hactive_bytes + DSI_HACTIVE_PACKET_EXTRA_SIZE +
  90. hfp_bytes + DSI_HFP_PACKET_EXTRA_SIZE;
  91. /*
  92. * XXX: Need to accurately calculate LP to HS transition timeout and add
  93. * it to bytes_per_line/bytes_per_frame.
  94. */
  95. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  96. bytes_per_line += DSI_EOTP_PACKET_SIZE;
  97. bytes_per_frame = vsync * bytes_per_line + vbp * bytes_per_line +
  98. vactive * bytes_per_line + vfp * bytes_per_line;
  99. if (eotp &&
  100. (video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ||
  101. video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS))
  102. bytes_per_frame += DSI_EOTP_PACKET_SIZE;
  103. num_frames = drm_mode_vrefresh(mode);
  104. bytes_per_x_frames = num_frames * bytes_per_frame;
  105. bytes_per_x_frames_x_lanes = bytes_per_x_frames / lane_count;
  106. /* the dsi clock is divided by 2 in the hardware to get dsi ddr clock */
  107. dsi_bit_clock_hz = bytes_per_x_frames_x_lanes * 8;
  108. dsi_clk = dsi_bit_clock_hz / 1000;
  109. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  110. dsi_clk *= 2;
  111. return dsi_clk;
  112. }
  113. #else
  114. /* Get DSI clock from pixel clock */
  115. static u32 dsi_clk_from_pclk(const struct drm_display_mode *mode,
  116. int pixel_format, int lane_count)
  117. {
  118. u32 dsi_clk_khz;
  119. u32 bpp;
  120. switch (pixel_format) {
  121. default:
  122. case VID_MODE_FORMAT_RGB888:
  123. case VID_MODE_FORMAT_RGB666_LOOSE:
  124. bpp = 24;
  125. break;
  126. case VID_MODE_FORMAT_RGB666:
  127. bpp = 18;
  128. break;
  129. case VID_MODE_FORMAT_RGB565:
  130. bpp = 16;
  131. break;
  132. }
  133. /* DSI data rate = pixel clock * bits per pixel / lane count
  134. pixel clock is converted from KHz to Hz */
  135. dsi_clk_khz = DIV_ROUND_CLOSEST(mode->clock * bpp, lane_count);
  136. return dsi_clk_khz;
  137. }
  138. #endif
  139. static int dsi_calc_mnp(u32 dsi_clk, struct dsi_mnp *dsi_mnp)
  140. {
  141. u32 m, n, p;
  142. u32 ref_clk;
  143. u32 error;
  144. u32 tmp_error;
  145. int target_dsi_clk;
  146. int calc_dsi_clk;
  147. u32 calc_m;
  148. u32 calc_p;
  149. u32 m_seed;
  150. /* dsi_clk is expected in KHZ */
  151. if (dsi_clk < 300000 || dsi_clk > 1150000) {
  152. DRM_ERROR("DSI CLK Out of Range\n");
  153. return -ECHRNG;
  154. }
  155. ref_clk = 25000;
  156. target_dsi_clk = dsi_clk;
  157. error = 0xFFFFFFFF;
  158. tmp_error = 0xFFFFFFFF;
  159. calc_m = 0;
  160. calc_p = 0;
  161. for (m = 62; m <= 92; m++) {
  162. for (p = 2; p <= 6; p++) {
  163. /* Find the optimal m and p divisors
  164. with minimal error +/- the required clock */
  165. calc_dsi_clk = (m * ref_clk) / p;
  166. if (calc_dsi_clk == target_dsi_clk) {
  167. calc_m = m;
  168. calc_p = p;
  169. error = 0;
  170. break;
  171. } else
  172. tmp_error = abs(target_dsi_clk - calc_dsi_clk);
  173. if (tmp_error < error) {
  174. error = tmp_error;
  175. calc_m = m;
  176. calc_p = p;
  177. }
  178. }
  179. if (error == 0)
  180. break;
  181. }
  182. m_seed = lfsr_converts[calc_m - 62];
  183. n = 1;
  184. dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
  185. dsi_mnp->dsi_pll_div = (n - 1) << DSI_PLL_N1_DIV_SHIFT |
  186. m_seed << DSI_PLL_M1_DIV_SHIFT;
  187. return 0;
  188. }
  189. /*
  190. * XXX: The muxing and gating is hard coded for now. Need to add support for
  191. * sharing PLLs with two DSI outputs.
  192. */
  193. static void vlv_configure_dsi_pll(struct intel_encoder *encoder)
  194. {
  195. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  196. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  197. const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
  198. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  199. int ret;
  200. struct dsi_mnp dsi_mnp;
  201. u32 dsi_clk;
  202. dsi_clk = dsi_clk_from_pclk(mode, intel_dsi->pixel_format,
  203. intel_dsi->lane_count);
  204. ret = dsi_calc_mnp(dsi_clk, &dsi_mnp);
  205. if (ret) {
  206. DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
  207. return;
  208. }
  209. dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
  210. DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
  211. dsi_mnp.dsi_pll_div, dsi_mnp.dsi_pll_ctrl);
  212. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
  213. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, dsi_mnp.dsi_pll_div);
  214. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, dsi_mnp.dsi_pll_ctrl);
  215. }
  216. void vlv_enable_dsi_pll(struct intel_encoder *encoder)
  217. {
  218. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  219. u32 tmp;
  220. DRM_DEBUG_KMS("\n");
  221. mutex_lock(&dev_priv->dpio_lock);
  222. vlv_configure_dsi_pll(encoder);
  223. /* wait at least 0.5 us after ungating before enabling VCO */
  224. usleep_range(1, 10);
  225. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  226. tmp |= DSI_PLL_VCO_EN;
  227. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  228. mutex_unlock(&dev_priv->dpio_lock);
  229. if (wait_for(I915_READ(PIPECONF(PIPE_A)) & PIPECONF_DSI_PLL_LOCKED, 20)) {
  230. DRM_ERROR("DSI PLL lock failed\n");
  231. return;
  232. }
  233. DRM_DEBUG_KMS("DSI PLL locked\n");
  234. }
  235. void vlv_disable_dsi_pll(struct intel_encoder *encoder)
  236. {
  237. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  238. u32 tmp;
  239. DRM_DEBUG_KMS("\n");
  240. mutex_lock(&dev_priv->dpio_lock);
  241. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  242. tmp &= ~DSI_PLL_VCO_EN;
  243. tmp |= DSI_PLL_LDO_GATE;
  244. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  245. mutex_unlock(&dev_priv->dpio_lock);
  246. }