]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/exynos/exynos_drm_vidi.c
drm/exynos: add render node support
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / exynos / exynos_drm_vidi.c
CommitLineData
b73d1230
ID
1/* 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 */
760285e7 13#include <drm/drmP.h>
b73d1230
ID
14
15#include <linux/kernel.h>
b73d1230 16#include <linux/platform_device.h>
1d50aa9c 17#include <linux/component.h>
b73d1230
ID
18
19#include <drm/exynos_drm.h>
20
760285e7
DH
21#include <drm/drm_edid.h>
22#include <drm/drm_crtc_helper.h>
4ea9526b 23#include <drm/drm_atomic_helper.h>
b73d1230
ID
24
25#include "exynos_drm_drv.h"
26#include "exynos_drm_crtc.h"
7ee14cdc 27#include "exynos_drm_plane.h"
e30655d0 28#include "exynos_drm_vidi.h"
b73d1230
ID
29
30/* vidi has totally three virtual windows. */
31#define WINDOWS_NR 3
32
ce6cb556
SP
33#define ctx_from_connector(c) container_of(c, struct vidi_context, \
34 connector)
b73d1230 35
b73d1230 36struct vidi_context {
2b8376c8 37 struct drm_encoder encoder;
1d50aa9c 38 struct platform_device *pdev;
080be03d 39 struct drm_device *drm_dev;
93bca243 40 struct exynos_drm_crtc *crtc;
ce6cb556 41 struct drm_connector connector;
7ee14cdc 42 struct exynos_drm_plane planes[WINDOWS_NR];
b73d1230
ID
43 struct edid *raw_edid;
44 unsigned int clkdiv;
45 unsigned int default_win;
46 unsigned long irq_flags;
47 unsigned int connected;
48 bool vblank_on;
49 bool suspended;
291257cf 50 bool direct_vblank;
b73d1230
ID
51 struct work_struct work;
52 struct mutex lock;
080be03d 53 int pipe;
b73d1230
ID
54};
55
2b8376c8 56static inline struct vidi_context *encoder_to_vidi(struct drm_encoder *e)
2f26bd72 57{
cf67cc9a 58 return container_of(e, struct vidi_context, encoder);
2f26bd72
AH
59}
60
b73d1230
ID
61static const char fake_edid_info[] = {
62 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
63 0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
64 0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
65 0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
66 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
67 0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
68 0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
69 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
70 0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
71 0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
72 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
73 0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
74 0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
75 0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
76 0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
77 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
78 0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
79 0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x06
84};
85
93bca243 86static int vidi_enable_vblank(struct exynos_drm_crtc *crtc)
b73d1230 87{
93bca243 88 struct vidi_context *ctx = crtc->ctx;
b73d1230 89
b73d1230
ID
90 if (ctx->suspended)
91 return -EPERM;
92
93 if (!test_and_set_bit(0, &ctx->irq_flags))
94 ctx->vblank_on = true;
95
291257cf
ID
96 ctx->direct_vblank = true;
97
98 /*
99 * in case of page flip request, vidi_finish_pageflip function
100 * will not be called because direct_vblank is true and then
9cc7610a 101 * that function will be called by crtc_ops->update_plane callback
291257cf
ID
102 */
103 schedule_work(&ctx->work);
104
b73d1230
ID
105 return 0;
106}
107
93bca243 108static void vidi_disable_vblank(struct exynos_drm_crtc *crtc)
b73d1230 109{
93bca243 110 struct vidi_context *ctx = crtc->ctx;
b73d1230 111
b73d1230
ID
112 if (ctx->suspended)
113 return;
114
115 if (test_and_clear_bit(0, &ctx->irq_flags))
116 ctx->vblank_on = false;
117}
118
1e1d1393
GP
119static void vidi_update_plane(struct exynos_drm_crtc *crtc,
120 struct exynos_drm_plane *plane)
b73d1230 121{
93bca243 122 struct vidi_context *ctx = crtc->ctx;
b73d1230 123
b73d1230
ID
124 if (ctx->suspended)
125 return;
126
7ee14cdc 127 DRM_DEBUG_KMS("dma_addr = %pad\n", plane->dma_addr);
b73d1230
ID
128
129 if (ctx->vblank_on)
130 schedule_work(&ctx->work);
131}
132
3cecda03 133static void vidi_enable(struct exynos_drm_crtc *crtc)
af65c804 134{
3cecda03 135 struct vidi_context *ctx = crtc->ctx;
7ee14cdc 136
3cecda03 137 mutex_lock(&ctx->lock);
af65c804 138
3cecda03 139 ctx->suspended = false;
af65c804 140
3cecda03
GP
141 /* if vblank was enabled status, enable it again. */
142 if (test_and_clear_bit(0, &ctx->irq_flags))
143 vidi_enable_vblank(ctx->crtc);
af65c804 144
3cecda03 145 mutex_unlock(&ctx->lock);
af65c804
SP
146}
147
3cecda03 148static void vidi_disable(struct exynos_drm_crtc *crtc)
af65c804 149{
93bca243 150 struct vidi_context *ctx = crtc->ctx;
af65c804
SP
151
152 mutex_lock(&ctx->lock);
153
3cecda03 154 ctx->suspended = true;
af65c804
SP
155
156 mutex_unlock(&ctx->lock);
157}
158
93bca243 159static int vidi_ctx_initialize(struct vidi_context *ctx,
f37cd5e8 160 struct drm_device *drm_dev)
080be03d 161{
f37cd5e8 162 struct exynos_drm_private *priv = drm_dev->dev_private;
080be03d 163
eb88e422 164 ctx->drm_dev = drm_dev;
8a326edd 165 ctx->pipe = priv->pipe++;
080be03d 166
080be03d
SP
167 return 0;
168}
169
f3aaf762 170static const struct exynos_drm_crtc_ops vidi_crtc_ops = {
3cecda03
GP
171 .enable = vidi_enable,
172 .disable = vidi_disable,
1c6244c3
SP
173 .enable_vblank = vidi_enable_vblank,
174 .disable_vblank = vidi_disable_vblank,
9cc7610a 175 .update_plane = vidi_update_plane,
b73d1230
ID
176};
177
b73d1230
ID
178static void vidi_fake_vblank_handler(struct work_struct *work)
179{
180 struct vidi_context *ctx = container_of(work, struct vidi_context,
181 work);
822f6dfd 182 int win;
b73d1230 183
080be03d 184 if (ctx->pipe < 0)
b73d1230
ID
185 return;
186
187 /* refresh rate is about 50Hz. */
188 usleep_range(16000, 20000);
189
291257cf
ID
190 mutex_lock(&ctx->lock);
191
192 if (ctx->direct_vblank) {
eafd540a 193 drm_crtc_handle_vblank(&ctx->crtc->base);
291257cf
ID
194 ctx->direct_vblank = false;
195 mutex_unlock(&ctx->lock);
196 return;
197 }
198
199 mutex_unlock(&ctx->lock);
200
822f6dfd
GP
201 for (win = 0 ; win < WINDOWS_NR ; win++) {
202 struct exynos_drm_plane *plane = &ctx->planes[win];
203
204 if (!plane->pending_fb)
205 continue;
206
207 exynos_drm_crtc_finish_update(ctx->crtc, plane);
208 }
b73d1230
ID
209}
210
b73d1230
ID
211static int vidi_show_connection(struct device *dev,
212 struct device_attribute *attr, char *buf)
213{
f01833cd 214 struct vidi_context *ctx = dev_get_drvdata(dev);
b73d1230 215 int rc;
b73d1230
ID
216
217 mutex_lock(&ctx->lock);
218
219 rc = sprintf(buf, "%d\n", ctx->connected);
220
221 mutex_unlock(&ctx->lock);
222
223 return rc;
224}
225
226static int vidi_store_connection(struct device *dev,
227 struct device_attribute *attr,
228 const char *buf, size_t len)
229{
f01833cd 230 struct vidi_context *ctx = dev_get_drvdata(dev);
b73d1230
ID
231 int ret;
232
b73d1230
ID
233 ret = kstrtoint(buf, 0, &ctx->connected);
234 if (ret)
235 return ret;
236
237 if (ctx->connected > 1)
238 return -EINVAL;
239
d07d39df
ID
240 /* use fake edid data for test. */
241 if (!ctx->raw_edid)
242 ctx->raw_edid = (struct edid *)fake_edid_info;
243
d7b8478a
ID
244 /* if raw_edid isn't same as fake data then it can't be tested. */
245 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
246 DRM_DEBUG_KMS("edid data is not fake data.\n");
247 return -EINVAL;
248 }
249
b73d1230
ID
250 DRM_DEBUG_KMS("requested connection.\n");
251
080be03d 252 drm_helper_hpd_irq_event(ctx->drm_dev);
b73d1230
ID
253
254 return len;
255}
256
257static DEVICE_ATTR(connection, 0644, vidi_show_connection,
258 vidi_store_connection);
259
260int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
261 struct drm_file *file_priv)
262{
cf67cc9a 263 struct vidi_context *ctx = dev_get_drvdata(drm_dev->dev);
b73d1230
ID
264 struct drm_exynos_vidi_connection *vidi = data;
265
b73d1230
ID
266 if (!vidi) {
267 DRM_DEBUG_KMS("user data for vidi is null.\n");
268 return -EINVAL;
269 }
270
b73d1230
ID
271 if (vidi->connection > 1) {
272 DRM_DEBUG_KMS("connection should be 0 or 1.\n");
273 return -EINVAL;
274 }
275
b73d1230
ID
276 if (ctx->connected == vidi->connection) {
277 DRM_DEBUG_KMS("same connection request.\n");
278 return -EINVAL;
279 }
280
d3b62dbf 281 if (vidi->connection) {
e7808df1
SWK
282 struct edid *raw_edid = (struct edid *)(uint32_t)vidi->edid;
283 if (!drm_edid_is_valid(raw_edid)) {
284 DRM_DEBUG_KMS("edid data is invalid.\n");
d3b62dbf
ID
285 return -EINVAL;
286 }
4ddc773b 287 ctx->raw_edid = drm_edid_duplicate(raw_edid);
d3b62dbf
ID
288 if (!ctx->raw_edid) {
289 DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
290 return -ENOMEM;
291 }
d3b62dbf
ID
292 } else {
293 /*
294 * with connection = 0, free raw_edid
295 * only if raw edid data isn't same as fake data.
296 */
297 if (ctx->raw_edid && ctx->raw_edid !=
298 (struct edid *)fake_edid_info) {
299 kfree(ctx->raw_edid);
300 ctx->raw_edid = NULL;
301 }
302 }
b73d1230
ID
303
304 ctx->connected = vidi->connection;
080be03d 305 drm_helper_hpd_irq_event(ctx->drm_dev);
b73d1230
ID
306
307 return 0;
308}
309
ce6cb556
SP
310static enum drm_connector_status vidi_detect(struct drm_connector *connector,
311 bool force)
312{
313 struct vidi_context *ctx = ctx_from_connector(connector);
314
315 /*
316 * connection request would come from user side
317 * to do hotplug through specific ioctl.
318 */
319 return ctx->connected ? connector_status_connected :
320 connector_status_disconnected;
321}
322
323static void vidi_connector_destroy(struct drm_connector *connector)
324{
325}
326
327static struct drm_connector_funcs vidi_connector_funcs = {
63498e30 328 .dpms = drm_atomic_helper_connector_dpms,
ce6cb556
SP
329 .fill_modes = drm_helper_probe_single_connector_modes,
330 .detect = vidi_detect,
331 .destroy = vidi_connector_destroy,
4ea9526b
GP
332 .reset = drm_atomic_helper_connector_reset,
333 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
334 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
ce6cb556
SP
335};
336
337static int vidi_get_modes(struct drm_connector *connector)
338{
339 struct vidi_context *ctx = ctx_from_connector(connector);
340 struct edid *edid;
341 int edid_len;
342
343 /*
344 * the edid data comes from user side and it would be set
345 * to ctx->raw_edid through specific ioctl.
346 */
347 if (!ctx->raw_edid) {
348 DRM_DEBUG_KMS("raw_edid is null.\n");
349 return -EFAULT;
350 }
351
352 edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
353 edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
354 if (!edid) {
355 DRM_DEBUG_KMS("failed to allocate edid\n");
356 return -ENOMEM;
357 }
358
359 drm_mode_connector_update_edid_property(connector, edid);
360
361 return drm_add_edid_modes(connector, edid);
362}
363
ce6cb556
SP
364static struct drm_encoder *vidi_best_encoder(struct drm_connector *connector)
365{
366 struct vidi_context *ctx = ctx_from_connector(connector);
367
2b8376c8 368 return &ctx->encoder;
ce6cb556
SP
369}
370
371static struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
372 .get_modes = vidi_get_modes,
ce6cb556
SP
373 .best_encoder = vidi_best_encoder,
374};
375
2b8376c8 376static int vidi_create_connector(struct drm_encoder *encoder)
ce6cb556 377{
2b8376c8 378 struct vidi_context *ctx = encoder_to_vidi(encoder);
ce6cb556
SP
379 struct drm_connector *connector = &ctx->connector;
380 int ret;
381
ce6cb556
SP
382 connector->polled = DRM_CONNECTOR_POLL_HPD;
383
384 ret = drm_connector_init(ctx->drm_dev, connector,
385 &vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
386 if (ret) {
387 DRM_ERROR("Failed to initialize connector with drm\n");
388 return ret;
389 }
390
391 drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
34ea3d38 392 drm_connector_register(connector);
ce6cb556
SP
393 drm_mode_connector_attach_encoder(connector, encoder);
394
395 return 0;
396}
397
2b8376c8
GP
398static bool exynos_vidi_mode_fixup(struct drm_encoder *encoder,
399 const struct drm_display_mode *mode,
400 struct drm_display_mode *adjusted_mode)
401{
402 return true;
403}
404
405static void exynos_vidi_mode_set(struct drm_encoder *encoder,
406 struct drm_display_mode *mode,
407 struct drm_display_mode *adjusted_mode)
408{
409}
410
411static void exynos_vidi_enable(struct drm_encoder *encoder)
412{
413}
414
415static void exynos_vidi_disable(struct drm_encoder *encoder)
416{
417}
418
419static struct drm_encoder_helper_funcs exynos_vidi_encoder_helper_funcs = {
420 .mode_fixup = exynos_vidi_mode_fixup,
421 .mode_set = exynos_vidi_mode_set,
422 .enable = exynos_vidi_enable,
423 .disable = exynos_vidi_disable,
424};
425
426static struct drm_encoder_funcs exynos_vidi_encoder_funcs = {
427 .destroy = drm_encoder_cleanup,
428};
429
1d50aa9c 430static int vidi_bind(struct device *dev, struct device *master, void *data)
f37cd5e8 431{
f01833cd 432 struct vidi_context *ctx = dev_get_drvdata(dev);
1d50aa9c 433 struct drm_device *drm_dev = data;
2b8376c8 434 struct drm_encoder *encoder = &ctx->encoder;
7ee14cdc
GP
435 struct exynos_drm_plane *exynos_plane;
436 enum drm_plane_type type;
6e2a3b66 437 unsigned int zpos;
2b8376c8 438 int pipe, ret;
f37cd5e8 439
0f04cf8d
JS
440 vidi_ctx_initialize(ctx, drm_dev);
441
7ee14cdc
GP
442 for (zpos = 0; zpos < WINDOWS_NR; zpos++) {
443 type = (zpos == ctx->default_win) ? DRM_PLANE_TYPE_PRIMARY :
444 DRM_PLANE_TYPE_OVERLAY;
445 ret = exynos_plane_init(drm_dev, &ctx->planes[zpos],
6e2a3b66 446 1 << ctx->pipe, type, zpos);
7ee14cdc
GP
447 if (ret)
448 return ret;
449 }
450
451 exynos_plane = &ctx->planes[ctx->default_win];
452 ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
453 ctx->pipe, EXYNOS_DISPLAY_TYPE_VIDI,
93bca243
GP
454 &vidi_crtc_ops, ctx);
455 if (IS_ERR(ctx->crtc)) {
f37cd5e8 456 DRM_ERROR("failed to create crtc.\n");
93bca243 457 return PTR_ERR(ctx->crtc);
f37cd5e8
ID
458 }
459
2b8376c8
GP
460 pipe = exynos_drm_crtc_get_pipe_from_type(drm_dev,
461 EXYNOS_DISPLAY_TYPE_VIDI);
462 if (pipe < 0)
463 return pipe;
464
465 encoder->possible_crtcs = 1 << pipe;
466
467 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
468
469 drm_encoder_init(drm_dev, encoder, &exynos_vidi_encoder_funcs,
470 DRM_MODE_ENCODER_TMDS);
471
472 drm_encoder_helper_add(encoder, &exynos_vidi_encoder_helper_funcs);
a2986e80 473
2b8376c8 474 ret = vidi_create_connector(encoder);
f37cd5e8 475 if (ret) {
a2986e80 476 DRM_ERROR("failed to create connector ret = %d\n", ret);
2b8376c8 477 drm_encoder_cleanup(encoder);
f37cd5e8
ID
478 return ret;
479 }
480
481 return 0;
482}
483
1d50aa9c
ID
484
485static void vidi_unbind(struct device *dev, struct device *master, void *data)
486{
487}
488
489static const struct component_ops vidi_component_ops = {
490 .bind = vidi_bind,
491 .unbind = vidi_unbind,
492};
493
56550d94 494static int vidi_probe(struct platform_device *pdev)
b73d1230 495{
b73d1230 496 struct vidi_context *ctx;
b73d1230
ID
497 int ret;
498
f37cd5e8 499 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
b73d1230
ID
500 if (!ctx)
501 return -ENOMEM;
502
503 ctx->default_win = 0;
1d50aa9c
ID
504 ctx->pdev = pdev;
505
b73d1230
ID
506 INIT_WORK(&ctx->work, vidi_fake_vblank_handler);
507
b73d1230
ID
508 mutex_init(&ctx->lock);
509
f01833cd 510 platform_set_drvdata(pdev, ctx);
b73d1230 511
f37cd5e8
ID
512 ret = device_create_file(&pdev->dev, &dev_attr_connection);
513 if (ret < 0) {
1d50aa9c 514 DRM_ERROR("failed to create connection sysfs.\n");
86650408 515 return ret;
f37cd5e8 516 }
b73d1230 517
1d50aa9c
ID
518 ret = component_add(&pdev->dev, &vidi_component_ops);
519 if (ret)
520 goto err_remove_file;
521
522 return ret;
523
524err_remove_file:
525 device_remove_file(&pdev->dev, &dev_attr_connection);
1d50aa9c
ID
526
527 return ret;
b73d1230
ID
528}
529
56550d94 530static int vidi_remove(struct platform_device *pdev)
b73d1230 531{
f01833cd 532 struct vidi_context *ctx = platform_get_drvdata(pdev);
b73d1230 533
d3b62dbf
ID
534 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
535 kfree(ctx->raw_edid);
536 ctx->raw_edid = NULL;
f37cd5e8
ID
537
538 return -EINVAL;
d3b62dbf
ID
539 }
540
1d50aa9c 541 component_del(&pdev->dev, &vidi_component_ops);
1d50aa9c 542
b73d1230
ID
543 return 0;
544}
545
b73d1230
ID
546struct platform_driver vidi_driver = {
547 .probe = vidi_probe,
56550d94 548 .remove = vidi_remove,
b73d1230
ID
549 .driver = {
550 .name = "exynos-drm-vidi",
551 .owner = THIS_MODULE,
b73d1230
ID
552 },
553};