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