exynos_drm_crtc.c 10 KB

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