exynos_drm_crtc.c 10 KB

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