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 * 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_crtc_helper.h"
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_crtc.h"
34 #include "exynos_drm_encoder.h"
35 #include "exynos_drm_display.h"
37 #define to_exynos_encoder(x) container_of(x, struct exynos_drm_encoder,\
41 * exynos specific encoder structure.
43 * @drm_encoder: encoder object.
44 * @display: specific encoder has its own display to control a hardware
45 * appropriately and we can access a hardware drawing on this display.
46 * @dpms: store the encoder dpms value.
48 struct exynos_drm_encoder {
49 struct drm_encoder drm_encoder;
50 struct exynos_drm_display *display;
54 static void exynos_drm_display_power(struct drm_encoder *encoder, int mode)
56 struct drm_device *dev = encoder->dev;
57 struct drm_connector *connector;
58 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
59 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
61 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
62 if (connector->encoder != encoder)
65 DRM_DEBUG_KMS("connector[%d] dpms[%d]\n", connector->base.id,
68 /* We want to make sure we order things correctly here. When
69 * turning on, start the controller, then the panel. When
70 * turning off, do the reverse.
73 case DRM_MODE_DPMS_ON:
74 if (display->controller_ops->power)
75 display->controller_ops->power(
76 display->controller_ctx, mode);
78 if (display->panel_ops->power)
79 display->panel_ops->power(display->panel_ctx,
82 exynos_encoder->dpms = mode;
84 case DRM_MODE_DPMS_STANDBY:
85 case DRM_MODE_DPMS_SUSPEND:
86 case DRM_MODE_DPMS_OFF:
87 if (display->panel_ops->power)
88 display->panel_ops->power(display->panel_ctx,
91 if (display->controller_ops->power)
92 display->controller_ops->power(
93 display->controller_ctx, mode);
95 exynos_encoder->dpms = mode;
98 DRM_ERROR("Unknown dpms mode: %d\n", mode);
104 int exynos_drm_encoder_get_dpms(struct drm_encoder *encoder)
106 struct drm_device *dev = encoder->dev;
107 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
110 mutex_lock(&dev->struct_mutex);
111 mode = exynos_encoder->dpms;
112 mutex_unlock(&dev->struct_mutex);
117 void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
119 struct drm_device *dev = encoder->dev;
120 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
122 DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode);
124 mutex_lock(&dev->struct_mutex);
126 if (exynos_encoder->dpms == mode) {
127 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
128 mutex_unlock(&dev->struct_mutex);
133 case DRM_MODE_DPMS_ON:
134 exynos_drm_display_power(encoder, mode);
136 case DRM_MODE_DPMS_STANDBY:
137 case DRM_MODE_DPMS_SUSPEND:
138 case DRM_MODE_DPMS_OFF:
139 exynos_drm_display_power(encoder, mode);
142 DRM_ERROR("unspecified mode %d\n", mode);
146 mutex_unlock(&dev->struct_mutex);
150 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
151 struct drm_display_mode *mode,
152 struct drm_display_mode *adjusted_mode)
154 struct drm_device *dev = encoder->dev;
155 struct drm_connector *connector;
156 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
158 DRM_DEBUG_KMS("%s\n", __FILE__);
160 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
161 if (connector->encoder != encoder)
164 if (display->panel_ops->mode_fixup)
165 display->panel_ops->mode_fixup(display->panel_ctx,
166 connector, mode, adjusted_mode);
172 static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
173 struct drm_display_mode *mode,
174 struct drm_display_mode *adjusted_mode)
176 struct drm_device *dev = encoder->dev;
177 struct drm_connector *connector;
178 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
179 struct exynos_drm_overlay *overlay = get_exynos_drm_overlay(dev,
182 DRM_DEBUG_KMS("%s\n", __FILE__);
184 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
185 if (connector->encoder != encoder)
188 if (display->panel_ops->mode_set)
189 display->panel_ops->mode_set(display->panel_ctx,
192 if (display->controller_ops->mode_set)
193 display->controller_ops->mode_set(
194 display->controller_ctx, overlay);
198 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
200 DRM_DEBUG_KMS("%s\n", __FILE__);
202 /* drm framework doesn't check NULL. */
205 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
207 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
209 DRM_DEBUG_KMS("%s\n", __FILE__);
210 if (display->controller_ops->commit)
211 display->controller_ops->commit(display->controller_ctx);
213 if (display->panel_ops->commit)
214 display->panel_ops->commit(display->panel_ctx);
217 static struct drm_crtc *
218 exynos_drm_encoder_get_crtc(struct drm_encoder *encoder)
220 return encoder->crtc;
223 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
224 .dpms = exynos_drm_encoder_dpms,
225 .mode_fixup = exynos_drm_encoder_mode_fixup,
226 .mode_set = exynos_drm_encoder_mode_set,
227 .prepare = exynos_drm_encoder_prepare,
228 .commit = exynos_drm_encoder_commit,
229 .get_crtc = exynos_drm_encoder_get_crtc,
232 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
234 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
236 DRM_DEBUG_KMS("%s\n", __FILE__);
238 exynos_encoder->display->pipe = -1;
240 drm_encoder_cleanup(encoder);
241 kfree(exynos_encoder);
244 static struct drm_encoder_funcs exynos_encoder_funcs = {
245 .destroy = exynos_drm_encoder_destroy,
248 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
250 struct drm_encoder *clone;
251 struct drm_device *dev = encoder->dev;
252 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
253 unsigned int clone_mask = 0;
256 list_for_each_entry(clone, &dev->mode_config.encoder_list, head) {
257 switch (display->display_type) {
258 case EXYNOS_DRM_DISPLAY_TYPE_FIMD:
259 case EXYNOS_DRM_DISPLAY_TYPE_MIXER:
260 case EXYNOS_DRM_DISPLAY_TYPE_VIDI:
261 clone_mask |= (1 << (cnt++));
271 void exynos_drm_encoder_setup(struct drm_device *dev)
273 struct drm_encoder *encoder;
275 DRM_DEBUG_KMS("%s\n", __FILE__);
277 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
278 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
282 exynos_drm_encoder_create(struct drm_device *dev,
283 struct exynos_drm_display *display,
284 unsigned int possible_crtcs)
286 struct drm_encoder *encoder;
287 struct exynos_drm_encoder *exynos_encoder;
289 DRM_DEBUG_KMS("%s\n", __FILE__);
291 if (!display || !possible_crtcs)
294 exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
295 if (!exynos_encoder) {
296 DRM_ERROR("failed to allocate encoder\n");
300 exynos_encoder->dpms = DRM_MODE_DPMS_OFF;
301 exynos_encoder->display = display;
302 encoder = &exynos_encoder->drm_encoder;
303 encoder->possible_crtcs = possible_crtcs;
305 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
307 drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
308 DRM_MODE_ENCODER_TMDS);
310 drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
312 DRM_DEBUG_KMS("encoder has been created\n");
317 struct exynos_drm_display *exynos_drm_get_display(struct drm_encoder *encoder)
319 return to_exynos_encoder(encoder)->display;
322 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
323 void (*fn)(struct drm_encoder *, void *))
325 struct drm_device *dev = crtc->dev;
326 struct drm_encoder *encoder;
327 struct exynos_drm_private *private = dev->dev_private;
328 struct exynos_drm_display *display;
330 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
332 * if crtc is detached from encoder, check pipe,
333 * otherwise check crtc attached to encoder
335 if (!encoder->crtc) {
336 display = exynos_drm_get_display(encoder);
337 if (display->pipe < 0 ||
338 private->crtc[display->pipe] != crtc)
340 } else if (encoder->crtc != crtc) {
348 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
350 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
351 int crtc = *(int *)data;
353 if (display->pipe == -1)
354 display->pipe = crtc;
356 /* TODO(seanpaul): Fix the pipe interaction here */
357 if (display->controller_ops->enable_vblank)
358 display->controller_ops->enable_vblank(display->controller_ctx,
362 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
364 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
365 int crtc = *(int *)data;
368 * TODO(seanpaul): This seems like a hack. I don't think it's actually
369 * needed for 2 reasons:
370 * (1) disable_vblank implies vblank has been enabled. If
371 * enable_vblank hasn't already been called, that's a bug.
372 * (2) Even if (1) isn't true, this function should just disable an
373 * interrupt, and shouldn't affect pipe.
375 if (display->pipe == -1)
376 display->pipe = crtc;
378 if (display->controller_ops->disable_vblank)
379 display->controller_ops->disable_vblank(
380 display->controller_ctx);
383 void exynos_drm_encoder_crtc_plane_commit(struct drm_encoder *encoder,
386 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
387 int zpos = DEFAULT_ZPOS;
392 if (display->controller_ops->win_commit)
393 display->controller_ops->win_commit(display->controller_ctx,
397 void exynos_drm_encoder_crtc_commit(struct drm_encoder *encoder, void *data)
399 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
400 int crtc = *(int *)data;
401 int zpos = DEFAULT_ZPOS;
403 DRM_DEBUG_KMS("%s\n", __FILE__);
406 * when crtc is detached from encoder, this pipe is used
407 * to select display operation
409 display->pipe = crtc;
411 exynos_drm_encoder_crtc_plane_commit(encoder, &zpos);
414 void exynos_drm_encoder_dpms_from_crtc(struct drm_encoder *encoder, void *data)
416 int mode = *(int *)data;
418 DRM_DEBUG_KMS("%s\n", __FILE__);
420 exynos_drm_encoder_dpms(encoder, mode);
423 void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
425 struct drm_device *dev = encoder->dev;
426 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
427 struct exynos_drm_display *display = exynos_encoder->display;
428 struct drm_connector *connector;
429 int mode = *(int *)data;
431 DRM_DEBUG_KMS("%s\n", __FILE__);
433 /* We want to make sure we order things correctly here. When turning on,
434 * start the controller, then the panel. When turning off, do the
438 case DRM_MODE_DPMS_ON:
439 if (display->controller_ops->dpms)
440 display->controller_ops->dpms(display->controller_ctx,
443 if (display->panel_ops->dpms)
444 display->panel_ops->dpms(display->panel_ctx, mode);
446 case DRM_MODE_DPMS_STANDBY:
447 case DRM_MODE_DPMS_SUSPEND:
448 case DRM_MODE_DPMS_OFF:
449 if (display->panel_ops->dpms)
450 display->panel_ops->dpms(display->panel_ctx, mode);
452 if (display->controller_ops->dpms)
453 display->controller_ops->dpms(display->controller_ctx,
457 DRM_ERROR("Unknown dpms mode: %d\n", mode);
462 * set current dpms mode to the connector connected to
463 * current encoder. connector->dpms would be checked
464 * at drm_helper_connector_dpms()
466 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
467 if (connector->encoder == encoder)
468 connector->dpms = mode;
471 * if this condition is ok then it means that the crtc is already
472 * detached from encoder and last function for detaching is properly
473 * done, so clear pipe from display to prevent repeated call.
475 if (mode > DRM_MODE_DPMS_ON) {
481 void exynos_drm_encoder_crtc_mode_set(struct drm_encoder *encoder, void *data)
483 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
484 struct exynos_drm_overlay *overlay = data;
486 if (display->controller_ops->mode_set)
487 display->controller_ops->mode_set(display->controller_ctx,
491 void exynos_drm_encoder_crtc_disable(struct drm_encoder *encoder, void *data)
493 struct exynos_drm_display *display = exynos_drm_get_display(encoder);
494 int zpos = DEFAULT_ZPOS;
501 if (display->controller_ops->win_disable)
502 display->controller_ops->win_disable(display->controller_ctx,