sti_drm_crtc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_drm_drv.h"
  16. #include "sti_drm_crtc.h"
  17. #include "sti_vtg.h"
  18. static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  19. {
  20. DRM_DEBUG_KMS("\n");
  21. }
  22. static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
  23. {
  24. struct sti_mixer *mixer = to_sti_mixer(crtc);
  25. struct device *dev = mixer->dev;
  26. struct sti_compositor *compo = dev_get_drvdata(dev);
  27. mixer->enabled = true;
  28. /* Prepare and enable the compo IP clock */
  29. if (mixer->id == STI_MIXER_MAIN) {
  30. if (clk_prepare_enable(compo->clk_compo_main))
  31. DRM_INFO("Failed to prepare/enable compo_main clk\n");
  32. } else {
  33. if (clk_prepare_enable(compo->clk_compo_aux))
  34. DRM_INFO("Failed to prepare/enable compo_aux clk\n");
  35. }
  36. sti_mixer_clear_all_layers(mixer);
  37. }
  38. static void sti_drm_crtc_commit(struct drm_crtc *crtc)
  39. {
  40. struct sti_mixer *mixer = to_sti_mixer(crtc);
  41. struct device *dev = mixer->dev;
  42. struct sti_compositor *compo = dev_get_drvdata(dev);
  43. struct sti_layer *layer;
  44. if ((!mixer || !compo)) {
  45. DRM_ERROR("Can not find mixer or compositor)\n");
  46. return;
  47. }
  48. /* get GDP which is reserved to the CRTC FB */
  49. layer = to_sti_layer(crtc->primary);
  50. if (layer)
  51. sti_layer_commit(layer);
  52. else
  53. DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
  54. /* Enable layer on mixer */
  55. if (sti_mixer_set_layer_status(mixer, layer, true))
  56. DRM_ERROR("Can not enable layer at mixer\n");
  57. drm_crtc_vblank_on(crtc);
  58. }
  59. static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  60. const struct drm_display_mode *mode,
  61. struct drm_display_mode *adjusted_mode)
  62. {
  63. /* accept the provided drm_display_mode, do not fix it up */
  64. return true;
  65. }
  66. static int
  67. sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode)
  68. {
  69. struct sti_mixer *mixer = to_sti_mixer(crtc);
  70. struct device *dev = mixer->dev;
  71. struct sti_compositor *compo = dev_get_drvdata(dev);
  72. struct clk *clk;
  73. int rate = mode->clock * 1000;
  74. int res;
  75. DRM_DEBUG_KMS("CRTC:%d (%s) mode:%d (%s)\n",
  76. crtc->base.id, sti_mixer_to_str(mixer),
  77. mode->base.id, mode->name);
  78. DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
  79. mode->vrefresh, mode->clock,
  80. mode->hdisplay,
  81. mode->hsync_start, mode->hsync_end,
  82. mode->htotal,
  83. mode->vdisplay,
  84. mode->vsync_start, mode->vsync_end,
  85. mode->vtotal, mode->type, mode->flags);
  86. /* Set rate and prepare/enable pixel clock */
  87. if (mixer->id == STI_MIXER_MAIN)
  88. clk = compo->clk_pix_main;
  89. else
  90. clk = compo->clk_pix_aux;
  91. res = clk_set_rate(clk, rate);
  92. if (res < 0) {
  93. DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
  94. return -EINVAL;
  95. }
  96. if (clk_prepare_enable(clk)) {
  97. DRM_ERROR("Failed to prepare/enable pix clk\n");
  98. return -EINVAL;
  99. }
  100. sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
  101. compo->vtg_main : compo->vtg_aux, &crtc->mode);
  102. res = sti_mixer_active_video_area(mixer, &crtc->mode);
  103. if (res) {
  104. DRM_ERROR("Can not set active video area\n");
  105. return -EINVAL;
  106. }
  107. return res;
  108. }
  109. static void sti_drm_crtc_disable(struct drm_crtc *crtc)
  110. {
  111. struct sti_mixer *mixer = to_sti_mixer(crtc);
  112. struct device *dev = mixer->dev;
  113. struct sti_compositor *compo = dev_get_drvdata(dev);
  114. if (!mixer->enabled)
  115. return;
  116. DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
  117. /* Disable Background */
  118. sti_mixer_set_background_status(mixer, false);
  119. drm_crtc_vblank_off(crtc);
  120. /* Disable pixel clock and compo IP clocks */
  121. if (mixer->id == STI_MIXER_MAIN) {
  122. clk_disable_unprepare(compo->clk_pix_main);
  123. clk_disable_unprepare(compo->clk_compo_main);
  124. } else {
  125. clk_disable_unprepare(compo->clk_pix_aux);
  126. clk_disable_unprepare(compo->clk_compo_aux);
  127. }
  128. mixer->enabled = false;
  129. }
  130. static void
  131. sti_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  132. {
  133. sti_drm_crtc_prepare(crtc);
  134. sti_drm_crtc_mode_set(crtc, &crtc->state->adjusted_mode);
  135. }
  136. static void sti_drm_atomic_begin(struct drm_crtc *crtc)
  137. {
  138. struct sti_mixer *mixer = to_sti_mixer(crtc);
  139. if (crtc->state->event) {
  140. crtc->state->event->pipe = drm_crtc_index(crtc);
  141. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  142. mixer->pending_event = crtc->state->event;
  143. crtc->state->event = NULL;
  144. }
  145. }
  146. static void sti_drm_atomic_flush(struct drm_crtc *crtc)
  147. {
  148. }
  149. static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
  150. .dpms = sti_drm_crtc_dpms,
  151. .prepare = sti_drm_crtc_prepare,
  152. .commit = sti_drm_crtc_commit,
  153. .mode_fixup = sti_drm_crtc_mode_fixup,
  154. .mode_set = drm_helper_crtc_mode_set,
  155. .mode_set_nofb = sti_drm_crtc_mode_set_nofb,
  156. .mode_set_base = drm_helper_crtc_mode_set_base,
  157. .disable = sti_drm_crtc_disable,
  158. .atomic_begin = sti_drm_atomic_begin,
  159. .atomic_flush = sti_drm_atomic_flush,
  160. };
  161. static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
  162. {
  163. DRM_DEBUG_KMS("\n");
  164. drm_crtc_cleanup(crtc);
  165. }
  166. static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
  167. struct drm_property *property,
  168. uint64_t val)
  169. {
  170. DRM_DEBUG_KMS("\n");
  171. return 0;
  172. }
  173. int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
  174. unsigned long event, void *data)
  175. {
  176. struct drm_device *drm_dev;
  177. struct sti_compositor *compo =
  178. container_of(nb, struct sti_compositor, vtg_vblank_nb);
  179. int *crtc = data;
  180. unsigned long flags;
  181. struct sti_drm_private *priv;
  182. drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
  183. priv = drm_dev->dev_private;
  184. if ((event != VTG_TOP_FIELD_EVENT) &&
  185. (event != VTG_BOTTOM_FIELD_EVENT)) {
  186. DRM_ERROR("unknown event: %lu\n", event);
  187. return -EINVAL;
  188. }
  189. drm_handle_vblank(drm_dev, *crtc);
  190. spin_lock_irqsave(&drm_dev->event_lock, flags);
  191. if (compo->mixer[*crtc]->pending_event) {
  192. drm_send_vblank_event(drm_dev, -1,
  193. compo->mixer[*crtc]->pending_event);
  194. drm_vblank_put(drm_dev, *crtc);
  195. compo->mixer[*crtc]->pending_event = NULL;
  196. }
  197. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  198. return 0;
  199. }
  200. int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  201. {
  202. struct sti_drm_private *dev_priv = dev->dev_private;
  203. struct sti_compositor *compo = dev_priv->compo;
  204. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  205. if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
  206. compo->vtg_main : compo->vtg_aux,
  207. vtg_vblank_nb, crtc)) {
  208. DRM_ERROR("Cannot register VTG notifier\n");
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
  214. void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  215. {
  216. struct sti_drm_private *priv = dev->dev_private;
  217. struct sti_compositor *compo = priv->compo;
  218. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  219. DRM_DEBUG_DRIVER("\n");
  220. if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
  221. compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
  222. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  223. /* free the resources of the pending requests */
  224. if (compo->mixer[crtc]->pending_event) {
  225. drm_vblank_put(dev, crtc);
  226. compo->mixer[crtc]->pending_event = NULL;
  227. }
  228. }
  229. EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
  230. static struct drm_crtc_funcs sti_crtc_funcs = {
  231. .set_config = drm_atomic_helper_set_config,
  232. .page_flip = drm_atomic_helper_page_flip,
  233. .destroy = sti_drm_crtc_destroy,
  234. .set_property = sti_drm_crtc_set_property,
  235. .reset = drm_atomic_helper_crtc_reset,
  236. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  237. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  238. };
  239. bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
  240. {
  241. struct sti_mixer *mixer = to_sti_mixer(crtc);
  242. if (mixer->id == STI_MIXER_MAIN)
  243. return true;
  244. return false;
  245. }
  246. EXPORT_SYMBOL(sti_drm_crtc_is_main);
  247. int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
  248. struct drm_plane *primary, struct drm_plane *cursor)
  249. {
  250. struct drm_crtc *crtc = &mixer->drm_crtc;
  251. int res;
  252. res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  253. &sti_crtc_funcs);
  254. if (res) {
  255. DRM_ERROR("Can not initialze CRTC\n");
  256. return -EINVAL;
  257. }
  258. drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
  259. DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
  260. crtc->base.id, sti_mixer_to_str(mixer));
  261. return 0;
  262. }