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