sti_crtc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  5. * Fabien Dessenne <fabien.dessenne@st.com>
  6. * for STMicroelectronics.
  7. */
  8. #include <linux/clk.h>
  9. #include <drm/drmP.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include <drm/drm_plane_helper.h>
  14. #include "sti_compositor.h"
  15. #include "sti_crtc.h"
  16. #include "sti_drv.h"
  17. #include "sti_vid.h"
  18. #include "sti_vtg.h"
  19. static void sti_crtc_atomic_enable(struct drm_crtc *crtc,
  20. struct drm_crtc_state *old_state)
  21. {
  22. struct sti_mixer *mixer = to_sti_mixer(crtc);
  23. DRM_DEBUG_DRIVER("\n");
  24. mixer->status = STI_MIXER_READY;
  25. drm_crtc_vblank_on(crtc);
  26. }
  27. static void sti_crtc_atomic_disable(struct drm_crtc *crtc,
  28. struct drm_crtc_state *old_state)
  29. {
  30. struct sti_mixer *mixer = to_sti_mixer(crtc);
  31. DRM_DEBUG_DRIVER("\n");
  32. mixer->status = STI_MIXER_DISABLING;
  33. }
  34. static int
  35. sti_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
  36. {
  37. struct sti_mixer *mixer = to_sti_mixer(crtc);
  38. struct device *dev = mixer->dev;
  39. struct sti_compositor *compo = dev_get_drvdata(dev);
  40. struct clk *compo_clk, *pix_clk;
  41. int rate = mode->clock * 1000;
  42. DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
  43. crtc->base.id, sti_mixer_to_str(mixer),
  44. mode->base.id, mode->name);
  45. DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
  46. mode->vrefresh, mode->clock,
  47. mode->hdisplay,
  48. mode->hsync_start, mode->hsync_end,
  49. mode->htotal,
  50. mode->vdisplay,
  51. mode->vsync_start, mode->vsync_end,
  52. mode->vtotal, mode->type, mode->flags);
  53. if (mixer->id == STI_MIXER_MAIN) {
  54. compo_clk = compo->clk_compo_main;
  55. pix_clk = compo->clk_pix_main;
  56. } else {
  57. compo_clk = compo->clk_compo_aux;
  58. pix_clk = compo->clk_pix_aux;
  59. }
  60. /* Prepare and enable the compo IP clock */
  61. if (clk_prepare_enable(compo_clk)) {
  62. DRM_INFO("Failed to prepare/enable compositor clk\n");
  63. goto compo_error;
  64. }
  65. /* Set rate and prepare/enable pixel clock */
  66. if (clk_set_rate(pix_clk, rate) < 0) {
  67. DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
  68. goto pix_error;
  69. }
  70. if (clk_prepare_enable(pix_clk)) {
  71. DRM_ERROR("Failed to prepare/enable pix clk\n");
  72. goto pix_error;
  73. }
  74. sti_vtg_set_config(compo->vtg[mixer->id], &crtc->mode);
  75. if (sti_mixer_active_video_area(mixer, &crtc->mode)) {
  76. DRM_ERROR("Can't set active video area\n");
  77. goto mixer_error;
  78. }
  79. return 0;
  80. mixer_error:
  81. clk_disable_unprepare(pix_clk);
  82. pix_error:
  83. clk_disable_unprepare(compo_clk);
  84. compo_error:
  85. return -EINVAL;
  86. }
  87. static void sti_crtc_disable(struct drm_crtc *crtc)
  88. {
  89. struct sti_mixer *mixer = to_sti_mixer(crtc);
  90. struct device *dev = mixer->dev;
  91. struct sti_compositor *compo = dev_get_drvdata(dev);
  92. DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
  93. /* Disable Background */
  94. sti_mixer_set_background_status(mixer, false);
  95. drm_crtc_vblank_off(crtc);
  96. /* Disable pixel clock and compo IP clocks */
  97. if (mixer->id == STI_MIXER_MAIN) {
  98. clk_disable_unprepare(compo->clk_pix_main);
  99. clk_disable_unprepare(compo->clk_compo_main);
  100. } else {
  101. clk_disable_unprepare(compo->clk_pix_aux);
  102. clk_disable_unprepare(compo->clk_compo_aux);
  103. }
  104. mixer->status = STI_MIXER_DISABLED;
  105. }
  106. static void
  107. sti_crtc_mode_set_nofb(struct drm_crtc *crtc)
  108. {
  109. sti_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
  110. }
  111. static void sti_crtc_atomic_flush(struct drm_crtc *crtc,
  112. struct drm_crtc_state *old_crtc_state)
  113. {
  114. struct drm_device *drm_dev = crtc->dev;
  115. struct sti_mixer *mixer = to_sti_mixer(crtc);
  116. struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
  117. struct drm_plane *p;
  118. struct drm_pending_vblank_event *event;
  119. unsigned long flags;
  120. DRM_DEBUG_DRIVER("\n");
  121. /* perform plane actions */
  122. list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
  123. struct sti_plane *plane = to_sti_plane(p);
  124. switch (plane->status) {
  125. case STI_PLANE_UPDATED:
  126. /* ignore update for other CRTC */
  127. if (p->state->crtc != crtc)
  128. continue;
  129. /* update planes tag as updated */
  130. DRM_DEBUG_DRIVER("update plane %s\n",
  131. sti_plane_to_str(plane));
  132. if (sti_mixer_set_plane_depth(mixer, plane)) {
  133. DRM_ERROR("Cannot set plane %s depth\n",
  134. sti_plane_to_str(plane));
  135. break;
  136. }
  137. if (sti_mixer_set_plane_status(mixer, plane, true)) {
  138. DRM_ERROR("Cannot enable plane %s at mixer\n",
  139. sti_plane_to_str(plane));
  140. break;
  141. }
  142. /* if plane is HQVDP_0 then commit the vid[0] */
  143. if (plane->desc == STI_HQVDP_0)
  144. sti_vid_commit(compo->vid[0], p->state);
  145. plane->status = STI_PLANE_READY;
  146. break;
  147. case STI_PLANE_DISABLING:
  148. /* disabling sequence for planes tag as disabling */
  149. DRM_DEBUG_DRIVER("disable plane %s from mixer\n",
  150. sti_plane_to_str(plane));
  151. if (sti_mixer_set_plane_status(mixer, plane, false)) {
  152. DRM_ERROR("Cannot disable plane %s at mixer\n",
  153. sti_plane_to_str(plane));
  154. continue;
  155. }
  156. if (plane->desc == STI_CURSOR)
  157. /* tag plane status for disabled */
  158. plane->status = STI_PLANE_DISABLED;
  159. else
  160. /* tag plane status for flushing */
  161. plane->status = STI_PLANE_FLUSHING;
  162. /* if plane is HQVDP_0 then disable the vid[0] */
  163. if (plane->desc == STI_HQVDP_0)
  164. sti_vid_disable(compo->vid[0]);
  165. break;
  166. default:
  167. /* Other status case are not handled */
  168. break;
  169. }
  170. }
  171. event = crtc->state->event;
  172. if (event) {
  173. crtc->state->event = NULL;
  174. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  175. if (drm_crtc_vblank_get(crtc) == 0)
  176. drm_crtc_arm_vblank_event(crtc, event);
  177. else
  178. drm_crtc_send_vblank_event(crtc, event);
  179. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  180. }
  181. }
  182. static const struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
  183. .mode_set_nofb = sti_crtc_mode_set_nofb,
  184. .atomic_flush = sti_crtc_atomic_flush,
  185. .atomic_enable = sti_crtc_atomic_enable,
  186. .atomic_disable = sti_crtc_atomic_disable,
  187. };
  188. static void sti_crtc_destroy(struct drm_crtc *crtc)
  189. {
  190. DRM_DEBUG_KMS("\n");
  191. drm_crtc_cleanup(crtc);
  192. }
  193. static int sti_crtc_set_property(struct drm_crtc *crtc,
  194. struct drm_property *property,
  195. uint64_t val)
  196. {
  197. DRM_DEBUG_KMS("\n");
  198. return 0;
  199. }
  200. int sti_crtc_vblank_cb(struct notifier_block *nb,
  201. unsigned long event, void *data)
  202. {
  203. struct sti_compositor *compo;
  204. struct drm_crtc *crtc = data;
  205. struct sti_mixer *mixer;
  206. struct sti_private *priv;
  207. unsigned int pipe;
  208. priv = crtc->dev->dev_private;
  209. pipe = drm_crtc_index(crtc);
  210. compo = container_of(nb, struct sti_compositor, vtg_vblank_nb[pipe]);
  211. mixer = compo->mixer[pipe];
  212. if ((event != VTG_TOP_FIELD_EVENT) &&
  213. (event != VTG_BOTTOM_FIELD_EVENT)) {
  214. DRM_ERROR("unknown event: %lu\n", event);
  215. return -EINVAL;
  216. }
  217. drm_crtc_handle_vblank(crtc);
  218. if (mixer->status == STI_MIXER_DISABLING) {
  219. struct drm_plane *p;
  220. /* Disable mixer only if all overlay planes (GDP and VDP)
  221. * are disabled */
  222. list_for_each_entry(p, &crtc->dev->mode_config.plane_list,
  223. head) {
  224. struct sti_plane *plane = to_sti_plane(p);
  225. if ((plane->desc & STI_PLANE_TYPE_MASK) <= STI_VDP)
  226. if (plane->status != STI_PLANE_DISABLED)
  227. return 0;
  228. }
  229. sti_crtc_disable(crtc);
  230. }
  231. return 0;
  232. }
  233. int sti_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
  234. {
  235. struct sti_private *dev_priv = dev->dev_private;
  236. struct sti_compositor *compo = dev_priv->compo;
  237. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
  238. struct drm_crtc *crtc = &compo->mixer[pipe]->drm_crtc;
  239. struct sti_vtg *vtg = compo->vtg[pipe];
  240. DRM_DEBUG_DRIVER("\n");
  241. if (sti_vtg_register_client(vtg, vtg_vblank_nb, crtc)) {
  242. DRM_ERROR("Cannot register VTG notifier\n");
  243. return -EINVAL;
  244. }
  245. return 0;
  246. }
  247. void sti_crtc_disable_vblank(struct drm_device *drm_dev, unsigned int pipe)
  248. {
  249. struct sti_private *priv = drm_dev->dev_private;
  250. struct sti_compositor *compo = priv->compo;
  251. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb[pipe];
  252. struct sti_vtg *vtg = compo->vtg[pipe];
  253. DRM_DEBUG_DRIVER("\n");
  254. if (sti_vtg_unregister_client(vtg, vtg_vblank_nb))
  255. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  256. }
  257. static int sti_crtc_late_register(struct drm_crtc *crtc)
  258. {
  259. struct sti_mixer *mixer = to_sti_mixer(crtc);
  260. struct sti_compositor *compo = dev_get_drvdata(mixer->dev);
  261. if (drm_crtc_index(crtc) == 0)
  262. return sti_compositor_debugfs_init(compo, crtc->dev->primary);
  263. return 0;
  264. }
  265. static const struct drm_crtc_funcs sti_crtc_funcs = {
  266. .set_config = drm_atomic_helper_set_config,
  267. .page_flip = drm_atomic_helper_page_flip,
  268. .destroy = sti_crtc_destroy,
  269. .set_property = sti_crtc_set_property,
  270. .reset = drm_atomic_helper_crtc_reset,
  271. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  272. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  273. .late_register = sti_crtc_late_register,
  274. };
  275. bool sti_crtc_is_main(struct drm_crtc *crtc)
  276. {
  277. struct sti_mixer *mixer = to_sti_mixer(crtc);
  278. if (mixer->id == STI_MIXER_MAIN)
  279. return true;
  280. return false;
  281. }
  282. int sti_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
  283. struct drm_plane *primary, struct drm_plane *cursor)
  284. {
  285. struct drm_crtc *crtc = &mixer->drm_crtc;
  286. int res;
  287. res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  288. &sti_crtc_funcs, NULL);
  289. if (res) {
  290. DRM_ERROR("Can't initialize CRTC\n");
  291. return -EINVAL;
  292. }
  293. drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
  294. DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
  295. crtc->base.id, sti_mixer_to_str(mixer));
  296. return 0;
  297. }