blob: de818831a51144393a6f237feb5d81077a1776bc [file] [log] [blame]
Inki Dae1c248b72011-10-04 19:19:01 +09001/* 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
Inki Dae2c871122011-11-12 15:23:32 +090032#include "exynos_drm_crtc.h"
Inki Dae1c248b72011-10-04 19:19:01 +090033#include "exynos_drm_drv.h"
34#include "exynos_drm_fb.h"
35#include "exynos_drm_encoder.h"
Inki Dae2c871122011-11-12 15:23:32 +090036#include "exynos_drm_gem.h"
Inki Dae1c248b72011-10-04 19:19:01 +090037
38#define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
39 drm_crtc)
40
41/*
Inki Dae1c248b72011-10-04 19:19:01 +090042 * Exynos specific crtc structure.
43 *
44 * @drm_crtc: crtc object.
45 * @overlay: contain information common to display controller and hdmi and
46 * contents of this overlay object would be copied to sub driver size.
47 * @pipe: a crtc index created at load() with a new crtc object creation
48 * and the crtc object would be set to private->crtc array
49 * to get a crtc object corresponding to this pipe from private->crtc
50 * array when irq interrupt occured. the reason of using this pipe is that
51 * drm framework doesn't support multiple irq yet.
52 * we can refer to the crtc to current hardware interrupt occured through
53 * this pipe value.
Inki Daeec05da92011-12-06 11:06:54 +090054 * @dpms: store the crtc dpms value
Inki Dae1c248b72011-10-04 19:19:01 +090055 */
56struct exynos_drm_crtc {
57 struct drm_crtc drm_crtc;
58 struct exynos_drm_overlay overlay;
59 unsigned int pipe;
Inki Daeec05da92011-12-06 11:06:54 +090060 unsigned int dpms;
Inki Dae1c248b72011-10-04 19:19:01 +090061};
62
Inki Dae8e9cc6a2011-10-14 13:29:47 +090063static void exynos_drm_crtc_apply(struct drm_crtc *crtc)
Inki Dae1c248b72011-10-04 19:19:01 +090064{
65 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
66 struct exynos_drm_overlay *overlay = &exynos_crtc->overlay;
67
68 exynos_drm_fn_encoder(crtc, overlay,
69 exynos_drm_encoder_crtc_mode_set);
Joonyoung Shimd2716c892011-11-04 17:04:45 +090070 exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
71 exynos_drm_encoder_crtc_commit);
Inki Dae1c248b72011-10-04 19:19:01 +090072}
73
Inki Dae2c871122011-11-12 15:23:32 +090074int exynos_drm_overlay_update(struct exynos_drm_overlay *overlay,
75 struct drm_framebuffer *fb,
76 struct drm_display_mode *mode,
77 struct exynos_drm_crtc_pos *pos)
Inki Dae1c248b72011-10-04 19:19:01 +090078{
Inki Dae2c871122011-11-12 15:23:32 +090079 struct exynos_drm_gem_buf *buffer;
Inki Dae19c8b832011-10-14 13:29:46 +090080 unsigned int actual_w;
81 unsigned int actual_h;
Seung-Woo Kim229d3532011-12-15 14:36:22 +090082 int nr = exynos_drm_format_num_buffers(fb->pixel_format);
83 int i;
Inki Dae1c248b72011-10-04 19:19:01 +090084
Seung-Woo Kim229d3532011-12-15 14:36:22 +090085 for (i = 0; i < nr; i++) {
86 buffer = exynos_drm_fb_buffer(fb, i);
87 if (!buffer) {
88 DRM_LOG_KMS("buffer is null\n");
89 return -EFAULT;
90 }
91
92 overlay->dma_addr[i] = buffer->dma_addr;
93 overlay->vaddr[i] = buffer->kvaddr;
94
95 DRM_DEBUG_KMS("buffer: %d, vaddr = 0x%lx, dma_addr = 0x%lx\n",
96 i, (unsigned long)overlay->vaddr[i],
97 (unsigned long)overlay->dma_addr[i]);
Inki Dae19c8b832011-10-14 13:29:46 +090098 }
Inki Dae1c248b72011-10-04 19:19:01 +090099
Inki Dae19c8b832011-10-14 13:29:46 +0900100 actual_w = min((mode->hdisplay - pos->crtc_x), pos->crtc_w);
101 actual_h = min((mode->vdisplay - pos->crtc_y), pos->crtc_h);
102
103 /* set drm framebuffer data. */
104 overlay->fb_x = pos->fb_x;
105 overlay->fb_y = pos->fb_y;
106 overlay->fb_width = fb->width;
107 overlay->fb_height = fb->height;
Inki Dae1c248b72011-10-04 19:19:01 +0900108 overlay->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200109 overlay->pitch = fb->pitches[0];
Seung-Woo Kim229d3532011-12-15 14:36:22 +0900110 overlay->pixel_format = fb->pixel_format;
Inki Dae19c8b832011-10-14 13:29:46 +0900111
112 /* set overlay range to be displayed. */
113 overlay->crtc_x = pos->crtc_x;
114 overlay->crtc_y = pos->crtc_y;
115 overlay->crtc_width = actual_w;
116 overlay->crtc_height = actual_h;
117
118 /* set drm mode data. */
119 overlay->mode_width = mode->hdisplay;
120 overlay->mode_height = mode->vdisplay;
121 overlay->refresh = mode->vrefresh;
122 overlay->scan_flag = mode->flags;
Inki Dae1c248b72011-10-04 19:19:01 +0900123
124 DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
Inki Dae19c8b832011-10-14 13:29:46 +0900125 overlay->crtc_x, overlay->crtc_y,
126 overlay->crtc_width, overlay->crtc_height);
Inki Dae1c248b72011-10-04 19:19:01 +0900127
Inki Dae19c8b832011-10-14 13:29:46 +0900128 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900129}
130
131static int exynos_drm_crtc_update(struct drm_crtc *crtc)
132{
133 struct exynos_drm_crtc *exynos_crtc;
134 struct exynos_drm_overlay *overlay;
135 struct exynos_drm_crtc_pos pos;
136 struct drm_display_mode *mode = &crtc->mode;
137 struct drm_framebuffer *fb = crtc->fb;
138
139 if (!mode || !fb)
140 return -EINVAL;
141
142 exynos_crtc = to_exynos_crtc(crtc);
143 overlay = &exynos_crtc->overlay;
144
145 memset(&pos, 0, sizeof(struct exynos_drm_crtc_pos));
Inki Dae19c8b832011-10-14 13:29:46 +0900146
147 /* it means the offset of framebuffer to be displayed. */
Inki Dae1c248b72011-10-04 19:19:01 +0900148 pos.fb_x = crtc->x;
149 pos.fb_y = crtc->y;
Inki Dae19c8b832011-10-14 13:29:46 +0900150
151 /* OSD position to be displayed. */
152 pos.crtc_x = 0;
153 pos.crtc_y = 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900154 pos.crtc_w = fb->width - crtc->x;
155 pos.crtc_h = fb->height - crtc->y;
156
Inki Dae19c8b832011-10-14 13:29:46 +0900157 return exynos_drm_overlay_update(overlay, crtc->fb, mode, &pos);
Inki Dae1c248b72011-10-04 19:19:01 +0900158}
159
160static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
161{
Inki Daeec05da92011-12-06 11:06:54 +0900162 struct drm_device *dev = crtc->dev;
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900163 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900164
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900165 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
166
Inki Daeec05da92011-12-06 11:06:54 +0900167 if (exynos_crtc->dpms == mode) {
168 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
169 return;
170 }
171
172 mutex_lock(&dev->struct_mutex);
173
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900174 switch (mode) {
175 case DRM_MODE_DPMS_ON:
Inki Daeec05da92011-12-06 11:06:54 +0900176 exynos_drm_fn_encoder(crtc, &mode,
177 exynos_drm_encoder_crtc_dpms);
178 exynos_crtc->dpms = mode;
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900179 break;
180 case DRM_MODE_DPMS_STANDBY:
181 case DRM_MODE_DPMS_SUSPEND:
182 case DRM_MODE_DPMS_OFF:
Inki Daeec05da92011-12-06 11:06:54 +0900183 exynos_drm_fn_encoder(crtc, &mode,
184 exynos_drm_encoder_crtc_dpms);
185 exynos_crtc->dpms = mode;
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900186 break;
187 default:
Inki Daeec05da92011-12-06 11:06:54 +0900188 DRM_ERROR("unspecified mode %d\n", mode);
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900189 break;
190 }
Inki Daeec05da92011-12-06 11:06:54 +0900191
192 mutex_unlock(&dev->struct_mutex);
Inki Dae1c248b72011-10-04 19:19:01 +0900193}
194
195static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
196{
197 DRM_DEBUG_KMS("%s\n", __FILE__);
198
199 /* drm framework doesn't check NULL. */
200}
201
202static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
203{
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900204 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
205
Inki Dae1c248b72011-10-04 19:19:01 +0900206 DRM_DEBUG_KMS("%s\n", __FILE__);
207
Inki Daeec05da92011-12-06 11:06:54 +0900208 /*
209 * when set_crtc is requested from user or at booting time,
210 * crtc->commit would be called without dpms call so if dpms is
211 * no power on then crtc->dpms should be called
212 * with DRM_MODE_DPMS_ON for the hardware power to be on.
213 */
214 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) {
215 int mode = DRM_MODE_DPMS_ON;
216
217 /*
218 * enable hardware(power on) to all encoders hdmi connected
219 * to current crtc.
220 */
221 exynos_drm_crtc_dpms(crtc, mode);
222 /*
223 * enable dma to all encoders connected to current crtc and
224 * lcd panel.
225 */
226 exynos_drm_fn_encoder(crtc, &mode,
227 exynos_drm_encoder_dpms_from_crtc);
228 }
229
Joonyoung Shimd2716c892011-11-04 17:04:45 +0900230 exynos_drm_fn_encoder(crtc, &exynos_crtc->pipe,
231 exynos_drm_encoder_crtc_commit);
Inki Dae1c248b72011-10-04 19:19:01 +0900232}
233
234static bool
235exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
236 struct drm_display_mode *mode,
237 struct drm_display_mode *adjusted_mode)
238{
239 DRM_DEBUG_KMS("%s\n", __FILE__);
240
241 /* drm framework doesn't check NULL */
242 return true;
243}
244
245static int
246exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
247 struct drm_display_mode *adjusted_mode, int x, int y,
248 struct drm_framebuffer *old_fb)
249{
250 DRM_DEBUG_KMS("%s\n", __FILE__);
251
252 mode = adjusted_mode;
253
254 return exynos_drm_crtc_update(crtc);
255}
256
257static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
258 struct drm_framebuffer *old_fb)
259{
260 int ret;
261
262 DRM_DEBUG_KMS("%s\n", __FILE__);
263
264 ret = exynos_drm_crtc_update(crtc);
265 if (ret)
266 return ret;
267
268 exynos_drm_crtc_apply(crtc);
269
270 return ret;
271}
272
273static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
274{
275 DRM_DEBUG_KMS("%s\n", __FILE__);
276 /* drm framework doesn't check NULL */
277}
278
279static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
280 .dpms = exynos_drm_crtc_dpms,
281 .prepare = exynos_drm_crtc_prepare,
282 .commit = exynos_drm_crtc_commit,
283 .mode_fixup = exynos_drm_crtc_mode_fixup,
284 .mode_set = exynos_drm_crtc_mode_set,
285 .mode_set_base = exynos_drm_crtc_mode_set_base,
286 .load_lut = exynos_drm_crtc_load_lut,
287};
288
289static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
290 struct drm_framebuffer *fb,
291 struct drm_pending_vblank_event *event)
292{
293 struct drm_device *dev = crtc->dev;
294 struct exynos_drm_private *dev_priv = dev->dev_private;
295 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
296 struct drm_framebuffer *old_fb = crtc->fb;
297 int ret = -EINVAL;
298
299 DRM_DEBUG_KMS("%s\n", __FILE__);
300
301 mutex_lock(&dev->struct_mutex);
302
Inki Daeccf4d882011-10-14 13:29:51 +0900303 if (event) {
304 /*
305 * the pipe from user always is 0 so we can set pipe number
306 * of current owner to event.
307 */
308 event->pipe = exynos_crtc->pipe;
309
Inki Dae1c248b72011-10-04 19:19:01 +0900310 ret = drm_vblank_get(dev, exynos_crtc->pipe);
311 if (ret) {
312 DRM_DEBUG("failed to acquire vblank counter\n");
Inki Daeccf4d882011-10-14 13:29:51 +0900313 list_del(&event->base.link);
314
Inki Dae1c248b72011-10-04 19:19:01 +0900315 goto out;
316 }
317
Inki Daec5614ae2012-02-15 11:25:20 +0900318 list_add_tail(&event->base.link,
319 &dev_priv->pageflip_event_list);
320
Inki Dae1c248b72011-10-04 19:19:01 +0900321 crtc->fb = fb;
322 ret = exynos_drm_crtc_update(crtc);
323 if (ret) {
324 crtc->fb = old_fb;
325 drm_vblank_put(dev, exynos_crtc->pipe);
Inki Daeccf4d882011-10-14 13:29:51 +0900326 list_del(&event->base.link);
Inki Dae1c248b72011-10-04 19:19:01 +0900327
328 goto out;
329 }
330
Inki Daef6b98252011-10-14 13:29:50 +0900331 /*
332 * the values related to a buffer of the drm framebuffer
333 * to be applied should be set at here. because these values
Inki Daeccf4d882011-10-14 13:29:51 +0900334 * first, are set to shadow registers and then to
Inki Daef6b98252011-10-14 13:29:50 +0900335 * real registers at vsync front porch period.
336 */
Inki Dae8e9cc6a2011-10-14 13:29:47 +0900337 exynos_drm_crtc_apply(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900338 }
339out:
340 mutex_unlock(&dev->struct_mutex);
341 return ret;
342}
343
344static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
345{
346 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
347 struct exynos_drm_private *private = crtc->dev->dev_private;
348
349 DRM_DEBUG_KMS("%s\n", __FILE__);
350
351 private->crtc[exynos_crtc->pipe] = NULL;
352
353 drm_crtc_cleanup(crtc);
354 kfree(exynos_crtc);
355}
356
357static struct drm_crtc_funcs exynos_crtc_funcs = {
358 .set_config = drm_crtc_helper_set_config,
359 .page_flip = exynos_drm_crtc_page_flip,
360 .destroy = exynos_drm_crtc_destroy,
361};
362
363struct exynos_drm_overlay *get_exynos_drm_overlay(struct drm_device *dev,
364 struct drm_crtc *crtc)
365{
366 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
367
368 return &exynos_crtc->overlay;
369}
370
371int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
372{
373 struct exynos_drm_crtc *exynos_crtc;
374 struct exynos_drm_private *private = dev->dev_private;
375 struct drm_crtc *crtc;
376
377 DRM_DEBUG_KMS("%s\n", __FILE__);
378
379 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
380 if (!exynos_crtc) {
381 DRM_ERROR("failed to allocate exynos crtc\n");
382 return -ENOMEM;
383 }
384
385 exynos_crtc->pipe = nr;
Inki Daeec05da92011-12-06 11:06:54 +0900386 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900387 exynos_crtc->overlay.zpos = DEFAULT_ZPOS;
Inki Dae1c248b72011-10-04 19:19:01 +0900388 crtc = &exynos_crtc->drm_crtc;
389
390 private->crtc[nr] = crtc;
391
392 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
393 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
394
395 return 0;
396}
397
398int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
399{
400 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900401 struct exynos_drm_crtc *exynos_crtc =
402 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900403
404 DRM_DEBUG_KMS("%s\n", __FILE__);
405
Inki Daeec05da92011-12-06 11:06:54 +0900406 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
407 return -EPERM;
408
Inki Dae1c248b72011-10-04 19:19:01 +0900409 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
410 exynos_drm_enable_vblank);
411
412 return 0;
413}
414
415void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
416{
417 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900418 struct exynos_drm_crtc *exynos_crtc =
419 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900420
421 DRM_DEBUG_KMS("%s\n", __FILE__);
422
Inki Daeec05da92011-12-06 11:06:54 +0900423 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
424 return;
425
Inki Dae1c248b72011-10-04 19:19:01 +0900426 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
427 exynos_drm_disable_vblank);
428}
429
430MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
431MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
432MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
433MODULE_DESCRIPTION("Samsung SoC DRM CRTC Driver");
434MODULE_LICENSE("GPL");