blob: 644b4b76e07176e3df75b0415227eac162fb3d26 [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * 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.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc_helper.h>
Gustavo Padovan4ea95262015-06-01 12:04:44 -030017#include <drm/drm_atomic.h>
18#include <drm/drm_atomic_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090019
Mark Browne30655d2013-08-13 00:46:40 +010020#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090021#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022#include "exynos_drm_encoder.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090023#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090024
Gustavo Padovan63498e32015-06-01 12:04:53 -030025static void exynos_drm_crtc_enable(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090026{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090027 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090028
Gustavo Padovan63498e32015-06-01 12:04:53 -030029 if (exynos_crtc->enabled)
Inki Daeec05da92011-12-06 11:06:54 +090030 return;
Inki Dae20cd2642013-05-21 16:55:58 +090031
Gustavo Padovan3cecda02015-06-01 12:04:55 -030032 if (exynos_crtc->ops->enable)
33 exynos_crtc->ops->enable(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +090034
Gustavo Padovan63498e32015-06-01 12:04:53 -030035 exynos_crtc->enabled = true;
Andrzej Hajdad6948b22014-10-10 14:31:55 +020036
Gustavo Padovan63498e32015-06-01 12:04:53 -030037 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090038}
39
Gustavo Padovan3fc48672015-06-01 12:04:51 -030040static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
41{
Gustavo Padovan63498e32015-06-01 12:04:53 -030042 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan3fc48672015-06-01 12:04:51 -030043
Gustavo Padovan63498e32015-06-01 12:04:53 -030044 if (!exynos_crtc->enabled)
45 return;
46
47 /* wait for the completion of page flip. */
48 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
49 (exynos_crtc->event == NULL), HZ/20))
50 exynos_crtc->event = NULL;
51
52 drm_crtc_vblank_off(crtc);
53
Gustavo Padovan3cecda02015-06-01 12:04:55 -030054 if (exynos_crtc->ops->disable)
55 exynos_crtc->ops->disable(exynos_crtc);
Gustavo Padovan63498e32015-06-01 12:04:53 -030056
57 exynos_crtc->enabled = false;
Gustavo Padovan3fc48672015-06-01 12:04:51 -030058}
59
Inki Dae1c248b72011-10-04 19:19:01 +090060static bool
61exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020062 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090063 struct drm_display_mode *adjusted_mode)
64{
Sean Paul4b405262014-01-30 16:19:19 -050065 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Sean Paul4b405262014-01-30 16:19:19 -050066
Gustavo Padovan93bca242015-01-18 18:16:23 +090067 if (exynos_crtc->ops->mode_fixup)
68 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
69 adjusted_mode);
Sean Paul4b405262014-01-30 16:19:19 -050070
Inki Dae1c248b72011-10-04 19:19:01 +090071 return true;
72}
73
Gustavo Padovan199329c2015-06-01 12:04:43 -030074static void
75exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090076{
Joonyoung Shim4070d212012-06-27 14:27:05 +090077 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090078
Gustavo Padovan199329c2015-06-01 12:04:43 -030079 if (exynos_crtc->ops->commit)
80 exynos_crtc->ops->commit(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090081}
82
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -030083static void exynos_crtc_atomic_begin(struct drm_crtc *crtc)
84{
85 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
86
87 if (crtc->state->event) {
88 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
89 exynos_crtc->event = crtc->state->event;
90 }
91}
92
93static void exynos_crtc_atomic_flush(struct drm_crtc *crtc)
94{
95}
96
Inki Dae1c248b72011-10-04 19:19:01 +090097static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
Gustavo Padovan63498e32015-06-01 12:04:53 -030098 .enable = exynos_drm_crtc_enable,
Gustavo Padovan3fc48672015-06-01 12:04:51 -030099 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900100 .mode_fixup = exynos_drm_crtc_mode_fixup,
Gustavo Padovan199329c2015-06-01 12:04:43 -0300101 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300102 .atomic_begin = exynos_crtc_atomic_begin,
103 .atomic_flush = exynos_crtc_atomic_flush,
Inki Dae1c248b72011-10-04 19:19:01 +0900104};
105
Inki Dae1c248b72011-10-04 19:19:01 +0900106static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
107{
108 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
109 struct exynos_drm_private *private = crtc->dev->dev_private;
110
Inki Dae1c248b72011-10-04 19:19:01 +0900111 private->crtc[exynos_crtc->pipe] = NULL;
112
113 drm_crtc_cleanup(crtc);
114 kfree(exynos_crtc);
115}
116
117static struct drm_crtc_funcs exynos_crtc_funcs = {
Gustavo Padovan47a7def2015-06-01 12:04:47 -0300118 .set_config = drm_atomic_helper_set_config,
Gustavo Padovan9d5ab6a2015-06-01 12:04:48 -0300119 .page_flip = drm_atomic_helper_page_flip,
Inki Dae1c248b72011-10-04 19:19:01 +0900120 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300121 .reset = drm_atomic_helper_crtc_reset,
122 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
123 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Inki Dae1c248b72011-10-04 19:19:01 +0900124};
125
Gustavo Padovan93bca242015-01-18 18:16:23 +0900126struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900127 struct drm_plane *plane,
128 int pipe,
129 enum exynos_drm_output_type type,
130 const struct exynos_drm_crtc_ops *ops,
131 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900132{
133 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200134 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900135 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200136 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900137
Inki Dae1c248b72011-10-04 19:19:01 +0900138 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900139 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900140 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900141
Inki Dae20cd2642013-05-21 16:55:58 +0900142 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
Sean Paul080be03d2014-02-19 21:02:55 +0900143
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200144 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200145 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900146 exynos_crtc->ops = ops;
147 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900148
Gustavo Padovan357193c2014-11-03 18:20:29 -0200149 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900150
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200151 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900152
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200153 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200154 &exynos_crtc_funcs);
155 if (ret < 0)
156 goto err_crtc;
157
Inki Dae1c248b72011-10-04 19:19:01 +0900158 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
159
Gustavo Padovan93bca242015-01-18 18:16:23 +0900160 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200161
162err_crtc:
163 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200164 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900165 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900166}
167
Sean Paul080be03d2014-02-19 21:02:55 +0900168int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900169{
170 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900171 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900172 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900173
Gustavo Padovan63498e32015-06-01 12:04:53 -0300174 if (!exynos_crtc->enabled)
Inki Daeec05da92011-12-06 11:06:54 +0900175 return -EPERM;
176
Gustavo Padovan93bca242015-01-18 18:16:23 +0900177 if (exynos_crtc->ops->enable_vblank)
178 exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900179
180 return 0;
181}
182
Sean Paul080be03d2014-02-19 21:02:55 +0900183void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900184{
185 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900186 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900187 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900188
Gustavo Padovan63498e32015-06-01 12:04:53 -0300189 if (!exynos_crtc->enabled)
Inki Daeec05da92011-12-06 11:06:54 +0900190 return;
191
Gustavo Padovan93bca242015-01-18 18:16:23 +0900192 if (exynos_crtc->ops->disable_vblank)
193 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900194}
Rahul Sharma663d8762013-01-03 05:44:04 -0500195
Sean Paul080be03d2014-02-19 21:02:55 +0900196void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
Rahul Sharma663d8762013-01-03 05:44:04 -0500197{
198 struct exynos_drm_private *dev_priv = dev->dev_private;
Sean Paul080be03d2014-02-19 21:02:55 +0900199 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
Inki Dae20cd2642013-05-21 16:55:58 +0900200 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
Rahul Sharma663d8762013-01-03 05:44:04 -0500201 unsigned long flags;
202
Rahul Sharma663d8762013-01-03 05:44:04 -0500203 spin_lock_irqsave(&dev->event_lock, flags);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300204 if (exynos_crtc->event) {
Rahul Sharma663d8762013-01-03 05:44:04 -0500205
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300206 drm_send_vblank_event(dev, -1, exynos_crtc->event);
Sean Paul080be03d2014-02-19 21:02:55 +0900207 drm_vblank_put(dev, pipe);
Inki Dae20cd2642013-05-21 16:55:58 +0900208 wake_up(&exynos_crtc->pending_flip_queue);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300209
Rahul Sharma663d8762013-01-03 05:44:04 -0500210 }
211
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300212 exynos_crtc->event = NULL;
Rahul Sharma663d8762013-01-03 05:44:04 -0500213 spin_unlock_irqrestore(&dev->event_lock, flags);
214}
Sean Paul080be03d2014-02-19 21:02:55 +0900215
Sean Paul080be03d2014-02-19 21:02:55 +0900216void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
217{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900218 struct exynos_drm_crtc *exynos_crtc;
Sean Paul080be03d2014-02-19 21:02:55 +0900219 struct drm_device *dev = fb->dev;
220 struct drm_crtc *crtc;
221
222 /*
223 * make sure that overlay data are updated to real hardware
224 * for all encoders.
225 */
226 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Gustavo Padovan93bca242015-01-18 18:16:23 +0900227 exynos_crtc = to_exynos_crtc(crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900228
229 /*
230 * wait for vblank interrupt
231 * - this makes sure that overlay data are updated to
232 * real hardware.
233 */
Gustavo Padovan93bca242015-01-18 18:16:23 +0900234 if (exynos_crtc->ops->wait_for_vblank)
235 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900236 }
237}
Inki Daef37cd5e2014-05-09 14:25:20 +0900238
239int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
240 unsigned int out_type)
241{
242 struct drm_crtc *crtc;
243
244 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
245 struct exynos_drm_crtc *exynos_crtc;
246
247 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200248 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200249 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900250 }
251
252 return -EPERM;
253}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900254
255void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
256{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900257 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900258
Gustavo Padovan93bca242015-01-18 18:16:23 +0900259 if (exynos_crtc->ops->te_handler)
260 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900261}