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