exynos_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_drv.h"
  17. #include "exynos_drm_encoder.h"
  18. #include "exynos_drm_plane.h"
  19. #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
  20. drm_crtc)
  21. enum exynos_crtc_mode {
  22. CRTC_MODE_NORMAL, /* normal mode */
  23. CRTC_MODE_BLANK, /* The private plane of crtc is blank */
  24. };
  25. /*
  26. * Exynos specific crtc structure.
  27. *
  28. * @drm_crtc: crtc object.
  29. * @drm_plane: pointer of private plane object for this crtc
  30. * @pipe: a crtc index created at load() with a new crtc object creation
  31. * and the crtc object would be set to private->crtc array
  32. * to get a crtc object corresponding to this pipe from private->crtc
  33. * array when irq interrupt occured. the reason of using this pipe is that
  34. * drm framework doesn't support multiple irq yet.
  35. * we can refer to the crtc to current hardware interrupt occured through
  36. * this pipe value.
  37. * @dpms: store the crtc dpms value
  38. * @mode: store the crtc mode value
  39. */
  40. struct exynos_drm_crtc {
  41. struct drm_crtc drm_crtc;
  42. struct drm_plane *plane;
  43. unsigned int pipe;
  44. unsigned int dpms;
  45. enum exynos_crtc_mode mode;
  46. wait_queue_head_t pending_flip_queue;
  47. atomic_t pending_flip;
  48. };
  49. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  50. {
  51. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  52. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  53. if (exynos_crtc->dpms == mode) {
  54. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  55. return;
  56. }
  57. if (mode > DRM_MODE_DPMS_ON) {
  58. /* wait for the completion of page flip. */
  59. wait_event(exynos_crtc->pending_flip_queue,
  60. atomic_read(&exynos_crtc->pending_flip) == 0);
  61. drm_vblank_off(crtc->dev, exynos_crtc->pipe);
  62. }
  63. exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
  64. exynos_crtc->dpms = mode;
  65. }
  66. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  67. {
  68. /* drm framework doesn't check NULL. */
  69. }
  70. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  71. {
  72. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  73. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  74. exynos_plane_commit(exynos_crtc->plane);
  75. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
  76. }
  77. static bool
  78. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  79. const struct drm_display_mode *mode,
  80. struct drm_display_mode *adjusted_mode)
  81. {
  82. /* drm framework doesn't check NULL */
  83. return true;
  84. }
  85. static int
  86. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  87. struct drm_display_mode *adjusted_mode, int x, int y,
  88. struct drm_framebuffer *old_fb)
  89. {
  90. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  91. struct drm_plane *plane = exynos_crtc->plane;
  92. unsigned int crtc_w;
  93. unsigned int crtc_h;
  94. int pipe = exynos_crtc->pipe;
  95. int ret;
  96. /*
  97. * copy the mode data adjusted by mode_fixup() into crtc->mode
  98. * so that hardware can be seet to proper mode.
  99. */
  100. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  101. crtc_w = crtc->fb->width - x;
  102. crtc_h = crtc->fb->height - y;
  103. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  104. x, y, crtc_w, crtc_h);
  105. if (ret)
  106. return ret;
  107. plane->crtc = crtc;
  108. plane->fb = crtc->fb;
  109. exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
  110. return 0;
  111. }
  112. static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
  113. struct drm_framebuffer *old_fb)
  114. {
  115. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  116. struct drm_plane *plane = exynos_crtc->plane;
  117. unsigned int crtc_w;
  118. unsigned int crtc_h;
  119. int ret;
  120. /* when framebuffer changing is requested, crtc's dpms should be on */
  121. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  122. DRM_ERROR("failed framebuffer changing request.\n");
  123. return -EPERM;
  124. }
  125. crtc_w = crtc->fb->width - x;
  126. crtc_h = crtc->fb->height - y;
  127. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  128. x, y, crtc_w, crtc_h);
  129. if (ret)
  130. return ret;
  131. exynos_drm_crtc_commit(crtc);
  132. return 0;
  133. }
  134. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  135. struct drm_framebuffer *old_fb)
  136. {
  137. return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
  138. }
  139. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  140. {
  141. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  142. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
  143. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  144. }
  145. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  146. .dpms = exynos_drm_crtc_dpms,
  147. .prepare = exynos_drm_crtc_prepare,
  148. .commit = exynos_drm_crtc_commit,
  149. .mode_fixup = exynos_drm_crtc_mode_fixup,
  150. .mode_set = exynos_drm_crtc_mode_set,
  151. .mode_set_base = exynos_drm_crtc_mode_set_base,
  152. .disable = exynos_drm_crtc_disable,
  153. };
  154. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  155. struct drm_framebuffer *fb,
  156. struct drm_pending_vblank_event *event,
  157. uint32_t page_flip_flags)
  158. {
  159. struct drm_device *dev = crtc->dev;
  160. struct exynos_drm_private *dev_priv = dev->dev_private;
  161. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  162. struct drm_framebuffer *old_fb = crtc->fb;
  163. int ret = -EINVAL;
  164. /* when the page flip is requested, crtc's dpms should be on */
  165. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  166. DRM_ERROR("failed page flip request.\n");
  167. return -EINVAL;
  168. }
  169. mutex_lock(&dev->struct_mutex);
  170. if (event) {
  171. /*
  172. * the pipe from user always is 0 so we can set pipe number
  173. * of current owner to event.
  174. */
  175. event->pipe = exynos_crtc->pipe;
  176. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  177. if (ret) {
  178. DRM_DEBUG("failed to acquire vblank counter\n");
  179. goto out;
  180. }
  181. spin_lock_irq(&dev->event_lock);
  182. list_add_tail(&event->base.link,
  183. &dev_priv->pageflip_event_list);
  184. atomic_set(&exynos_crtc->pending_flip, 1);
  185. spin_unlock_irq(&dev->event_lock);
  186. crtc->fb = fb;
  187. ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
  188. NULL);
  189. if (ret) {
  190. crtc->fb = old_fb;
  191. spin_lock_irq(&dev->event_lock);
  192. drm_vblank_put(dev, exynos_crtc->pipe);
  193. list_del(&event->base.link);
  194. spin_unlock_irq(&dev->event_lock);
  195. goto out;
  196. }
  197. }
  198. out:
  199. mutex_unlock(&dev->struct_mutex);
  200. return ret;
  201. }
  202. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  203. {
  204. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  205. struct exynos_drm_private *private = crtc->dev->dev_private;
  206. private->crtc[exynos_crtc->pipe] = NULL;
  207. drm_crtc_cleanup(crtc);
  208. kfree(exynos_crtc);
  209. }
  210. static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
  211. struct drm_property *property,
  212. uint64_t val)
  213. {
  214. struct drm_device *dev = crtc->dev;
  215. struct exynos_drm_private *dev_priv = dev->dev_private;
  216. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  217. if (property == dev_priv->crtc_mode_property) {
  218. enum exynos_crtc_mode mode = val;
  219. if (mode == exynos_crtc->mode)
  220. return 0;
  221. exynos_crtc->mode = mode;
  222. switch (mode) {
  223. case CRTC_MODE_NORMAL:
  224. exynos_drm_crtc_commit(crtc);
  225. break;
  226. case CRTC_MODE_BLANK:
  227. exynos_plane_dpms(exynos_crtc->plane,
  228. DRM_MODE_DPMS_OFF);
  229. break;
  230. default:
  231. break;
  232. }
  233. return 0;
  234. }
  235. return -EINVAL;
  236. }
  237. static struct drm_crtc_funcs exynos_crtc_funcs = {
  238. .set_config = drm_crtc_helper_set_config,
  239. .page_flip = exynos_drm_crtc_page_flip,
  240. .destroy = exynos_drm_crtc_destroy,
  241. .set_property = exynos_drm_crtc_set_property,
  242. };
  243. static const struct drm_prop_enum_list mode_names[] = {
  244. { CRTC_MODE_NORMAL, "normal" },
  245. { CRTC_MODE_BLANK, "blank" },
  246. };
  247. static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
  248. {
  249. struct drm_device *dev = crtc->dev;
  250. struct exynos_drm_private *dev_priv = dev->dev_private;
  251. struct drm_property *prop;
  252. prop = dev_priv->crtc_mode_property;
  253. if (!prop) {
  254. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  255. ARRAY_SIZE(mode_names));
  256. if (!prop)
  257. return;
  258. dev_priv->crtc_mode_property = prop;
  259. }
  260. drm_object_attach_property(&crtc->base, prop, 0);
  261. }
  262. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  263. {
  264. struct exynos_drm_crtc *exynos_crtc;
  265. struct exynos_drm_private *private = dev->dev_private;
  266. struct drm_crtc *crtc;
  267. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  268. if (!exynos_crtc) {
  269. DRM_ERROR("failed to allocate exynos crtc\n");
  270. return -ENOMEM;
  271. }
  272. exynos_crtc->pipe = nr;
  273. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  274. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  275. atomic_set(&exynos_crtc->pending_flip, 0);
  276. exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
  277. if (!exynos_crtc->plane) {
  278. kfree(exynos_crtc);
  279. return -ENOMEM;
  280. }
  281. crtc = &exynos_crtc->drm_crtc;
  282. private->crtc[nr] = crtc;
  283. drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
  284. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  285. exynos_drm_crtc_attach_mode_property(crtc);
  286. return 0;
  287. }
  288. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  289. {
  290. struct exynos_drm_private *private = dev->dev_private;
  291. struct exynos_drm_crtc *exynos_crtc =
  292. to_exynos_crtc(private->crtc[crtc]);
  293. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  294. return -EPERM;
  295. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  296. exynos_drm_enable_vblank);
  297. return 0;
  298. }
  299. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  300. {
  301. struct exynos_drm_private *private = dev->dev_private;
  302. struct exynos_drm_crtc *exynos_crtc =
  303. to_exynos_crtc(private->crtc[crtc]);
  304. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  305. return;
  306. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  307. exynos_drm_disable_vblank);
  308. }
  309. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc)
  310. {
  311. struct exynos_drm_private *dev_priv = dev->dev_private;
  312. struct drm_pending_vblank_event *e, *t;
  313. struct drm_crtc *drm_crtc = dev_priv->crtc[crtc];
  314. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  315. unsigned long flags;
  316. spin_lock_irqsave(&dev->event_lock, flags);
  317. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  318. base.link) {
  319. /* if event's pipe isn't same as crtc then ignore it. */
  320. if (crtc != e->pipe)
  321. continue;
  322. list_del(&e->base.link);
  323. drm_send_vblank_event(dev, -1, e);
  324. drm_vblank_put(dev, crtc);
  325. atomic_set(&exynos_crtc->pending_flip, 0);
  326. wake_up(&exynos_crtc->pending_flip_queue);
  327. }
  328. spin_unlock_irqrestore(&dev->event_lock, flags);
  329. }