blob: 14f5c1d34028fac5aeb0c6e66dfed6b25160d502 [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 *
Inki Daed81aecb2012-12-18 02:30:17 +09009 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
Inki Dae1c248b72011-10-04 19:19:01 +090013 */
14
David Howells760285e2012-10-02 18:01:07 +010015#include <drm/drmP.h>
16#include <drm/drm_crtc_helper.h>
Inki Dae1c248b72011-10-04 19:19:01 +090017
18#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090019#include "exynos_drm_encoder.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090020#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090021
22#define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
23 drm_crtc)
24
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090025enum exynos_crtc_mode {
26 CRTC_MODE_NORMAL, /* normal mode */
27 CRTC_MODE_BLANK, /* The private plane of crtc is blank */
28};
29
Inki Dae1c248b72011-10-04 19:19:01 +090030/*
Inki Dae1c248b72011-10-04 19:19:01 +090031 * Exynos specific crtc structure.
32 *
33 * @drm_crtc: crtc object.
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090034 * @drm_plane: pointer of private plane object for this crtc
Inki Dae1c248b72011-10-04 19:19:01 +090035 * @pipe: a crtc index created at load() with a new crtc object creation
36 * and the crtc object would be set to private->crtc array
37 * to get a crtc object corresponding to this pipe from private->crtc
38 * array when irq interrupt occured. the reason of using this pipe is that
39 * drm framework doesn't support multiple irq yet.
40 * we can refer to the crtc to current hardware interrupt occured through
41 * this pipe value.
Inki Daeec05da92011-12-06 11:06:54 +090042 * @dpms: store the crtc dpms value
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090043 * @mode: store the crtc mode value
Inki Dae1c248b72011-10-04 19:19:01 +090044 */
45struct exynos_drm_crtc {
46 struct drm_crtc drm_crtc;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090047 struct drm_plane *plane;
Inki Dae1c248b72011-10-04 19:19:01 +090048 unsigned int pipe;
Inki Daeec05da92011-12-06 11:06:54 +090049 unsigned int dpms;
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090050 enum exynos_crtc_mode mode;
Inki Dae20cd2642013-05-21 16:55:58 +090051 wait_queue_head_t pending_flip_queue;
52 atomic_t pending_flip;
Inki Dae1c248b72011-10-04 19:19:01 +090053};
54
Inki Dae1c248b72011-10-04 19:19:01 +090055static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
56{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090057 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090058
Joonyoung Shimd2716c82011-11-04 17:04:45 +090059 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
60
Inki Daeec05da92011-12-06 11:06:54 +090061 if (exynos_crtc->dpms == mode) {
62 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
63 return;
64 }
65
Inki Dae20cd2642013-05-21 16:55:58 +090066 if (mode > DRM_MODE_DPMS_ON) {
67 /* wait for the completion of page flip. */
68 wait_event(exynos_crtc->pending_flip_queue,
69 atomic_read(&exynos_crtc->pending_flip) == 0);
70 drm_vblank_off(crtc->dev, exynos_crtc->pipe);
71 }
72
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090073 exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
74 exynos_crtc->dpms = mode;
Inki Dae1c248b72011-10-04 19:19:01 +090075}
76
77static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
78{
Inki Dae1c248b72011-10-04 19:19:01 +090079 /* drm framework doesn't check NULL. */
80}
81
82static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
83{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090084 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
85
Inki Dae50caf252012-08-20 21:29:25 +090086 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
Joonyoung Shim4070d212012-06-27 14:27:05 +090087 exynos_plane_commit(exynos_crtc->plane);
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090088 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
Inki Dae1c248b72011-10-04 19:19:01 +090089}
90
91static bool
92exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020093 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +090094 struct drm_display_mode *adjusted_mode)
95{
Inki Dae1c248b72011-10-04 19:19:01 +090096 /* drm framework doesn't check NULL */
97 return true;
98}
99
100static int
101exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
102 struct drm_display_mode *adjusted_mode, int x, int y,
103 struct drm_framebuffer *old_fb)
104{
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900105 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900106 struct drm_plane *plane = exynos_crtc->plane;
107 unsigned int crtc_w;
108 unsigned int crtc_h;
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900109 int pipe = exynos_crtc->pipe;
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900110 int ret;
111
Inki Dae1de425b2012-03-16 18:47:04 +0900112 /*
113 * copy the mode data adjusted by mode_fixup() into crtc->mode
114 * so that hardware can be seet to proper mode.
115 */
116 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
Inki Dae1c248b72011-10-04 19:19:01 +0900117
Joonyoung Shim4070d212012-06-27 14:27:05 +0900118 crtc_w = crtc->fb->width - x;
119 crtc_h = crtc->fb->height - y;
120
121 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
122 x, y, crtc_w, crtc_h);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900123 if (ret)
124 return ret;
125
Joonyoung Shim4070d212012-06-27 14:27:05 +0900126 plane->crtc = crtc;
127 plane->fb = crtc->fb;
128
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900129 exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900130
131 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900132}
133
Inki Dae7fd65df2013-05-12 16:09:33 +0900134static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
Inki Dae1c248b72011-10-04 19:19:01 +0900135 struct drm_framebuffer *old_fb)
136{
Joonyoung Shim4070d212012-06-27 14:27:05 +0900137 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
138 struct drm_plane *plane = exynos_crtc->plane;
139 unsigned int crtc_w;
140 unsigned int crtc_h;
Inki Dae1c248b72011-10-04 19:19:01 +0900141 int ret;
142
Inki Dae32aeab12012-09-14 13:29:47 +0900143 /* when framebuffer changing is requested, crtc's dpms should be on */
144 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
145 DRM_ERROR("failed framebuffer changing request.\n");
146 return -EPERM;
147 }
148
Joonyoung Shim4070d212012-06-27 14:27:05 +0900149 crtc_w = crtc->fb->width - x;
150 crtc_h = crtc->fb->height - y;
151
152 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
153 x, y, crtc_w, crtc_h);
Inki Dae1c248b72011-10-04 19:19:01 +0900154 if (ret)
155 return ret;
156
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900157 exynos_drm_crtc_commit(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900158
Joonyoung Shim4070d212012-06-27 14:27:05 +0900159 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900160}
161
Inki Dae7fd65df2013-05-12 16:09:33 +0900162static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
163 struct drm_framebuffer *old_fb)
164{
165 return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
166}
167
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900168static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
169{
170 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
171
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900172 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
173 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
174}
175
Inki Dae1c248b72011-10-04 19:19:01 +0900176static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
177 .dpms = exynos_drm_crtc_dpms,
178 .prepare = exynos_drm_crtc_prepare,
179 .commit = exynos_drm_crtc_commit,
180 .mode_fixup = exynos_drm_crtc_mode_fixup,
181 .mode_set = exynos_drm_crtc_mode_set,
182 .mode_set_base = exynos_drm_crtc_mode_set_base,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900183 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900184};
185
186static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
Keith Packarded8d1972013-07-22 18:49:58 -0700187 struct drm_framebuffer *fb,
188 struct drm_pending_vblank_event *event,
189 uint32_t page_flip_flags)
Inki Dae1c248b72011-10-04 19:19:01 +0900190{
191 struct drm_device *dev = crtc->dev;
192 struct exynos_drm_private *dev_priv = dev->dev_private;
193 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
194 struct drm_framebuffer *old_fb = crtc->fb;
195 int ret = -EINVAL;
196
Inki Daeef6223d2012-09-11 18:25:21 +0900197 /* when the page flip is requested, crtc's dpms should be on */
198 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
199 DRM_ERROR("failed page flip request.\n");
200 return -EINVAL;
201 }
202
Inki Dae1c248b72011-10-04 19:19:01 +0900203 mutex_lock(&dev->struct_mutex);
204
Inki Daeccf4d882011-10-14 13:29:51 +0900205 if (event) {
206 /*
207 * the pipe from user always is 0 so we can set pipe number
208 * of current owner to event.
209 */
210 event->pipe = exynos_crtc->pipe;
211
Inki Dae1c248b72011-10-04 19:19:01 +0900212 ret = drm_vblank_get(dev, exynos_crtc->pipe);
213 if (ret) {
214 DRM_DEBUG("failed to acquire vblank counter\n");
Inki Daeccf4d882011-10-14 13:29:51 +0900215
Inki Dae1c248b72011-10-04 19:19:01 +0900216 goto out;
217 }
218
Imre Deak85473322012-11-02 13:30:47 +0200219 spin_lock_irq(&dev->event_lock);
Inki Daec5614ae2012-02-15 11:25:20 +0900220 list_add_tail(&event->base.link,
221 &dev_priv->pageflip_event_list);
Inki Dae20cd2642013-05-21 16:55:58 +0900222 atomic_set(&exynos_crtc->pending_flip, 1);
Imre Deak85473322012-11-02 13:30:47 +0200223 spin_unlock_irq(&dev->event_lock);
Inki Daec5614ae2012-02-15 11:25:20 +0900224
Inki Dae1c248b72011-10-04 19:19:01 +0900225 crtc->fb = fb;
Inki Dae7fd65df2013-05-12 16:09:33 +0900226 ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
Joonyoung Shim4070d212012-06-27 14:27:05 +0900227 NULL);
Inki Dae1c248b72011-10-04 19:19:01 +0900228 if (ret) {
229 crtc->fb = old_fb;
Imre Deak85473322012-11-02 13:30:47 +0200230
231 spin_lock_irq(&dev->event_lock);
Inki Dae1c248b72011-10-04 19:19:01 +0900232 drm_vblank_put(dev, exynos_crtc->pipe);
Inki Daeccf4d882011-10-14 13:29:51 +0900233 list_del(&event->base.link);
Imre Deak85473322012-11-02 13:30:47 +0200234 spin_unlock_irq(&dev->event_lock);
Inki Dae1c248b72011-10-04 19:19:01 +0900235
236 goto out;
237 }
Inki Dae1c248b72011-10-04 19:19:01 +0900238 }
239out:
240 mutex_unlock(&dev->struct_mutex);
241 return ret;
242}
243
244static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
245{
246 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
247 struct exynos_drm_private *private = crtc->dev->dev_private;
248
Inki Dae1c248b72011-10-04 19:19:01 +0900249 private->crtc[exynos_crtc->pipe] = NULL;
250
251 drm_crtc_cleanup(crtc);
252 kfree(exynos_crtc);
253}
254
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900255static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
256 struct drm_property *property,
257 uint64_t val)
258{
259 struct drm_device *dev = crtc->dev;
260 struct exynos_drm_private *dev_priv = dev->dev_private;
261 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
262
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900263 if (property == dev_priv->crtc_mode_property) {
264 enum exynos_crtc_mode mode = val;
265
266 if (mode == exynos_crtc->mode)
267 return 0;
268
269 exynos_crtc->mode = mode;
270
271 switch (mode) {
272 case CRTC_MODE_NORMAL:
273 exynos_drm_crtc_commit(crtc);
274 break;
275 case CRTC_MODE_BLANK:
276 exynos_plane_dpms(exynos_crtc->plane,
277 DRM_MODE_DPMS_OFF);
278 break;
279 default:
280 break;
281 }
282
283 return 0;
284 }
285
286 return -EINVAL;
287}
288
Inki Dae1c248b72011-10-04 19:19:01 +0900289static struct drm_crtc_funcs exynos_crtc_funcs = {
290 .set_config = drm_crtc_helper_set_config,
291 .page_flip = exynos_drm_crtc_page_flip,
292 .destroy = exynos_drm_crtc_destroy,
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900293 .set_property = exynos_drm_crtc_set_property,
Inki Dae1c248b72011-10-04 19:19:01 +0900294};
295
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900296static const struct drm_prop_enum_list mode_names[] = {
297 { CRTC_MODE_NORMAL, "normal" },
298 { CRTC_MODE_BLANK, "blank" },
299};
300
301static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
302{
303 struct drm_device *dev = crtc->dev;
304 struct exynos_drm_private *dev_priv = dev->dev_private;
305 struct drm_property *prop;
306
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900307 prop = dev_priv->crtc_mode_property;
308 if (!prop) {
309 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
310 ARRAY_SIZE(mode_names));
311 if (!prop)
312 return;
313
314 dev_priv->crtc_mode_property = prop;
315 }
316
317 drm_object_attach_property(&crtc->base, prop, 0);
318}
319
Inki Dae1c248b72011-10-04 19:19:01 +0900320int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
321{
322 struct exynos_drm_crtc *exynos_crtc;
323 struct exynos_drm_private *private = dev->dev_private;
324 struct drm_crtc *crtc;
325
Inki Dae1c248b72011-10-04 19:19:01 +0900326 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
327 if (!exynos_crtc) {
328 DRM_ERROR("failed to allocate exynos crtc\n");
329 return -ENOMEM;
330 }
331
332 exynos_crtc->pipe = nr;
Inki Daeec05da92011-12-06 11:06:54 +0900333 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Inki Dae20cd2642013-05-21 16:55:58 +0900334 init_waitqueue_head(&exynos_crtc->pending_flip_queue);
335 atomic_set(&exynos_crtc->pending_flip, 0);
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900336 exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
337 if (!exynos_crtc->plane) {
338 kfree(exynos_crtc);
339 return -ENOMEM;
340 }
341
Inki Dae1c248b72011-10-04 19:19:01 +0900342 crtc = &exynos_crtc->drm_crtc;
343
344 private->crtc[nr] = crtc;
345
346 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
347 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
348
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900349 exynos_drm_crtc_attach_mode_property(crtc);
350
Inki Dae1c248b72011-10-04 19:19:01 +0900351 return 0;
352}
353
354int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
355{
356 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900357 struct exynos_drm_crtc *exynos_crtc =
358 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900359
Inki Daeec05da92011-12-06 11:06:54 +0900360 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
361 return -EPERM;
362
Inki Dae1c248b72011-10-04 19:19:01 +0900363 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
364 exynos_drm_enable_vblank);
365
366 return 0;
367}
368
369void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
370{
371 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900372 struct exynos_drm_crtc *exynos_crtc =
373 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900374
Inki Daeec05da92011-12-06 11:06:54 +0900375 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
376 return;
377
Inki Dae1c248b72011-10-04 19:19:01 +0900378 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
379 exynos_drm_disable_vblank);
380}
Rahul Sharma663d8762013-01-03 05:44:04 -0500381
382void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int crtc)
383{
384 struct exynos_drm_private *dev_priv = dev->dev_private;
385 struct drm_pending_vblank_event *e, *t;
Inki Dae20cd2642013-05-21 16:55:58 +0900386 struct drm_crtc *drm_crtc = dev_priv->crtc[crtc];
387 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
Rahul Sharma663d8762013-01-03 05:44:04 -0500388 unsigned long flags;
389
Rahul Sharma663d8762013-01-03 05:44:04 -0500390 spin_lock_irqsave(&dev->event_lock, flags);
391
392 list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
393 base.link) {
394 /* if event's pipe isn't same as crtc then ignore it. */
395 if (crtc != e->pipe)
396 continue;
397
Rob Clarkc5cca972013-05-22 11:48:40 +0900398 list_del(&e->base.link);
399 drm_send_vblank_event(dev, -1, e);
Rahul Sharma663d8762013-01-03 05:44:04 -0500400 drm_vblank_put(dev, crtc);
Inki Dae20cd2642013-05-21 16:55:58 +0900401 atomic_set(&exynos_crtc->pending_flip, 0);
402 wake_up(&exynos_crtc->pending_flip_queue);
Rahul Sharma663d8762013-01-03 05:44:04 -0500403 }
404
405 spin_unlock_irqrestore(&dev->event_lock, flags);
406}