exynos_drm_crtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <drm/drmP.h>
  29. #include <drm/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. enum exynos_crtc_mode {
  36. CRTC_MODE_NORMAL, /* normal mode */
  37. CRTC_MODE_BLANK, /* The private plane of crtc is blank */
  38. };
  39. /*
  40. * Exynos specific crtc structure.
  41. *
  42. * @drm_crtc: crtc object.
  43. * @drm_plane: pointer of private plane object for this crtc
  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. * @dpms: store the crtc dpms value
  52. * @mode: store the crtc mode value
  53. */
  54. struct exynos_drm_crtc {
  55. struct drm_crtc drm_crtc;
  56. struct drm_plane *plane;
  57. unsigned int pipe;
  58. unsigned int dpms;
  59. enum exynos_crtc_mode mode;
  60. };
  61. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  62. {
  63. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  64. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  65. if (exynos_crtc->dpms == mode) {
  66. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  67. return;
  68. }
  69. exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
  70. exynos_crtc->dpms = mode;
  71. }
  72. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  73. {
  74. DRM_DEBUG_KMS("%s\n", __FILE__);
  75. /* drm framework doesn't check NULL. */
  76. }
  77. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  78. {
  79. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  80. DRM_DEBUG_KMS("%s\n", __FILE__);
  81. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  82. exynos_plane_commit(exynos_crtc->plane);
  83. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
  84. }
  85. static bool
  86. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  87. const struct drm_display_mode *mode,
  88. struct drm_display_mode *adjusted_mode)
  89. {
  90. DRM_DEBUG_KMS("%s\n", __FILE__);
  91. /* drm framework doesn't check NULL */
  92. return true;
  93. }
  94. static int
  95. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  96. struct drm_display_mode *adjusted_mode, int x, int y,
  97. struct drm_framebuffer *old_fb)
  98. {
  99. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  100. struct drm_plane *plane = exynos_crtc->plane;
  101. unsigned int crtc_w;
  102. unsigned int crtc_h;
  103. int pipe = exynos_crtc->pipe;
  104. int ret;
  105. DRM_DEBUG_KMS("%s\n", __FILE__);
  106. /*
  107. * copy the mode data adjusted by mode_fixup() into crtc->mode
  108. * so that hardware can be seet to proper mode.
  109. */
  110. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  111. crtc_w = crtc->fb->width - x;
  112. crtc_h = crtc->fb->height - y;
  113. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  114. x, y, crtc_w, crtc_h);
  115. if (ret)
  116. return ret;
  117. plane->crtc = crtc;
  118. plane->fb = crtc->fb;
  119. exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
  120. return 0;
  121. }
  122. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  123. struct drm_framebuffer *old_fb)
  124. {
  125. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  126. struct drm_plane *plane = exynos_crtc->plane;
  127. unsigned int crtc_w;
  128. unsigned int crtc_h;
  129. int ret;
  130. DRM_DEBUG_KMS("%s\n", __FILE__);
  131. /* when framebuffer changing is requested, crtc's dpms should be on */
  132. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  133. DRM_ERROR("failed framebuffer changing request.\n");
  134. return -EPERM;
  135. }
  136. crtc_w = crtc->fb->width - x;
  137. crtc_h = crtc->fb->height - y;
  138. ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
  139. x, y, crtc_w, crtc_h);
  140. if (ret)
  141. return ret;
  142. exynos_drm_crtc_commit(crtc);
  143. return 0;
  144. }
  145. static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
  146. {
  147. DRM_DEBUG_KMS("%s\n", __FILE__);
  148. /* drm framework doesn't check NULL */
  149. }
  150. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  151. {
  152. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  153. DRM_DEBUG_KMS("%s\n", __FILE__);
  154. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
  155. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  156. }
  157. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  158. .dpms = exynos_drm_crtc_dpms,
  159. .prepare = exynos_drm_crtc_prepare,
  160. .commit = exynos_drm_crtc_commit,
  161. .mode_fixup = exynos_drm_crtc_mode_fixup,
  162. .mode_set = exynos_drm_crtc_mode_set,
  163. .mode_set_base = exynos_drm_crtc_mode_set_base,
  164. .load_lut = exynos_drm_crtc_load_lut,
  165. .disable = exynos_drm_crtc_disable,
  166. };
  167. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  168. struct drm_framebuffer *fb,
  169. struct drm_pending_vblank_event *event)
  170. {
  171. struct drm_device *dev = crtc->dev;
  172. struct exynos_drm_private *dev_priv = dev->dev_private;
  173. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  174. struct drm_framebuffer *old_fb = crtc->fb;
  175. int ret = -EINVAL;
  176. DRM_DEBUG_KMS("%s\n", __FILE__);
  177. /* when the page flip is requested, crtc's dpms should be on */
  178. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  179. DRM_ERROR("failed page flip request.\n");
  180. return -EINVAL;
  181. }
  182. mutex_lock(&dev->struct_mutex);
  183. if (event) {
  184. /*
  185. * the pipe from user always is 0 so we can set pipe number
  186. * of current owner to event.
  187. */
  188. event->pipe = exynos_crtc->pipe;
  189. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  190. if (ret) {
  191. DRM_DEBUG("failed to acquire vblank counter\n");
  192. list_del(&event->base.link);
  193. goto out;
  194. }
  195. list_add_tail(&event->base.link,
  196. &dev_priv->pageflip_event_list);
  197. crtc->fb = fb;
  198. ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
  199. NULL);
  200. if (ret) {
  201. crtc->fb = old_fb;
  202. drm_vblank_put(dev, exynos_crtc->pipe);
  203. list_del(&event->base.link);
  204. goto out;
  205. }
  206. }
  207. out:
  208. mutex_unlock(&dev->struct_mutex);
  209. return ret;
  210. }
  211. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  212. {
  213. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  214. struct exynos_drm_private *private = crtc->dev->dev_private;
  215. DRM_DEBUG_KMS("%s\n", __FILE__);
  216. private->crtc[exynos_crtc->pipe] = NULL;
  217. drm_crtc_cleanup(crtc);
  218. kfree(exynos_crtc);
  219. }
  220. static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
  221. struct drm_property *property,
  222. uint64_t val)
  223. {
  224. struct drm_device *dev = crtc->dev;
  225. struct exynos_drm_private *dev_priv = dev->dev_private;
  226. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  227. DRM_DEBUG_KMS("%s\n", __func__);
  228. if (property == dev_priv->crtc_mode_property) {
  229. enum exynos_crtc_mode mode = val;
  230. if (mode == exynos_crtc->mode)
  231. return 0;
  232. exynos_crtc->mode = mode;
  233. switch (mode) {
  234. case CRTC_MODE_NORMAL:
  235. exynos_drm_crtc_commit(crtc);
  236. break;
  237. case CRTC_MODE_BLANK:
  238. exynos_plane_dpms(exynos_crtc->plane,
  239. DRM_MODE_DPMS_OFF);
  240. break;
  241. default:
  242. break;
  243. }
  244. return 0;
  245. }
  246. return -EINVAL;
  247. }
  248. static struct drm_crtc_funcs exynos_crtc_funcs = {
  249. .set_config = drm_crtc_helper_set_config,
  250. .page_flip = exynos_drm_crtc_page_flip,
  251. .destroy = exynos_drm_crtc_destroy,
  252. .set_property = exynos_drm_crtc_set_property,
  253. };
  254. static const struct drm_prop_enum_list mode_names[] = {
  255. { CRTC_MODE_NORMAL, "normal" },
  256. { CRTC_MODE_BLANK, "blank" },
  257. };
  258. static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
  259. {
  260. struct drm_device *dev = crtc->dev;
  261. struct exynos_drm_private *dev_priv = dev->dev_private;
  262. struct drm_property *prop;
  263. DRM_DEBUG_KMS("%s\n", __func__);
  264. prop = dev_priv->crtc_mode_property;
  265. if (!prop) {
  266. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  267. ARRAY_SIZE(mode_names));
  268. if (!prop)
  269. return;
  270. dev_priv->crtc_mode_property = prop;
  271. }
  272. drm_object_attach_property(&crtc->base, prop, 0);
  273. }
  274. int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
  275. {
  276. struct exynos_drm_crtc *exynos_crtc;
  277. struct exynos_drm_private *private = dev->dev_private;
  278. struct drm_crtc *crtc;
  279. DRM_DEBUG_KMS("%s\n", __FILE__);
  280. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  281. if (!exynos_crtc) {
  282. DRM_ERROR("failed to allocate exynos crtc\n");
  283. return -ENOMEM;
  284. }
  285. exynos_crtc->pipe = nr;
  286. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  287. exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
  288. if (!exynos_crtc->plane) {
  289. kfree(exynos_crtc);
  290. return -ENOMEM;
  291. }
  292. crtc = &exynos_crtc->drm_crtc;
  293. private->crtc[nr] = crtc;
  294. drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
  295. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  296. exynos_drm_crtc_attach_mode_property(crtc);
  297. return 0;
  298. }
  299. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
  300. {
  301. struct exynos_drm_private *private = dev->dev_private;
  302. struct exynos_drm_crtc *exynos_crtc =
  303. to_exynos_crtc(private->crtc[crtc]);
  304. DRM_DEBUG_KMS("%s\n", __FILE__);
  305. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  306. return -EPERM;
  307. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  308. exynos_drm_enable_vblank);
  309. return 0;
  310. }
  311. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
  312. {
  313. struct exynos_drm_private *private = dev->dev_private;
  314. struct exynos_drm_crtc *exynos_crtc =
  315. to_exynos_crtc(private->crtc[crtc]);
  316. DRM_DEBUG_KMS("%s\n", __FILE__);
  317. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  318. return;
  319. exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
  320. exynos_drm_disable_vblank);
  321. }