sti_crtc.c 9.8 KB

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