1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
4  */
5 
6 #include <linux/component.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 
10 #include <drm/drm_of.h>
11 #include <drm/drmP.h>
12 #include <drm/drm_crtc_helper.h>
13 
14 #include "sun8i_dw_hdmi.h"
15 #include "sun8i_tcon_top.h"
16 
17 static void sun8i_dw_hdmi_encoder_mode_set(struct drm_encoder *encoder,
18 					   struct drm_display_mode *mode,
19 					   struct drm_display_mode *adj_mode)
20 {
21 	struct sun8i_dw_hdmi *hdmi = encoder_to_sun8i_dw_hdmi(encoder);
22 
23 	clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
24 }
25 
26 static const struct drm_encoder_helper_funcs
27 sun8i_dw_hdmi_encoder_helper_funcs = {
28 	.mode_set = sun8i_dw_hdmi_encoder_mode_set,
29 };
30 
31 static const struct drm_encoder_funcs sun8i_dw_hdmi_encoder_funcs = {
32 	.destroy = drm_encoder_cleanup,
33 };
34 
35 static enum drm_mode_status
36 sun8i_dw_hdmi_mode_valid(struct drm_connector *connector,
37 			 const struct drm_display_mode *mode)
38 {
39 	if (mode->clock > 297000)
40 		return MODE_CLOCK_HIGH;
41 
42 	return MODE_OK;
43 }
44 
45 static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node *node)
46 {
47 	return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
48 		!!of_match_node(sun8i_tcon_top_of_table, node);
49 }
50 
51 static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm,
52 					     struct device_node *node)
53 {
54 	struct device_node *port, *ep, *remote, *remote_port;
55 	u32 crtcs = 0;
56 
57 	remote = of_graph_get_remote_node(node, 0, -1);
58 	if (!remote)
59 		return 0;
60 
61 	if (sun8i_dw_hdmi_node_is_tcon_top(remote)) {
62 		port = of_graph_get_port_by_id(remote, 4);
63 		if (!port)
64 			goto crtcs_exit;
65 
66 		for_each_child_of_node(port, ep) {
67 			remote_port = of_graph_get_remote_port(ep);
68 			if (remote_port) {
69 				crtcs |= drm_of_crtc_port_mask(drm, remote_port);
70 				of_node_put(remote_port);
71 			}
72 		}
73 	} else {
74 		crtcs = drm_of_find_possible_crtcs(drm, node);
75 	}
76 
77 crtcs_exit:
78 	of_node_put(remote);
79 
80 	return crtcs;
81 }
82 
83 static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
84 			      void *data)
85 {
86 	struct platform_device *pdev = to_platform_device(dev);
87 	struct dw_hdmi_plat_data *plat_data;
88 	struct drm_device *drm = data;
89 	struct device_node *phy_node;
90 	struct drm_encoder *encoder;
91 	struct sun8i_dw_hdmi *hdmi;
92 	int ret;
93 
94 	if (!pdev->dev.of_node)
95 		return -ENODEV;
96 
97 	hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
98 	if (!hdmi)
99 		return -ENOMEM;
100 
101 	plat_data = &hdmi->plat_data;
102 	hdmi->dev = &pdev->dev;
103 	encoder = &hdmi->encoder;
104 
105 	encoder->possible_crtcs =
106 		sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node);
107 	/*
108 	 * If we failed to find the CRTC(s) which this encoder is
109 	 * supposed to be connected to, it's because the CRTC has
110 	 * not been registered yet.  Defer probing, and hope that
111 	 * the required CRTC is added later.
112 	 */
113 	if (encoder->possible_crtcs == 0)
114 		return -EPROBE_DEFER;
115 
116 	hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
117 	if (IS_ERR(hdmi->rst_ctrl)) {
118 		dev_err(dev, "Could not get ctrl reset control\n");
119 		return PTR_ERR(hdmi->rst_ctrl);
120 	}
121 
122 	hdmi->clk_tmds = devm_clk_get(dev, "tmds");
123 	if (IS_ERR(hdmi->clk_tmds)) {
124 		dev_err(dev, "Couldn't get the tmds clock\n");
125 		return PTR_ERR(hdmi->clk_tmds);
126 	}
127 
128 	hdmi->regulator = devm_regulator_get(dev, "hvcc");
129 	if (IS_ERR(hdmi->regulator)) {
130 		dev_err(dev, "Couldn't get regulator\n");
131 		return PTR_ERR(hdmi->regulator);
132 	}
133 
134 	ret = regulator_enable(hdmi->regulator);
135 	if (ret) {
136 		dev_err(dev, "Failed to enable regulator\n");
137 		return ret;
138 	}
139 
140 	ret = reset_control_deassert(hdmi->rst_ctrl);
141 	if (ret) {
142 		dev_err(dev, "Could not deassert ctrl reset control\n");
143 		goto err_disable_regulator;
144 	}
145 
146 	ret = clk_prepare_enable(hdmi->clk_tmds);
147 	if (ret) {
148 		dev_err(dev, "Could not enable tmds clock\n");
149 		goto err_assert_ctrl_reset;
150 	}
151 
152 	phy_node = of_parse_phandle(dev->of_node, "phys", 0);
153 	if (!phy_node) {
154 		dev_err(dev, "Can't found PHY phandle\n");
155 		goto err_disable_clk_tmds;
156 	}
157 
158 	ret = sun8i_hdmi_phy_probe(hdmi, phy_node);
159 	of_node_put(phy_node);
160 	if (ret) {
161 		dev_err(dev, "Couldn't get the HDMI PHY\n");
162 		goto err_disable_clk_tmds;
163 	}
164 
165 	drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
166 	drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
167 			 DRM_MODE_ENCODER_TMDS, NULL);
168 
169 	sun8i_hdmi_phy_init(hdmi->phy);
170 
171 	plat_data->mode_valid = &sun8i_dw_hdmi_mode_valid;
172 	plat_data->phy_ops = sun8i_hdmi_phy_get_ops();
173 	plat_data->phy_name = "sun8i_dw_hdmi_phy";
174 	plat_data->phy_data = hdmi->phy;
175 
176 	platform_set_drvdata(pdev, hdmi);
177 
178 	hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
179 
180 	/*
181 	 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
182 	 * which would have called the encoder cleanup.  Do it manually.
183 	 */
184 	if (IS_ERR(hdmi->hdmi)) {
185 		ret = PTR_ERR(hdmi->hdmi);
186 		goto cleanup_encoder;
187 	}
188 
189 	return 0;
190 
191 cleanup_encoder:
192 	drm_encoder_cleanup(encoder);
193 	sun8i_hdmi_phy_remove(hdmi);
194 err_disable_clk_tmds:
195 	clk_disable_unprepare(hdmi->clk_tmds);
196 err_assert_ctrl_reset:
197 	reset_control_assert(hdmi->rst_ctrl);
198 err_disable_regulator:
199 	regulator_disable(hdmi->regulator);
200 
201 	return ret;
202 }
203 
204 static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
205 				 void *data)
206 {
207 	struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
208 
209 	dw_hdmi_unbind(hdmi->hdmi);
210 	sun8i_hdmi_phy_remove(hdmi);
211 	clk_disable_unprepare(hdmi->clk_tmds);
212 	reset_control_assert(hdmi->rst_ctrl);
213 	regulator_disable(hdmi->regulator);
214 }
215 
216 static const struct component_ops sun8i_dw_hdmi_ops = {
217 	.bind	= sun8i_dw_hdmi_bind,
218 	.unbind	= sun8i_dw_hdmi_unbind,
219 };
220 
221 static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
222 {
223 	return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
224 }
225 
226 static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
227 {
228 	component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
229 
230 	return 0;
231 }
232 
233 static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
234 	{ .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
235 	{ /* sentinel */ },
236 };
237 MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
238 
239 static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
240 	.probe  = sun8i_dw_hdmi_probe,
241 	.remove = sun8i_dw_hdmi_remove,
242 	.driver = {
243 		.name = "sun8i-dw-hdmi",
244 		.of_match_table = sun8i_dw_hdmi_dt_ids,
245 	},
246 };
247 module_platform_driver(sun8i_dw_hdmi_pltfm_driver);
248 
249 MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
250 MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
251 MODULE_LICENSE("GPL");
252