blob: 047257bf9be0bd9b1b101e28e191122c5d536d5a [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
32#include "exynos_drm_drv.h"
Inki Dae1c248b72011-10-04 19:19:01 +090033#include "exynos_drm_encoder.h"
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090034#include "exynos_drm_plane.h"
Inki Dae1c248b72011-10-04 19:19:01 +090035
36#define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
37 drm_crtc)
38
39/*
Inki Dae1c248b72011-10-04 19:19:01 +090040 * Exynos specific crtc structure.
41 *
42 * @drm_crtc: crtc object.
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090043 * @drm_plane: pointer of private plane object for this crtc
Inki Dae1c248b72011-10-04 19:19:01 +090044 * @pipe: a crtc index created at load() with a new crtc object creation
45 * and the crtc object would be set to private->crtc array
46 * to get a crtc object corresponding to this pipe from private->crtc
47 * array when irq interrupt occured. the reason of using this pipe is that
48 * drm framework doesn't support multiple irq yet.
49 * we can refer to the crtc to current hardware interrupt occured through
50 * this pipe value.
Inki Daeec05da92011-12-06 11:06:54 +090051 * @dpms: store the crtc dpms value
Inki Dae1c248b72011-10-04 19:19:01 +090052 */
53struct exynos_drm_crtc {
54 struct drm_crtc drm_crtc;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090055 struct drm_plane *plane;
Inki Dae1c248b72011-10-04 19:19:01 +090056 unsigned int pipe;
Inki Daeec05da92011-12-06 11:06:54 +090057 unsigned int dpms;
Inki Dae1c248b72011-10-04 19:19:01 +090058};
59
Inki Dae1c248b72011-10-04 19:19:01 +090060static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
61{
Inki Daeec05da92011-12-06 11:06:54 +090062 struct drm_device *dev = crtc->dev;
Joonyoung Shimd2716c82011-11-04 17:04:45 +090063 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090064
Joonyoung Shimd2716c82011-11-04 17:04:45 +090065 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
66
Inki Daeec05da92011-12-06 11:06:54 +090067 if (exynos_crtc->dpms == mode) {
68 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
69 return;
70 }
71
72 mutex_lock(&dev->struct_mutex);
73
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090074 exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
75 exynos_crtc->dpms = mode;
Inki Daeec05da92011-12-06 11:06:54 +090076
77 mutex_unlock(&dev->struct_mutex);
Inki Dae1c248b72011-10-04 19:19:01 +090078}
79
80static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
81{
82 DRM_DEBUG_KMS("%s\n", __FILE__);
83
84 /* drm framework doesn't check NULL. */
85}
86
87static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
88{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090089 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
90
Inki Dae1c248b72011-10-04 19:19:01 +090091 DRM_DEBUG_KMS("%s\n", __FILE__);
92
Joonyoung Shim4070d212012-06-27 14:27:05 +090093 exynos_plane_commit(exynos_crtc->plane);
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090094 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
Inki Dae1c248b72011-10-04 19:19:01 +090095}
96
97static bool
98exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020099 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +0900100 struct drm_display_mode *adjusted_mode)
101{
102 DRM_DEBUG_KMS("%s\n", __FILE__);
103
104 /* drm framework doesn't check NULL */
105 return true;
106}
107
108static int
109exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
110 struct drm_display_mode *adjusted_mode, int x, int y,
111 struct drm_framebuffer *old_fb)
112{
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900113 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900114 struct drm_plane *plane = exynos_crtc->plane;
115 unsigned int crtc_w;
116 unsigned int crtc_h;
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900117 int pipe = exynos_crtc->pipe;
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900118 int ret;
119
Inki Dae1c248b72011-10-04 19:19:01 +0900120 DRM_DEBUG_KMS("%s\n", __FILE__);
121
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900122 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
123
Inki Dae1de425b2012-03-16 18:47:04 +0900124 /*
125 * copy the mode data adjusted by mode_fixup() into crtc->mode
126 * so that hardware can be seet to proper mode.
127 */
128 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
Inki Dae1c248b72011-10-04 19:19:01 +0900129
Joonyoung Shim4070d212012-06-27 14:27:05 +0900130 crtc_w = crtc->fb->width - x;
131 crtc_h = crtc->fb->height - y;
132
133 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
134 x, y, crtc_w, crtc_h);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900135 if (ret)
136 return ret;
137
Joonyoung Shim4070d212012-06-27 14:27:05 +0900138 plane->crtc = crtc;
139 plane->fb = crtc->fb;
140
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900141 exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900142
143 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900144}
145
146static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
147 struct drm_framebuffer *old_fb)
148{
Joonyoung Shim4070d212012-06-27 14:27:05 +0900149 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
150 struct drm_plane *plane = exynos_crtc->plane;
151 unsigned int crtc_w;
152 unsigned int crtc_h;
Inki Dae1c248b72011-10-04 19:19:01 +0900153 int ret;
154
155 DRM_DEBUG_KMS("%s\n", __FILE__);
156
Joonyoung Shim4070d212012-06-27 14:27:05 +0900157 crtc_w = crtc->fb->width - x;
158 crtc_h = crtc->fb->height - y;
159
160 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
161 x, y, crtc_w, crtc_h);
Inki Dae1c248b72011-10-04 19:19:01 +0900162 if (ret)
163 return ret;
164
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900165 exynos_drm_crtc_commit(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900166
Joonyoung Shim4070d212012-06-27 14:27:05 +0900167 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900168}
169
170static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
171{
172 DRM_DEBUG_KMS("%s\n", __FILE__);
173 /* drm framework doesn't check NULL */
174}
175
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900176static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
177{
178 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
179
180 DRM_DEBUG_KMS("%s\n", __FILE__);
181
182 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
183 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
184}
185
Inki Dae1c248b72011-10-04 19:19:01 +0900186static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
187 .dpms = exynos_drm_crtc_dpms,
188 .prepare = exynos_drm_crtc_prepare,
189 .commit = exynos_drm_crtc_commit,
190 .mode_fixup = exynos_drm_crtc_mode_fixup,
191 .mode_set = exynos_drm_crtc_mode_set,
192 .mode_set_base = exynos_drm_crtc_mode_set_base,
193 .load_lut = exynos_drm_crtc_load_lut,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900194 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900195};
196
197static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
198 struct drm_framebuffer *fb,
199 struct drm_pending_vblank_event *event)
200{
201 struct drm_device *dev = crtc->dev;
202 struct exynos_drm_private *dev_priv = dev->dev_private;
203 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
204 struct drm_framebuffer *old_fb = crtc->fb;
205 int ret = -EINVAL;
206
207 DRM_DEBUG_KMS("%s\n", __FILE__);
208
209 mutex_lock(&dev->struct_mutex);
210
Inki Daeccf4d882011-10-14 13:29:51 +0900211 if (event) {
212 /*
213 * the pipe from user always is 0 so we can set pipe number
214 * of current owner to event.
215 */
216 event->pipe = exynos_crtc->pipe;
217
Inki Dae1c248b72011-10-04 19:19:01 +0900218 ret = drm_vblank_get(dev, exynos_crtc->pipe);
219 if (ret) {
220 DRM_DEBUG("failed to acquire vblank counter\n");
Inki Daeccf4d882011-10-14 13:29:51 +0900221 list_del(&event->base.link);
222
Inki Dae1c248b72011-10-04 19:19:01 +0900223 goto out;
224 }
225
Inki Daec5614ae2012-02-15 11:25:20 +0900226 list_add_tail(&event->base.link,
227 &dev_priv->pageflip_event_list);
228
Inki Dae1c248b72011-10-04 19:19:01 +0900229 crtc->fb = fb;
Joonyoung Shim4070d212012-06-27 14:27:05 +0900230 ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
231 NULL);
Inki Dae1c248b72011-10-04 19:19:01 +0900232 if (ret) {
233 crtc->fb = old_fb;
234 drm_vblank_put(dev, exynos_crtc->pipe);
Inki Daeccf4d882011-10-14 13:29:51 +0900235 list_del(&event->base.link);
Inki Dae1c248b72011-10-04 19:19:01 +0900236
237 goto out;
238 }
Inki Dae1c248b72011-10-04 19:19:01 +0900239 }
240out:
241 mutex_unlock(&dev->struct_mutex);
242 return ret;
243}
244
245static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
246{
247 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
248 struct exynos_drm_private *private = crtc->dev->dev_private;
249
250 DRM_DEBUG_KMS("%s\n", __FILE__);
251
252 private->crtc[exynos_crtc->pipe] = NULL;
253
254 drm_crtc_cleanup(crtc);
255 kfree(exynos_crtc);
256}
257
258static struct drm_crtc_funcs exynos_crtc_funcs = {
259 .set_config = drm_crtc_helper_set_config,
260 .page_flip = exynos_drm_crtc_page_flip,
261 .destroy = exynos_drm_crtc_destroy,
262};
263
Inki Dae1c248b72011-10-04 19:19:01 +0900264int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
265{
266 struct exynos_drm_crtc *exynos_crtc;
267 struct exynos_drm_private *private = dev->dev_private;
268 struct drm_crtc *crtc;
269
270 DRM_DEBUG_KMS("%s\n", __FILE__);
271
272 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
273 if (!exynos_crtc) {
274 DRM_ERROR("failed to allocate exynos crtc\n");
275 return -ENOMEM;
276 }
277
278 exynos_crtc->pipe = nr;
Inki Daeec05da92011-12-06 11:06:54 +0900279 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900280 exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
281 if (!exynos_crtc->plane) {
282 kfree(exynos_crtc);
283 return -ENOMEM;
284 }
285
Inki Dae1c248b72011-10-04 19:19:01 +0900286 crtc = &exynos_crtc->drm_crtc;
287
288 private->crtc[nr] = crtc;
289
290 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
291 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
292
293 return 0;
294}
295
296int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
297{
298 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900299 struct exynos_drm_crtc *exynos_crtc =
300 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900301
302 DRM_DEBUG_KMS("%s\n", __FILE__);
303
Inki Daeec05da92011-12-06 11:06:54 +0900304 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
305 return -EPERM;
306
Inki Dae1c248b72011-10-04 19:19:01 +0900307 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
308 exynos_drm_enable_vblank);
309
310 return 0;
311}
312
313void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
314{
315 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900316 struct exynos_drm_crtc *exynos_crtc =
317 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900318
319 DRM_DEBUG_KMS("%s\n", __FILE__);
320
Inki Daeec05da92011-12-06 11:06:54 +0900321 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
322 return;
323
Inki Dae1c248b72011-10-04 19:19:01 +0900324 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
325 exynos_drm_disable_vblank);
326}