sti_crtc.c 9.9 KB

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