blob: ebc01503d50ec1f2a9034bb81fa4724f12555f16 [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>
Inki Dae1c248b72011-10-04 19:19:01 +090017
Mark Browne30655d2013-08-13 00:46:40 +010018#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090019#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090020#include "exynos_drm_encoder.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090021#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090022
23#define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
24 drm_crtc)
25
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090026enum exynos_crtc_mode {
27 CRTC_MODE_NORMAL, /* normal mode */
28 CRTC_MODE_BLANK, /* The private plane of crtc is blank */
29};
30
Inki Dae1c248b72011-10-04 19:19:01 +090031/*
Inki Dae1c248b72011-10-04 19:19:01 +090032 * Exynos specific crtc structure.
33 *
34 * @drm_crtc: crtc object.
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090035 * @drm_plane: pointer of private plane object for this crtc
Inki Dae1c248b72011-10-04 19:19:01 +090036 * @pipe: a crtc index created at load() with a new crtc object creation
37 * and the crtc object would be set to private->crtc array
38 * to get a crtc object corresponding to this pipe from private->crtc
39 * array when irq interrupt occured. the reason of using this pipe is that
40 * drm framework doesn't support multiple irq yet.
41 * we can refer to the crtc to current hardware interrupt occured through
42 * this pipe value.
Inki Daeec05da92011-12-06 11:06:54 +090043 * @dpms: store the crtc dpms value
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090044 * @mode: store the crtc mode value
Inki Dae1c248b72011-10-04 19:19:01 +090045 */
46struct exynos_drm_crtc {
47 struct drm_crtc drm_crtc;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090048 struct drm_plane *plane;
Inki Dae1c248b72011-10-04 19:19:01 +090049 unsigned int pipe;
Inki Daeec05da92011-12-06 11:06:54 +090050 unsigned int dpms;
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090051 enum exynos_crtc_mode mode;
Inki Dae20cd2642013-05-21 16:55:58 +090052 wait_queue_head_t pending_flip_queue;
53 atomic_t pending_flip;
Inki Dae1c248b72011-10-04 19:19:01 +090054};
55
Inki Dae1c248b72011-10-04 19:19:01 +090056static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
57{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090058 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090059
Joonyoung Shimd2716c82011-11-04 17:04:45 +090060 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
61
Inki Daeec05da92011-12-06 11:06:54 +090062 if (exynos_crtc->dpms == mode) {
63 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
64 return;
65 }
66
Inki Dae20cd2642013-05-21 16:55:58 +090067 if (mode > DRM_MODE_DPMS_ON) {
68 /* wait for the completion of page flip. */
69 wait_event(exynos_crtc->pending_flip_queue,
70 atomic_read(&exynos_crtc->pending_flip) == 0);
71 drm_vblank_off(crtc->dev, exynos_crtc->pipe);
72 }
73
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090074 exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
75 exynos_crtc->dpms = mode;
Inki Dae1c248b72011-10-04 19:19:01 +090076}
77
78static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
79{
Inki Dae1c248b72011-10-04 19:19:01 +090080 /* drm framework doesn't check NULL. */
81}
82
83static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
84{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090085 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
86
Inki Dae50caf252012-08-20 21:29:25 +090087 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Joonyoung Shim4070d212012-06-27 14:27:05 +090088 exynos_plane_commit(exynos_crtc->plane);
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090089 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
Inki Dae1c248b72011-10-04 19:19:01 +090090}
91
92static bool
93exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020094 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090095 struct drm_display_mode *adjusted_mode)
96{
Inki Dae1c248b72011-10-04 19:19:01 +090097 /* drm framework doesn't check NULL */
98 return true;
99}
100
101static int
102exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
103 struct drm_display_mode *adjusted_mode, int x, int y,
104 struct drm_framebuffer *old_fb)
105{
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900106 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900107 struct drm_plane *plane = exynos_crtc->plane;
108 unsigned int crtc_w;
109 unsigned int crtc_h;
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900110 int pipe = exynos_crtc->pipe;
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900111 int ret;
112
Inki Dae1de425b2012-03-16 18:47:04 +0900113 /*
114 * copy the mode data adjusted by mode_fixup() into crtc->mode
115 * so that hardware can be seet to proper mode.
116 */
117 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
Inki Dae1c248b72011-10-04 19:19:01 +0900118
Joonyoung Shim4070d212012-06-27 14:27:05 +0900119 crtc_w = crtc->fb->width - x;
120 crtc_h = crtc->fb->height - y;
121
122 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
123 x, y, crtc_w, crtc_h);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900124 if (ret)
125 return ret;
126
Joonyoung Shim4070d212012-06-27 14:27:05 +0900127 plane->crtc = crtc;
128 plane->fb = crtc->fb;
129
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900130 exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900131
132 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900133}
134
Inki Dae7fd65df2013-05-12 16:09:33 +0900135static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
Inki Dae1c248b72011-10-04 19:19:01 +0900136 struct drm_framebuffer *old_fb)
137{
Joonyoung Shim4070d212012-06-27 14:27:05 +0900138 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
139 struct drm_plane *plane = exynos_crtc->plane;
140 unsigned int crtc_w;
141 unsigned int crtc_h;
Inki Dae1c248b72011-10-04 19:19:01 +0900142 int ret;
143
Inki Dae32aeab12012-09-14 13:29:47 +0900144 /* when framebuffer changing is requested, crtc's dpms should be on */
145 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
146 DRM_ERROR("failed framebuffer changing request.\n");
147 return -EPERM;
148 }
149
Joonyoung Shim4070d212012-06-27 14:27:05 +0900150 crtc_w = crtc->fb->width - x;
151 crtc_h = crtc->fb->height - y;
152
153 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
154 x, y, crtc_w, crtc_h);
Inki Dae1c248b72011-10-04 19:19:01 +0900155 if (ret)
156 return ret;
157
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900158 exynos_drm_crtc_commit(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900159
Joonyoung Shim4070d212012-06-27 14:27:05 +0900160 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900161}
162
Inki Dae7fd65df2013-05-12 16:09:33 +0900163static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
164 struct drm_framebuffer *old_fb)
165{
166 return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
167}
168
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900169static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
170{
171 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
172
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900173 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
174 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
175}
176
Inki Dae1c248b72011-10-04 19:19:01 +0900177static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
178 .dpms = exynos_drm_crtc_dpms,
179 .prepare = exynos_drm_crtc_prepare,
180 .commit = exynos_drm_crtc_commit,
181 .mode_fixup = exynos_drm_crtc_mode_fixup,
182 .mode_set = exynos_drm_crtc_mode_set,
183 .mode_set_base = exynos_drm_crtc_mode_set_base,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900184 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900185};
186
187static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
Keith Packarded8d1972013-07-22 18:49:58 -0700188 struct drm_framebuffer *fb,
189 struct drm_pending_vblank_event *event,
190 uint32_t page_flip_flags)
Inki Dae1c248b72011-10-04 19:19:01 +0900191{
192 struct drm_device *dev = crtc->dev;
193 struct exynos_drm_private *dev_priv = dev->dev_private;
194 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
195 struct drm_framebuffer *old_fb = crtc->fb;
196 int ret = -EINVAL;
197
Inki Daeef6223d2012-09-11 18:25:21 +0900198 /* when the page flip is requested, crtc's dpms should be on */
199 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
200 DRM_ERROR("failed page flip request.\n");
201 return -EINVAL;
202 }
203
Inki Dae1c248b72011-10-04 19:19:01 +0900204 mutex_lock(&dev->struct_mutex);
205
Inki Daeccf4d882011-10-14 13:29:51 +0900206 if (event) {
207 /*
208 * the pipe from user always is 0 so we can set pipe number
209 * of current owner to event.
210 */
211 event->pipe = exynos_crtc->pipe;
212
Inki Dae1c248b72011-10-04 19:19:01 +0900213 ret = drm_vblank_get(dev, exynos_crtc->pipe);
214 if (ret) {
215 DRM_DEBUG("failed to acquire vblank counter\n");
Inki Daeccf4d882011-10-14 13:29:51 +0900216
Inki Dae1c248b72011-10-04 19:19:01 +0900217 goto out;
218 }
219
Imre Deak85473322012-11-02 13:30:47 +0200220 spin_lock_irq(&dev->event_lock);
Inki Daec5614ae2012-02-15 11:25:20 +0900221 list_add_tail(&event->base.link,
222 &dev_priv->pageflip_event_list);
Inki Dae20cd2642013-05-21 16:55:58 +0900223 atomic_set(&exynos_crtc->pending_flip, 1);
Imre Deak85473322012-11-02 13:30:47 +0200224 spin_unlock_irq(&dev->event_lock);
Inki Daec5614ae2012-02-15 11:25:20 +0900225
Inki Dae1c248b72011-10-04 19:19:01 +0900226 crtc->fb = fb;
Inki Dae7fd65df2013-05-12 16:09:33 +0900227 ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
Joonyoung Shim4070d212012-06-27 14:27:05 +0900228 NULL);
Inki Dae1c248b72011-10-04 19:19:01 +0900229 if (ret) {
230 crtc->fb = old_fb;
Imre Deak85473322012-11-02 13:30:47 +0200231
232 spin_lock_irq(&dev->event_lock);
Inki Dae1c248b72011-10-04 19:19:01 +0900233 drm_vblank_put(dev, exynos_crtc->pipe);
Inki Daeccf4d882011-10-14 13:29:51 +0900234 list_del(&event->base.link);
Imre Deak85473322012-11-02 13:30:47 +0200235 spin_unlock_irq(&dev->event_lock);
Inki Dae1c248b72011-10-04 19:19:01 +0900236
237 goto out;
238 }
Inki Dae1c248b72011-10-04 19:19:01 +0900239 }
240out:
241 mutex_unlock(&dev->struct_mutex);
242 return ret;
243}
244
245static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
246{
247 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
248 struct exynos_drm_private *private = crtc->dev->dev_private;
249
Inki Dae1c248b72011-10-04 19:19:01 +0900250 private->crtc[exynos_crtc->pipe] = NULL;
251
252 drm_crtc_cleanup(crtc);
253 kfree(exynos_crtc);
254}
255
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900256static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
257 struct drm_property *property,
258 uint64_t val)
259{
260 struct drm_device *dev = crtc->dev;
261 struct exynos_drm_private *dev_priv = dev->dev_private;
262 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
263
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900264 if (property == dev_priv->crtc_mode_property) {
265 enum exynos_crtc_mode mode = val;
266
267 if (mode == exynos_crtc->mode)
268 return 0;
269
270 exynos_crtc->mode = mode;
271
272 switch (mode) {
273 case CRTC_MODE_NORMAL:
274 exynos_drm_crtc_commit(crtc);
275 break;
276 case CRTC_MODE_BLANK:
277 exynos_plane_dpms(exynos_crtc->plane,
278 DRM_MODE_DPMS_OFF);
279 break;
280 default:
281 break;
282 }
283
284 return 0;
285 }
286
287 return -EINVAL;
288}
289
Inki Dae1c248b72011-10-04 19:19:01 +0900290static struct drm_crtc_funcs exynos_crtc_funcs = {
291 .set_config = drm_crtc_helper_set_config,
292 .page_flip = exynos_drm_crtc_page_flip,
293 .destroy = exynos_drm_crtc_destroy,
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900294 .set_property = exynos_drm_crtc_set_property,
Inki Dae1c248b72011-10-04 19:19:01 +0900295};
296
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900297static const struct drm_prop_enum_list mode_names[] = {
298 { CRTC_MODE_NORMAL, "normal" },
299 { CRTC_MODE_BLANK, "blank" },
300};
301
302static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
303{
304 struct drm_device *dev = crtc->dev;
305 struct exynos_drm_private *dev_priv = dev->dev_private;
306 struct drm_property *prop;
307
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900308 prop = dev_priv->crtc_mode_property;
309 if (!prop) {
310 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
311 ARRAY_SIZE(mode_names));
312 if (!prop)
313 return;
314
315 dev_priv->crtc_mode_property = prop;
316 }
317
318 drm_object_attach_property(&crtc->base, prop, 0);
319}
320
Inki Dae1c248b72011-10-04 19:19:01 +0900321int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
322{
323 struct exynos_drm_crtc *exynos_crtc;
324 struct exynos_drm_private *private = dev->dev_private;
325 struct drm_crtc *crtc;
326
Inki Dae1c248b72011-10-04 19:19:01 +0900327 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
Sachin Kamat38bb5252013-08-19 19:04:55 +0900328 if (!exynos_crtc)
Inki Dae1c248b72011-10-04 19:19:01 +0900329 return -ENOMEM;
Inki Dae1c248b72011-10-04 19:19:01 +0900330
331 exynos_crtc->pipe = nr;
Inki Daeec05da92011-12-06 11:06:54 +0900332 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Inki Dae20cd2642013-05-21 16:55:58 +0900333 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
334 atomic_set(&exynos_crtc->pending_flip, 0);
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900335 exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
336 if (!exynos_crtc->plane) {
337 kfree(exynos_crtc);
338 return -ENOMEM;
339 }
340
Inki Dae1c248b72011-10-04 19:19:01 +0900341 crtc = &exynos_crtc->drm_crtc;
342
343 private->crtc[nr] = crtc;
344
345 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
346 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
347
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900348 exynos_drm_crtc_attach_mode_property(crtc);
349
Inki Dae1c248b72011-10-04 19:19:01 +0900350 return 0;
351}
352
353int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
354{
355 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900356 struct exynos_drm_crtc *exynos_crtc =
357 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900358
Inki Daeec05da92011-12-06 11:06:54 +0900359 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
360 return -EPERM;
361
Inki Dae1c248b72011-10-04 19:19:01 +0900362 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
363 exynos_drm_enable_vblank);
364
365 return 0;
366}
367
368void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
369{
370 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900371 struct exynos_drm_crtc *exynos_crtc =
372 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900373
Inki Daeec05da92011-12-06 11:06:54 +0900374 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
375 return;
376
Inki Dae1c248b72011-10-04 19:19:01 +0900377 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
378 exynos_drm_disable_vblank);
379}
Rahul Sharma663d8762013-01-03 05:44:04 -0500380
381void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc)
382{
383 struct exynos_drm_private *dev_priv = dev->dev_private;
384 struct drm_pending_vblank_event *e, *t;
Inki Dae20cd2642013-05-21 16:55:58 +0900385 struct drm_crtc *drm_crtc = dev_priv->crtc[crtc];
386 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
Rahul Sharma663d8762013-01-03 05:44:04 -0500387 unsigned long flags;
388
Rahul Sharma663d8762013-01-03 05:44:04 -0500389 spin_lock_irqsave(&dev->event_lock, flags);
390
391 list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
392 base.link) {
393 /* if event's pipe isn't same as crtc then ignore it. */
394 if (crtc != e->pipe)
395 continue;
396
Rob Clarkc5cca972013-05-22 11:48:40 +0900397 list_del(&e->base.link);
398 drm_send_vblank_event(dev, -1, e);
Rahul Sharma663d8762013-01-03 05:44:04 -0500399 drm_vblank_put(dev, crtc);
Inki Dae20cd2642013-05-21 16:55:58 +0900400 atomic_set(&exynos_crtc->pending_flip, 0);
401 wake_up(&exynos_crtc->pending_flip_queue);
Rahul Sharma663d8762013-01-03 05:44:04 -0500402 }
403
404 spin_unlock_irqrestore(&dev->event_lock, flags);
405}