1 /* exynos_drm_encoder.c
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_connector.h"
22 #define to_exynos_encoder(x) container_of(x, struct exynos_drm_encoder,\
26 * exynos specific encoder structure.
28 * @drm_encoder: encoder object.
29 * @manager: specific encoder has its own manager to control a hardware
30 * appropriately and we can access a hardware drawing on this manager.
31 * @dpms: store the encoder dpms value.
32 * @updated: indicate whether overlay data updating is needed or not.
34 struct exynos_drm_encoder
{
35 struct drm_crtc
*old_crtc
;
36 struct drm_encoder drm_encoder
;
37 struct exynos_drm_manager
*manager
;
42 static void exynos_drm_connector_power(struct drm_encoder
*encoder
, int mode
)
44 struct drm_device
*dev
= encoder
->dev
;
45 struct drm_connector
*connector
;
47 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
48 if (exynos_drm_best_encoder(connector
) == encoder
) {
49 DRM_DEBUG_KMS("connector[%d] dpms[%d]\n",
50 connector
->base
.id
, mode
);
52 exynos_drm_display_power(connector
, mode
);
57 static void exynos_drm_encoder_dpms(struct drm_encoder
*encoder
, int mode
)
59 struct drm_device
*dev
= encoder
->dev
;
60 struct exynos_drm_manager
*manager
= exynos_drm_get_manager(encoder
);
61 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
62 struct exynos_drm_encoder
*exynos_encoder
= to_exynos_encoder(encoder
);
64 DRM_DEBUG_KMS("encoder dpms: %d\n", mode
);
66 if (exynos_encoder
->dpms
== mode
) {
67 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
71 mutex_lock(&dev
->struct_mutex
);
74 case DRM_MODE_DPMS_ON
:
75 if (manager_ops
&& manager_ops
->apply
)
76 if (!exynos_encoder
->updated
)
77 manager_ops
->apply(manager
->dev
);
79 exynos_drm_connector_power(encoder
, mode
);
80 exynos_encoder
->dpms
= mode
;
82 case DRM_MODE_DPMS_STANDBY
:
83 case DRM_MODE_DPMS_SUSPEND
:
84 case DRM_MODE_DPMS_OFF
:
85 exynos_drm_connector_power(encoder
, mode
);
86 exynos_encoder
->dpms
= mode
;
87 exynos_encoder
->updated
= false;
90 DRM_ERROR("unspecified mode %d\n", mode
);
94 mutex_unlock(&dev
->struct_mutex
);
98 exynos_drm_encoder_mode_fixup(struct drm_encoder
*encoder
,
99 const struct drm_display_mode
*mode
,
100 struct drm_display_mode
*adjusted_mode
)
102 struct drm_device
*dev
= encoder
->dev
;
103 struct drm_connector
*connector
;
104 struct exynos_drm_manager
*manager
= exynos_drm_get_manager(encoder
);
105 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
107 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
108 if (connector
->encoder
== encoder
)
109 if (manager_ops
&& manager_ops
->mode_fixup
)
110 manager_ops
->mode_fixup(manager
->dev
, connector
,
111 mode
, adjusted_mode
);
117 static void disable_plane_to_crtc(struct drm_device
*dev
,
118 struct drm_crtc
*old_crtc
,
119 struct drm_crtc
*new_crtc
)
121 struct drm_plane
*plane
;
124 * if old_crtc isn't same as encoder->crtc then it means that
125 * user changed crtc id to another one so the plane to old_crtc
126 * should be disabled and plane->crtc should be set to new_crtc
129 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
130 if (plane
->crtc
== old_crtc
) {
132 * do not change below call order.
134 * plane->funcs->disable_plane call checks
135 * if encoder->crtc is same as plane->crtc and if same
136 * then overlay_ops->disable callback will be called
137 * to diasble current hw overlay so plane->crtc should
138 * have new_crtc because new_crtc was set to
139 * encoder->crtc in advance.
141 plane
->crtc
= new_crtc
;
142 plane
->funcs
->disable_plane(plane
);
147 static void exynos_drm_encoder_mode_set(struct drm_encoder
*encoder
,
148 struct drm_display_mode
*mode
,
149 struct drm_display_mode
*adjusted_mode
)
151 struct drm_device
*dev
= encoder
->dev
;
152 struct drm_connector
*connector
;
153 struct exynos_drm_manager
*manager
;
154 struct exynos_drm_manager_ops
*manager_ops
;
156 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
, head
) {
157 if (connector
->encoder
== encoder
) {
158 struct exynos_drm_encoder
*exynos_encoder
;
160 exynos_encoder
= to_exynos_encoder(encoder
);
162 if (exynos_encoder
->old_crtc
!= encoder
->crtc
&&
163 exynos_encoder
->old_crtc
) {
166 * disable a plane to old crtc and change
167 * crtc of the plane to new one.
169 disable_plane_to_crtc(dev
,
170 exynos_encoder
->old_crtc
,
174 manager
= exynos_drm_get_manager(encoder
);
175 manager_ops
= manager
->ops
;
177 if (manager_ops
&& manager_ops
->mode_set
)
178 manager_ops
->mode_set(manager
->dev
,
181 exynos_encoder
->old_crtc
= encoder
->crtc
;
186 static void exynos_drm_encoder_prepare(struct drm_encoder
*encoder
)
188 /* drm framework doesn't check NULL. */
191 static void exynos_drm_encoder_commit(struct drm_encoder
*encoder
)
193 struct exynos_drm_encoder
*exynos_encoder
= to_exynos_encoder(encoder
);
194 struct exynos_drm_manager
*manager
= exynos_encoder
->manager
;
195 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
197 if (manager_ops
&& manager_ops
->commit
)
198 manager_ops
->commit(manager
->dev
);
201 * this will avoid one issue that overlay data is updated to
202 * real hardware two times.
203 * And this variable will be used to check if the data was
204 * already updated or not by exynos_drm_encoder_dpms function.
206 exynos_encoder
->updated
= true;
209 * In case of setcrtc, there is no way to update encoder's dpms
212 exynos_encoder
->dpms
= DRM_MODE_DPMS_ON
;
215 void exynos_drm_encoder_complete_scanout(struct drm_framebuffer
*fb
)
217 struct exynos_drm_encoder
*exynos_encoder
;
218 struct exynos_drm_manager_ops
*ops
;
219 struct drm_device
*dev
= fb
->dev
;
220 struct drm_encoder
*encoder
;
223 * make sure that overlay data are updated to real hardware
226 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
227 exynos_encoder
= to_exynos_encoder(encoder
);
228 ops
= exynos_encoder
->manager
->ops
;
231 * wait for vblank interrupt
232 * - this makes sure that overlay data are updated to
235 if (ops
->wait_for_vblank
)
236 ops
->wait_for_vblank(exynos_encoder
->manager
->dev
);
241 static void exynos_drm_encoder_disable(struct drm_encoder
*encoder
)
243 struct drm_plane
*plane
;
244 struct drm_device
*dev
= encoder
->dev
;
246 exynos_drm_encoder_dpms(encoder
, DRM_MODE_DPMS_OFF
);
248 /* all planes connected to this encoder should be also disabled. */
249 list_for_each_entry(plane
, &dev
->mode_config
.plane_list
, head
) {
250 if (plane
->crtc
== encoder
->crtc
)
251 plane
->funcs
->disable_plane(plane
);
255 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs
= {
256 .dpms
= exynos_drm_encoder_dpms
,
257 .mode_fixup
= exynos_drm_encoder_mode_fixup
,
258 .mode_set
= exynos_drm_encoder_mode_set
,
259 .prepare
= exynos_drm_encoder_prepare
,
260 .commit
= exynos_drm_encoder_commit
,
261 .disable
= exynos_drm_encoder_disable
,
264 static void exynos_drm_encoder_destroy(struct drm_encoder
*encoder
)
266 struct exynos_drm_encoder
*exynos_encoder
=
267 to_exynos_encoder(encoder
);
269 exynos_encoder
->manager
->pipe
= -1;
271 drm_encoder_cleanup(encoder
);
272 kfree(exynos_encoder
);
275 static struct drm_encoder_funcs exynos_encoder_funcs
= {
276 .destroy
= exynos_drm_encoder_destroy
,
279 static unsigned int exynos_drm_encoder_clones(struct drm_encoder
*encoder
)
281 struct drm_encoder
*clone
;
282 struct drm_device
*dev
= encoder
->dev
;
283 struct exynos_drm_encoder
*exynos_encoder
= to_exynos_encoder(encoder
);
284 struct exynos_drm_display_ops
*display_ops
=
285 exynos_encoder
->manager
->display_ops
;
286 unsigned int clone_mask
= 0;
289 list_for_each_entry(clone
, &dev
->mode_config
.encoder_list
, head
) {
290 switch (display_ops
->type
) {
291 case EXYNOS_DISPLAY_TYPE_LCD
:
292 case EXYNOS_DISPLAY_TYPE_HDMI
:
293 case EXYNOS_DISPLAY_TYPE_VIDI
:
294 clone_mask
|= (1 << (cnt
++));
304 void exynos_drm_encoder_setup(struct drm_device
*dev
)
306 struct drm_encoder
*encoder
;
308 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
)
309 encoder
->possible_clones
= exynos_drm_encoder_clones(encoder
);
313 exynos_drm_encoder_create(struct drm_device
*dev
,
314 struct exynos_drm_manager
*manager
,
315 unsigned int possible_crtcs
)
317 struct drm_encoder
*encoder
;
318 struct exynos_drm_encoder
*exynos_encoder
;
320 if (!manager
|| !possible_crtcs
)
326 exynos_encoder
= kzalloc(sizeof(*exynos_encoder
), GFP_KERNEL
);
330 exynos_encoder
->dpms
= DRM_MODE_DPMS_OFF
;
331 exynos_encoder
->manager
= manager
;
332 encoder
= &exynos_encoder
->drm_encoder
;
333 encoder
->possible_crtcs
= possible_crtcs
;
335 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder
->possible_crtcs
);
337 drm_encoder_init(dev
, encoder
, &exynos_encoder_funcs
,
338 DRM_MODE_ENCODER_TMDS
);
340 drm_encoder_helper_add(encoder
, &exynos_encoder_helper_funcs
);
342 DRM_DEBUG_KMS("encoder has been created\n");
347 struct exynos_drm_manager
*exynos_drm_get_manager(struct drm_encoder
*encoder
)
349 return to_exynos_encoder(encoder
)->manager
;
352 void exynos_drm_fn_encoder(struct drm_crtc
*crtc
, void *data
,
353 void (*fn
)(struct drm_encoder
*, void *))
355 struct drm_device
*dev
= crtc
->dev
;
356 struct drm_encoder
*encoder
;
357 struct exynos_drm_private
*private = dev
->dev_private
;
358 struct exynos_drm_manager
*manager
;
360 list_for_each_entry(encoder
, &dev
->mode_config
.encoder_list
, head
) {
362 * if crtc is detached from encoder, check pipe,
363 * otherwise check crtc attached to encoder
365 if (!encoder
->crtc
) {
366 manager
= to_exynos_encoder(encoder
)->manager
;
367 if (manager
->pipe
< 0 ||
368 private->crtc
[manager
->pipe
] != crtc
)
371 if (encoder
->crtc
!= crtc
)
379 void exynos_drm_enable_vblank(struct drm_encoder
*encoder
, void *data
)
381 struct exynos_drm_manager
*manager
=
382 to_exynos_encoder(encoder
)->manager
;
383 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
384 int crtc
= *(int *)data
;
386 if (manager
->pipe
!= crtc
)
389 if (manager_ops
->enable_vblank
)
390 manager_ops
->enable_vblank(manager
->dev
);
393 void exynos_drm_disable_vblank(struct drm_encoder
*encoder
, void *data
)
395 struct exynos_drm_manager
*manager
=
396 to_exynos_encoder(encoder
)->manager
;
397 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
398 int crtc
= *(int *)data
;
400 if (manager
->pipe
!= crtc
)
403 if (manager_ops
->disable_vblank
)
404 manager_ops
->disable_vblank(manager
->dev
);
407 void exynos_drm_encoder_crtc_dpms(struct drm_encoder
*encoder
, void *data
)
409 struct exynos_drm_encoder
*exynos_encoder
= to_exynos_encoder(encoder
);
410 struct exynos_drm_manager
*manager
= exynos_encoder
->manager
;
411 struct exynos_drm_manager_ops
*manager_ops
= manager
->ops
;
412 int mode
= *(int *)data
;
414 if (manager_ops
&& manager_ops
->dpms
)
415 manager_ops
->dpms(manager
->dev
, mode
);
418 * if this condition is ok then it means that the crtc is already
419 * detached from encoder and last function for detaching is properly
420 * done, so clear pipe from manager to prevent repeated call.
422 if (mode
> DRM_MODE_DPMS_ON
) {
428 void exynos_drm_encoder_crtc_pipe(struct drm_encoder
*encoder
, void *data
)
430 struct exynos_drm_manager
*manager
=
431 to_exynos_encoder(encoder
)->manager
;
432 int pipe
= *(int *)data
;
435 * when crtc is detached from encoder, this pipe is used
436 * to select manager operation
438 manager
->pipe
= pipe
;
441 void exynos_drm_encoder_plane_mode_set(struct drm_encoder
*encoder
, void *data
)
443 struct exynos_drm_manager
*manager
=
444 to_exynos_encoder(encoder
)->manager
;
445 struct exynos_drm_overlay_ops
*overlay_ops
= manager
->overlay_ops
;
446 struct exynos_drm_overlay
*overlay
= data
;
448 if (overlay_ops
&& overlay_ops
->mode_set
)
449 overlay_ops
->mode_set(manager
->dev
, overlay
);
452 void exynos_drm_encoder_plane_commit(struct drm_encoder
*encoder
, void *data
)
454 struct exynos_drm_manager
*manager
=
455 to_exynos_encoder(encoder
)->manager
;
456 struct exynos_drm_overlay_ops
*overlay_ops
= manager
->overlay_ops
;
457 int zpos
= DEFAULT_ZPOS
;
462 if (overlay_ops
&& overlay_ops
->commit
)
463 overlay_ops
->commit(manager
->dev
, zpos
);
466 void exynos_drm_encoder_plane_enable(struct drm_encoder
*encoder
, void *data
)
468 struct exynos_drm_manager
*manager
=
469 to_exynos_encoder(encoder
)->manager
;
470 struct exynos_drm_overlay_ops
*overlay_ops
= manager
->overlay_ops
;
471 int zpos
= DEFAULT_ZPOS
;
476 if (overlay_ops
&& overlay_ops
->enable
)
477 overlay_ops
->enable(manager
->dev
, zpos
);
480 void exynos_drm_encoder_plane_disable(struct drm_encoder
*encoder
, void *data
)
482 struct exynos_drm_manager
*manager
=
483 to_exynos_encoder(encoder
)->manager
;
484 struct exynos_drm_overlay_ops
*overlay_ops
= manager
->overlay_ops
;
485 int zpos
= DEFAULT_ZPOS
;
490 if (overlay_ops
&& overlay_ops
->disable
)
491 overlay_ops
->disable(manager
->dev
, zpos
);