Merge tag 'v3.2-rc6' of /home/airlied/devel/kernel/linux-2.6 into drm-core-next
[~shefty/rdma-dev.git] / drivers / gpu / drm / exynos / exynos_drm_crtc.c
1 /* exynos_drm_crtc.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_crtc.h"
33 #include "exynos_drm_drv.h"
34 #include "exynos_drm_fb.h"
35 #include "exynos_drm_encoder.h"
36 #include "exynos_drm_gem.h"
37 #include "exynos_drm_buf.h"
38
39 #define to_exynos_crtc(x)       container_of(x, struct exynos_drm_crtc,\
40                                 drm_crtc)
41
42 /*
43  * Exynos specific crtc structure.
44  *
45  * @drm_crtc: crtc object.
46  * @overlay: contain information common to display controller and hdmi and
47  *      contents of this overlay object would be copied to sub driver size.
48  * @pipe: a crtc index created at load() with a new crtc object creation
49  *      and the crtc object would be set to private->crtc array
50  *      to get a crtc object corresponding to this pipe from private->crtc
51  *      array when irq interrupt occured. the reason of using this pipe is that
52  *      drm framework doesn't support multiple irq yet.
53  *      we can refer to the crtc to current hardware interrupt occured through
54  *      this pipe value.
55  */
56 struct exynos_drm_crtc {
57         struct drm_crtc                 drm_crtc;
58         struct exynos_drm_overlay       overlay;
59         unsigned int                    pipe;
60 };
61
62 static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
63 {
64         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
65         struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
66
67         exynos_drm_fn_encoder(crtc, overlay,
68                         exynos_drm_encoder_crtc_mode_set);
69         exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
70                         exynos_drm_encoder_crtc_commit);
71 }
72
73 int exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
74                               struct drm_framebuffer *fb,
75                               struct drm_display_mode *mode,
76                               struct exynos_drm_crtc_pos *pos)
77 {
78         struct exynos_drm_gem_buf *buffer;
79         unsigned int actual_w;
80         unsigned int actual_h;
81
82         buffer = exynos_drm_fb_get_buf(fb);
83         if (!buffer) {
84                 DRM_LOG_KMS("buffer is null.\n");
85                 return -EFAULT;
86         }
87
88         overlay->dma_addr = buffer->dma_addr;
89         overlay->vaddr = buffer->kvaddr;
90
91         DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
92                         (unsigned long)overlay->vaddr,
93                         (unsigned long)overlay->dma_addr);
94
95         actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
96         actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
97
98         /* set drm framebuffer data. */
99         overlay->fb_x = pos->fb_x;
100         overlay->fb_y = pos->fb_y;
101         overlay->fb_width = fb->width;
102         overlay->fb_height = fb->height;
103         overlay->bpp = fb->bits_per_pixel;
104         overlay->pitch = fb->pitches[0];
105
106         /* set overlay range to be displayed. */
107         overlay->crtc_x = pos->crtc_x;
108         overlay->crtc_y = pos->crtc_y;
109         overlay->crtc_width = actual_w;
110         overlay->crtc_height = actual_h;
111
112         /* set drm mode data. */
113         overlay->mode_width = mode->hdisplay;
114         overlay->mode_height = mode->vdisplay;
115         overlay->refresh = mode->vrefresh;
116         overlay->scan_flag = mode->flags;
117
118         DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
119                         overlay->crtc_x, overlay->crtc_y,
120                         overlay->crtc_width, overlay->crtc_height);
121
122         return 0;
123 }
124
125 static int exynos_drm_crtc_update(struct drm_crtc *crtc)
126 {
127         struct exynos_drm_crtc *exynos_crtc;
128         struct exynos_drm_overlay *overlay;
129         struct exynos_drm_crtc_pos pos;
130         struct drm_display_mode *mode = &crtc->mode;
131         struct drm_framebuffer *fb = crtc->fb;
132
133         if (!mode || !fb)
134                 return -EINVAL;
135
136         exynos_crtc = to_exynos_crtc(crtc);
137         overlay = &exynos_crtc->overlay;
138
139         memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
140
141         /* it means the offset of framebuffer to be displayed. */
142         pos.fb_x = crtc->x;
143         pos.fb_y = crtc->y;
144
145         /* OSD position to be displayed. */
146         pos.crtc_x = 0;
147         pos.crtc_y = 0;
148         pos.crtc_w = fb->width - crtc->x;
149         pos.crtc_h = fb->height - crtc->y;
150
151         return exynos_drm_overlay_update(overlay, crtc->fb, mode, &pos);
152 }
153
154 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
155 {
156         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
157
158         DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
159
160         switch (mode) {
161         case DRM_MODE_DPMS_ON:
162                 exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
163                                 exynos_drm_encoder_crtc_commit);
164                 break;
165         case DRM_MODE_DPMS_STANDBY:
166         case DRM_MODE_DPMS_SUSPEND:
167         case DRM_MODE_DPMS_OFF:
168                 /* TODO */
169                 exynos_drm_fn_encoder(crtc, NULL,
170                                 exynos_drm_encoder_crtc_disable);
171                 break;
172         default:
173                 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
174                 break;
175         }
176 }
177
178 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
179 {
180         DRM_DEBUG_KMS("%s\n", __FILE__);
181
182         /* drm framework doesn't check NULL. */
183 }
184
185 static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
186 {
187         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
188
189         DRM_DEBUG_KMS("%s\n", __FILE__);
190
191         exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
192                         exynos_drm_encoder_crtc_commit);
193 }
194
195 static bool
196 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
197                             struct drm_display_mode *mode,
198                             struct drm_display_mode *adjusted_mode)
199 {
200         DRM_DEBUG_KMS("%s\n", __FILE__);
201
202         /* drm framework doesn't check NULL */
203         return true;
204 }
205
206 static int
207 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
208                           struct drm_display_mode *adjusted_mode, int x, int y,
209                           struct drm_framebuffer *old_fb)
210 {
211         DRM_DEBUG_KMS("%s\n", __FILE__);
212
213         mode = adjusted_mode;
214
215         return exynos_drm_crtc_update(crtc);
216 }
217
218 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
219                                           struct drm_framebuffer *old_fb)
220 {
221         int ret;
222
223         DRM_DEBUG_KMS("%s\n", __FILE__);
224
225         ret = exynos_drm_crtc_update(crtc);
226         if (ret)
227                 return ret;
228
229         exynos_drm_crtc_apply(crtc);
230
231         return ret;
232 }
233
234 static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
235 {
236         DRM_DEBUG_KMS("%s\n", __FILE__);
237         /* drm framework doesn't check NULL */
238 }
239
240 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
241         .dpms           = exynos_drm_crtc_dpms,
242         .prepare        = exynos_drm_crtc_prepare,
243         .commit         = exynos_drm_crtc_commit,
244         .mode_fixup     = exynos_drm_crtc_mode_fixup,
245         .mode_set       = exynos_drm_crtc_mode_set,
246         .mode_set_base  = exynos_drm_crtc_mode_set_base,
247         .load_lut       = exynos_drm_crtc_load_lut,
248 };
249
250 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
251                                       struct drm_framebuffer *fb,
252                                       struct drm_pending_vblank_event *event)
253 {
254         struct drm_device *dev = crtc->dev;
255         struct exynos_drm_private *dev_priv = dev->dev_private;
256         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
257         struct drm_framebuffer *old_fb = crtc->fb;
258         int ret = -EINVAL;
259
260         DRM_DEBUG_KMS("%s\n", __FILE__);
261
262         mutex_lock(&dev->struct_mutex);
263
264         if (event) {
265                 /*
266                  * the pipe from user always is 0 so we can set pipe number
267                  * of current owner to event.
268                  */
269                 event->pipe = exynos_crtc->pipe;
270
271                 list_add_tail(&event->base.link,
272                                 &dev_priv->pageflip_event_list);
273
274                 ret = drm_vblank_get(dev, exynos_crtc->pipe);
275                 if (ret) {
276                         DRM_DEBUG("failed to acquire vblank counter\n");
277                         list_del(&event->base.link);
278
279                         goto out;
280                 }
281
282                 crtc->fb = fb;
283                 ret = exynos_drm_crtc_update(crtc);
284                 if (ret) {
285                         crtc->fb = old_fb;
286                         drm_vblank_put(dev, exynos_crtc->pipe);
287                         list_del(&event->base.link);
288
289                         goto out;
290                 }
291
292                 /*
293                  * the values related to a buffer of the drm framebuffer
294                  * to be applied should be set at here. because these values
295                  * first, are set to shadow registers and then to
296                  * real registers at vsync front porch period.
297                  */
298                 exynos_drm_crtc_apply(crtc);
299         }
300 out:
301         mutex_unlock(&dev->struct_mutex);
302         return ret;
303 }
304
305 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
306 {
307         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
308         struct exynos_drm_private *private = crtc->dev->dev_private;
309
310         DRM_DEBUG_KMS("%s\n", __FILE__);
311
312         private->crtc[exynos_crtc->pipe] = NULL;
313
314         drm_crtc_cleanup(crtc);
315         kfree(exynos_crtc);
316 }
317
318 static struct drm_crtc_funcs exynos_crtc_funcs = {
319         .set_config     = drm_crtc_helper_set_config,
320         .page_flip      = exynos_drm_crtc_page_flip,
321         .destroy        = exynos_drm_crtc_destroy,
322 };
323
324 struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
325                 struct drm_crtc *crtc)
326 {
327         struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
328
329         return &exynos_crtc->overlay;
330 }
331
332 int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
333 {
334         struct exynos_drm_crtc *exynos_crtc;
335         struct exynos_drm_private *private = dev->dev_private;
336         struct drm_crtc *crtc;
337
338         DRM_DEBUG_KMS("%s\n", __FILE__);
339
340         exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
341         if (!exynos_crtc) {
342                 DRM_ERROR("failed to allocate exynos crtc\n");
343                 return -ENOMEM;
344         }
345
346         exynos_crtc->pipe = nr;
347         crtc = &exynos_crtc->drm_crtc;
348
349         private->crtc[nr] = crtc;
350
351         drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
352         drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
353
354         return 0;
355 }
356
357 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
358 {
359         struct exynos_drm_private *private = dev->dev_private;
360
361         DRM_DEBUG_KMS("%s\n", __FILE__);
362
363         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
364                         exynos_drm_enable_vblank);
365
366         return 0;
367 }
368
369 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
370 {
371         struct exynos_drm_private *private = dev->dev_private;
372
373         DRM_DEBUG_KMS("%s\n", __FILE__);
374
375         exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
376                         exynos_drm_disable_vblank);
377 }
378
379 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
380 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
381 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
382 MODULE_DESCRIPTION("Samsung SoC DRM CRTC Driver");
383 MODULE_LICENSE("GPL");