ff70ef7489d8fdf51411651571eb2b9073a3d769
[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  */
47 struct exynos_drm_encoder {
48         struct drm_encoder drm_encoder;
49         struct exynos_drm_display *display;
50 };
51
52 void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
53 {
54         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
55
56         DRM_DEBUG_KMS("[ENCODER:%d:%s] [DPMS:%s]\n", DRM_BASE_ID(encoder),
57                         drm_get_encoder_name(encoder), drm_get_dpms_name(mode));
58
59         if (!drm_helper_encoder_in_use(encoder))
60                 return;
61
62         if (display->panel_ops->dpms)
63                 display->panel_ops->dpms(display->panel_ctx, mode);
64 }
65
66 static bool
67 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
68                                struct drm_display_mode *mode,
69                                struct drm_display_mode *adjusted_mode)
70 {
71         struct drm_device *dev = encoder->dev;
72         struct drm_connector *connector;
73         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
74
75         DRM_DEBUG_KMS("[ENCODER:%d:%s] [MODE:%d:%s]\n", DRM_BASE_ID(encoder),
76                         drm_get_encoder_name(encoder), DRM_BASE_ID(mode),
77                         mode->name);
78
79         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
80                 if (connector->encoder != encoder)
81                         continue;
82
83                 if (display->panel_ops->mode_fixup)
84                         display->panel_ops->mode_fixup(display->panel_ctx,
85                                         connector, mode, adjusted_mode);
86         }
87
88         return true;
89 }
90
91 static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
92                                          struct drm_display_mode *mode,
93                                          struct drm_display_mode *adjusted_mode)
94 {
95         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
96
97         DRM_DEBUG_KMS("[ENCODER:%d:%s] [MODE:%d:%s]\n", DRM_BASE_ID(encoder),
98                         drm_get_encoder_name(encoder), DRM_BASE_ID(mode),
99                         mode->name);
100
101         if (!drm_helper_encoder_in_use(encoder))
102                 return;
103
104         if (display->panel_ops->mode_set)
105                 display->panel_ops->mode_set(display->panel_ctx, adjusted_mode);
106 }
107
108 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
109 {
110         DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", DRM_BASE_ID(encoder),
111                         drm_get_encoder_name(encoder));
112
113         exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
114 }
115
116 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
117 {
118         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
119
120         DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", DRM_BASE_ID(encoder),
121                         drm_get_encoder_name(encoder));
122
123         exynos_drm_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
124
125         if (display->panel_ops->commit)
126                 display->panel_ops->commit(display->panel_ctx);
127 }
128
129 static struct drm_crtc *
130 exynos_drm_encoder_get_crtc(struct drm_encoder *encoder)
131 {
132         return encoder->crtc;
133 }
134
135 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
136         .dpms           = exynos_drm_encoder_dpms,
137         .mode_fixup     = exynos_drm_encoder_mode_fixup,
138         .mode_set       = exynos_drm_encoder_mode_set,
139         .prepare        = exynos_drm_encoder_prepare,
140         .commit         = exynos_drm_encoder_commit,
141         .get_crtc       = exynos_drm_encoder_get_crtc,
142 };
143
144 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
145 {
146         struct exynos_drm_encoder *exynos_encoder = to_exynos_encoder(encoder);
147
148         DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", DRM_BASE_ID(encoder),
149                         drm_get_encoder_name(encoder));
150
151         exynos_encoder->display->pipe = -1;
152
153         drm_encoder_cleanup(encoder);
154         kfree(exynos_encoder);
155 }
156
157 static struct drm_encoder_funcs exynos_encoder_funcs = {
158         .destroy = exynos_drm_encoder_destroy,
159 };
160
161 static unsigned int exynos_drm_encoder_clones(struct drm_encoder *encoder)
162 {
163         struct drm_encoder *clone;
164         struct drm_device *dev = encoder->dev;
165         struct exynos_drm_display *display = exynos_drm_get_display(encoder);
166         unsigned int clone_mask = 0;
167         int cnt = 0;
168
169         list_for_each_entry(clone, &dev->mode_config.encoder_list, head) {
170                 switch (display->display_type) {
171                 case EXYNOS_DRM_DISPLAY_TYPE_FIMD:
172                 case EXYNOS_DRM_DISPLAY_TYPE_MIXER:
173                 case EXYNOS_DRM_DISPLAY_TYPE_VIDI:
174                         clone_mask |= (1 << (cnt++));
175                         break;
176                 default:
177                         continue;
178                 }
179         }
180
181         return clone_mask;
182 }
183
184 void exynos_drm_encoder_setup(struct drm_device *dev)
185 {
186         struct drm_encoder *encoder;
187
188         DRM_DEBUG_KMS("[DEV:%s]\n", dev->devname);
189
190         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
191                 encoder->possible_clones = exynos_drm_encoder_clones(encoder);
192 }
193
194 struct drm_encoder *
195 exynos_drm_encoder_create(struct drm_device *dev,
196                            struct exynos_drm_display *display,
197                            unsigned int possible_crtcs)
198 {
199         struct drm_encoder *encoder;
200         struct exynos_drm_encoder *exynos_encoder;
201
202         DRM_DEBUG_KMS("[DEV:%s] display_type: %s possible_crtcs: 0x%x\n",
203                         dev->devname, exynos_display_type_name(display),
204                         possible_crtcs);
205
206         if (!display || !possible_crtcs)
207                 return NULL;
208
209         exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
210         if (!exynos_encoder) {
211                 DRM_ERROR("failed to allocate encoder\n");
212                 return NULL;
213         }
214
215         exynos_encoder->display = display;
216         encoder = &exynos_encoder->drm_encoder;
217         encoder->possible_crtcs = possible_crtcs;
218
219         drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
220                         DRM_MODE_ENCODER_TMDS);
221
222         drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
223
224         DRM_DEBUG_KMS("[ENCODER:%d:%s] has been created\n",
225                         DRM_BASE_ID(encoder), drm_get_encoder_name(encoder));
226
227         return encoder;
228 }
229
230 struct exynos_drm_display *exynos_drm_get_display(struct drm_encoder *encoder)
231 {
232         return to_exynos_encoder(encoder)->display;
233 }