blob: f58dd52f6d60d428cf0fe038859d9cc3f870a910 [file] [log] [blame]
Inki Daeb73d1232012-03-21 10:55:26 +09001/* exynos_drm_vidi.c
2 *
3 * Copyright (C) 2012 Samsung Electronics Co.Ltd
4 * Authors:
5 * Inki Dae <inki.dae@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drmP.h>
Inki Daeb73d1232012-03-21 10:55:26 +090014
15#include <linux/kernel.h>
Inki Daeb73d1232012-03-21 10:55:26 +090016#include <linux/platform_device.h>
17
18#include <drm/exynos_drm.h>
19
David Howells760285e2012-10-02 18:01:07 +010020#include <drm/drm_edid.h>
21#include <drm/drm_crtc_helper.h>
Inki Daeb73d1232012-03-21 10:55:26 +090022
23#include "exynos_drm_drv.h"
24#include "exynos_drm_crtc.h"
25#include "exynos_drm_encoder.h"
Mark Browne30655d2013-08-13 00:46:40 +010026#include "exynos_drm_vidi.h"
Inki Daeb73d1232012-03-21 10:55:26 +090027
28/* vidi has totally three virtual windows. */
29#define WINDOWS_NR 3
30
Sean Paulce6cb552014-01-30 16:38:07 -050031#define ctx_from_connector(c) container_of(c, struct vidi_context, \
32 connector)
Inki Daeb73d1232012-03-21 10:55:26 +090033
34struct vidi_win_data {
35 unsigned int offset_x;
36 unsigned int offset_y;
37 unsigned int ovl_width;
38 unsigned int ovl_height;
39 unsigned int fb_width;
40 unsigned int fb_height;
41 unsigned int bpp;
42 dma_addr_t dma_addr;
Inki Daeb73d1232012-03-21 10:55:26 +090043 unsigned int buf_offsize;
44 unsigned int line_size; /* bytes */
45 bool enabled;
46};
47
48struct vidi_context {
Andrzej Hajdaf01833c2014-11-17 09:54:16 +010049 struct exynos_drm_manager manager;
Andrzej Hajda73404262014-11-17 09:54:22 +010050 struct exynos_drm_display display;
Sean Paul080be03d2014-02-19 21:02:55 +090051 struct drm_device *drm_dev;
Inki Daeb73d1232012-03-21 10:55:26 +090052 struct drm_crtc *crtc;
Sean Paulce6cb552014-01-30 16:38:07 -050053 struct drm_encoder *encoder;
54 struct drm_connector connector;
Inki Daef37cd5e2014-05-09 14:25:20 +090055 struct exynos_drm_subdrv subdrv;
Inki Daeb73d1232012-03-21 10:55:26 +090056 struct vidi_win_data win_data[WINDOWS_NR];
57 struct edid *raw_edid;
58 unsigned int clkdiv;
59 unsigned int default_win;
60 unsigned long irq_flags;
61 unsigned int connected;
62 bool vblank_on;
63 bool suspended;
Inki Dae291257c2012-09-19 11:02:43 +090064 bool direct_vblank;
Inki Daeb73d1232012-03-21 10:55:26 +090065 struct work_struct work;
66 struct mutex lock;
Sean Paul080be03d2014-02-19 21:02:55 +090067 int pipe;
Inki Daeb73d1232012-03-21 10:55:26 +090068};
69
Andrzej Hajdae1819aa2014-11-17 09:54:17 +010070static inline struct vidi_context *manager_to_vidi(struct exynos_drm_manager *m)
71{
72 return container_of(m, struct vidi_context, manager);
73}
74
Inki Daeb73d1232012-03-21 10:55:26 +090075static const char fake_edid_info[] = {
76 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
77 0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
78 0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
79 0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
80 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
81 0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
82 0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
83 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
84 0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
85 0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
86 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
87 0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
88 0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
89 0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
90 0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
91 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
92 0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
93 0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x06
98};
99
Sean Paulbb7704d2014-01-30 16:19:06 -0500100static void vidi_apply(struct exynos_drm_manager *mgr)
Inki Daeb73d1232012-03-21 10:55:26 +0900101{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100102 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900103 struct exynos_drm_manager_ops *mgr_ops = mgr->ops;
Inki Daeb73d1232012-03-21 10:55:26 +0900104 struct vidi_win_data *win_data;
105 int i;
106
Inki Daeb73d1232012-03-21 10:55:26 +0900107 for (i = 0; i < WINDOWS_NR; i++) {
108 win_data = &ctx->win_data[i];
Sean Paul1c6244c2014-01-30 16:19:02 -0500109 if (win_data->enabled && (mgr_ops && mgr_ops->win_commit))
Sean Paulbb7704d2014-01-30 16:19:06 -0500110 mgr_ops->win_commit(mgr, i);
Inki Daeb73d1232012-03-21 10:55:26 +0900111 }
112
113 if (mgr_ops && mgr_ops->commit)
Sean Paulbb7704d2014-01-30 16:19:06 -0500114 mgr_ops->commit(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900115}
116
Sean Paulbb7704d2014-01-30 16:19:06 -0500117static void vidi_commit(struct exynos_drm_manager *mgr)
Inki Daeb73d1232012-03-21 10:55:26 +0900118{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100119 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900120
Inki Daeb73d1232012-03-21 10:55:26 +0900121 if (ctx->suspended)
122 return;
123}
124
Sean Paulbb7704d2014-01-30 16:19:06 -0500125static int vidi_enable_vblank(struct exynos_drm_manager *mgr)
Inki Daeb73d1232012-03-21 10:55:26 +0900126{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100127 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900128
Inki Daeb73d1232012-03-21 10:55:26 +0900129 if (ctx->suspended)
130 return -EPERM;
131
132 if (!test_and_set_bit(0, &ctx->irq_flags))
133 ctx->vblank_on = true;
134
Inki Dae291257c2012-09-19 11:02:43 +0900135 ctx->direct_vblank = true;
136
137 /*
138 * in case of page flip request, vidi_finish_pageflip function
139 * will not be called because direct_vblank is true and then
Sean Paul1c6244c2014-01-30 16:19:02 -0500140 * that function will be called by manager_ops->win_commit callback
Inki Dae291257c2012-09-19 11:02:43 +0900141 */
142 schedule_work(&ctx->work);
143
Inki Daeb73d1232012-03-21 10:55:26 +0900144 return 0;
145}
146
Sean Paulbb7704d2014-01-30 16:19:06 -0500147static void vidi_disable_vblank(struct exynos_drm_manager *mgr)
Inki Daeb73d1232012-03-21 10:55:26 +0900148{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100149 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900150
Inki Daeb73d1232012-03-21 10:55:26 +0900151 if (ctx->suspended)
152 return;
153
154 if (test_and_clear_bit(0, &ctx->irq_flags))
155 ctx->vblank_on = false;
156}
157
Sean Paulbb7704d2014-01-30 16:19:06 -0500158static void vidi_win_mode_set(struct exynos_drm_manager *mgr,
159 struct exynos_drm_overlay *overlay)
Inki Daeb73d1232012-03-21 10:55:26 +0900160{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100161 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900162 struct vidi_win_data *win_data;
163 int win;
164 unsigned long offset;
165
Inki Daeb73d1232012-03-21 10:55:26 +0900166 if (!overlay) {
Sean Paulbb7704d2014-01-30 16:19:06 -0500167 DRM_ERROR("overlay is NULL\n");
Inki Daeb73d1232012-03-21 10:55:26 +0900168 return;
169 }
170
171 win = overlay->zpos;
172 if (win == DEFAULT_ZPOS)
173 win = ctx->default_win;
174
Inki Daea7f98d62013-05-28 16:01:21 +0900175 if (win < 0 || win >= WINDOWS_NR)
Inki Daeb73d1232012-03-21 10:55:26 +0900176 return;
177
178 offset = overlay->fb_x * (overlay->bpp >> 3);
179 offset += overlay->fb_y * overlay->pitch;
180
181 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, overlay->pitch);
182
183 win_data = &ctx->win_data[win];
184
185 win_data->offset_x = overlay->crtc_x;
186 win_data->offset_y = overlay->crtc_y;
187 win_data->ovl_width = overlay->crtc_width;
188 win_data->ovl_height = overlay->crtc_height;
189 win_data->fb_width = overlay->fb_width;
190 win_data->fb_height = overlay->fb_height;
191 win_data->dma_addr = overlay->dma_addr[0] + offset;
Inki Daeb73d1232012-03-21 10:55:26 +0900192 win_data->bpp = overlay->bpp;
193 win_data->buf_offsize = (overlay->fb_width - overlay->crtc_width) *
194 (overlay->bpp >> 3);
195 win_data->line_size = overlay->crtc_width * (overlay->bpp >> 3);
196
197 /*
198 * some parts of win_data should be transferred to user side
199 * through specific ioctl.
200 */
201
202 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
203 win_data->offset_x, win_data->offset_y);
204 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
205 win_data->ovl_width, win_data->ovl_height);
YoungJun Choddd8e952012-12-10 15:44:58 +0900206 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
Inki Daeb73d1232012-03-21 10:55:26 +0900207 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
208 overlay->fb_width, overlay->crtc_width);
209}
210
Sean Paulbb7704d2014-01-30 16:19:06 -0500211static void vidi_win_commit(struct exynos_drm_manager *mgr, int zpos)
Inki Daeb73d1232012-03-21 10:55:26 +0900212{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100213 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900214 struct vidi_win_data *win_data;
215 int win = zpos;
216
Inki Daeb73d1232012-03-21 10:55:26 +0900217 if (ctx->suspended)
218 return;
219
220 if (win == DEFAULT_ZPOS)
221 win = ctx->default_win;
222
Inki Daea7f98d62013-05-28 16:01:21 +0900223 if (win < 0 || win >= WINDOWS_NR)
Inki Daeb73d1232012-03-21 10:55:26 +0900224 return;
225
226 win_data = &ctx->win_data[win];
227
228 win_data->enabled = true;
229
Jingoo Hanb8eade22014-04-22 14:45:32 +0900230 DRM_DEBUG_KMS("dma_addr = %pad\n", &win_data->dma_addr);
Inki Daeb73d1232012-03-21 10:55:26 +0900231
232 if (ctx->vblank_on)
233 schedule_work(&ctx->work);
234}
235
Sean Paulbb7704d2014-01-30 16:19:06 -0500236static void vidi_win_disable(struct exynos_drm_manager *mgr, int zpos)
Inki Daeb73d1232012-03-21 10:55:26 +0900237{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100238 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daeb73d1232012-03-21 10:55:26 +0900239 struct vidi_win_data *win_data;
240 int win = zpos;
241
Inki Daeb73d1232012-03-21 10:55:26 +0900242 if (win == DEFAULT_ZPOS)
243 win = ctx->default_win;
244
Inki Daea7f98d62013-05-28 16:01:21 +0900245 if (win < 0 || win >= WINDOWS_NR)
Inki Daeb73d1232012-03-21 10:55:26 +0900246 return;
247
248 win_data = &ctx->win_data[win];
249 win_data->enabled = false;
250
251 /* TODO. */
252}
253
Sean Paulaf65c802014-01-30 16:19:27 -0500254static int vidi_power_on(struct exynos_drm_manager *mgr, bool enable)
255{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100256 struct vidi_context *ctx = manager_to_vidi(mgr);
Sean Paulaf65c802014-01-30 16:19:27 -0500257
258 DRM_DEBUG_KMS("%s\n", __FILE__);
259
260 if (enable != false && enable != true)
261 return -EINVAL;
262
263 if (enable) {
264 ctx->suspended = false;
265
266 /* if vblank was enabled status, enable it again. */
267 if (test_and_clear_bit(0, &ctx->irq_flags))
268 vidi_enable_vblank(mgr);
269
270 vidi_apply(mgr);
271 } else {
272 ctx->suspended = true;
273 }
274
275 return 0;
276}
277
278static void vidi_dpms(struct exynos_drm_manager *mgr, int mode)
279{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100280 struct vidi_context *ctx = manager_to_vidi(mgr);
Sean Paulaf65c802014-01-30 16:19:27 -0500281
282 DRM_DEBUG_KMS("%d\n", mode);
283
284 mutex_lock(&ctx->lock);
285
286 switch (mode) {
287 case DRM_MODE_DPMS_ON:
288 vidi_power_on(mgr, true);
289 break;
290 case DRM_MODE_DPMS_STANDBY:
291 case DRM_MODE_DPMS_SUSPEND:
292 case DRM_MODE_DPMS_OFF:
293 vidi_power_on(mgr, false);
294 break;
295 default:
296 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
297 break;
298 }
299
300 mutex_unlock(&ctx->lock);
301}
302
Sean Paul080be03d2014-02-19 21:02:55 +0900303static int vidi_mgr_initialize(struct exynos_drm_manager *mgr,
Inki Daef37cd5e2014-05-09 14:25:20 +0900304 struct drm_device *drm_dev)
Sean Paul080be03d2014-02-19 21:02:55 +0900305{
Andrzej Hajdae1819aa2014-11-17 09:54:17 +0100306 struct vidi_context *ctx = manager_to_vidi(mgr);
Inki Daef37cd5e2014-05-09 14:25:20 +0900307 struct exynos_drm_private *priv = drm_dev->dev_private;
Sean Paul080be03d2014-02-19 21:02:55 +0900308
Inki Daef37cd5e2014-05-09 14:25:20 +0900309 mgr->drm_dev = ctx->drm_dev = drm_dev;
310 mgr->pipe = ctx->pipe = priv->pipe++;
Sean Paul080be03d2014-02-19 21:02:55 +0900311
Sean Paul080be03d2014-02-19 21:02:55 +0900312 return 0;
313}
314
Sean Paul1c6244c2014-01-30 16:19:02 -0500315static struct exynos_drm_manager_ops vidi_manager_ops = {
316 .dpms = vidi_dpms,
Sean Paul1c6244c2014-01-30 16:19:02 -0500317 .commit = vidi_commit,
318 .enable_vblank = vidi_enable_vblank,
319 .disable_vblank = vidi_disable_vblank,
320 .win_mode_set = vidi_win_mode_set,
321 .win_commit = vidi_win_commit,
322 .win_disable = vidi_win_disable,
Inki Daeb73d1232012-03-21 10:55:26 +0900323};
324
Inki Daeb73d1232012-03-21 10:55:26 +0900325static void vidi_fake_vblank_handler(struct work_struct *work)
326{
327 struct vidi_context *ctx = container_of(work, struct vidi_context,
328 work);
Inki Daeb73d1232012-03-21 10:55:26 +0900329
Sean Paul080be03d2014-02-19 21:02:55 +0900330 if (ctx->pipe < 0)
Inki Daeb73d1232012-03-21 10:55:26 +0900331 return;
332
333 /* refresh rate is about 50Hz. */
334 usleep_range(16000, 20000);
335
Inki Dae291257c2012-09-19 11:02:43 +0900336 mutex_lock(&ctx->lock);
337
338 if (ctx->direct_vblank) {
Sean Paul080be03d2014-02-19 21:02:55 +0900339 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
Inki Dae291257c2012-09-19 11:02:43 +0900340 ctx->direct_vblank = false;
341 mutex_unlock(&ctx->lock);
342 return;
343 }
344
345 mutex_unlock(&ctx->lock);
346
Sean Paul080be03d2014-02-19 21:02:55 +0900347 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
Inki Daeb73d1232012-03-21 10:55:26 +0900348}
349
Inki Daeb73d1232012-03-21 10:55:26 +0900350static int vidi_show_connection(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100353 struct vidi_context *ctx = dev_get_drvdata(dev);
Inki Daeb73d1232012-03-21 10:55:26 +0900354 int rc;
Inki Daeb73d1232012-03-21 10:55:26 +0900355
356 mutex_lock(&ctx->lock);
357
358 rc = sprintf(buf, "%d\n", ctx->connected);
359
360 mutex_unlock(&ctx->lock);
361
362 return rc;
363}
364
365static int vidi_store_connection(struct device *dev,
366 struct device_attribute *attr,
367 const char *buf, size_t len)
368{
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100369 struct vidi_context *ctx = dev_get_drvdata(dev);
Inki Daeb73d1232012-03-21 10:55:26 +0900370 int ret;
371
Inki Daeb73d1232012-03-21 10:55:26 +0900372 ret = kstrtoint(buf, 0, &ctx->connected);
373 if (ret)
374 return ret;
375
376 if (ctx->connected > 1)
377 return -EINVAL;
378
Inki Daed07d39d2012-06-27 16:00:56 +0900379 /* use fake edid data for test. */
380 if (!ctx->raw_edid)
381 ctx->raw_edid = (struct edid *)fake_edid_info;
382
Inki Daed7b84782012-06-27 16:16:26 +0900383 /* if raw_edid isn't same as fake data then it can't be tested. */
384 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
385 DRM_DEBUG_KMS("edid data is not fake data.\n");
386 return -EINVAL;
387 }
388
Inki Daeb73d1232012-03-21 10:55:26 +0900389 DRM_DEBUG_KMS("requested connection.\n");
390
Sean Paul080be03d2014-02-19 21:02:55 +0900391 drm_helper_hpd_irq_event(ctx->drm_dev);
Inki Daeb73d1232012-03-21 10:55:26 +0900392
393 return len;
394}
395
396static DEVICE_ATTR(connection, 0644, vidi_show_connection,
397 vidi_store_connection);
398
399int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
400 struct drm_file *file_priv)
401{
402 struct vidi_context *ctx = NULL;
403 struct drm_encoder *encoder;
Sean Paul080be03d2014-02-19 21:02:55 +0900404 struct exynos_drm_display *display;
Inki Daeb73d1232012-03-21 10:55:26 +0900405 struct drm_exynos_vidi_connection *vidi = data;
406
Inki Daeb73d1232012-03-21 10:55:26 +0900407 if (!vidi) {
408 DRM_DEBUG_KMS("user data for vidi is null.\n");
409 return -EINVAL;
410 }
411
Inki Daeb73d1232012-03-21 10:55:26 +0900412 if (vidi->connection > 1) {
413 DRM_DEBUG_KMS("connection should be 0 or 1.\n");
414 return -EINVAL;
415 }
416
417 list_for_each_entry(encoder, &drm_dev->mode_config.encoder_list,
418 head) {
Sean Paul080be03d2014-02-19 21:02:55 +0900419 display = exynos_drm_get_display(encoder);
Inki Daeb73d1232012-03-21 10:55:26 +0900420
Sean Paul080be03d2014-02-19 21:02:55 +0900421 if (display->type == EXYNOS_DISPLAY_TYPE_VIDI) {
422 ctx = display->ctx;
Inki Daeb73d1232012-03-21 10:55:26 +0900423 break;
424 }
425 }
426
427 if (!ctx) {
428 DRM_DEBUG_KMS("not found virtual device type encoder.\n");
429 return -EINVAL;
430 }
431
432 if (ctx->connected == vidi->connection) {
433 DRM_DEBUG_KMS("same connection request.\n");
434 return -EINVAL;
435 }
436
Inki Daed3b62db2012-06-27 16:36:12 +0900437 if (vidi->connection) {
Seung-Woo Kime7808df2013-01-10 19:35:06 +0900438 struct edid *raw_edid = (struct edid *)(uint32_t)vidi->edid;
439 if (!drm_edid_is_valid(raw_edid)) {
440 DRM_DEBUG_KMS("edid data is invalid.\n");
Inki Daed3b62db2012-06-27 16:36:12 +0900441 return -EINVAL;
442 }
Jani Nikula4ddc7732013-09-27 15:08:29 +0300443 ctx->raw_edid = drm_edid_duplicate(raw_edid);
Inki Daed3b62db2012-06-27 16:36:12 +0900444 if (!ctx->raw_edid) {
445 DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
446 return -ENOMEM;
447 }
Inki Daed3b62db2012-06-27 16:36:12 +0900448 } else {
449 /*
450 * with connection = 0, free raw_edid
451 * only if raw edid data isn't same as fake data.
452 */
453 if (ctx->raw_edid && ctx->raw_edid !=
454 (struct edid *)fake_edid_info) {
455 kfree(ctx->raw_edid);
456 ctx->raw_edid = NULL;
457 }
458 }
Inki Daeb73d1232012-03-21 10:55:26 +0900459
460 ctx->connected = vidi->connection;
Sean Paul080be03d2014-02-19 21:02:55 +0900461 drm_helper_hpd_irq_event(ctx->drm_dev);
Inki Daeb73d1232012-03-21 10:55:26 +0900462
463 return 0;
464}
465
Sean Paulce6cb552014-01-30 16:38:07 -0500466static enum drm_connector_status vidi_detect(struct drm_connector *connector,
467 bool force)
468{
469 struct vidi_context *ctx = ctx_from_connector(connector);
470
471 /*
472 * connection request would come from user side
473 * to do hotplug through specific ioctl.
474 */
475 return ctx->connected ? connector_status_connected :
476 connector_status_disconnected;
477}
478
479static void vidi_connector_destroy(struct drm_connector *connector)
480{
481}
482
483static struct drm_connector_funcs vidi_connector_funcs = {
484 .dpms = drm_helper_connector_dpms,
485 .fill_modes = drm_helper_probe_single_connector_modes,
486 .detect = vidi_detect,
487 .destroy = vidi_connector_destroy,
488};
489
490static int vidi_get_modes(struct drm_connector *connector)
491{
492 struct vidi_context *ctx = ctx_from_connector(connector);
493 struct edid *edid;
494 int edid_len;
495
496 /*
497 * the edid data comes from user side and it would be set
498 * to ctx->raw_edid through specific ioctl.
499 */
500 if (!ctx->raw_edid) {
501 DRM_DEBUG_KMS("raw_edid is null.\n");
502 return -EFAULT;
503 }
504
505 edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
506 edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
507 if (!edid) {
508 DRM_DEBUG_KMS("failed to allocate edid\n");
509 return -ENOMEM;
510 }
511
512 drm_mode_connector_update_edid_property(connector, edid);
513
514 return drm_add_edid_modes(connector, edid);
515}
516
Sean Paulce6cb552014-01-30 16:38:07 -0500517static struct drm_encoder *vidi_best_encoder(struct drm_connector *connector)
518{
519 struct vidi_context *ctx = ctx_from_connector(connector);
520
521 return ctx->encoder;
522}
523
524static struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
525 .get_modes = vidi_get_modes,
Sean Paulce6cb552014-01-30 16:38:07 -0500526 .best_encoder = vidi_best_encoder,
527};
528
529static int vidi_create_connector(struct exynos_drm_display *display,
530 struct drm_encoder *encoder)
531{
532 struct vidi_context *ctx = display->ctx;
533 struct drm_connector *connector = &ctx->connector;
534 int ret;
535
536 ctx->encoder = encoder;
537 connector->polled = DRM_CONNECTOR_POLL_HPD;
538
539 ret = drm_connector_init(ctx->drm_dev, connector,
540 &vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
541 if (ret) {
542 DRM_ERROR("Failed to initialize connector with drm\n");
543 return ret;
544 }
545
546 drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100547 drm_connector_register(connector);
Sean Paulce6cb552014-01-30 16:38:07 -0500548 drm_mode_connector_attach_encoder(connector, encoder);
549
550 return 0;
551}
552
553
554static struct exynos_drm_display_ops vidi_display_ops = {
555 .create_connector = vidi_create_connector,
556};
557
Inki Daef37cd5e2014-05-09 14:25:20 +0900558static int vidi_subdrv_probe(struct drm_device *drm_dev, struct device *dev)
559{
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100560 struct vidi_context *ctx = dev_get_drvdata(dev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900561 struct drm_crtc *crtc = ctx->crtc;
562 int ret;
563
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100564 vidi_mgr_initialize(&ctx->manager, drm_dev);
Inki Daef37cd5e2014-05-09 14:25:20 +0900565
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100566 ret = exynos_drm_crtc_create(&ctx->manager);
Inki Daef37cd5e2014-05-09 14:25:20 +0900567 if (ret) {
568 DRM_ERROR("failed to create crtc.\n");
569 return ret;
570 }
571
Andrzej Hajda73404262014-11-17 09:54:22 +0100572 ret = exynos_drm_create_enc_conn(drm_dev, &ctx->display);
Inki Daef37cd5e2014-05-09 14:25:20 +0900573 if (ret) {
574 crtc->funcs->destroy(crtc);
575 DRM_ERROR("failed to create encoder and connector.\n");
576 return ret;
577 }
578
579 return 0;
580}
581
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800582static int vidi_probe(struct platform_device *pdev)
Inki Daeb73d1232012-03-21 10:55:26 +0900583{
Inki Daef37cd5e2014-05-09 14:25:20 +0900584 struct exynos_drm_subdrv *subdrv;
Inki Daeb73d1232012-03-21 10:55:26 +0900585 struct vidi_context *ctx;
Inki Daeb73d1232012-03-21 10:55:26 +0900586 int ret;
587
Inki Daef37cd5e2014-05-09 14:25:20 +0900588 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
Inki Daeb73d1232012-03-21 10:55:26 +0900589 if (!ctx)
590 return -ENOMEM;
591
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100592 ctx->manager.type = EXYNOS_DISPLAY_TYPE_VIDI;
593 ctx->manager.ops = &vidi_manager_ops;
Andrzej Hajda73404262014-11-17 09:54:22 +0100594 ctx->display.type = EXYNOS_DISPLAY_TYPE_VIDI;
595 ctx->display.ops = &vidi_display_ops;
Inki Daeb73d1232012-03-21 10:55:26 +0900596 ctx->default_win = 0;
597
598 INIT_WORK(&ctx->work, vidi_fake_vblank_handler);
599
Andrzej Hajda73404262014-11-17 09:54:22 +0100600 ctx->display.ctx = ctx;
Inki Daeb73d1232012-03-21 10:55:26 +0900601
602 mutex_init(&ctx->lock);
603
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100604 platform_set_drvdata(pdev, ctx);
Inki Daeb73d1232012-03-21 10:55:26 +0900605
Inki Daef37cd5e2014-05-09 14:25:20 +0900606 subdrv = &ctx->subdrv;
607 subdrv->dev = &pdev->dev;
608 subdrv->probe = vidi_subdrv_probe;
Inki Daeb73d1232012-03-21 10:55:26 +0900609
Inki Daef37cd5e2014-05-09 14:25:20 +0900610 ret = exynos_drm_subdrv_register(subdrv);
611 if (ret < 0) {
612 dev_err(&pdev->dev, "failed to register drm vidi device\n");
613 return ret;
614 }
615
616 ret = device_create_file(&pdev->dev, &dev_attr_connection);
617 if (ret < 0) {
618 exynos_drm_subdrv_unregister(subdrv);
619 DRM_INFO("failed to create connection sysfs.\n");
620 }
Inki Daeb73d1232012-03-21 10:55:26 +0900621
622 return 0;
623}
624
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800625static int vidi_remove(struct platform_device *pdev)
Inki Daeb73d1232012-03-21 10:55:26 +0900626{
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100627 struct vidi_context *ctx = platform_get_drvdata(pdev);
Inki Daeb73d1232012-03-21 10:55:26 +0900628
Inki Daed3b62db2012-06-27 16:36:12 +0900629 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
630 kfree(ctx->raw_edid);
631 ctx->raw_edid = NULL;
Inki Daef37cd5e2014-05-09 14:25:20 +0900632
633 return -EINVAL;
Inki Daed3b62db2012-06-27 16:36:12 +0900634 }
635
Inki Daeb73d1232012-03-21 10:55:26 +0900636 return 0;
637}
638
Inki Daeb73d1232012-03-21 10:55:26 +0900639struct platform_driver vidi_driver = {
640 .probe = vidi_probe,
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800641 .remove = vidi_remove,
Inki Daeb73d1232012-03-21 10:55:26 +0900642 .driver = {
643 .name = "exynos-drm-vidi",
644 .owner = THIS_MODULE,
Inki Daeb73d1232012-03-21 10:55:26 +0900645 },
646};
Inki Daef37cd5e2014-05-09 14:25:20 +0900647
648int exynos_drm_probe_vidi(void)
649{
650 struct platform_device *pdev;
651 int ret;
652
653 pdev = platform_device_register_simple("exynos-drm-vidi", -1, NULL, 0);
654 if (IS_ERR(pdev))
655 return PTR_ERR(pdev);
656
657 ret = platform_driver_register(&vidi_driver);
658 if (ret) {
659 platform_device_unregister(pdev);
660 return ret;
661 }
662
663 return ret;
664}
665
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100666static int exynos_drm_remove_vidi_device(struct device *dev, void *data)
667{
668 platform_device_unregister(to_platform_device(dev));
669
670 return 0;
671}
672
Inki Daef37cd5e2014-05-09 14:25:20 +0900673void exynos_drm_remove_vidi(void)
674{
Andrzej Hajdaf01833c2014-11-17 09:54:16 +0100675 int ret = driver_for_each_device(&vidi_driver.driver, NULL, NULL,
676 exynos_drm_remove_vidi_device);
677 /* silence compiler warning */
678 (void)ret;
Inki Daef37cd5e2014-05-09 14:25:20 +0900679
680 platform_driver_unregister(&vidi_driver);
Inki Daef37cd5e2014-05-09 14:25:20 +0900681}