xref: /linux/drivers/gpu/drm/mediatek/mtk_drm_drv.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  * Author: YT SHEN <yt.shen@mediatek.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
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 #include <drm/drm_gem.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <linux/component.h>
22 #include <linux/iommu.h>
23 #include <linux/of_address.h>
24 #include <linux/of_platform.h>
25 #include <linux/pm_runtime.h>
26 
27 #include "mtk_drm_crtc.h"
28 #include "mtk_drm_ddp.h"
29 #include "mtk_drm_ddp_comp.h"
30 #include "mtk_drm_drv.h"
31 #include "mtk_drm_fb.h"
32 #include "mtk_drm_gem.h"
33 
34 #define DRIVER_NAME "mediatek"
35 #define DRIVER_DESC "Mediatek SoC DRM"
36 #define DRIVER_DATE "20150513"
37 #define DRIVER_MAJOR 1
38 #define DRIVER_MINOR 0
39 
40 static void mtk_atomic_schedule(struct mtk_drm_private *private,
41 				struct drm_atomic_state *state)
42 {
43 	private->commit.state = state;
44 	schedule_work(&private->commit.work);
45 }
46 
47 static void mtk_atomic_wait_for_fences(struct drm_atomic_state *state)
48 {
49 	struct drm_plane *plane;
50 	struct drm_plane_state *plane_state;
51 	int i;
52 
53 	for_each_plane_in_state(state, plane, plane_state, i)
54 		mtk_fb_wait(plane->state->fb);
55 }
56 
57 static void mtk_atomic_complete(struct mtk_drm_private *private,
58 				struct drm_atomic_state *state)
59 {
60 	struct drm_device *drm = private->drm;
61 
62 	mtk_atomic_wait_for_fences(state);
63 
64 	drm_atomic_helper_commit_modeset_disables(drm, state);
65 	drm_atomic_helper_commit_planes(drm, state, false);
66 	drm_atomic_helper_commit_modeset_enables(drm, state);
67 	drm_atomic_helper_wait_for_vblanks(drm, state);
68 	drm_atomic_helper_cleanup_planes(drm, state);
69 	drm_atomic_state_free(state);
70 }
71 
72 static void mtk_atomic_work(struct work_struct *work)
73 {
74 	struct mtk_drm_private *private = container_of(work,
75 			struct mtk_drm_private, commit.work);
76 
77 	mtk_atomic_complete(private, private->commit.state);
78 }
79 
80 static int mtk_atomic_commit(struct drm_device *drm,
81 			     struct drm_atomic_state *state,
82 			     bool async)
83 {
84 	struct mtk_drm_private *private = drm->dev_private;
85 	int ret;
86 
87 	ret = drm_atomic_helper_prepare_planes(drm, state);
88 	if (ret)
89 		return ret;
90 
91 	mutex_lock(&private->commit.lock);
92 	flush_work(&private->commit.work);
93 
94 	drm_atomic_helper_swap_state(drm, state);
95 
96 	if (async)
97 		mtk_atomic_schedule(private, state);
98 	else
99 		mtk_atomic_complete(private, state);
100 
101 	mutex_unlock(&private->commit.lock);
102 
103 	return 0;
104 }
105 
106 static const struct drm_mode_config_funcs mtk_drm_mode_config_funcs = {
107 	.fb_create = mtk_drm_mode_fb_create,
108 	.atomic_check = drm_atomic_helper_check,
109 	.atomic_commit = mtk_atomic_commit,
110 };
111 
112 static const enum mtk_ddp_comp_id mtk_ddp_main[] = {
113 	DDP_COMPONENT_OVL0,
114 	DDP_COMPONENT_COLOR0,
115 	DDP_COMPONENT_AAL,
116 	DDP_COMPONENT_OD,
117 	DDP_COMPONENT_RDMA0,
118 	DDP_COMPONENT_UFOE,
119 	DDP_COMPONENT_DSI0,
120 	DDP_COMPONENT_PWM0,
121 };
122 
123 static const enum mtk_ddp_comp_id mtk_ddp_ext[] = {
124 	DDP_COMPONENT_OVL1,
125 	DDP_COMPONENT_COLOR1,
126 	DDP_COMPONENT_GAMMA,
127 	DDP_COMPONENT_RDMA1,
128 	DDP_COMPONENT_DPI0,
129 };
130 
131 static int mtk_drm_kms_init(struct drm_device *drm)
132 {
133 	struct mtk_drm_private *private = drm->dev_private;
134 	struct platform_device *pdev;
135 	struct device_node *np;
136 	int ret;
137 
138 	if (!iommu_present(&platform_bus_type))
139 		return -EPROBE_DEFER;
140 
141 	pdev = of_find_device_by_node(private->mutex_node);
142 	if (!pdev) {
143 		dev_err(drm->dev, "Waiting for disp-mutex device %s\n",
144 			private->mutex_node->full_name);
145 		of_node_put(private->mutex_node);
146 		return -EPROBE_DEFER;
147 	}
148 	private->mutex_dev = &pdev->dev;
149 
150 	drm_mode_config_init(drm);
151 
152 	drm->mode_config.min_width = 64;
153 	drm->mode_config.min_height = 64;
154 
155 	/*
156 	 * set max width and height as default value(4096x4096).
157 	 * this value would be used to check framebuffer size limitation
158 	 * at drm_mode_addfb().
159 	 */
160 	drm->mode_config.max_width = 4096;
161 	drm->mode_config.max_height = 4096;
162 	drm->mode_config.funcs = &mtk_drm_mode_config_funcs;
163 
164 	ret = component_bind_all(drm->dev, drm);
165 	if (ret)
166 		goto err_config_cleanup;
167 
168 	/*
169 	 * We currently support two fixed data streams, each optional,
170 	 * and each statically assigned to a crtc:
171 	 * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ...
172 	 */
173 	ret = mtk_drm_crtc_create(drm, mtk_ddp_main, ARRAY_SIZE(mtk_ddp_main));
174 	if (ret < 0)
175 		goto err_component_unbind;
176 	/* ... and OVL1 -> COLOR1 -> GAMMA -> RDMA1 -> DPI0. */
177 	ret = mtk_drm_crtc_create(drm, mtk_ddp_ext, ARRAY_SIZE(mtk_ddp_ext));
178 	if (ret < 0)
179 		goto err_component_unbind;
180 
181 	/* Use OVL device for all DMA memory allocations */
182 	np = private->comp_node[mtk_ddp_main[0]] ?:
183 	     private->comp_node[mtk_ddp_ext[0]];
184 	pdev = of_find_device_by_node(np);
185 	if (!pdev) {
186 		ret = -ENODEV;
187 		dev_err(drm->dev, "Need at least one OVL device\n");
188 		goto err_component_unbind;
189 	}
190 
191 	private->dma_dev = &pdev->dev;
192 
193 	/*
194 	 * We don't use the drm_irq_install() helpers provided by the DRM
195 	 * core, so we need to set this manually in order to allow the
196 	 * DRM_IOCTL_WAIT_VBLANK to operate correctly.
197 	 */
198 	drm->irq_enabled = true;
199 	ret = drm_vblank_init(drm, MAX_CRTC);
200 	if (ret < 0)
201 		goto err_component_unbind;
202 
203 	drm_kms_helper_poll_init(drm);
204 	drm_mode_config_reset(drm);
205 
206 	return 0;
207 
208 err_component_unbind:
209 	component_unbind_all(drm->dev, drm);
210 err_config_cleanup:
211 	drm_mode_config_cleanup(drm);
212 
213 	return ret;
214 }
215 
216 static void mtk_drm_kms_deinit(struct drm_device *drm)
217 {
218 	drm_kms_helper_poll_fini(drm);
219 
220 	drm_vblank_cleanup(drm);
221 	component_unbind_all(drm->dev, drm);
222 	drm_mode_config_cleanup(drm);
223 }
224 
225 static const struct file_operations mtk_drm_fops = {
226 	.owner = THIS_MODULE,
227 	.open = drm_open,
228 	.release = drm_release,
229 	.unlocked_ioctl = drm_ioctl,
230 	.mmap = mtk_drm_gem_mmap,
231 	.poll = drm_poll,
232 	.read = drm_read,
233 #ifdef CONFIG_COMPAT
234 	.compat_ioctl = drm_compat_ioctl,
235 #endif
236 };
237 
238 static struct drm_driver mtk_drm_driver = {
239 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
240 			   DRIVER_ATOMIC,
241 
242 	.get_vblank_counter = drm_vblank_count,
243 	.enable_vblank = mtk_drm_crtc_enable_vblank,
244 	.disable_vblank = mtk_drm_crtc_disable_vblank,
245 
246 	.gem_free_object = mtk_drm_gem_free_object,
247 	.gem_vm_ops = &drm_gem_cma_vm_ops,
248 	.dumb_create = mtk_drm_gem_dumb_create,
249 	.dumb_map_offset = mtk_drm_gem_dumb_map_offset,
250 	.dumb_destroy = drm_gem_dumb_destroy,
251 
252 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
253 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
254 	.gem_prime_export = drm_gem_prime_export,
255 	.gem_prime_import = drm_gem_prime_import,
256 	.gem_prime_get_sg_table = mtk_gem_prime_get_sg_table,
257 	.gem_prime_import_sg_table = mtk_gem_prime_import_sg_table,
258 	.gem_prime_mmap = mtk_drm_gem_mmap_buf,
259 	.fops = &mtk_drm_fops,
260 
261 	.name = DRIVER_NAME,
262 	.desc = DRIVER_DESC,
263 	.date = DRIVER_DATE,
264 	.major = DRIVER_MAJOR,
265 	.minor = DRIVER_MINOR,
266 };
267 
268 static int compare_of(struct device *dev, void *data)
269 {
270 	return dev->of_node == data;
271 }
272 
273 static int mtk_drm_bind(struct device *dev)
274 {
275 	struct mtk_drm_private *private = dev_get_drvdata(dev);
276 	struct drm_device *drm;
277 	int ret;
278 
279 	drm = drm_dev_alloc(&mtk_drm_driver, dev);
280 	if (!drm)
281 		return -ENOMEM;
282 
283 	drm_dev_set_unique(drm, dev_name(dev));
284 
285 	drm->dev_private = private;
286 	private->drm = drm;
287 
288 	ret = mtk_drm_kms_init(drm);
289 	if (ret < 0)
290 		goto err_free;
291 
292 	ret = drm_dev_register(drm, 0);
293 	if (ret < 0)
294 		goto err_deinit;
295 
296 	ret = drm_connector_register_all(drm);
297 	if (ret < 0)
298 		goto err_unregister;
299 
300 	return 0;
301 
302 err_unregister:
303 	drm_dev_unregister(drm);
304 err_deinit:
305 	mtk_drm_kms_deinit(drm);
306 err_free:
307 	drm_dev_unref(drm);
308 	return ret;
309 }
310 
311 static void mtk_drm_unbind(struct device *dev)
312 {
313 	struct mtk_drm_private *private = dev_get_drvdata(dev);
314 
315 	drm_put_dev(private->drm);
316 	private->drm = NULL;
317 }
318 
319 static const struct component_master_ops mtk_drm_ops = {
320 	.bind		= mtk_drm_bind,
321 	.unbind		= mtk_drm_unbind,
322 };
323 
324 static const struct of_device_id mtk_ddp_comp_dt_ids[] = {
325 	{ .compatible = "mediatek,mt8173-disp-ovl",   .data = (void *)MTK_DISP_OVL },
326 	{ .compatible = "mediatek,mt8173-disp-rdma",  .data = (void *)MTK_DISP_RDMA },
327 	{ .compatible = "mediatek,mt8173-disp-wdma",  .data = (void *)MTK_DISP_WDMA },
328 	{ .compatible = "mediatek,mt8173-disp-color", .data = (void *)MTK_DISP_COLOR },
329 	{ .compatible = "mediatek,mt8173-disp-aal",   .data = (void *)MTK_DISP_AAL},
330 	{ .compatible = "mediatek,mt8173-disp-gamma", .data = (void *)MTK_DISP_GAMMA, },
331 	{ .compatible = "mediatek,mt8173-disp-ufoe",  .data = (void *)MTK_DISP_UFOE },
332 	{ .compatible = "mediatek,mt8173-dsi",        .data = (void *)MTK_DSI },
333 	{ .compatible = "mediatek,mt8173-dpi",        .data = (void *)MTK_DPI },
334 	{ .compatible = "mediatek,mt8173-disp-mutex", .data = (void *)MTK_DISP_MUTEX },
335 	{ .compatible = "mediatek,mt8173-disp-pwm",   .data = (void *)MTK_DISP_PWM },
336 	{ .compatible = "mediatek,mt8173-disp-od",    .data = (void *)MTK_DISP_OD },
337 	{ }
338 };
339 
340 static int mtk_drm_probe(struct platform_device *pdev)
341 {
342 	struct device *dev = &pdev->dev;
343 	struct mtk_drm_private *private;
344 	struct resource *mem;
345 	struct device_node *node;
346 	struct component_match *match = NULL;
347 	int ret;
348 	int i;
349 
350 	private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL);
351 	if (!private)
352 		return -ENOMEM;
353 
354 	mutex_init(&private->commit.lock);
355 	INIT_WORK(&private->commit.work, mtk_atomic_work);
356 
357 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
358 	private->config_regs = devm_ioremap_resource(dev, mem);
359 	if (IS_ERR(private->config_regs)) {
360 		ret = PTR_ERR(private->config_regs);
361 		dev_err(dev, "Failed to ioremap mmsys-config resource: %d\n",
362 			ret);
363 		return ret;
364 	}
365 
366 	/* Iterate over sibling DISP function blocks */
367 	for_each_child_of_node(dev->of_node->parent, node) {
368 		const struct of_device_id *of_id;
369 		enum mtk_ddp_comp_type comp_type;
370 		int comp_id;
371 
372 		of_id = of_match_node(mtk_ddp_comp_dt_ids, node);
373 		if (!of_id)
374 			continue;
375 
376 		if (!of_device_is_available(node)) {
377 			dev_dbg(dev, "Skipping disabled component %s\n",
378 				node->full_name);
379 			continue;
380 		}
381 
382 		comp_type = (enum mtk_ddp_comp_type)of_id->data;
383 
384 		if (comp_type == MTK_DISP_MUTEX) {
385 			private->mutex_node = of_node_get(node);
386 			continue;
387 		}
388 
389 		comp_id = mtk_ddp_comp_get_id(node, comp_type);
390 		if (comp_id < 0) {
391 			dev_warn(dev, "Skipping unknown component %s\n",
392 				 node->full_name);
393 			continue;
394 		}
395 
396 		private->comp_node[comp_id] = of_node_get(node);
397 
398 		/*
399 		 * Currently only the OVL, RDMA, DSI, and DPI blocks have
400 		 * separate component platform drivers and initialize their own
401 		 * DDP component structure. The others are initialized here.
402 		 */
403 		if (comp_type == MTK_DISP_OVL ||
404 		    comp_type == MTK_DISP_RDMA ||
405 		    comp_type == MTK_DSI ||
406 		    comp_type == MTK_DPI) {
407 			dev_info(dev, "Adding component match for %s\n",
408 				 node->full_name);
409 			component_match_add(dev, &match, compare_of, node);
410 		} else {
411 			struct mtk_ddp_comp *comp;
412 
413 			comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
414 			if (!comp) {
415 				ret = -ENOMEM;
416 				goto err_node;
417 			}
418 
419 			ret = mtk_ddp_comp_init(dev, node, comp, comp_id, NULL);
420 			if (ret)
421 				goto err_node;
422 
423 			private->ddp_comp[comp_id] = comp;
424 		}
425 	}
426 
427 	if (!private->mutex_node) {
428 		dev_err(dev, "Failed to find disp-mutex node\n");
429 		ret = -ENODEV;
430 		goto err_node;
431 	}
432 
433 	pm_runtime_enable(dev);
434 
435 	platform_set_drvdata(pdev, private);
436 
437 	ret = component_master_add_with_match(dev, &mtk_drm_ops, match);
438 	if (ret)
439 		goto err_pm;
440 
441 	return 0;
442 
443 err_pm:
444 	pm_runtime_disable(dev);
445 err_node:
446 	of_node_put(private->mutex_node);
447 	for (i = 0; i < DDP_COMPONENT_ID_MAX; i++)
448 		of_node_put(private->comp_node[i]);
449 	return ret;
450 }
451 
452 static int mtk_drm_remove(struct platform_device *pdev)
453 {
454 	struct mtk_drm_private *private = platform_get_drvdata(pdev);
455 	struct drm_device *drm = private->drm;
456 	int i;
457 
458 	drm_connector_unregister_all(drm);
459 	drm_dev_unregister(drm);
460 	mtk_drm_kms_deinit(drm);
461 	drm_dev_unref(drm);
462 
463 	component_master_del(&pdev->dev, &mtk_drm_ops);
464 	pm_runtime_disable(&pdev->dev);
465 	of_node_put(private->mutex_node);
466 	for (i = 0; i < DDP_COMPONENT_ID_MAX; i++)
467 		of_node_put(private->comp_node[i]);
468 
469 	return 0;
470 }
471 
472 #ifdef CONFIG_PM_SLEEP
473 static int mtk_drm_sys_suspend(struct device *dev)
474 {
475 	struct mtk_drm_private *private = dev_get_drvdata(dev);
476 	struct drm_device *drm = private->drm;
477 
478 	drm_kms_helper_poll_disable(drm);
479 
480 	private->suspend_state = drm_atomic_helper_suspend(drm);
481 	if (IS_ERR(private->suspend_state)) {
482 		drm_kms_helper_poll_enable(drm);
483 		return PTR_ERR(private->suspend_state);
484 	}
485 
486 	DRM_DEBUG_DRIVER("mtk_drm_sys_suspend\n");
487 	return 0;
488 }
489 
490 static int mtk_drm_sys_resume(struct device *dev)
491 {
492 	struct mtk_drm_private *private = dev_get_drvdata(dev);
493 	struct drm_device *drm = private->drm;
494 
495 	drm_atomic_helper_resume(drm, private->suspend_state);
496 	drm_kms_helper_poll_enable(drm);
497 
498 	DRM_DEBUG_DRIVER("mtk_drm_sys_resume\n");
499 	return 0;
500 }
501 #endif
502 
503 static SIMPLE_DEV_PM_OPS(mtk_drm_pm_ops, mtk_drm_sys_suspend,
504 			 mtk_drm_sys_resume);
505 
506 static const struct of_device_id mtk_drm_of_ids[] = {
507 	{ .compatible = "mediatek,mt8173-mmsys", },
508 	{ }
509 };
510 
511 static struct platform_driver mtk_drm_platform_driver = {
512 	.probe	= mtk_drm_probe,
513 	.remove	= mtk_drm_remove,
514 	.driver	= {
515 		.name	= "mediatek-drm",
516 		.of_match_table = mtk_drm_of_ids,
517 		.pm     = &mtk_drm_pm_ops,
518 	},
519 };
520 
521 static struct platform_driver * const mtk_drm_drivers[] = {
522 	&mtk_ddp_driver,
523 	&mtk_disp_ovl_driver,
524 	&mtk_disp_rdma_driver,
525 	&mtk_dpi_driver,
526 	&mtk_drm_platform_driver,
527 	&mtk_dsi_driver,
528 	&mtk_mipi_tx_driver,
529 };
530 
531 static int __init mtk_drm_init(void)
532 {
533 	int ret;
534 	int i;
535 
536 	for (i = 0; i < ARRAY_SIZE(mtk_drm_drivers); i++) {
537 		ret = platform_driver_register(mtk_drm_drivers[i]);
538 		if (ret < 0) {
539 			pr_err("Failed to register %s driver: %d\n",
540 			       mtk_drm_drivers[i]->driver.name, ret);
541 			goto err;
542 		}
543 	}
544 
545 	return 0;
546 
547 err:
548 	while (--i >= 0)
549 		platform_driver_unregister(mtk_drm_drivers[i]);
550 
551 	return ret;
552 }
553 
554 static void __exit mtk_drm_exit(void)
555 {
556 	int i;
557 
558 	for (i = ARRAY_SIZE(mtk_drm_drivers) - 1; i >= 0; i--)
559 		platform_driver_unregister(mtk_drm_drivers[i]);
560 }
561 
562 module_init(mtk_drm_init);
563 module_exit(mtk_drm_exit);
564 
565 MODULE_AUTHOR("YT SHEN <yt.shen@mediatek.com>");
566 MODULE_DESCRIPTION("Mediatek SoC DRM driver");
567 MODULE_LICENSE("GPL v2");
568