exynos_drm_crtc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "exynos_drm_crtc.h"
  17. #include "exynos_drm_drv.h"
  18. #include "exynos_drm_encoder.h"
  19. #include "exynos_drm_plane.h"
  20. #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
  21. drm_crtc)
  22. enum exynos_crtc_mode {
  23. CRTC_MODE_NORMAL, /* normal mode */
  24. CRTC_MODE_BLANK, /* The private plane of crtc is blank */
  25. };
  26. /*
  27. * Exynos specific crtc structure.
  28. *
  29. * @drm_crtc: crtc object.
  30. * @manager: the manager associated with this crtc
  31. * @pipe: a crtc index created at load() with a new crtc object creation
  32. * and the crtc object would be set to private->crtc array
  33. * to get a crtc object corresponding to this pipe from private->crtc
  34. * array when irq interrupt occurred. the reason of using this pipe is that
  35. * drm framework doesn't support multiple irq yet.
  36. * we can refer to the crtc to current hardware interrupt occurred through
  37. * this pipe value.
  38. * @dpms: store the crtc dpms value
  39. * @mode: store the crtc mode value
  40. */
  41. struct exynos_drm_crtc {
  42. struct drm_crtc drm_crtc;
  43. struct exynos_drm_manager *manager;
  44. unsigned int pipe;
  45. unsigned int dpms;
  46. enum exynos_crtc_mode mode;
  47. wait_queue_head_t pending_flip_queue;
  48. atomic_t pending_flip;
  49. };
  50. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  51. {
  52. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  53. struct exynos_drm_manager *manager = exynos_crtc->manager;
  54. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  55. if (exynos_crtc->dpms == mode) {
  56. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  57. return;
  58. }
  59. if (mode > DRM_MODE_DPMS_ON) {
  60. /* wait for the completion of page flip. */
  61. if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
  62. !atomic_read(&exynos_crtc->pending_flip),
  63. HZ/20))
  64. atomic_set(&exynos_crtc->pending_flip, 0);
  65. drm_vblank_off(crtc->dev, exynos_crtc->pipe);
  66. }
  67. if (manager->ops->dpms)
  68. manager->ops->dpms(manager, mode);
  69. exynos_crtc->dpms = mode;
  70. }
  71. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  72. {
  73. /* drm framework doesn't check NULL. */
  74. }
  75. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  76. {
  77. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  78. struct exynos_drm_manager *manager = exynos_crtc->manager;
  79. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  80. exynos_plane_commit(crtc->primary);
  81. if (manager->ops->commit)
  82. manager->ops->commit(manager);
  83. exynos_plane_dpms(crtc->primary, 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. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  91. struct exynos_drm_manager *manager = exynos_crtc->manager;
  92. if (manager->ops->mode_fixup)
  93. return manager->ops->mode_fixup(manager, mode, adjusted_mode);
  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 exynos_drm_manager *manager = exynos_crtc->manager;
  103. struct drm_framebuffer *fb = crtc->primary->fb;
  104. unsigned int crtc_w;
  105. unsigned int crtc_h;
  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 = fb->width - x;
  112. crtc_h = fb->height - y;
  113. if (manager->ops->mode_set)
  114. manager->ops->mode_set(manager, &crtc->mode);
  115. return exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
  116. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  117. }
  118. static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
  119. struct drm_framebuffer *old_fb)
  120. {
  121. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  122. struct drm_framebuffer *fb = crtc->primary->fb;
  123. unsigned int crtc_w;
  124. unsigned int crtc_h;
  125. int ret;
  126. /* when framebuffer changing is requested, crtc's dpms should be on */
  127. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  128. DRM_ERROR("failed framebuffer changing request.\n");
  129. return -EPERM;
  130. }
  131. crtc_w = fb->width - x;
  132. crtc_h = fb->height - y;
  133. ret = exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0,
  134. crtc_w, crtc_h, x, y, crtc_w, crtc_h);
  135. if (ret)
  136. return ret;
  137. exynos_drm_crtc_commit(crtc);
  138. return 0;
  139. }
  140. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  141. struct drm_framebuffer *old_fb)
  142. {
  143. return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
  144. }
  145. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  146. {
  147. struct drm_plane *plane;
  148. int ret;
  149. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  150. drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
  151. if (plane->crtc != crtc)
  152. continue;
  153. ret = plane->funcs->disable_plane(plane);
  154. if (ret)
  155. DRM_ERROR("Failed to disable plane %d\n", ret);
  156. }
  157. }
  158. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  159. .dpms = exynos_drm_crtc_dpms,
  160. .prepare = exynos_drm_crtc_prepare,
  161. .commit = exynos_drm_crtc_commit,
  162. .mode_fixup = exynos_drm_crtc_mode_fixup,
  163. .mode_set = exynos_drm_crtc_mode_set,
  164. .mode_set_base = exynos_drm_crtc_mode_set_base,
  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. uint32_t page_flip_flags)
  171. {
  172. struct drm_device *dev = crtc->dev;
  173. struct exynos_drm_private *dev_priv = dev->dev_private;
  174. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  175. struct drm_framebuffer *old_fb = crtc->primary->fb;
  176. int ret = -EINVAL;
  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. goto out;
  193. }
  194. spin_lock_irq(&dev->event_lock);
  195. list_add_tail(&event->base.link,
  196. &dev_priv->pageflip_event_list);
  197. atomic_set(&exynos_crtc->pending_flip, 1);
  198. spin_unlock_irq(&dev->event_lock);
  199. crtc->primary->fb = fb;
  200. ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
  201. NULL);
  202. if (ret) {
  203. crtc->primary->fb = old_fb;
  204. spin_lock_irq(&dev->event_lock);
  205. drm_vblank_put(dev, exynos_crtc->pipe);
  206. list_del(&event->base.link);
  207. atomic_set(&exynos_crtc->pending_flip, 0);
  208. spin_unlock_irq(&dev->event_lock);
  209. goto out;
  210. }
  211. }
  212. out:
  213. mutex_unlock(&dev->struct_mutex);
  214. return ret;
  215. }
  216. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  217. {
  218. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  219. struct exynos_drm_private *private = crtc->dev->dev_private;
  220. private->crtc[exynos_crtc->pipe] = NULL;
  221. drm_crtc_cleanup(crtc);
  222. kfree(exynos_crtc);
  223. }
  224. static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
  225. struct drm_property *property,
  226. uint64_t val)
  227. {
  228. struct drm_device *dev = crtc->dev;
  229. struct exynos_drm_private *dev_priv = dev->dev_private;
  230. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  231. if (property == dev_priv->crtc_mode_property) {
  232. enum exynos_crtc_mode mode = val;
  233. if (mode == exynos_crtc->mode)
  234. return 0;
  235. exynos_crtc->mode = mode;
  236. switch (mode) {
  237. case CRTC_MODE_NORMAL:
  238. exynos_drm_crtc_commit(crtc);
  239. break;
  240. case CRTC_MODE_BLANK:
  241. exynos_plane_dpms(crtc->primary, DRM_MODE_DPMS_OFF);
  242. break;
  243. default:
  244. break;
  245. }
  246. return 0;
  247. }
  248. return -EINVAL;
  249. }
  250. static struct drm_crtc_funcs exynos_crtc_funcs = {
  251. .set_config = drm_crtc_helper_set_config,
  252. .page_flip = exynos_drm_crtc_page_flip,
  253. .destroy = exynos_drm_crtc_destroy,
  254. .set_property = exynos_drm_crtc_set_property,
  255. };
  256. static const struct drm_prop_enum_list mode_names[] = {
  257. { CRTC_MODE_NORMAL, "normal" },
  258. { CRTC_MODE_BLANK, "blank" },
  259. };
  260. static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
  261. {
  262. struct drm_device *dev = crtc->dev;
  263. struct exynos_drm_private *dev_priv = dev->dev_private;
  264. struct drm_property *prop;
  265. prop = dev_priv->crtc_mode_property;
  266. if (!prop) {
  267. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  268. ARRAY_SIZE(mode_names));
  269. if (!prop)
  270. return;
  271. dev_priv->crtc_mode_property = prop;
  272. }
  273. drm_object_attach_property(&crtc->base, prop, 0);
  274. }
  275. int exynos_drm_crtc_create(struct exynos_drm_manager *manager)
  276. {
  277. struct exynos_drm_crtc *exynos_crtc;
  278. struct drm_plane *plane;
  279. struct exynos_drm_private *private = manager->drm_dev->dev_private;
  280. struct drm_crtc *crtc;
  281. int ret;
  282. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  283. if (!exynos_crtc)
  284. return -ENOMEM;
  285. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  286. atomic_set(&exynos_crtc->pending_flip, 0);
  287. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  288. exynos_crtc->manager = manager;
  289. exynos_crtc->pipe = manager->pipe;
  290. plane = exynos_plane_init(manager->drm_dev, 1 << manager->pipe,
  291. DRM_PLANE_TYPE_PRIMARY);
  292. if (IS_ERR(plane)) {
  293. ret = PTR_ERR(plane);
  294. goto err_plane;
  295. }
  296. manager->crtc = &exynos_crtc->drm_crtc;
  297. crtc = &exynos_crtc->drm_crtc;
  298. private->crtc[manager->pipe] = crtc;
  299. ret = drm_crtc_init_with_planes(manager->drm_dev, crtc, plane, NULL,
  300. &exynos_crtc_funcs);
  301. if (ret < 0)
  302. goto err_crtc;
  303. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  304. exynos_drm_crtc_attach_mode_property(crtc);
  305. return 0;
  306. err_crtc:
  307. plane->funcs->destroy(plane);
  308. err_plane:
  309. kfree(exynos_crtc);
  310. return ret;
  311. }
  312. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  313. {
  314. struct exynos_drm_private *private = dev->dev_private;
  315. struct exynos_drm_crtc *exynos_crtc =
  316. to_exynos_crtc(private->crtc[pipe]);
  317. struct exynos_drm_manager *manager = exynos_crtc->manager;
  318. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  319. return -EPERM;
  320. if (manager->ops->enable_vblank)
  321. manager->ops->enable_vblank(manager);
  322. return 0;
  323. }
  324. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  325. {
  326. struct exynos_drm_private *private = dev->dev_private;
  327. struct exynos_drm_crtc *exynos_crtc =
  328. to_exynos_crtc(private->crtc[pipe]);
  329. struct exynos_drm_manager *manager = exynos_crtc->manager;
  330. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  331. return;
  332. if (manager->ops->disable_vblank)
  333. manager->ops->disable_vblank(manager);
  334. }
  335. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
  336. {
  337. struct exynos_drm_private *dev_priv = dev->dev_private;
  338. struct drm_pending_vblank_event *e, *t;
  339. struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
  340. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  341. unsigned long flags;
  342. spin_lock_irqsave(&dev->event_lock, flags);
  343. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  344. base.link) {
  345. /* if event's pipe isn't same as crtc then ignore it. */
  346. if (pipe != e->pipe)
  347. continue;
  348. list_del(&e->base.link);
  349. drm_send_vblank_event(dev, -1, e);
  350. drm_vblank_put(dev, pipe);
  351. atomic_set(&exynos_crtc->pending_flip, 0);
  352. wake_up(&exynos_crtc->pending_flip_queue);
  353. }
  354. spin_unlock_irqrestore(&dev->event_lock, flags);
  355. }
  356. void exynos_drm_crtc_plane_mode_set(struct drm_crtc *crtc,
  357. struct exynos_drm_overlay *overlay)
  358. {
  359. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  360. if (manager->ops->win_mode_set)
  361. manager->ops->win_mode_set(manager, overlay);
  362. }
  363. void exynos_drm_crtc_plane_commit(struct drm_crtc *crtc, int zpos)
  364. {
  365. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  366. if (manager->ops->win_commit)
  367. manager->ops->win_commit(manager, zpos);
  368. }
  369. void exynos_drm_crtc_plane_enable(struct drm_crtc *crtc, int zpos)
  370. {
  371. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  372. if (manager->ops->win_enable)
  373. manager->ops->win_enable(manager, zpos);
  374. }
  375. void exynos_drm_crtc_plane_disable(struct drm_crtc *crtc, int zpos)
  376. {
  377. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  378. if (manager->ops->win_disable)
  379. manager->ops->win_disable(manager, zpos);
  380. }
  381. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  382. {
  383. struct exynos_drm_manager *manager;
  384. struct drm_device *dev = fb->dev;
  385. struct drm_crtc *crtc;
  386. /*
  387. * make sure that overlay data are updated to real hardware
  388. * for all encoders.
  389. */
  390. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  391. manager = to_exynos_crtc(crtc)->manager;
  392. /*
  393. * wait for vblank interrupt
  394. * - this makes sure that overlay data are updated to
  395. * real hardware.
  396. */
  397. if (manager->ops->wait_for_vblank)
  398. manager->ops->wait_for_vblank(manager);
  399. }
  400. }
  401. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  402. unsigned int out_type)
  403. {
  404. struct drm_crtc *crtc;
  405. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  406. struct exynos_drm_crtc *exynos_crtc;
  407. exynos_crtc = to_exynos_crtc(crtc);
  408. if (exynos_crtc->manager->type == out_type)
  409. return exynos_crtc->manager->pipe;
  410. }
  411. return -EPERM;
  412. }
  413. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  414. {
  415. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  416. if (manager->ops->te_handler)
  417. manager->ops->te_handler(manager);
  418. }