exynos_drm_crtc.c 12 KB

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