exynos_drm_crtc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* exynos_drm_crtc.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "exynos_drm_crtc.h"
  17. #include "exynos_drm_drv.h"
  18. #include "exynos_drm_encoder.h"
  19. #include "exynos_drm_plane.h"
  20. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  21. {
  22. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  23. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  24. if (exynos_crtc->dpms == mode) {
  25. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  26. return;
  27. }
  28. if (mode > DRM_MODE_DPMS_ON) {
  29. /* wait for the completion of page flip. */
  30. if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
  31. (exynos_crtc->event == NULL), HZ/20))
  32. exynos_crtc->event = NULL;
  33. drm_crtc_vblank_off(crtc);
  34. }
  35. if (exynos_crtc->ops->dpms)
  36. exynos_crtc->ops->dpms(exynos_crtc, mode);
  37. exynos_crtc->dpms = mode;
  38. if (mode == DRM_MODE_DPMS_ON)
  39. drm_crtc_vblank_on(crtc);
  40. }
  41. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  42. {
  43. /* drm framework doesn't check NULL. */
  44. }
  45. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  46. {
  47. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  48. struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
  49. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  50. if (exynos_crtc->ops->win_commit)
  51. exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
  52. if (exynos_crtc->ops->commit)
  53. exynos_crtc->ops->commit(exynos_crtc);
  54. }
  55. static bool
  56. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  57. const struct drm_display_mode *mode,
  58. struct drm_display_mode *adjusted_mode)
  59. {
  60. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  61. if (exynos_crtc->ops->mode_fixup)
  62. return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
  63. adjusted_mode);
  64. return true;
  65. }
  66. static int
  67. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  68. struct drm_display_mode *adjusted_mode, int x, int y,
  69. struct drm_framebuffer *old_fb)
  70. {
  71. struct drm_framebuffer *fb = crtc->primary->fb;
  72. unsigned int crtc_w;
  73. unsigned int crtc_h;
  74. int ret;
  75. /*
  76. * copy the mode data adjusted by mode_fixup() into crtc->mode
  77. * so that hardware can be seet to proper mode.
  78. */
  79. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  80. ret = exynos_check_plane(crtc->primary, fb);
  81. if (ret < 0)
  82. return ret;
  83. crtc_w = fb->width - x;
  84. crtc_h = fb->height - y;
  85. exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
  86. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  87. return 0;
  88. }
  89. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  90. struct drm_framebuffer *old_fb)
  91. {
  92. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  93. struct drm_framebuffer *fb = crtc->primary->fb;
  94. unsigned int crtc_w;
  95. unsigned int crtc_h;
  96. /* when framebuffer changing is requested, crtc's dpms should be on */
  97. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  98. DRM_ERROR("failed framebuffer changing request.\n");
  99. return -EPERM;
  100. }
  101. crtc_w = fb->width - x;
  102. crtc_h = fb->height - y;
  103. return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
  104. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  105. }
  106. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  107. {
  108. struct drm_plane *plane;
  109. int ret;
  110. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  111. drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
  112. if (plane->crtc != crtc)
  113. continue;
  114. ret = plane->funcs->disable_plane(plane);
  115. if (ret)
  116. DRM_ERROR("Failed to disable plane %d\n", ret);
  117. }
  118. }
  119. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  120. .dpms = exynos_drm_crtc_dpms,
  121. .prepare = exynos_drm_crtc_prepare,
  122. .commit = exynos_drm_crtc_commit,
  123. .mode_fixup = exynos_drm_crtc_mode_fixup,
  124. .mode_set = exynos_drm_crtc_mode_set,
  125. .mode_set_base = exynos_drm_crtc_mode_set_base,
  126. .disable = exynos_drm_crtc_disable,
  127. };
  128. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  129. struct drm_framebuffer *fb,
  130. struct drm_pending_vblank_event *event,
  131. uint32_t page_flip_flags)
  132. {
  133. struct drm_device *dev = crtc->dev;
  134. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  135. struct drm_framebuffer *old_fb = crtc->primary->fb;
  136. unsigned int crtc_w, crtc_h;
  137. int ret;
  138. /* when the page flip is requested, crtc's dpms should be on */
  139. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  140. DRM_ERROR("failed page flip request.\n");
  141. return -EINVAL;
  142. }
  143. if (!event)
  144. return -EINVAL;
  145. spin_lock_irq(&dev->event_lock);
  146. if (exynos_crtc->event) {
  147. ret = -EBUSY;
  148. goto out;
  149. }
  150. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  151. if (ret) {
  152. DRM_DEBUG("failed to acquire vblank counter\n");
  153. goto out;
  154. }
  155. exynos_crtc->event = event;
  156. spin_unlock_irq(&dev->event_lock);
  157. /*
  158. * the pipe from user always is 0 so we can set pipe number
  159. * of current owner to event.
  160. */
  161. event->pipe = exynos_crtc->pipe;
  162. crtc->primary->fb = fb;
  163. crtc_w = fb->width - crtc->x;
  164. crtc_h = fb->height - crtc->y;
  165. ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
  166. crtc_w, crtc_h, crtc->x, crtc->y,
  167. crtc_w, crtc_h);
  168. if (ret) {
  169. crtc->primary->fb = old_fb;
  170. spin_lock_irq(&dev->event_lock);
  171. exynos_crtc->event = NULL;
  172. drm_vblank_put(dev, exynos_crtc->pipe);
  173. spin_unlock_irq(&dev->event_lock);
  174. return ret;
  175. }
  176. return 0;
  177. out:
  178. spin_unlock_irq(&dev->event_lock);
  179. return ret;
  180. }
  181. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  182. {
  183. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  184. struct exynos_drm_private *private = crtc->dev->dev_private;
  185. private->crtc[exynos_crtc->pipe] = NULL;
  186. drm_crtc_cleanup(crtc);
  187. kfree(exynos_crtc);
  188. }
  189. static struct drm_crtc_funcs exynos_crtc_funcs = {
  190. .set_config = drm_crtc_helper_set_config,
  191. .page_flip = exynos_drm_crtc_page_flip,
  192. .destroy = exynos_drm_crtc_destroy,
  193. };
  194. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  195. struct drm_plane *plane,
  196. int pipe,
  197. enum exynos_drm_output_type type,
  198. const struct exynos_drm_crtc_ops *ops,
  199. void *ctx)
  200. {
  201. struct exynos_drm_crtc *exynos_crtc;
  202. struct exynos_drm_private *private = drm_dev->dev_private;
  203. struct drm_crtc *crtc;
  204. int ret;
  205. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  206. if (!exynos_crtc)
  207. return ERR_PTR(-ENOMEM);
  208. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  209. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  210. exynos_crtc->pipe = pipe;
  211. exynos_crtc->type = type;
  212. exynos_crtc->ops = ops;
  213. exynos_crtc->ctx = ctx;
  214. crtc = &exynos_crtc->base;
  215. private->crtc[pipe] = crtc;
  216. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  217. &exynos_crtc_funcs);
  218. if (ret < 0)
  219. goto err_crtc;
  220. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  221. return exynos_crtc;
  222. err_crtc:
  223. plane->funcs->destroy(plane);
  224. kfree(exynos_crtc);
  225. return ERR_PTR(ret);
  226. }
  227. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  228. {
  229. struct exynos_drm_private *private = dev->dev_private;
  230. struct exynos_drm_crtc *exynos_crtc =
  231. to_exynos_crtc(private->crtc[pipe]);
  232. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  233. return -EPERM;
  234. if (exynos_crtc->ops->enable_vblank)
  235. exynos_crtc->ops->enable_vblank(exynos_crtc);
  236. return 0;
  237. }
  238. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  239. {
  240. struct exynos_drm_private *private = dev->dev_private;
  241. struct exynos_drm_crtc *exynos_crtc =
  242. to_exynos_crtc(private->crtc[pipe]);
  243. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  244. return;
  245. if (exynos_crtc->ops->disable_vblank)
  246. exynos_crtc->ops->disable_vblank(exynos_crtc);
  247. }
  248. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
  249. {
  250. struct exynos_drm_private *dev_priv = dev->dev_private;
  251. struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
  252. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  253. unsigned long flags;
  254. spin_lock_irqsave(&dev->event_lock, flags);
  255. if (exynos_crtc->event) {
  256. drm_send_vblank_event(dev, -1, exynos_crtc->event);
  257. drm_vblank_put(dev, pipe);
  258. wake_up(&exynos_crtc->pending_flip_queue);
  259. }
  260. exynos_crtc->event = NULL;
  261. spin_unlock_irqrestore(&dev->event_lock, flags);
  262. }
  263. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  264. {
  265. struct exynos_drm_crtc *exynos_crtc;
  266. struct drm_device *dev = fb->dev;
  267. struct drm_crtc *crtc;
  268. /*
  269. * make sure that overlay data are updated to real hardware
  270. * for all encoders.
  271. */
  272. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  273. exynos_crtc = to_exynos_crtc(crtc);
  274. /*
  275. * wait for vblank interrupt
  276. * - this makes sure that overlay data are updated to
  277. * real hardware.
  278. */
  279. if (exynos_crtc->ops->wait_for_vblank)
  280. exynos_crtc->ops->wait_for_vblank(exynos_crtc);
  281. }
  282. }
  283. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  284. unsigned int out_type)
  285. {
  286. struct drm_crtc *crtc;
  287. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  288. struct exynos_drm_crtc *exynos_crtc;
  289. exynos_crtc = to_exynos_crtc(crtc);
  290. if (exynos_crtc->type == out_type)
  291. return exynos_crtc->pipe;
  292. }
  293. return -EPERM;
  294. }
  295. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  296. {
  297. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  298. if (exynos_crtc->ops->te_handler)
  299. exynos_crtc->ops->te_handler(exynos_crtc);
  300. }