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 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
30 #include <drm/drm_crtc_helper.h>
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_encoder.h"
34 #include "exynos_drm_plane.h"
36 #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
39 enum exynos_crtc_mode
{
40 CRTC_MODE_NORMAL
, /* normal mode */
41 CRTC_MODE_BLANK
, /* The private plane of crtc is blank */
45 * Exynos specific crtc structure.
47 * @drm_crtc: crtc object.
48 * @drm_plane: pointer of private plane object for this crtc
49 * @pipe: a crtc index created at load() with a new crtc object creation
50 * and the crtc object would be set to private->crtc array
51 * to get a crtc object corresponding to this pipe from private->crtc
52 * array when irq interrupt occured. the reason of using this pipe is that
53 * drm framework doesn't support multiple irq yet.
54 * we can refer to the crtc to current hardware interrupt occured through
56 * @dpms: store the crtc dpms value
57 * @mode: store the crtc mode value
59 struct exynos_drm_crtc
{
60 struct drm_crtc drm_crtc
;
61 struct drm_plane
*plane
;
64 enum exynos_crtc_mode mode
;
67 static void exynos_drm_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
69 struct drm_device
*dev
= crtc
->dev
;
70 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
72 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc
->base
.id
, mode
);
74 if (exynos_crtc
->dpms
== mode
) {
75 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
79 mutex_lock(&dev
->struct_mutex
);
81 exynos_drm_fn_encoder(crtc
, &mode
, exynos_drm_encoder_crtc_dpms
);
82 exynos_crtc
->dpms
= mode
;
84 mutex_unlock(&dev
->struct_mutex
);
87 static void exynos_drm_crtc_prepare(struct drm_crtc
*crtc
)
89 DRM_DEBUG_KMS("%s\n", __FILE__
);
91 /* drm framework doesn't check NULL. */
94 static void exynos_drm_crtc_commit(struct drm_crtc
*crtc
)
96 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
98 DRM_DEBUG_KMS("%s\n", __FILE__
);
100 exynos_plane_commit(exynos_crtc
->plane
);
101 exynos_plane_dpms(exynos_crtc
->plane
, DRM_MODE_DPMS_ON
);
105 exynos_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
106 const struct drm_display_mode
*mode
,
107 struct drm_display_mode
*adjusted_mode
)
109 DRM_DEBUG_KMS("%s\n", __FILE__
);
111 /* drm framework doesn't check NULL */
116 exynos_drm_crtc_mode_set(struct drm_crtc
*crtc
, struct drm_display_mode
*mode
,
117 struct drm_display_mode
*adjusted_mode
, int x
, int y
,
118 struct drm_framebuffer
*old_fb
)
120 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
121 struct drm_plane
*plane
= exynos_crtc
->plane
;
124 int pipe
= exynos_crtc
->pipe
;
127 DRM_DEBUG_KMS("%s\n", __FILE__
);
129 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
132 * copy the mode data adjusted by mode_fixup() into crtc->mode
133 * so that hardware can be seet to proper mode.
135 memcpy(&crtc
->mode
, adjusted_mode
, sizeof(*adjusted_mode
));
137 crtc_w
= crtc
->fb
->width
- x
;
138 crtc_h
= crtc
->fb
->height
- y
;
140 ret
= exynos_plane_mode_set(plane
, crtc
, crtc
->fb
, 0, 0, crtc_w
, crtc_h
,
141 x
, y
, crtc_w
, crtc_h
);
146 plane
->fb
= crtc
->fb
;
148 exynos_drm_fn_encoder(crtc
, &pipe
, exynos_drm_encoder_crtc_pipe
);
153 static int exynos_drm_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
154 struct drm_framebuffer
*old_fb
)
156 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
157 struct drm_plane
*plane
= exynos_crtc
->plane
;
162 DRM_DEBUG_KMS("%s\n", __FILE__
);
164 crtc_w
= crtc
->fb
->width
- x
;
165 crtc_h
= crtc
->fb
->height
- y
;
167 ret
= exynos_plane_mode_set(plane
, crtc
, crtc
->fb
, 0, 0, crtc_w
, crtc_h
,
168 x
, y
, crtc_w
, crtc_h
);
172 exynos_drm_crtc_commit(crtc
);
177 static void exynos_drm_crtc_load_lut(struct drm_crtc
*crtc
)
179 DRM_DEBUG_KMS("%s\n", __FILE__
);
180 /* drm framework doesn't check NULL */
183 static void exynos_drm_crtc_disable(struct drm_crtc
*crtc
)
185 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
187 DRM_DEBUG_KMS("%s\n", __FILE__
);
189 exynos_plane_dpms(exynos_crtc
->plane
, DRM_MODE_DPMS_OFF
);
190 exynos_drm_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
193 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs
= {
194 .dpms
= exynos_drm_crtc_dpms
,
195 .prepare
= exynos_drm_crtc_prepare
,
196 .commit
= exynos_drm_crtc_commit
,
197 .mode_fixup
= exynos_drm_crtc_mode_fixup
,
198 .mode_set
= exynos_drm_crtc_mode_set
,
199 .mode_set_base
= exynos_drm_crtc_mode_set_base
,
200 .load_lut
= exynos_drm_crtc_load_lut
,
201 .disable
= exynos_drm_crtc_disable
,
204 static int exynos_drm_crtc_page_flip(struct drm_crtc
*crtc
,
205 struct drm_framebuffer
*fb
,
206 struct drm_pending_vblank_event
*event
)
208 struct drm_device
*dev
= crtc
->dev
;
209 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
210 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
211 struct drm_framebuffer
*old_fb
= crtc
->fb
;
214 DRM_DEBUG_KMS("%s\n", __FILE__
);
216 mutex_lock(&dev
->struct_mutex
);
220 * the pipe from user always is 0 so we can set pipe number
221 * of current owner to event.
223 event
->pipe
= exynos_crtc
->pipe
;
225 ret
= drm_vblank_get(dev
, exynos_crtc
->pipe
);
227 DRM_DEBUG("failed to acquire vblank counter\n");
228 list_del(&event
->base
.link
);
233 list_add_tail(&event
->base
.link
,
234 &dev_priv
->pageflip_event_list
);
237 ret
= exynos_drm_crtc_mode_set_base(crtc
, crtc
->x
, crtc
->y
,
241 drm_vblank_put(dev
, exynos_crtc
->pipe
);
242 list_del(&event
->base
.link
);
248 mutex_unlock(&dev
->struct_mutex
);
252 static void exynos_drm_crtc_destroy(struct drm_crtc
*crtc
)
254 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
255 struct exynos_drm_private
*private = crtc
->dev
->dev_private
;
257 DRM_DEBUG_KMS("%s\n", __FILE__
);
259 private->crtc
[exynos_crtc
->pipe
] = NULL
;
261 drm_crtc_cleanup(crtc
);
265 static int exynos_drm_crtc_set_property(struct drm_crtc
*crtc
,
266 struct drm_property
*property
,
269 struct drm_device
*dev
= crtc
->dev
;
270 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
271 struct exynos_drm_crtc
*exynos_crtc
= to_exynos_crtc(crtc
);
273 DRM_DEBUG_KMS("%s\n", __func__
);
275 if (property
== dev_priv
->crtc_mode_property
) {
276 enum exynos_crtc_mode mode
= val
;
278 if (mode
== exynos_crtc
->mode
)
281 exynos_crtc
->mode
= mode
;
284 case CRTC_MODE_NORMAL
:
285 exynos_drm_crtc_commit(crtc
);
287 case CRTC_MODE_BLANK
:
288 exynos_plane_dpms(exynos_crtc
->plane
,
301 static struct drm_crtc_funcs exynos_crtc_funcs
= {
302 .set_config
= drm_crtc_helper_set_config
,
303 .page_flip
= exynos_drm_crtc_page_flip
,
304 .destroy
= exynos_drm_crtc_destroy
,
305 .set_property
= exynos_drm_crtc_set_property
,
308 static const struct drm_prop_enum_list mode_names
[] = {
309 { CRTC_MODE_NORMAL
, "normal" },
310 { CRTC_MODE_BLANK
, "blank" },
313 static void exynos_drm_crtc_attach_mode_property(struct drm_crtc
*crtc
)
315 struct drm_device
*dev
= crtc
->dev
;
316 struct exynos_drm_private
*dev_priv
= dev
->dev_private
;
317 struct drm_property
*prop
;
319 DRM_DEBUG_KMS("%s\n", __func__
);
321 prop
= dev_priv
->crtc_mode_property
;
323 prop
= drm_property_create_enum(dev
, 0, "mode", mode_names
,
324 ARRAY_SIZE(mode_names
));
328 dev_priv
->crtc_mode_property
= prop
;
331 drm_object_attach_property(&crtc
->base
, prop
, 0);
334 int exynos_drm_crtc_create(struct drm_device
*dev
, unsigned int nr
)
336 struct exynos_drm_crtc
*exynos_crtc
;
337 struct exynos_drm_private
*private = dev
->dev_private
;
338 struct drm_crtc
*crtc
;
340 DRM_DEBUG_KMS("%s\n", __FILE__
);
342 exynos_crtc
= kzalloc(sizeof(*exynos_crtc
), GFP_KERNEL
);
344 DRM_ERROR("failed to allocate exynos crtc\n");
348 exynos_crtc
->pipe
= nr
;
349 exynos_crtc
->dpms
= DRM_MODE_DPMS_OFF
;
350 exynos_crtc
->plane
= exynos_plane_init(dev
, 1 << nr
, true);
351 if (!exynos_crtc
->plane
) {
356 crtc
= &exynos_crtc
->drm_crtc
;
358 private->crtc
[nr
] = crtc
;
360 drm_crtc_init(dev
, crtc
, &exynos_crtc_funcs
);
361 drm_crtc_helper_add(crtc
, &exynos_crtc_helper_funcs
);
363 exynos_drm_crtc_attach_mode_property(crtc
);
368 int exynos_drm_crtc_enable_vblank(struct drm_device
*dev
, int crtc
)
370 struct exynos_drm_private
*private = dev
->dev_private
;
371 struct exynos_drm_crtc
*exynos_crtc
=
372 to_exynos_crtc(private->crtc
[crtc
]);
374 DRM_DEBUG_KMS("%s\n", __FILE__
);
376 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
379 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
380 exynos_drm_enable_vblank
);
385 void exynos_drm_crtc_disable_vblank(struct drm_device
*dev
, int crtc
)
387 struct exynos_drm_private
*private = dev
->dev_private
;
388 struct exynos_drm_crtc
*exynos_crtc
=
389 to_exynos_crtc(private->crtc
[crtc
]);
391 DRM_DEBUG_KMS("%s\n", __FILE__
);
393 if (exynos_crtc
->dpms
!= DRM_MODE_DPMS_ON
)
396 exynos_drm_fn_encoder(private->crtc
[crtc
], &crtc
,
397 exynos_drm_disable_vblank
);