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.
32 struct exynos_drm_encoder {
33 struct drm_crtc *old_crtc;
34 struct drm_encoder drm_encoder;
35 struct exynos_drm_manager *manager;
38 static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
40 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
41 struct exynos_drm_display_ops *display_ops = manager->display_ops;
43 DRM_DEBUG_KMS("encoder dpms: %d\n", mode);
45 if (display_ops && display_ops->dpms)
46 display_ops->dpms(manager->ctx, mode);
50 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
51 const struct drm_display_mode *mode,
52 struct drm_display_mode *adjusted_mode)
54 struct drm_device *dev = encoder->dev;
55 struct drm_connector *connector;
56 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
57 struct exynos_drm_manager_ops *manager_ops = manager->ops;
59 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
60 if (connector->encoder == encoder)
61 if (manager_ops && manager_ops->mode_fixup)
62 manager_ops->mode_fixup(manager, connector,
69 static void disable_plane_to_crtc(struct drm_device *dev,
70 struct drm_crtc *old_crtc,
71 struct drm_crtc *new_crtc)
73 struct drm_plane *plane;
76 * if old_crtc isn't same as encoder->crtc then it means that
77 * user changed crtc id to another one so the plane to old_crtc
78 * should be disabled and plane->crtc should be set to new_crtc
81 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
82 if (plane->crtc == old_crtc) {
84 * do not change below call order.
86 * plane->funcs->disable_plane call checks
87 * if encoder->crtc is same as plane->crtc and if same
88 * then manager_ops->win_disable callback will be called
89 * to diasble current hw overlay so plane->crtc should
90 * have new_crtc because new_crtc was set to
91 * encoder->crtc in advance.
93 plane->crtc = new_crtc;
94 plane->funcs->disable_plane(plane);
99 static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
100 struct drm_display_mode *mode,
101 struct drm_display_mode *adjusted_mode)
103 struct drm_device *dev = encoder->dev;
104 struct drm_connector *connector;
105 struct exynos_drm_manager *manager;
106 struct exynos_drm_manager_ops *manager_ops;
108 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
109 if (connector->encoder == encoder) {
110 struct exynos_drm_encoder *exynos_encoder;
112 exynos_encoder = to_exynos_encoder(encoder);
114 if (exynos_encoder->old_crtc != encoder->crtc &&
115 exynos_encoder->old_crtc) {
118 * disable a plane to old crtc and change
119 * crtc of the plane to new one.
121 disable_plane_to_crtc(dev,
122 exynos_encoder->old_crtc,
126 manager = exynos_drm_get_manager(encoder);
127 manager_ops = manager->ops;
129 if (manager_ops && manager_ops->mode_set)
130 manager_ops->mode_set(manager, adjusted_mode);
132 exynos_encoder->old_crtc = encoder->crtc;
137 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
139 /* drm framework doesn't check NULL. */
142 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
144 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
145 struct exynos_drm_manager *manager = exynos_encoder->manager;
146 struct exynos_drm_manager_ops *manager_ops = manager->ops;
148 if (manager_ops && manager_ops->commit)
149 manager_ops->commit(manager);
152 void exynos_drm_encoder_complete_scanout(struct drm_framebuffer *fb)
154 struct exynos_drm_encoder *exynos_encoder;
155 struct exynos_drm_manager_ops *ops;
156 struct drm_device *dev = fb->dev;
157 struct drm_encoder *encoder;
160 * make sure that overlay data are updated to real hardware
163 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
164 exynos_encoder = to_exynos_encoder(encoder);
165 ops = exynos_encoder->manager->ops;
168 * wait for vblank interrupt
169 * - this makes sure that overlay data are updated to
172 if (ops->wait_for_vblank)
173 ops->wait_for_vblank(exynos_encoder->manager);
178 static void exynos_drm_encoder_disable(struct drm_encoder *encoder)
180 struct drm_plane *plane;
181 struct drm_device *dev = encoder->dev;
183 exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
185 /* all planes connected to this encoder should be also disabled. */
186 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
187 if (plane->crtc == encoder->crtc)
188 plane->funcs->disable_plane(plane);
192 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
193 .dpms = exynos_drm_encoder_dpms,
194 .mode_fixup = exynos_drm_encoder_mode_fixup,
195 .mode_set = exynos_drm_encoder_mode_set,
196 .prepare = exynos_drm_encoder_prepare,
197 .commit = exynos_drm_encoder_commit,
198 .disable = exynos_drm_encoder_disable,
201 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
203 struct exynos_drm_encoder *exynos_encoder =
204 to_exynos_encoder(encoder);
206 exynos_encoder->manager->pipe = -1;
208 drm_encoder_cleanup(encoder);
209 kfree(exynos_encoder);
212 static struct drm_encoder_funcs exynos_encoder_funcs = {
213 .destroy = exynos_drm_encoder_destroy,
216 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
218 struct drm_encoder *clone;
219 struct drm_device *dev = encoder->dev;
220 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
221 struct exynos_drm_display_ops *display_ops =
222 exynos_encoder->manager->display_ops;
223 unsigned int clone_mask = 0;
226 list_for_each_entry(clone, &dev->mode_config.encoder_list, head) {
227 switch (display_ops->type) {
228 case EXYNOS_DISPLAY_TYPE_LCD:
229 case EXYNOS_DISPLAY_TYPE_HDMI:
230 case EXYNOS_DISPLAY_TYPE_VIDI:
231 clone_mask |= (1 << (cnt++));
241 void exynos_drm_encoder_setup(struct drm_device *dev)
243 struct drm_encoder *encoder;
245 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
246 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
250 exynos_drm_encoder_create(struct drm_device *dev,
251 struct exynos_drm_manager *manager,
252 unsigned int possible_crtcs)
254 struct drm_encoder *encoder;
255 struct exynos_drm_encoder *exynos_encoder;
258 if (!manager || !possible_crtcs)
264 exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
268 exynos_encoder->manager = manager;
269 encoder = &exynos_encoder->drm_encoder;
270 encoder->possible_crtcs = possible_crtcs;
272 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
274 drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
275 DRM_MODE_ENCODER_TMDS);
277 drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
279 if (manager->ops && manager->ops->initialize) {
280 ret = manager->ops->initialize(manager, dev);
282 DRM_ERROR("Manager initialize failed %d\n", ret);
287 if (manager->display_ops && manager->display_ops->initialize) {
288 ret = manager->display_ops->initialize(manager->dev, dev);
290 DRM_ERROR("Display initialize failed %d\n", ret);
295 DRM_DEBUG_KMS("encoder has been created\n");
300 exynos_drm_encoder_destroy(&exynos_encoder->drm_encoder);
304 struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder)
306 return to_exynos_encoder(encoder)->manager;
309 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
310 void (*fn)(struct drm_encoder *, void *))
312 struct drm_device *dev = crtc->dev;
313 struct drm_encoder *encoder;
314 struct exynos_drm_private *private = dev->dev_private;
315 struct exynos_drm_manager *manager;
317 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
319 * if crtc is detached from encoder, check pipe,
320 * otherwise check crtc attached to encoder
322 if (!encoder->crtc) {
323 manager = to_exynos_encoder(encoder)->manager;
324 if (manager->pipe < 0 ||
325 private->crtc[manager->pipe] != crtc)
328 if (encoder->crtc != crtc)
336 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
338 struct exynos_drm_manager *manager =
339 to_exynos_encoder(encoder)->manager;
340 struct exynos_drm_manager_ops *manager_ops = manager->ops;
341 int crtc = *(int *)data;
343 if (manager->pipe != crtc)
346 if (manager_ops->enable_vblank)
347 manager_ops->enable_vblank(manager);
350 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
352 struct exynos_drm_manager *manager =
353 to_exynos_encoder(encoder)->manager;
354 struct exynos_drm_manager_ops *manager_ops = manager->ops;
355 int crtc = *(int *)data;
357 if (manager->pipe != crtc)
360 if (manager_ops->disable_vblank)
361 manager_ops->disable_vblank(manager);
364 void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
366 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
367 struct exynos_drm_manager *manager = exynos_encoder->manager;
368 struct exynos_drm_manager_ops *manager_ops = manager->ops;
369 int mode = *(int *)data;
371 if (manager_ops && manager_ops->dpms)
372 manager_ops->dpms(manager, mode);
375 * if this condition is ok then it means that the crtc is already
376 * detached from encoder and last function for detaching is properly
377 * done, so clear pipe from manager to prevent repeated call.
379 if (mode > DRM_MODE_DPMS_ON) {
385 void exynos_drm_encoder_crtc_pipe(struct drm_encoder *encoder, void *data)
387 struct exynos_drm_manager *manager =
388 to_exynos_encoder(encoder)->manager;
389 int pipe = *(int *)data;
392 * when crtc is detached from encoder, this pipe is used
393 * to select manager operation
395 manager->pipe = pipe;
398 void exynos_drm_encoder_plane_mode_set(struct drm_encoder *encoder, void *data)
400 struct exynos_drm_manager *manager =
401 to_exynos_encoder(encoder)->manager;
402 struct exynos_drm_manager_ops *manager_ops = manager->ops;
403 struct exynos_drm_overlay *overlay = data;
405 if (manager_ops && manager_ops->win_mode_set)
406 manager_ops->win_mode_set(manager, overlay);
409 void exynos_drm_encoder_plane_commit(struct drm_encoder *encoder, void *data)
411 struct exynos_drm_manager *manager =
412 to_exynos_encoder(encoder)->manager;
413 struct exynos_drm_manager_ops *manager_ops = manager->ops;
414 int zpos = DEFAULT_ZPOS;
419 if (manager_ops && manager_ops->win_commit)
420 manager_ops->win_commit(manager, zpos);
423 void exynos_drm_encoder_plane_enable(struct drm_encoder *encoder, void *data)
425 struct exynos_drm_manager *manager =
426 to_exynos_encoder(encoder)->manager;
427 struct exynos_drm_manager_ops *manager_ops = manager->ops;
428 int zpos = DEFAULT_ZPOS;
433 if (manager_ops && manager_ops->win_enable)
434 manager_ops->win_enable(manager, zpos);
437 void exynos_drm_encoder_plane_disable(struct drm_encoder *encoder, void *data)
439 struct exynos_drm_manager *manager =
440 to_exynos_encoder(encoder)->manager;
441 struct exynos_drm_manager_ops *manager_ops = manager->ops;
442 int zpos = DEFAULT_ZPOS;
447 if (manager_ops && manager_ops->win_disable)
448 manager_ops->win_disable(manager, zpos);