1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3  * Authors:
4  *	Inki Dae <inki.dae@samsung.com>
5  *	Joonyoung Shim <jy0922.shim@samsung.com>
6  *	Seung-Woo Kim <sw0312.kim@samsung.com>
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13 
14 #include <linux/pm_runtime.h>
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc_helper.h>
19 
20 #include <linux/component.h>
21 
22 #include <drm/exynos_drm.h>
23 
24 #include "exynos_drm_drv.h"
25 #include "exynos_drm_fbdev.h"
26 #include "exynos_drm_fb.h"
27 #include "exynos_drm_gem.h"
28 #include "exynos_drm_plane.h"
29 #include "exynos_drm_vidi.h"
30 #include "exynos_drm_g2d.h"
31 #include "exynos_drm_ipp.h"
32 #include "exynos_drm_iommu.h"
33 
34 #define DRIVER_NAME	"exynos"
35 #define DRIVER_DESC	"Samsung SoC DRM"
36 #define DRIVER_DATE	"20110530"
37 #define DRIVER_MAJOR	1
38 #define DRIVER_MINOR	0
39 
40 static struct device *exynos_drm_get_dma_device(void);
41 
42 int exynos_atomic_check(struct drm_device *dev,
43 			struct drm_atomic_state *state)
44 {
45 	int ret;
46 
47 	ret = drm_atomic_helper_check_modeset(dev, state);
48 	if (ret)
49 		return ret;
50 
51 	ret = drm_atomic_normalize_zpos(dev, state);
52 	if (ret)
53 		return ret;
54 
55 	ret = drm_atomic_helper_check_planes(dev, state);
56 	if (ret)
57 		return ret;
58 
59 	return ret;
60 }
61 
62 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
63 {
64 	struct drm_exynos_file_private *file_priv;
65 	int ret;
66 
67 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
68 	if (!file_priv)
69 		return -ENOMEM;
70 
71 	file->driver_priv = file_priv;
72 
73 	ret = exynos_drm_subdrv_open(dev, file);
74 	if (ret)
75 		goto err_file_priv_free;
76 
77 	return ret;
78 
79 err_file_priv_free:
80 	kfree(file_priv);
81 	file->driver_priv = NULL;
82 	return ret;
83 }
84 
85 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
86 {
87 	exynos_drm_subdrv_close(dev, file);
88 	kfree(file->driver_priv);
89 	file->driver_priv = NULL;
90 }
91 
92 static void exynos_drm_lastclose(struct drm_device *dev)
93 {
94 	exynos_drm_fbdev_restore_mode(dev);
95 }
96 
97 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
98 	.fault = exynos_drm_gem_fault,
99 	.open = drm_gem_vm_open,
100 	.close = drm_gem_vm_close,
101 };
102 
103 static const struct drm_ioctl_desc exynos_ioctls[] = {
104 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
105 			DRM_AUTH | DRM_RENDER_ALLOW),
106 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP, exynos_drm_gem_map_ioctl,
107 			DRM_AUTH | DRM_RENDER_ALLOW),
108 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET, exynos_drm_gem_get_ioctl,
109 			DRM_RENDER_ALLOW),
110 	DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION, vidi_connection_ioctl,
111 			DRM_AUTH),
112 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER, exynos_g2d_get_ver_ioctl,
113 			DRM_AUTH | DRM_RENDER_ALLOW),
114 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST, exynos_g2d_set_cmdlist_ioctl,
115 			DRM_AUTH | DRM_RENDER_ALLOW),
116 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC, exynos_g2d_exec_ioctl,
117 			DRM_AUTH | DRM_RENDER_ALLOW),
118 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY, exynos_drm_ipp_get_property,
119 			DRM_AUTH | DRM_RENDER_ALLOW),
120 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY, exynos_drm_ipp_set_property,
121 			DRM_AUTH | DRM_RENDER_ALLOW),
122 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF, exynos_drm_ipp_queue_buf,
123 			DRM_AUTH | DRM_RENDER_ALLOW),
124 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL, exynos_drm_ipp_cmd_ctrl,
125 			DRM_AUTH | DRM_RENDER_ALLOW),
126 };
127 
128 static const struct file_operations exynos_drm_driver_fops = {
129 	.owner		= THIS_MODULE,
130 	.open		= drm_open,
131 	.mmap		= exynos_drm_gem_mmap,
132 	.poll		= drm_poll,
133 	.read		= drm_read,
134 	.unlocked_ioctl	= drm_ioctl,
135 	.compat_ioctl = drm_compat_ioctl,
136 	.release	= drm_release,
137 };
138 
139 static struct drm_driver exynos_drm_driver = {
140 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME
141 				  | DRIVER_ATOMIC | DRIVER_RENDER,
142 	.open			= exynos_drm_open,
143 	.lastclose		= exynos_drm_lastclose,
144 	.postclose		= exynos_drm_postclose,
145 	.gem_free_object_unlocked = exynos_drm_gem_free_object,
146 	.gem_vm_ops		= &exynos_drm_gem_vm_ops,
147 	.dumb_create		= exynos_drm_gem_dumb_create,
148 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
149 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
150 	.gem_prime_export	= drm_gem_prime_export,
151 	.gem_prime_import	= drm_gem_prime_import,
152 	.gem_prime_get_sg_table	= exynos_drm_gem_prime_get_sg_table,
153 	.gem_prime_import_sg_table	= exynos_drm_gem_prime_import_sg_table,
154 	.gem_prime_vmap		= exynos_drm_gem_prime_vmap,
155 	.gem_prime_vunmap	= exynos_drm_gem_prime_vunmap,
156 	.gem_prime_mmap		= exynos_drm_gem_prime_mmap,
157 	.ioctls			= exynos_ioctls,
158 	.num_ioctls		= ARRAY_SIZE(exynos_ioctls),
159 	.fops			= &exynos_drm_driver_fops,
160 	.name	= DRIVER_NAME,
161 	.desc	= DRIVER_DESC,
162 	.date	= DRIVER_DATE,
163 	.major	= DRIVER_MAJOR,
164 	.minor	= DRIVER_MINOR,
165 };
166 
167 #ifdef CONFIG_PM_SLEEP
168 static int exynos_drm_suspend(struct device *dev)
169 {
170 	struct drm_device *drm_dev = dev_get_drvdata(dev);
171 	struct drm_connector *connector;
172 	struct drm_connector_list_iter conn_iter;
173 
174 	if (pm_runtime_suspended(dev) || !drm_dev)
175 		return 0;
176 
177 	drm_connector_list_iter_begin(drm_dev, &conn_iter);
178 	drm_for_each_connector_iter(connector, &conn_iter) {
179 		int old_dpms = connector->dpms;
180 
181 		if (connector->funcs->dpms)
182 			connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
183 
184 		/* Set the old mode back to the connector for resume */
185 		connector->dpms = old_dpms;
186 	}
187 	drm_connector_list_iter_end(&conn_iter);
188 
189 	return 0;
190 }
191 
192 static int exynos_drm_resume(struct device *dev)
193 {
194 	struct drm_device *drm_dev = dev_get_drvdata(dev);
195 	struct drm_connector *connector;
196 	struct drm_connector_list_iter conn_iter;
197 
198 	if (pm_runtime_suspended(dev) || !drm_dev)
199 		return 0;
200 
201 	drm_connector_list_iter_begin(drm_dev, &conn_iter);
202 	drm_for_each_connector_iter(connector, &conn_iter) {
203 		if (connector->funcs->dpms) {
204 			int dpms = connector->dpms;
205 
206 			connector->dpms = DRM_MODE_DPMS_OFF;
207 			connector->funcs->dpms(connector, dpms);
208 		}
209 	}
210 	drm_connector_list_iter_end(&conn_iter);
211 
212 	return 0;
213 }
214 #endif
215 
216 static const struct dev_pm_ops exynos_drm_pm_ops = {
217 	SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
218 };
219 
220 /* forward declaration */
221 static struct platform_driver exynos_drm_platform_driver;
222 
223 struct exynos_drm_driver_info {
224 	struct platform_driver *driver;
225 	unsigned int flags;
226 };
227 
228 #define DRM_COMPONENT_DRIVER	BIT(0)	/* supports component framework */
229 #define DRM_VIRTUAL_DEVICE	BIT(1)	/* create virtual platform device */
230 #define DRM_DMA_DEVICE		BIT(2)	/* can be used for dma allocations */
231 
232 #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
233 
234 /*
235  * Connector drivers should not be placed before associated crtc drivers,
236  * because connector requires pipe number of its crtc during initialization.
237  */
238 static struct exynos_drm_driver_info exynos_drm_drivers[] = {
239 	{
240 		DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
241 		DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
242 	}, {
243 		DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
244 		DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
245 	}, {
246 		DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
247 		DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
248 	}, {
249 		DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
250 		DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
251 	}, {
252 		DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
253 		DRM_COMPONENT_DRIVER
254 	}, {
255 		DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
256 		DRM_COMPONENT_DRIVER
257 	}, {
258 		DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
259 		DRM_COMPONENT_DRIVER
260 	}, {
261 		DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
262 		DRM_COMPONENT_DRIVER
263 	}, {
264 		DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
265 		DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
266 	}, {
267 		DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
268 	}, {
269 		DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
270 	}, {
271 		DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
272 	}, {
273 		DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
274 	}, {
275 		DRV_PTR(ipp_driver, CONFIG_DRM_EXYNOS_IPP),
276 		DRM_VIRTUAL_DEVICE
277 	}, {
278 		&exynos_drm_platform_driver,
279 		DRM_VIRTUAL_DEVICE
280 	}
281 };
282 
283 static int compare_dev(struct device *dev, void *data)
284 {
285 	return dev == (struct device *)data;
286 }
287 
288 static struct component_match *exynos_drm_match_add(struct device *dev)
289 {
290 	struct component_match *match = NULL;
291 	int i;
292 
293 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
294 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
295 		struct device *p = NULL, *d;
296 
297 		if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
298 			continue;
299 
300 		while ((d = bus_find_device(&platform_bus_type, p,
301 					    &info->driver->driver,
302 					    (void *)platform_bus_type.match))) {
303 			put_device(p);
304 			component_match_add(dev, &match, compare_dev, d);
305 			p = d;
306 		}
307 		put_device(p);
308 	}
309 
310 	return match ?: ERR_PTR(-ENODEV);
311 }
312 
313 static int exynos_drm_bind(struct device *dev)
314 {
315 	struct exynos_drm_private *private;
316 	struct drm_encoder *encoder;
317 	struct drm_device *drm;
318 	unsigned int clone_mask;
319 	int cnt, ret;
320 
321 	drm = drm_dev_alloc(&exynos_drm_driver, dev);
322 	if (IS_ERR(drm))
323 		return PTR_ERR(drm);
324 
325 	private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
326 	if (!private) {
327 		ret = -ENOMEM;
328 		goto err_free_drm;
329 	}
330 
331 	init_waitqueue_head(&private->wait);
332 	spin_lock_init(&private->lock);
333 
334 	dev_set_drvdata(dev, drm);
335 	drm->dev_private = (void *)private;
336 
337 	/* the first real CRTC device is used for all dma mapping operations */
338 	private->dma_dev = exynos_drm_get_dma_device();
339 	if (!private->dma_dev) {
340 		DRM_ERROR("no device found for DMA mapping operations.\n");
341 		ret = -ENODEV;
342 		goto err_free_private;
343 	}
344 	DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
345 		 dev_name(private->dma_dev));
346 
347 	/* create common IOMMU mapping for all devices attached to Exynos DRM */
348 	ret = drm_create_iommu_mapping(drm);
349 	if (ret < 0) {
350 		DRM_ERROR("failed to create iommu mapping.\n");
351 		goto err_free_private;
352 	}
353 
354 	drm_mode_config_init(drm);
355 
356 	exynos_drm_mode_config_init(drm);
357 
358 	/* setup possible_clones. */
359 	cnt = 0;
360 	clone_mask = 0;
361 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
362 		clone_mask |= (1 << (cnt++));
363 
364 	list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
365 		encoder->possible_clones = clone_mask;
366 
367 	/* Try to bind all sub drivers. */
368 	ret = component_bind_all(drm->dev, drm);
369 	if (ret)
370 		goto err_mode_config_cleanup;
371 
372 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
373 	if (ret)
374 		goto err_unbind_all;
375 
376 	/* Probe non kms sub drivers and virtual display driver. */
377 	ret = exynos_drm_device_subdrv_probe(drm);
378 	if (ret)
379 		goto err_unbind_all;
380 
381 	drm_mode_config_reset(drm);
382 
383 	/*
384 	 * enable drm irq mode.
385 	 * - with irq_enabled = true, we can use the vblank feature.
386 	 *
387 	 * P.S. note that we wouldn't use drm irq handler but
388 	 *	just specific driver own one instead because
389 	 *	drm framework supports only one irq handler.
390 	 */
391 	drm->irq_enabled = true;
392 
393 	/* init kms poll for handling hpd */
394 	drm_kms_helper_poll_init(drm);
395 
396 	ret = exynos_drm_fbdev_init(drm);
397 	if (ret)
398 		goto err_cleanup_poll;
399 
400 	/* register the DRM device */
401 	ret = drm_dev_register(drm, 0);
402 	if (ret < 0)
403 		goto err_cleanup_fbdev;
404 
405 	return 0;
406 
407 err_cleanup_fbdev:
408 	exynos_drm_fbdev_fini(drm);
409 err_cleanup_poll:
410 	drm_kms_helper_poll_fini(drm);
411 	exynos_drm_device_subdrv_remove(drm);
412 err_unbind_all:
413 	component_unbind_all(drm->dev, drm);
414 err_mode_config_cleanup:
415 	drm_mode_config_cleanup(drm);
416 	drm_release_iommu_mapping(drm);
417 err_free_private:
418 	kfree(private);
419 err_free_drm:
420 	drm_dev_unref(drm);
421 
422 	return ret;
423 }
424 
425 static void exynos_drm_unbind(struct device *dev)
426 {
427 	struct drm_device *drm = dev_get_drvdata(dev);
428 
429 	drm_dev_unregister(drm);
430 
431 	exynos_drm_device_subdrv_remove(drm);
432 
433 	exynos_drm_fbdev_fini(drm);
434 	drm_kms_helper_poll_fini(drm);
435 
436 	component_unbind_all(drm->dev, drm);
437 	drm_mode_config_cleanup(drm);
438 	drm_release_iommu_mapping(drm);
439 
440 	kfree(drm->dev_private);
441 	drm->dev_private = NULL;
442 
443 	drm_dev_unref(drm);
444 }
445 
446 static const struct component_master_ops exynos_drm_ops = {
447 	.bind		= exynos_drm_bind,
448 	.unbind		= exynos_drm_unbind,
449 };
450 
451 static int exynos_drm_platform_probe(struct platform_device *pdev)
452 {
453 	struct component_match *match;
454 
455 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
456 
457 	match = exynos_drm_match_add(&pdev->dev);
458 	if (IS_ERR(match))
459 		return PTR_ERR(match);
460 
461 	return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
462 					       match);
463 }
464 
465 static int exynos_drm_platform_remove(struct platform_device *pdev)
466 {
467 	component_master_del(&pdev->dev, &exynos_drm_ops);
468 	return 0;
469 }
470 
471 static struct platform_driver exynos_drm_platform_driver = {
472 	.probe	= exynos_drm_platform_probe,
473 	.remove	= exynos_drm_platform_remove,
474 	.driver	= {
475 		.name	= "exynos-drm",
476 		.pm	= &exynos_drm_pm_ops,
477 	},
478 };
479 
480 static struct device *exynos_drm_get_dma_device(void)
481 {
482 	int i;
483 
484 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
485 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
486 		struct device *dev;
487 
488 		if (!info->driver || !(info->flags & DRM_DMA_DEVICE))
489 			continue;
490 
491 		while ((dev = bus_find_device(&platform_bus_type, NULL,
492 					    &info->driver->driver,
493 					    (void *)platform_bus_type.match))) {
494 			put_device(dev);
495 			return dev;
496 		}
497 	}
498 	return NULL;
499 }
500 
501 static void exynos_drm_unregister_devices(void)
502 {
503 	int i;
504 
505 	for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
506 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
507 		struct device *dev;
508 
509 		if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
510 			continue;
511 
512 		while ((dev = bus_find_device(&platform_bus_type, NULL,
513 					    &info->driver->driver,
514 					    (void *)platform_bus_type.match))) {
515 			put_device(dev);
516 			platform_device_unregister(to_platform_device(dev));
517 		}
518 	}
519 }
520 
521 static int exynos_drm_register_devices(void)
522 {
523 	struct platform_device *pdev;
524 	int i;
525 
526 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
527 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
528 
529 		if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
530 			continue;
531 
532 		pdev = platform_device_register_simple(
533 					info->driver->driver.name, -1, NULL, 0);
534 		if (IS_ERR(pdev))
535 			goto fail;
536 	}
537 
538 	return 0;
539 fail:
540 	exynos_drm_unregister_devices();
541 	return PTR_ERR(pdev);
542 }
543 
544 static void exynos_drm_unregister_drivers(void)
545 {
546 	int i;
547 
548 	for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
549 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
550 
551 		if (!info->driver)
552 			continue;
553 
554 		platform_driver_unregister(info->driver);
555 	}
556 }
557 
558 static int exynos_drm_register_drivers(void)
559 {
560 	int i, ret;
561 
562 	for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
563 		struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
564 
565 		if (!info->driver)
566 			continue;
567 
568 		ret = platform_driver_register(info->driver);
569 		if (ret)
570 			goto fail;
571 	}
572 	return 0;
573 fail:
574 	exynos_drm_unregister_drivers();
575 	return ret;
576 }
577 
578 static int exynos_drm_init(void)
579 {
580 	int ret;
581 
582 	ret = exynos_drm_register_devices();
583 	if (ret)
584 		return ret;
585 
586 	ret = exynos_drm_register_drivers();
587 	if (ret)
588 		goto err_unregister_pdevs;
589 
590 	return 0;
591 
592 err_unregister_pdevs:
593 	exynos_drm_unregister_devices();
594 
595 	return ret;
596 }
597 
598 static void exynos_drm_exit(void)
599 {
600 	exynos_drm_unregister_drivers();
601 	exynos_drm_unregister_devices();
602 }
603 
604 module_init(exynos_drm_init);
605 module_exit(exynos_drm_exit);
606 
607 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
608 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
609 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
610 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
611 MODULE_LICENSE("GPL");
612