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_drv.h"
19 #include "exynos_drm_encoder.h"
20 #include "exynos_drm_plane.h"
22 #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
25 enum exynos_crtc_mode
{
26 CRTC_MODE_NORMAL
, /* normal mode */
27 CRTC_MODE_BLANK
, /* The private plane of crtc is blank */
31 * Exynos specific crtc structure.
33 * @drm_crtc: crtc object.
34 * @drm_plane: pointer of private plane object for this crtc
35 * @pipe: a crtc index created at load() with a new crtc object creation
36 * and the crtc object would be set to private->crtc array
37 * to get a crtc object corresponding to this pipe from private->crtc
38 * array when irq interrupt occured. the reason of using this pipe is that
39 * drm framework doesn't support multiple irq yet.
40 * we can refer to the crtc to current hardware interrupt occured through
42 * @dpms: store the crtc dpms value
43 * @mode: store the crtc mode value
45 struct exynos_drm_crtc
{
46 struct drm_crtc drm_crtc
;
47 struct drm_plane
*plane
;
50 enum exynos_crtc_mode mode
;
53 static void exynos_drm_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
55 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
57 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc
->base
.id
, mode
);
59 if (exynos_crtc
->dpms
== mode
) {
60 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
64 exynos_drm_fn_encoder(crtc
, &mode
, exynos_drm_encoder_crtc_dpms
);
65 exynos_crtc
->dpms
= mode
;
68 static void exynos_drm_crtc_prepare(struct drm_crtc
*crtc
)
70 DRM_DEBUG_KMS("%s\n", __FILE__
);
72 /* drm framework doesn't check NULL. */
75 static void exynos_drm_crtc_commit(struct drm_crtc
*crtc
)
77 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
79 DRM_DEBUG_KMS("%s\n", __FILE__
);
81 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
82 exynos_plane_commit(exynos_crtc
->plane
);
83 exynos_plane_dpms(exynos_crtc
->plane
, DRM_MODE_DPMS_ON
);
87 exynos_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
88 const struct drm_display_mode
*mode
,
89 struct drm_display_mode
*adjusted_mode
)
91 DRM_DEBUG_KMS("%s\n", __FILE__
);
93 /* drm framework doesn't check NULL */
98 exynos_drm_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
,
99 struct drm_display_mode
*adjusted_mode
, int x
, int y
,
100 struct drm_framebuffer
*old_fb
)
102 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
103 struct drm_plane
*plane
= exynos_crtc
->plane
;
106 int pipe
= exynos_crtc
->pipe
;
109 DRM_DEBUG_KMS("%s\n", __FILE__
);
112 * copy the mode data adjusted by mode_fixup() into crtc->mode
113 * so that hardware can be seet to proper mode.
115 memcpy(&crtc
->mode
, adjusted_mode
, sizeof(*adjusted_mode
));
117 crtc_w
= crtc
->fb
->width
- x
;
118 crtc_h
= crtc
->fb
->height
- y
;
120 ret
= exynos_plane_mode_set(plane
, crtc
, crtc
->fb
, 0, 0, crtc_w
, crtc_h
,
121 x
, y
, crtc_w
, crtc_h
);
126 plane
->fb
= crtc
->fb
;
128 exynos_drm_fn_encoder(crtc
, &pipe
, exynos_drm_encoder_crtc_pipe
);
133 static int exynos_drm_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
134 struct drm_framebuffer
*old_fb
)
136 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
137 struct drm_plane
*plane
= exynos_crtc
->plane
;
142 DRM_DEBUG_KMS("%s\n", __FILE__
);
144 /* 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");
150 crtc_w
= crtc
->fb
->width
- x
;
151 crtc_h
= crtc
->fb
->height
- y
;
153 ret
= exynos_plane_mode_set(plane
, crtc
, crtc
->fb
, 0, 0, crtc_w
, crtc_h
,
154 x
, y
, crtc_w
, crtc_h
);
158 exynos_drm_crtc_commit(crtc
);
163 static void exynos_drm_crtc_load_lut(struct drm_crtc
*crtc
)
165 DRM_DEBUG_KMS("%s\n", __FILE__
);
166 /* drm framework doesn't check NULL */
169 static void exynos_drm_crtc_disable(struct drm_crtc
*crtc
)
171 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
173 DRM_DEBUG_KMS("%s\n", __FILE__
);
175 exynos_plane_dpms(exynos_crtc
->plane
, DRM_MODE_DPMS_OFF
);
176 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
179 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs
= {
180 .dpms
= exynos_drm_crtc_dpms
,
181 .prepare
= exynos_drm_crtc_prepare
,
182 .commit
= exynos_drm_crtc_commit
,
183 .mode_fixup
= exynos_drm_crtc_mode_fixup
,
184 .mode_set
= exynos_drm_crtc_mode_set
,
185 .mode_set_base
= exynos_drm_crtc_mode_set_base
,
186 .load_lut
= exynos_drm_crtc_load_lut
,
187 .disable
= exynos_drm_crtc_disable
,
190 static int exynos_drm_crtc_page_flip(struct drm_crtc
*crtc
,
191 struct drm_framebuffer
*fb
,
192 struct drm_pending_vblank_event
*event
)
194 struct drm_device
*dev
= crtc
->dev
;
195 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
196 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
197 struct drm_framebuffer
*old_fb
= crtc
->fb
;
200 DRM_DEBUG_KMS("%s\n", __FILE__
);
202 /* when the page flip is requested, crtc's dpms should be on */
203 if (exynos_crtc
->dpms
> DRM_MODE_DPMS_ON
) {
204 DRM_ERROR("failed page flip request.\n");
208 mutex_lock(&dev
->struct_mutex
);
212 * the pipe from user always is 0 so we can set pipe number
213 * of current owner to event.
215 event
->pipe
= exynos_crtc
->pipe
;
217 ret
= drm_vblank_get(dev
, exynos_crtc
->pipe
);
219 DRM_DEBUG("failed to acquire vblank counter\n");
224 spin_lock_irq(&dev
->event_lock
);
225 list_add_tail(&event
->base
.link
,
226 &dev_priv
->pageflip_event_list
);
227 spin_unlock_irq(&dev
->event_lock
);
230 ret
= exynos_drm_crtc_mode_set_base(crtc
, crtc
->x
, crtc
->y
,
235 spin_lock_irq(&dev
->event_lock
);
236 drm_vblank_put(dev
, exynos_crtc
->pipe
);
237 list_del(&event
->base
.link
);
238 spin_unlock_irq(&dev
->event_lock
);
244 mutex_unlock(&dev
->struct_mutex
);
248 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
250 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
251 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
253 DRM_DEBUG_KMS("%s\n", __FILE__
);
255 private->crtc
[exynos_crtc
->pipe
] = NULL
;
257 drm_crtc_cleanup(crtc
);
261 static int exynos_drm_crtc_set_property(struct drm_crtc
*crtc
,
262 struct drm_property
*property
,
265 struct drm_device
*dev
= crtc
->dev
;
266 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
267 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
269 DRM_DEBUG_KMS("%s\n", __func__
);
271 if (property
== dev_priv
->crtc_mode_property
) {
272 enum exynos_crtc_mode mode
= val
;
274 if (mode
== exynos_crtc
->mode
)
277 exynos_crtc
->mode
= mode
;
280 case CRTC_MODE_NORMAL
:
281 exynos_drm_crtc_commit(crtc
);
283 case CRTC_MODE_BLANK
:
284 exynos_plane_dpms(exynos_crtc
->plane
,
297 static struct drm_crtc_funcs exynos_crtc_funcs
= {
298 .set_config
= drm_crtc_helper_set_config
,
299 .page_flip
= exynos_drm_crtc_page_flip
,
300 .destroy
= exynos_drm_crtc_destroy
,
301 .set_property
= exynos_drm_crtc_set_property
,
304 static const struct drm_prop_enum_list mode_names
[] = {
305 { CRTC_MODE_NORMAL
, "normal" },
306 { CRTC_MODE_BLANK
, "blank" },
309 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc
*crtc
)
311 struct drm_device
*dev
= crtc
->dev
;
312 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
313 struct drm_property
*prop
;
315 DRM_DEBUG_KMS("%s\n", __func__
);
317 prop
= dev_priv
->crtc_mode_property
;
319 prop
= drm_property_create_enum(dev
, 0, "mode", mode_names
,
320 ARRAY_SIZE(mode_names
));
324 dev_priv
->crtc_mode_property
= prop
;
327 drm_object_attach_property(&crtc
->base
, prop
, 0);
330 int exynos_drm_crtc_create(struct drm_device
*dev
, unsigned int nr
)
332 struct exynos_drm_crtc
*exynos_crtc
;
333 struct exynos_drm_private
*private = dev
->dev_private
;
334 struct drm_crtc
*crtc
;
336 DRM_DEBUG_KMS("%s\n", __FILE__
);
338 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
340 DRM_ERROR("failed to allocate exynos crtc\n");
344 exynos_crtc
->pipe
= nr
;
345 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
346 exynos_crtc
->plane
= exynos_plane_init(dev
, 1 << nr
, true);
347 if (!exynos_crtc
->plane
) {
352 crtc
= &exynos_crtc
->drm_crtc
;
354 private->crtc
[nr
] = crtc
;
356 drm_crtc_init(dev
, crtc
, &exynos_crtc_funcs
);
357 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
359 exynos_drm_crtc_attach_mode_property(crtc
);
364 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int crtc
)
366 struct exynos_drm_private
*private = dev
->dev_private
;
367 struct exynos_drm_crtc
*exynos_crtc
=
368 to_exynos_crtc(private->crtc
[crtc
]);
370 DRM_DEBUG_KMS("%s\n", __FILE__
);
372 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
375 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
376 exynos_drm_enable_vblank
);
381 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int crtc
)
383 struct exynos_drm_private
*private = dev
->dev_private
;
384 struct exynos_drm_crtc
*exynos_crtc
=
385 to_exynos_crtc(private->crtc
[crtc
]);
387 DRM_DEBUG_KMS("%s\n", __FILE__
);
389 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
392 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
393 exynos_drm_disable_vblank
);
396 void exynos_drm_crtc_finish_pageflip(struct drm_device
*dev
, int crtc
)
398 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
399 struct drm_pending_vblank_event
*e
, *t
;
402 DRM_DEBUG_KMS("%s\n", __FILE__
);
404 spin_lock_irqsave(&dev
->event_lock
, flags
);
406 list_for_each_entry_safe(e
, t
, &dev_priv
->pageflip_event_list
,
408 /* if event's pipe isn't same as crtc then ignore it. */
412 list_del(&e
->base
.link
);
413 drm_send_vblank_event(dev
, -1, e
);
414 drm_vblank_put(dev
, crtc
);
417 spin_unlock_irqrestore(&dev
->event_lock
, flags
);