Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[pandora-kernel.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
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.
45  */
46 struct exynos_drm_encoder {
47         struct drm_encoder              drm_encoder;
48         struct exynos_drm_manager       *manager;
49 };
50
51 static void exynos_drm_encoder_dpms(struct drm_encoder *encoder, int mode)
52 {
53         struct drm_device *dev = encoder->dev;
54         struct drm_connector *connector;
55         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
56         struct exynos_drm_manager_ops *manager_ops = manager->ops;
57
58         DRM_DEBUG_KMS("%s, encoder dpms: %d\n", __FILE__, mode);
59
60         switch (mode) {
61         case DRM_MODE_DPMS_ON:
62                 if (manager_ops && manager_ops->commit)
63                         manager_ops->commit(manager->dev);
64                 break;
65         case DRM_MODE_DPMS_STANDBY:
66         case DRM_MODE_DPMS_SUSPEND:
67         case DRM_MODE_DPMS_OFF:
68                 /* TODO */
69                 if (manager_ops && manager_ops->disable)
70                         manager_ops->disable(manager->dev);
71                 break;
72         default:
73                 DRM_ERROR("unspecified mode %d\n", mode);
74                 break;
75         }
76
77         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
78                 if (connector->encoder == encoder) {
79                         struct exynos_drm_display_ops *display_ops =
80                                                         manager->display_ops;
81
82                         DRM_DEBUG_KMS("connector[%d] dpms[%d]\n",
83                                         connector->base.id, mode);
84                         if (display_ops && display_ops->power_on)
85                                 display_ops->power_on(manager->dev, mode);
86                 }
87         }
88 }
89
90 static bool
91 exynos_drm_encoder_mode_fixup(struct drm_encoder *encoder,
92                                struct drm_display_mode *mode,
93                                struct drm_display_mode *adjusted_mode)
94 {
95         DRM_DEBUG_KMS("%s\n", __FILE__);
96
97         /* drm framework doesn't check NULL. */
98
99         return true;
100 }
101
102 static void exynos_drm_encoder_mode_set(struct drm_encoder *encoder,
103                                          struct drm_display_mode *mode,
104                                          struct drm_display_mode *adjusted_mode)
105 {
106         struct drm_device *dev = encoder->dev;
107         struct drm_connector *connector;
108         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
109         struct exynos_drm_manager_ops *manager_ops = manager->ops;
110         struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
111         struct exynos_drm_overlay *overlay = get_exynos_drm_overlay(dev,
112                                                 encoder->crtc);
113
114         DRM_DEBUG_KMS("%s\n", __FILE__);
115
116         mode = adjusted_mode;
117
118         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
119                 if (connector->encoder == encoder) {
120                         if (manager_ops && manager_ops->mode_set)
121                                 manager_ops->mode_set(manager->dev, mode);
122
123                         if (overlay_ops && overlay_ops->mode_set)
124                                 overlay_ops->mode_set(manager->dev, overlay);
125                 }
126         }
127 }
128
129 static void exynos_drm_encoder_prepare(struct drm_encoder *encoder)
130 {
131         DRM_DEBUG_KMS("%s\n", __FILE__);
132
133         /* drm framework doesn't check NULL. */
134 }
135
136 static void exynos_drm_encoder_commit(struct drm_encoder *encoder)
137 {
138         struct exynos_drm_manager *manager = exynos_drm_get_manager(encoder);
139         struct exynos_drm_manager_ops *manager_ops = manager->ops;
140
141         DRM_DEBUG_KMS("%s\n", __FILE__);
142
143         if (manager_ops && manager_ops->commit)
144                 manager_ops->commit(manager->dev);
145 }
146
147 static struct drm_crtc *
148 exynos_drm_encoder_get_crtc(struct drm_encoder *encoder)
149 {
150         return encoder->crtc;
151 }
152
153 static struct drm_encoder_helper_funcs exynos_encoder_helper_funcs = {
154         .dpms           = exynos_drm_encoder_dpms,
155         .mode_fixup     = exynos_drm_encoder_mode_fixup,
156         .mode_set       = exynos_drm_encoder_mode_set,
157         .prepare        = exynos_drm_encoder_prepare,
158         .commit         = exynos_drm_encoder_commit,
159         .get_crtc       = exynos_drm_encoder_get_crtc,
160 };
161
162 static void exynos_drm_encoder_destroy(struct drm_encoder *encoder)
163 {
164         struct exynos_drm_encoder *exynos_encoder =
165                 to_exynos_encoder(encoder);
166
167         DRM_DEBUG_KMS("%s\n", __FILE__);
168
169         exynos_encoder->manager->pipe = -1;
170
171         drm_encoder_cleanup(encoder);
172         encoder->dev->mode_config.num_encoder--;
173         kfree(exynos_encoder);
174 }
175
176 static struct drm_encoder_funcs exynos_encoder_funcs = {
177         .destroy = exynos_drm_encoder_destroy,
178 };
179
180 struct drm_encoder *
181 exynos_drm_encoder_create(struct drm_device *dev,
182                            struct exynos_drm_manager *manager,
183                            unsigned int possible_crtcs)
184 {
185         struct drm_encoder *encoder;
186         struct exynos_drm_encoder *exynos_encoder;
187
188         DRM_DEBUG_KMS("%s\n", __FILE__);
189
190         if (!manager || !possible_crtcs)
191                 return NULL;
192
193         if (!manager->dev)
194                 return NULL;
195
196         exynos_encoder = kzalloc(sizeof(*exynos_encoder), GFP_KERNEL);
197         if (!exynos_encoder) {
198                 DRM_ERROR("failed to allocate encoder\n");
199                 return NULL;
200         }
201
202         exynos_encoder->manager = manager;
203         encoder = &exynos_encoder->drm_encoder;
204         encoder->possible_crtcs = possible_crtcs;
205
206         DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
207
208         drm_encoder_init(dev, encoder, &exynos_encoder_funcs,
209                         DRM_MODE_ENCODER_TMDS);
210
211         drm_encoder_helper_add(encoder, &exynos_encoder_helper_funcs);
212
213         DRM_DEBUG_KMS("encoder has been created\n");
214
215         return encoder;
216 }
217
218 struct exynos_drm_manager *exynos_drm_get_manager(struct drm_encoder *encoder)
219 {
220         return to_exynos_encoder(encoder)->manager;
221 }
222
223 void exynos_drm_fn_encoder(struct drm_crtc *crtc, void *data,
224                             void (*fn)(struct drm_encoder *, void *))
225 {
226         struct drm_device *dev = crtc->dev;
227         struct drm_encoder *encoder;
228         struct exynos_drm_private *private = dev->dev_private;
229         struct exynos_drm_manager *manager;
230
231         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
232                 /*
233                  * if crtc is detached from encoder, check pipe,
234                  * otherwise check crtc attached to encoder
235                  */
236                 if (!encoder->crtc) {
237                         manager = to_exynos_encoder(encoder)->manager;
238                         if (manager->pipe < 0 ||
239                                         private->crtc[manager->pipe] != crtc)
240                                 continue;
241                 } else {
242                         if (encoder->crtc != crtc)
243                                 continue;
244                 }
245
246                 fn(encoder, data);
247         }
248 }
249
250 void exynos_drm_enable_vblank(struct drm_encoder *encoder, void *data)
251 {
252         struct exynos_drm_manager *manager =
253                 to_exynos_encoder(encoder)->manager;
254         struct exynos_drm_manager_ops *manager_ops = manager->ops;
255         int crtc = *(int *)data;
256
257         if (manager->pipe == -1)
258                 manager->pipe = crtc;
259
260         if (manager_ops->enable_vblank)
261                 manager_ops->enable_vblank(manager->dev);
262 }
263
264 void exynos_drm_disable_vblank(struct drm_encoder *encoder, void *data)
265 {
266         struct exynos_drm_manager *manager =
267                 to_exynos_encoder(encoder)->manager;
268         struct exynos_drm_manager_ops *manager_ops = manager->ops;
269         int crtc = *(int *)data;
270
271         if (manager->pipe == -1)
272                 manager->pipe = crtc;
273
274         if (manager_ops->disable_vblank)
275                 manager_ops->disable_vblank(manager->dev);
276 }
277
278 void exynos_drm_encoder_crtc_commit(struct drm_encoder *encoder, void *data)
279 {
280         struct exynos_drm_manager *manager =
281                 to_exynos_encoder(encoder)->manager;
282         struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
283         int crtc = *(int *)data;
284
285         DRM_DEBUG_KMS("%s\n", __FILE__);
286
287         /*
288          * when crtc is detached from encoder, this pipe is used
289          * to select manager operation
290          */
291         manager->pipe = crtc;
292
293         if (overlay_ops && overlay_ops->commit)
294                 overlay_ops->commit(manager->dev);
295 }
296
297 void exynos_drm_encoder_crtc_mode_set(struct drm_encoder *encoder, void *data)
298 {
299         struct exynos_drm_manager *manager =
300                 to_exynos_encoder(encoder)->manager;
301         struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
302         struct exynos_drm_overlay *overlay = data;
303
304         if (overlay_ops && overlay_ops->mode_set)
305                 overlay_ops->mode_set(manager->dev, overlay);
306 }
307
308 void exynos_drm_encoder_crtc_disable(struct drm_encoder *encoder, void *data)
309 {
310         struct exynos_drm_manager *manager =
311                 to_exynos_encoder(encoder)->manager;
312         struct exynos_drm_overlay_ops *overlay_ops = manager->overlay_ops;
313
314         DRM_DEBUG_KMS("\n");
315
316         if (overlay_ops && overlay_ops->disable)
317                 overlay_ops->disable(manager->dev);
318
319         /*
320          * crtc is already detached from encoder and last
321          * function for detaching is properly done, so
322          * clear pipe from manager to prevent repeated call
323          */
324         if (!encoder->crtc)
325                 manager->pipe = -1;
326 }
327
328 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
329 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
330 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
331 MODULE_DESCRIPTION("Samsung SoC DRM Encoder Driver");
332 MODULE_LICENSE("GPL");