1 // SPDX-License-Identifier: GPL-2.0
3 * ZynqMP DisplayPort Subsystem Driver
5 * Copyright (C) 2017 - 2020 Xilinx, Inc.
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/module.h>
15 #include <linux/of_reserved_mem.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_device.h>
21 #include <drm/drm_drv.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_fourcc.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_gem_framebuffer_helper.h>
26 #include <drm/drm_managed.h>
27 #include <drm/drm_mode_config.h>
28 #include <drm/drm_probe_helper.h>
29 #include <drm/drm_vblank.h>
31 #include "zynqmp_disp.h"
32 #include "zynqmp_dp.h"
33 #include "zynqmp_dpsub.h"
35 /* -----------------------------------------------------------------------------
36 * Dumb Buffer & Framebuffer Allocation
39 static int zynqmp_dpsub_dumb_create(struct drm_file *file_priv,
40 struct drm_device *drm,
41 struct drm_mode_create_dumb *args)
43 struct zynqmp_dpsub *dpsub = to_zynqmp_dpsub(drm);
44 unsigned int pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
46 /* Enforce the alignment constraints of the DMA engine. */
47 args->pitch = ALIGN(pitch, dpsub->dma_align);
49 return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
52 static struct drm_framebuffer *
53 zynqmp_dpsub_fb_create(struct drm_device *drm, struct drm_file *file_priv,
54 const struct drm_mode_fb_cmd2 *mode_cmd)
56 struct zynqmp_dpsub *dpsub = to_zynqmp_dpsub(drm);
57 struct drm_mode_fb_cmd2 cmd = *mode_cmd;
60 /* Enforce the alignment constraints of the DMA engine. */
61 for (i = 0; i < ARRAY_SIZE(cmd.pitches); ++i)
62 cmd.pitches[i] = ALIGN(cmd.pitches[i], dpsub->dma_align);
64 return drm_gem_fb_create(drm, file_priv, &cmd);
67 static const struct drm_mode_config_funcs zynqmp_dpsub_mode_config_funcs = {
68 .fb_create = zynqmp_dpsub_fb_create,
69 .atomic_check = drm_atomic_helper_check,
70 .atomic_commit = drm_atomic_helper_commit,
73 /* -----------------------------------------------------------------------------
77 DEFINE_DRM_GEM_CMA_FOPS(zynqmp_dpsub_drm_fops);
79 static const struct drm_driver zynqmp_dpsub_drm_driver = {
80 .driver_features = DRIVER_MODESET | DRIVER_GEM |
83 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(zynqmp_dpsub_dumb_create),
85 .fops = &zynqmp_dpsub_drm_fops,
87 .name = "zynqmp-dpsub",
88 .desc = "Xilinx DisplayPort Subsystem Driver",
94 static int zynqmp_dpsub_drm_init(struct zynqmp_dpsub *dpsub)
96 struct drm_device *drm = &dpsub->drm;
99 /* Initialize mode config, vblank and the KMS poll helper. */
100 ret = drmm_mode_config_init(drm);
104 drm->mode_config.funcs = &zynqmp_dpsub_mode_config_funcs;
105 drm->mode_config.min_width = 0;
106 drm->mode_config.min_height = 0;
107 drm->mode_config.max_width = ZYNQMP_DISP_MAX_WIDTH;
108 drm->mode_config.max_height = ZYNQMP_DISP_MAX_HEIGHT;
110 ret = drm_vblank_init(drm, 1);
114 drm->irq_enabled = 1;
116 drm_kms_helper_poll_init(drm);
119 * Initialize the DISP and DP components. This will creates planes,
120 * CRTC, encoder and connector. The DISP should be initialized first as
121 * the DP encoder needs the CRTC.
123 ret = zynqmp_disp_drm_init(dpsub);
127 ret = zynqmp_dp_drm_init(dpsub);
131 /* Reset all components and register the DRM device. */
132 drm_mode_config_reset(drm);
134 ret = drm_dev_register(drm, 0);
138 /* Initialize fbdev generic emulation. */
139 drm_fbdev_generic_setup(drm, 24);
144 drm_kms_helper_poll_fini(drm);
148 /* -----------------------------------------------------------------------------
152 static int __maybe_unused zynqmp_dpsub_suspend(struct device *dev)
154 struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
156 return drm_mode_config_helper_suspend(&dpsub->drm);
159 static int __maybe_unused zynqmp_dpsub_resume(struct device *dev)
161 struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
163 return drm_mode_config_helper_resume(&dpsub->drm);
166 static const struct dev_pm_ops zynqmp_dpsub_pm_ops = {
167 SET_SYSTEM_SLEEP_PM_OPS(zynqmp_dpsub_suspend, zynqmp_dpsub_resume)
170 /* -----------------------------------------------------------------------------
174 static int zynqmp_dpsub_init_clocks(struct zynqmp_dpsub *dpsub)
178 dpsub->apb_clk = devm_clk_get(dpsub->dev, "dp_apb_clk");
179 if (IS_ERR(dpsub->apb_clk))
180 return PTR_ERR(dpsub->apb_clk);
182 ret = clk_prepare_enable(dpsub->apb_clk);
184 dev_err(dpsub->dev, "failed to enable the APB clock\n");
191 static int zynqmp_dpsub_probe(struct platform_device *pdev)
193 struct zynqmp_dpsub *dpsub;
196 /* Allocate private data. */
197 dpsub = devm_drm_dev_alloc(&pdev->dev, &zynqmp_dpsub_drm_driver,
198 struct zynqmp_dpsub, drm);
200 return PTR_ERR(dpsub);
202 dpsub->dev = &pdev->dev;
203 platform_set_drvdata(pdev, dpsub);
205 dma_set_mask(dpsub->dev, DMA_BIT_MASK(ZYNQMP_DISP_MAX_DMA_BIT));
207 /* Try the reserved memory. Proceed if there's none. */
208 of_reserved_mem_device_init(&pdev->dev);
210 ret = zynqmp_dpsub_init_clocks(dpsub);
214 pm_runtime_enable(&pdev->dev);
217 * DP should be probed first so that the zynqmp_disp can set the output
218 * format accordingly.
220 ret = zynqmp_dp_probe(dpsub, &dpsub->drm);
224 ret = zynqmp_disp_probe(dpsub, &dpsub->drm);
228 ret = zynqmp_dpsub_drm_init(dpsub);
232 dev_info(&pdev->dev, "ZynqMP DisplayPort Subsystem driver probed");
237 zynqmp_disp_remove(dpsub);
239 zynqmp_dp_remove(dpsub);
241 pm_runtime_disable(&pdev->dev);
242 clk_disable_unprepare(dpsub->apb_clk);
244 of_reserved_mem_device_release(&pdev->dev);
248 static int zynqmp_dpsub_remove(struct platform_device *pdev)
250 struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
251 struct drm_device *drm = &dpsub->drm;
253 drm_dev_unregister(drm);
254 drm_atomic_helper_shutdown(drm);
255 drm_kms_helper_poll_fini(drm);
257 zynqmp_disp_remove(dpsub);
258 zynqmp_dp_remove(dpsub);
260 pm_runtime_disable(&pdev->dev);
261 clk_disable_unprepare(dpsub->apb_clk);
262 of_reserved_mem_device_release(&pdev->dev);
267 static void zynqmp_dpsub_shutdown(struct platform_device *pdev)
269 struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
271 drm_atomic_helper_shutdown(&dpsub->drm);
274 static const struct of_device_id zynqmp_dpsub_of_match[] = {
275 { .compatible = "xlnx,zynqmp-dpsub-1.7", },
276 { /* end of table */ },
278 MODULE_DEVICE_TABLE(of, zynqmp_dpsub_of_match);
280 static struct platform_driver zynqmp_dpsub_driver = {
281 .probe = zynqmp_dpsub_probe,
282 .remove = zynqmp_dpsub_remove,
283 .shutdown = zynqmp_dpsub_shutdown,
285 .name = "zynqmp-dpsub",
286 .pm = &zynqmp_dpsub_pm_ops,
287 .of_match_table = zynqmp_dpsub_of_match,
291 module_platform_driver(zynqmp_dpsub_driver);
293 MODULE_AUTHOR("Xilinx, Inc.");
294 MODULE_DESCRIPTION("ZynqMP DP Subsystem Driver");
295 MODULE_LICENSE("GPL v2");