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_crtc_helper.h>
17 
18 #include <linux/anon_inodes.h>
19 
20 #include <drm/exynos_drm.h>
21 
22 #include "exynos_drm_drv.h"
23 #include "exynos_drm_crtc.h"
24 #include "exynos_drm_encoder.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_dmabuf.h"
31 #include "exynos_drm_g2d.h"
32 #include "exynos_drm_ipp.h"
33 #include "exynos_drm_iommu.h"
34 
35 #define DRIVER_NAME	"exynos"
36 #define DRIVER_DESC	"Samsung SoC DRM"
37 #define DRIVER_DATE	"20110530"
38 #define DRIVER_MAJOR	1
39 #define DRIVER_MINOR	0
40 
41 #define VBLANK_OFF_DELAY	50000
42 
43 /* platform device pointer for eynos drm device. */
44 static struct platform_device *exynos_drm_pdev;
45 
46 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
47 {
48 	struct exynos_drm_private *private;
49 	int ret;
50 	int nr;
51 
52 	private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
53 	if (!private)
54 		return -ENOMEM;
55 
56 	INIT_LIST_HEAD(&private->pageflip_event_list);
57 	dev_set_drvdata(dev->dev, dev);
58 	dev->dev_private = (void *)private;
59 
60 	/*
61 	 * create mapping to manage iommu table and set a pointer to iommu
62 	 * mapping structure to iommu_mapping of private data.
63 	 * also this iommu_mapping can be used to check if iommu is supported
64 	 * or not.
65 	 */
66 	ret = drm_create_iommu_mapping(dev);
67 	if (ret < 0) {
68 		DRM_ERROR("failed to create iommu mapping.\n");
69 		goto err_free_private;
70 	}
71 
72 	drm_mode_config_init(dev);
73 
74 	exynos_drm_mode_config_init(dev);
75 
76 	ret = exynos_drm_initialize_managers(dev);
77 	if (ret)
78 		goto err_mode_config_cleanup;
79 
80 	for (nr = 0; nr < MAX_PLANE; nr++) {
81 		struct drm_plane *plane;
82 		unsigned long possible_crtcs = (1 << MAX_CRTC) - 1;
83 
84 		plane = exynos_plane_init(dev, possible_crtcs, false);
85 		if (!plane)
86 			goto err_manager_cleanup;
87 	}
88 
89 	ret = exynos_drm_initialize_displays(dev);
90 	if (ret)
91 		goto err_manager_cleanup;
92 
93 	/* init kms poll for handling hpd */
94 	drm_kms_helper_poll_init(dev);
95 
96 	ret = drm_vblank_init(dev, MAX_CRTC);
97 	if (ret)
98 		goto err_display_cleanup;
99 
100 	/*
101 	 * probe sub drivers such as display controller and hdmi driver,
102 	 * that were registered at probe() of platform driver
103 	 * to the sub driver and create encoder and connector for them.
104 	 */
105 	ret = exynos_drm_device_register(dev);
106 	if (ret)
107 		goto err_vblank;
108 
109 	/* setup possible_clones. */
110 	exynos_drm_encoder_setup(dev);
111 
112 	drm_vblank_offdelay = VBLANK_OFF_DELAY;
113 
114 	platform_set_drvdata(dev->platformdev, dev);
115 
116 	/* force connectors detection */
117 	drm_helper_hpd_irq_event(dev);
118 
119 	return 0;
120 
121 err_vblank:
122 	drm_vblank_cleanup(dev);
123 err_display_cleanup:
124 	exynos_drm_remove_displays(dev);
125 err_manager_cleanup:
126 	exynos_drm_remove_managers(dev);
127 err_mode_config_cleanup:
128 	drm_mode_config_cleanup(dev);
129 	drm_release_iommu_mapping(dev);
130 err_free_private:
131 	kfree(private);
132 
133 	return ret;
134 }
135 
136 static int exynos_drm_unload(struct drm_device *dev)
137 {
138 	exynos_drm_fbdev_fini(dev);
139 	exynos_drm_device_unregister(dev);
140 	drm_vblank_cleanup(dev);
141 	drm_kms_helper_poll_fini(dev);
142 	exynos_drm_remove_displays(dev);
143 	exynos_drm_remove_managers(dev);
144 	drm_mode_config_cleanup(dev);
145 
146 	drm_release_iommu_mapping(dev);
147 	kfree(dev->dev_private);
148 
149 	dev->dev_private = NULL;
150 
151 	return 0;
152 }
153 
154 static const struct file_operations exynos_drm_gem_fops = {
155 	.mmap = exynos_drm_gem_mmap_buffer,
156 };
157 
158 static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
159 {
160 	struct drm_connector *connector;
161 
162 	drm_modeset_lock_all(dev);
163 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
164 		int old_dpms = connector->dpms;
165 
166 		if (connector->funcs->dpms)
167 			connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
168 
169 		/* Set the old mode back to the connector for resume */
170 		connector->dpms = old_dpms;
171 	}
172 	drm_modeset_unlock_all(dev);
173 
174 	return 0;
175 }
176 
177 static int exynos_drm_resume(struct drm_device *dev)
178 {
179 	struct drm_connector *connector;
180 
181 	drm_modeset_lock_all(dev);
182 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
183 		if (connector->funcs->dpms)
184 			connector->funcs->dpms(connector, connector->dpms);
185 	}
186 
187 	drm_helper_resume_force_mode(dev);
188 	drm_modeset_unlock_all(dev);
189 
190 	return 0;
191 }
192 
193 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
194 {
195 	struct drm_exynos_file_private *file_priv;
196 	struct file *anon_filp;
197 	int ret;
198 
199 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
200 	if (!file_priv)
201 		return -ENOMEM;
202 
203 	file->driver_priv = file_priv;
204 
205 	ret = exynos_drm_subdrv_open(dev, file);
206 	if (ret)
207 		goto err_file_priv_free;
208 
209 	anon_filp = anon_inode_getfile("exynos_gem", &exynos_drm_gem_fops,
210 					NULL, 0);
211 	if (IS_ERR(anon_filp)) {
212 		ret = PTR_ERR(anon_filp);
213 		goto err_subdrv_close;
214 	}
215 
216 	anon_filp->f_mode = FMODE_READ | FMODE_WRITE;
217 	file_priv->anon_filp = anon_filp;
218 
219 	return ret;
220 
221 err_subdrv_close:
222 	exynos_drm_subdrv_close(dev, file);
223 
224 err_file_priv_free:
225 	kfree(file_priv);
226 	file->driver_priv = NULL;
227 	return ret;
228 }
229 
230 static void exynos_drm_preclose(struct drm_device *dev,
231 					struct drm_file *file)
232 {
233 	exynos_drm_subdrv_close(dev, file);
234 }
235 
236 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
237 {
238 	struct exynos_drm_private *private = dev->dev_private;
239 	struct drm_exynos_file_private *file_priv;
240 	struct drm_pending_vblank_event *v, *vt;
241 	struct drm_pending_event *e, *et;
242 	unsigned long flags;
243 
244 	if (!file->driver_priv)
245 		return;
246 
247 	/* Release all events not unhandled by page flip handler. */
248 	spin_lock_irqsave(&dev->event_lock, flags);
249 	list_for_each_entry_safe(v, vt, &private->pageflip_event_list,
250 			base.link) {
251 		if (v->base.file_priv == file) {
252 			list_del(&v->base.link);
253 			drm_vblank_put(dev, v->pipe);
254 			v->base.destroy(&v->base);
255 		}
256 	}
257 
258 	/* Release all events handled by page flip handler but not freed. */
259 	list_for_each_entry_safe(e, et, &file->event_list, link) {
260 		list_del(&e->link);
261 		e->destroy(e);
262 	}
263 	spin_unlock_irqrestore(&dev->event_lock, flags);
264 
265 	file_priv = file->driver_priv;
266 	if (file_priv->anon_filp)
267 		fput(file_priv->anon_filp);
268 
269 	kfree(file->driver_priv);
270 	file->driver_priv = NULL;
271 }
272 
273 static void exynos_drm_lastclose(struct drm_device *dev)
274 {
275 	exynos_drm_fbdev_restore_mode(dev);
276 }
277 
278 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
279 	.fault = exynos_drm_gem_fault,
280 	.open = drm_gem_vm_open,
281 	.close = drm_gem_vm_close,
282 };
283 
284 static const struct drm_ioctl_desc exynos_ioctls[] = {
285 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
286 			DRM_UNLOCKED | DRM_AUTH),
287 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP_OFFSET,
288 			exynos_drm_gem_map_offset_ioctl, DRM_UNLOCKED |
289 			DRM_AUTH),
290 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MMAP,
291 			exynos_drm_gem_mmap_ioctl, DRM_UNLOCKED | DRM_AUTH),
292 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
293 			exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
294 	DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
295 			vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
296 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
297 			exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
298 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
299 			exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
300 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
301 			exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
302 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
303 			exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
304 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
305 			exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
306 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
307 			exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
308 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
309 			exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
310 };
311 
312 static const struct file_operations exynos_drm_driver_fops = {
313 	.owner		= THIS_MODULE,
314 	.open		= drm_open,
315 	.mmap		= exynos_drm_gem_mmap,
316 	.poll		= drm_poll,
317 	.read		= drm_read,
318 	.unlocked_ioctl	= drm_ioctl,
319 #ifdef CONFIG_COMPAT
320 	.compat_ioctl = drm_compat_ioctl,
321 #endif
322 	.release	= drm_release,
323 };
324 
325 static struct drm_driver exynos_drm_driver = {
326 	.driver_features	= DRIVER_HAVE_IRQ | DRIVER_MODESET |
327 					DRIVER_GEM | DRIVER_PRIME,
328 	.load			= exynos_drm_load,
329 	.unload			= exynos_drm_unload,
330 	.suspend		= exynos_drm_suspend,
331 	.resume			= exynos_drm_resume,
332 	.open			= exynos_drm_open,
333 	.preclose		= exynos_drm_preclose,
334 	.lastclose		= exynos_drm_lastclose,
335 	.postclose		= exynos_drm_postclose,
336 	.get_vblank_counter	= drm_vblank_count,
337 	.enable_vblank		= exynos_drm_crtc_enable_vblank,
338 	.disable_vblank		= exynos_drm_crtc_disable_vblank,
339 	.gem_free_object	= exynos_drm_gem_free_object,
340 	.gem_vm_ops		= &exynos_drm_gem_vm_ops,
341 	.dumb_create		= exynos_drm_gem_dumb_create,
342 	.dumb_map_offset	= exynos_drm_gem_dumb_map_offset,
343 	.dumb_destroy		= drm_gem_dumb_destroy,
344 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
345 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
346 	.gem_prime_export	= exynos_dmabuf_prime_export,
347 	.gem_prime_import	= exynos_dmabuf_prime_import,
348 	.ioctls			= exynos_ioctls,
349 	.num_ioctls		= ARRAY_SIZE(exynos_ioctls),
350 	.fops			= &exynos_drm_driver_fops,
351 	.name	= DRIVER_NAME,
352 	.desc	= DRIVER_DESC,
353 	.date	= DRIVER_DATE,
354 	.major	= DRIVER_MAJOR,
355 	.minor	= DRIVER_MINOR,
356 };
357 
358 static int exynos_drm_platform_probe(struct platform_device *pdev)
359 {
360 	int ret;
361 
362 	ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
363 	if (ret)
364 		return ret;
365 
366 	pm_runtime_enable(&pdev->dev);
367 	pm_runtime_get_sync(&pdev->dev);
368 
369 	return drm_platform_init(&exynos_drm_driver, pdev);
370 }
371 
372 static int exynos_drm_platform_remove(struct platform_device *pdev)
373 {
374 	drm_put_dev(platform_get_drvdata(pdev));
375 
376 	return 0;
377 }
378 
379 #ifdef CONFIG_PM_SLEEP
380 static int exynos_drm_sys_suspend(struct device *dev)
381 {
382 	struct drm_device *drm_dev = dev_get_drvdata(dev);
383 	pm_message_t message;
384 
385 	if (pm_runtime_suspended(dev))
386 		return 0;
387 
388 	message.event = PM_EVENT_SUSPEND;
389 	return exynos_drm_suspend(drm_dev, message);
390 }
391 
392 static int exynos_drm_sys_resume(struct device *dev)
393 {
394 	struct drm_device *drm_dev = dev_get_drvdata(dev);
395 
396 	if (pm_runtime_suspended(dev))
397 		return 0;
398 
399 	return exynos_drm_resume(drm_dev);
400 }
401 #endif
402 
403 #ifdef CONFIG_PM_RUNTIME
404 static int exynos_drm_runtime_suspend(struct device *dev)
405 {
406 	struct drm_device *drm_dev = dev_get_drvdata(dev);
407 	pm_message_t message;
408 
409 	if (pm_runtime_suspended(dev))
410 		return 0;
411 
412 	message.event = PM_EVENT_SUSPEND;
413 	return exynos_drm_suspend(drm_dev, message);
414 }
415 
416 static int exynos_drm_runtime_resume(struct device *dev)
417 {
418 	struct drm_device *drm_dev = dev_get_drvdata(dev);
419 
420 	if (!pm_runtime_suspended(dev))
421 		return 0;
422 
423 	return exynos_drm_resume(drm_dev);
424 }
425 #endif
426 
427 static const struct dev_pm_ops exynos_drm_pm_ops = {
428 	SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
429 	SET_RUNTIME_PM_OPS(exynos_drm_runtime_suspend,
430 			exynos_drm_runtime_resume, NULL)
431 };
432 
433 static struct platform_driver exynos_drm_platform_driver = {
434 	.probe		= exynos_drm_platform_probe,
435 	.remove		= exynos_drm_platform_remove,
436 	.driver		= {
437 		.owner	= THIS_MODULE,
438 		.name	= "exynos-drm",
439 		.pm	= &exynos_drm_pm_ops,
440 	},
441 };
442 
443 static int __init exynos_drm_init(void)
444 {
445 	int ret;
446 
447 #ifdef CONFIG_DRM_EXYNOS_DP
448 	ret = platform_driver_register(&dp_driver);
449 	if (ret < 0)
450 		goto out_dp;
451 #endif
452 
453 #ifdef CONFIG_DRM_EXYNOS_DSI
454 	ret = platform_driver_register(&dsi_driver);
455 	if (ret < 0)
456 		goto out_dsi;
457 #endif
458 
459 #ifdef CONFIG_DRM_EXYNOS_FIMD
460 	ret = platform_driver_register(&fimd_driver);
461 	if (ret < 0)
462 		goto out_fimd;
463 #endif
464 
465 #ifdef CONFIG_DRM_EXYNOS_HDMI
466 	ret = platform_driver_register(&hdmi_driver);
467 	if (ret < 0)
468 		goto out_hdmi;
469 	ret = platform_driver_register(&mixer_driver);
470 	if (ret < 0)
471 		goto out_mixer;
472 #endif
473 
474 #ifdef CONFIG_DRM_EXYNOS_VIDI
475 	ret = platform_driver_register(&vidi_driver);
476 	if (ret < 0)
477 		goto out_vidi;
478 #endif
479 
480 #ifdef CONFIG_DRM_EXYNOS_G2D
481 	ret = platform_driver_register(&g2d_driver);
482 	if (ret < 0)
483 		goto out_g2d;
484 #endif
485 
486 #ifdef CONFIG_DRM_EXYNOS_FIMC
487 	ret = platform_driver_register(&fimc_driver);
488 	if (ret < 0)
489 		goto out_fimc;
490 #endif
491 
492 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
493 	ret = platform_driver_register(&rotator_driver);
494 	if (ret < 0)
495 		goto out_rotator;
496 #endif
497 
498 #ifdef CONFIG_DRM_EXYNOS_GSC
499 	ret = platform_driver_register(&gsc_driver);
500 	if (ret < 0)
501 		goto out_gsc;
502 #endif
503 
504 #ifdef CONFIG_DRM_EXYNOS_IPP
505 	ret = platform_driver_register(&ipp_driver);
506 	if (ret < 0)
507 		goto out_ipp;
508 
509 	ret = exynos_platform_device_ipp_register();
510 	if (ret < 0)
511 		goto out_ipp_dev;
512 #endif
513 
514 	ret = platform_driver_register(&exynos_drm_platform_driver);
515 	if (ret < 0)
516 		goto out_drm;
517 
518 	exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
519 				NULL, 0);
520 	if (IS_ERR(exynos_drm_pdev)) {
521 		ret = PTR_ERR(exynos_drm_pdev);
522 		goto out;
523 	}
524 
525 	return 0;
526 
527 out:
528 	platform_driver_unregister(&exynos_drm_platform_driver);
529 
530 out_drm:
531 #ifdef CONFIG_DRM_EXYNOS_IPP
532 	exynos_platform_device_ipp_unregister();
533 out_ipp_dev:
534 	platform_driver_unregister(&ipp_driver);
535 out_ipp:
536 #endif
537 
538 #ifdef CONFIG_DRM_EXYNOS_GSC
539 	platform_driver_unregister(&gsc_driver);
540 out_gsc:
541 #endif
542 
543 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
544 	platform_driver_unregister(&rotator_driver);
545 out_rotator:
546 #endif
547 
548 #ifdef CONFIG_DRM_EXYNOS_FIMC
549 	platform_driver_unregister(&fimc_driver);
550 out_fimc:
551 #endif
552 
553 #ifdef CONFIG_DRM_EXYNOS_G2D
554 	platform_driver_unregister(&g2d_driver);
555 out_g2d:
556 #endif
557 
558 #ifdef CONFIG_DRM_EXYNOS_VIDI
559 	platform_driver_unregister(&vidi_driver);
560 out_vidi:
561 #endif
562 
563 #ifdef CONFIG_DRM_EXYNOS_HDMI
564 	platform_driver_unregister(&mixer_driver);
565 out_mixer:
566 	platform_driver_unregister(&hdmi_driver);
567 out_hdmi:
568 #endif
569 
570 #ifdef CONFIG_DRM_EXYNOS_FIMD
571 	platform_driver_unregister(&fimd_driver);
572 out_fimd:
573 #endif
574 
575 #ifdef CONFIG_DRM_EXYNOS_DSI
576 	platform_driver_unregister(&dsi_driver);
577 out_dsi:
578 #endif
579 
580 #ifdef CONFIG_DRM_EXYNOS_DP
581 	platform_driver_unregister(&dp_driver);
582 out_dp:
583 #endif
584 	return ret;
585 }
586 
587 static void __exit exynos_drm_exit(void)
588 {
589 	platform_device_unregister(exynos_drm_pdev);
590 
591 	platform_driver_unregister(&exynos_drm_platform_driver);
592 
593 #ifdef CONFIG_DRM_EXYNOS_IPP
594 	exynos_platform_device_ipp_unregister();
595 	platform_driver_unregister(&ipp_driver);
596 #endif
597 
598 #ifdef CONFIG_DRM_EXYNOS_GSC
599 	platform_driver_unregister(&gsc_driver);
600 #endif
601 
602 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
603 	platform_driver_unregister(&rotator_driver);
604 #endif
605 
606 #ifdef CONFIG_DRM_EXYNOS_FIMC
607 	platform_driver_unregister(&fimc_driver);
608 #endif
609 
610 #ifdef CONFIG_DRM_EXYNOS_G2D
611 	platform_driver_unregister(&g2d_driver);
612 #endif
613 
614 #ifdef CONFIG_DRM_EXYNOS_HDMI
615 	platform_driver_unregister(&mixer_driver);
616 	platform_driver_unregister(&hdmi_driver);
617 #endif
618 
619 #ifdef CONFIG_DRM_EXYNOS_VIDI
620 	platform_driver_unregister(&vidi_driver);
621 #endif
622 
623 #ifdef CONFIG_DRM_EXYNOS_FIMD
624 	platform_driver_unregister(&fimd_driver);
625 #endif
626 
627 #ifdef CONFIG_DRM_EXYNOS_DSI
628 	platform_driver_unregister(&dsi_driver);
629 #endif
630 
631 #ifdef CONFIG_DRM_EXYNOS_DP
632 	platform_driver_unregister(&dp_driver);
633 #endif
634 }
635 
636 module_init(exynos_drm_init);
637 module_exit(exynos_drm_exit);
638 
639 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
640 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
641 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
642 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
643 MODULE_LICENSE("GPL");
644