exynos_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. !atomic_read(&exynos_crtc->pending_flip),
  32. HZ/20))
  33. atomic_set(&exynos_crtc->pending_flip, 0);
  34. drm_crtc_vblank_off(crtc);
  35. }
  36. if (exynos_crtc->ops->dpms)
  37. exynos_crtc->ops->dpms(exynos_crtc, mode);
  38. exynos_crtc->dpms = mode;
  39. if (mode == DRM_MODE_DPMS_ON)
  40. drm_crtc_vblank_on(crtc);
  41. }
  42. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  43. {
  44. /* drm framework doesn't check NULL. */
  45. }
  46. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  47. {
  48. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  49. struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
  50. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  51. if (exynos_crtc->ops->win_commit)
  52. exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
  53. if (exynos_crtc->ops->commit)
  54. exynos_crtc->ops->commit(exynos_crtc);
  55. exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_ON);
  56. }
  57. static bool
  58. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  59. const struct drm_display_mode *mode,
  60. struct drm_display_mode *adjusted_mode)
  61. {
  62. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  63. if (exynos_crtc->ops->mode_fixup)
  64. return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
  65. adjusted_mode);
  66. return true;
  67. }
  68. static int
  69. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  70. struct drm_display_mode *adjusted_mode, int x, int y,
  71. struct drm_framebuffer *old_fb)
  72. {
  73. struct drm_framebuffer *fb = crtc->primary->fb;
  74. unsigned int crtc_w;
  75. unsigned int crtc_h;
  76. int ret;
  77. /*
  78. * copy the mode data adjusted by mode_fixup() into crtc->mode
  79. * so that hardware can be seet to proper mode.
  80. */
  81. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  82. ret = exynos_check_plane(crtc->primary, fb);
  83. if (ret < 0)
  84. return ret;
  85. crtc_w = fb->width - x;
  86. crtc_h = fb->height - y;
  87. exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
  88. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  89. return 0;
  90. }
  91. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  92. struct drm_framebuffer *old_fb)
  93. {
  94. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  95. struct drm_framebuffer *fb = crtc->primary->fb;
  96. unsigned int crtc_w;
  97. unsigned int crtc_h;
  98. /* when framebuffer changing is requested, crtc's dpms should be on */
  99. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  100. DRM_ERROR("failed framebuffer changing request.\n");
  101. return -EPERM;
  102. }
  103. crtc_w = fb->width - x;
  104. crtc_h = fb->height - y;
  105. return exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
  106. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  107. }
  108. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  109. {
  110. struct drm_plane *plane;
  111. int ret;
  112. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  113. drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
  114. if (plane->crtc != crtc)
  115. continue;
  116. ret = plane->funcs->disable_plane(plane);
  117. if (ret)
  118. DRM_ERROR("Failed to disable plane %d\n", ret);
  119. }
  120. }
  121. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  122. .dpms = exynos_drm_crtc_dpms,
  123. .prepare = exynos_drm_crtc_prepare,
  124. .commit = exynos_drm_crtc_commit,
  125. .mode_fixup = exynos_drm_crtc_mode_fixup,
  126. .mode_set = exynos_drm_crtc_mode_set,
  127. .mode_set_base = exynos_drm_crtc_mode_set_base,
  128. .disable = exynos_drm_crtc_disable,
  129. };
  130. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  131. struct drm_framebuffer *fb,
  132. struct drm_pending_vblank_event *event,
  133. uint32_t page_flip_flags)
  134. {
  135. struct drm_device *dev = crtc->dev;
  136. struct exynos_drm_private *dev_priv = dev->dev_private;
  137. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  138. struct drm_framebuffer *old_fb = crtc->primary->fb;
  139. unsigned int crtc_w, crtc_h;
  140. int ret = -EINVAL;
  141. /* when the page flip is requested, crtc's dpms should be on */
  142. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  143. DRM_ERROR("failed page flip request.\n");
  144. return -EINVAL;
  145. }
  146. mutex_lock(&dev->struct_mutex);
  147. if (event) {
  148. /*
  149. * the pipe from user always is 0 so we can set pipe number
  150. * of current owner to event.
  151. */
  152. event->pipe = exynos_crtc->pipe;
  153. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  154. if (ret) {
  155. DRM_DEBUG("failed to acquire vblank counter\n");
  156. goto out;
  157. }
  158. spin_lock_irq(&dev->event_lock);
  159. list_add_tail(&event->base.link,
  160. &dev_priv->pageflip_event_list);
  161. atomic_set(&exynos_crtc->pending_flip, 1);
  162. spin_unlock_irq(&dev->event_lock);
  163. crtc->primary->fb = fb;
  164. crtc_w = fb->width - crtc->x;
  165. crtc_h = fb->height - crtc->y;
  166. ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
  167. crtc_w, crtc_h, crtc->x, crtc->y,
  168. crtc_w, crtc_h);
  169. if (ret) {
  170. crtc->primary->fb = old_fb;
  171. spin_lock_irq(&dev->event_lock);
  172. drm_vblank_put(dev, exynos_crtc->pipe);
  173. list_del(&event->base.link);
  174. atomic_set(&exynos_crtc->pending_flip, 0);
  175. spin_unlock_irq(&dev->event_lock);
  176. goto out;
  177. }
  178. }
  179. out:
  180. mutex_unlock(&dev->struct_mutex);
  181. return ret;
  182. }
  183. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  184. {
  185. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  186. struct exynos_drm_private *private = crtc->dev->dev_private;
  187. private->crtc[exynos_crtc->pipe] = NULL;
  188. drm_crtc_cleanup(crtc);
  189. kfree(exynos_crtc);
  190. }
  191. static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
  192. struct drm_property *property,
  193. uint64_t val)
  194. {
  195. struct drm_device *dev = crtc->dev;
  196. struct exynos_drm_private *dev_priv = dev->dev_private;
  197. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  198. if (property == dev_priv->crtc_mode_property) {
  199. enum exynos_crtc_mode mode = val;
  200. if (mode == exynos_crtc->mode)
  201. return 0;
  202. exynos_crtc->mode = mode;
  203. switch (mode) {
  204. case CRTC_MODE_NORMAL:
  205. exynos_drm_crtc_commit(crtc);
  206. break;
  207. case CRTC_MODE_BLANK:
  208. exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
  209. break;
  210. default:
  211. break;
  212. }
  213. return 0;
  214. }
  215. return -EINVAL;
  216. }
  217. static struct drm_crtc_funcs exynos_crtc_funcs = {
  218. .set_config = drm_crtc_helper_set_config,
  219. .page_flip = exynos_drm_crtc_page_flip,
  220. .destroy = exynos_drm_crtc_destroy,
  221. .set_property = exynos_drm_crtc_set_property,
  222. };
  223. static const struct drm_prop_enum_list mode_names[] = {
  224. { CRTC_MODE_NORMAL, "normal" },
  225. { CRTC_MODE_BLANK, "blank" },
  226. };
  227. static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
  228. {
  229. struct drm_device *dev = crtc->dev;
  230. struct exynos_drm_private *dev_priv = dev->dev_private;
  231. struct drm_property *prop;
  232. prop = dev_priv->crtc_mode_property;
  233. if (!prop) {
  234. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  235. ARRAY_SIZE(mode_names));
  236. if (!prop)
  237. return;
  238. dev_priv->crtc_mode_property = prop;
  239. }
  240. drm_object_attach_property(&crtc->base, prop, 0);
  241. }
  242. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  243. int pipe,
  244. enum exynos_drm_output_type type,
  245. struct exynos_drm_crtc_ops *ops,
  246. void *ctx)
  247. {
  248. struct exynos_drm_crtc *exynos_crtc;
  249. struct drm_plane *plane;
  250. struct exynos_drm_private *private = drm_dev->dev_private;
  251. struct drm_crtc *crtc;
  252. int ret;
  253. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  254. if (!exynos_crtc)
  255. return ERR_PTR(-ENOMEM);
  256. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  257. atomic_set(&exynos_crtc->pending_flip, 0);
  258. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  259. exynos_crtc->pipe = pipe;
  260. exynos_crtc->type = type;
  261. exynos_crtc->ops = ops;
  262. exynos_crtc->ctx = ctx;
  263. plane = exynos_plane_init(drm_dev, 1 << pipe,
  264. DRM_PLANE_TYPE_PRIMARY);
  265. if (IS_ERR(plane)) {
  266. ret = PTR_ERR(plane);
  267. goto err_plane;
  268. }
  269. crtc = &exynos_crtc->base;
  270. private->crtc[pipe] = crtc;
  271. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  272. &exynos_crtc_funcs);
  273. if (ret < 0)
  274. goto err_crtc;
  275. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  276. exynos_drm_crtc_attach_mode_property(crtc);
  277. return exynos_crtc;
  278. err_crtc:
  279. plane->funcs->destroy(plane);
  280. err_plane:
  281. kfree(exynos_crtc);
  282. return ERR_PTR(ret);
  283. }
  284. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  285. {
  286. struct exynos_drm_private *private = dev->dev_private;
  287. struct exynos_drm_crtc *exynos_crtc =
  288. to_exynos_crtc(private->crtc[pipe]);
  289. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  290. return -EPERM;
  291. if (exynos_crtc->ops->enable_vblank)
  292. exynos_crtc->ops->enable_vblank(exynos_crtc);
  293. return 0;
  294. }
  295. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  296. {
  297. struct exynos_drm_private *private = dev->dev_private;
  298. struct exynos_drm_crtc *exynos_crtc =
  299. to_exynos_crtc(private->crtc[pipe]);
  300. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  301. return;
  302. if (exynos_crtc->ops->disable_vblank)
  303. exynos_crtc->ops->disable_vblank(exynos_crtc);
  304. }
  305. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
  306. {
  307. struct exynos_drm_private *dev_priv = dev->dev_private;
  308. struct drm_pending_vblank_event *e, *t;
  309. struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
  310. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  311. unsigned long flags;
  312. spin_lock_irqsave(&dev->event_lock, flags);
  313. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  314. base.link) {
  315. /* if event's pipe isn't same as crtc then ignore it. */
  316. if (pipe != e->pipe)
  317. continue;
  318. list_del(&e->base.link);
  319. drm_send_vblank_event(dev, -1, e);
  320. drm_vblank_put(dev, pipe);
  321. atomic_set(&exynos_crtc->pending_flip, 0);
  322. wake_up(&exynos_crtc->pending_flip_queue);
  323. }
  324. spin_unlock_irqrestore(&dev->event_lock, flags);
  325. }
  326. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  327. {
  328. struct exynos_drm_crtc *exynos_crtc;
  329. struct drm_device *dev = fb->dev;
  330. struct drm_crtc *crtc;
  331. /*
  332. * make sure that overlay data are updated to real hardware
  333. * for all encoders.
  334. */
  335. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  336. exynos_crtc = to_exynos_crtc(crtc);
  337. /*
  338. * wait for vblank interrupt
  339. * - this makes sure that overlay data are updated to
  340. * real hardware.
  341. */
  342. if (exynos_crtc->ops->wait_for_vblank)
  343. exynos_crtc->ops->wait_for_vblank(exynos_crtc);
  344. }
  345. }
  346. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  347. unsigned int out_type)
  348. {
  349. struct drm_crtc *crtc;
  350. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  351. struct exynos_drm_crtc *exynos_crtc;
  352. exynos_crtc = to_exynos_crtc(crtc);
  353. if (exynos_crtc->type == out_type)
  354. return exynos_crtc->pipe;
  355. }
  356. return -EPERM;
  357. }
  358. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  359. {
  360. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  361. if (exynos_crtc->ops->te_handler)
  362. exynos_crtc->ops->te_handler(exynos_crtc);
  363. }