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>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
20 #include "exynos_drm_crtc.h"
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_encoder.h"
23 #include "exynos_drm_plane.h"
25 static void exynos_drm_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
27 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
29 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc
->base
.id
, mode
);
31 if (exynos_crtc
->dpms
== mode
) {
32 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
36 if (mode
> DRM_MODE_DPMS_ON
) {
37 /* wait for the completion of page flip. */
38 if (!wait_event_timeout(exynos_crtc
->pending_flip_queue
,
39 (exynos_crtc
->event
== NULL
), HZ
/20))
40 exynos_crtc
->event
= NULL
;
41 drm_crtc_vblank_off(crtc
);
44 if (exynos_crtc
->ops
->dpms
)
45 exynos_crtc
->ops
->dpms(exynos_crtc
, mode
);
47 exynos_crtc
->dpms
= mode
;
49 if (mode
== DRM_MODE_DPMS_ON
)
50 drm_crtc_vblank_on(crtc
);
53 static void exynos_drm_crtc_prepare(struct drm_crtc
*crtc
)
55 /* drm framework doesn't check NULL. */
58 static void exynos_drm_crtc_commit(struct drm_crtc
*crtc
)
60 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
61 struct exynos_drm_plane
*exynos_plane
= to_exynos_plane(crtc
->primary
);
63 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
65 if (exynos_crtc
->ops
->win_commit
)
66 exynos_crtc
->ops
->win_commit(exynos_crtc
, exynos_plane
->zpos
);
70 exynos_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
71 const struct drm_display_mode
*mode
,
72 struct drm_display_mode
*adjusted_mode
)
74 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
76 if (exynos_crtc
->ops
->mode_fixup
)
77 return exynos_crtc
->ops
->mode_fixup(exynos_crtc
, mode
,
84 exynos_drm_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
86 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
88 if (exynos_crtc
->ops
->commit
)
89 exynos_crtc
->ops
->commit(exynos_crtc
);
92 static void exynos_drm_crtc_disable(struct drm_crtc
*crtc
)
94 struct drm_plane
*plane
;
97 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
99 drm_for_each_legacy_plane(plane
, &crtc
->dev
->mode_config
.plane_list
) {
100 if (plane
->crtc
!= crtc
)
103 ret
= plane
->funcs
->disable_plane(plane
);
105 DRM_ERROR("Failed to disable plane %d\n", ret
);
109 static 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
,
114 .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
,
117 .disable
= exynos_drm_crtc_disable
,
120 static int exynos_drm_crtc_page_flip(struct drm_crtc
*crtc
,
121 struct drm_framebuffer
*fb
,
122 struct drm_pending_vblank_event
*event
,
123 uint32_t page_flip_flags
)
125 struct drm_device
*dev
= crtc
->dev
;
126 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
127 unsigned int crtc_w
, crtc_h
;
130 /* 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");
139 spin_lock_irq(&dev
->event_lock
);
140 if (exynos_crtc
->event
) {
145 ret
= exynos_check_plane(crtc
->primary
, fb
);
149 ret
= drm_vblank_get(dev
, exynos_crtc
->pipe
);
151 DRM_DEBUG("failed to acquire vblank counter\n");
155 exynos_crtc
->event
= event
;
156 spin_unlock_irq(&dev
->event_lock
);
159 * the pipe from user always is 0 so we can set pipe number
160 * of current owner to event.
162 event
->pipe
= exynos_crtc
->pipe
;
164 crtc
->primary
->fb
= fb
;
165 crtc_w
= fb
->width
- crtc
->x
;
166 crtc_h
= fb
->height
- crtc
->y
;
167 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);
174 spin_unlock_irq(&dev
->event_lock
);
178 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
180 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
181 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
183 private->crtc
[exynos_crtc
->pipe
] = NULL
;
185 drm_crtc_cleanup(crtc
);
189 static struct drm_crtc_funcs exynos_crtc_funcs
= {
190 .set_config
= drm_crtc_helper_set_config
,
191 .page_flip
= exynos_drm_crtc_page_flip
,
192 .destroy
= exynos_drm_crtc_destroy
,
193 .reset
= drm_atomic_helper_crtc_reset
,
194 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
195 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
198 struct exynos_drm_crtc
*exynos_drm_crtc_create(struct drm_device
*drm_dev
,
199 struct drm_plane
*plane
,
201 enum exynos_drm_output_type type
,
202 const struct exynos_drm_crtc_ops
*ops
,
205 struct exynos_drm_crtc
*exynos_crtc
;
206 struct exynos_drm_private
*private = drm_dev
->dev_private
;
207 struct drm_crtc
*crtc
;
210 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
212 return ERR_PTR(-ENOMEM
);
214 init_waitqueue_head(&exynos_crtc
->pending_flip_queue
);
216 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
217 exynos_crtc
->pipe
= pipe
;
218 exynos_crtc
->type
= type
;
219 exynos_crtc
->ops
= ops
;
220 exynos_crtc
->ctx
= ctx
;
222 crtc
= &exynos_crtc
->base
;
224 private->crtc
[pipe
] = crtc
;
226 ret
= drm_crtc_init_with_planes(drm_dev
, crtc
, plane
, NULL
,
231 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
236 plane
->funcs
->destroy(plane
);
241 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int pipe
)
243 struct exynos_drm_private
*private = dev
->dev_private
;
244 struct exynos_drm_crtc
*exynos_crtc
=
245 to_exynos_crtc(private->crtc
[pipe
]);
247 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
250 if (exynos_crtc
->ops
->enable_vblank
)
251 exynos_crtc
->ops
->enable_vblank(exynos_crtc
);
256 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int pipe
)
258 struct exynos_drm_private
*private = dev
->dev_private
;
259 struct exynos_drm_crtc
*exynos_crtc
=
260 to_exynos_crtc(private->crtc
[pipe
]);
262 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
265 if (exynos_crtc
->ops
->disable_vblank
)
266 exynos_crtc
->ops
->disable_vblank(exynos_crtc
);
269 void exynos_drm_crtc_finish_pageflip(struct drm_device
*dev
, int pipe
)
271 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
272 struct drm_crtc
*drm_crtc
= dev_priv
->crtc
[pipe
];
273 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(drm_crtc
);
276 spin_lock_irqsave(&dev
->event_lock
, flags
);
277 if (exynos_crtc
->event
) {
279 drm_send_vblank_event(dev
, -1, exynos_crtc
->event
);
280 drm_vblank_put(dev
, pipe
);
281 wake_up(&exynos_crtc
->pending_flip_queue
);
285 exynos_crtc
->event
= NULL
;
286 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
289 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer
*fb
)
291 struct exynos_drm_crtc
*exynos_crtc
;
292 struct drm_device
*dev
= fb
->dev
;
293 struct drm_crtc
*crtc
;
296 * make sure that overlay data are updated to real hardware
299 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
300 exynos_crtc
= to_exynos_crtc(crtc
);
303 * wait for vblank interrupt
304 * - this makes sure that overlay data are updated to
307 if (exynos_crtc
->ops
->wait_for_vblank
)
308 exynos_crtc
->ops
->wait_for_vblank(exynos_crtc
);
312 int exynos_drm_crtc_get_pipe_from_type(struct drm_device
*drm_dev
,
313 unsigned int out_type
)
315 struct drm_crtc
*crtc
;
317 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
) {
318 struct exynos_drm_crtc
*exynos_crtc
;
320 exynos_crtc
= to_exynos_crtc(crtc
);
321 if (exynos_crtc
->type
== out_type
)
322 return exynos_crtc
->pipe
;
328 void exynos_drm_crtc_te_handler(struct drm_crtc
*crtc
)
330 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
332 if (exynos_crtc
->ops
->te_handler
)
333 exynos_crtc
->ops
->te_handler(exynos_crtc
);