xref: /linux/drivers/gpu/drm/exynos/exynos_dp.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
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_panel.h>
30 #include <drm/drm_print.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/exynos_drm.h>
33 
34 #include "exynos_drm_crtc.h"
35 
36 #define to_dp(nm)	container_of(nm, struct exynos_dp_device, nm)
37 
38 struct exynos_dp_device {
39 	struct drm_encoder         encoder;
40 	struct drm_device          *drm_dev;
41 	struct device              *dev;
42 
43 	struct analogix_dp_device *adp;
44 	struct analogix_dp_plat_data plat_data;
45 };
46 
47 static int exynos_dp_crtc_clock_enable(struct analogix_dp_plat_data *plat_data,
48 				bool enable)
49 {
50 	struct exynos_dp_device *dp = to_dp(plat_data);
51 	struct drm_encoder *encoder = &dp->encoder;
52 
53 	if (!encoder->crtc)
54 		return -EPERM;
55 
56 	exynos_drm_pipe_clk_enable(to_exynos_crtc(encoder->crtc), enable);
57 
58 	return 0;
59 }
60 
61 static int exynos_dp_poweron(struct analogix_dp_plat_data *plat_data)
62 {
63 	return exynos_dp_crtc_clock_enable(plat_data, true);
64 }
65 
66 static int exynos_dp_poweroff(struct analogix_dp_plat_data *plat_data)
67 {
68 	return exynos_dp_crtc_clock_enable(plat_data, false);
69 }
70 
71 static void exynos_dp_mode_set(struct drm_encoder *encoder,
72 			       struct drm_display_mode *mode,
73 			       struct drm_display_mode *adjusted_mode)
74 {
75 }
76 
77 static void exynos_dp_nop(struct drm_encoder *encoder)
78 {
79 	/* do nothing */
80 }
81 
82 static const struct drm_encoder_funcs exynos_dp_encoder_funcs = {
83 	.destroy = drm_encoder_cleanup,
84 };
85 
86 static const struct drm_encoder_helper_funcs exynos_dp_encoder_helper_funcs = {
87 	.mode_set = exynos_dp_mode_set,
88 	.enable = exynos_dp_nop,
89 	.disable = exynos_dp_nop,
90 };
91 
92 static int exynos_dp_bind(struct device *dev, struct device *master, void *data)
93 {
94 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
95 	struct drm_encoder *encoder = &dp->encoder;
96 	struct drm_device *drm_dev = data;
97 	struct drm_connector *connector;
98 	int ret;
99 
100 	dp->drm_dev = drm_dev;
101 
102 	ret = drm_encoder_init(drm_dev, encoder, &exynos_dp_encoder_funcs,
103 			       DRM_MODE_ENCODER_TMDS, NULL);
104 	if (ret) {
105 		dev_err(dp->dev, "Failed to initialize encoder\n");
106 		return ret;
107 	}
108 
109 	drm_encoder_helper_add(encoder, &exynos_dp_encoder_helper_funcs);
110 
111 	ret = exynos_drm_set_possible_crtcs(encoder, EXYNOS_DISPLAY_TYPE_LCD);
112 	if (ret < 0)
113 		return ret;
114 
115 	dp->plat_data.encoder = encoder;
116 
117 	ret = analogix_dp_bind(dp->adp, dp->drm_dev);
118 	if (ret) {
119 		dp->encoder.funcs->destroy(&dp->encoder);
120 		return ret;
121 	}
122 
123 	connector = drm_bridge_connector_init(dp->drm_dev, dp->plat_data.encoder);
124 	if (IS_ERR(connector)) {
125 		ret = PTR_ERR(connector);
126 		dev_err(dp->dev, "Failed to initialize bridge_connector\n");
127 		return ret;
128 	}
129 
130 	return 0;
131 }
132 
133 static void exynos_dp_unbind(struct device *dev, struct device *master,
134 			     void *data)
135 {
136 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
137 
138 	analogix_dp_unbind(dp->adp);
139 	dp->encoder.funcs->destroy(&dp->encoder);
140 }
141 
142 static const struct component_ops exynos_dp_ops = {
143 	.bind	= exynos_dp_bind,
144 	.unbind	= exynos_dp_unbind,
145 };
146 
147 static int exynos_dp_probe(struct platform_device *pdev)
148 {
149 	struct device *dev = &pdev->dev;
150 	struct device_node *np;
151 	struct exynos_dp_device *dp;
152 
153 	dp = devm_kzalloc(&pdev->dev, sizeof(struct exynos_dp_device),
154 			  GFP_KERNEL);
155 	if (!dp)
156 		return -ENOMEM;
157 
158 	dp->dev = dev;
159 	/*
160 	 * We just use the drvdata until driver run into component
161 	 * add function, and then we would set drvdata to null, so
162 	 * that analogix dp driver would take charge of the drvdata.
163 	 */
164 	platform_set_drvdata(pdev, dp);
165 
166 	/* This is for the backward compatibility. */
167 	np = of_parse_phandle(dev->of_node, "panel", 0);
168 	if (np) {
169 		dp->plat_data.panel = of_drm_find_panel(np);
170 
171 		of_node_put(np);
172 		if (IS_ERR(dp->plat_data.panel))
173 			return PTR_ERR(dp->plat_data.panel);
174 
175 		goto out;
176 	}
177 
178 	if (of_get_display_timings(dev->of_node)) {
179 		dp->plat_data.next_bridge = devm_drm_of_display_mode_bridge(dp->dev,
180 									dp->dev->of_node,
181 									DRM_MODE_CONNECTOR_eDP);
182 		if (IS_ERR(dp->plat_data.next_bridge))
183 			return PTR_ERR(dp->plat_data.next_bridge);
184 	}
185 
186 	/* The remote port can be either a panel or a bridge */
187 	dp->plat_data.dev_type = EXYNOS_DP;
188 	dp->plat_data.power_on = exynos_dp_poweron;
189 	dp->plat_data.power_off = exynos_dp_poweroff;
190 	dp->plat_data.ops = &exynos_dp_ops;
191 
192 out:
193 	dp->adp = analogix_dp_probe(dev, &dp->plat_data);
194 	if (IS_ERR(dp->adp)) {
195 		/*
196 		 * The driver core does not invoke remove() for failed probes,
197 		 * so release the probe-time panel reference here.
198 		 */
199 		if (dp->plat_data.panel)
200 			drm_panel_put(dp->plat_data.panel);
201 		return PTR_ERR(dp->adp);
202 	}
203 
204 	if (dp->plat_data.panel || dp->plat_data.next_bridge)
205 		return component_add(&pdev->dev, &exynos_dp_ops);
206 	else
207 		return analogix_dp_finish_probe(dp->adp);
208 }
209 
210 static void exynos_dp_remove(struct platform_device *pdev)
211 {
212 	struct exynos_dp_device *dp = platform_get_drvdata(pdev);
213 
214 	/*
215 	 * Release the probe-time reference from of_drm_find_panel(). If bind
216 	 * ran, the panel_bridge holds a second reference that devm cleanup
217 	 * will release when the bridge is destroyed after remove() returns.
218 	 */
219 	if (dp->plat_data.panel)
220 		drm_panel_put(dp->plat_data.panel);
221 
222 	component_del(&pdev->dev, &exynos_dp_ops);
223 }
224 
225 static int exynos_dp_suspend(struct device *dev)
226 {
227 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
228 
229 	return analogix_dp_suspend(dp->adp);
230 }
231 
232 static int exynos_dp_resume(struct device *dev)
233 {
234 	struct exynos_dp_device *dp = dev_get_drvdata(dev);
235 
236 	return analogix_dp_resume(dp->adp);
237 }
238 
239 static DEFINE_RUNTIME_DEV_PM_OPS(exynos_dp_pm_ops, exynos_dp_suspend,
240 				 exynos_dp_resume, NULL);
241 
242 static const struct of_device_id exynos_dp_match[] = {
243 	{ .compatible = "samsung,exynos5-dp" },
244 	{},
245 };
246 MODULE_DEVICE_TABLE(of, exynos_dp_match);
247 
248 struct platform_driver dp_driver = {
249 	.probe		= exynos_dp_probe,
250 	.remove		= exynos_dp_remove,
251 	.driver		= {
252 		.name	= "exynos-dp",
253 		.pm	= pm_ptr(&exynos_dp_pm_ops),
254 		.of_match_table = exynos_dp_match,
255 	},
256 };
257 
258 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
259 MODULE_DESCRIPTION("Samsung Specific Analogix-DP Driver Extension");
260 MODULE_LICENSE("GPL v2");
261