sti_crtc.c 9.2 KB

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