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