3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Inki Dae <inki.dae@samsung.com>
6 * Joonyoung Shim <jy0922.shim@samsung.com>
7 * Seung-Woo Kim <sw0312.kim@samsung.com>
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.
16 #include <drm/drm_crtc_helper.h>
18 #include "exynos_drm_crtc.h"
19 #include "exynos_drm_drv.h"
20 #include "exynos_drm_encoder.h"
21 #include "exynos_drm_plane.h"
23 static void exynos_drm_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
25 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
27 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc
->base
.id
, mode
);
29 if (exynos_crtc
->dpms
== mode
) {
30 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
34 if (mode
> DRM_MODE_DPMS_ON
) {
35 /* wait for the completion of page flip. */
36 if (!wait_event_timeout(exynos_crtc
->pending_flip_queue
,
37 (exynos_crtc
->event
== NULL
), HZ
/20))
38 exynos_crtc
->event
= NULL
;
39 drm_crtc_vblank_off(crtc
);
42 if (exynos_crtc
->ops
->dpms
)
43 exynos_crtc
->ops
->dpms(exynos_crtc
, mode
);
45 exynos_crtc
->dpms
= mode
;
47 if (mode
== DRM_MODE_DPMS_ON
)
48 drm_crtc_vblank_on(crtc
);
51 static void exynos_drm_crtc_prepare(struct drm_crtc
*crtc
)
53 /* drm framework doesn't check NULL. */
56 static void exynos_drm_crtc_commit(struct drm_crtc
*crtc
)
58 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
59 struct exynos_drm_plane
*exynos_plane
= to_exynos_plane(crtc
->primary
);
61 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
63 if (exynos_crtc
->ops
->win_commit
)
64 exynos_crtc
->ops
->win_commit(exynos_crtc
, exynos_plane
->zpos
);
68 exynos_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
69 const struct drm_display_mode
*mode
,
70 struct drm_display_mode
*adjusted_mode
)
72 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
74 if (exynos_crtc
->ops
->mode_fixup
)
75 return exynos_crtc
->ops
->mode_fixup(exynos_crtc
, mode
,
82 exynos_drm_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
84 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
86 if (exynos_crtc
->ops
->commit
)
87 exynos_crtc
->ops
->commit(exynos_crtc
);
90 static void exynos_drm_crtc_disable(struct drm_crtc
*crtc
)
92 struct drm_plane
*plane
;
95 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
97 drm_for_each_legacy_plane(plane
, &crtc
->dev
->mode_config
.plane_list
) {
98 if (plane
->crtc
!= crtc
)
101 ret
= plane
->funcs
->disable_plane(plane
);
103 DRM_ERROR("Failed to disable plane %d\n", ret
);
107 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs
= {
108 .dpms
= exynos_drm_crtc_dpms
,
109 .prepare
= exynos_drm_crtc_prepare
,
110 .commit
= exynos_drm_crtc_commit
,
111 .mode_fixup
= exynos_drm_crtc_mode_fixup
,
112 .mode_set
= drm_helper_crtc_mode_set
,
113 .mode_set_nofb
= exynos_drm_crtc_mode_set_nofb
,
114 .mode_set_base
= drm_helper_crtc_mode_set_base
,
115 .disable
= exynos_drm_crtc_disable
,
118 static int exynos_drm_crtc_page_flip(struct drm_crtc
*crtc
,
119 struct drm_framebuffer
*fb
,
120 struct drm_pending_vblank_event
*event
,
121 uint32_t page_flip_flags
)
123 struct drm_device
*dev
= crtc
->dev
;
124 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
125 unsigned int crtc_w
, crtc_h
;
128 /* when the page flip is requested, crtc's dpms should be on */
129 if (exynos_crtc
->dpms
> DRM_MODE_DPMS_ON
) {
130 DRM_ERROR("failed page flip request.\n");
137 spin_lock_irq(&dev
->event_lock
);
138 if (exynos_crtc
->event
) {
143 ret
= exynos_check_plane(crtc
->primary
, fb
);
147 ret
= drm_vblank_get(dev
, exynos_crtc
->pipe
);
149 DRM_DEBUG("failed to acquire vblank counter\n");
153 exynos_crtc
->event
= event
;
154 spin_unlock_irq(&dev
->event_lock
);
157 * the pipe from user always is 0 so we can set pipe number
158 * of current owner to event.
160 event
->pipe
= exynos_crtc
->pipe
;
162 crtc
->primary
->fb
= fb
;
163 crtc_w
= fb
->width
- crtc
->x
;
164 crtc_h
= fb
->height
- crtc
->y
;
165 exynos_update_plane(crtc
->primary
, crtc
, fb
, 0, 0,
166 crtc_w
, crtc_h
, crtc
->x
<< 16, crtc
->y
<< 16,
167 crtc_w
<< 16, crtc_h
<< 16);
172 spin_unlock_irq(&dev
->event_lock
);
176 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
178 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
179 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
181 private->crtc
[exynos_crtc
->pipe
] = NULL
;
183 drm_crtc_cleanup(crtc
);
187 static struct drm_crtc_funcs exynos_crtc_funcs
= {
188 .set_config
= drm_crtc_helper_set_config
,
189 .page_flip
= exynos_drm_crtc_page_flip
,
190 .destroy
= exynos_drm_crtc_destroy
,
193 struct exynos_drm_crtc
*exynos_drm_crtc_create(struct drm_device
*drm_dev
,
194 struct drm_plane
*plane
,
196 enum exynos_drm_output_type type
,
197 const struct exynos_drm_crtc_ops
*ops
,
200 struct exynos_drm_crtc
*exynos_crtc
;
201 struct exynos_drm_private
*private = drm_dev
->dev_private
;
202 struct drm_crtc
*crtc
;
205 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
207 return ERR_PTR(-ENOMEM
);
209 init_waitqueue_head(&exynos_crtc
->pending_flip_queue
);
211 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
212 exynos_crtc
->pipe
= pipe
;
213 exynos_crtc
->type
= type
;
214 exynos_crtc
->ops
= ops
;
215 exynos_crtc
->ctx
= ctx
;
217 crtc
= &exynos_crtc
->base
;
219 private->crtc
[pipe
] = crtc
;
221 ret
= drm_crtc_init_with_planes(drm_dev
, crtc
, plane
, NULL
,
226 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
231 plane
->funcs
->destroy(plane
);
236 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int pipe
)
238 struct exynos_drm_private
*private = dev
->dev_private
;
239 struct exynos_drm_crtc
*exynos_crtc
=
240 to_exynos_crtc(private->crtc
[pipe
]);
242 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
245 if (exynos_crtc
->ops
->enable_vblank
)
246 exynos_crtc
->ops
->enable_vblank(exynos_crtc
);
251 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int pipe
)
253 struct exynos_drm_private
*private = dev
->dev_private
;
254 struct exynos_drm_crtc
*exynos_crtc
=
255 to_exynos_crtc(private->crtc
[pipe
]);
257 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
260 if (exynos_crtc
->ops
->disable_vblank
)
261 exynos_crtc
->ops
->disable_vblank(exynos_crtc
);
264 void exynos_drm_crtc_finish_pageflip(struct drm_device
*dev
, int pipe
)
266 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
267 struct drm_crtc
*drm_crtc
= dev_priv
->crtc
[pipe
];
268 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(drm_crtc
);
271 spin_lock_irqsave(&dev
->event_lock
, flags
);
272 if (exynos_crtc
->event
) {
274 drm_send_vblank_event(dev
, -1, exynos_crtc
->event
);
275 drm_vblank_put(dev
, pipe
);
276 wake_up(&exynos_crtc
->pending_flip_queue
);
280 exynos_crtc
->event
= NULL
;
281 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
284 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer
*fb
)
286 struct exynos_drm_crtc
*exynos_crtc
;
287 struct drm_device
*dev
= fb
->dev
;
288 struct drm_crtc
*crtc
;
291 * make sure that overlay data are updated to real hardware
294 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
295 exynos_crtc
= to_exynos_crtc(crtc
);
298 * wait for vblank interrupt
299 * - this makes sure that overlay data are updated to
302 if (exynos_crtc
->ops
->wait_for_vblank
)
303 exynos_crtc
->ops
->wait_for_vblank(exynos_crtc
);
307 int exynos_drm_crtc_get_pipe_from_type(struct drm_device
*drm_dev
,
308 unsigned int out_type
)
310 struct drm_crtc
*crtc
;
312 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
) {
313 struct exynos_drm_crtc
*exynos_crtc
;
315 exynos_crtc
= to_exynos_crtc(crtc
);
316 if (exynos_crtc
->type
== out_type
)
317 return exynos_crtc
->pipe
;
323 void exynos_drm_crtc_te_handler(struct drm_crtc
*crtc
)
325 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
327 if (exynos_crtc
->ops
->te_handler
)
328 exynos_crtc
->ops
->te_handler(exynos_crtc
);