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 !atomic_read(&exynos_crtc
->pending_flip
),
39 atomic_set(&exynos_crtc
->pending_flip
, 0);
40 drm_crtc_vblank_off(crtc
);
43 if (exynos_crtc
->ops
->dpms
)
44 exynos_crtc
->ops
->dpms(exynos_crtc
, mode
);
46 exynos_crtc
->dpms
= mode
;
48 if (mode
== DRM_MODE_DPMS_ON
)
49 drm_crtc_vblank_on(crtc
);
52 static void exynos_drm_crtc_prepare(struct drm_crtc
*crtc
)
54 /* drm framework doesn't check NULL. */
57 static void exynos_drm_crtc_commit(struct drm_crtc
*crtc
)
59 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
60 struct exynos_drm_plane
*exynos_plane
= to_exynos_plane(crtc
->primary
);
62 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
64 if (exynos_crtc
->ops
->win_commit
)
65 exynos_crtc
->ops
->win_commit(exynos_crtc
, exynos_plane
->zpos
);
67 if (exynos_crtc
->ops
->commit
)
68 exynos_crtc
->ops
->commit(exynos_crtc
);
70 exynos_plane_dpms(crtc
->primary
, DRM_MODE_DPMS_ON
);
74 exynos_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
75 const struct drm_display_mode
*mode
,
76 struct drm_display_mode
*adjusted_mode
)
78 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
80 if (exynos_crtc
->ops
->mode_fixup
)
81 return exynos_crtc
->ops
->mode_fixup(exynos_crtc
, mode
,
88 exynos_drm_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
,
89 struct drm_display_mode
*adjusted_mode
, int x
, int y
,
90 struct drm_framebuffer
*old_fb
)
92 struct drm_framebuffer
*fb
= crtc
->primary
->fb
;
98 * copy the mode data adjusted by mode_fixup() into crtc->mode
99 * so that hardware can be seet to proper mode.
101 memcpy(&crtc
->mode
, adjusted_mode
, sizeof(*adjusted_mode
));
103 ret
= exynos_check_plane(crtc
->primary
, fb
);
107 crtc_w
= fb
->width
- x
;
108 crtc_h
= fb
->height
- y
;
109 exynos_plane_mode_set(crtc
->primary
, crtc
, fb
, 0, 0,
110 crtc_w
, crtc_h
, x
, y
, crtc_w
, crtc_h
);
115 static int exynos_drm_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
116 struct drm_framebuffer
*old_fb
)
118 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
119 struct drm_framebuffer
*fb
= crtc
->primary
->fb
;
123 /* when framebuffer changing is requested, crtc's dpms should be on */
124 if (exynos_crtc
->dpms
> DRM_MODE_DPMS_ON
) {
125 DRM_ERROR("failed framebuffer changing request.\n");
129 crtc_w
= fb
->width
- x
;
130 crtc_h
= fb
->height
- y
;
132 return exynos_update_plane(crtc
->primary
, crtc
, fb
, 0, 0,
133 crtc_w
, crtc_h
, x
, y
, crtc_w
, crtc_h
);
136 static void exynos_drm_crtc_disable(struct drm_crtc
*crtc
)
138 struct drm_plane
*plane
;
141 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
143 drm_for_each_legacy_plane(plane
, &crtc
->dev
->mode_config
.plane_list
) {
144 if (plane
->crtc
!= crtc
)
147 ret
= plane
->funcs
->disable_plane(plane
);
149 DRM_ERROR("Failed to disable plane %d\n", ret
);
153 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs
= {
154 .dpms
= exynos_drm_crtc_dpms
,
155 .prepare
= exynos_drm_crtc_prepare
,
156 .commit
= exynos_drm_crtc_commit
,
157 .mode_fixup
= exynos_drm_crtc_mode_fixup
,
158 .mode_set
= exynos_drm_crtc_mode_set
,
159 .mode_set_base
= exynos_drm_crtc_mode_set_base
,
160 .disable
= exynos_drm_crtc_disable
,
163 static int exynos_drm_crtc_page_flip(struct drm_crtc
*crtc
,
164 struct drm_framebuffer
*fb
,
165 struct drm_pending_vblank_event
*event
,
166 uint32_t page_flip_flags
)
168 struct drm_device
*dev
= crtc
->dev
;
169 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
170 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
171 struct drm_framebuffer
*old_fb
= crtc
->primary
->fb
;
172 unsigned int crtc_w
, crtc_h
;
175 /* when the page flip is requested, crtc's dpms should be on */
176 if (exynos_crtc
->dpms
> DRM_MODE_DPMS_ON
) {
177 DRM_ERROR("failed page flip request.\n");
181 mutex_lock(&dev
->struct_mutex
);
185 * the pipe from user always is 0 so we can set pipe number
186 * of current owner to event.
188 event
->pipe
= exynos_crtc
->pipe
;
190 ret
= drm_vblank_get(dev
, exynos_crtc
->pipe
);
192 DRM_DEBUG("failed to acquire vblank counter\n");
197 spin_lock_irq(&dev
->event_lock
);
198 list_add_tail(&event
->base
.link
,
199 &dev_priv
->pageflip_event_list
);
200 atomic_set(&exynos_crtc
->pending_flip
, 1);
201 spin_unlock_irq(&dev
->event_lock
);
203 crtc
->primary
->fb
= fb
;
204 crtc_w
= fb
->width
- crtc
->x
;
205 crtc_h
= fb
->height
- crtc
->y
;
206 ret
= exynos_update_plane(crtc
->primary
, crtc
, fb
, 0, 0,
207 crtc_w
, crtc_h
, crtc
->x
, crtc
->y
,
210 crtc
->primary
->fb
= old_fb
;
212 spin_lock_irq(&dev
->event_lock
);
213 drm_vblank_put(dev
, exynos_crtc
->pipe
);
214 list_del(&event
->base
.link
);
215 atomic_set(&exynos_crtc
->pending_flip
, 0);
216 spin_unlock_irq(&dev
->event_lock
);
222 mutex_unlock(&dev
->struct_mutex
);
226 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
228 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
229 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
231 private->crtc
[exynos_crtc
->pipe
] = NULL
;
233 drm_crtc_cleanup(crtc
);
237 static int exynos_drm_crtc_set_property(struct drm_crtc
*crtc
,
238 struct drm_property
*property
,
241 struct drm_device
*dev
= crtc
->dev
;
242 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
243 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
245 if (property
== dev_priv
->crtc_mode_property
) {
246 enum exynos_crtc_mode mode
= val
;
248 if (mode
== exynos_crtc
->mode
)
251 exynos_crtc
->mode
= mode
;
254 case CRTC_MODE_NORMAL
:
255 exynos_drm_crtc_commit(crtc
);
257 case CRTC_MODE_BLANK
:
258 exynos_plane_dpms(crtc
->primary
, DRM_MODE_DPMS_OFF
);
270 static struct drm_crtc_funcs exynos_crtc_funcs
= {
271 .set_config
= drm_crtc_helper_set_config
,
272 .page_flip
= exynos_drm_crtc_page_flip
,
273 .destroy
= exynos_drm_crtc_destroy
,
274 .set_property
= exynos_drm_crtc_set_property
,
277 static const struct drm_prop_enum_list mode_names
[] = {
278 { CRTC_MODE_NORMAL
, "normal" },
279 { CRTC_MODE_BLANK
, "blank" },
282 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc
*crtc
)
284 struct drm_device
*dev
= crtc
->dev
;
285 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
286 struct drm_property
*prop
;
288 prop
= dev_priv
->crtc_mode_property
;
290 prop
= drm_property_create_enum(dev
, 0, "mode", mode_names
,
291 ARRAY_SIZE(mode_names
));
295 dev_priv
->crtc_mode_property
= prop
;
298 drm_object_attach_property(&crtc
->base
, prop
, 0);
301 struct exynos_drm_crtc
*exynos_drm_crtc_create(struct drm_device
*drm_dev
,
303 enum exynos_drm_output_type type
,
304 struct exynos_drm_crtc_ops
*ops
,
307 struct exynos_drm_crtc
*exynos_crtc
;
308 struct drm_plane
*plane
;
309 struct exynos_drm_private
*private = drm_dev
->dev_private
;
310 struct drm_crtc
*crtc
;
313 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
315 return ERR_PTR(-ENOMEM
);
317 init_waitqueue_head(&exynos_crtc
->pending_flip_queue
);
318 atomic_set(&exynos_crtc
->pending_flip
, 0);
320 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
321 exynos_crtc
->pipe
= pipe
;
322 exynos_crtc
->type
= type
;
323 exynos_crtc
->ops
= ops
;
324 exynos_crtc
->ctx
= ctx
;
325 plane
= exynos_plane_init(drm_dev
, 1 << pipe
,
326 DRM_PLANE_TYPE_PRIMARY
);
328 ret
= PTR_ERR(plane
);
332 crtc
= &exynos_crtc
->base
;
334 private->crtc
[pipe
] = crtc
;
336 ret
= drm_crtc_init_with_planes(drm_dev
, crtc
, plane
, NULL
,
341 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
343 exynos_drm_crtc_attach_mode_property(crtc
);
348 plane
->funcs
->destroy(plane
);
354 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int pipe
)
356 struct exynos_drm_private
*private = dev
->dev_private
;
357 struct exynos_drm_crtc
*exynos_crtc
=
358 to_exynos_crtc(private->crtc
[pipe
]);
360 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
363 if (exynos_crtc
->ops
->enable_vblank
)
364 exynos_crtc
->ops
->enable_vblank(exynos_crtc
);
369 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int pipe
)
371 struct exynos_drm_private
*private = dev
->dev_private
;
372 struct exynos_drm_crtc
*exynos_crtc
=
373 to_exynos_crtc(private->crtc
[pipe
]);
375 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
378 if (exynos_crtc
->ops
->disable_vblank
)
379 exynos_crtc
->ops
->disable_vblank(exynos_crtc
);
382 void exynos_drm_crtc_finish_pageflip(struct drm_device
*dev
, int pipe
)
384 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
385 struct drm_pending_vblank_event
*e
, *t
;
386 struct drm_crtc
*drm_crtc
= dev_priv
->crtc
[pipe
];
387 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(drm_crtc
);
390 spin_lock_irqsave(&dev
->event_lock
, flags
);
392 list_for_each_entry_safe(e
, t
, &dev_priv
->pageflip_event_list
,
394 /* if event's pipe isn't same as crtc then ignore it. */
398 list_del(&e
->base
.link
);
399 drm_send_vblank_event(dev
, -1, e
);
400 drm_vblank_put(dev
, pipe
);
401 atomic_set(&exynos_crtc
->pending_flip
, 0);
402 wake_up(&exynos_crtc
->pending_flip_queue
);
405 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
408 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer
*fb
)
410 struct exynos_drm_crtc
*exynos_crtc
;
411 struct drm_device
*dev
= fb
->dev
;
412 struct drm_crtc
*crtc
;
415 * make sure that overlay data are updated to real hardware
418 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
419 exynos_crtc
= to_exynos_crtc(crtc
);
422 * wait for vblank interrupt
423 * - this makes sure that overlay data are updated to
426 if (exynos_crtc
->ops
->wait_for_vblank
)
427 exynos_crtc
->ops
->wait_for_vblank(exynos_crtc
);
431 int exynos_drm_crtc_get_pipe_from_type(struct drm_device
*drm_dev
,
432 unsigned int out_type
)
434 struct drm_crtc
*crtc
;
436 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
) {
437 struct exynos_drm_crtc
*exynos_crtc
;
439 exynos_crtc
= to_exynos_crtc(crtc
);
440 if (exynos_crtc
->type
== out_type
)
441 return exynos_crtc
->pipe
;
447 void exynos_drm_crtc_te_handler(struct drm_crtc
*crtc
)
449 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
451 if (exynos_crtc
->ops
->te_handler
)
452 exynos_crtc
->ops
->te_handler(exynos_crtc
);