Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / gpu / drm / exynos / exynos_drm_encoder.c
blobef4754f1519bfbb4334b71c1372cc8d594526bd9
1 /* exynos_drm_encoder.c
3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Authors:
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
18 * Software.
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.
29 #include "drmP.h"
30 #include "drm_crtc_helper.h"
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_crtc.h"
34 #include "exynos_drm_encoder.h"
36 #define to_exynos_encoder(x) container_of(x, struct exynos_drm_encoder,\
37 drm_encoder)
40 * exynos specific encoder structure.
42 * @drm_encoder: encoder object.
43 * @manager: specific encoder has its own manager to control a hardware
44 * appropriately and we can access a hardware drawing on this manager.
45 * @dpms: store the encoder dpms value.
47 struct exynos_drm_encoder {
48 struct drm_encoder drm_encoder;
49 struct exynos_drm_manager *manager;
50 int dpms;
53 static void exynos_drm_display_power(struct drm_encoder *encoder, int mode)
55 struct drm_device *dev = encoder->dev;
56 struct drm_connector *connector;
57 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
59 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
60 if (connector->encoder == encoder) {
61 struct exynos_drm_display_ops *display_ops =
62 manager->display_ops;
64 DRM_DEBUG_KMS("connector[%d] dpms[%d]\n",
65 connector->base.id, mode);
66 if (display_ops && display_ops->power_on)
67 display_ops->power_on(manager->dev, mode);
72 static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
74 struct drm_device *dev = encoder->dev;
75 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
76 struct exynos_drm_manager_ops *manager_ops = manager->ops;
77 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
79 DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode);
81 if (exynos_encoder->dpms == mode) {
82 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
83 return;
86 mutex_lock(&dev->struct_mutex);
88 switch (mode) {
89 case DRM_MODE_DPMS_ON:
90 if (manager_ops && manager_ops->apply)
91 manager_ops->apply(manager->dev);
92 exynos_drm_display_power(encoder, mode);
93 exynos_encoder->dpms = mode;
94 break;
95 case DRM_MODE_DPMS_STANDBY:
96 case DRM_MODE_DPMS_SUSPEND:
97 case DRM_MODE_DPMS_OFF:
98 exynos_drm_display_power(encoder, mode);
99 exynos_encoder->dpms = mode;
100 break;
101 default:
102 DRM_ERROR("unspecified mode %d\n", mode);
103 break;
106 mutex_unlock(&dev->struct_mutex);
109 static bool
110 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
111 struct drm_display_mode *mode,
112 struct drm_display_mode *adjusted_mode)
114 DRM_DEBUG_KMS("%s\n", __FILE__);
116 /* drm framework doesn't check NULL. */
118 return true;
121 static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
122 struct drm_display_mode *mode,
123 struct drm_display_mode *adjusted_mode)
125 struct drm_device *dev = encoder->dev;
126 struct drm_connector *connector;
127 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
128 struct exynos_drm_manager_ops *manager_ops = manager->ops;
129 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
130 struct exynos_drm_overlay *overlay = get_exynos_drm_overlay(dev,
131 encoder->crtc);
133 DRM_DEBUG_KMS("%s\n", __FILE__);
135 mode = adjusted_mode;
137 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
138 if (connector->encoder == encoder) {
139 if (manager_ops && manager_ops->mode_set)
140 manager_ops->mode_set(manager->dev, mode);
142 if (overlay_ops && overlay_ops->mode_set)
143 overlay_ops->mode_set(manager->dev, overlay);
148 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
150 DRM_DEBUG_KMS("%s\n", __FILE__);
152 /* drm framework doesn't check NULL. */
155 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
157 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
158 struct exynos_drm_manager_ops *manager_ops = manager->ops;
160 DRM_DEBUG_KMS("%s\n", __FILE__);
162 if (manager_ops && manager_ops->commit)
163 manager_ops->commit(manager->dev);
166 static struct drm_crtc *
167 exynos_drm_encoder_get_crtc(struct drm_encoder *encoder)
169 return encoder->crtc;
172 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
173 .dpms = exynos_drm_encoder_dpms,
174 .mode_fixup = exynos_drm_encoder_mode_fixup,
175 .mode_set = exynos_drm_encoder_mode_set,
176 .prepare = exynos_drm_encoder_prepare,
177 .commit = exynos_drm_encoder_commit,
178 .get_crtc = exynos_drm_encoder_get_crtc,
181 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
183 struct exynos_drm_encoder *exynos_encoder =
184 to_exynos_encoder(encoder);
186 DRM_DEBUG_KMS("%s\n", __FILE__);
188 exynos_encoder->manager->pipe = -1;
190 drm_encoder_cleanup(encoder);
191 kfree(exynos_encoder);
194 static struct drm_encoder_funcs exynos_encoder_funcs = {
195 .destroy = exynos_drm_encoder_destroy,
198 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
200 struct drm_encoder *clone;
201 struct drm_device *dev = encoder->dev;
202 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
203 struct exynos_drm_display_ops *display_ops =
204 exynos_encoder->manager->display_ops;
205 unsigned int clone_mask = 0;
206 int cnt = 0;
208 list_for_each_entry(clone, &dev->mode_config.encoder_list, head) {
209 switch (display_ops->type) {
210 case EXYNOS_DISPLAY_TYPE_LCD:
211 case EXYNOS_DISPLAY_TYPE_HDMI:
212 clone_mask |= (1 << (cnt++));
213 break;
214 default:
215 continue;
219 return clone_mask;
222 void exynos_drm_encoder_setup(struct drm_device *dev)
224 struct drm_encoder *encoder;
226 DRM_DEBUG_KMS("%s\n", __FILE__);
228 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
229 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
232 struct drm_encoder *
233 exynos_drm_encoder_create(struct drm_device *dev,
234 struct exynos_drm_manager *manager,
235 unsigned int possible_crtcs)
237 struct drm_encoder *encoder;
238 struct exynos_drm_encoder *exynos_encoder;
240 DRM_DEBUG_KMS("%s\n", __FILE__);
242 if (!manager || !possible_crtcs)
243 return NULL;
245 if (!manager->dev)
246 return NULL;
248 exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
249 if (!exynos_encoder) {
250 DRM_ERROR("failed to allocate encoder\n");
251 return NULL;
254 exynos_encoder->dpms = DRM_MODE_DPMS_OFF;
255 exynos_encoder->manager = manager;
256 encoder = &exynos_encoder->drm_encoder;
257 encoder->possible_crtcs = possible_crtcs;
259 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
261 drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
262 DRM_MODE_ENCODER_TMDS);
264 drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
266 DRM_DEBUG_KMS("encoder has been created\n");
268 return encoder;
271 struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder)
273 return to_exynos_encoder(encoder)->manager;
276 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
277 void (*fn)(struct drm_encoder *, void *))
279 struct drm_device *dev = crtc->dev;
280 struct drm_encoder *encoder;
281 struct exynos_drm_private *private = dev->dev_private;
282 struct exynos_drm_manager *manager;
284 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
286 * if crtc is detached from encoder, check pipe,
287 * otherwise check crtc attached to encoder
289 if (!encoder->crtc) {
290 manager = to_exynos_encoder(encoder)->manager;
291 if (manager->pipe < 0 ||
292 private->crtc[manager->pipe] != crtc)
293 continue;
294 } else {
295 if (encoder->crtc != crtc)
296 continue;
299 fn(encoder, data);
303 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
305 struct exynos_drm_manager *manager =
306 to_exynos_encoder(encoder)->manager;
307 struct exynos_drm_manager_ops *manager_ops = manager->ops;
308 int crtc = *(int *)data;
310 if (manager->pipe == -1)
311 manager->pipe = crtc;
313 if (manager_ops->enable_vblank)
314 manager_ops->enable_vblank(manager->dev);
317 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
319 struct exynos_drm_manager *manager =
320 to_exynos_encoder(encoder)->manager;
321 struct exynos_drm_manager_ops *manager_ops = manager->ops;
322 int crtc = *(int *)data;
324 if (manager->pipe == -1)
325 manager->pipe = crtc;
327 if (manager_ops->disable_vblank)
328 manager_ops->disable_vblank(manager->dev);
331 void exynos_drm_encoder_crtc_plane_commit(struct drm_encoder *encoder,
332 void *data)
334 struct exynos_drm_manager *manager =
335 to_exynos_encoder(encoder)->manager;
336 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
337 int zpos = DEFAULT_ZPOS;
339 if (data)
340 zpos = *(int *)data;
342 if (overlay_ops && overlay_ops->commit)
343 overlay_ops->commit(manager->dev, zpos);
346 void exynos_drm_encoder_crtc_commit(struct drm_encoder *encoder, void *data)
348 struct exynos_drm_manager *manager =
349 to_exynos_encoder(encoder)->manager;
350 int crtc = *(int *)data;
351 int zpos = DEFAULT_ZPOS;
353 DRM_DEBUG_KMS("%s\n", __FILE__);
356 * when crtc is detached from encoder, this pipe is used
357 * to select manager operation
359 manager->pipe = crtc;
361 exynos_drm_encoder_crtc_plane_commit(encoder, &zpos);
364 void exynos_drm_encoder_dpms_from_crtc(struct drm_encoder *encoder, void *data)
366 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
367 int mode = *(int *)data;
369 DRM_DEBUG_KMS("%s\n", __FILE__);
371 exynos_drm_encoder_dpms(encoder, mode);
373 exynos_encoder->dpms = mode;
376 void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
378 struct drm_device *dev = encoder->dev;
379 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
380 struct exynos_drm_manager *manager = exynos_encoder->manager;
381 struct exynos_drm_manager_ops *manager_ops = manager->ops;
382 struct drm_connector *connector;
383 int mode = *(int *)data;
385 DRM_DEBUG_KMS("%s\n", __FILE__);
387 if (manager_ops && manager_ops->dpms)
388 manager_ops->dpms(manager->dev, mode);
391 * set current dpms mode to the connector connected to
392 * current encoder. connector->dpms would be checked
393 * at drm_helper_connector_dpms()
395 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
396 if (connector->encoder == encoder)
397 connector->dpms = mode;
400 * if this condition is ok then it means that the crtc is already
401 * detached from encoder and last function for detaching is properly
402 * done, so clear pipe from manager to prevent repeated call.
404 if (mode > DRM_MODE_DPMS_ON) {
405 if (!encoder->crtc)
406 manager->pipe = -1;
410 void exynos_drm_encoder_crtc_mode_set(struct drm_encoder *encoder, void *data)
412 struct exynos_drm_manager *manager =
413 to_exynos_encoder(encoder)->manager;
414 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
415 struct exynos_drm_overlay *overlay = data;
417 if (overlay_ops && overlay_ops->mode_set)
418 overlay_ops->mode_set(manager->dev, overlay);
421 void exynos_drm_encoder_crtc_disable(struct drm_encoder *encoder, void *data)
423 struct exynos_drm_manager *manager =
424 to_exynos_encoder(encoder)->manager;
425 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
426 int zpos = DEFAULT_ZPOS;
428 DRM_DEBUG_KMS("\n");
430 if (data)
431 zpos = *(int *)data;
433 if (overlay_ops && overlay_ops->disable)
434 overlay_ops->disable(manager->dev, zpos);
437 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
438 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
439 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
440 MODULE_DESCRIPTION("Samsung SoC DRM Encoder Driver");
441 MODULE_LICENSE("GPL");