Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
13#include <drm/drmP.h>
14
15#include <linux/kernel.h>
16#include <linux/platform_device.h>
17#include <linux/component.h>
18
19#include <drm/exynos_drm.h>
20
21#include <drm/drm_edid.h>
22#include <drm/drm_crtc_helper.h>
23
24#include "exynos_drm_drv.h"
25#include "exynos_drm_crtc.h"
26#include "exynos_drm_encoder.h"
27#include "exynos_drm_vidi.h"
28
29/* vidi has totally three virtual windows. */
30#define WINDOWS_NR 3
31
32#define ctx_from_connector(c) container_of(c, struct vidi_context, \
33 connector)
34
35struct vidi_win_data {
36 unsigned int offset_x;
37 unsigned int offset_y;
38 unsigned int ovl_width;
39 unsigned int ovl_height;
40 unsigned int fb_width;
41 unsigned int fb_height;
42 unsigned int bpp;
43 dma_addr_t dma_addr;
44 unsigned int buf_offsize;
45 unsigned int line_size; /* bytes */
46 bool enabled;
47};
48
49struct vidi_context {
50 struct exynos_drm_display display;
51 struct platform_device *pdev;
52 struct drm_device *drm_dev;
53 struct exynos_drm_crtc *crtc;
54 struct drm_encoder *encoder;
55 struct drm_connector connector;
56 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;
64 bool direct_vblank;
65 struct work_struct work;
66 struct mutex lock;
67 int pipe;
68};
69
70static inline struct vidi_context *display_to_vidi(struct exynos_drm_display *d)
71{
72 return container_of(d, struct vidi_context, display);
73}
74
75static 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
100static void vidi_apply(struct vidi_context *ctx)
101{
102 struct exynos_drm_crtc_ops *crtc_ops = ctx->crtc->ops;
103 struct vidi_win_data *win_data;
104 int i;
105
106 for (i = 0; i < WINDOWS_NR; i++) {
107 win_data = &ctx->win_data[i];
108 if (win_data->enabled && (crtc_ops && crtc_ops->win_commit))
109 crtc_ops->win_commit(ctx->crtc, i);
110 }
111}
112
113static int vidi_enable_vblank(struct exynos_drm_crtc *crtc)
114{
115 struct vidi_context *ctx = crtc->ctx;
116
117 if (ctx->suspended)
118 return -EPERM;
119
120 if (!test_and_set_bit(0, &ctx->irq_flags))
121 ctx->vblank_on = true;
122
123 ctx->direct_vblank = true;
124
125 /*
126 * in case of page flip request, vidi_finish_pageflip function
127 * will not be called because direct_vblank is true and then
128 * that function will be called by crtc_ops->win_commit callback
129 */
130 schedule_work(&ctx->work);
131
132 return 0;
133}
134
135static void vidi_disable_vblank(struct exynos_drm_crtc *crtc)
136{
137 struct vidi_context *ctx = crtc->ctx;
138
139 if (ctx->suspended)
140 return;
141
142 if (test_and_clear_bit(0, &ctx->irq_flags))
143 ctx->vblank_on = false;
144}
145
146static void vidi_win_mode_set(struct exynos_drm_crtc *crtc,
147 struct exynos_drm_plane *plane)
148{
149 struct vidi_context *ctx = crtc->ctx;
150 struct vidi_win_data *win_data;
151 int win;
152 unsigned long offset;
153
154 if (!plane) {
155 DRM_ERROR("plane is NULL\n");
156 return;
157 }
158
159 win = plane->zpos;
160 if (win == DEFAULT_ZPOS)
161 win = ctx->default_win;
162
163 if (win < 0 || win >= WINDOWS_NR)
164 return;
165
166 offset = plane->fb_x * (plane->bpp >> 3);
167 offset += plane->fb_y * plane->pitch;
168
169 DRM_DEBUG_KMS("offset = 0x%lx, pitch = %x\n", offset, plane->pitch);
170
171 win_data = &ctx->win_data[win];
172
173 win_data->offset_x = plane->crtc_x;
174 win_data->offset_y = plane->crtc_y;
175 win_data->ovl_width = plane->crtc_width;
176 win_data->ovl_height = plane->crtc_height;
177 win_data->fb_width = plane->fb_width;
178 win_data->fb_height = plane->fb_height;
179 win_data->dma_addr = plane->dma_addr[0] + offset;
180 win_data->bpp = plane->bpp;
181 win_data->buf_offsize = (plane->fb_width - plane->crtc_width) *
182 (plane->bpp >> 3);
183 win_data->line_size = plane->crtc_width * (plane->bpp >> 3);
184
185 /*
186 * some parts of win_data should be transferred to user side
187 * through specific ioctl.
188 */
189
190 DRM_DEBUG_KMS("offset_x = %d, offset_y = %d\n",
191 win_data->offset_x, win_data->offset_y);
192 DRM_DEBUG_KMS("ovl_width = %d, ovl_height = %d\n",
193 win_data->ovl_width, win_data->ovl_height);
194 DRM_DEBUG_KMS("paddr = 0x%lx\n", (unsigned long)win_data->dma_addr);
195 DRM_DEBUG_KMS("fb_width = %d, crtc_width = %d\n",
196 plane->fb_width, plane->crtc_width);
197}
198
199static void vidi_win_commit(struct exynos_drm_crtc *crtc, int zpos)
200{
201 struct vidi_context *ctx = crtc->ctx;
202 struct vidi_win_data *win_data;
203 int win = zpos;
204
205 if (ctx->suspended)
206 return;
207
208 if (win == DEFAULT_ZPOS)
209 win = ctx->default_win;
210
211 if (win < 0 || win >= WINDOWS_NR)
212 return;
213
214 win_data = &ctx->win_data[win];
215
216 win_data->enabled = true;
217
218 DRM_DEBUG_KMS("dma_addr = %pad\n", &win_data->dma_addr);
219
220 if (ctx->vblank_on)
221 schedule_work(&ctx->work);
222}
223
224static void vidi_win_disable(struct exynos_drm_crtc *crtc, int zpos)
225{
226 struct vidi_context *ctx = crtc->ctx;
227 struct vidi_win_data *win_data;
228 int win = zpos;
229
230 if (win == DEFAULT_ZPOS)
231 win = ctx->default_win;
232
233 if (win < 0 || win >= WINDOWS_NR)
234 return;
235
236 win_data = &ctx->win_data[win];
237 win_data->enabled = false;
238
239 /* TODO. */
240}
241
242static int vidi_power_on(struct vidi_context *ctx, bool enable)
243{
244 DRM_DEBUG_KMS("%s\n", __FILE__);
245
246 if (enable != false && enable != true)
247 return -EINVAL;
248
249 if (enable) {
250 ctx->suspended = false;
251
252 /* if vblank was enabled status, enable it again. */
253 if (test_and_clear_bit(0, &ctx->irq_flags))
254 vidi_enable_vblank(ctx->crtc);
255
256 vidi_apply(ctx);
257 } else {
258 ctx->suspended = true;
259 }
260
261 return 0;
262}
263
264static void vidi_dpms(struct exynos_drm_crtc *crtc, int mode)
265{
266 struct vidi_context *ctx = crtc->ctx;
267
268 DRM_DEBUG_KMS("%d\n", mode);
269
270 mutex_lock(&ctx->lock);
271
272 switch (mode) {
273 case DRM_MODE_DPMS_ON:
274 vidi_power_on(ctx, true);
275 break;
276 case DRM_MODE_DPMS_STANDBY:
277 case DRM_MODE_DPMS_SUSPEND:
278 case DRM_MODE_DPMS_OFF:
279 vidi_power_on(ctx, false);
280 break;
281 default:
282 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
283 break;
284 }
285
286 mutex_unlock(&ctx->lock);
287}
288
289static int vidi_ctx_initialize(struct vidi_context *ctx,
290 struct drm_device *drm_dev)
291{
292 struct exynos_drm_private *priv = drm_dev->dev_private;
293
294 ctx->drm_dev = drm_dev;
295 ctx->pipe = priv->pipe++;
296
297 return 0;
298}
299
300static struct exynos_drm_crtc_ops vidi_crtc_ops = {
301 .dpms = vidi_dpms,
302 .enable_vblank = vidi_enable_vblank,
303 .disable_vblank = vidi_disable_vblank,
304 .win_mode_set = vidi_win_mode_set,
305 .win_commit = vidi_win_commit,
306 .win_disable = vidi_win_disable,
307};
308
309static void vidi_fake_vblank_handler(struct work_struct *work)
310{
311 struct vidi_context *ctx = container_of(work, struct vidi_context,
312 work);
313
314 if (ctx->pipe < 0)
315 return;
316
317 /* refresh rate is about 50Hz. */
318 usleep_range(16000, 20000);
319
320 mutex_lock(&ctx->lock);
321
322 if (ctx->direct_vblank) {
323 drm_handle_vblank(ctx->drm_dev, ctx->pipe);
324 ctx->direct_vblank = false;
325 mutex_unlock(&ctx->lock);
326 return;
327 }
328
329 mutex_unlock(&ctx->lock);
330
331 exynos_drm_crtc_finish_pageflip(ctx->drm_dev, ctx->pipe);
332}
333
334static int vidi_show_connection(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 struct vidi_context *ctx = dev_get_drvdata(dev);
338 int rc;
339
340 mutex_lock(&ctx->lock);
341
342 rc = sprintf(buf, "%d\n", ctx->connected);
343
344 mutex_unlock(&ctx->lock);
345
346 return rc;
347}
348
349static int vidi_store_connection(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t len)
352{
353 struct vidi_context *ctx = dev_get_drvdata(dev);
354 int ret;
355
356 ret = kstrtoint(buf, 0, &ctx->connected);
357 if (ret)
358 return ret;
359
360 if (ctx->connected > 1)
361 return -EINVAL;
362
363 /* use fake edid data for test. */
364 if (!ctx->raw_edid)
365 ctx->raw_edid = (struct edid *)fake_edid_info;
366
367 /* if raw_edid isn't same as fake data then it can't be tested. */
368 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
369 DRM_DEBUG_KMS("edid data is not fake data.\n");
370 return -EINVAL;
371 }
372
373 DRM_DEBUG_KMS("requested connection.\n");
374
375 drm_helper_hpd_irq_event(ctx->drm_dev);
376
377 return len;
378}
379
380static DEVICE_ATTR(connection, 0644, vidi_show_connection,
381 vidi_store_connection);
382
383int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
384 struct drm_file *file_priv)
385{
386 struct vidi_context *ctx = NULL;
387 struct drm_encoder *encoder;
388 struct exynos_drm_display *display;
389 struct drm_exynos_vidi_connection *vidi = data;
390
391 if (!vidi) {
392 DRM_DEBUG_KMS("user data for vidi is null.\n");
393 return -EINVAL;
394 }
395
396 if (vidi->connection > 1) {
397 DRM_DEBUG_KMS("connection should be 0 or 1.\n");
398 return -EINVAL;
399 }
400
401 list_for_each_entry(encoder, &drm_dev->mode_config.encoder_list,
402 head) {
403 display = exynos_drm_get_display(encoder);
404
405 if (display->type == EXYNOS_DISPLAY_TYPE_VIDI) {
406 ctx = display_to_vidi(display);
407 break;
408 }
409 }
410
411 if (!ctx) {
412 DRM_DEBUG_KMS("not found virtual device type encoder.\n");
413 return -EINVAL;
414 }
415
416 if (ctx->connected == vidi->connection) {
417 DRM_DEBUG_KMS("same connection request.\n");
418 return -EINVAL;
419 }
420
421 if (vidi->connection) {
422 struct edid *raw_edid = (struct edid *)(uint32_t)vidi->edid;
423 if (!drm_edid_is_valid(raw_edid)) {
424 DRM_DEBUG_KMS("edid data is invalid.\n");
425 return -EINVAL;
426 }
427 ctx->raw_edid = drm_edid_duplicate(raw_edid);
428 if (!ctx->raw_edid) {
429 DRM_DEBUG_KMS("failed to allocate raw_edid.\n");
430 return -ENOMEM;
431 }
432 } else {
433 /*
434 * with connection = 0, free raw_edid
435 * only if raw edid data isn't same as fake data.
436 */
437 if (ctx->raw_edid && ctx->raw_edid !=
438 (struct edid *)fake_edid_info) {
439 kfree(ctx->raw_edid);
440 ctx->raw_edid = NULL;
441 }
442 }
443
444 ctx->connected = vidi->connection;
445 drm_helper_hpd_irq_event(ctx->drm_dev);
446
447 return 0;
448}
449
450static enum drm_connector_status vidi_detect(struct drm_connector *connector,
451 bool force)
452{
453 struct vidi_context *ctx = ctx_from_connector(connector);
454
455 /*
456 * connection request would come from user side
457 * to do hotplug through specific ioctl.
458 */
459 return ctx->connected ? connector_status_connected :
460 connector_status_disconnected;
461}
462
463static void vidi_connector_destroy(struct drm_connector *connector)
464{
465}
466
467static struct drm_connector_funcs vidi_connector_funcs = {
468 .dpms = drm_helper_connector_dpms,
469 .fill_modes = drm_helper_probe_single_connector_modes,
470 .detect = vidi_detect,
471 .destroy = vidi_connector_destroy,
472};
473
474static int vidi_get_modes(struct drm_connector *connector)
475{
476 struct vidi_context *ctx = ctx_from_connector(connector);
477 struct edid *edid;
478 int edid_len;
479
480 /*
481 * the edid data comes from user side and it would be set
482 * to ctx->raw_edid through specific ioctl.
483 */
484 if (!ctx->raw_edid) {
485 DRM_DEBUG_KMS("raw_edid is null.\n");
486 return -EFAULT;
487 }
488
489 edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
490 edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
491 if (!edid) {
492 DRM_DEBUG_KMS("failed to allocate edid\n");
493 return -ENOMEM;
494 }
495
496 drm_mode_connector_update_edid_property(connector, edid);
497
498 return drm_add_edid_modes(connector, edid);
499}
500
501static struct drm_encoder *vidi_best_encoder(struct drm_connector *connector)
502{
503 struct vidi_context *ctx = ctx_from_connector(connector);
504
505 return ctx->encoder;
506}
507
508static struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
509 .get_modes = vidi_get_modes,
510 .best_encoder = vidi_best_encoder,
511};
512
513static int vidi_create_connector(struct exynos_drm_display *display,
514 struct drm_encoder *encoder)
515{
516 struct vidi_context *ctx = display_to_vidi(display);
517 struct drm_connector *connector = &ctx->connector;
518 int ret;
519
520 ctx->encoder = encoder;
521 connector->polled = DRM_CONNECTOR_POLL_HPD;
522
523 ret = drm_connector_init(ctx->drm_dev, connector,
524 &vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
525 if (ret) {
526 DRM_ERROR("Failed to initialize connector with drm\n");
527 return ret;
528 }
529
530 drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
531 drm_connector_register(connector);
532 drm_mode_connector_attach_encoder(connector, encoder);
533
534 return 0;
535}
536
537
538static struct exynos_drm_display_ops vidi_display_ops = {
539 .create_connector = vidi_create_connector,
540};
541
542static int vidi_bind(struct device *dev, struct device *master, void *data)
543{
544 struct vidi_context *ctx = dev_get_drvdata(dev);
545 struct drm_device *drm_dev = data;
546 int ret;
547
548 vidi_ctx_initialize(ctx, drm_dev);
549
550 ctx->crtc = exynos_drm_crtc_create(drm_dev, ctx->pipe,
551 EXYNOS_DISPLAY_TYPE_VIDI,
552 &vidi_crtc_ops, ctx);
553 if (IS_ERR(ctx->crtc)) {
554 DRM_ERROR("failed to create crtc.\n");
555 return PTR_ERR(ctx->crtc);
556 }
557
558 ret = exynos_drm_create_enc_conn(drm_dev, &ctx->display);
559 if (ret) {
560 ctx->crtc->base.funcs->destroy(&ctx->crtc->base);
561 return ret;
562 }
563
564 return 0;
565}
566
567
568static void vidi_unbind(struct device *dev, struct device *master, void *data)
569{
570}
571
572static const struct component_ops vidi_component_ops = {
573 .bind = vidi_bind,
574 .unbind = vidi_unbind,
575};
576
577static int vidi_probe(struct platform_device *pdev)
578{
579 struct vidi_context *ctx;
580 int ret;
581
582 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
583 if (!ctx)
584 return -ENOMEM;
585
586 ctx->display.type = EXYNOS_DISPLAY_TYPE_VIDI;
587 ctx->display.ops = &vidi_display_ops;
588 ctx->default_win = 0;
589 ctx->pdev = pdev;
590
591 ret = exynos_drm_component_add(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC,
592 EXYNOS_DISPLAY_TYPE_VIDI);
593 if (ret)
594 return ret;
595
596 ret = exynos_drm_component_add(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR,
597 ctx->display.type);
598 if (ret)
599 goto err_del_crtc_component;
600
601 INIT_WORK(&ctx->work, vidi_fake_vblank_handler);
602
603 mutex_init(&ctx->lock);
604
605 platform_set_drvdata(pdev, ctx);
606
607 ret = device_create_file(&pdev->dev, &dev_attr_connection);
608 if (ret < 0) {
609 DRM_ERROR("failed to create connection sysfs.\n");
610 goto err_del_conn_component;
611 }
612
613 ret = component_add(&pdev->dev, &vidi_component_ops);
614 if (ret)
615 goto err_remove_file;
616
617 return ret;
618
619err_remove_file:
620 device_remove_file(&pdev->dev, &dev_attr_connection);
621err_del_conn_component:
622 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
623err_del_crtc_component:
624 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
625
626 return ret;
627}
628
629static int vidi_remove(struct platform_device *pdev)
630{
631 struct vidi_context *ctx = platform_get_drvdata(pdev);
632
633 if (ctx->raw_edid != (struct edid *)fake_edid_info) {
634 kfree(ctx->raw_edid);
635 ctx->raw_edid = NULL;
636
637 return -EINVAL;
638 }
639
640 component_del(&pdev->dev, &vidi_component_ops);
641 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CONNECTOR);
642 exynos_drm_component_del(&pdev->dev, EXYNOS_DEVICE_TYPE_CRTC);
643
644 return 0;
645}
646
647struct platform_driver vidi_driver = {
648 .probe = vidi_probe,
649 .remove = vidi_remove,
650 .driver = {
651 .name = "exynos-drm-vidi",
652 .owner = THIS_MODULE,
653 },
654};
655
656int exynos_drm_probe_vidi(void)
657{
658 struct platform_device *pdev;
659 int ret;
660
661 pdev = platform_device_register_simple("exynos-drm-vidi", -1, NULL, 0);
662 if (IS_ERR(pdev))
663 return PTR_ERR(pdev);
664
665 ret = platform_driver_register(&vidi_driver);
666 if (ret) {
667 platform_device_unregister(pdev);
668 return ret;
669 }
670
671 return ret;
672}
673
674static int exynos_drm_remove_vidi_device(struct device *dev, void *data)
675{
676 platform_device_unregister(to_platform_device(dev));
677
678 return 0;
679}
680
681void exynos_drm_remove_vidi(void)
682{
683 int ret = driver_for_each_device(&vidi_driver.driver, NULL, NULL,
684 exynos_drm_remove_vidi_device);
685 /* silence compiler warning */
686 (void)ret;
687
688 platform_driver_unregister(&vidi_driver);
689}