blob: e51503fbaf2bcbf19edbdf423638a725b5703a6e [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* exynos_drm_encoder.c
2 *
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>
8 *
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:
15 *
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.
19 *
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.
27 */
28
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
30#include <drm/drm_crtc_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090031
32#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090033#include "exynos_drm_encoder.h"
Inki Dae50caf252012-08-20 21:29:25 +090034#include "exynos_drm_connector.h"
Inki Dae1c248b72011-10-04 19:19:01 +090035
36#define to_exynos_encoder(x) container_of(x, struct exynos_drm_encoder,\
37 drm_encoder)
38
39/*
40 * exynos specific encoder structure.
41 *
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.
Inki Daeec05da92011-12-06 11:06:54 +090045 * @dpms: store the encoder dpms value.
Inki Dae1c248b72011-10-04 19:19:01 +090046 */
47struct exynos_drm_encoder {
Inki Dae1b85a072012-08-17 17:58:38 +090048 struct drm_crtc *old_crtc;
Inki Dae1c248b72011-10-04 19:19:01 +090049 struct drm_encoder drm_encoder;
50 struct exynos_drm_manager *manager;
Inki Daeec05da92011-12-06 11:06:54 +090051 int dpms;
Inki Dae1c248b72011-10-04 19:19:01 +090052};
53
Inki Dae50caf252012-08-20 21:29:25 +090054static void exynos_drm_connector_power(struct drm_encoder *encoder, int mode)
Inki Dae1c248b72011-10-04 19:19:01 +090055{
56 struct drm_device *dev = encoder->dev;
57 struct drm_connector *connector;
Joonyoung Shim396464d2011-11-14 15:20:49 +090058
Inki Dae1c248b72011-10-04 19:19:01 +090059 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Inki Dae58f6aad2012-09-24 20:04:24 +090060 if (exynos_drm_best_encoder(connector) == encoder) {
Joonyoung Shimd2716c82011-11-04 17:04:45 +090061 DRM_DEBUG_KMS("connector[%d] dpms[%d]\n",
62 connector->base.id, mode);
Inki Dae50caf252012-08-20 21:29:25 +090063
64 exynos_drm_display_power(connector, mode);
Inki Dae1c248b72011-10-04 19:19:01 +090065 }
66 }
67}
68
Inki Daeec05da92011-12-06 11:06:54 +090069static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
70{
71 struct drm_device *dev = encoder->dev;
72 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
73 struct exynos_drm_manager_ops *manager_ops = manager->ops;
74 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
75
76 DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode);
77
78 if (exynos_encoder->dpms == mode) {
79 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
80 return;
81 }
82
83 mutex_lock(&dev->struct_mutex);
84
85 switch (mode) {
86 case DRM_MODE_DPMS_ON:
87 if (manager_ops && manager_ops->apply)
88 manager_ops->apply(manager->dev);
Inki Dae50caf252012-08-20 21:29:25 +090089 exynos_drm_connector_power(encoder, mode);
Inki Daeec05da92011-12-06 11:06:54 +090090 exynos_encoder->dpms = mode;
91 break;
92 case DRM_MODE_DPMS_STANDBY:
93 case DRM_MODE_DPMS_SUSPEND:
94 case DRM_MODE_DPMS_OFF:
Inki Dae50caf252012-08-20 21:29:25 +090095 exynos_drm_connector_power(encoder, mode);
Inki Daeec05da92011-12-06 11:06:54 +090096 exynos_encoder->dpms = mode;
97 break;
98 default:
99 DRM_ERROR("unspecified mode %d\n", mode);
100 break;
101 }
102
103 mutex_unlock(&dev->struct_mutex);
104}
105
Inki Dae1c248b72011-10-04 19:19:01 +0900106static bool
107exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200108 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +0900109 struct drm_display_mode *adjusted_mode)
110{
Inki Dae1de425b2012-03-16 18:47:04 +0900111 struct drm_device *dev = encoder->dev;
112 struct drm_connector *connector;
113 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
114 struct exynos_drm_manager_ops *manager_ops = manager->ops;
115
Inki Dae1c248b72011-10-04 19:19:01 +0900116 DRM_DEBUG_KMS("%s\n", __FILE__);
117
Inki Dae1de425b2012-03-16 18:47:04 +0900118 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
119 if (connector->encoder == encoder)
120 if (manager_ops && manager_ops->mode_fixup)
121 manager_ops->mode_fixup(manager->dev, connector,
122 mode, adjusted_mode);
123 }
Inki Dae1c248b72011-10-04 19:19:01 +0900124
125 return true;
126}
127
Inki Dae1b85a072012-08-17 17:58:38 +0900128static void disable_plane_to_crtc(struct drm_device *dev,
129 struct drm_crtc *old_crtc,
130 struct drm_crtc *new_crtc)
131{
132 struct drm_plane *plane;
133
134 /*
135 * if old_crtc isn't same as encoder->crtc then it means that
136 * user changed crtc id to another one so the plane to old_crtc
137 * should be disabled and plane->crtc should be set to new_crtc
138 * (encoder->crtc)
139 */
140 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
141 if (plane->crtc == old_crtc) {
142 /*
143 * do not change below call order.
144 *
145 * plane->funcs->disable_plane call checks
146 * if encoder->crtc is same as plane->crtc and if same
147 * then overlay_ops->disable callback will be called
148 * to diasble current hw overlay so plane->crtc should
149 * have new_crtc because new_crtc was set to
150 * encoder->crtc in advance.
151 */
152 plane->crtc = new_crtc;
153 plane->funcs->disable_plane(plane);
154 }
155 }
156}
157
Inki Dae1c248b72011-10-04 19:19:01 +0900158static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
159 struct drm_display_mode *mode,
160 struct drm_display_mode *adjusted_mode)
161{
162 struct drm_device *dev = encoder->dev;
163 struct drm_connector *connector;
Inki Dae1b85a072012-08-17 17:58:38 +0900164 struct exynos_drm_manager *manager;
165 struct exynos_drm_manager_ops *manager_ops;
Inki Dae1c248b72011-10-04 19:19:01 +0900166
167 DRM_DEBUG_KMS("%s\n", __FILE__);
168
Inki Dae1c248b72011-10-04 19:19:01 +0900169 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Inki Dae1b85a072012-08-17 17:58:38 +0900170 if (connector->encoder == encoder) {
171 struct exynos_drm_encoder *exynos_encoder;
172
173 exynos_encoder = to_exynos_encoder(encoder);
174
175 if (exynos_encoder->old_crtc != encoder->crtc &&
176 exynos_encoder->old_crtc) {
177
178 /*
179 * disable a plane to old crtc and change
180 * crtc of the plane to new one.
181 */
182 disable_plane_to_crtc(dev,
183 exynos_encoder->old_crtc,
184 encoder->crtc);
185 }
186
187 manager = exynos_drm_get_manager(encoder);
188 manager_ops = manager->ops;
189
Inki Dae1c248b72011-10-04 19:19:01 +0900190 if (manager_ops && manager_ops->mode_set)
Inki Dae1de425b2012-03-16 18:47:04 +0900191 manager_ops->mode_set(manager->dev,
192 adjusted_mode);
Inki Dae1b85a072012-08-17 17:58:38 +0900193
194 exynos_encoder->old_crtc = encoder->crtc;
195 }
Inki Dae1c248b72011-10-04 19:19:01 +0900196 }
197}
198
199static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
200{
201 DRM_DEBUG_KMS("%s\n", __FILE__);
202
203 /* drm framework doesn't check NULL. */
204}
205
206static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
207{
208 struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
209 struct exynos_drm_manager_ops *manager_ops = manager->ops;
Inki Dae1c248b72011-10-04 19:19:01 +0900210
211 DRM_DEBUG_KMS("%s\n", __FILE__);
212
213 if (manager_ops && manager_ops->commit)
214 manager_ops->commit(manager->dev);
Inki Dae1c248b72011-10-04 19:19:01 +0900215}
216
Inki Daebcf4cef2012-08-24 10:54:12 -0700217static void exynos_drm_encoder_disable(struct drm_encoder *encoder)
218{
219 struct drm_plane *plane;
220 struct drm_device *dev = encoder->dev;
221
222 exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
223
224 /* all planes connected to this encoder should be also disabled. */
225 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
226 if (plane->crtc == encoder->crtc)
227 plane->funcs->disable_plane(plane);
228 }
229}
230
Inki Dae1c248b72011-10-04 19:19:01 +0900231static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
232 .dpms = exynos_drm_encoder_dpms,
233 .mode_fixup = exynos_drm_encoder_mode_fixup,
234 .mode_set = exynos_drm_encoder_mode_set,
235 .prepare = exynos_drm_encoder_prepare,
236 .commit = exynos_drm_encoder_commit,
Inki Daebcf4cef2012-08-24 10:54:12 -0700237 .disable = exynos_drm_encoder_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900238};
239
240static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
241{
242 struct exynos_drm_encoder *exynos_encoder =
243 to_exynos_encoder(encoder);
244
245 DRM_DEBUG_KMS("%s\n", __FILE__);
246
247 exynos_encoder->manager->pipe = -1;
248
249 drm_encoder_cleanup(encoder);
Inki Dae1c248b72011-10-04 19:19:01 +0900250 kfree(exynos_encoder);
251}
252
253static struct drm_encoder_funcs exynos_encoder_funcs = {
254 .destroy = exynos_drm_encoder_destroy,
255};
256
Inki Daed081f562012-02-15 11:25:19 +0900257static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
258{
259 struct drm_encoder *clone;
260 struct drm_device *dev = encoder->dev;
261 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
262 struct exynos_drm_display_ops *display_ops =
263 exynos_encoder->manager->display_ops;
264 unsigned int clone_mask = 0;
265 int cnt = 0;
266
267 list_for_each_entry(clone, &dev->mode_config.encoder_list, head) {
268 switch (display_ops->type) {
269 case EXYNOS_DISPLAY_TYPE_LCD:
270 case EXYNOS_DISPLAY_TYPE_HDMI:
Inki Daeb73d1232012-03-21 10:55:26 +0900271 case EXYNOS_DISPLAY_TYPE_VIDI:
Inki Daed081f562012-02-15 11:25:19 +0900272 clone_mask |= (1 << (cnt++));
273 break;
274 default:
275 continue;
276 }
277 }
278
279 return clone_mask;
280}
281
282void exynos_drm_encoder_setup(struct drm_device *dev)
283{
284 struct drm_encoder *encoder;
285
286 DRM_DEBUG_KMS("%s\n", __FILE__);
287
288 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
289 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
290}
291
Inki Dae1c248b72011-10-04 19:19:01 +0900292struct drm_encoder *
293exynos_drm_encoder_create(struct drm_device *dev,
294 struct exynos_drm_manager *manager,
295 unsigned int possible_crtcs)
296{
297 struct drm_encoder *encoder;
298 struct exynos_drm_encoder *exynos_encoder;
299
300 DRM_DEBUG_KMS("%s\n", __FILE__);
301
302 if (!manager || !possible_crtcs)
303 return NULL;
304
305 if (!manager->dev)
306 return NULL;
307
308 exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
309 if (!exynos_encoder) {
310 DRM_ERROR("failed to allocate encoder\n");
311 return NULL;
312 }
313
Inki Daeec05da92011-12-06 11:06:54 +0900314 exynos_encoder->dpms = DRM_MODE_DPMS_OFF;
Inki Dae1c248b72011-10-04 19:19:01 +0900315 exynos_encoder->manager = manager;
316 encoder = &exynos_encoder->drm_encoder;
317 encoder->possible_crtcs = possible_crtcs;
318
319 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
320
321 drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
322 DRM_MODE_ENCODER_TMDS);
323
324 drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
325
326 DRM_DEBUG_KMS("encoder has been created\n");
327
328 return encoder;
329}
330
331struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder)
332{
333 return to_exynos_encoder(encoder)->manager;
334}
335
336void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
337 void (*fn)(struct drm_encoder *, void *))
338{
339 struct drm_device *dev = crtc->dev;
340 struct drm_encoder *encoder;
Joonyoung Shimd2716c82011-11-04 17:04:45 +0900341 struct exynos_drm_private *private = dev->dev_private;
342 struct exynos_drm_manager *manager;
Inki Dae1c248b72011-10-04 19:19:01 +0900343
344 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Joonyoung Shimd2716c82011-11-04 17:04:45 +0900345 /*
346 * if crtc is detached from encoder, check pipe,
347 * otherwise check crtc attached to encoder
348 */
349 if (!encoder->crtc) {
350 manager = to_exynos_encoder(encoder)->manager;
351 if (manager->pipe < 0 ||
352 private->crtc[manager->pipe] != crtc)
353 continue;
354 } else {
355 if (encoder->crtc != crtc)
356 continue;
357 }
Inki Dae1c248b72011-10-04 19:19:01 +0900358
359 fn(encoder, data);
360 }
361}
362
363void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
364{
365 struct exynos_drm_manager *manager =
366 to_exynos_encoder(encoder)->manager;
367 struct exynos_drm_manager_ops *manager_ops = manager->ops;
368 int crtc = *(int *)data;
369
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900370 if (manager->pipe != crtc)
371 return;
Inki Dae1c248b72011-10-04 19:19:01 +0900372
373 if (manager_ops->enable_vblank)
374 manager_ops->enable_vblank(manager->dev);
375}
376
377void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
378{
379 struct exynos_drm_manager *manager =
380 to_exynos_encoder(encoder)->manager;
381 struct exynos_drm_manager_ops *manager_ops = manager->ops;
382 int crtc = *(int *)data;
383
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900384 if (manager->pipe != crtc)
385 return;
Inki Dae1c248b72011-10-04 19:19:01 +0900386
387 if (manager_ops->disable_vblank)
388 manager_ops->disable_vblank(manager->dev);
389}
390
Inki Daeec05da92011-12-06 11:06:54 +0900391void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
392{
Inki Daeec05da92011-12-06 11:06:54 +0900393 struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
394 struct exynos_drm_manager *manager = exynos_encoder->manager;
395 struct exynos_drm_manager_ops *manager_ops = manager->ops;
Inki Daeec05da92011-12-06 11:06:54 +0900396 int mode = *(int *)data;
397
398 DRM_DEBUG_KMS("%s\n", __FILE__);
399
400 if (manager_ops && manager_ops->dpms)
401 manager_ops->dpms(manager->dev, mode);
402
403 /*
Inki Dae50caf252012-08-20 21:29:25 +0900404 * set current mode to new one so that data aren't updated into
405 * registers by drm_helper_connector_dpms two times.
406 *
407 * in case that drm_crtc_helper_set_mode() is called,
408 * overlay_ops->commit() and manager_ops->commit() callbacks
409 * can be called two times, first at drm_crtc_helper_set_mode()
410 * and second at drm_helper_connector_dpms().
411 * so with this setting, when drm_helper_connector_dpms() is called
412 * encoder->funcs->dpms() will be ignored.
413 */
414 exynos_encoder->dpms = mode;
415
416 /*
Inki Daeec05da92011-12-06 11:06:54 +0900417 * if this condition is ok then it means that the crtc is already
418 * detached from encoder and last function for detaching is properly
419 * done, so clear pipe from manager to prevent repeated call.
420 */
421 if (mode > DRM_MODE_DPMS_ON) {
422 if (!encoder->crtc)
423 manager->pipe = -1;
424 }
425}
426
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900427void exynos_drm_encoder_crtc_pipe(struct drm_encoder *encoder, void *data)
428{
429 struct exynos_drm_manager *manager =
430 to_exynos_encoder(encoder)->manager;
431 int pipe = *(int *)data;
432
433 DRM_DEBUG_KMS("%s\n", __FILE__);
434
435 /*
436 * when crtc is detached from encoder, this pipe is used
437 * to select manager operation
438 */
439 manager->pipe = pipe;
440}
Joonyoung Shim4070d212012-06-27 14:27:05 +0900441
442void exynos_drm_encoder_plane_mode_set(struct drm_encoder *encoder, void *data)
443{
444 struct exynos_drm_manager *manager =
445 to_exynos_encoder(encoder)->manager;
446 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
447 struct exynos_drm_overlay *overlay = data;
448
449 DRM_DEBUG_KMS("%s\n", __FILE__);
450
451 if (overlay_ops && overlay_ops->mode_set)
452 overlay_ops->mode_set(manager->dev, overlay);
453}
454
455void exynos_drm_encoder_plane_commit(struct drm_encoder *encoder, void *data)
456{
457 struct exynos_drm_manager *manager =
458 to_exynos_encoder(encoder)->manager;
459 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
460 int zpos = DEFAULT_ZPOS;
461
462 DRM_DEBUG_KMS("%s\n", __FILE__);
463
464 if (data)
465 zpos = *(int *)data;
466
467 if (overlay_ops && overlay_ops->commit)
468 overlay_ops->commit(manager->dev, zpos);
469}
470
Joonyoung Shimcf5188a2012-06-27 14:27:09 +0900471void exynos_drm_encoder_plane_enable(struct drm_encoder *encoder, void *data)
472{
473 struct exynos_drm_manager *manager =
474 to_exynos_encoder(encoder)->manager;
475 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
476 int zpos = DEFAULT_ZPOS;
477
478 DRM_DEBUG_KMS("%s\n", __FILE__);
479
480 if (data)
481 zpos = *(int *)data;
482
483 if (overlay_ops && overlay_ops->enable)
484 overlay_ops->enable(manager->dev, zpos);
485}
486
Joonyoung Shim4070d212012-06-27 14:27:05 +0900487void exynos_drm_encoder_plane_disable(struct drm_encoder *encoder, void *data)
488{
489 struct exynos_drm_manager *manager =
490 to_exynos_encoder(encoder)->manager;
491 struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
492 int zpos = DEFAULT_ZPOS;
493
494 DRM_DEBUG_KMS("%s\n", __FILE__);
495
496 if (data)
497 zpos = *(int *)data;
498
499 if (overlay_ops && overlay_ops->disable)
500 overlay_ops->disable(manager->dev, zpos);
Inki Daeb0119412012-08-20 19:55:14 +0900501
502 /*
503 * wait for vblank interrupt
504 * - this makes sure that hardware overlay is disabled to avoid
505 * for the dma accesses to memory after gem buffer was released
506 * because the setting for disabling the overlay will be updated
507 * at vsync.
508 */
509 if (overlay_ops->wait_for_vblank)
510 overlay_ops->wait_for_vblank(manager->dev);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900511}