mdp5_cmd_encoder.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "mdp5_kms.h"
  14. #include "drm_crtc.h"
  15. #include "drm_crtc_helper.h"
  16. struct mdp5_cmd_encoder {
  17. struct drm_encoder base;
  18. struct mdp5_interface intf;
  19. bool enabled;
  20. uint32_t bsc;
  21. struct mdp5_ctl *ctl;
  22. };
  23. #define to_mdp5_cmd_encoder(x) container_of(x, struct mdp5_cmd_encoder, base)
  24. static struct mdp5_kms *get_kms(struct drm_encoder *encoder)
  25. {
  26. struct msm_drm_private *priv = encoder->dev->dev_private;
  27. return to_mdp5_kms(to_mdp_kms(priv->kms));
  28. }
  29. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  30. #include <mach/board.h>
  31. #include <linux/msm-bus.h>
  32. #include <linux/msm-bus-board.h>
  33. #define MDP_BUS_VECTOR_ENTRY(ab_val, ib_val) \
  34. { \
  35. .src = MSM_BUS_MASTER_MDP_PORT0, \
  36. .dst = MSM_BUS_SLAVE_EBI_CH0, \
  37. .ab = (ab_val), \
  38. .ib = (ib_val), \
  39. }
  40. static struct msm_bus_vectors mdp_bus_vectors[] = {
  41. MDP_BUS_VECTOR_ENTRY(0, 0),
  42. MDP_BUS_VECTOR_ENTRY(2000000000, 2000000000),
  43. };
  44. static struct msm_bus_paths mdp_bus_usecases[] = { {
  45. .num_paths = 1,
  46. .vectors = &mdp_bus_vectors[0],
  47. }, {
  48. .num_paths = 1,
  49. .vectors = &mdp_bus_vectors[1],
  50. } };
  51. static struct msm_bus_scale_pdata mdp_bus_scale_table = {
  52. .usecase = mdp_bus_usecases,
  53. .num_usecases = ARRAY_SIZE(mdp_bus_usecases),
  54. .name = "mdss_mdp",
  55. };
  56. static void bs_init(struct mdp5_cmd_encoder *mdp5_cmd_enc)
  57. {
  58. mdp5_cmd_enc->bsc = msm_bus_scale_register_client(
  59. &mdp_bus_scale_table);
  60. DBG("bus scale client: %08x", mdp5_cmd_enc->bsc);
  61. }
  62. static void bs_fini(struct mdp5_cmd_encoder *mdp5_cmd_enc)
  63. {
  64. if (mdp5_cmd_enc->bsc) {
  65. msm_bus_scale_unregister_client(mdp5_cmd_enc->bsc);
  66. mdp5_cmd_enc->bsc = 0;
  67. }
  68. }
  69. static void bs_set(struct mdp5_cmd_encoder *mdp5_cmd_enc, int idx)
  70. {
  71. if (mdp5_cmd_enc->bsc) {
  72. DBG("set bus scaling: %d", idx);
  73. /* HACK: scaling down, and then immediately back up
  74. * seems to leave things broken (underflow).. so
  75. * never disable:
  76. */
  77. idx = 1;
  78. msm_bus_scale_client_update_request(mdp5_cmd_enc->bsc, idx);
  79. }
  80. }
  81. #else
  82. static void bs_init(struct mdp5_cmd_encoder *mdp5_cmd_enc) {}
  83. static void bs_fini(struct mdp5_cmd_encoder *mdp5_cmd_enc) {}
  84. static void bs_set(struct mdp5_cmd_encoder *mdp5_cmd_enc, int idx) {}
  85. #endif
  86. #define VSYNC_CLK_RATE 19200000
  87. static int pingpong_tearcheck_setup(struct drm_encoder *encoder,
  88. struct drm_display_mode *mode)
  89. {
  90. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  91. struct device *dev = encoder->dev->dev;
  92. u32 total_lines_x100, vclks_line, cfg;
  93. long vsync_clk_speed;
  94. int pp_id = GET_PING_PONG_ID(mdp5_crtc_get_lm(encoder->crtc));
  95. if (IS_ERR_OR_NULL(mdp5_kms->vsync_clk)) {
  96. dev_err(dev, "vsync_clk is not initialized\n");
  97. return -EINVAL;
  98. }
  99. total_lines_x100 = mode->vtotal * mode->vrefresh;
  100. if (!total_lines_x100) {
  101. dev_err(dev, "%s: vtotal(%d) or vrefresh(%d) is 0\n",
  102. __func__, mode->vtotal, mode->vrefresh);
  103. return -EINVAL;
  104. }
  105. vsync_clk_speed = clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE);
  106. if (vsync_clk_speed <= 0) {
  107. dev_err(dev, "vsync_clk round rate failed %ld\n",
  108. vsync_clk_speed);
  109. return -EINVAL;
  110. }
  111. vclks_line = vsync_clk_speed * 100 / total_lines_x100;
  112. cfg = MDP5_PP_SYNC_CONFIG_VSYNC_COUNTER_EN
  113. | MDP5_PP_SYNC_CONFIG_VSYNC_IN_EN;
  114. cfg |= MDP5_PP_SYNC_CONFIG_VSYNC_COUNT(vclks_line);
  115. mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_CONFIG_VSYNC(pp_id), cfg);
  116. mdp5_write(mdp5_kms,
  117. REG_MDP5_PP_SYNC_CONFIG_HEIGHT(pp_id), 0xfff0);
  118. mdp5_write(mdp5_kms,
  119. REG_MDP5_PP_VSYNC_INIT_VAL(pp_id), mode->vdisplay);
  120. mdp5_write(mdp5_kms, REG_MDP5_PP_RD_PTR_IRQ(pp_id), mode->vdisplay + 1);
  121. mdp5_write(mdp5_kms, REG_MDP5_PP_START_POS(pp_id), mode->vdisplay);
  122. mdp5_write(mdp5_kms, REG_MDP5_PP_SYNC_THRESH(pp_id),
  123. MDP5_PP_SYNC_THRESH_START(4) |
  124. MDP5_PP_SYNC_THRESH_CONTINUE(4));
  125. return 0;
  126. }
  127. static int pingpong_tearcheck_enable(struct drm_encoder *encoder)
  128. {
  129. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  130. int pp_id = GET_PING_PONG_ID(mdp5_crtc_get_lm(encoder->crtc));
  131. int ret;
  132. ret = clk_set_rate(mdp5_kms->vsync_clk,
  133. clk_round_rate(mdp5_kms->vsync_clk, VSYNC_CLK_RATE));
  134. if (ret) {
  135. dev_err(encoder->dev->dev,
  136. "vsync_clk clk_set_rate failed, %d\n", ret);
  137. return ret;
  138. }
  139. ret = clk_prepare_enable(mdp5_kms->vsync_clk);
  140. if (ret) {
  141. dev_err(encoder->dev->dev,
  142. "vsync_clk clk_prepare_enable failed, %d\n", ret);
  143. return ret;
  144. }
  145. mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 1);
  146. return 0;
  147. }
  148. static void pingpong_tearcheck_disable(struct drm_encoder *encoder)
  149. {
  150. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  151. int pp_id = GET_PING_PONG_ID(mdp5_crtc_get_lm(encoder->crtc));
  152. mdp5_write(mdp5_kms, REG_MDP5_PP_TEAR_CHECK_EN(pp_id), 0);
  153. clk_disable_unprepare(mdp5_kms->vsync_clk);
  154. }
  155. static void mdp5_cmd_encoder_destroy(struct drm_encoder *encoder)
  156. {
  157. struct mdp5_cmd_encoder *mdp5_cmd_enc = to_mdp5_cmd_encoder(encoder);
  158. bs_fini(mdp5_cmd_enc);
  159. drm_encoder_cleanup(encoder);
  160. kfree(mdp5_cmd_enc);
  161. }
  162. static const struct drm_encoder_funcs mdp5_cmd_encoder_funcs = {
  163. .destroy = mdp5_cmd_encoder_destroy,
  164. };
  165. static bool mdp5_cmd_encoder_mode_fixup(struct drm_encoder *encoder,
  166. const struct drm_display_mode *mode,
  167. struct drm_display_mode *adjusted_mode)
  168. {
  169. return true;
  170. }
  171. static void mdp5_cmd_encoder_mode_set(struct drm_encoder *encoder,
  172. struct drm_display_mode *mode,
  173. struct drm_display_mode *adjusted_mode)
  174. {
  175. struct mdp5_cmd_encoder *mdp5_cmd_enc = to_mdp5_cmd_encoder(encoder);
  176. mode = adjusted_mode;
  177. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  178. mode->base.id, mode->name,
  179. mode->vrefresh, mode->clock,
  180. mode->hdisplay, mode->hsync_start,
  181. mode->hsync_end, mode->htotal,
  182. mode->vdisplay, mode->vsync_start,
  183. mode->vsync_end, mode->vtotal,
  184. mode->type, mode->flags);
  185. pingpong_tearcheck_setup(encoder, mode);
  186. mdp5_crtc_set_pipeline(encoder->crtc, &mdp5_cmd_enc->intf,
  187. mdp5_cmd_enc->ctl);
  188. }
  189. static void mdp5_cmd_encoder_disable(struct drm_encoder *encoder)
  190. {
  191. struct mdp5_cmd_encoder *mdp5_cmd_enc = to_mdp5_cmd_encoder(encoder);
  192. struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
  193. struct mdp5_interface *intf = &mdp5_cmd_enc->intf;
  194. if (WARN_ON(!mdp5_cmd_enc->enabled))
  195. return;
  196. pingpong_tearcheck_disable(encoder);
  197. mdp5_ctl_set_encoder_state(ctl, false);
  198. mdp5_ctl_commit(ctl, mdp_ctl_flush_mask_encoder(intf));
  199. bs_set(mdp5_cmd_enc, 0);
  200. mdp5_cmd_enc->enabled = false;
  201. }
  202. static void mdp5_cmd_encoder_enable(struct drm_encoder *encoder)
  203. {
  204. struct mdp5_cmd_encoder *mdp5_cmd_enc = to_mdp5_cmd_encoder(encoder);
  205. struct mdp5_ctl *ctl = mdp5_cmd_enc->ctl;
  206. struct mdp5_interface *intf = &mdp5_cmd_enc->intf;
  207. if (WARN_ON(mdp5_cmd_enc->enabled))
  208. return;
  209. bs_set(mdp5_cmd_enc, 1);
  210. if (pingpong_tearcheck_enable(encoder))
  211. return;
  212. mdp5_ctl_commit(ctl, mdp_ctl_flush_mask_encoder(intf));
  213. mdp5_ctl_set_encoder_state(ctl, true);
  214. mdp5_cmd_enc->enabled = true;
  215. }
  216. static const struct drm_encoder_helper_funcs mdp5_cmd_encoder_helper_funcs = {
  217. .mode_fixup = mdp5_cmd_encoder_mode_fixup,
  218. .mode_set = mdp5_cmd_encoder_mode_set,
  219. .disable = mdp5_cmd_encoder_disable,
  220. .enable = mdp5_cmd_encoder_enable,
  221. };
  222. int mdp5_cmd_encoder_set_split_display(struct drm_encoder *encoder,
  223. struct drm_encoder *slave_encoder)
  224. {
  225. struct mdp5_cmd_encoder *mdp5_cmd_enc = to_mdp5_cmd_encoder(encoder);
  226. struct mdp5_kms *mdp5_kms;
  227. int intf_num;
  228. u32 data = 0;
  229. if (!encoder || !slave_encoder)
  230. return -EINVAL;
  231. mdp5_kms = get_kms(encoder);
  232. intf_num = mdp5_cmd_enc->intf.num;
  233. /* Switch slave encoder's trigger MUX, to use the master's
  234. * start signal for the slave encoder
  235. */
  236. if (intf_num == 1)
  237. data |= MDP5_MDP_SPLIT_DPL_UPPER_INTF2_SW_TRG_MUX;
  238. else if (intf_num == 2)
  239. data |= MDP5_MDP_SPLIT_DPL_UPPER_INTF1_SW_TRG_MUX;
  240. else
  241. return -EINVAL;
  242. /* Smart Panel, Sync mode */
  243. data |= MDP5_MDP_SPLIT_DPL_UPPER_SMART_PANEL;
  244. /* Make sure clocks are on when connectors calling this function. */
  245. mdp5_enable(mdp5_kms);
  246. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_UPPER(0), data);
  247. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_LOWER(0),
  248. MDP5_MDP_SPLIT_DPL_LOWER_SMART_PANEL);
  249. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_EN(0), 1);
  250. mdp5_disable(mdp5_kms);
  251. return 0;
  252. }
  253. /* initialize command mode encoder */
  254. struct drm_encoder *mdp5_cmd_encoder_init(struct drm_device *dev,
  255. struct mdp5_interface *intf, struct mdp5_ctl *ctl)
  256. {
  257. struct drm_encoder *encoder = NULL;
  258. struct mdp5_cmd_encoder *mdp5_cmd_enc;
  259. int ret;
  260. if (WARN_ON((intf->type != INTF_DSI) &&
  261. (intf->mode != MDP5_INTF_DSI_MODE_COMMAND))) {
  262. ret = -EINVAL;
  263. goto fail;
  264. }
  265. mdp5_cmd_enc = kzalloc(sizeof(*mdp5_cmd_enc), GFP_KERNEL);
  266. if (!mdp5_cmd_enc) {
  267. ret = -ENOMEM;
  268. goto fail;
  269. }
  270. memcpy(&mdp5_cmd_enc->intf, intf, sizeof(mdp5_cmd_enc->intf));
  271. encoder = &mdp5_cmd_enc->base;
  272. mdp5_cmd_enc->ctl = ctl;
  273. drm_encoder_init(dev, encoder, &mdp5_cmd_encoder_funcs,
  274. DRM_MODE_ENCODER_DSI);
  275. drm_encoder_helper_add(encoder, &mdp5_cmd_encoder_helper_funcs);
  276. bs_init(mdp5_cmd_enc);
  277. return encoder;
  278. fail:
  279. if (encoder)
  280. mdp5_cmd_encoder_destroy(encoder);
  281. return ERR_PTR(ret);
  282. }