blob: f782d1f6f796d51be499c702937da9c43fb9e331 [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
Inki Dae1c248b72011-10-04 19:19:01 +090025static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
26{
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
Joonyoung Shimd2716c82011-11-04 17:04:45 +090029 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
30
Inki Daeec05da92011-12-06 11:06:54 +090031 if (exynos_crtc->dpms == mode) {
32 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
33 return;
34 }
35
Inki Dae20cd2642013-05-21 16:55:58 +090036 if (mode > DRM_MODE_DPMS_ON) {
37 /* wait for the completion of page flip. */
YoungJun Choe35d7222014-07-17 18:01:17 +090038 if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
Mandeep Singh Bainese7527472015-04-01 13:02:12 -030039 (exynos_crtc->event == NULL), HZ/20))
40 exynos_crtc->event = NULL;
Andrzej Hajdad6948b22014-10-10 14:31:55 +020041 drm_crtc_vblank_off(crtc);
Inki Dae20cd2642013-05-21 16:55:58 +090042 }
43
Gustavo Padovan93bca242015-01-18 18:16:23 +090044 if (exynos_crtc->ops->dpms)
45 exynos_crtc->ops->dpms(exynos_crtc, mode);
Sean Paul080be03d2014-02-19 21:02:55 +090046
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090047 exynos_crtc->dpms = mode;
Andrzej Hajdad6948b22014-10-10 14:31:55 +020048
49 if (mode == DRM_MODE_DPMS_ON)
50 drm_crtc_vblank_on(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090051}
52
53static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
54{
Inki Dae1c248b72011-10-04 19:19:01 +090055 /* drm framework doesn't check NULL. */
56}
57
58static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
59{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090060 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan9d5310c2014-11-13 22:17:46 -020061 struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary);
Joonyoung Shimd2716c82011-11-04 17:04:45 +090062
Inki Dae50caf252012-08-20 21:29:25 +090063 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Sean Paul080be03d2014-02-19 21:02:55 +090064
Gustavo Padovan93bca242015-01-18 18:16:23 +090065 if (exynos_crtc->ops->win_commit)
66 exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
Inki Dae1c248b72011-10-04 19:19:01 +090067}
68
69static bool
70exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020071 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090072 struct drm_display_mode *adjusted_mode)
73{
Sean Paul4b405262014-01-30 16:19:19 -050074 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Sean Paul4b405262014-01-30 16:19:19 -050075
Gustavo Padovan93bca242015-01-18 18:16:23 +090076 if (exynos_crtc->ops->mode_fixup)
77 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
78 adjusted_mode);
Sean Paul4b405262014-01-30 16:19:19 -050079
Inki Dae1c248b72011-10-04 19:19:01 +090080 return true;
81}
82
Gustavo Padovan199329c2015-06-01 12:04:43 -030083static void
84exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090085{
Joonyoung Shim4070d212012-06-27 14:27:05 +090086 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090087
Gustavo Padovan199329c2015-06-01 12:04:43 -030088 if (exynos_crtc->ops->commit)
89 exynos_crtc->ops->commit(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090090}
91
Joonyoung Shima365d9e2012-06-27 14:27:10 +090092static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
93{
Sean Paula9c4cd22014-01-30 16:19:17 -050094 struct drm_plane *plane;
95 int ret;
Joonyoung Shima365d9e2012-06-27 14:27:10 +090096
Joonyoung Shima365d9e2012-06-27 14:27:10 +090097 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
Sean Paula9c4cd22014-01-30 16:19:17 -050098
Matt Roper08863272014-04-01 15:22:31 -070099 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
Sean Paula9c4cd22014-01-30 16:19:17 -0500100 if (plane->crtc != crtc)
101 continue;
102
103 ret = plane->funcs->disable_plane(plane);
104 if (ret)
105 DRM_ERROR("Failed to disable plane %d\n", ret);
106 }
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900107}
108
Inki Dae1c248b72011-10-04 19:19:01 +0900109static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
110 .dpms = exynos_drm_crtc_dpms,
111 .prepare = exynos_drm_crtc_prepare,
112 .commit = exynos_drm_crtc_commit,
113 .mode_fixup = exynos_drm_crtc_mode_fixup,
Gustavo Padovan199329c2015-06-01 12:04:43 -0300114 .mode_set = drm_helper_crtc_mode_set,
115 .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
116 .mode_set_base = drm_helper_crtc_mode_set_base,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900117 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900118};
119
120static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
Keith Packarded8d1972013-07-22 18:49:58 -0700121 struct drm_framebuffer *fb,
122 struct drm_pending_vblank_event *event,
123 uint32_t page_flip_flags)
Inki Dae1c248b72011-10-04 19:19:01 +0900124{
125 struct drm_device *dev = crtc->dev;
Inki Dae1c248b72011-10-04 19:19:01 +0900126 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan8b9c4502014-11-25 16:18:34 -0200127 unsigned int crtc_w, crtc_h;
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300128 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900129
Inki Daeef6223d2012-09-11 18:25:21 +0900130 /* when the page flip is requested, crtc's dpms should be on */
131 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
132 DRM_ERROR("failed page flip request.\n");
133 return -EINVAL;
134 }
135
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300136 if (!event)
137 return -EINVAL;
Inki Dae1c248b72011-10-04 19:19:01 +0900138
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300139 spin_lock_irq(&dev->event_lock);
140 if (exynos_crtc->event) {
141 ret = -EBUSY;
142 goto out;
Inki Dae1c248b72011-10-04 19:19:01 +0900143 }
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300144
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300145 ret = exynos_check_plane(crtc->primary, fb);
146 if (ret)
147 goto out;
148
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300149 ret = drm_vblank_get(dev, exynos_crtc->pipe);
150 if (ret) {
151 DRM_DEBUG("failed to acquire vblank counter\n");
152 goto out;
153 }
154
155 exynos_crtc->event = event;
156 spin_unlock_irq(&dev->event_lock);
157
158 /*
159 * the pipe from user always is 0 so we can set pipe number
160 * of current owner to event.
161 */
162 event->pipe = exynos_crtc->pipe;
163
164 crtc->primary->fb = fb;
165 crtc_w = fb->width - crtc->x;
166 crtc_h = fb->height - crtc->y;
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300167 exynos_update_plane(crtc->primary, crtc, fb, 0, 0,
168 crtc_w, crtc_h, crtc->x << 16, crtc->y << 16,
169 crtc_w << 16, crtc_h << 16);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300170
Gustavo Padovan7cf1ff252015-06-01 12:04:45 -0300171 if (crtc->primary->state)
172 drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
173
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300174 return 0;
175
Inki Dae1c248b72011-10-04 19:19:01 +0900176out:
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300177 spin_unlock_irq(&dev->event_lock);
Inki Dae1c248b72011-10-04 19:19:01 +0900178 return ret;
179}
180
181static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
182{
183 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
184 struct exynos_drm_private *private = crtc->dev->dev_private;
185
Inki Dae1c248b72011-10-04 19:19:01 +0900186 private->crtc[exynos_crtc->pipe] = NULL;
187
188 drm_crtc_cleanup(crtc);
189 kfree(exynos_crtc);
190}
191
192static struct drm_crtc_funcs exynos_crtc_funcs = {
193 .set_config = drm_crtc_helper_set_config,
194 .page_flip = exynos_drm_crtc_page_flip,
195 .destroy = exynos_drm_crtc_destroy,
Gustavo Padovan4ea95262015-06-01 12:04:44 -0300196 .reset = drm_atomic_helper_crtc_reset,
197 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
198 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Inki Dae1c248b72011-10-04 19:19:01 +0900199};
200
Gustavo Padovan93bca242015-01-18 18:16:23 +0900201struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
Krzysztof Kozlowskif3aaf762015-05-07 09:04:45 +0900202 struct drm_plane *plane,
203 int pipe,
204 enum exynos_drm_output_type type,
205 const struct exynos_drm_crtc_ops *ops,
206 void *ctx)
Inki Dae1c248b72011-10-04 19:19:01 +0900207{
208 struct exynos_drm_crtc *exynos_crtc;
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200209 struct exynos_drm_private *private = drm_dev->dev_private;
Inki Dae1c248b72011-10-04 19:19:01 +0900210 struct drm_crtc *crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200211 int ret;
Inki Dae1c248b72011-10-04 19:19:01 +0900212
Inki Dae1c248b72011-10-04 19:19:01 +0900213 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900214 if (!exynos_crtc)
Gustavo Padovan93bca242015-01-18 18:16:23 +0900215 return ERR_PTR(-ENOMEM);
Inki Dae1c248b72011-10-04 19:19:01 +0900216
Inki Dae20cd2642013-05-21 16:55:58 +0900217 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
Sean Paul080be03d2014-02-19 21:02:55 +0900218
219 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200220 exynos_crtc->pipe = pipe;
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200221 exynos_crtc->type = type;
Gustavo Padovan93bca242015-01-18 18:16:23 +0900222 exynos_crtc->ops = ops;
223 exynos_crtc->ctx = ctx;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900224
Gustavo Padovan357193c2014-11-03 18:20:29 -0200225 crtc = &exynos_crtc->base;
Inki Dae1c248b72011-10-04 19:19:01 +0900226
Gustavo Padovane09f2b02014-11-04 18:25:27 -0200227 private->crtc[pipe] = crtc;
Inki Dae1c248b72011-10-04 19:19:01 +0900228
Gustavo Padovaneb88e422014-11-26 16:43:27 -0200229 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200230 &exynos_crtc_funcs);
231 if (ret < 0)
232 goto err_crtc;
233
Inki Dae1c248b72011-10-04 19:19:01 +0900234 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
235
Gustavo Padovan93bca242015-01-18 18:16:23 +0900236 return exynos_crtc;
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200237
238err_crtc:
239 plane->funcs->destroy(plane);
Andrzej Hajda72ed6cc2014-09-19 14:58:53 +0200240 kfree(exynos_crtc);
Gustavo Padovan93bca242015-01-18 18:16:23 +0900241 return ERR_PTR(ret);
Inki Dae1c248b72011-10-04 19:19:01 +0900242}
243
Sean Paul080be03d2014-02-19 21:02:55 +0900244int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900245{
246 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900247 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900248 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900249
Inki Daeec05da92011-12-06 11:06:54 +0900250 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
251 return -EPERM;
252
Gustavo Padovan93bca242015-01-18 18:16:23 +0900253 if (exynos_crtc->ops->enable_vblank)
254 exynos_crtc->ops->enable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900255
256 return 0;
257}
258
Sean Paul080be03d2014-02-19 21:02:55 +0900259void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
Inki Dae1c248b72011-10-04 19:19:01 +0900260{
261 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900262 struct exynos_drm_crtc *exynos_crtc =
Sean Paul080be03d2014-02-19 21:02:55 +0900263 to_exynos_crtc(private->crtc[pipe]);
Inki Dae1c248b72011-10-04 19:19:01 +0900264
Inki Daeec05da92011-12-06 11:06:54 +0900265 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
266 return;
267
Gustavo Padovan93bca242015-01-18 18:16:23 +0900268 if (exynos_crtc->ops->disable_vblank)
269 exynos_crtc->ops->disable_vblank(exynos_crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900270}
Rahul Sharma663d8762013-01-03 05:44:04 -0500271
Sean Paul080be03d2014-02-19 21:02:55 +0900272void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
Rahul Sharma663d8762013-01-03 05:44:04 -0500273{
274 struct exynos_drm_private *dev_priv = dev->dev_private;
Sean Paul080be03d2014-02-19 21:02:55 +0900275 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
Inki Dae20cd2642013-05-21 16:55:58 +0900276 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
Rahul Sharma663d8762013-01-03 05:44:04 -0500277 unsigned long flags;
278
Rahul Sharma663d8762013-01-03 05:44:04 -0500279 spin_lock_irqsave(&dev->event_lock, flags);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300280 if (exynos_crtc->event) {
Rahul Sharma663d8762013-01-03 05:44:04 -0500281
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300282 drm_send_vblank_event(dev, -1, exynos_crtc->event);
Sean Paul080be03d2014-02-19 21:02:55 +0900283 drm_vblank_put(dev, pipe);
Inki Dae20cd2642013-05-21 16:55:58 +0900284 wake_up(&exynos_crtc->pending_flip_queue);
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300285
Rahul Sharma663d8762013-01-03 05:44:04 -0500286 }
287
Mandeep Singh Bainese7527472015-04-01 13:02:12 -0300288 exynos_crtc->event = NULL;
Rahul Sharma663d8762013-01-03 05:44:04 -0500289 spin_unlock_irqrestore(&dev->event_lock, flags);
290}
Sean Paul080be03d2014-02-19 21:02:55 +0900291
Sean Paul080be03d2014-02-19 21:02:55 +0900292void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
293{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900294 struct exynos_drm_crtc *exynos_crtc;
Sean Paul080be03d2014-02-19 21:02:55 +0900295 struct drm_device *dev = fb->dev;
296 struct drm_crtc *crtc;
297
298 /*
299 * make sure that overlay data are updated to real hardware
300 * for all encoders.
301 */
302 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Gustavo Padovan93bca242015-01-18 18:16:23 +0900303 exynos_crtc = to_exynos_crtc(crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900304
305 /*
306 * wait for vblank interrupt
307 * - this makes sure that overlay data are updated to
308 * real hardware.
309 */
Gustavo Padovan93bca242015-01-18 18:16:23 +0900310 if (exynos_crtc->ops->wait_for_vblank)
311 exynos_crtc->ops->wait_for_vblank(exynos_crtc);
Sean Paul080be03d2014-02-19 21:02:55 +0900312 }
313}
Inki Daef37cd5e2014-05-09 14:25:20 +0900314
315int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
316 unsigned int out_type)
317{
318 struct drm_crtc *crtc;
319
320 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
321 struct exynos_drm_crtc *exynos_crtc;
322
323 exynos_crtc = to_exynos_crtc(crtc);
Gustavo Padovan5d1741a2014-11-05 19:51:35 -0200324 if (exynos_crtc->type == out_type)
Gustavo Padovan8a326ed2014-11-04 18:44:47 -0200325 return exynos_crtc->pipe;
Inki Daef37cd5e2014-05-09 14:25:20 +0900326 }
327
328 return -EPERM;
329}
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900330
331void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
332{
Gustavo Padovan93bca242015-01-18 18:16:23 +0900333 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900334
Gustavo Padovan93bca242015-01-18 18:16:23 +0900335 if (exynos_crtc->ops->te_handler)
336 exynos_crtc->ops->te_handler(exynos_crtc);
YoungJun Cho5595d4d2014-07-17 18:01:19 +0900337}