tidss_kms.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Tomi Valkeinen <[email protected]>
  5. */
  6. #include <linux/dma-fence.h>
  7. #include <drm/drm_atomic.h>
  8. #include <drm/drm_atomic_helper.h>
  9. #include <drm/drm_bridge.h>
  10. #include <drm/drm_crtc_helper.h>
  11. #include <drm/drm_fb_helper.h>
  12. #include <drm/drm_gem_framebuffer_helper.h>
  13. #include <drm/drm_of.h>
  14. #include <drm/drm_panel.h>
  15. #include <drm/drm_vblank.h>
  16. #include "tidss_crtc.h"
  17. #include "tidss_dispc.h"
  18. #include "tidss_drv.h"
  19. #include "tidss_encoder.h"
  20. #include "tidss_kms.h"
  21. #include "tidss_plane.h"
  22. static void tidss_atomic_commit_tail(struct drm_atomic_state *old_state)
  23. {
  24. struct drm_device *ddev = old_state->dev;
  25. struct tidss_device *tidss = to_tidss(ddev);
  26. bool fence_cookie = dma_fence_begin_signalling();
  27. dev_dbg(ddev->dev, "%s\n", __func__);
  28. tidss_runtime_get(tidss);
  29. drm_atomic_helper_commit_modeset_disables(ddev, old_state);
  30. drm_atomic_helper_commit_planes(ddev, old_state, 0);
  31. drm_atomic_helper_commit_modeset_enables(ddev, old_state);
  32. drm_atomic_helper_commit_hw_done(old_state);
  33. dma_fence_end_signalling(fence_cookie);
  34. drm_atomic_helper_wait_for_flip_done(ddev, old_state);
  35. drm_atomic_helper_cleanup_planes(ddev, old_state);
  36. tidss_runtime_put(tidss);
  37. }
  38. static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = {
  39. .atomic_commit_tail = tidss_atomic_commit_tail,
  40. };
  41. static int tidss_atomic_check(struct drm_device *ddev,
  42. struct drm_atomic_state *state)
  43. {
  44. struct drm_plane_state *opstate;
  45. struct drm_plane_state *npstate;
  46. struct drm_plane *plane;
  47. struct drm_crtc_state *cstate;
  48. struct drm_crtc *crtc;
  49. int ret, i;
  50. ret = drm_atomic_helper_check(ddev, state);
  51. if (ret)
  52. return ret;
  53. /*
  54. * Add all active planes on a CRTC to the atomic state, if
  55. * x/y/z position or activity of any plane on that CRTC
  56. * changes. This is needed for updating the plane positions in
  57. * tidss_crtc_position_planes() which is called from
  58. * crtc_atomic_enable() and crtc_atomic_flush(). We have an
  59. * extra flag to mark x,y-position changes and together
  60. * with zpos_changed the condition recognizes all the above
  61. * cases.
  62. */
  63. for_each_oldnew_plane_in_state(state, plane, opstate, npstate, i) {
  64. if (!npstate->crtc || !npstate->visible)
  65. continue;
  66. if (!opstate->crtc || opstate->crtc_x != npstate->crtc_x ||
  67. opstate->crtc_y != npstate->crtc_y) {
  68. cstate = drm_atomic_get_crtc_state(state,
  69. npstate->crtc);
  70. if (IS_ERR(cstate))
  71. return PTR_ERR(cstate);
  72. to_tidss_crtc_state(cstate)->plane_pos_changed = true;
  73. }
  74. }
  75. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  76. if (to_tidss_crtc_state(cstate)->plane_pos_changed ||
  77. cstate->zpos_changed) {
  78. ret = drm_atomic_add_affected_planes(state, crtc);
  79. if (ret)
  80. return ret;
  81. }
  82. }
  83. return 0;
  84. }
  85. static const struct drm_mode_config_funcs mode_config_funcs = {
  86. .fb_create = drm_gem_fb_create,
  87. .atomic_check = tidss_atomic_check,
  88. .atomic_commit = drm_atomic_helper_commit,
  89. };
  90. static int tidss_dispc_modeset_init(struct tidss_device *tidss)
  91. {
  92. struct device *dev = tidss->dev;
  93. unsigned int fourccs_len;
  94. const u32 *fourccs = dispc_plane_formats(tidss->dispc, &fourccs_len);
  95. unsigned int i;
  96. struct pipe {
  97. u32 hw_videoport;
  98. struct drm_bridge *bridge;
  99. u32 enc_type;
  100. };
  101. const struct dispc_features *feat = tidss->feat;
  102. u32 max_vps = feat->num_vps;
  103. u32 max_planes = feat->num_planes;
  104. struct pipe pipes[TIDSS_MAX_PORTS];
  105. u32 num_pipes = 0;
  106. u32 crtc_mask;
  107. /* first find all the connected panels & bridges */
  108. for (i = 0; i < max_vps; i++) {
  109. struct drm_panel *panel;
  110. struct drm_bridge *bridge;
  111. u32 enc_type = DRM_MODE_ENCODER_NONE;
  112. int ret;
  113. ret = drm_of_find_panel_or_bridge(dev->of_node, i, 0,
  114. &panel, &bridge);
  115. if (ret == -ENODEV) {
  116. dev_dbg(dev, "no panel/bridge for port %d\n", i);
  117. continue;
  118. } else if (ret) {
  119. dev_dbg(dev, "port %d probe returned %d\n", i, ret);
  120. return ret;
  121. }
  122. if (panel) {
  123. u32 conn_type;
  124. dev_dbg(dev, "Setting up panel for port %d\n", i);
  125. switch (feat->vp_bus_type[i]) {
  126. case DISPC_VP_OLDI:
  127. enc_type = DRM_MODE_ENCODER_LVDS;
  128. conn_type = DRM_MODE_CONNECTOR_LVDS;
  129. break;
  130. case DISPC_VP_DPI:
  131. enc_type = DRM_MODE_ENCODER_DPI;
  132. conn_type = DRM_MODE_CONNECTOR_DPI;
  133. break;
  134. default:
  135. WARN_ON(1);
  136. return -EINVAL;
  137. }
  138. if (panel->connector_type != conn_type) {
  139. dev_err(dev,
  140. "%s: Panel %s has incompatible connector type for vp%d (%d != %d)\n",
  141. __func__, dev_name(panel->dev), i,
  142. panel->connector_type, conn_type);
  143. return -EINVAL;
  144. }
  145. bridge = devm_drm_panel_bridge_add(dev, panel);
  146. if (IS_ERR(bridge)) {
  147. dev_err(dev,
  148. "failed to set up panel bridge for port %d\n",
  149. i);
  150. return PTR_ERR(bridge);
  151. }
  152. }
  153. pipes[num_pipes].hw_videoport = i;
  154. pipes[num_pipes].bridge = bridge;
  155. pipes[num_pipes].enc_type = enc_type;
  156. num_pipes++;
  157. }
  158. /* all planes can be on any crtc */
  159. crtc_mask = (1 << num_pipes) - 1;
  160. /* then create a plane, a crtc and an encoder for each panel/bridge */
  161. for (i = 0; i < num_pipes; ++i) {
  162. struct tidss_plane *tplane;
  163. struct tidss_crtc *tcrtc;
  164. struct drm_encoder *enc;
  165. u32 hw_plane_id = feat->vid_order[tidss->num_planes];
  166. int ret;
  167. tplane = tidss_plane_create(tidss, hw_plane_id,
  168. DRM_PLANE_TYPE_PRIMARY, crtc_mask,
  169. fourccs, fourccs_len);
  170. if (IS_ERR(tplane)) {
  171. dev_err(tidss->dev, "plane create failed\n");
  172. return PTR_ERR(tplane);
  173. }
  174. tidss->planes[tidss->num_planes++] = &tplane->plane;
  175. tcrtc = tidss_crtc_create(tidss, pipes[i].hw_videoport,
  176. &tplane->plane);
  177. if (IS_ERR(tcrtc)) {
  178. dev_err(tidss->dev, "crtc create failed\n");
  179. return PTR_ERR(tcrtc);
  180. }
  181. tidss->crtcs[tidss->num_crtcs++] = &tcrtc->crtc;
  182. enc = tidss_encoder_create(tidss, pipes[i].enc_type,
  183. 1 << tcrtc->crtc.index);
  184. if (IS_ERR(enc)) {
  185. dev_err(tidss->dev, "encoder create failed\n");
  186. return PTR_ERR(enc);
  187. }
  188. ret = drm_bridge_attach(enc, pipes[i].bridge, NULL, 0);
  189. if (ret)
  190. return ret;
  191. }
  192. /* create overlay planes of the leftover planes */
  193. while (tidss->num_planes < max_planes) {
  194. struct tidss_plane *tplane;
  195. u32 hw_plane_id = feat->vid_order[tidss->num_planes];
  196. tplane = tidss_plane_create(tidss, hw_plane_id,
  197. DRM_PLANE_TYPE_OVERLAY, crtc_mask,
  198. fourccs, fourccs_len);
  199. if (IS_ERR(tplane)) {
  200. dev_err(tidss->dev, "plane create failed\n");
  201. return PTR_ERR(tplane);
  202. }
  203. tidss->planes[tidss->num_planes++] = &tplane->plane;
  204. }
  205. return 0;
  206. }
  207. int tidss_modeset_init(struct tidss_device *tidss)
  208. {
  209. struct drm_device *ddev = &tidss->ddev;
  210. int ret;
  211. dev_dbg(tidss->dev, "%s\n", __func__);
  212. ret = drmm_mode_config_init(ddev);
  213. if (ret)
  214. return ret;
  215. ddev->mode_config.min_width = 8;
  216. ddev->mode_config.min_height = 8;
  217. ddev->mode_config.max_width = 8096;
  218. ddev->mode_config.max_height = 8096;
  219. ddev->mode_config.normalize_zpos = true;
  220. ddev->mode_config.funcs = &mode_config_funcs;
  221. ddev->mode_config.helper_private = &mode_config_helper_funcs;
  222. ret = tidss_dispc_modeset_init(tidss);
  223. if (ret)
  224. return ret;
  225. ret = drm_vblank_init(ddev, tidss->num_crtcs);
  226. if (ret)
  227. return ret;
  228. drm_mode_config_reset(ddev);
  229. dev_dbg(tidss->dev, "%s done\n", __func__);
  230. return 0;
  231. }