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");
220 list_del(&event
->base
.link
);
225 spin_lock_irq(&dev
->event_lock
);
226 list_add_tail(&event
->base
.link
,
227 &dev_priv
->pageflip_event_list
);
228 spin_unlock_irq(&dev
->event_lock
);
231 ret
= exynos_drm_crtc_mode_set_base(crtc
, crtc
->x
, crtc
->y
,
236 spin_lock_irq(&dev
->event_lock
);
237 drm_vblank_put(dev
, exynos_crtc
->pipe
);
238 list_del(&event
->base
.link
);
239 spin_unlock_irq(&dev
->event_lock
);
245 mutex_unlock(&dev
->struct_mutex
);
249 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
251 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
252 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
254 DRM_DEBUG_KMS("%s\n", __FILE__
);
256 private->crtc
[exynos_crtc
->pipe
] = NULL
;
258 drm_crtc_cleanup(crtc
);
262 static int exynos_drm_crtc_set_property(struct drm_crtc
*crtc
,
263 struct drm_property
*property
,
266 struct drm_device
*dev
= crtc
->dev
;
267 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
268 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
270 DRM_DEBUG_KMS("%s\n", __func__
);
272 if (property
== dev_priv
->crtc_mode_property
) {
273 enum exynos_crtc_mode mode
= val
;
275 if (mode
== exynos_crtc
->mode
)
278 exynos_crtc
->mode
= mode
;
281 case CRTC_MODE_NORMAL
:
282 exynos_drm_crtc_commit(crtc
);
284 case CRTC_MODE_BLANK
:
285 exynos_plane_dpms(exynos_crtc
->plane
,
298 static struct drm_crtc_funcs exynos_crtc_funcs
= {
299 .set_config
= drm_crtc_helper_set_config
,
300 .page_flip
= exynos_drm_crtc_page_flip
,
301 .destroy
= exynos_drm_crtc_destroy
,
302 .set_property
= exynos_drm_crtc_set_property
,
305 static const struct drm_prop_enum_list mode_names
[] = {
306 { CRTC_MODE_NORMAL
, "normal" },
307 { CRTC_MODE_BLANK
, "blank" },
310 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc
*crtc
)
312 struct drm_device
*dev
= crtc
->dev
;
313 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
314 struct drm_property
*prop
;
316 DRM_DEBUG_KMS("%s\n", __func__
);
318 prop
= dev_priv
->crtc_mode_property
;
320 prop
= drm_property_create_enum(dev
, 0, "mode", mode_names
,
321 ARRAY_SIZE(mode_names
));
325 dev_priv
->crtc_mode_property
= prop
;
328 drm_object_attach_property(&crtc
->base
, prop
, 0);
331 int exynos_drm_crtc_create(struct drm_device
*dev
, unsigned int nr
)
333 struct exynos_drm_crtc
*exynos_crtc
;
334 struct exynos_drm_private
*private = dev
->dev_private
;
335 struct drm_crtc
*crtc
;
337 DRM_DEBUG_KMS("%s\n", __FILE__
);
339 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
341 DRM_ERROR("failed to allocate exynos crtc\n");
345 exynos_crtc
->pipe
= nr
;
346 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
347 exynos_crtc
->plane
= exynos_plane_init(dev
, 1 << nr
, true);
348 if (!exynos_crtc
->plane
) {
353 crtc
= &exynos_crtc
->drm_crtc
;
355 private->crtc
[nr
] = crtc
;
357 drm_crtc_init(dev
, crtc
, &exynos_crtc_funcs
);
358 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
360 exynos_drm_crtc_attach_mode_property(crtc
);
365 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int crtc
)
367 struct exynos_drm_private
*private = dev
->dev_private
;
368 struct exynos_drm_crtc
*exynos_crtc
=
369 to_exynos_crtc(private->crtc
[crtc
]);
371 DRM_DEBUG_KMS("%s\n", __FILE__
);
373 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
376 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
377 exynos_drm_enable_vblank
);
382 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int crtc
)
384 struct exynos_drm_private
*private = dev
->dev_private
;
385 struct exynos_drm_crtc
*exynos_crtc
=
386 to_exynos_crtc(private->crtc
[crtc
]);
388 DRM_DEBUG_KMS("%s\n", __FILE__
);
390 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
393 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
394 exynos_drm_disable_vblank
);
397 void exynos_drm_crtc_finish_pageflip(struct drm_device
*dev
, int crtc
)
399 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
400 struct drm_pending_vblank_event
*e
, *t
;
404 DRM_DEBUG_KMS("%s\n", __FILE__
);
406 spin_lock_irqsave(&dev
->event_lock
, flags
);
408 list_for_each_entry_safe(e
, t
, &dev_priv
->pageflip_event_list
,
410 /* if event's pipe isn't same as crtc then ignore it. */
414 do_gettimeofday(&now
);
415 e
->event
.sequence
= 0;
416 e
->event
.tv_sec
= now
.tv_sec
;
417 e
->event
.tv_usec
= now
.tv_usec
;
419 list_move_tail(&e
->base
.link
, &e
->base
.file_priv
->event_list
);
420 wake_up_interruptible(&e
->base
.file_priv
->event_wait
);
421 drm_vblank_put(dev
, crtc
);
424 spin_unlock_irqrestore(&dev
->event_lock
, flags
);