exynos_drm_crtc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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->src_width = pos->src_w;
  97. overlay->src_height = pos->src_h;
  98. overlay->bpp = fb->bits_per_pixel;
  99. overlay->pitch = fb->pitches[0];
  100. overlay->pixel_format = fb->pixel_format;
  101. /* set overlay range to be displayed. */
  102. overlay->crtc_x = pos->crtc_x;
  103. overlay->crtc_y = pos->crtc_y;
  104. overlay->crtc_width = actual_w;
  105. overlay->crtc_height = actual_h;
  106. /* set drm mode data. */
  107. overlay->mode_width = mode->hdisplay;
  108. overlay->mode_height = mode->vdisplay;
  109. overlay->refresh = mode->vrefresh;
  110. overlay->scan_flag = mode->flags;
  111. DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
  112. overlay->crtc_x, overlay->crtc_y,
  113. overlay->crtc_width, overlay->crtc_height);
  114. return 0;
  115. }
  116. static int exynos_drm_crtc_update(struct drm_crtc *crtc)
  117. {
  118. struct exynos_drm_crtc *exynos_crtc;
  119. struct exynos_drm_overlay *overlay;
  120. struct exynos_drm_crtc_pos pos;
  121. struct drm_display_mode *mode = &crtc->mode;
  122. struct drm_framebuffer *fb = crtc->fb;
  123. if (!mode || !fb)
  124. return -EINVAL;
  125. exynos_crtc = to_exynos_crtc(crtc);
  126. overlay = &exynos_crtc->overlay;
  127. memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
  128. /* it means the offset of framebuffer to be displayed. */
  129. pos.fb_x = crtc->x;
  130. pos.fb_y = crtc->y;
  131. /* OSD position to be displayed. */
  132. pos.crtc_x = 0;
  133. pos.crtc_y = 0;
  134. pos.crtc_w = fb->width - crtc->x;
  135. pos.crtc_h = fb->height - crtc->y;
  136. pos.src_w = pos.crtc_w;
  137. pos.src_h = pos.crtc_h;
  138. return exynos_drm_overlay_update(overlay, crtc->fb, mode, &pos);
  139. }
  140. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  141. {
  142. struct drm_device *dev = crtc->dev;
  143. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  144. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  145. if (exynos_crtc->dpms == mode) {
  146. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  147. return;
  148. }
  149. mutex_lock(&dev->struct_mutex);
  150. switch (mode) {
  151. case DRM_MODE_DPMS_ON:
  152. exynos_drm_fn_encoder(crtc, &mode,
  153. exynos_drm_encoder_crtc_dpms);
  154. exynos_crtc->dpms = mode;
  155. break;
  156. case DRM_MODE_DPMS_STANDBY:
  157. case DRM_MODE_DPMS_SUSPEND:
  158. case DRM_MODE_DPMS_OFF:
  159. exynos_drm_fn_encoder(crtc, &mode,
  160. exynos_drm_encoder_crtc_dpms);
  161. exynos_crtc->dpms = mode;
  162. break;
  163. default:
  164. DRM_ERROR("unspecified mode %d\n", mode);
  165. break;
  166. }
  167. mutex_unlock(&dev->struct_mutex);
  168. }
  169. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  170. {
  171. DRM_DEBUG_KMS("%s\n", __FILE__);
  172. /* drm framework doesn't check NULL. */
  173. }
  174. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  175. {
  176. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  177. DRM_DEBUG_KMS("%s\n", __FILE__);
  178. /*
  179. * when set_crtc is requested from user or at booting time,
  180. * crtc->commit would be called without dpms call so if dpms is
  181. * no power on then crtc->dpms should be called
  182. * with DRM_MODE_DPMS_ON for the hardware power to be on.
  183. */
  184. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) {
  185. int mode = DRM_MODE_DPMS_ON;
  186. /*
  187. * enable hardware(power on) to all encoders hdmi connected
  188. * to current crtc.
  189. */
  190. exynos_drm_crtc_dpms(crtc, mode);
  191. /*
  192. * enable dma to all encoders connected to current crtc and
  193. * lcd panel.
  194. */
  195. exynos_drm_fn_encoder(crtc, &mode,
  196. exynos_drm_encoder_dpms_from_crtc);
  197. }
  198. exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
  199. exynos_drm_encoder_crtc_commit);
  200. }
  201. static bool
  202. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  203. struct drm_display_mode *mode,
  204. struct drm_display_mode *adjusted_mode)
  205. {
  206. DRM_DEBUG_KMS("%s\n", __FILE__);
  207. /* drm framework doesn't check NULL */
  208. return true;
  209. }
  210. static int
  211. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  212. struct drm_display_mode *adjusted_mode, int x, int y,
  213. struct drm_framebuffer *old_fb)
  214. {
  215. DRM_DEBUG_KMS("%s\n", __FILE__);
  216. /*
  217. * copy the mode data adjusted by mode_fixup() into crtc->mode
  218. * so that hardware can be seet to proper mode.
  219. */
  220. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  221. return exynos_drm_crtc_update(crtc);
  222. }
  223. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  224. struct drm_framebuffer *old_fb)
  225. {
  226. int ret;
  227. DRM_DEBUG_KMS("%s\n", __FILE__);
  228. ret = exynos_drm_crtc_update(crtc);
  229. if (ret)
  230. return ret;
  231. exynos_drm_crtc_apply(crtc);
  232. return ret;
  233. }
  234. static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
  235. {
  236. DRM_DEBUG_KMS("%s\n", __FILE__);
  237. /* drm framework doesn't check NULL */
  238. }
  239. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  240. .dpms = exynos_drm_crtc_dpms,
  241. .prepare = exynos_drm_crtc_prepare,
  242. .commit = exynos_drm_crtc_commit,
  243. .mode_fixup = exynos_drm_crtc_mode_fixup,
  244. .mode_set = exynos_drm_crtc_mode_set,
  245. .mode_set_base = exynos_drm_crtc_mode_set_base,
  246. .load_lut = exynos_drm_crtc_load_lut,
  247. };
  248. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  249. struct drm_framebuffer *fb,
  250. struct drm_pending_vblank_event *event)
  251. {
  252. struct drm_device *dev = crtc->dev;
  253. struct exynos_drm_private *dev_priv = dev->dev_private;
  254. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  255. struct drm_framebuffer *old_fb = crtc->fb;
  256. int ret = -EINVAL;
  257. DRM_DEBUG_KMS("%s\n", __FILE__);
  258. mutex_lock(&dev->struct_mutex);
  259. if (event) {
  260. /*
  261. * the pipe from user always is 0 so we can set pipe number
  262. * of current owner to event.
  263. */
  264. event->pipe = exynos_crtc->pipe;
  265. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  266. if (ret) {
  267. DRM_DEBUG("failed to acquire vblank counter\n");
  268. list_del(&event->base.link);
  269. goto out;
  270. }
  271. list_add_tail(&event->base.link,
  272. &dev_priv->pageflip_event_list);
  273. crtc->fb = fb;
  274. ret = exynos_drm_crtc_update(crtc);
  275. if (ret) {
  276. crtc->fb = old_fb;
  277. drm_vblank_put(dev, exynos_crtc->pipe);
  278. list_del(&event->base.link);
  279. goto out;
  280. }
  281. /*
  282. * the values related to a buffer of the drm framebuffer
  283. * to be applied should be set at here. because these values
  284. * first, are set to shadow registers and then to
  285. * real registers at vsync front porch period.
  286. */
  287. exynos_drm_crtc_apply(crtc);
  288. }
  289. out:
  290. mutex_unlock(&dev->struct_mutex);
  291. return ret;
  292. }
  293. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  294. {
  295. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  296. struct exynos_drm_private *private = crtc->dev->dev_private;
  297. DRM_DEBUG_KMS("%s\n", __FILE__);
  298. private->crtc[exynos_crtc->pipe] = NULL;
  299. drm_crtc_cleanup(crtc);
  300. kfree(exynos_crtc);
  301. }
  302. static struct drm_crtc_funcs exynos_crtc_funcs = {
  303. .set_config = drm_crtc_helper_set_config,
  304. .page_flip = exynos_drm_crtc_page_flip,
  305. .destroy = exynos_drm_crtc_destroy,
  306. };
  307. struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
  308. struct drm_crtc *crtc)
  309. {
  310. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  311. return &exynos_crtc->overlay;
  312. }
  313. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  314. {
  315. struct exynos_drm_crtc *exynos_crtc;
  316. struct exynos_drm_private *private = dev->dev_private;
  317. struct drm_crtc *crtc;
  318. DRM_DEBUG_KMS("%s\n", __FILE__);
  319. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  320. if (!exynos_crtc) {
  321. DRM_ERROR("failed to allocate exynos crtc\n");
  322. return -ENOMEM;
  323. }
  324. exynos_crtc->pipe = nr;
  325. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  326. exynos_crtc->overlay.zpos = DEFAULT_ZPOS;
  327. crtc = &exynos_crtc->drm_crtc;
  328. private->crtc[nr] = crtc;
  329. drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
  330. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  331. return 0;
  332. }
  333. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  334. {
  335. struct exynos_drm_private *private = dev->dev_private;
  336. struct exynos_drm_crtc *exynos_crtc =
  337. to_exynos_crtc(private->crtc[crtc]);
  338. DRM_DEBUG_KMS("%s\n", __FILE__);
  339. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  340. return -EPERM;
  341. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  342. exynos_drm_enable_vblank);
  343. return 0;
  344. }
  345. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  346. {
  347. struct exynos_drm_private *private = dev->dev_private;
  348. struct exynos_drm_crtc *exynos_crtc =
  349. to_exynos_crtc(private->crtc[crtc]);
  350. DRM_DEBUG_KMS("%s\n", __FILE__);
  351. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  352. return;
  353. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  354. exynos_drm_disable_vblank);
  355. }