exynos_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. };
  47. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  48. {
  49. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  50. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  51. if (exynos_crtc->dpms == mode) {
  52. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  53. return;
  54. }
  55. exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
  56. exynos_crtc->dpms = mode;
  57. }
  58. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  59. {
  60. DRM_DEBUG_KMS("%s\n", __FILE__);
  61. /* drm framework doesn't check NULL. */
  62. }
  63. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  64. {
  65. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  66. DRM_DEBUG_KMS("%s\n", __FILE__);
  67. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  68. exynos_plane_commit(exynos_crtc->plane);
  69. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
  70. }
  71. static bool
  72. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  73. const struct drm_display_mode *mode,
  74. struct drm_display_mode *adjusted_mode)
  75. {
  76. DRM_DEBUG_KMS("%s\n", __FILE__);
  77. /* drm framework doesn't check NULL */
  78. return true;
  79. }
  80. static int
  81. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  82. struct drm_display_mode *adjusted_mode, int x, int y,
  83. struct drm_framebuffer *old_fb)
  84. {
  85. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  86. struct drm_plane *plane = exynos_crtc->plane;
  87. unsigned int crtc_w;
  88. unsigned int crtc_h;
  89. int pipe = exynos_crtc->pipe;
  90. int ret;
  91. DRM_DEBUG_KMS("%s\n", __FILE__);
  92. /*
  93. * copy the mode data adjusted by mode_fixup() into crtc->mode
  94. * so that hardware can be seet to proper mode.
  95. */
  96. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  97. crtc_w = crtc->fb->width - x;
  98. crtc_h = crtc->fb->height - y;
  99. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  100. x, y, crtc_w, crtc_h);
  101. if (ret)
  102. return ret;
  103. plane->crtc = crtc;
  104. plane->fb = crtc->fb;
  105. exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
  106. return 0;
  107. }
  108. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  109. struct drm_framebuffer *old_fb)
  110. {
  111. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  112. struct drm_plane *plane = exynos_crtc->plane;
  113. unsigned int crtc_w;
  114. unsigned int crtc_h;
  115. int ret;
  116. DRM_DEBUG_KMS("%s\n", __FILE__);
  117. /* when framebuffer changing is requested, crtc's dpms should be on */
  118. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  119. DRM_ERROR("failed framebuffer changing request.\n");
  120. return -EPERM;
  121. }
  122. crtc_w = crtc->fb->width - x;
  123. crtc_h = crtc->fb->height - y;
  124. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  125. x, y, crtc_w, crtc_h);
  126. if (ret)
  127. return ret;
  128. exynos_drm_crtc_commit(crtc);
  129. return 0;
  130. }
  131. static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
  132. {
  133. DRM_DEBUG_KMS("%s\n", __FILE__);
  134. /* drm framework doesn't check NULL */
  135. }
  136. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  137. {
  138. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  139. DRM_DEBUG_KMS("%s\n", __FILE__);
  140. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
  141. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  142. }
  143. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  144. .dpms = exynos_drm_crtc_dpms,
  145. .prepare = exynos_drm_crtc_prepare,
  146. .commit = exynos_drm_crtc_commit,
  147. .mode_fixup = exynos_drm_crtc_mode_fixup,
  148. .mode_set = exynos_drm_crtc_mode_set,
  149. .mode_set_base = exynos_drm_crtc_mode_set_base,
  150. .load_lut = exynos_drm_crtc_load_lut,
  151. .disable = exynos_drm_crtc_disable,
  152. };
  153. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  154. struct drm_framebuffer *fb,
  155. struct drm_pending_vblank_event *event)
  156. {
  157. struct drm_device *dev = crtc->dev;
  158. struct exynos_drm_private *dev_priv = dev->dev_private;
  159. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  160. struct drm_framebuffer *old_fb = crtc->fb;
  161. int ret = -EINVAL;
  162. DRM_DEBUG_KMS("%s\n", __FILE__);
  163. /* when the page flip is requested, crtc's dpms should be on */
  164. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  165. DRM_ERROR("failed page flip request.\n");
  166. return -EINVAL;
  167. }
  168. mutex_lock(&dev->struct_mutex);
  169. if (event) {
  170. /*
  171. * the pipe from user always is 0 so we can set pipe number
  172. * of current owner to event.
  173. */
  174. event->pipe = exynos_crtc->pipe;
  175. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  176. if (ret) {
  177. DRM_DEBUG("failed to acquire vblank counter\n");
  178. list_del(&event->base.link);
  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. spin_unlock_irq(&dev->event_lock);
  185. crtc->fb = fb;
  186. ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
  187. NULL);
  188. if (ret) {
  189. crtc->fb = old_fb;
  190. spin_lock_irq(&dev->event_lock);
  191. drm_vblank_put(dev, exynos_crtc->pipe);
  192. list_del(&event->base.link);
  193. spin_unlock_irq(&dev->event_lock);
  194. goto out;
  195. }
  196. }
  197. out:
  198. mutex_unlock(&dev->struct_mutex);
  199. return ret;
  200. }
  201. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  202. {
  203. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  204. struct exynos_drm_private *private = crtc->dev->dev_private;
  205. DRM_DEBUG_KMS("%s\n", __FILE__);
  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. DRM_DEBUG_KMS("%s\n", __func__);
  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. DRM_DEBUG_KMS("%s\n", __func__);
  254. prop = dev_priv->crtc_mode_property;
  255. if (!prop) {
  256. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  257. ARRAY_SIZE(mode_names));
  258. if (!prop)
  259. return;
  260. dev_priv->crtc_mode_property = prop;
  261. }
  262. drm_object_attach_property(&crtc->base, prop, 0);
  263. }
  264. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  265. {
  266. struct exynos_drm_crtc *exynos_crtc;
  267. struct exynos_drm_private *private = dev->dev_private;
  268. struct drm_crtc *crtc;
  269. DRM_DEBUG_KMS("%s\n", __FILE__);
  270. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  271. if (!exynos_crtc) {
  272. DRM_ERROR("failed to allocate exynos crtc\n");
  273. return -ENOMEM;
  274. }
  275. exynos_crtc->pipe = nr;
  276. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  277. exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
  278. if (!exynos_crtc->plane) {
  279. kfree(exynos_crtc);
  280. return -ENOMEM;
  281. }
  282. crtc = &exynos_crtc->drm_crtc;
  283. private->crtc[nr] = crtc;
  284. drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
  285. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  286. exynos_drm_crtc_attach_mode_property(crtc);
  287. return 0;
  288. }
  289. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  290. {
  291. struct exynos_drm_private *private = dev->dev_private;
  292. struct exynos_drm_crtc *exynos_crtc =
  293. to_exynos_crtc(private->crtc[crtc]);
  294. DRM_DEBUG_KMS("%s\n", __FILE__);
  295. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  296. return -EPERM;
  297. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  298. exynos_drm_enable_vblank);
  299. return 0;
  300. }
  301. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  302. {
  303. struct exynos_drm_private *private = dev->dev_private;
  304. struct exynos_drm_crtc *exynos_crtc =
  305. to_exynos_crtc(private->crtc[crtc]);
  306. DRM_DEBUG_KMS("%s\n", __FILE__);
  307. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  308. return;
  309. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  310. exynos_drm_disable_vblank);
  311. }
  312. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc)
  313. {
  314. struct exynos_drm_private *dev_priv = dev->dev_private;
  315. struct drm_pending_vblank_event *e, *t;
  316. struct timeval now;
  317. unsigned long flags;
  318. DRM_DEBUG_KMS("%s\n", __FILE__);
  319. spin_lock_irqsave(&dev->event_lock, flags);
  320. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  321. base.link) {
  322. /* if event's pipe isn't same as crtc then ignore it. */
  323. if (crtc != e->pipe)
  324. continue;
  325. do_gettimeofday(&now);
  326. e->event.sequence = 0;
  327. e->event.tv_sec = now.tv_sec;
  328. e->event.tv_usec = now.tv_usec;
  329. list_move_tail(&e->base.link, &e->base.file_priv->event_list);
  330. wake_up_interruptible(&e->base.file_priv->event_wait);
  331. drm_vblank_put(dev, crtc);
  332. }
  333. spin_unlock_irqrestore(&dev->event_lock, flags);
  334. }