sti_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_crtc_helper.h>
  11. #include <drm/drm_plane_helper.h>
  12. #include "sti_compositor.h"
  13. #include "sti_drm_drv.h"
  14. #include "sti_drm_crtc.h"
  15. #include "sti_vtg.h"
  16. static void sti_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  17. {
  18. DRM_DEBUG_KMS("\n");
  19. }
  20. static void sti_drm_crtc_prepare(struct drm_crtc *crtc)
  21. {
  22. struct sti_mixer *mixer = to_sti_mixer(crtc);
  23. struct device *dev = mixer->dev;
  24. struct sti_compositor *compo = dev_get_drvdata(dev);
  25. mixer->enabled = true;
  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. sti_mixer_clear_all_layers(mixer);
  35. }
  36. static void sti_drm_crtc_commit(struct drm_crtc *crtc)
  37. {
  38. struct sti_mixer *mixer = to_sti_mixer(crtc);
  39. struct device *dev = mixer->dev;
  40. struct sti_compositor *compo = dev_get_drvdata(dev);
  41. struct sti_layer *layer;
  42. if ((!mixer || !compo)) {
  43. DRM_ERROR("Can not find mixer or compositor)\n");
  44. return;
  45. }
  46. /* get GDP which is reserved to the CRTC FB */
  47. layer = to_sti_layer(crtc->primary);
  48. if (layer)
  49. sti_layer_commit(layer);
  50. else
  51. DRM_ERROR("Can not find CRTC dedicated plane (GDP0)\n");
  52. /* Enable layer on mixer */
  53. if (sti_mixer_set_layer_status(mixer, layer, true))
  54. DRM_ERROR("Can not enable layer at mixer\n");
  55. drm_crtc_vblank_on(crtc);
  56. }
  57. static bool sti_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  58. const struct drm_display_mode *mode,
  59. struct drm_display_mode *adjusted_mode)
  60. {
  61. /* accept the provided drm_display_mode, do not fix it up */
  62. return true;
  63. }
  64. static int
  65. sti_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  66. struct drm_display_mode *adjusted_mode, int x, int y,
  67. struct drm_framebuffer *old_fb)
  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 sti_layer *layer;
  73. struct clk *clk;
  74. int rate = mode->clock * 1000;
  75. int res;
  76. unsigned int w, h;
  77. DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d mode:%d (%s)\n",
  78. crtc->base.id, sti_mixer_to_str(mixer),
  79. crtc->primary->fb->base.id, mode->base.id, mode->name);
  80. DRM_DEBUG_KMS("%d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
  81. mode->vrefresh, mode->clock,
  82. mode->hdisplay,
  83. mode->hsync_start, mode->hsync_end,
  84. mode->htotal,
  85. mode->vdisplay,
  86. mode->vsync_start, mode->vsync_end,
  87. mode->vtotal, mode->type, mode->flags);
  88. /* Set rate and prepare/enable pixel clock */
  89. if (mixer->id == STI_MIXER_MAIN)
  90. clk = compo->clk_pix_main;
  91. else
  92. clk = compo->clk_pix_aux;
  93. res = clk_set_rate(clk, rate);
  94. if (res < 0) {
  95. DRM_ERROR("Cannot set rate (%dHz) for pix clk\n", rate);
  96. return -EINVAL;
  97. }
  98. if (clk_prepare_enable(clk)) {
  99. DRM_ERROR("Failed to prepare/enable pix clk\n");
  100. return -EINVAL;
  101. }
  102. sti_vtg_set_config(mixer->id == STI_MIXER_MAIN ?
  103. compo->vtg_main : compo->vtg_aux, &crtc->mode);
  104. /* a GDP is reserved to the CRTC FB */
  105. layer = to_sti_layer(crtc->primary);
  106. if (!layer) {
  107. DRM_ERROR("Can not find GDP0)\n");
  108. return -EINVAL;
  109. }
  110. /* copy the mode data adjusted by mode_fixup() into crtc->mode
  111. * so that hardware can be set to proper mode
  112. */
  113. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  114. res = sti_mixer_set_layer_depth(mixer, layer);
  115. if (res) {
  116. DRM_ERROR("Can not set layer depth\n");
  117. return -EINVAL;
  118. }
  119. res = sti_mixer_active_video_area(mixer, &crtc->mode);
  120. if (res) {
  121. DRM_ERROR("Can not set active video area\n");
  122. return -EINVAL;
  123. }
  124. w = crtc->primary->fb->width - x;
  125. h = crtc->primary->fb->height - y;
  126. return sti_layer_prepare(layer, crtc,
  127. crtc->primary->fb, &crtc->mode,
  128. mixer->id, 0, 0, w, h, x, y, w, h);
  129. }
  130. static int sti_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  131. struct drm_framebuffer *old_fb)
  132. {
  133. struct sti_mixer *mixer = to_sti_mixer(crtc);
  134. struct sti_layer *layer;
  135. unsigned int w, h;
  136. int ret;
  137. DRM_DEBUG_KMS("CRTC:%d (%s) fb:%d (%d,%d)\n",
  138. crtc->base.id, sti_mixer_to_str(mixer),
  139. crtc->primary->fb->base.id, x, y);
  140. /* GDP is reserved to the CRTC FB */
  141. layer = to_sti_layer(crtc->primary);
  142. if (!layer) {
  143. DRM_ERROR("Can not find GDP0)\n");
  144. ret = -EINVAL;
  145. goto out;
  146. }
  147. w = crtc->primary->fb->width - crtc->x;
  148. h = crtc->primary->fb->height - crtc->y;
  149. ret = sti_layer_prepare(layer, crtc,
  150. crtc->primary->fb, &crtc->mode,
  151. mixer->id, 0, 0, w, h,
  152. crtc->x, crtc->y, w, h);
  153. if (ret) {
  154. DRM_ERROR("Can not prepare layer\n");
  155. goto out;
  156. }
  157. sti_drm_crtc_commit(crtc);
  158. out:
  159. return ret;
  160. }
  161. static void sti_drm_crtc_load_lut(struct drm_crtc *crtc)
  162. {
  163. /* do nothing */
  164. }
  165. static void sti_drm_crtc_disable(struct drm_crtc *crtc)
  166. {
  167. struct sti_mixer *mixer = to_sti_mixer(crtc);
  168. struct device *dev = mixer->dev;
  169. struct sti_compositor *compo = dev_get_drvdata(dev);
  170. struct sti_layer *layer;
  171. if (!mixer->enabled)
  172. return;
  173. DRM_DEBUG_KMS("CRTC:%d (%s)\n", crtc->base.id, sti_mixer_to_str(mixer));
  174. /* Disable Background */
  175. sti_mixer_set_background_status(mixer, false);
  176. /* Disable GDP */
  177. layer = to_sti_layer(crtc->primary);
  178. if (!layer) {
  179. DRM_ERROR("Cannot find GDP0\n");
  180. return;
  181. }
  182. /* Disable layer at mixer level */
  183. if (sti_mixer_set_layer_status(mixer, layer, false))
  184. DRM_ERROR("Can not disable %s layer at mixer\n",
  185. sti_layer_to_str(layer));
  186. /* Wait a while to be sure that a Vsync event is received */
  187. msleep(WAIT_NEXT_VSYNC_MS);
  188. /* Then disable layer itself */
  189. sti_layer_disable(layer);
  190. drm_crtc_vblank_off(crtc);
  191. /* Disable pixel clock and compo IP clocks */
  192. if (mixer->id == STI_MIXER_MAIN) {
  193. clk_disable_unprepare(compo->clk_pix_main);
  194. clk_disable_unprepare(compo->clk_compo_main);
  195. } else {
  196. clk_disable_unprepare(compo->clk_pix_aux);
  197. clk_disable_unprepare(compo->clk_compo_aux);
  198. }
  199. mixer->enabled = false;
  200. }
  201. static struct drm_crtc_helper_funcs sti_crtc_helper_funcs = {
  202. .dpms = sti_drm_crtc_dpms,
  203. .prepare = sti_drm_crtc_prepare,
  204. .commit = sti_drm_crtc_commit,
  205. .mode_fixup = sti_drm_crtc_mode_fixup,
  206. .mode_set = sti_drm_crtc_mode_set,
  207. .mode_set_base = sti_drm_crtc_mode_set_base,
  208. .load_lut = sti_drm_crtc_load_lut,
  209. .disable = sti_drm_crtc_disable,
  210. };
  211. static int sti_drm_crtc_page_flip(struct drm_crtc *crtc,
  212. struct drm_framebuffer *fb,
  213. struct drm_pending_vblank_event *event,
  214. uint32_t page_flip_flags)
  215. {
  216. struct drm_device *drm_dev = crtc->dev;
  217. struct drm_framebuffer *old_fb;
  218. struct sti_mixer *mixer = to_sti_mixer(crtc);
  219. unsigned long flags;
  220. int ret;
  221. DRM_DEBUG_KMS("fb %d --> fb %d\n",
  222. crtc->primary->fb->base.id, fb->base.id);
  223. mutex_lock(&drm_dev->struct_mutex);
  224. old_fb = crtc->primary->fb;
  225. crtc->primary->fb = fb;
  226. ret = sti_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, old_fb);
  227. if (ret) {
  228. DRM_ERROR("failed\n");
  229. crtc->primary->fb = old_fb;
  230. goto out;
  231. }
  232. if (event) {
  233. event->pipe = mixer->id;
  234. ret = drm_vblank_get(drm_dev, event->pipe);
  235. if (ret) {
  236. DRM_ERROR("Cannot get vblank\n");
  237. goto out;
  238. }
  239. spin_lock_irqsave(&drm_dev->event_lock, flags);
  240. if (mixer->pending_event) {
  241. drm_vblank_put(drm_dev, event->pipe);
  242. ret = -EBUSY;
  243. } else {
  244. mixer->pending_event = event;
  245. }
  246. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  247. }
  248. out:
  249. mutex_unlock(&drm_dev->struct_mutex);
  250. return ret;
  251. }
  252. static void sti_drm_crtc_destroy(struct drm_crtc *crtc)
  253. {
  254. DRM_DEBUG_KMS("\n");
  255. drm_crtc_cleanup(crtc);
  256. }
  257. static int sti_drm_crtc_set_property(struct drm_crtc *crtc,
  258. struct drm_property *property,
  259. uint64_t val)
  260. {
  261. DRM_DEBUG_KMS("\n");
  262. return 0;
  263. }
  264. int sti_drm_crtc_vblank_cb(struct notifier_block *nb,
  265. unsigned long event, void *data)
  266. {
  267. struct drm_device *drm_dev;
  268. struct sti_compositor *compo =
  269. container_of(nb, struct sti_compositor, vtg_vblank_nb);
  270. int *crtc = data;
  271. unsigned long flags;
  272. struct sti_drm_private *priv;
  273. drm_dev = compo->mixer[*crtc]->drm_crtc.dev;
  274. priv = drm_dev->dev_private;
  275. if ((event != VTG_TOP_FIELD_EVENT) &&
  276. (event != VTG_BOTTOM_FIELD_EVENT)) {
  277. DRM_ERROR("unknown event: %lu\n", event);
  278. return -EINVAL;
  279. }
  280. drm_handle_vblank(drm_dev, *crtc);
  281. spin_lock_irqsave(&drm_dev->event_lock, flags);
  282. if (compo->mixer[*crtc]->pending_event) {
  283. drm_send_vblank_event(drm_dev, -1,
  284. compo->mixer[*crtc]->pending_event);
  285. drm_vblank_put(drm_dev, *crtc);
  286. compo->mixer[*crtc]->pending_event = NULL;
  287. }
  288. spin_unlock_irqrestore(&drm_dev->event_lock, flags);
  289. return 0;
  290. }
  291. int sti_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  292. {
  293. struct sti_drm_private *dev_priv = dev->dev_private;
  294. struct sti_compositor *compo = dev_priv->compo;
  295. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  296. if (sti_vtg_register_client(crtc == STI_MIXER_MAIN ?
  297. compo->vtg_main : compo->vtg_aux,
  298. vtg_vblank_nb, crtc)) {
  299. DRM_ERROR("Cannot register VTG notifier\n");
  300. return -EINVAL;
  301. }
  302. return 0;
  303. }
  304. EXPORT_SYMBOL(sti_drm_crtc_enable_vblank);
  305. void sti_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  306. {
  307. struct sti_drm_private *priv = dev->dev_private;
  308. struct sti_compositor *compo = priv->compo;
  309. struct notifier_block *vtg_vblank_nb = &compo->vtg_vblank_nb;
  310. DRM_DEBUG_DRIVER("\n");
  311. if (sti_vtg_unregister_client(crtc == STI_MIXER_MAIN ?
  312. compo->vtg_main : compo->vtg_aux, vtg_vblank_nb))
  313. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  314. /* free the resources of the pending requests */
  315. if (compo->mixer[crtc]->pending_event) {
  316. drm_vblank_put(dev, crtc);
  317. compo->mixer[crtc]->pending_event = NULL;
  318. }
  319. }
  320. EXPORT_SYMBOL(sti_drm_crtc_disable_vblank);
  321. static struct drm_crtc_funcs sti_crtc_funcs = {
  322. .set_config = drm_crtc_helper_set_config,
  323. .page_flip = sti_drm_crtc_page_flip,
  324. .destroy = sti_drm_crtc_destroy,
  325. .set_property = sti_drm_crtc_set_property,
  326. };
  327. bool sti_drm_crtc_is_main(struct drm_crtc *crtc)
  328. {
  329. struct sti_mixer *mixer = to_sti_mixer(crtc);
  330. if (mixer->id == STI_MIXER_MAIN)
  331. return true;
  332. return false;
  333. }
  334. EXPORT_SYMBOL(sti_drm_crtc_is_main);
  335. int sti_drm_crtc_init(struct drm_device *drm_dev, struct sti_mixer *mixer,
  336. struct drm_plane *primary, struct drm_plane *cursor)
  337. {
  338. struct drm_crtc *crtc = &mixer->drm_crtc;
  339. int res;
  340. res = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
  341. &sti_crtc_funcs);
  342. if (res) {
  343. DRM_ERROR("Can not initialze CRTC\n");
  344. return -EINVAL;
  345. }
  346. drm_crtc_helper_add(crtc, &sti_crtc_helper_funcs);
  347. DRM_DEBUG_DRIVER("drm CRTC:%d mapped to %s\n",
  348. crtc->base.id, sti_mixer_to_str(mixer));
  349. return 0;
  350. }