blob: abb1e2f8227f3304a88b475bcc3190dec81be8c8 [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
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090039enum exynos_crtc_mode {
40 CRTC_MODE_NORMAL, /* normal mode */
41 CRTC_MODE_BLANK, /* The private plane of crtc is blank */
42};
43
Inki Dae1c248b72011-10-04 19:19:01 +090044/*
Inki Dae1c248b72011-10-04 19:19:01 +090045 * Exynos specific crtc structure.
46 *
47 * @drm_crtc: crtc object.
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090048 * @drm_plane: pointer of private plane object for this crtc
Inki Dae1c248b72011-10-04 19:19:01 +090049 * @pipe: a crtc index created at load() with a new crtc object creation
50 * and the crtc object would be set to private->crtc array
51 * to get a crtc object corresponding to this pipe from private->crtc
52 * array when irq interrupt occured. the reason of using this pipe is that
53 * drm framework doesn't support multiple irq yet.
54 * we can refer to the crtc to current hardware interrupt occured through
55 * this pipe value.
Inki Daeec05da92011-12-06 11:06:54 +090056 * @dpms: store the crtc dpms value
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090057 * @mode: store the crtc mode value
Inki Dae1c248b72011-10-04 19:19:01 +090058 */
59struct exynos_drm_crtc {
60 struct drm_crtc drm_crtc;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +090061 struct drm_plane *plane;
Inki Dae1c248b72011-10-04 19:19:01 +090062 unsigned int pipe;
Inki Daeec05da92011-12-06 11:06:54 +090063 unsigned int dpms;
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +090064 enum exynos_crtc_mode mode;
Inki Dae1c248b72011-10-04 19:19:01 +090065};
66
Inki Dae1c248b72011-10-04 19:19:01 +090067static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
68{
Inki Daeec05da92011-12-06 11:06:54 +090069 struct drm_device *dev = crtc->dev;
Joonyoung Shimd2716c82011-11-04 17:04:45 +090070 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +090071
Joonyoung Shimd2716c82011-11-04 17:04:45 +090072 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
73
Inki Daeec05da92011-12-06 11:06:54 +090074 if (exynos_crtc->dpms == mode) {
75 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
76 return;
77 }
78
79 mutex_lock(&dev->struct_mutex);
80
Joonyoung Shimcf5188a2012-06-27 14:27:09 +090081 exynos_drm_fn_encoder(crtc, &mode, exynos_drm_encoder_crtc_dpms);
82 exynos_crtc->dpms = mode;
Inki Daeec05da92011-12-06 11:06:54 +090083
84 mutex_unlock(&dev->struct_mutex);
Inki Dae1c248b72011-10-04 19:19:01 +090085}
86
87static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
88{
89 DRM_DEBUG_KMS("%s\n", __FILE__);
90
91 /* drm framework doesn't check NULL. */
92}
93
94static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
95{
Joonyoung Shimd2716c82011-11-04 17:04:45 +090096 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
97
Inki Dae1c248b72011-10-04 19:19:01 +090098 DRM_DEBUG_KMS("%s\n", __FILE__);
99
Joonyoung Shim4070d212012-06-27 14:27:05 +0900100 exynos_plane_commit(exynos_crtc->plane);
Joonyoung Shimcf5188a2012-06-27 14:27:09 +0900101 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
Inki Dae1c248b72011-10-04 19:19:01 +0900102}
103
104static bool
105exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200106 const struct drm_display_mode *mode,
Inki Dae1c248b72011-10-04 19:19:01 +0900107 struct drm_display_mode *adjusted_mode)
108{
109 DRM_DEBUG_KMS("%s\n", __FILE__);
110
111 /* drm framework doesn't check NULL */
112 return true;
113}
114
115static int
116exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
117 struct drm_display_mode *adjusted_mode, int x, int y,
118 struct drm_framebuffer *old_fb)
119{
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900120 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900121 struct drm_plane *plane = exynos_crtc->plane;
122 unsigned int crtc_w;
123 unsigned int crtc_h;
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900124 int pipe = exynos_crtc->pipe;
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900125 int ret;
126
Inki Dae1c248b72011-10-04 19:19:01 +0900127 DRM_DEBUG_KMS("%s\n", __FILE__);
128
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900129 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
130
Inki Dae1de425b2012-03-16 18:47:04 +0900131 /*
132 * copy the mode data adjusted by mode_fixup() into crtc->mode
133 * so that hardware can be seet to proper mode.
134 */
135 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
Inki Dae1c248b72011-10-04 19:19:01 +0900136
Joonyoung Shim4070d212012-06-27 14:27:05 +0900137 crtc_w = crtc->fb->width - x;
138 crtc_h = crtc->fb->height - y;
139
140 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
141 x, y, crtc_w, crtc_h);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900142 if (ret)
143 return ret;
144
Joonyoung Shim4070d212012-06-27 14:27:05 +0900145 plane->crtc = crtc;
146 plane->fb = crtc->fb;
147
Joonyoung Shimd249ce02012-06-27 14:27:02 +0900148 exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe);
Joonyoung Shimaeb29222012-06-27 14:27:01 +0900149
150 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900151}
152
153static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
154 struct drm_framebuffer *old_fb)
155{
Joonyoung Shim4070d212012-06-27 14:27:05 +0900156 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
157 struct drm_plane *plane = exynos_crtc->plane;
158 unsigned int crtc_w;
159 unsigned int crtc_h;
Inki Dae1c248b72011-10-04 19:19:01 +0900160 int ret;
161
162 DRM_DEBUG_KMS("%s\n", __FILE__);
163
Joonyoung Shim4070d212012-06-27 14:27:05 +0900164 crtc_w = crtc->fb->width - x;
165 crtc_h = crtc->fb->height - y;
166
167 ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h,
168 x, y, crtc_w, crtc_h);
Inki Dae1c248b72011-10-04 19:19:01 +0900169 if (ret)
170 return ret;
171
Joonyoung Shimbebab8f2012-06-27 14:27:07 +0900172 exynos_drm_crtc_commit(crtc);
Inki Dae1c248b72011-10-04 19:19:01 +0900173
Joonyoung Shim4070d212012-06-27 14:27:05 +0900174 return 0;
Inki Dae1c248b72011-10-04 19:19:01 +0900175}
176
177static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc)
178{
179 DRM_DEBUG_KMS("%s\n", __FILE__);
180 /* drm framework doesn't check NULL */
181}
182
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900183static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
184{
185 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
186
187 DRM_DEBUG_KMS("%s\n", __FILE__);
188
189 exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_OFF);
190 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
191}
192
Inki Dae1c248b72011-10-04 19:19:01 +0900193static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
194 .dpms = exynos_drm_crtc_dpms,
195 .prepare = exynos_drm_crtc_prepare,
196 .commit = exynos_drm_crtc_commit,
197 .mode_fixup = exynos_drm_crtc_mode_fixup,
198 .mode_set = exynos_drm_crtc_mode_set,
199 .mode_set_base = exynos_drm_crtc_mode_set_base,
200 .load_lut = exynos_drm_crtc_load_lut,
Joonyoung Shima365d9e2012-06-27 14:27:10 +0900201 .disable = exynos_drm_crtc_disable,
Inki Dae1c248b72011-10-04 19:19:01 +0900202};
203
204static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
205 struct drm_framebuffer *fb,
206 struct drm_pending_vblank_event *event)
207{
208 struct drm_device *dev = crtc->dev;
209 struct exynos_drm_private *dev_priv = dev->dev_private;
210 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
211 struct drm_framebuffer *old_fb = crtc->fb;
212 int ret = -EINVAL;
213
214 DRM_DEBUG_KMS("%s\n", __FILE__);
215
216 mutex_lock(&dev->struct_mutex);
217
Inki Daeccf4d882011-10-14 13:29:51 +0900218 if (event) {
219 /*
220 * the pipe from user always is 0 so we can set pipe number
221 * of current owner to event.
222 */
223 event->pipe = exynos_crtc->pipe;
224
Inki Dae1c248b72011-10-04 19:19:01 +0900225 ret = drm_vblank_get(dev, exynos_crtc->pipe);
226 if (ret) {
227 DRM_DEBUG("failed to acquire vblank counter\n");
Inki Daeccf4d882011-10-14 13:29:51 +0900228 list_del(&event->base.link);
229
Inki Dae1c248b72011-10-04 19:19:01 +0900230 goto out;
231 }
232
Inki Daec5614ae2012-02-15 11:25:20 +0900233 list_add_tail(&event->base.link,
234 &dev_priv->pageflip_event_list);
235
Inki Dae1c248b72011-10-04 19:19:01 +0900236 crtc->fb = fb;
Joonyoung Shim4070d212012-06-27 14:27:05 +0900237 ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y,
238 NULL);
Inki Dae1c248b72011-10-04 19:19:01 +0900239 if (ret) {
240 crtc->fb = old_fb;
241 drm_vblank_put(dev, exynos_crtc->pipe);
Inki Daeccf4d882011-10-14 13:29:51 +0900242 list_del(&event->base.link);
Inki Dae1c248b72011-10-04 19:19:01 +0900243
244 goto out;
245 }
Inki Dae1c248b72011-10-04 19:19:01 +0900246 }
247out:
248 mutex_unlock(&dev->struct_mutex);
249 return ret;
250}
251
252static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
253{
254 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
255 struct exynos_drm_private *private = crtc->dev->dev_private;
256
257 DRM_DEBUG_KMS("%s\n", __FILE__);
258
259 private->crtc[exynos_crtc->pipe] = NULL;
260
261 drm_crtc_cleanup(crtc);
262 kfree(exynos_crtc);
263}
264
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900265static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
266 struct drm_property *property,
267 uint64_t val)
268{
269 struct drm_device *dev = crtc->dev;
270 struct exynos_drm_private *dev_priv = dev->dev_private;
271 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
272
273 DRM_DEBUG_KMS("%s\n", __func__);
274
275 if (property == dev_priv->crtc_mode_property) {
276 enum exynos_crtc_mode mode = val;
277
278 if (mode == exynos_crtc->mode)
279 return 0;
280
281 exynos_crtc->mode = mode;
282
283 switch (mode) {
284 case CRTC_MODE_NORMAL:
285 exynos_drm_crtc_commit(crtc);
286 break;
287 case CRTC_MODE_BLANK:
288 exynos_plane_dpms(exynos_crtc->plane,
289 DRM_MODE_DPMS_OFF);
290 break;
291 default:
292 break;
293 }
294
295 return 0;
296 }
297
298 return -EINVAL;
299}
300
Inki Dae1c248b72011-10-04 19:19:01 +0900301static struct drm_crtc_funcs exynos_crtc_funcs = {
302 .set_config = drm_crtc_helper_set_config,
303 .page_flip = exynos_drm_crtc_page_flip,
304 .destroy = exynos_drm_crtc_destroy,
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900305 .set_property = exynos_drm_crtc_set_property,
Inki Dae1c248b72011-10-04 19:19:01 +0900306};
307
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900308static const struct drm_prop_enum_list mode_names[] = {
309 { CRTC_MODE_NORMAL, "normal" },
310 { CRTC_MODE_BLANK, "blank" },
311};
312
313static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
314{
315 struct drm_device *dev = crtc->dev;
316 struct exynos_drm_private *dev_priv = dev->dev_private;
317 struct drm_property *prop;
318
319 DRM_DEBUG_KMS("%s\n", __func__);
320
321 prop = dev_priv->crtc_mode_property;
322 if (!prop) {
323 prop = drm_property_create_enum(dev, 0, "mode", mode_names,
324 ARRAY_SIZE(mode_names));
325 if (!prop)
326 return;
327
328 dev_priv->crtc_mode_property = prop;
329 }
330
331 drm_object_attach_property(&crtc->base, prop, 0);
332}
333
Inki Dae1c248b72011-10-04 19:19:01 +0900334int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr)
335{
336 struct exynos_drm_crtc *exynos_crtc;
337 struct exynos_drm_private *private = dev->dev_private;
338 struct drm_crtc *crtc;
339
340 DRM_DEBUG_KMS("%s\n", __FILE__);
341
342 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
343 if (!exynos_crtc) {
344 DRM_ERROR("failed to allocate exynos crtc\n");
345 return -ENOMEM;
346 }
347
348 exynos_crtc->pipe = nr;
Inki Daeec05da92011-12-06 11:06:54 +0900349 exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900350 exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true);
351 if (!exynos_crtc->plane) {
352 kfree(exynos_crtc);
353 return -ENOMEM;
354 }
355
Inki Dae1c248b72011-10-04 19:19:01 +0900356 crtc = &exynos_crtc->drm_crtc;
357
358 private->crtc[nr] = crtc;
359
360 drm_crtc_init(dev, crtc, &exynos_crtc_funcs);
361 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
362
Joonyoung Shim3b8d1cf2012-06-27 14:27:11 +0900363 exynos_drm_crtc_attach_mode_property(crtc);
364
Inki Dae1c248b72011-10-04 19:19:01 +0900365 return 0;
366}
367
368int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc)
369{
370 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900371 struct exynos_drm_crtc *exynos_crtc =
372 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900373
374 DRM_DEBUG_KMS("%s\n", __FILE__);
375
Inki Daeec05da92011-12-06 11:06:54 +0900376 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
377 return -EPERM;
378
Inki Dae1c248b72011-10-04 19:19:01 +0900379 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
380 exynos_drm_enable_vblank);
381
382 return 0;
383}
384
385void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc)
386{
387 struct exynos_drm_private *private = dev->dev_private;
Inki Daeec05da92011-12-06 11:06:54 +0900388 struct exynos_drm_crtc *exynos_crtc =
389 to_exynos_crtc(private->crtc[crtc]);
Inki Dae1c248b72011-10-04 19:19:01 +0900390
391 DRM_DEBUG_KMS("%s\n", __FILE__);
392
Inki Daeec05da92011-12-06 11:06:54 +0900393 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
394 return;
395
Inki Dae1c248b72011-10-04 19:19:01 +0900396 exynos_drm_fn_encoder(private->crtc[crtc], &crtc,
397 exynos_drm_disable_vblank);
398}