1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* exynos_drm_vidi.c
3  *
4  * Copyright (C) 2012 Samsung Electronics Co.Ltd
5  * Authors:
6  *	Inki Dae <inki.dae@samsung.com>
7  */
8 
9 #include <linux/component.h>
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
12 #include <linux/timer.h>
13 
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_edid.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_vblank.h>
18 #include <drm/exynos_drm.h>
19 
20 #include "exynos_drm_crtc.h"
21 #include "exynos_drm_drv.h"
22 #include "exynos_drm_fb.h"
23 #include "exynos_drm_plane.h"
24 #include "exynos_drm_vidi.h"
25 
26 /* VIDI uses fixed refresh rate of 50Hz */
27 #define VIDI_REFRESH_TIME (1000 / 50)
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 
35 struct vidi_context {
36 	struct drm_encoder		encoder;
37 	struct drm_device		*drm_dev;
38 	struct device			*dev;
39 	struct exynos_drm_crtc		*crtc;
40 	struct drm_connector		connector;
41 	struct exynos_drm_plane		planes[WINDOWS_NR];
42 	struct edid			*raw_edid;
43 	unsigned int			clkdiv;
44 	unsigned int			connected;
45 	bool				suspended;
46 	struct timer_list		timer;
47 	struct mutex			lock;
48 };
49 
50 static inline struct vidi_context *encoder_to_vidi(struct drm_encoder *e)
51 {
52 	return container_of(e, struct vidi_context, encoder);
53 }
54 
55 static const char fake_edid_info[] = {
56 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x4c, 0x2d, 0x05, 0x05,
57 	0x00, 0x00, 0x00, 0x00, 0x30, 0x12, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
58 	0x0a, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, 0xbd,
59 	0xee, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
60 	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x66, 0x21, 0x50, 0xb0, 0x51, 0x00,
61 	0x1b, 0x30, 0x40, 0x70, 0x36, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e,
62 	0x01, 0x1d, 0x00, 0x72, 0x51, 0xd0, 0x1e, 0x20, 0x6e, 0x28, 0x55, 0x00,
63 	0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18,
64 	0x4b, 0x1a, 0x44, 0x17, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
65 	0x00, 0x00, 0x00, 0xfc, 0x00, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47,
66 	0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xbc, 0x02, 0x03, 0x1e, 0xf1,
67 	0x46, 0x84, 0x05, 0x03, 0x10, 0x20, 0x22, 0x23, 0x09, 0x07, 0x07, 0x83,
68 	0x01, 0x00, 0x00, 0xe2, 0x00, 0x0f, 0x67, 0x03, 0x0c, 0x00, 0x10, 0x00,
69 	0xb8, 0x2d, 0x01, 0x1d, 0x80, 0x18, 0x71, 0x1c, 0x16, 0x20, 0x58, 0x2c,
70 	0x25, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x9e, 0x8c, 0x0a, 0xd0, 0x8a,
71 	0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00, 0xa0, 0x5a, 0x00, 0x00,
72 	0x00, 0x18, 0x02, 0x3a, 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
73 	0x45, 0x00, 0xa0, 0x5a, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00,
74 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 	0x00, 0x00, 0x00, 0x06
78 };
79 
80 static const uint32_t formats[] = {
81 	DRM_FORMAT_XRGB8888,
82 	DRM_FORMAT_ARGB8888,
83 	DRM_FORMAT_NV12,
84 };
85 
86 static const enum drm_plane_type vidi_win_types[WINDOWS_NR] = {
87 	DRM_PLANE_TYPE_PRIMARY,
88 	DRM_PLANE_TYPE_OVERLAY,
89 	DRM_PLANE_TYPE_CURSOR,
90 };
91 
92 static int vidi_enable_vblank(struct exynos_drm_crtc *crtc)
93 {
94 	struct vidi_context *ctx = crtc->ctx;
95 
96 	if (ctx->suspended)
97 		return -EPERM;
98 
99 	mod_timer(&ctx->timer,
100 		jiffies + msecs_to_jiffies(VIDI_REFRESH_TIME) - 1);
101 
102 	return 0;
103 }
104 
105 static void vidi_disable_vblank(struct exynos_drm_crtc *crtc)
106 {
107 }
108 
109 static void vidi_update_plane(struct exynos_drm_crtc *crtc,
110 			      struct exynos_drm_plane *plane)
111 {
112 	struct drm_plane_state *state = plane->base.state;
113 	struct vidi_context *ctx = crtc->ctx;
114 	dma_addr_t addr;
115 
116 	if (ctx->suspended)
117 		return;
118 
119 	addr = exynos_drm_fb_dma_addr(state->fb, 0);
120 	DRM_DEV_DEBUG_KMS(ctx->dev, "dma_addr = %pad\n", &addr);
121 }
122 
123 static void vidi_enable(struct exynos_drm_crtc *crtc)
124 {
125 	struct vidi_context *ctx = crtc->ctx;
126 
127 	mutex_lock(&ctx->lock);
128 
129 	ctx->suspended = false;
130 
131 	mutex_unlock(&ctx->lock);
132 
133 	drm_crtc_vblank_on(&crtc->base);
134 }
135 
136 static void vidi_disable(struct exynos_drm_crtc *crtc)
137 {
138 	struct vidi_context *ctx = crtc->ctx;
139 
140 	drm_crtc_vblank_off(&crtc->base);
141 
142 	mutex_lock(&ctx->lock);
143 
144 	ctx->suspended = true;
145 
146 	mutex_unlock(&ctx->lock);
147 }
148 
149 static const struct exynos_drm_crtc_ops vidi_crtc_ops = {
150 	.enable = vidi_enable,
151 	.disable = vidi_disable,
152 	.enable_vblank = vidi_enable_vblank,
153 	.disable_vblank = vidi_disable_vblank,
154 	.update_plane = vidi_update_plane,
155 	.atomic_flush = exynos_crtc_handle_event,
156 };
157 
158 static void vidi_fake_vblank_timer(struct timer_list *t)
159 {
160 	struct vidi_context *ctx = from_timer(ctx, t, timer);
161 
162 	if (drm_crtc_handle_vblank(&ctx->crtc->base))
163 		mod_timer(&ctx->timer,
164 			jiffies + msecs_to_jiffies(VIDI_REFRESH_TIME) - 1);
165 }
166 
167 static ssize_t vidi_show_connection(struct device *dev,
168 				struct device_attribute *attr, char *buf)
169 {
170 	struct vidi_context *ctx = dev_get_drvdata(dev);
171 	int rc;
172 
173 	mutex_lock(&ctx->lock);
174 
175 	rc = sprintf(buf, "%d\n", ctx->connected);
176 
177 	mutex_unlock(&ctx->lock);
178 
179 	return rc;
180 }
181 
182 static ssize_t vidi_store_connection(struct device *dev,
183 				struct device_attribute *attr,
184 				const char *buf, size_t len)
185 {
186 	struct vidi_context *ctx = dev_get_drvdata(dev);
187 	int ret;
188 
189 	ret = kstrtoint(buf, 0, &ctx->connected);
190 	if (ret)
191 		return ret;
192 
193 	if (ctx->connected > 1)
194 		return -EINVAL;
195 
196 	/* use fake edid data for test. */
197 	if (!ctx->raw_edid)
198 		ctx->raw_edid = (struct edid *)fake_edid_info;
199 
200 	/* if raw_edid isn't same as fake data then it can't be tested. */
201 	if (ctx->raw_edid != (struct edid *)fake_edid_info) {
202 		DRM_DEV_DEBUG_KMS(dev, "edid data is not fake data.\n");
203 		return -EINVAL;
204 	}
205 
206 	DRM_DEV_DEBUG_KMS(dev, "requested connection.\n");
207 
208 	drm_helper_hpd_irq_event(ctx->drm_dev);
209 
210 	return len;
211 }
212 
213 static DEVICE_ATTR(connection, 0644, vidi_show_connection,
214 			vidi_store_connection);
215 
216 int vidi_connection_ioctl(struct drm_device *drm_dev, void *data,
217 				struct drm_file *file_priv)
218 {
219 	struct vidi_context *ctx = dev_get_drvdata(drm_dev->dev);
220 	struct drm_exynos_vidi_connection *vidi = data;
221 
222 	if (!vidi) {
223 		DRM_DEV_DEBUG_KMS(ctx->dev,
224 				  "user data for vidi is null.\n");
225 		return -EINVAL;
226 	}
227 
228 	if (vidi->connection > 1) {
229 		DRM_DEV_DEBUG_KMS(ctx->dev,
230 				  "connection should be 0 or 1.\n");
231 		return -EINVAL;
232 	}
233 
234 	if (ctx->connected == vidi->connection) {
235 		DRM_DEV_DEBUG_KMS(ctx->dev,
236 				  "same connection request.\n");
237 		return -EINVAL;
238 	}
239 
240 	if (vidi->connection) {
241 		struct edid *raw_edid;
242 
243 		raw_edid = (struct edid *)(unsigned long)vidi->edid;
244 		if (!drm_edid_is_valid(raw_edid)) {
245 			DRM_DEV_DEBUG_KMS(ctx->dev,
246 					  "edid data is invalid.\n");
247 			return -EINVAL;
248 		}
249 		ctx->raw_edid = drm_edid_duplicate(raw_edid);
250 		if (!ctx->raw_edid) {
251 			DRM_DEV_DEBUG_KMS(ctx->dev,
252 					  "failed to allocate raw_edid.\n");
253 			return -ENOMEM;
254 		}
255 	} else {
256 		/*
257 		 * with connection = 0, free raw_edid
258 		 * only if raw edid data isn't same as fake data.
259 		 */
260 		if (ctx->raw_edid && ctx->raw_edid !=
261 				(struct edid *)fake_edid_info) {
262 			kfree(ctx->raw_edid);
263 			ctx->raw_edid = NULL;
264 		}
265 	}
266 
267 	ctx->connected = vidi->connection;
268 	drm_helper_hpd_irq_event(ctx->drm_dev);
269 
270 	return 0;
271 }
272 
273 static enum drm_connector_status vidi_detect(struct drm_connector *connector,
274 			bool force)
275 {
276 	struct vidi_context *ctx = ctx_from_connector(connector);
277 
278 	/*
279 	 * connection request would come from user side
280 	 * to do hotplug through specific ioctl.
281 	 */
282 	return ctx->connected ? connector_status_connected :
283 			connector_status_disconnected;
284 }
285 
286 static void vidi_connector_destroy(struct drm_connector *connector)
287 {
288 }
289 
290 static const struct drm_connector_funcs vidi_connector_funcs = {
291 	.fill_modes = drm_helper_probe_single_connector_modes,
292 	.detect = vidi_detect,
293 	.destroy = vidi_connector_destroy,
294 	.reset = drm_atomic_helper_connector_reset,
295 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
296 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
297 };
298 
299 static int vidi_get_modes(struct drm_connector *connector)
300 {
301 	struct vidi_context *ctx = ctx_from_connector(connector);
302 	struct edid *edid;
303 	int edid_len;
304 
305 	/*
306 	 * the edid data comes from user side and it would be set
307 	 * to ctx->raw_edid through specific ioctl.
308 	 */
309 	if (!ctx->raw_edid) {
310 		DRM_DEV_DEBUG_KMS(ctx->dev, "raw_edid is null.\n");
311 		return -EFAULT;
312 	}
313 
314 	edid_len = (1 + ctx->raw_edid->extensions) * EDID_LENGTH;
315 	edid = kmemdup(ctx->raw_edid, edid_len, GFP_KERNEL);
316 	if (!edid) {
317 		DRM_DEV_DEBUG_KMS(ctx->dev, "failed to allocate edid\n");
318 		return -ENOMEM;
319 	}
320 
321 	drm_connector_update_edid_property(connector, edid);
322 
323 	return drm_add_edid_modes(connector, edid);
324 }
325 
326 static const struct drm_connector_helper_funcs vidi_connector_helper_funcs = {
327 	.get_modes = vidi_get_modes,
328 };
329 
330 static int vidi_create_connector(struct drm_encoder *encoder)
331 {
332 	struct vidi_context *ctx = encoder_to_vidi(encoder);
333 	struct drm_connector *connector = &ctx->connector;
334 	int ret;
335 
336 	connector->polled = DRM_CONNECTOR_POLL_HPD;
337 
338 	ret = drm_connector_init(ctx->drm_dev, connector,
339 			&vidi_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
340 	if (ret) {
341 		DRM_DEV_ERROR(ctx->dev,
342 			      "Failed to initialize connector with drm\n");
343 		return ret;
344 	}
345 
346 	drm_connector_helper_add(connector, &vidi_connector_helper_funcs);
347 	drm_connector_attach_encoder(connector, encoder);
348 
349 	return 0;
350 }
351 
352 static void exynos_vidi_mode_set(struct drm_encoder *encoder,
353 			       struct drm_display_mode *mode,
354 			       struct drm_display_mode *adjusted_mode)
355 {
356 }
357 
358 static void exynos_vidi_enable(struct drm_encoder *encoder)
359 {
360 }
361 
362 static void exynos_vidi_disable(struct drm_encoder *encoder)
363 {
364 }
365 
366 static const struct drm_encoder_helper_funcs exynos_vidi_encoder_helper_funcs = {
367 	.mode_set = exynos_vidi_mode_set,
368 	.enable = exynos_vidi_enable,
369 	.disable = exynos_vidi_disable,
370 };
371 
372 static const struct drm_encoder_funcs exynos_vidi_encoder_funcs = {
373 	.destroy = drm_encoder_cleanup,
374 };
375 
376 static int vidi_bind(struct device *dev, struct device *master, void *data)
377 {
378 	struct vidi_context *ctx = dev_get_drvdata(dev);
379 	struct drm_device *drm_dev = data;
380 	struct drm_encoder *encoder = &ctx->encoder;
381 	struct exynos_drm_plane *exynos_plane;
382 	struct exynos_drm_plane_config plane_config = { 0 };
383 	unsigned int i;
384 	int ret;
385 
386 	ctx->drm_dev = drm_dev;
387 
388 	plane_config.pixel_formats = formats;
389 	plane_config.num_pixel_formats = ARRAY_SIZE(formats);
390 
391 	for (i = 0; i < WINDOWS_NR; i++) {
392 		plane_config.zpos = i;
393 		plane_config.type = vidi_win_types[i];
394 
395 		ret = exynos_plane_init(drm_dev, &ctx->planes[i], i,
396 					&plane_config);
397 		if (ret)
398 			return ret;
399 	}
400 
401 	exynos_plane = &ctx->planes[DEFAULT_WIN];
402 	ctx->crtc = exynos_drm_crtc_create(drm_dev, &exynos_plane->base,
403 			EXYNOS_DISPLAY_TYPE_VIDI, &vidi_crtc_ops, ctx);
404 	if (IS_ERR(ctx->crtc)) {
405 		DRM_DEV_ERROR(dev, "failed to create crtc.\n");
406 		return PTR_ERR(ctx->crtc);
407 	}
408 
409 	drm_encoder_init(drm_dev, encoder, &exynos_vidi_encoder_funcs,
410 			 DRM_MODE_ENCODER_TMDS, NULL);
411 
412 	drm_encoder_helper_add(encoder, &exynos_vidi_encoder_helper_funcs);
413 
414 	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_VIDI);
415 	if (ret < 0)
416 		return ret;
417 
418 	ret = vidi_create_connector(encoder);
419 	if (ret) {
420 		DRM_DEV_ERROR(dev, "failed to create connector ret = %d\n",
421 			      ret);
422 		drm_encoder_cleanup(encoder);
423 		return ret;
424 	}
425 
426 	return 0;
427 }
428 
429 
430 static void vidi_unbind(struct device *dev, struct device *master, void *data)
431 {
432 	struct vidi_context *ctx = dev_get_drvdata(dev);
433 
434 	del_timer_sync(&ctx->timer);
435 }
436 
437 static const struct component_ops vidi_component_ops = {
438 	.bind	= vidi_bind,
439 	.unbind = vidi_unbind,
440 };
441 
442 static int vidi_probe(struct platform_device *pdev)
443 {
444 	struct vidi_context *ctx;
445 	struct device *dev = &pdev->dev;
446 	int ret;
447 
448 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
449 	if (!ctx)
450 		return -ENOMEM;
451 
452 	ctx->dev = dev;
453 
454 	timer_setup(&ctx->timer, vidi_fake_vblank_timer, 0);
455 
456 	mutex_init(&ctx->lock);
457 
458 	platform_set_drvdata(pdev, ctx);
459 
460 	ret = device_create_file(dev, &dev_attr_connection);
461 	if (ret < 0) {
462 		DRM_DEV_ERROR(dev,
463 			      "failed to create connection sysfs.\n");
464 		return ret;
465 	}
466 
467 	ret = component_add(dev, &vidi_component_ops);
468 	if (ret)
469 		goto err_remove_file;
470 
471 	return ret;
472 
473 err_remove_file:
474 	device_remove_file(dev, &dev_attr_connection);
475 
476 	return ret;
477 }
478 
479 static int vidi_remove(struct platform_device *pdev)
480 {
481 	struct vidi_context *ctx = platform_get_drvdata(pdev);
482 
483 	if (ctx->raw_edid != (struct edid *)fake_edid_info) {
484 		kfree(ctx->raw_edid);
485 		ctx->raw_edid = NULL;
486 
487 		return -EINVAL;
488 	}
489 
490 	component_del(&pdev->dev, &vidi_component_ops);
491 
492 	return 0;
493 }
494 
495 struct platform_driver vidi_driver = {
496 	.probe		= vidi_probe,
497 	.remove		= vidi_remove,
498 	.driver		= {
499 		.name	= "exynos-drm-vidi",
500 		.owner	= THIS_MODULE,
501 	},
502 };
503