tidss_kms.c 6.9 KB

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