1 // SPDX-License-Identifier: GPL-2.0
3 * ZynqMP DisplayPort Subsystem Driver
5 * Copyright (C) 2017 - 2020 Xilinx, Inc.
8 * - Hyun Woo Kwon <hyun.kwon@xilinx.com>
9 * - Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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_module.h>
29 #include <drm/drm_probe_helper.h>
30 #include <drm/drm_vblank.h>
32 #include "zynqmp_disp.h"
33 #include "zynqmp_dp.h"
34 #include "zynqmp_dpsub.h"
36 /* -----------------------------------------------------------------------------
37 * Dumb Buffer & Framebuffer Allocation
40 static int zynqmp_dpsub_dumb_create(struct drm_file *file_priv,
41 struct drm_device *drm,
42 struct drm_mode_create_dumb *args)
44 struct zynqmp_dpsub *dpsub = to_zynqmp_dpsub(drm);
45 unsigned int pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
47 /* Enforce the alignment constraints of the DMA engine. */
48 args->pitch = ALIGN(pitch, dpsub->dma_align);
50 return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
53 static struct drm_framebuffer *
54 zynqmp_dpsub_fb_create(struct drm_device *drm, struct drm_file *file_priv,
55 const struct drm_mode_fb_cmd2 *mode_cmd)
57 struct zynqmp_dpsub *dpsub = to_zynqmp_dpsub(drm);
58 struct drm_mode_fb_cmd2 cmd = *mode_cmd;
61 /* Enforce the alignment constraints of the DMA engine. */
62 for (i = 0; i < ARRAY_SIZE(cmd.pitches); ++i)
63 cmd.pitches[i] = ALIGN(cmd.pitches[i], dpsub->dma_align);
65 return drm_gem_fb_create(drm, file_priv, &cmd);
68 static const struct drm_mode_config_funcs zynqmp_dpsub_mode_config_funcs = {
69 .fb_create = zynqmp_dpsub_fb_create,
70 .atomic_check = drm_atomic_helper_check,
71 .atomic_commit = drm_atomic_helper_commit,
74 /* -----------------------------------------------------------------------------
78 DEFINE_DRM_GEM_CMA_FOPS(zynqmp_dpsub_drm_fops);
80 static const struct drm_driver zynqmp_dpsub_drm_driver = {
81 .driver_features = DRIVER_MODESET | DRIVER_GEM |
84 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(zynqmp_dpsub_dumb_create),
86 .fops = &zynqmp_dpsub_drm_fops,
88 .name = "zynqmp-dpsub",
89 .desc = "Xilinx DisplayPort Subsystem Driver",
95 static int zynqmp_dpsub_drm_init(struct zynqmp_dpsub *dpsub)
97 struct drm_device *drm = &dpsub->drm;
100 /* Initialize mode config, vblank and the KMS poll helper. */
101 ret = drmm_mode_config_init(drm);
105 drm->mode_config.funcs = &zynqmp_dpsub_mode_config_funcs;
106 drm->mode_config.min_width = 0;
107 drm->mode_config.min_height = 0;
108 drm->mode_config.max_width = ZYNQMP_DISP_MAX_WIDTH;
109 drm->mode_config.max_height = ZYNQMP_DISP_MAX_HEIGHT;
111 ret = drm_vblank_init(drm, 1);
115 drm_kms_helper_poll_init(drm);
118 * Initialize the DISP and DP components. This will creates planes,
119 * CRTC, encoder and connector. The DISP should be initialized first as
120 * the DP encoder needs the CRTC.
122 ret = zynqmp_disp_drm_init(dpsub);
126 ret = zynqmp_dp_drm_init(dpsub);
130 /* Reset all components and register the DRM device. */
131 drm_mode_config_reset(drm);
133 ret = drm_dev_register(drm, 0);
137 /* Initialize fbdev generic emulation. */
138 drm_fbdev_generic_setup(drm, 24);
143 drm_kms_helper_poll_fini(drm);
147 /* -----------------------------------------------------------------------------
151 static int __maybe_unused zynqmp_dpsub_suspend(struct device *dev)
153 struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
155 return drm_mode_config_helper_suspend(&dpsub->drm);
158 static int __maybe_unused zynqmp_dpsub_resume(struct device *dev)
160 struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
162 return drm_mode_config_helper_resume(&dpsub->drm);
165 static const struct dev_pm_ops zynqmp_dpsub_pm_ops = {
166 SET_SYSTEM_SLEEP_PM_OPS(zynqmp_dpsub_suspend, zynqmp_dpsub_resume)
169 /* -----------------------------------------------------------------------------
173 static int zynqmp_dpsub_init_clocks(struct zynqmp_dpsub *dpsub)
177 dpsub->apb_clk = devm_clk_get(dpsub->dev, "dp_apb_clk");
178 if (IS_ERR(dpsub->apb_clk))
179 return PTR_ERR(dpsub->apb_clk);
181 ret = clk_prepare_enable(dpsub->apb_clk);
183 dev_err(dpsub->dev, "failed to enable the APB clock\n");
190 static int zynqmp_dpsub_probe(struct platform_device *pdev)
192 struct zynqmp_dpsub *dpsub;
195 /* Allocate private data. */
196 dpsub = devm_drm_dev_alloc(&pdev->dev, &zynqmp_dpsub_drm_driver,
197 struct zynqmp_dpsub, drm);
199 return PTR_ERR(dpsub);
201 dpsub->dev = &pdev->dev;
202 platform_set_drvdata(pdev, dpsub);
204 dma_set_mask(dpsub->dev, DMA_BIT_MASK(ZYNQMP_DISP_MAX_DMA_BIT));
206 /* Try the reserved memory. Proceed if there's none. */
207 of_reserved_mem_device_init(&pdev->dev);
209 ret = zynqmp_dpsub_init_clocks(dpsub);
213 pm_runtime_enable(&pdev->dev);
216 * DP should be probed first so that the zynqmp_disp can set the output
217 * format accordingly.
219 ret = zynqmp_dp_probe(dpsub, &dpsub->drm);
223 ret = zynqmp_disp_probe(dpsub, &dpsub->drm);
227 ret = zynqmp_dpsub_drm_init(dpsub);
231 dev_info(&pdev->dev, "ZynqMP DisplayPort Subsystem driver probed");
236 zynqmp_disp_remove(dpsub);
238 zynqmp_dp_remove(dpsub);
240 pm_runtime_disable(&pdev->dev);
241 clk_disable_unprepare(dpsub->apb_clk);
243 of_reserved_mem_device_release(&pdev->dev);
247 static int zynqmp_dpsub_remove(struct platform_device *pdev)
249 struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
250 struct drm_device *drm = &dpsub->drm;
252 drm_dev_unregister(drm);
253 drm_atomic_helper_shutdown(drm);
254 drm_kms_helper_poll_fini(drm);
256 zynqmp_disp_remove(dpsub);
257 zynqmp_dp_remove(dpsub);
259 pm_runtime_disable(&pdev->dev);
260 clk_disable_unprepare(dpsub->apb_clk);
261 of_reserved_mem_device_release(&pdev->dev);
266 static void zynqmp_dpsub_shutdown(struct platform_device *pdev)
268 struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
270 drm_atomic_helper_shutdown(&dpsub->drm);
273 static const struct of_device_id zynqmp_dpsub_of_match[] = {
274 { .compatible = "xlnx,zynqmp-dpsub-1.7", },
275 { /* end of table */ },
277 MODULE_DEVICE_TABLE(of, zynqmp_dpsub_of_match);
279 static struct platform_driver zynqmp_dpsub_driver = {
280 .probe = zynqmp_dpsub_probe,
281 .remove = zynqmp_dpsub_remove,
282 .shutdown = zynqmp_dpsub_shutdown,
284 .name = "zynqmp-dpsub",
285 .pm = &zynqmp_dpsub_pm_ops,
286 .of_match_table = zynqmp_dpsub_of_match,
290 drm_module_platform_driver(zynqmp_dpsub_driver);
292 MODULE_AUTHOR("Xilinx, Inc.");
293 MODULE_DESCRIPTION("ZynqMP DP Subsystem Driver");
294 MODULE_LICENSE("GPL v2");