]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/exynos/exynos_drm_encoder.c
drm/exynos: Don't keep dpms state in encoder
[karo-tx-linux.git] / drivers / gpu / drm / exynos / exynos_drm_encoder.c
1 /* 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  * 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.
13  */
14
15 #include <drm/drmP.h>
16 #include <drm/drm_crtc_helper.h>
17
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_encoder.h"
20 #include "exynos_drm_connector.h"
21
22 #define to_exynos_encoder(x)    container_of(x, struct exynos_drm_encoder,\
23                                 drm_encoder)
24
25 /*
26  * exynos specific encoder structure.
27  *
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  */
32 struct exynos_drm_encoder {
33         struct drm_crtc                 *old_crtc;
34         struct drm_encoder              drm_encoder;
35         struct exynos_drm_manager       *manager;
36 };
37
38 static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
39 {
40         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
41         struct exynos_drm_display_ops *display_ops = manager->display_ops;
42
43         DRM_DEBUG_KMS("encoder dpms: %d\n", mode);
44
45         if (display_ops && display_ops->dpms)
46                 display_ops->dpms(manager->ctx, mode);
47 }
48
49 static bool
50 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
51                                const struct drm_display_mode *mode,
52                                struct drm_display_mode *adjusted_mode)
53 {
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;
58
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,
63                                                         mode, adjusted_mode);
64         }
65
66         return true;
67 }
68
69 static void disable_plane_to_crtc(struct drm_device *dev,
70                                                 struct drm_crtc *old_crtc,
71                                                 struct drm_crtc *new_crtc)
72 {
73         struct drm_plane *plane;
74
75         /*
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
79          * (encoder->crtc)
80          */
81         list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
82                 if (plane->crtc == old_crtc) {
83                         /*
84                          * do not change below call order.
85                          *
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.
92                          */
93                         plane->crtc = new_crtc;
94                         plane->funcs->disable_plane(plane);
95                 }
96         }
97 }
98
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)
102 {
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;
107
108         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
109                 if (connector->encoder == encoder) {
110                         struct exynos_drm_encoder *exynos_encoder;
111
112                         exynos_encoder = to_exynos_encoder(encoder);
113
114                         if (exynos_encoder->old_crtc != encoder->crtc &&
115                                         exynos_encoder->old_crtc) {
116
117                                 /*
118                                  * disable a plane to old crtc and change
119                                  * crtc of the plane to new one.
120                                  */
121                                 disable_plane_to_crtc(dev,
122                                                 exynos_encoder->old_crtc,
123                                                 encoder->crtc);
124                         }
125
126                         manager = exynos_drm_get_manager(encoder);
127                         manager_ops = manager->ops;
128
129                         if (manager_ops && manager_ops->mode_set)
130                                 manager_ops->mode_set(manager, adjusted_mode);
131
132                         exynos_encoder->old_crtc = encoder->crtc;
133                 }
134         }
135 }
136
137 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
138 {
139         /* drm framework doesn't check NULL. */
140 }
141
142 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
143 {
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;
147
148         if (manager_ops && manager_ops->commit)
149                 manager_ops->commit(manager);
150 }
151
152 void exynos_drm_encoder_complete_scanout(struct drm_framebuffer *fb)
153 {
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;
158
159         /*
160          * make sure that overlay data are updated to real hardware
161          * for all encoders.
162          */
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;
166
167                 /*
168                  * wait for vblank interrupt
169                  * - this makes sure that overlay data are updated to
170                  *      real hardware.
171                  */
172                 if (ops->wait_for_vblank)
173                         ops->wait_for_vblank(exynos_encoder->manager);
174         }
175 }
176
177
178 static void exynos_drm_encoder_disable(struct drm_encoder *encoder)
179 {
180         struct drm_plane *plane;
181         struct drm_device *dev = encoder->dev;
182
183         exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
184
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);
189         }
190 }
191
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,
199 };
200
201 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
202 {
203         struct exynos_drm_encoder *exynos_encoder =
204                 to_exynos_encoder(encoder);
205
206         exynos_encoder->manager->pipe = -1;
207
208         drm_encoder_cleanup(encoder);
209         kfree(exynos_encoder);
210 }
211
212 static struct drm_encoder_funcs exynos_encoder_funcs = {
213         .destroy = exynos_drm_encoder_destroy,
214 };
215
216 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
217 {
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;
224         int cnt = 0;
225
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++));
232                         break;
233                 default:
234                         continue;
235                 }
236         }
237
238         return clone_mask;
239 }
240
241 void exynos_drm_encoder_setup(struct drm_device *dev)
242 {
243         struct drm_encoder *encoder;
244
245         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
246                 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
247 }
248
249 struct drm_encoder *
250 exynos_drm_encoder_create(struct drm_device *dev,
251                            struct exynos_drm_manager *manager,
252                            unsigned int possible_crtcs)
253 {
254         struct drm_encoder *encoder;
255         struct exynos_drm_encoder *exynos_encoder;
256         int ret;
257
258         if (!manager || !possible_crtcs)
259                 return NULL;
260
261         if (!manager->dev)
262                 return NULL;
263
264         exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
265         if (!exynos_encoder)
266                 return NULL;
267
268         exynos_encoder->manager = manager;
269         encoder = &exynos_encoder->drm_encoder;
270         encoder->possible_crtcs = possible_crtcs;
271
272         DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
273
274         drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
275                         DRM_MODE_ENCODER_TMDS);
276
277         drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
278
279         if (manager->ops && manager->ops->initialize) {
280                 ret = manager->ops->initialize(manager, dev);
281                 if (ret) {
282                         DRM_ERROR("Manager initialize failed %d\n", ret);
283                         goto error;
284                 }
285         }
286
287         if (manager->display_ops && manager->display_ops->initialize) {
288                 ret = manager->display_ops->initialize(manager->dev, dev);
289                 if (ret) {
290                         DRM_ERROR("Display initialize failed %d\n", ret);
291                         goto error;
292                 }
293         }
294
295         DRM_DEBUG_KMS("encoder has been created\n");
296
297         return encoder;
298
299 error:
300         exynos_drm_encoder_destroy(&exynos_encoder->drm_encoder);
301         return NULL;
302 }
303
304 struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder)
305 {
306         return to_exynos_encoder(encoder)->manager;
307 }
308
309 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
310                             void (*fn)(struct drm_encoder *, void *))
311 {
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;
316
317         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
318                 /*
319                  * if crtc is detached from encoder, check pipe,
320                  * otherwise check crtc attached to encoder
321                  */
322                 if (!encoder->crtc) {
323                         manager = to_exynos_encoder(encoder)->manager;
324                         if (manager->pipe < 0 ||
325                                         private->crtc[manager->pipe] != crtc)
326                                 continue;
327                 } else {
328                         if (encoder->crtc != crtc)
329                                 continue;
330                 }
331
332                 fn(encoder, data);
333         }
334 }
335
336 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
337 {
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;
342
343         if (manager->pipe != crtc)
344                 return;
345
346         if (manager_ops->enable_vblank)
347                 manager_ops->enable_vblank(manager);
348 }
349
350 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
351 {
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;
356
357         if (manager->pipe != crtc)
358                 return;
359
360         if (manager_ops->disable_vblank)
361                 manager_ops->disable_vblank(manager);
362 }
363
364 void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
365 {
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;
370
371         if (manager_ops && manager_ops->dpms)
372                 manager_ops->dpms(manager, mode);
373
374         /*
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.
378          */
379         if (mode > DRM_MODE_DPMS_ON) {
380                 if (!encoder->crtc)
381                         manager->pipe = -1;
382         }
383 }
384
385 void exynos_drm_encoder_crtc_pipe(struct drm_encoder *encoder, void *data)
386 {
387         struct exynos_drm_manager *manager =
388                 to_exynos_encoder(encoder)->manager;
389         int pipe = *(int *)data;
390
391         /*
392          * when crtc is detached from encoder, this pipe is used
393          * to select manager operation
394          */
395         manager->pipe = pipe;
396 }
397
398 void exynos_drm_encoder_plane_mode_set(struct drm_encoder *encoder, void *data)
399 {
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;
404
405         if (manager_ops && manager_ops->win_mode_set)
406                 manager_ops->win_mode_set(manager, overlay);
407 }
408
409 void exynos_drm_encoder_plane_commit(struct drm_encoder *encoder, void *data)
410 {
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;
415
416         if (data)
417                 zpos = *(int *)data;
418
419         if (manager_ops && manager_ops->win_commit)
420                 manager_ops->win_commit(manager, zpos);
421 }
422
423 void exynos_drm_encoder_plane_enable(struct drm_encoder *encoder, void *data)
424 {
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;
429
430         if (data)
431                 zpos = *(int *)data;
432
433         if (manager_ops && manager_ops->win_enable)
434                 manager_ops->win_enable(manager, zpos);
435 }
436
437 void exynos_drm_encoder_plane_disable(struct drm_encoder *encoder, void *data)
438 {
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;
443
444         if (data)
445                 zpos = *(int *)data;
446
447         if (manager_ops && manager_ops->win_disable)
448                 manager_ops->win_disable(manager, zpos);
449 }