intel_dsi_pll.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. static u32 dsi_rr_formula(const struct drm_display_mode *mode,
  49. int pixel_format, int video_mode_format,
  50. int lane_count, bool eotp)
  51. {
  52. u32 bpp;
  53. u32 hactive, vactive, hfp, hsync, hbp, vfp, vsync, vbp;
  54. u32 hsync_bytes, hbp_bytes, hactive_bytes, hfp_bytes;
  55. u32 bytes_per_line, bytes_per_frame;
  56. u32 num_frames;
  57. u32 bytes_per_x_frames, bytes_per_x_frames_x_lanes;
  58. u32 dsi_bit_clock_hz;
  59. u32 dsi_clk;
  60. switch (pixel_format) {
  61. default:
  62. case VID_MODE_FORMAT_RGB888:
  63. case VID_MODE_FORMAT_RGB666_LOOSE:
  64. bpp = 24;
  65. break;
  66. case VID_MODE_FORMAT_RGB666:
  67. bpp = 18;
  68. break;
  69. case VID_MODE_FORMAT_RGB565:
  70. bpp = 16;
  71. break;
  72. }
  73. hactive = mode->hdisplay;
  74. vactive = mode->vdisplay;
  75. hfp = mode->hsync_start - mode->hdisplay;
  76. hsync = mode->hsync_end - mode->hsync_start;
  77. hbp = mode->htotal - mode->hsync_end;
  78. vfp = mode->vsync_start - mode->vdisplay;
  79. vsync = mode->vsync_end - mode->vsync_start;
  80. vbp = mode->vtotal - mode->vsync_end;
  81. hsync_bytes = DIV_ROUND_UP(hsync * bpp, 8);
  82. hbp_bytes = DIV_ROUND_UP(hbp * bpp, 8);
  83. hactive_bytes = DIV_ROUND_UP(hactive * bpp, 8);
  84. hfp_bytes = DIV_ROUND_UP(hfp * bpp, 8);
  85. bytes_per_line = DSI_HSS_PACKET_SIZE + hsync_bytes +
  86. DSI_HSA_PACKET_EXTRA_SIZE + DSI_HSE_PACKET_SIZE +
  87. hbp_bytes + DSI_HBP_PACKET_EXTRA_SIZE +
  88. hactive_bytes + DSI_HACTIVE_PACKET_EXTRA_SIZE +
  89. hfp_bytes + DSI_HFP_PACKET_EXTRA_SIZE;
  90. /*
  91. * XXX: Need to accurately calculate LP to HS transition timeout and add
  92. * it to bytes_per_line/bytes_per_frame.
  93. */
  94. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  95. bytes_per_line += DSI_EOTP_PACKET_SIZE;
  96. bytes_per_frame = vsync * bytes_per_line + vbp * bytes_per_line +
  97. vactive * bytes_per_line + vfp * bytes_per_line;
  98. if (eotp &&
  99. (video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ||
  100. video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS))
  101. bytes_per_frame += DSI_EOTP_PACKET_SIZE;
  102. num_frames = drm_mode_vrefresh(mode);
  103. bytes_per_x_frames = num_frames * bytes_per_frame;
  104. bytes_per_x_frames_x_lanes = bytes_per_x_frames / lane_count;
  105. /* the dsi clock is divided by 2 in the hardware to get dsi ddr clock */
  106. dsi_bit_clock_hz = bytes_per_x_frames_x_lanes * 8;
  107. dsi_clk = dsi_bit_clock_hz / (1000 * 1000);
  108. if (eotp && video_mode_format == VIDEO_MODE_BURST)
  109. dsi_clk *= 2;
  110. return dsi_clk;
  111. }
  112. #ifdef MNP_FROM_TABLE
  113. struct dsi_clock_table {
  114. u32 freq;
  115. u8 m;
  116. u8 p;
  117. };
  118. static const struct dsi_clock_table dsi_clk_tbl[] = {
  119. {300, 72, 6}, {313, 75, 6}, {323, 78, 6}, {333, 80, 6},
  120. {343, 82, 6}, {353, 85, 6}, {363, 87, 6}, {373, 90, 6},
  121. {383, 92, 6}, {390, 78, 5}, {393, 79, 5}, {400, 80, 5},
  122. {401, 80, 5}, {402, 80, 5}, {403, 81, 5}, {404, 81, 5},
  123. {405, 81, 5}, {406, 81, 5}, {407, 81, 5}, {408, 82, 5},
  124. {409, 82, 5}, {410, 82, 5}, {411, 82, 5}, {412, 82, 5},
  125. {413, 83, 5}, {414, 83, 5}, {415, 83, 5}, {416, 83, 5},
  126. {417, 83, 5}, {418, 84, 5}, {419, 84, 5}, {420, 84, 5},
  127. {430, 86, 5}, {440, 88, 5}, {450, 90, 5}, {460, 92, 5},
  128. {470, 75, 4}, {480, 77, 4}, {490, 78, 4}, {500, 80, 4},
  129. {510, 82, 4}, {520, 83, 4}, {530, 85, 4}, {540, 86, 4},
  130. {550, 88, 4}, {560, 90, 4}, {570, 91, 4}, {580, 70, 3},
  131. {590, 71, 3}, {600, 72, 3}, {610, 73, 3}, {620, 74, 3},
  132. {630, 76, 3}, {640, 77, 3}, {650, 78, 3}, {660, 79, 3},
  133. {670, 80, 3}, {680, 82, 3}, {690, 83, 3}, {700, 84, 3},
  134. {710, 85, 3}, {720, 86, 3}, {730, 88, 3}, {740, 89, 3},
  135. {750, 90, 3}, {760, 91, 3}, {770, 92, 3}, {780, 62, 2},
  136. {790, 63, 2}, {800, 64, 2}, {880, 70, 2}, {900, 72, 2},
  137. {1000, 80, 2}, /* dsi clock frequency in Mhz*/
  138. };
  139. static int dsi_calc_mnp(u32 dsi_clk, struct dsi_mnp *dsi_mnp)
  140. {
  141. unsigned int i;
  142. u8 m;
  143. u8 n;
  144. u8 p;
  145. u32 m_seed;
  146. if (dsi_clk < 300 || dsi_clk > 1000)
  147. return -ECHRNG;
  148. for (i = 0; i <= ARRAY_SIZE(dsi_clk_tbl); i++) {
  149. if (dsi_clk_tbl[i].freq > dsi_clk)
  150. break;
  151. }
  152. m = dsi_clk_tbl[i].m;
  153. p = dsi_clk_tbl[i].p;
  154. m_seed = lfsr_converts[m - 62];
  155. n = 1;
  156. dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + p - 2);
  157. dsi_mnp->dsi_pll_div = (n - 1) << DSI_PLL_N1_DIV_SHIFT |
  158. m_seed << DSI_PLL_M1_DIV_SHIFT;
  159. return 0;
  160. }
  161. #else
  162. static int dsi_calc_mnp(u32 dsi_clk, struct dsi_mnp *dsi_mnp)
  163. {
  164. u32 m, n, p;
  165. u32 ref_clk;
  166. u32 error;
  167. u32 tmp_error;
  168. u32 target_dsi_clk;
  169. u32 calc_dsi_clk;
  170. u32 calc_m;
  171. u32 calc_p;
  172. u32 m_seed;
  173. if (dsi_clk < 300 || dsi_clk > 1150) {
  174. DRM_ERROR("DSI CLK Out of Range\n");
  175. return -ECHRNG;
  176. }
  177. ref_clk = 25000;
  178. target_dsi_clk = dsi_clk * 1000;
  179. error = 0xFFFFFFFF;
  180. calc_m = 0;
  181. calc_p = 0;
  182. for (m = 62; m <= 92; m++) {
  183. for (p = 2; p <= 6; p++) {
  184. calc_dsi_clk = (m * ref_clk) / p;
  185. if (calc_dsi_clk >= target_dsi_clk) {
  186. tmp_error = calc_dsi_clk - target_dsi_clk;
  187. if (tmp_error < error) {
  188. error = tmp_error;
  189. calc_m = m;
  190. calc_p = p;
  191. }
  192. }
  193. }
  194. }
  195. m_seed = lfsr_converts[calc_m - 62];
  196. n = 1;
  197. dsi_mnp->dsi_pll_ctrl = 1 << (DSI_PLL_P1_POST_DIV_SHIFT + calc_p - 2);
  198. dsi_mnp->dsi_pll_div = (n - 1) << DSI_PLL_N1_DIV_SHIFT |
  199. m_seed << DSI_PLL_M1_DIV_SHIFT;
  200. return 0;
  201. }
  202. #endif
  203. /*
  204. * XXX: The muxing and gating is hard coded for now. Need to add support for
  205. * sharing PLLs with two DSI outputs.
  206. */
  207. static void vlv_configure_dsi_pll(struct intel_encoder *encoder)
  208. {
  209. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  210. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  211. const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
  212. struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
  213. int ret;
  214. struct dsi_mnp dsi_mnp;
  215. u32 dsi_clk;
  216. dsi_clk = dsi_rr_formula(mode, intel_dsi->pixel_format,
  217. intel_dsi->video_mode_format,
  218. intel_dsi->lane_count, !intel_dsi->eot_disable);
  219. ret = dsi_calc_mnp(dsi_clk, &dsi_mnp);
  220. if (ret) {
  221. DRM_DEBUG_KMS("dsi_calc_mnp failed\n");
  222. return;
  223. }
  224. dsi_mnp.dsi_pll_ctrl |= DSI_PLL_CLK_GATE_DSI0_DSIPLL;
  225. DRM_DEBUG_KMS("dsi pll div %08x, ctrl %08x\n",
  226. dsi_mnp.dsi_pll_div, dsi_mnp.dsi_pll_ctrl);
  227. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, 0);
  228. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_DIVIDER, dsi_mnp.dsi_pll_div);
  229. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, dsi_mnp.dsi_pll_ctrl);
  230. }
  231. void vlv_enable_dsi_pll(struct intel_encoder *encoder)
  232. {
  233. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  234. u32 tmp;
  235. DRM_DEBUG_KMS("\n");
  236. mutex_lock(&dev_priv->dpio_lock);
  237. vlv_configure_dsi_pll(encoder);
  238. /* wait at least 0.5 us after ungating before enabling VCO */
  239. usleep_range(1, 10);
  240. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  241. tmp |= DSI_PLL_VCO_EN;
  242. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  243. mutex_unlock(&dev_priv->dpio_lock);
  244. if (wait_for(I915_READ(PIPECONF(PIPE_A)) & PIPECONF_DSI_PLL_LOCKED, 20)) {
  245. DRM_ERROR("DSI PLL lock failed\n");
  246. return;
  247. }
  248. DRM_DEBUG_KMS("DSI PLL locked\n");
  249. }
  250. void vlv_disable_dsi_pll(struct intel_encoder *encoder)
  251. {
  252. struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
  253. u32 tmp;
  254. DRM_DEBUG_KMS("\n");
  255. mutex_lock(&dev_priv->dpio_lock);
  256. tmp = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  257. tmp &= ~DSI_PLL_VCO_EN;
  258. tmp |= DSI_PLL_LDO_GATE;
  259. vlv_cck_write(dev_priv, CCK_REG_DSI_PLL_CONTROL, tmp);
  260. mutex_unlock(&dev_priv->dpio_lock);
  261. }