drm/exynos: Remove apply() callback
[cascardo/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  * 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
29 #include "drmP.h"
30 #include "drm_crtc_helper.h"
31
32 #include "exynos_drm_drv.h"
33 #include "exynos_drm_crtc.h"
34 #include "exynos_drm_encoder.h"
35 #include "exynos_drm_display.h"
36
37 #define to_exynos_encoder(x)    container_of(x, struct exynos_drm_encoder,\
38                                 drm_encoder)
39
40 /*
41  * exynos specific encoder structure.
42  *
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.
47  */
48 struct exynos_drm_encoder {
49         struct drm_encoder drm_encoder;
50         struct exynos_drm_display *display;
51         int dpms;
52 };
53
54 static void exynos_drm_display_power(struct drm_encoder *encoder, int mode)
55 {
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);
60
61         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
62                 if (connector->encoder != encoder)
63                         continue;
64
65                 DRM_DEBUG_KMS("connector[%d] dpms[%d]\n", connector->base.id,
66                                 mode);
67
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.
71                  */
72                 switch (mode) {
73                 case DRM_MODE_DPMS_ON:
74                         if (display->controller_ops->power)
75                                 display->controller_ops->power(
76                                                 display->controller_ctx, mode);
77
78                         if (display->panel_ops->power)
79                                 display->panel_ops->power(display->panel_ctx,
80                                                 mode);
81
82                         exynos_encoder->dpms = mode;
83                         break;
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,
89                                                 mode);
90
91                         if (display->controller_ops->power)
92                                 display->controller_ops->power(
93                                                 display->controller_ctx, mode);
94
95                         exynos_encoder->dpms = mode;
96                         break;
97                 default:
98                         DRM_ERROR("Unknown dpms mode: %d\n", mode);
99                         break;
100                 }
101         }
102 }
103
104 int exynos_drm_encoder_get_dpms(struct drm_encoder *encoder)
105 {
106         struct drm_device *dev = encoder->dev;
107         struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
108         int mode;
109
110         mutex_lock(&dev->struct_mutex);
111         mode = exynos_encoder->dpms;
112         mutex_unlock(&dev->struct_mutex);
113
114         return mode;
115 }
116
117 void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
118 {
119         struct drm_device *dev = encoder->dev;
120         struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
121
122         DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode);
123
124         mutex_lock(&dev->struct_mutex);
125
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);
129                 return;
130         }
131
132         switch (mode) {
133         case DRM_MODE_DPMS_ON:
134                 exynos_drm_display_power(encoder, mode);
135                 break;
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);
140                 break;
141         default:
142                 DRM_ERROR("unspecified mode %d\n", mode);
143                 break;
144         }
145
146         mutex_unlock(&dev->struct_mutex);
147 }
148
149 static bool
150 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
151                                struct drm_display_mode *mode,
152                                struct drm_display_mode *adjusted_mode)
153 {
154         struct drm_device *dev = encoder->dev;
155         struct drm_connector *connector;
156         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
157
158         DRM_DEBUG_KMS("%s\n", __FILE__);
159
160         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
161                 if (connector->encoder != encoder)
162                         continue;
163
164                 if (display->panel_ops->mode_fixup)
165                         display->panel_ops->mode_fixup(display->panel_ctx,
166                                         connector, mode, adjusted_mode);
167         }
168
169         return true;
170 }
171
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)
175 {
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,
180                                                 encoder->crtc);
181
182         DRM_DEBUG_KMS("%s\n", __FILE__);
183
184         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
185                 if (connector->encoder != encoder)
186                         continue;
187
188                 if (display->panel_ops->mode_set)
189                         display->panel_ops->mode_set(display->panel_ctx,
190                                         adjusted_mode);
191
192                 if (display->controller_ops->mode_set)
193                         display->controller_ops->mode_set(
194                                         display->controller_ctx, overlay);
195         }
196 }
197
198 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
199 {
200         DRM_DEBUG_KMS("%s\n", __FILE__);
201
202         /* drm framework doesn't check NULL. */
203 }
204
205 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
206 {
207         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
208
209         DRM_DEBUG_KMS("%s\n", __FILE__);
210         if (display->controller_ops->commit)
211                 display->controller_ops->commit(display->controller_ctx);
212
213         if (display->panel_ops->commit)
214                 display->panel_ops->commit(display->panel_ctx);
215 }
216
217 static struct drm_crtc *
218 exynos_drm_encoder_get_crtc(struct drm_encoder *encoder)
219 {
220         return encoder->crtc;
221 }
222
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,
230 };
231
232 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
233 {
234         struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
235
236         DRM_DEBUG_KMS("%s\n", __FILE__);
237
238         exynos_encoder->display->pipe = -1;
239
240         drm_encoder_cleanup(encoder);
241         kfree(exynos_encoder);
242 }
243
244 static struct drm_encoder_funcs exynos_encoder_funcs = {
245         .destroy = exynos_drm_encoder_destroy,
246 };
247
248 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
249 {
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;
254         int cnt = 0;
255
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++));
262                         break;
263                 default:
264                         continue;
265                 }
266         }
267
268         return clone_mask;
269 }
270
271 void exynos_drm_encoder_setup(struct drm_device *dev)
272 {
273         struct drm_encoder *encoder;
274
275         DRM_DEBUG_KMS("%s\n", __FILE__);
276
277         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
278                 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
279 }
280
281 struct drm_encoder *
282 exynos_drm_encoder_create(struct drm_device *dev,
283                            struct exynos_drm_display *display,
284                            unsigned int possible_crtcs)
285 {
286         struct drm_encoder *encoder;
287         struct exynos_drm_encoder *exynos_encoder;
288
289         DRM_DEBUG_KMS("%s\n", __FILE__);
290
291         if (!display || !possible_crtcs)
292                 return NULL;
293
294         exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
295         if (!exynos_encoder) {
296                 DRM_ERROR("failed to allocate encoder\n");
297                 return NULL;
298         }
299
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;
304
305         DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
306
307         drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
308                         DRM_MODE_ENCODER_TMDS);
309
310         drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
311
312         DRM_DEBUG_KMS("encoder has been created\n");
313
314         return encoder;
315 }
316
317 struct exynos_drm_display *exynos_drm_get_display(struct drm_encoder *encoder)
318 {
319         return to_exynos_encoder(encoder)->display;
320 }
321
322 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
323                             void (*fn)(struct drm_encoder *, void *))
324 {
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;
329
330         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
331                 /*
332                  * if crtc is detached from encoder, check pipe,
333                  * otherwise check crtc attached to encoder
334                  */
335                 if (!encoder->crtc) {
336                         display = exynos_drm_get_display(encoder);
337                         if (display->pipe < 0 ||
338                                         private->crtc[display->pipe] != crtc)
339                                 continue;
340                 } else if (encoder->crtc != crtc) {
341                         continue;
342                 }
343
344                 fn(encoder, data);
345         }
346 }
347
348 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
349 {
350         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
351         int crtc = *(int *)data;
352
353         if (display->pipe == -1)
354                 display->pipe = crtc;
355
356         /* TODO(seanpaul): Fix the pipe interaction here */
357         if (display->controller_ops->enable_vblank)
358                 display->controller_ops->enable_vblank(display->controller_ctx,
359                                 display->pipe);
360 }
361
362 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
363 {
364         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
365         int crtc = *(int *)data;
366
367         /*
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.
374          */
375         if (display->pipe == -1)
376                 display->pipe = crtc;
377
378         if (display->controller_ops->disable_vblank)
379                 display->controller_ops->disable_vblank(
380                                 display->controller_ctx);
381 }
382
383 void exynos_drm_encoder_crtc_plane_commit(struct drm_encoder *encoder,
384                                           void *data)
385 {
386         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
387         int zpos = DEFAULT_ZPOS;
388
389         if (data)
390                 zpos = *(int *)data;
391
392         if (display->controller_ops->win_commit)
393                 display->controller_ops->win_commit(display->controller_ctx,
394                                 zpos);
395 }
396
397 void exynos_drm_encoder_crtc_commit(struct drm_encoder *encoder, void *data)
398 {
399         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
400         int crtc = *(int *)data;
401         int zpos = DEFAULT_ZPOS;
402
403         DRM_DEBUG_KMS("%s\n", __FILE__);
404
405         /*
406          * when crtc is detached from encoder, this pipe is used
407          * to select display operation
408          */
409         display->pipe = crtc;
410
411         exynos_drm_encoder_crtc_plane_commit(encoder, &zpos);
412 }
413
414 void exynos_drm_encoder_dpms_from_crtc(struct drm_encoder *encoder, void *data)
415 {
416         int mode = *(int *)data;
417
418         DRM_DEBUG_KMS("%s\n", __FILE__);
419
420         exynos_drm_encoder_dpms(encoder, mode);
421 }
422
423 void exynos_drm_encoder_crtc_dpms(struct drm_encoder *encoder, void *data)
424 {
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;
430
431         DRM_DEBUG_KMS("%s\n", __FILE__);
432
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
435          * reverse.
436          */
437         switch (mode) {
438         case DRM_MODE_DPMS_ON:
439                 if (display->controller_ops->dpms)
440                         display->controller_ops->dpms(display->controller_ctx,
441                                         mode);
442
443                 if (display->panel_ops->dpms)
444                         display->panel_ops->dpms(display->panel_ctx, mode);
445                 break;
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);
451
452                 if (display->controller_ops->dpms)
453                         display->controller_ops->dpms(display->controller_ctx,
454                                         mode);
455                 break;
456         default:
457                 DRM_ERROR("Unknown dpms mode: %d\n", mode);
458                 break;
459         }
460
461         /*
462          * set current dpms mode to the connector connected to
463          * current encoder. connector->dpms would be checked
464          * at drm_helper_connector_dpms()
465          */
466         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
467                 if (connector->encoder == encoder)
468                         connector->dpms = mode;
469
470         /*
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.
474          */
475         if (mode > DRM_MODE_DPMS_ON) {
476                 if (!encoder->crtc)
477                         display->pipe = -1;
478         }
479 }
480
481 void exynos_drm_encoder_crtc_mode_set(struct drm_encoder *encoder, void *data)
482 {
483         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
484         struct exynos_drm_overlay *overlay = data;
485
486         if (display->controller_ops->mode_set)
487                 display->controller_ops->mode_set(display->controller_ctx,
488                                 overlay);
489 }
490
491 void exynos_drm_encoder_crtc_disable(struct drm_encoder *encoder, void *data)
492 {
493         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
494         int zpos = DEFAULT_ZPOS;
495
496         DRM_DEBUG_KMS("\n");
497
498         if (data)
499                 zpos = *(int *)data;
500
501         if (display->controller_ops->win_disable)
502                 display->controller_ops->win_disable(display->controller_ctx,
503                                 zpos);
504 }