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>
19 #include <drm/drm_encoder.h>
21 #include "exynos_drm_crtc.h"
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_plane.h"
25 static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
26 struct drm_crtc_state *old_state)
28 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
30 if (exynos_crtc->ops->enable)
31 exynos_crtc->ops->enable(exynos_crtc);
33 drm_crtc_vblank_on(crtc);
36 static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
37 struct drm_crtc_state *old_state)
39 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
41 drm_crtc_vblank_off(crtc);
43 if (exynos_crtc->ops->disable)
44 exynos_crtc->ops->disable(exynos_crtc);
46 spin_lock_irq(&crtc->dev->event_lock);
47 if (crtc->state->event && !crtc->state->active) {
48 drm_crtc_send_vblank_event(crtc, crtc->state->event);
49 crtc->state->event = NULL;
51 spin_unlock_irq(&crtc->dev->event_lock);
54 static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
55 struct drm_crtc_state *state)
57 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
62 if (exynos_crtc->ops->atomic_check)
63 return exynos_crtc->ops->atomic_check(exynos_crtc, state);
68 static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
69 struct drm_crtc_state *old_crtc_state)
71 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
73 if (exynos_crtc->ops->atomic_begin)
74 exynos_crtc->ops->atomic_begin(exynos_crtc);
77 static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
78 struct drm_crtc_state *old_crtc_state)
80 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
82 if (exynos_crtc->ops->atomic_flush)
83 exynos_crtc->ops->atomic_flush(exynos_crtc);
86 static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
87 const struct drm_display_mode *mode)
89 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
91 if (exynos_crtc->ops->mode_valid)
92 return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
97 static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
98 .mode_valid = exynos_crtc_mode_valid,
99 .atomic_check = exynos_crtc_atomic_check,
100 .atomic_begin = exynos_crtc_atomic_begin,
101 .atomic_flush = exynos_crtc_atomic_flush,
102 .atomic_enable = exynos_drm_crtc_atomic_enable,
103 .atomic_disable = exynos_drm_crtc_atomic_disable,
106 void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
108 struct drm_crtc *crtc = &exynos_crtc->base;
109 struct drm_pending_vblank_event *event = crtc->state->event;
114 crtc->state->event = NULL;
116 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
118 spin_lock_irqsave(&crtc->dev->event_lock, flags);
119 drm_crtc_arm_vblank_event(crtc, event);
120 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
123 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
125 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
127 drm_crtc_cleanup(crtc);
131 static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
133 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
135 if (exynos_crtc->ops->enable_vblank)
136 return exynos_crtc->ops->enable_vblank(exynos_crtc);
141 static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
143 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
145 if (exynos_crtc->ops->disable_vblank)
146 exynos_crtc->ops->disable_vblank(exynos_crtc);
149 static const struct drm_crtc_funcs exynos_crtc_funcs = {
150 .set_config = drm_atomic_helper_set_config,
151 .page_flip = drm_atomic_helper_page_flip,
152 .destroy = exynos_drm_crtc_destroy,
153 .reset = drm_atomic_helper_crtc_reset,
154 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
155 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
156 .enable_vblank = exynos_drm_crtc_enable_vblank,
157 .disable_vblank = exynos_drm_crtc_disable_vblank,
160 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
161 struct drm_plane *plane,
162 enum exynos_drm_output_type type,
163 const struct exynos_drm_crtc_ops *ops,
166 struct exynos_drm_crtc *exynos_crtc;
167 struct drm_crtc *crtc;
170 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
174 exynos_crtc->type = type;
175 exynos_crtc->ops = ops;
176 exynos_crtc->ctx = ctx;
178 crtc = &exynos_crtc->base;
180 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
181 &exynos_crtc_funcs, NULL);
185 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
190 plane->funcs->destroy(plane);
195 struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
196 enum exynos_drm_output_type out_type)
198 struct drm_crtc *crtc;
200 drm_for_each_crtc(crtc, drm_dev)
201 if (to_exynos_crtc(crtc)->type == out_type)
202 return to_exynos_crtc(crtc);
204 return ERR_PTR(-EPERM);
207 int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
208 enum exynos_drm_output_type out_type)
210 struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
214 return PTR_ERR(crtc);
216 encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
221 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
223 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
225 if (exynos_crtc->ops->te_handler)
226 exynos_crtc->ops->te_handler(exynos_crtc);