mdp5_encoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mdp5_kms.h"
  19. #include "drm_crtc.h"
  20. #include "drm_crtc_helper.h"
  21. struct mdp5_encoder {
  22. struct drm_encoder base;
  23. struct mdp5_interface intf;
  24. spinlock_t intf_lock; /* protect REG_MDP5_INTF_* registers */
  25. bool enabled;
  26. uint32_t bsc;
  27. struct mdp5_ctl *ctl;
  28. };
  29. #define to_mdp5_encoder(x) container_of(x, struct mdp5_encoder, base)
  30. static struct mdp5_kms *get_kms(struct drm_encoder *encoder)
  31. {
  32. struct msm_drm_private *priv = encoder->dev->dev_private;
  33. return to_mdp5_kms(to_mdp_kms(priv->kms));
  34. }
  35. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  36. #include <mach/board.h>
  37. #include <mach/msm_bus.h>
  38. #include <mach/msm_bus_board.h>
  39. #define MDP_BUS_VECTOR_ENTRY(ab_val, ib_val) \
  40. { \
  41. .src = MSM_BUS_MASTER_MDP_PORT0, \
  42. .dst = MSM_BUS_SLAVE_EBI_CH0, \
  43. .ab = (ab_val), \
  44. .ib = (ib_val), \
  45. }
  46. static struct msm_bus_vectors mdp_bus_vectors[] = {
  47. MDP_BUS_VECTOR_ENTRY(0, 0),
  48. MDP_BUS_VECTOR_ENTRY(2000000000, 2000000000),
  49. };
  50. static struct msm_bus_paths mdp_bus_usecases[] = { {
  51. .num_paths = 1,
  52. .vectors = &mdp_bus_vectors[0],
  53. }, {
  54. .num_paths = 1,
  55. .vectors = &mdp_bus_vectors[1],
  56. } };
  57. static struct msm_bus_scale_pdata mdp_bus_scale_table = {
  58. .usecase = mdp_bus_usecases,
  59. .num_usecases = ARRAY_SIZE(mdp_bus_usecases),
  60. .name = "mdss_mdp",
  61. };
  62. static void bs_init(struct mdp5_encoder *mdp5_encoder)
  63. {
  64. mdp5_encoder->bsc = msm_bus_scale_register_client(
  65. &mdp_bus_scale_table);
  66. DBG("bus scale client: %08x", mdp5_encoder->bsc);
  67. }
  68. static void bs_fini(struct mdp5_encoder *mdp5_encoder)
  69. {
  70. if (mdp5_encoder->bsc) {
  71. msm_bus_scale_unregister_client(mdp5_encoder->bsc);
  72. mdp5_encoder->bsc = 0;
  73. }
  74. }
  75. static void bs_set(struct mdp5_encoder *mdp5_encoder, int idx)
  76. {
  77. if (mdp5_encoder->bsc) {
  78. DBG("set bus scaling: %d", idx);
  79. /* HACK: scaling down, and then immediately back up
  80. * seems to leave things broken (underflow).. so
  81. * never disable:
  82. */
  83. idx = 1;
  84. msm_bus_scale_client_update_request(mdp5_encoder->bsc, idx);
  85. }
  86. }
  87. #else
  88. static void bs_init(struct mdp5_encoder *mdp5_encoder) {}
  89. static void bs_fini(struct mdp5_encoder *mdp5_encoder) {}
  90. static void bs_set(struct mdp5_encoder *mdp5_encoder, int idx) {}
  91. #endif
  92. static void mdp5_encoder_destroy(struct drm_encoder *encoder)
  93. {
  94. struct mdp5_encoder *mdp5_encoder = to_mdp5_encoder(encoder);
  95. bs_fini(mdp5_encoder);
  96. drm_encoder_cleanup(encoder);
  97. kfree(mdp5_encoder);
  98. }
  99. static const struct drm_encoder_funcs mdp5_encoder_funcs = {
  100. .destroy = mdp5_encoder_destroy,
  101. };
  102. static bool mdp5_encoder_mode_fixup(struct drm_encoder *encoder,
  103. const struct drm_display_mode *mode,
  104. struct drm_display_mode *adjusted_mode)
  105. {
  106. return true;
  107. }
  108. static void mdp5_encoder_mode_set(struct drm_encoder *encoder,
  109. struct drm_display_mode *mode,
  110. struct drm_display_mode *adjusted_mode)
  111. {
  112. struct mdp5_encoder *mdp5_encoder = to_mdp5_encoder(encoder);
  113. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  114. struct drm_device *dev = encoder->dev;
  115. struct drm_connector *connector;
  116. int intf = mdp5_encoder->intf.num;
  117. uint32_t dtv_hsync_skew, vsync_period, vsync_len, ctrl_pol;
  118. uint32_t display_v_start, display_v_end;
  119. uint32_t hsync_start_x, hsync_end_x;
  120. uint32_t format = 0x2100;
  121. unsigned long flags;
  122. mode = adjusted_mode;
  123. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  124. mode->base.id, mode->name,
  125. mode->vrefresh, mode->clock,
  126. mode->hdisplay, mode->hsync_start,
  127. mode->hsync_end, mode->htotal,
  128. mode->vdisplay, mode->vsync_start,
  129. mode->vsync_end, mode->vtotal,
  130. mode->type, mode->flags);
  131. ctrl_pol = 0;
  132. /* DSI controller cannot handle active-low sync signals. */
  133. if (mdp5_encoder->intf.type != INTF_DSI) {
  134. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  135. ctrl_pol |= MDP5_INTF_POLARITY_CTL_HSYNC_LOW;
  136. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  137. ctrl_pol |= MDP5_INTF_POLARITY_CTL_VSYNC_LOW;
  138. }
  139. /* probably need to get DATA_EN polarity from panel.. */
  140. dtv_hsync_skew = 0; /* get this from panel? */
  141. /* Get color format from panel, default is 8bpc */
  142. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  143. if (connector->encoder == encoder) {
  144. switch (connector->display_info.bpc) {
  145. case 4:
  146. format |= 0;
  147. break;
  148. case 5:
  149. format |= 0x15;
  150. break;
  151. case 6:
  152. format |= 0x2A;
  153. break;
  154. case 8:
  155. default:
  156. format |= 0x3F;
  157. break;
  158. }
  159. break;
  160. }
  161. }
  162. hsync_start_x = (mode->htotal - mode->hsync_start);
  163. hsync_end_x = mode->htotal - (mode->hsync_start - mode->hdisplay) - 1;
  164. vsync_period = mode->vtotal * mode->htotal;
  165. vsync_len = (mode->vsync_end - mode->vsync_start) * mode->htotal;
  166. display_v_start = (mode->vtotal - mode->vsync_start) * mode->htotal + dtv_hsync_skew;
  167. display_v_end = vsync_period - ((mode->vsync_start - mode->vdisplay) * mode->htotal) + dtv_hsync_skew - 1;
  168. /*
  169. * For edp only:
  170. * DISPLAY_V_START = (VBP * HCYCLE) + HBP
  171. * DISPLAY_V_END = (VBP + VACTIVE) * HCYCLE - 1 - HFP
  172. */
  173. if (mdp5_encoder->intf.type == INTF_eDP) {
  174. display_v_start += mode->htotal - mode->hsync_start;
  175. display_v_end -= mode->hsync_start - mode->hdisplay;
  176. }
  177. spin_lock_irqsave(&mdp5_encoder->intf_lock, flags);
  178. mdp5_write(mdp5_kms, REG_MDP5_INTF_HSYNC_CTL(intf),
  179. MDP5_INTF_HSYNC_CTL_PULSEW(mode->hsync_end - mode->hsync_start) |
  180. MDP5_INTF_HSYNC_CTL_PERIOD(mode->htotal));
  181. mdp5_write(mdp5_kms, REG_MDP5_INTF_VSYNC_PERIOD_F0(intf), vsync_period);
  182. mdp5_write(mdp5_kms, REG_MDP5_INTF_VSYNC_LEN_F0(intf), vsync_len);
  183. mdp5_write(mdp5_kms, REG_MDP5_INTF_DISPLAY_HCTL(intf),
  184. MDP5_INTF_DISPLAY_HCTL_START(hsync_start_x) |
  185. MDP5_INTF_DISPLAY_HCTL_END(hsync_end_x));
  186. mdp5_write(mdp5_kms, REG_MDP5_INTF_DISPLAY_VSTART_F0(intf), display_v_start);
  187. mdp5_write(mdp5_kms, REG_MDP5_INTF_DISPLAY_VEND_F0(intf), display_v_end);
  188. mdp5_write(mdp5_kms, REG_MDP5_INTF_BORDER_COLOR(intf), 0);
  189. mdp5_write(mdp5_kms, REG_MDP5_INTF_UNDERFLOW_COLOR(intf), 0xff);
  190. mdp5_write(mdp5_kms, REG_MDP5_INTF_HSYNC_SKEW(intf), dtv_hsync_skew);
  191. mdp5_write(mdp5_kms, REG_MDP5_INTF_POLARITY_CTL(intf), ctrl_pol);
  192. mdp5_write(mdp5_kms, REG_MDP5_INTF_ACTIVE_HCTL(intf),
  193. MDP5_INTF_ACTIVE_HCTL_START(0) |
  194. MDP5_INTF_ACTIVE_HCTL_END(0));
  195. mdp5_write(mdp5_kms, REG_MDP5_INTF_ACTIVE_VSTART_F0(intf), 0);
  196. mdp5_write(mdp5_kms, REG_MDP5_INTF_ACTIVE_VEND_F0(intf), 0);
  197. mdp5_write(mdp5_kms, REG_MDP5_INTF_PANEL_FORMAT(intf), format);
  198. mdp5_write(mdp5_kms, REG_MDP5_INTF_FRAME_LINE_COUNT_EN(intf), 0x3); /* frame+line? */
  199. spin_unlock_irqrestore(&mdp5_encoder->intf_lock, flags);
  200. mdp5_crtc_set_pipeline(encoder->crtc, &mdp5_encoder->intf,
  201. mdp5_encoder->ctl);
  202. }
  203. static void mdp5_encoder_disable(struct drm_encoder *encoder)
  204. {
  205. struct mdp5_encoder *mdp5_encoder = to_mdp5_encoder(encoder);
  206. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  207. struct mdp5_ctl *ctl = mdp5_encoder->ctl;
  208. int lm = mdp5_crtc_get_lm(encoder->crtc);
  209. struct mdp5_interface *intf = &mdp5_encoder->intf;
  210. int intfn = mdp5_encoder->intf.num;
  211. unsigned long flags;
  212. if (WARN_ON(!mdp5_encoder->enabled))
  213. return;
  214. mdp5_ctl_set_encoder_state(ctl, false);
  215. spin_lock_irqsave(&mdp5_encoder->intf_lock, flags);
  216. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(intfn), 0);
  217. spin_unlock_irqrestore(&mdp5_encoder->intf_lock, flags);
  218. mdp5_ctl_commit(ctl, mdp_ctl_flush_mask_encoder(intf));
  219. /*
  220. * Wait for a vsync so we know the ENABLE=0 latched before
  221. * the (connector) source of the vsync's gets disabled,
  222. * otherwise we end up in a funny state if we re-enable
  223. * before the disable latches, which results that some of
  224. * the settings changes for the new modeset (like new
  225. * scanout buffer) don't latch properly..
  226. */
  227. mdp_irq_wait(&mdp5_kms->base, intf2vblank(lm, intf));
  228. bs_set(mdp5_encoder, 0);
  229. mdp5_encoder->enabled = false;
  230. }
  231. static void mdp5_encoder_enable(struct drm_encoder *encoder)
  232. {
  233. struct mdp5_encoder *mdp5_encoder = to_mdp5_encoder(encoder);
  234. struct mdp5_kms *mdp5_kms = get_kms(encoder);
  235. struct mdp5_ctl *ctl = mdp5_encoder->ctl;
  236. struct mdp5_interface *intf = &mdp5_encoder->intf;
  237. int intfn = mdp5_encoder->intf.num;
  238. unsigned long flags;
  239. if (WARN_ON(mdp5_encoder->enabled))
  240. return;
  241. bs_set(mdp5_encoder, 1);
  242. spin_lock_irqsave(&mdp5_encoder->intf_lock, flags);
  243. mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(intfn), 1);
  244. spin_unlock_irqrestore(&mdp5_encoder->intf_lock, flags);
  245. mdp5_ctl_commit(ctl, mdp_ctl_flush_mask_encoder(intf));
  246. mdp5_ctl_set_encoder_state(ctl, true);
  247. mdp5_encoder->enabled = true;
  248. }
  249. static const struct drm_encoder_helper_funcs mdp5_encoder_helper_funcs = {
  250. .mode_fixup = mdp5_encoder_mode_fixup,
  251. .mode_set = mdp5_encoder_mode_set,
  252. .disable = mdp5_encoder_disable,
  253. .enable = mdp5_encoder_enable,
  254. };
  255. int mdp5_encoder_set_split_display(struct drm_encoder *encoder,
  256. struct drm_encoder *slave_encoder)
  257. {
  258. struct mdp5_encoder *mdp5_encoder = to_mdp5_encoder(encoder);
  259. struct mdp5_encoder *mdp5_slave_enc = to_mdp5_encoder(slave_encoder);
  260. struct mdp5_kms *mdp5_kms;
  261. int intf_num;
  262. u32 data = 0;
  263. if (!encoder || !slave_encoder)
  264. return -EINVAL;
  265. mdp5_kms = get_kms(encoder);
  266. intf_num = mdp5_encoder->intf.num;
  267. /* Switch slave encoder's TimingGen Sync mode,
  268. * to use the master's enable signal for the slave encoder.
  269. */
  270. if (intf_num == 1)
  271. data |= MDP5_MDP_SPLIT_DPL_LOWER_INTF2_TG_SYNC;
  272. else if (intf_num == 2)
  273. data |= MDP5_MDP_SPLIT_DPL_LOWER_INTF1_TG_SYNC;
  274. else
  275. return -EINVAL;
  276. /* Make sure clocks are on when connectors calling this function. */
  277. mdp5_enable(mdp5_kms);
  278. /* Dumb Panel, Sync mode */
  279. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_UPPER(0), 0);
  280. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_LOWER(0), data);
  281. mdp5_write(mdp5_kms, REG_MDP5_MDP_SPLIT_DPL_EN(0), 1);
  282. mdp5_ctl_pair(mdp5_encoder->ctl, mdp5_slave_enc->ctl, true);
  283. mdp5_disable(mdp5_kms);
  284. return 0;
  285. }
  286. /* initialize encoder */
  287. struct drm_encoder *mdp5_encoder_init(struct drm_device *dev,
  288. struct mdp5_interface *intf, struct mdp5_ctl *ctl)
  289. {
  290. struct drm_encoder *encoder = NULL;
  291. struct mdp5_encoder *mdp5_encoder;
  292. int enc_type = (intf->type == INTF_DSI) ?
  293. DRM_MODE_ENCODER_DSI : DRM_MODE_ENCODER_TMDS;
  294. int ret;
  295. mdp5_encoder = kzalloc(sizeof(*mdp5_encoder), GFP_KERNEL);
  296. if (!mdp5_encoder) {
  297. ret = -ENOMEM;
  298. goto fail;
  299. }
  300. memcpy(&mdp5_encoder->intf, intf, sizeof(mdp5_encoder->intf));
  301. encoder = &mdp5_encoder->base;
  302. mdp5_encoder->ctl = ctl;
  303. spin_lock_init(&mdp5_encoder->intf_lock);
  304. drm_encoder_init(dev, encoder, &mdp5_encoder_funcs, enc_type);
  305. drm_encoder_helper_add(encoder, &mdp5_encoder_helper_funcs);
  306. bs_init(mdp5_encoder);
  307. return encoder;
  308. fail:
  309. if (encoder)
  310. mdp5_encoder_destroy(encoder);
  311. return ERR_PTR(ret);
  312. }