exynos_drm_crtc.c 11 KB

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