exynos_drm_crtc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include "drmP.h"
  29. #include "drm_crtc_helper.h"
  30. #include "exynos_drm_drv.h"
  31. #include "exynos_drm_encoder.h"
  32. #include "exynos_drm_plane.h"
  33. #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
  34. drm_crtc)
  35. /*
  36. * Exynos specific crtc structure.
  37. *
  38. * @drm_crtc: crtc object.
  39. * @drm_plane: pointer of private plane object for this crtc
  40. * @pipe: a crtc index created at load() with a new crtc object creation
  41. * and the crtc object would be set to private->crtc array
  42. * to get a crtc object corresponding to this pipe from private->crtc
  43. * array when irq interrupt occured. the reason of using this pipe is that
  44. * drm framework doesn't support multiple irq yet.
  45. * we can refer to the crtc to current hardware interrupt occured through
  46. * this pipe value.
  47. * @dpms: store the crtc dpms value
  48. */
  49. struct exynos_drm_crtc {
  50. struct drm_crtc drm_crtc;
  51. struct drm_plane *plane;
  52. unsigned int pipe;
  53. unsigned int dpms;
  54. };
  55. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  56. {
  57. struct drm_device *dev = crtc->dev;
  58. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  59. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  60. if (exynos_crtc->dpms == mode) {
  61. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  62. return;
  63. }
  64. mutex_lock(&dev->struct_mutex);
  65. exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
  66. exynos_crtc->dpms = mode;
  67. mutex_unlock(&dev->struct_mutex);
  68. }
  69. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  70. {
  71. DRM_DEBUG_KMS("%s\n", __FILE__);
  72. /* drm framework doesn't check NULL. */
  73. }
  74. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  75. {
  76. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  77. DRM_DEBUG_KMS("%s\n", __FILE__);
  78. exynos_plane_commit(exynos_crtc->plane);
  79. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
  80. }
  81. static bool
  82. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  83. const struct drm_display_mode *mode,
  84. struct drm_display_mode *adjusted_mode)
  85. {
  86. DRM_DEBUG_KMS("%s\n", __FILE__);
  87. /* drm framework doesn't check NULL */
  88. return true;
  89. }
  90. static int
  91. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  92. struct drm_display_mode *adjusted_mode, int x, int y,
  93. struct drm_framebuffer *old_fb)
  94. {
  95. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  96. struct drm_plane *plane = exynos_crtc->plane;
  97. unsigned int crtc_w;
  98. unsigned int crtc_h;
  99. int pipe = exynos_crtc->pipe;
  100. int ret;
  101. DRM_DEBUG_KMS("%s\n", __FILE__);
  102. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  103. /*
  104. * copy the mode data adjusted by mode_fixup() into crtc->mode
  105. * so that hardware can be seet to proper mode.
  106. */
  107. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  108. crtc_w = crtc->fb->width - x;
  109. crtc_h = crtc->fb->height - y;
  110. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  111. x, y, crtc_w, crtc_h);
  112. if (ret)
  113. return ret;
  114. plane->crtc = crtc;
  115. plane->fb = crtc->fb;
  116. exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
  117. return 0;
  118. }
  119. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  120. struct drm_framebuffer *old_fb)
  121. {
  122. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  123. struct drm_plane *plane = exynos_crtc->plane;
  124. unsigned int crtc_w;
  125. unsigned int crtc_h;
  126. int ret;
  127. DRM_DEBUG_KMS("%s\n", __FILE__);
  128. crtc_w = crtc->fb->width - x;
  129. crtc_h = crtc->fb->height - y;
  130. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  131. x, y, crtc_w, crtc_h);
  132. if (ret)
  133. return ret;
  134. exynos_drm_crtc_commit(crtc);
  135. return 0;
  136. }
  137. static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
  138. {
  139. DRM_DEBUG_KMS("%s\n", __FILE__);
  140. /* drm framework doesn't check NULL */
  141. }
  142. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  143. {
  144. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  145. DRM_DEBUG_KMS("%s\n", __FILE__);
  146. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
  147. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  148. }
  149. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  150. .dpms = exynos_drm_crtc_dpms,
  151. .prepare = exynos_drm_crtc_prepare,
  152. .commit = exynos_drm_crtc_commit,
  153. .mode_fixup = exynos_drm_crtc_mode_fixup,
  154. .mode_set = exynos_drm_crtc_mode_set,
  155. .mode_set_base = exynos_drm_crtc_mode_set_base,
  156. .load_lut = exynos_drm_crtc_load_lut,
  157. .disable = exynos_drm_crtc_disable,
  158. };
  159. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  160. struct drm_framebuffer *fb,
  161. struct drm_pending_vblank_event *event)
  162. {
  163. struct drm_device *dev = crtc->dev;
  164. struct exynos_drm_private *dev_priv = dev->dev_private;
  165. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  166. struct drm_framebuffer *old_fb = crtc->fb;
  167. int ret = -EINVAL;
  168. DRM_DEBUG_KMS("%s\n", __FILE__);
  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. list_del(&event->base.link);
  180. goto out;
  181. }
  182. list_add_tail(&event->base.link,
  183. &dev_priv->pageflip_event_list);
  184. crtc->fb = fb;
  185. ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
  186. NULL);
  187. if (ret) {
  188. crtc->fb = old_fb;
  189. drm_vblank_put(dev, exynos_crtc->pipe);
  190. list_del(&event->base.link);
  191. goto out;
  192. }
  193. }
  194. out:
  195. mutex_unlock(&dev->struct_mutex);
  196. return ret;
  197. }
  198. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  199. {
  200. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  201. struct exynos_drm_private *private = crtc->dev->dev_private;
  202. DRM_DEBUG_KMS("%s\n", __FILE__);
  203. private->crtc[exynos_crtc->pipe] = NULL;
  204. drm_crtc_cleanup(crtc);
  205. kfree(exynos_crtc);
  206. }
  207. static struct drm_crtc_funcs exynos_crtc_funcs = {
  208. .set_config = drm_crtc_helper_set_config,
  209. .page_flip = exynos_drm_crtc_page_flip,
  210. .destroy = exynos_drm_crtc_destroy,
  211. };
  212. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  213. {
  214. struct exynos_drm_crtc *exynos_crtc;
  215. struct exynos_drm_private *private = dev->dev_private;
  216. struct drm_crtc *crtc;
  217. DRM_DEBUG_KMS("%s\n", __FILE__);
  218. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  219. if (!exynos_crtc) {
  220. DRM_ERROR("failed to allocate exynos crtc\n");
  221. return -ENOMEM;
  222. }
  223. exynos_crtc->pipe = nr;
  224. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  225. exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
  226. if (!exynos_crtc->plane) {
  227. kfree(exynos_crtc);
  228. return -ENOMEM;
  229. }
  230. crtc = &exynos_crtc->drm_crtc;
  231. private->crtc[nr] = crtc;
  232. drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
  233. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  234. return 0;
  235. }
  236. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  237. {
  238. struct exynos_drm_private *private = dev->dev_private;
  239. struct exynos_drm_crtc *exynos_crtc =
  240. to_exynos_crtc(private->crtc[crtc]);
  241. DRM_DEBUG_KMS("%s\n", __FILE__);
  242. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  243. return -EPERM;
  244. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  245. exynos_drm_enable_vblank);
  246. return 0;
  247. }
  248. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  249. {
  250. struct exynos_drm_private *private = dev->dev_private;
  251. struct exynos_drm_crtc *exynos_crtc =
  252. to_exynos_crtc(private->crtc[crtc]);
  253. DRM_DEBUG_KMS("%s\n", __FILE__);
  254. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  255. return;
  256. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  257. exynos_drm_disable_vblank);
  258. }