exynos_drm_crtc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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_fb.h"
  32. #include "exynos_drm_encoder.h"
  33. #include "exynos_drm_buf.h"
  34. #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
  35. drm_crtc)
  36. /*
  37. * Exynos specific crtc postion structure.
  38. *
  39. * @fb_x: offset x on a framebuffer to be displyed
  40. * - the unit is screen coordinates.
  41. * @fb_y: offset y on a framebuffer to be displayed
  42. * - the unit is screen coordinates.
  43. * @crtc_x: offset x on hardware screen.
  44. * @crtc_y: offset y on hardware screen.
  45. * @crtc_w: width of hardware screen.
  46. * @crtc_h: height of hardware screen.
  47. */
  48. struct exynos_drm_crtc_pos {
  49. unsigned int fb_x;
  50. unsigned int fb_y;
  51. unsigned int crtc_x;
  52. unsigned int crtc_y;
  53. unsigned int crtc_w;
  54. unsigned int crtc_h;
  55. };
  56. /*
  57. * Exynos specific crtc structure.
  58. *
  59. * @drm_crtc: crtc object.
  60. * @overlay: contain information common to display controller and hdmi and
  61. * contents of this overlay object would be copied to sub driver size.
  62. * @pipe: a crtc index created at load() with a new crtc object creation
  63. * and the crtc object would be set to private->crtc array
  64. * to get a crtc object corresponding to this pipe from private->crtc
  65. * array when irq interrupt occured. the reason of using this pipe is that
  66. * drm framework doesn't support multiple irq yet.
  67. * we can refer to the crtc to current hardware interrupt occured through
  68. * this pipe value.
  69. */
  70. struct exynos_drm_crtc {
  71. struct drm_crtc drm_crtc;
  72. struct exynos_drm_overlay overlay;
  73. unsigned int pipe;
  74. };
  75. static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
  76. {
  77. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  78. struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
  79. exynos_drm_fn_encoder(crtc, overlay,
  80. exynos_drm_encoder_crtc_mode_set);
  81. exynos_drm_fn_encoder(crtc, NULL, exynos_drm_encoder_crtc_commit);
  82. }
  83. static int exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
  84. struct drm_framebuffer *fb,
  85. struct drm_display_mode *mode,
  86. struct exynos_drm_crtc_pos *pos)
  87. {
  88. struct exynos_drm_buf_entry *entry;
  89. unsigned int actual_w;
  90. unsigned int actual_h;
  91. entry = exynos_drm_fb_get_buf(fb);
  92. if (!entry) {
  93. DRM_LOG_KMS("entry is null.\n");
  94. return -EFAULT;
  95. }
  96. overlay->paddr = entry->paddr;
  97. overlay->vaddr = entry->vaddr;
  98. DRM_DEBUG_KMS("vaddr = 0x%lx, paddr = 0x%lx\n",
  99. (unsigned long)overlay->vaddr,
  100. (unsigned long)overlay->paddr);
  101. actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
  102. actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
  103. /* set drm framebuffer data. */
  104. overlay->fb_x = pos->fb_x;
  105. overlay->fb_y = pos->fb_y;
  106. overlay->fb_width = fb->width;
  107. overlay->fb_height = fb->height;
  108. overlay->bpp = fb->bits_per_pixel;
  109. overlay->pitch = fb->pitch;
  110. /* set overlay range to be displayed. */
  111. overlay->crtc_x = pos->crtc_x;
  112. overlay->crtc_y = pos->crtc_y;
  113. overlay->crtc_width = actual_w;
  114. overlay->crtc_height = actual_h;
  115. /* set drm mode data. */
  116. overlay->mode_width = mode->hdisplay;
  117. overlay->mode_height = mode->vdisplay;
  118. overlay->refresh = mode->vrefresh;
  119. overlay->scan_flag = mode->flags;
  120. DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
  121. overlay->crtc_x, overlay->crtc_y,
  122. overlay->crtc_width, overlay->crtc_height);
  123. return 0;
  124. }
  125. static int exynos_drm_crtc_update(struct drm_crtc *crtc)
  126. {
  127. struct exynos_drm_crtc *exynos_crtc;
  128. struct exynos_drm_overlay *overlay;
  129. struct exynos_drm_crtc_pos pos;
  130. struct drm_display_mode *mode = &crtc->mode;
  131. struct drm_framebuffer *fb = crtc->fb;
  132. if (!mode || !fb)
  133. return -EINVAL;
  134. exynos_crtc = to_exynos_crtc(crtc);
  135. overlay = &exynos_crtc->overlay;
  136. memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
  137. /* it means the offset of framebuffer to be displayed. */
  138. pos.fb_x = crtc->x;
  139. pos.fb_y = crtc->y;
  140. /* OSD position to be displayed. */
  141. pos.crtc_x = 0;
  142. pos.crtc_y = 0;
  143. pos.crtc_w = fb->width - crtc->x;
  144. pos.crtc_h = fb->height - crtc->y;
  145. return exynos_drm_overlay_update(overlay, crtc->fb, mode, &pos);
  146. }
  147. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  148. {
  149. DRM_DEBUG_KMS("%s\n", __FILE__);
  150. /* TODO */
  151. }
  152. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  153. {
  154. DRM_DEBUG_KMS("%s\n", __FILE__);
  155. /* drm framework doesn't check NULL. */
  156. }
  157. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  158. {
  159. DRM_DEBUG_KMS("%s\n", __FILE__);
  160. /* drm framework doesn't check NULL. */
  161. }
  162. static bool
  163. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  164. struct drm_display_mode *mode,
  165. struct drm_display_mode *adjusted_mode)
  166. {
  167. DRM_DEBUG_KMS("%s\n", __FILE__);
  168. /* drm framework doesn't check NULL */
  169. return true;
  170. }
  171. static int
  172. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  173. struct drm_display_mode *adjusted_mode, int x, int y,
  174. struct drm_framebuffer *old_fb)
  175. {
  176. DRM_DEBUG_KMS("%s\n", __FILE__);
  177. mode = adjusted_mode;
  178. return exynos_drm_crtc_update(crtc);
  179. }
  180. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  181. struct drm_framebuffer *old_fb)
  182. {
  183. int ret;
  184. DRM_DEBUG_KMS("%s\n", __FILE__);
  185. ret = exynos_drm_crtc_update(crtc);
  186. if (ret)
  187. return ret;
  188. exynos_drm_crtc_apply(crtc);
  189. return ret;
  190. }
  191. static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
  192. {
  193. DRM_DEBUG_KMS("%s\n", __FILE__);
  194. /* drm framework doesn't check NULL */
  195. }
  196. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  197. .dpms = exynos_drm_crtc_dpms,
  198. .prepare = exynos_drm_crtc_prepare,
  199. .commit = exynos_drm_crtc_commit,
  200. .mode_fixup = exynos_drm_crtc_mode_fixup,
  201. .mode_set = exynos_drm_crtc_mode_set,
  202. .mode_set_base = exynos_drm_crtc_mode_set_base,
  203. .load_lut = exynos_drm_crtc_load_lut,
  204. };
  205. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  206. struct drm_framebuffer *fb,
  207. struct drm_pending_vblank_event *event)
  208. {
  209. struct drm_device *dev = crtc->dev;
  210. struct exynos_drm_private *dev_priv = dev->dev_private;
  211. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  212. struct drm_framebuffer *old_fb = crtc->fb;
  213. int ret = -EINVAL;
  214. DRM_DEBUG_KMS("%s\n", __FILE__);
  215. mutex_lock(&dev->struct_mutex);
  216. if (event) {
  217. /*
  218. * the pipe from user always is 0 so we can set pipe number
  219. * of current owner to event.
  220. */
  221. event->pipe = exynos_crtc->pipe;
  222. list_add_tail(&event->base.link,
  223. &dev_priv->pageflip_event_list);
  224. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  225. if (ret) {
  226. DRM_DEBUG("failed to acquire vblank counter\n");
  227. list_del(&event->base.link);
  228. goto out;
  229. }
  230. crtc->fb = fb;
  231. ret = exynos_drm_crtc_update(crtc);
  232. if (ret) {
  233. crtc->fb = old_fb;
  234. drm_vblank_put(dev, exynos_crtc->pipe);
  235. list_del(&event->base.link);
  236. goto out;
  237. }
  238. /*
  239. * the values related to a buffer of the drm framebuffer
  240. * to be applied should be set at here. because these values
  241. * first, are set to shadow registers and then to
  242. * real registers at vsync front porch period.
  243. */
  244. exynos_drm_crtc_apply(crtc);
  245. }
  246. out:
  247. mutex_unlock(&dev->struct_mutex);
  248. return ret;
  249. }
  250. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  251. {
  252. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  253. struct exynos_drm_private *private = crtc->dev->dev_private;
  254. DRM_DEBUG_KMS("%s\n", __FILE__);
  255. private->crtc[exynos_crtc->pipe] = NULL;
  256. drm_crtc_cleanup(crtc);
  257. kfree(exynos_crtc);
  258. }
  259. static struct drm_crtc_funcs exynos_crtc_funcs = {
  260. .set_config = drm_crtc_helper_set_config,
  261. .page_flip = exynos_drm_crtc_page_flip,
  262. .destroy = exynos_drm_crtc_destroy,
  263. };
  264. struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
  265. struct drm_crtc *crtc)
  266. {
  267. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  268. return &exynos_crtc->overlay;
  269. }
  270. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  271. {
  272. struct exynos_drm_crtc *exynos_crtc;
  273. struct exynos_drm_private *private = dev->dev_private;
  274. struct drm_crtc *crtc;
  275. DRM_DEBUG_KMS("%s\n", __FILE__);
  276. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  277. if (!exynos_crtc) {
  278. DRM_ERROR("failed to allocate exynos crtc\n");
  279. return -ENOMEM;
  280. }
  281. exynos_crtc->pipe = nr;
  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. 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. DRM_DEBUG_KMS("%s\n", __FILE__);
  292. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  293. exynos_drm_enable_vblank);
  294. return 0;
  295. }
  296. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  297. {
  298. struct exynos_drm_private *private = dev->dev_private;
  299. DRM_DEBUG_KMS("%s\n", __FILE__);
  300. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  301. exynos_drm_disable_vblank);
  302. }
  303. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  304. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  305. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  306. MODULE_DESCRIPTION("Samsung SoC DRM CRTC Driver");
  307. MODULE_LICENSE("GPL");