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/component.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 static struct platform_device *exynos_drm_pdev;
42 
43 static DEFINE_MUTEX(drm_component_lock);
44 static LIST_HEAD(drm_component_list);
45 
46 struct component_dev {
47 	struct list_head list;
48 	struct device *crtc_dev;
49 	struct device *conn_dev;
50 	enum exynos_drm_output_type out_type;
51 	unsigned int dev_type_flag;
52 };
53 
54 static int exynos_drm_load(struct drm_device *dev, unsigned long flags)
55 {
56 	struct exynos_drm_private *private;
57 	int ret;
58 	int nr;
59 
60 	private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
61 	if (!private)
62 		return -ENOMEM;
63 
64 	INIT_LIST_HEAD(&private->pageflip_event_list);
65 	dev_set_drvdata(dev->dev, dev);
66 	dev->dev_private = (void *)private;
67 
68 	/*
69 	 * create mapping to manage iommu table and set a pointer to iommu
70 	 * mapping structure to iommu_mapping of private data.
71 	 * also this iommu_mapping can be used to check if iommu is supported
72 	 * or not.
73 	 */
74 	ret = drm_create_iommu_mapping(dev);
75 	if (ret < 0) {
76 		DRM_ERROR("failed to create iommu mapping.\n");
77 		goto err_free_private;
78 	}
79 
80 	drm_mode_config_init(dev);
81 
82 	exynos_drm_mode_config_init(dev);
83 
84 	for (nr = 0; nr < MAX_PLANE; nr++) {
85 		struct drm_plane *plane;
86 		unsigned long possible_crtcs = (1 << MAX_CRTC) - 1;
87 
88 		plane = exynos_plane_init(dev, possible_crtcs,
89 					  DRM_PLANE_TYPE_OVERLAY);
90 		if (!IS_ERR(plane))
91 			continue;
92 
93 		ret = PTR_ERR(plane);
94 		goto err_mode_config_cleanup;
95 	}
96 
97 	/* setup possible_clones. */
98 	exynos_drm_encoder_setup(dev);
99 
100 	platform_set_drvdata(dev->platformdev, dev);
101 
102 	/* Try to bind all sub drivers. */
103 	ret = component_bind_all(dev->dev, dev);
104 	if (ret)
105 		goto err_mode_config_cleanup;
106 
107 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
108 	if (ret)
109 		goto err_unbind_all;
110 
111 	/* Probe non kms sub drivers and virtual display driver. */
112 	ret = exynos_drm_device_subdrv_probe(dev);
113 	if (ret)
114 		goto err_cleanup_vblank;
115 
116 	/*
117 	 * enable drm irq mode.
118 	 * - with irq_enabled = true, we can use the vblank feature.
119 	 *
120 	 * P.S. note that we wouldn't use drm irq handler but
121 	 *	just specific driver own one instead because
122 	 *	drm framework supports only one irq handler.
123 	 */
124 	dev->irq_enabled = true;
125 
126 	/*
127 	 * with vblank_disable_allowed = true, vblank interrupt will be disabled
128 	 * by drm timer once a current process gives up ownership of
129 	 * vblank event.(after drm_vblank_put function is called)
130 	 */
131 	dev->vblank_disable_allowed = true;
132 
133 	/* init kms poll for handling hpd */
134 	drm_kms_helper_poll_init(dev);
135 
136 	/* force connectors detection */
137 	drm_helper_hpd_irq_event(dev);
138 
139 	return 0;
140 
141 err_cleanup_vblank:
142 	drm_vblank_cleanup(dev);
143 err_unbind_all:
144 	component_unbind_all(dev->dev, dev);
145 err_mode_config_cleanup:
146 	drm_mode_config_cleanup(dev);
147 	drm_release_iommu_mapping(dev);
148 err_free_private:
149 	kfree(private);
150 
151 	return ret;
152 }
153 
154 static int exynos_drm_unload(struct drm_device *dev)
155 {
156 	exynos_drm_device_subdrv_remove(dev);
157 
158 	exynos_drm_fbdev_fini(dev);
159 	drm_kms_helper_poll_fini(dev);
160 
161 	drm_vblank_cleanup(dev);
162 	component_unbind_all(dev->dev, dev);
163 	drm_mode_config_cleanup(dev);
164 	drm_release_iommu_mapping(dev);
165 
166 	kfree(dev->dev_private);
167 	dev->dev_private = NULL;
168 
169 	return 0;
170 }
171 
172 static int exynos_drm_suspend(struct drm_device *dev, pm_message_t state)
173 {
174 	struct drm_connector *connector;
175 
176 	drm_modeset_lock_all(dev);
177 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
178 		int old_dpms = connector->dpms;
179 
180 		if (connector->funcs->dpms)
181 			connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
182 
183 		/* Set the old mode back to the connector for resume */
184 		connector->dpms = old_dpms;
185 	}
186 	drm_modeset_unlock_all(dev);
187 
188 	return 0;
189 }
190 
191 static int exynos_drm_resume(struct drm_device *dev)
192 {
193 	struct drm_connector *connector;
194 
195 	drm_modeset_lock_all(dev);
196 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
197 		if (connector->funcs->dpms) {
198 			int dpms = connector->dpms;
199 
200 			connector->dpms = DRM_MODE_DPMS_OFF;
201 			connector->funcs->dpms(connector, dpms);
202 		}
203 	}
204 	drm_modeset_unlock_all(dev);
205 
206 	return 0;
207 }
208 
209 static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
210 {
211 	struct drm_exynos_file_private *file_priv;
212 	int ret;
213 
214 	file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
215 	if (!file_priv)
216 		return -ENOMEM;
217 
218 	file->driver_priv = file_priv;
219 
220 	ret = exynos_drm_subdrv_open(dev, file);
221 	if (ret)
222 		goto err_file_priv_free;
223 
224 	return ret;
225 
226 err_file_priv_free:
227 	kfree(file_priv);
228 	file->driver_priv = NULL;
229 	return ret;
230 }
231 
232 static void exynos_drm_preclose(struct drm_device *dev,
233 					struct drm_file *file)
234 {
235 	exynos_drm_subdrv_close(dev, file);
236 }
237 
238 static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
239 {
240 	struct exynos_drm_private *private = dev->dev_private;
241 	struct drm_pending_vblank_event *v, *vt;
242 	struct drm_pending_event *e, *et;
243 	unsigned long flags;
244 
245 	if (!file->driver_priv)
246 		return;
247 
248 	/* Release all events not unhandled by page flip handler. */
249 	spin_lock_irqsave(&dev->event_lock, flags);
250 	list_for_each_entry_safe(v, vt, &private->pageflip_event_list,
251 			base.link) {
252 		if (v->base.file_priv == file) {
253 			list_del(&v->base.link);
254 			drm_vblank_put(dev, v->pipe);
255 			v->base.destroy(&v->base);
256 		}
257 	}
258 
259 	/* Release all events handled by page flip handler but not freed. */
260 	list_for_each_entry_safe(e, et, &file->event_list, link) {
261 		list_del(&e->link);
262 		e->destroy(e);
263 	}
264 	spin_unlock_irqrestore(&dev->event_lock, flags);
265 
266 	kfree(file->driver_priv);
267 	file->driver_priv = NULL;
268 }
269 
270 static void exynos_drm_lastclose(struct drm_device *dev)
271 {
272 	exynos_drm_fbdev_restore_mode(dev);
273 }
274 
275 static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
276 	.fault = exynos_drm_gem_fault,
277 	.open = drm_gem_vm_open,
278 	.close = drm_gem_vm_close,
279 };
280 
281 static const struct drm_ioctl_desc exynos_ioctls[] = {
282 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
283 			DRM_UNLOCKED | DRM_AUTH),
284 	DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET,
285 			exynos_drm_gem_get_ioctl, DRM_UNLOCKED),
286 	DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION,
287 			vidi_connection_ioctl, DRM_UNLOCKED | DRM_AUTH),
288 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER,
289 			exynos_g2d_get_ver_ioctl, DRM_UNLOCKED | DRM_AUTH),
290 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST,
291 			exynos_g2d_set_cmdlist_ioctl, DRM_UNLOCKED | DRM_AUTH),
292 	DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC,
293 			exynos_g2d_exec_ioctl, DRM_UNLOCKED | DRM_AUTH),
294 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY,
295 			exynos_drm_ipp_get_property, DRM_UNLOCKED | DRM_AUTH),
296 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY,
297 			exynos_drm_ipp_set_property, DRM_UNLOCKED | DRM_AUTH),
298 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF,
299 			exynos_drm_ipp_queue_buf, DRM_UNLOCKED | DRM_AUTH),
300 	DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL,
301 			exynos_drm_ipp_cmd_ctrl, DRM_UNLOCKED | DRM_AUTH),
302 };
303 
304 static const struct file_operations exynos_drm_driver_fops = {
305 	.owner		= THIS_MODULE,
306 	.open		= drm_open,
307 	.mmap		= exynos_drm_gem_mmap,
308 	.poll		= drm_poll,
309 	.read		= drm_read,
310 	.unlocked_ioctl	= drm_ioctl,
311 #ifdef CONFIG_COMPAT
312 	.compat_ioctl = drm_compat_ioctl,
313 #endif
314 	.release	= drm_release,
315 };
316 
317 static struct drm_driver exynos_drm_driver = {
318 	.driver_features	= DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
319 	.load			= exynos_drm_load,
320 	.unload			= exynos_drm_unload,
321 	.suspend		= exynos_drm_suspend,
322 	.resume			= exynos_drm_resume,
323 	.open			= exynos_drm_open,
324 	.preclose		= exynos_drm_preclose,
325 	.lastclose		= exynos_drm_lastclose,
326 	.postclose		= exynos_drm_postclose,
327 	.set_busid		= drm_platform_set_busid,
328 	.get_vblank_counter	= drm_vblank_count,
329 	.enable_vblank		= exynos_drm_crtc_enable_vblank,
330 	.disable_vblank		= exynos_drm_crtc_disable_vblank,
331 	.gem_free_object	= exynos_drm_gem_free_object,
332 	.gem_vm_ops		= &exynos_drm_gem_vm_ops,
333 	.dumb_create		= exynos_drm_gem_dumb_create,
334 	.dumb_map_offset	= exynos_drm_gem_dumb_map_offset,
335 	.dumb_destroy		= drm_gem_dumb_destroy,
336 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
337 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
338 	.gem_prime_export	= exynos_dmabuf_prime_export,
339 	.gem_prime_import	= exynos_dmabuf_prime_import,
340 	.ioctls			= exynos_ioctls,
341 	.num_ioctls		= ARRAY_SIZE(exynos_ioctls),
342 	.fops			= &exynos_drm_driver_fops,
343 	.name	= DRIVER_NAME,
344 	.desc	= DRIVER_DESC,
345 	.date	= DRIVER_DATE,
346 	.major	= DRIVER_MAJOR,
347 	.minor	= DRIVER_MINOR,
348 };
349 
350 #ifdef CONFIG_PM_SLEEP
351 static int exynos_drm_sys_suspend(struct device *dev)
352 {
353 	struct drm_device *drm_dev = dev_get_drvdata(dev);
354 	pm_message_t message;
355 
356 	if (pm_runtime_suspended(dev) || !drm_dev)
357 		return 0;
358 
359 	message.event = PM_EVENT_SUSPEND;
360 	return exynos_drm_suspend(drm_dev, message);
361 }
362 
363 static int exynos_drm_sys_resume(struct device *dev)
364 {
365 	struct drm_device *drm_dev = dev_get_drvdata(dev);
366 
367 	if (pm_runtime_suspended(dev) || !drm_dev)
368 		return 0;
369 
370 	return exynos_drm_resume(drm_dev);
371 }
372 #endif
373 
374 static const struct dev_pm_ops exynos_drm_pm_ops = {
375 	SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_sys_suspend, exynos_drm_sys_resume)
376 };
377 
378 int exynos_drm_component_add(struct device *dev,
379 				enum exynos_drm_device_type dev_type,
380 				enum exynos_drm_output_type out_type)
381 {
382 	struct component_dev *cdev;
383 
384 	if (dev_type != EXYNOS_DEVICE_TYPE_CRTC &&
385 			dev_type != EXYNOS_DEVICE_TYPE_CONNECTOR) {
386 		DRM_ERROR("invalid device type.\n");
387 		return -EINVAL;
388 	}
389 
390 	mutex_lock(&drm_component_lock);
391 
392 	/*
393 	 * Make sure to check if there is a component which has two device
394 	 * objects, for connector and for encoder/connector.
395 	 * It should make sure that crtc and encoder/connector drivers are
396 	 * ready before exynos drm core binds them.
397 	 */
398 	list_for_each_entry(cdev, &drm_component_list, list) {
399 		if (cdev->out_type == out_type) {
400 			/*
401 			 * If crtc and encoder/connector device objects are
402 			 * added already just return.
403 			 */
404 			if (cdev->dev_type_flag == (EXYNOS_DEVICE_TYPE_CRTC |
405 						EXYNOS_DEVICE_TYPE_CONNECTOR)) {
406 				mutex_unlock(&drm_component_lock);
407 				return 0;
408 			}
409 
410 			if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
411 				cdev->crtc_dev = dev;
412 				cdev->dev_type_flag |= dev_type;
413 			}
414 
415 			if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
416 				cdev->conn_dev = dev;
417 				cdev->dev_type_flag |= dev_type;
418 			}
419 
420 			mutex_unlock(&drm_component_lock);
421 			return 0;
422 		}
423 	}
424 
425 	mutex_unlock(&drm_component_lock);
426 
427 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
428 	if (!cdev)
429 		return -ENOMEM;
430 
431 	if (dev_type == EXYNOS_DEVICE_TYPE_CRTC)
432 		cdev->crtc_dev = dev;
433 	if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR)
434 		cdev->conn_dev = dev;
435 
436 	cdev->out_type = out_type;
437 	cdev->dev_type_flag = dev_type;
438 
439 	mutex_lock(&drm_component_lock);
440 	list_add_tail(&cdev->list, &drm_component_list);
441 	mutex_unlock(&drm_component_lock);
442 
443 	return 0;
444 }
445 
446 void exynos_drm_component_del(struct device *dev,
447 				enum exynos_drm_device_type dev_type)
448 {
449 	struct component_dev *cdev, *next;
450 
451 	mutex_lock(&drm_component_lock);
452 
453 	list_for_each_entry_safe(cdev, next, &drm_component_list, list) {
454 		if (dev_type == EXYNOS_DEVICE_TYPE_CRTC) {
455 			if (cdev->crtc_dev == dev) {
456 				cdev->crtc_dev = NULL;
457 				cdev->dev_type_flag &= ~dev_type;
458 			}
459 		}
460 
461 		if (dev_type == EXYNOS_DEVICE_TYPE_CONNECTOR) {
462 			if (cdev->conn_dev == dev) {
463 				cdev->conn_dev = NULL;
464 				cdev->dev_type_flag &= ~dev_type;
465 			}
466 		}
467 
468 		/*
469 		 * Release cdev object only in case that both of crtc and
470 		 * encoder/connector device objects are NULL.
471 		 */
472 		if (!cdev->crtc_dev && !cdev->conn_dev) {
473 			list_del(&cdev->list);
474 			kfree(cdev);
475 		}
476 	}
477 
478 	mutex_unlock(&drm_component_lock);
479 }
480 
481 static int compare_dev(struct device *dev, void *data)
482 {
483 	return dev == (struct device *)data;
484 }
485 
486 static struct component_match *exynos_drm_match_add(struct device *dev)
487 {
488 	struct component_match *match = NULL;
489 	struct component_dev *cdev;
490 	unsigned int attach_cnt = 0;
491 
492 	mutex_lock(&drm_component_lock);
493 
494 	/* Do not retry to probe if there is no any kms driver regitered. */
495 	if (list_empty(&drm_component_list)) {
496 		mutex_unlock(&drm_component_lock);
497 		return ERR_PTR(-ENODEV);
498 	}
499 
500 	list_for_each_entry(cdev, &drm_component_list, list) {
501 		/*
502 		 * Add components to master only in case that crtc and
503 		 * encoder/connector device objects exist.
504 		 */
505 		if (!cdev->crtc_dev || !cdev->conn_dev)
506 			continue;
507 
508 		attach_cnt++;
509 
510 		mutex_unlock(&drm_component_lock);
511 
512 		/*
513 		 * fimd and dpi modules have same device object so add
514 		 * only crtc device object in this case.
515 		 */
516 		if (cdev->crtc_dev == cdev->conn_dev) {
517 			component_match_add(dev, &match, compare_dev,
518 						cdev->crtc_dev);
519 			goto out_lock;
520 		}
521 
522 		/*
523 		 * Do not chage below call order.
524 		 * crtc device first should be added to master because
525 		 * connector/encoder need pipe number of crtc when they
526 		 * are created.
527 		 */
528 		component_match_add(dev, &match, compare_dev, cdev->crtc_dev);
529 		component_match_add(dev, &match, compare_dev, cdev->conn_dev);
530 
531 out_lock:
532 		mutex_lock(&drm_component_lock);
533 	}
534 
535 	mutex_unlock(&drm_component_lock);
536 
537 	return attach_cnt ? match : ERR_PTR(-EPROBE_DEFER);
538 }
539 
540 static int exynos_drm_bind(struct device *dev)
541 {
542 	return drm_platform_init(&exynos_drm_driver, to_platform_device(dev));
543 }
544 
545 static void exynos_drm_unbind(struct device *dev)
546 {
547 	drm_put_dev(dev_get_drvdata(dev));
548 }
549 
550 static const struct component_master_ops exynos_drm_ops = {
551 	.bind		= exynos_drm_bind,
552 	.unbind		= exynos_drm_unbind,
553 };
554 
555 static struct platform_driver *const exynos_drm_kms_drivers[] = {
556 #ifdef CONFIG_DRM_EXYNOS_FIMD
557 	&fimd_driver,
558 #endif
559 #ifdef CONFIG_DRM_EXYNOS_DP
560 	&dp_driver,
561 #endif
562 #ifdef CONFIG_DRM_EXYNOS_DSI
563 	&dsi_driver,
564 #endif
565 #ifdef CONFIG_DRM_EXYNOS_HDMI
566 	&mixer_driver,
567 	&hdmi_driver,
568 #endif
569 };
570 
571 static struct platform_driver *const exynos_drm_non_kms_drivers[] = {
572 #ifdef CONFIG_DRM_EXYNOS_G2D
573 	&g2d_driver,
574 #endif
575 #ifdef CONFIG_DRM_EXYNOS_FIMC
576 	&fimc_driver,
577 #endif
578 #ifdef CONFIG_DRM_EXYNOS_ROTATOR
579 	&rotator_driver,
580 #endif
581 #ifdef CONFIG_DRM_EXYNOS_GSC
582 	&gsc_driver,
583 #endif
584 #ifdef CONFIG_DRM_EXYNOS_IPP
585 	&ipp_driver,
586 #endif
587 };
588 
589 static int exynos_drm_platform_probe(struct platform_device *pdev)
590 {
591 	struct component_match *match;
592 
593 	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
594 	exynos_drm_driver.num_ioctls = ARRAY_SIZE(exynos_ioctls);
595 
596 	match = exynos_drm_match_add(&pdev->dev);
597 	if (IS_ERR(match)) {
598 		return PTR_ERR(match);
599 	}
600 
601 	return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
602 					       match);
603 }
604 
605 static int exynos_drm_platform_remove(struct platform_device *pdev)
606 {
607 	component_master_del(&pdev->dev, &exynos_drm_ops);
608 	return 0;
609 }
610 
611 static const char * const strings[] = {
612 	"samsung,exynos3",
613 	"samsung,exynos4",
614 	"samsung,exynos5",
615 };
616 
617 static struct platform_driver exynos_drm_platform_driver = {
618 	.probe	= exynos_drm_platform_probe,
619 	.remove	= exynos_drm_platform_remove,
620 	.driver	= {
621 		.name	= "exynos-drm",
622 		.pm	= &exynos_drm_pm_ops,
623 	},
624 };
625 
626 static int exynos_drm_init(void)
627 {
628 	bool is_exynos = false;
629 	int ret, i, j;
630 
631 	/*
632 	 * Register device object only in case of Exynos SoC.
633 	 *
634 	 * Below codes resolves temporarily infinite loop issue incurred
635 	 * by Exynos drm driver when using multi-platform kernel.
636 	 * So these codes will be replaced with more generic way later.
637 	 */
638 	for (i = 0; i < ARRAY_SIZE(strings); i++) {
639 		if (of_machine_is_compatible(strings[i])) {
640 			is_exynos = true;
641 			break;
642 		}
643 	}
644 
645 	if (!is_exynos)
646 		return -ENODEV;
647 
648 	/*
649 	 * Register device object only in case of Exynos SoC.
650 	 *
651 	 * Below codes resolves temporarily infinite loop issue incurred
652 	 * by Exynos drm driver when using multi-platform kernel.
653 	 * So these codes will be replaced with more generic way later.
654 	 */
655 	if (!of_machine_is_compatible("samsung,exynos3") &&
656 			!of_machine_is_compatible("samsung,exynos4") &&
657 			!of_machine_is_compatible("samsung,exynos5"))
658 		return -ENODEV;
659 
660 	exynos_drm_pdev = platform_device_register_simple("exynos-drm", -1,
661 								NULL, 0);
662 	if (IS_ERR(exynos_drm_pdev))
663 		return PTR_ERR(exynos_drm_pdev);
664 
665 	ret = exynos_drm_probe_vidi();
666 	if (ret < 0)
667 		goto err_unregister_pd;
668 
669 	for (i = 0; i < ARRAY_SIZE(exynos_drm_kms_drivers); ++i) {
670 		ret = platform_driver_register(exynos_drm_kms_drivers[i]);
671 		if (ret < 0)
672 			goto err_unregister_kms_drivers;
673 	}
674 
675 	for (j = 0; j < ARRAY_SIZE(exynos_drm_non_kms_drivers); ++j) {
676 		ret = platform_driver_register(exynos_drm_non_kms_drivers[j]);
677 		if (ret < 0)
678 			goto err_unregister_non_kms_drivers;
679 	}
680 
681 #ifdef CONFIG_DRM_EXYNOS_IPP
682 	ret = exynos_platform_device_ipp_register();
683 	if (ret < 0)
684 		goto err_unregister_non_kms_drivers;
685 #endif
686 
687 	ret = platform_driver_register(&exynos_drm_platform_driver);
688 	if (ret)
689 		goto err_unregister_resources;
690 
691 	return 0;
692 
693 err_unregister_resources:
694 #ifdef CONFIG_DRM_EXYNOS_IPP
695 	exynos_platform_device_ipp_unregister();
696 #endif
697 
698 err_unregister_non_kms_drivers:
699 	while (--j >= 0)
700 		platform_driver_unregister(exynos_drm_non_kms_drivers[j]);
701 
702 err_unregister_kms_drivers:
703 	while (--i >= 0)
704 		platform_driver_unregister(exynos_drm_kms_drivers[i]);
705 
706 	exynos_drm_remove_vidi();
707 
708 err_unregister_pd:
709 	platform_device_unregister(exynos_drm_pdev);
710 
711 	return ret;
712 }
713 
714 static void exynos_drm_exit(void)
715 {
716 	int i;
717 
718 #ifdef CONFIG_DRM_EXYNOS_IPP
719 	exynos_platform_device_ipp_unregister();
720 #endif
721 
722 	for (i = ARRAY_SIZE(exynos_drm_non_kms_drivers) - 1; i >= 0; --i)
723 		platform_driver_unregister(exynos_drm_non_kms_drivers[i]);
724 
725 	for (i = ARRAY_SIZE(exynos_drm_kms_drivers) - 1; i >= 0; --i)
726 		platform_driver_unregister(exynos_drm_kms_drivers[i]);
727 
728 	platform_driver_unregister(&exynos_drm_platform_driver);
729 
730 	exynos_drm_remove_vidi();
731 
732 	platform_device_unregister(exynos_drm_pdev);
733 }
734 
735 module_init(exynos_drm_init);
736 module_exit(exynos_drm_exit);
737 
738 MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
739 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
740 MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
741 MODULE_DESCRIPTION("Samsung SoC DRM Driver");
742 MODULE_LICENSE("GPL");
743