mdp4_dtv_encoder.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "mdp4_kms.h"
  18. #include "drm_crtc.h"
  19. #include "drm_crtc_helper.h"
  20. struct mdp4_dtv_encoder {
  21. struct drm_encoder base;
  22. struct clk *src_clk;
  23. struct clk *hdmi_clk;
  24. struct clk *mdp_clk;
  25. unsigned long int pixclock;
  26. bool enabled;
  27. uint32_t bsc;
  28. };
  29. #define to_mdp4_dtv_encoder(x) container_of(x, struct mdp4_dtv_encoder, base)
  30. static struct mdp4_kms *get_kms(struct drm_encoder *encoder)
  31. {
  32. struct msm_drm_private *priv = encoder->dev->dev_private;
  33. return to_mdp4_kms(to_mdp_kms(priv->kms));
  34. }
  35. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  36. #include <mach/board.h>
  37. /* not ironically named at all.. no, really.. */
  38. static void bs_init(struct mdp4_dtv_encoder *mdp4_dtv_encoder)
  39. {
  40. struct drm_device *dev = mdp4_dtv_encoder->base.dev;
  41. struct lcdc_platform_data *dtv_pdata = mdp4_find_pdata("dtv.0");
  42. if (!dtv_pdata) {
  43. dev_err(dev->dev, "could not find dtv pdata\n");
  44. return;
  45. }
  46. if (dtv_pdata->bus_scale_table) {
  47. mdp4_dtv_encoder->bsc = msm_bus_scale_register_client(
  48. dtv_pdata->bus_scale_table);
  49. DBG("bus scale client: %08x", mdp4_dtv_encoder->bsc);
  50. DBG("lcdc_power_save: %p", dtv_pdata->lcdc_power_save);
  51. if (dtv_pdata->lcdc_power_save)
  52. dtv_pdata->lcdc_power_save(1);
  53. }
  54. }
  55. static void bs_fini(struct mdp4_dtv_encoder *mdp4_dtv_encoder)
  56. {
  57. if (mdp4_dtv_encoder->bsc) {
  58. msm_bus_scale_unregister_client(mdp4_dtv_encoder->bsc);
  59. mdp4_dtv_encoder->bsc = 0;
  60. }
  61. }
  62. static void bs_set(struct mdp4_dtv_encoder *mdp4_dtv_encoder, int idx)
  63. {
  64. if (mdp4_dtv_encoder->bsc) {
  65. DBG("set bus scaling: %d", idx);
  66. msm_bus_scale_client_update_request(mdp4_dtv_encoder->bsc, idx);
  67. }
  68. }
  69. #else
  70. static void bs_init(struct mdp4_dtv_encoder *mdp4_dtv_encoder) {}
  71. static void bs_fini(struct mdp4_dtv_encoder *mdp4_dtv_encoder) {}
  72. static void bs_set(struct mdp4_dtv_encoder *mdp4_dtv_encoder, int idx) {}
  73. #endif
  74. static void mdp4_dtv_encoder_destroy(struct drm_encoder *encoder)
  75. {
  76. struct mdp4_dtv_encoder *mdp4_dtv_encoder = to_mdp4_dtv_encoder(encoder);
  77. bs_fini(mdp4_dtv_encoder);
  78. drm_encoder_cleanup(encoder);
  79. kfree(mdp4_dtv_encoder);
  80. }
  81. static const struct drm_encoder_funcs mdp4_dtv_encoder_funcs = {
  82. .destroy = mdp4_dtv_encoder_destroy,
  83. };
  84. static bool mdp4_dtv_encoder_mode_fixup(struct drm_encoder *encoder,
  85. const struct drm_display_mode *mode,
  86. struct drm_display_mode *adjusted_mode)
  87. {
  88. return true;
  89. }
  90. static void mdp4_dtv_encoder_mode_set(struct drm_encoder *encoder,
  91. struct drm_display_mode *mode,
  92. struct drm_display_mode *adjusted_mode)
  93. {
  94. struct mdp4_dtv_encoder *mdp4_dtv_encoder = to_mdp4_dtv_encoder(encoder);
  95. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  96. uint32_t dtv_hsync_skew, vsync_period, vsync_len, ctrl_pol;
  97. uint32_t display_v_start, display_v_end;
  98. uint32_t hsync_start_x, hsync_end_x;
  99. mode = adjusted_mode;
  100. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  101. mode->base.id, mode->name,
  102. mode->vrefresh, mode->clock,
  103. mode->hdisplay, mode->hsync_start,
  104. mode->hsync_end, mode->htotal,
  105. mode->vdisplay, mode->vsync_start,
  106. mode->vsync_end, mode->vtotal,
  107. mode->type, mode->flags);
  108. mdp4_dtv_encoder->pixclock = mode->clock * 1000;
  109. DBG("pixclock=%lu", mdp4_dtv_encoder->pixclock);
  110. ctrl_pol = 0;
  111. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  112. ctrl_pol |= MDP4_DTV_CTRL_POLARITY_HSYNC_LOW;
  113. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  114. ctrl_pol |= MDP4_DTV_CTRL_POLARITY_VSYNC_LOW;
  115. /* probably need to get DATA_EN polarity from panel.. */
  116. dtv_hsync_skew = 0; /* get this from panel? */
  117. hsync_start_x = (mode->htotal - mode->hsync_start);
  118. hsync_end_x = mode->htotal - (mode->hsync_start - mode->hdisplay) - 1;
  119. vsync_period = mode->vtotal * mode->htotal;
  120. vsync_len = (mode->vsync_end - mode->vsync_start) * mode->htotal;
  121. display_v_start = (mode->vtotal - mode->vsync_start) * mode->htotal + dtv_hsync_skew;
  122. display_v_end = vsync_period - ((mode->vsync_start - mode->vdisplay) * mode->htotal) + dtv_hsync_skew - 1;
  123. mdp4_write(mdp4_kms, REG_MDP4_DTV_HSYNC_CTRL,
  124. MDP4_DTV_HSYNC_CTRL_PULSEW(mode->hsync_end - mode->hsync_start) |
  125. MDP4_DTV_HSYNC_CTRL_PERIOD(mode->htotal));
  126. mdp4_write(mdp4_kms, REG_MDP4_DTV_VSYNC_PERIOD, vsync_period);
  127. mdp4_write(mdp4_kms, REG_MDP4_DTV_VSYNC_LEN, vsync_len);
  128. mdp4_write(mdp4_kms, REG_MDP4_DTV_DISPLAY_HCTRL,
  129. MDP4_DTV_DISPLAY_HCTRL_START(hsync_start_x) |
  130. MDP4_DTV_DISPLAY_HCTRL_END(hsync_end_x));
  131. mdp4_write(mdp4_kms, REG_MDP4_DTV_DISPLAY_VSTART, display_v_start);
  132. mdp4_write(mdp4_kms, REG_MDP4_DTV_DISPLAY_VEND, display_v_end);
  133. mdp4_write(mdp4_kms, REG_MDP4_DTV_BORDER_CLR, 0);
  134. mdp4_write(mdp4_kms, REG_MDP4_DTV_UNDERFLOW_CLR,
  135. MDP4_DTV_UNDERFLOW_CLR_ENABLE_RECOVERY |
  136. MDP4_DTV_UNDERFLOW_CLR_COLOR(0xff));
  137. mdp4_write(mdp4_kms, REG_MDP4_DTV_HSYNC_SKEW, dtv_hsync_skew);
  138. mdp4_write(mdp4_kms, REG_MDP4_DTV_CTRL_POLARITY, ctrl_pol);
  139. mdp4_write(mdp4_kms, REG_MDP4_DTV_ACTIVE_HCTL,
  140. MDP4_DTV_ACTIVE_HCTL_START(0) |
  141. MDP4_DTV_ACTIVE_HCTL_END(0));
  142. mdp4_write(mdp4_kms, REG_MDP4_DTV_ACTIVE_VSTART, 0);
  143. mdp4_write(mdp4_kms, REG_MDP4_DTV_ACTIVE_VEND, 0);
  144. }
  145. static void mdp4_dtv_encoder_disable(struct drm_encoder *encoder)
  146. {
  147. struct mdp4_dtv_encoder *mdp4_dtv_encoder = to_mdp4_dtv_encoder(encoder);
  148. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  149. if (WARN_ON(!mdp4_dtv_encoder->enabled))
  150. return;
  151. mdp4_write(mdp4_kms, REG_MDP4_DTV_ENABLE, 0);
  152. /*
  153. * Wait for a vsync so we know the ENABLE=0 latched before
  154. * the (connector) source of the vsync's gets disabled,
  155. * otherwise we end up in a funny state if we re-enable
  156. * before the disable latches, which results that some of
  157. * the settings changes for the new modeset (like new
  158. * scanout buffer) don't latch properly..
  159. */
  160. mdp_irq_wait(&mdp4_kms->base, MDP4_IRQ_EXTERNAL_VSYNC);
  161. clk_disable_unprepare(mdp4_dtv_encoder->src_clk);
  162. clk_disable_unprepare(mdp4_dtv_encoder->hdmi_clk);
  163. clk_disable_unprepare(mdp4_dtv_encoder->mdp_clk);
  164. bs_set(mdp4_dtv_encoder, 0);
  165. mdp4_dtv_encoder->enabled = false;
  166. }
  167. static void mdp4_dtv_encoder_enable(struct drm_encoder *encoder)
  168. {
  169. struct drm_device *dev = encoder->dev;
  170. struct mdp4_dtv_encoder *mdp4_dtv_encoder = to_mdp4_dtv_encoder(encoder);
  171. struct mdp4_kms *mdp4_kms = get_kms(encoder);
  172. unsigned long pc = mdp4_dtv_encoder->pixclock;
  173. int ret;
  174. if (WARN_ON(mdp4_dtv_encoder->enabled))
  175. return;
  176. mdp4_crtc_set_config(encoder->crtc,
  177. MDP4_DMA_CONFIG_R_BPC(BPC8) |
  178. MDP4_DMA_CONFIG_G_BPC(BPC8) |
  179. MDP4_DMA_CONFIG_B_BPC(BPC8) |
  180. MDP4_DMA_CONFIG_PACK(0x21));
  181. mdp4_crtc_set_intf(encoder->crtc, INTF_LCDC_DTV, 1);
  182. bs_set(mdp4_dtv_encoder, 1);
  183. DBG("setting src_clk=%lu", pc);
  184. ret = clk_set_rate(mdp4_dtv_encoder->src_clk, pc);
  185. if (ret)
  186. dev_err(dev->dev, "failed to set src_clk to %lu: %d\n", pc, ret);
  187. clk_prepare_enable(mdp4_dtv_encoder->src_clk);
  188. ret = clk_prepare_enable(mdp4_dtv_encoder->hdmi_clk);
  189. if (ret)
  190. dev_err(dev->dev, "failed to enable hdmi_clk: %d\n", ret);
  191. ret = clk_prepare_enable(mdp4_dtv_encoder->mdp_clk);
  192. if (ret)
  193. dev_err(dev->dev, "failed to enabled mdp_clk: %d\n", ret);
  194. mdp4_write(mdp4_kms, REG_MDP4_DTV_ENABLE, 1);
  195. mdp4_dtv_encoder->enabled = true;
  196. }
  197. static const struct drm_encoder_helper_funcs mdp4_dtv_encoder_helper_funcs = {
  198. .mode_fixup = mdp4_dtv_encoder_mode_fixup,
  199. .mode_set = mdp4_dtv_encoder_mode_set,
  200. .enable = mdp4_dtv_encoder_enable,
  201. .disable = mdp4_dtv_encoder_disable,
  202. };
  203. long mdp4_dtv_round_pixclk(struct drm_encoder *encoder, unsigned long rate)
  204. {
  205. struct mdp4_dtv_encoder *mdp4_dtv_encoder = to_mdp4_dtv_encoder(encoder);
  206. return clk_round_rate(mdp4_dtv_encoder->src_clk, rate);
  207. }
  208. /* initialize encoder */
  209. struct drm_encoder *mdp4_dtv_encoder_init(struct drm_device *dev)
  210. {
  211. struct drm_encoder *encoder = NULL;
  212. struct mdp4_dtv_encoder *mdp4_dtv_encoder;
  213. int ret;
  214. mdp4_dtv_encoder = kzalloc(sizeof(*mdp4_dtv_encoder), GFP_KERNEL);
  215. if (!mdp4_dtv_encoder) {
  216. ret = -ENOMEM;
  217. goto fail;
  218. }
  219. encoder = &mdp4_dtv_encoder->base;
  220. drm_encoder_init(dev, encoder, &mdp4_dtv_encoder_funcs,
  221. DRM_MODE_ENCODER_TMDS);
  222. drm_encoder_helper_add(encoder, &mdp4_dtv_encoder_helper_funcs);
  223. mdp4_dtv_encoder->src_clk = devm_clk_get(dev->dev, "src_clk");
  224. if (IS_ERR(mdp4_dtv_encoder->src_clk)) {
  225. dev_err(dev->dev, "failed to get src_clk\n");
  226. ret = PTR_ERR(mdp4_dtv_encoder->src_clk);
  227. goto fail;
  228. }
  229. mdp4_dtv_encoder->hdmi_clk = devm_clk_get(dev->dev, "hdmi_clk");
  230. if (IS_ERR(mdp4_dtv_encoder->hdmi_clk)) {
  231. dev_err(dev->dev, "failed to get hdmi_clk\n");
  232. ret = PTR_ERR(mdp4_dtv_encoder->hdmi_clk);
  233. goto fail;
  234. }
  235. mdp4_dtv_encoder->mdp_clk = devm_clk_get(dev->dev, "mdp_clk");
  236. if (IS_ERR(mdp4_dtv_encoder->mdp_clk)) {
  237. dev_err(dev->dev, "failed to get mdp_clk\n");
  238. ret = PTR_ERR(mdp4_dtv_encoder->mdp_clk);
  239. goto fail;
  240. }
  241. bs_init(mdp4_dtv_encoder);
  242. return encoder;
  243. fail:
  244. if (encoder)
  245. mdp4_dtv_encoder_destroy(encoder);
  246. return ERR_PTR(ret);
  247. }