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