xref: /linux/drivers/gpu/drm/exynos/exynos_dp.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Samsung SoC DP (Display Port) interface driver.
4  *
5  * Copyright (C) 2012 Samsung Electronics Co., Ltd.
6  * Author: Jingoo Han <jg1.han@samsung.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/component.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_graph.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <video/of_display_timing.h>
18 #include <video/of_videomode.h>
19 #include <video/videomode.h>
20 
21 #include <drm/bridge/analogix_dp.h>
22 #include <drm/bridge/of-display-mode-bridge.h>
23 #include <drm/drm_atomic_helper.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_bridge_connector.h>
26 #include <drm/drm_crtc.h>
27 #include <drm/drm_encoder.h>
28 #include <drm/drm_of.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_probe_helper.h>
31 #include <drm/exynos_drm.h>
32 
33 #include "exynos_drm_crtc.h"
34 
35 #define to_dp(nm)	container_of(nm, struct exynos_dp_device, nm)
36 
37 struct exynos_dp_device {
38 	struct drm_encoder         encoder;
39 	struct drm_device          *drm_dev;
40 	struct device              *dev;
41 
42 	struct analogix_dp_device *adp;
43 	struct analogix_dp_plat_data plat_data;
44 };
45 
46 static int exynos_dp_crtc_clock_enable(struct analogix_dp_plat_data *plat_data,
47 				bool enable)
48 {
49 	struct exynos_dp_device *dp = to_dp(plat_data);
50 	struct drm_encoder *encoder = &dp->encoder;
51 
52 	if (!encoder->crtc)
53 		return -EPERM;
54 
55 	exynos_drm_pipe_clk_enable(to_exynos_crtc(encoder->crtc), enable);
56 
57 	return 0;
58 }
59 
60 static int exynos_dp_poweron(struct analogix_dp_plat_data *plat_data)
61 {
62 	return exynos_dp_crtc_clock_enable(plat_data, true);
63 }
64 
65 static int exynos_dp_poweroff(struct analogix_dp_plat_data *plat_data)
66 {
67 	return exynos_dp_crtc_clock_enable(plat_data, false);
68 }
69 
70 static void exynos_dp_mode_set(struct drm_encoder *encoder,
71 			       struct drm_display_mode *mode,
72 			       struct drm_display_mode *adjusted_mode)
73 {
74 }
75 
76 static void exynos_dp_nop(struct drm_encoder *encoder)
77 {
78 	/* do nothing */
79 }
80 
81 static const struct drm_encoder_funcs exynos_dp_encoder_funcs = {
82 	.destroy = drm_encoder_cleanup,
83 };
84 
85 static const struct drm_encoder_helper_funcs exynos_dp_encoder_helper_funcs = {
86 	.mode_set = exynos_dp_mode_set,
87 	.enable = exynos_dp_nop,
88 	.disable = exynos_dp_nop,
89 };
90 
91 static int exynos_dp_bind(struct device *dev, struct device *master, void *data)
92 {
93 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
94 	struct drm_encoder *encoder = &dp->encoder;
95 	struct drm_device *drm_dev = data;
96 	struct drm_connector *connector;
97 	int ret;
98 
99 	dp->drm_dev = drm_dev;
100 
101 	ret = drm_encoder_init(drm_dev, encoder, &exynos_dp_encoder_funcs,
102 			       DRM_MODE_ENCODER_TMDS, NULL);
103 	if (ret) {
104 		dev_err(dp->dev, "Failed to initialize encoder\n");
105 		return ret;
106 	}
107 
108 	drm_encoder_helper_add(encoder, &exynos_dp_encoder_helper_funcs);
109 
110 	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
111 	if (ret < 0)
112 		return ret;
113 
114 	dp->plat_data.encoder = encoder;
115 
116 	ret = analogix_dp_bind(dp->adp, dp->drm_dev);
117 	if (ret) {
118 		dp->encoder.funcs->destroy(&dp->encoder);
119 		return ret;
120 	}
121 
122 	connector = drm_bridge_connector_init(dp->drm_dev, dp->plat_data.encoder);
123 	if (IS_ERR(connector)) {
124 		ret = PTR_ERR(connector);
125 		dev_err(dp->dev, "Failed to initialize bridge_connector\n");
126 		return ret;
127 	}
128 
129 	return 0;
130 }
131 
132 static void exynos_dp_unbind(struct device *dev, struct device *master,
133 			     void *data)
134 {
135 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
136 
137 	analogix_dp_unbind(dp->adp);
138 	dp->encoder.funcs->destroy(&dp->encoder);
139 }
140 
141 static const struct component_ops exynos_dp_ops = {
142 	.bind	= exynos_dp_bind,
143 	.unbind	= exynos_dp_unbind,
144 };
145 
146 static int exynos_dp_probe(struct platform_device *pdev)
147 {
148 	struct device *dev = &pdev->dev;
149 	struct exynos_dp_device *dp;
150 
151 	dp = devm_kzalloc(&pdev->dev, sizeof(struct exynos_dp_device),
152 			  GFP_KERNEL);
153 	if (!dp)
154 		return -ENOMEM;
155 
156 	dp->dev = dev;
157 	/*
158 	 * We just use the drvdata until driver run into component
159 	 * add function, and then we would set drvdata to null, so
160 	 * that analogix dp driver would take charge of the drvdata.
161 	 */
162 	platform_set_drvdata(pdev, dp);
163 
164 	if (of_get_display_timings(dev->of_node)) {
165 		dp->plat_data.next_bridge = devm_drm_of_display_mode_bridge(dp->dev,
166 									dp->dev->of_node,
167 									DRM_MODE_CONNECTOR_eDP);
168 		if (IS_ERR(dp->plat_data.next_bridge))
169 			return PTR_ERR(dp->plat_data.next_bridge);
170 	}
171 
172 	/* The remote port can be either a panel or a bridge */
173 	dp->plat_data.dev_type = EXYNOS_DP;
174 	dp->plat_data.power_on = exynos_dp_poweron;
175 	dp->plat_data.power_off = exynos_dp_poweroff;
176 	dp->plat_data.ops = &exynos_dp_ops;
177 
178 	dp->adp = analogix_dp_probe(dev, &dp->plat_data);
179 	if (IS_ERR(dp->adp))
180 		return PTR_ERR(dp->adp);
181 
182 	if (dp->plat_data.next_bridge)
183 		return component_add(&pdev->dev, &exynos_dp_ops);
184 	else
185 		return analogix_dp_finish_probe(dp->adp);
186 }
187 
188 static void exynos_dp_remove(struct platform_device *pdev)
189 {
190 	component_del(&pdev->dev, &exynos_dp_ops);
191 }
192 
193 static int exynos_dp_suspend(struct device *dev)
194 {
195 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
196 
197 	return analogix_dp_suspend(dp->adp);
198 }
199 
200 static int exynos_dp_resume(struct device *dev)
201 {
202 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
203 
204 	return analogix_dp_resume(dp->adp);
205 }
206 
207 static DEFINE_RUNTIME_DEV_PM_OPS(exynos_dp_pm_ops, exynos_dp_suspend,
208 				 exynos_dp_resume, NULL);
209 
210 static const struct of_device_id exynos_dp_match[] = {
211 	{ .compatible = "samsung,exynos5-dp" },
212 	{},
213 };
214 MODULE_DEVICE_TABLE(of, exynos_dp_match);
215 
216 struct platform_driver dp_driver = {
217 	.probe		= exynos_dp_probe,
218 	.remove		= exynos_dp_remove,
219 	.driver		= {
220 		.name	= "exynos-dp",
221 		.pm	= pm_ptr(&exynos_dp_pm_ops),
222 		.of_match_table = exynos_dp_match,
223 	},
224 };
225 
226 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
227 MODULE_DESCRIPTION("Samsung Specific Analogix-DP Driver Extension");
228 MODULE_LICENSE("GPL v2");
229