1 // SPDX-License-Identifier: GPL-2.0+
6 #include <linux/component.h>
7 #include <linux/module.h>
8 #include <linux/of_device.h>
9 #include <linux/platform_device.h>
11 #include <drm/drm_crtc_helper.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_simple_kms_helper.h>
15 #include "sun8i_dw_hdmi.h"
16 #include "sun8i_tcon_top.h"
18 static void sun8i_dw_hdmi_encoder_mode_set(struct drm_encoder *encoder,
19 struct drm_display_mode *mode,
20 struct drm_display_mode *adj_mode)
22 struct sun8i_dw_hdmi *hdmi = encoder_to_sun8i_dw_hdmi(encoder);
24 clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
27 static const struct drm_encoder_helper_funcs
28 sun8i_dw_hdmi_encoder_helper_funcs = {
29 .mode_set = sun8i_dw_hdmi_encoder_mode_set,
32 static enum drm_mode_status
33 sun8i_dw_hdmi_mode_valid_a83t(struct dw_hdmi *hdmi, void *data,
34 const struct drm_display_info *info,
35 const struct drm_display_mode *mode)
37 if (mode->clock > 297000)
38 return MODE_CLOCK_HIGH;
43 static enum drm_mode_status
44 sun8i_dw_hdmi_mode_valid_h6(struct dw_hdmi *hdmi, void *data,
45 const struct drm_display_info *info,
46 const struct drm_display_mode *mode)
49 * Controller support maximum of 594 MHz, which correlates to
50 * 4K@60Hz 4:4:4 or RGB.
52 if (mode->clock > 594000)
53 return MODE_CLOCK_HIGH;
58 static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node *node)
60 return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
61 !!of_match_node(sun8i_tcon_top_of_table, node);
64 static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm,
65 struct device_node *node)
67 struct device_node *port, *ep, *remote, *remote_port;
70 remote = of_graph_get_remote_node(node, 0, -1);
74 if (sun8i_dw_hdmi_node_is_tcon_top(remote)) {
75 port = of_graph_get_port_by_id(remote, 4);
79 for_each_child_of_node(port, ep) {
80 remote_port = of_graph_get_remote_port(ep);
82 crtcs |= drm_of_crtc_port_mask(drm, remote_port);
83 of_node_put(remote_port);
87 crtcs = drm_of_find_possible_crtcs(drm, node);
96 static int sun8i_dw_hdmi_find_connector_pdev(struct device *dev,
97 struct platform_device **pdev_out)
99 struct platform_device *pdev;
100 struct device_node *remote;
102 remote = of_graph_get_remote_node(dev->of_node, 1, -1);
106 if (!of_device_is_compatible(remote, "hdmi-connector")) {
111 pdev = of_find_device_by_node(remote);
120 static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
123 struct platform_device *pdev = to_platform_device(dev), *connector_pdev;
124 struct dw_hdmi_plat_data *plat_data;
125 struct drm_device *drm = data;
126 struct device_node *phy_node;
127 struct drm_encoder *encoder;
128 struct sun8i_dw_hdmi *hdmi;
131 if (!pdev->dev.of_node)
134 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
138 plat_data = &hdmi->plat_data;
139 hdmi->dev = &pdev->dev;
140 encoder = &hdmi->encoder;
142 hdmi->quirks = of_device_get_match_data(dev);
144 encoder->possible_crtcs =
145 sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node);
147 * If we failed to find the CRTC(s) which this encoder is
148 * supposed to be connected to, it's because the CRTC has
149 * not been registered yet. Defer probing, and hope that
150 * the required CRTC is added later.
152 if (encoder->possible_crtcs == 0)
153 return -EPROBE_DEFER;
155 hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
156 if (IS_ERR(hdmi->rst_ctrl)) {
157 dev_err(dev, "Could not get ctrl reset control\n");
158 return PTR_ERR(hdmi->rst_ctrl);
161 hdmi->clk_tmds = devm_clk_get(dev, "tmds");
162 if (IS_ERR(hdmi->clk_tmds)) {
163 dev_err(dev, "Couldn't get the tmds clock\n");
164 return PTR_ERR(hdmi->clk_tmds);
167 hdmi->regulator = devm_regulator_get(dev, "hvcc");
168 if (IS_ERR(hdmi->regulator)) {
169 dev_err(dev, "Couldn't get regulator\n");
170 return PTR_ERR(hdmi->regulator);
173 ret = sun8i_dw_hdmi_find_connector_pdev(dev, &connector_pdev);
175 hdmi->ddc_en = gpiod_get_optional(&connector_pdev->dev,
176 "ddc-en", GPIOD_OUT_HIGH);
177 platform_device_put(connector_pdev);
179 if (IS_ERR(hdmi->ddc_en)) {
180 dev_err(dev, "Couldn't get ddc-en gpio\n");
181 return PTR_ERR(hdmi->ddc_en);
185 ret = regulator_enable(hdmi->regulator);
187 dev_err(dev, "Failed to enable regulator\n");
188 goto err_unref_ddc_en;
191 gpiod_set_value(hdmi->ddc_en, 1);
193 ret = reset_control_deassert(hdmi->rst_ctrl);
195 dev_err(dev, "Could not deassert ctrl reset control\n");
196 goto err_disable_ddc_en;
199 ret = clk_prepare_enable(hdmi->clk_tmds);
201 dev_err(dev, "Could not enable tmds clock\n");
202 goto err_assert_ctrl_reset;
205 phy_node = of_parse_phandle(dev->of_node, "phys", 0);
207 dev_err(dev, "Can't found PHY phandle\n");
209 goto err_disable_clk_tmds;
212 ret = sun8i_hdmi_phy_get(hdmi, phy_node);
213 of_node_put(phy_node);
215 dev_err(dev, "Couldn't get the HDMI PHY\n");
216 goto err_disable_clk_tmds;
219 drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
220 drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
222 sun8i_hdmi_phy_init(hdmi->phy);
224 plat_data->mode_valid = hdmi->quirks->mode_valid;
225 plat_data->use_drm_infoframe = hdmi->quirks->use_drm_infoframe;
226 sun8i_hdmi_phy_set_ops(hdmi->phy, plat_data);
228 platform_set_drvdata(pdev, hdmi);
230 hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
233 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
234 * which would have called the encoder cleanup. Do it manually.
236 if (IS_ERR(hdmi->hdmi)) {
237 ret = PTR_ERR(hdmi->hdmi);
238 goto cleanup_encoder;
244 drm_encoder_cleanup(encoder);
245 err_disable_clk_tmds:
246 clk_disable_unprepare(hdmi->clk_tmds);
247 err_assert_ctrl_reset:
248 reset_control_assert(hdmi->rst_ctrl);
250 gpiod_set_value(hdmi->ddc_en, 0);
251 regulator_disable(hdmi->regulator);
254 gpiod_put(hdmi->ddc_en);
259 static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
262 struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
264 dw_hdmi_unbind(hdmi->hdmi);
265 clk_disable_unprepare(hdmi->clk_tmds);
266 reset_control_assert(hdmi->rst_ctrl);
267 gpiod_set_value(hdmi->ddc_en, 0);
268 regulator_disable(hdmi->regulator);
271 gpiod_put(hdmi->ddc_en);
274 static const struct component_ops sun8i_dw_hdmi_ops = {
275 .bind = sun8i_dw_hdmi_bind,
276 .unbind = sun8i_dw_hdmi_unbind,
279 static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
281 return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
284 static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
286 component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
291 static const struct sun8i_dw_hdmi_quirks sun8i_a83t_quirks = {
292 .mode_valid = sun8i_dw_hdmi_mode_valid_a83t,
295 static const struct sun8i_dw_hdmi_quirks sun50i_h6_quirks = {
296 .mode_valid = sun8i_dw_hdmi_mode_valid_h6,
297 .use_drm_infoframe = true,
300 static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
302 .compatible = "allwinner,sun8i-a83t-dw-hdmi",
303 .data = &sun8i_a83t_quirks,
306 .compatible = "allwinner,sun50i-h6-dw-hdmi",
307 .data = &sun50i_h6_quirks,
311 MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
313 static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
314 .probe = sun8i_dw_hdmi_probe,
315 .remove = sun8i_dw_hdmi_remove,
317 .name = "sun8i-dw-hdmi",
318 .of_match_table = sun8i_dw_hdmi_dt_ids,
322 static int __init sun8i_dw_hdmi_init(void)
326 ret = platform_driver_register(&sun8i_dw_hdmi_pltfm_driver);
330 ret = platform_driver_register(&sun8i_hdmi_phy_driver);
332 platform_driver_unregister(&sun8i_dw_hdmi_pltfm_driver);
339 static void __exit sun8i_dw_hdmi_exit(void)
341 platform_driver_unregister(&sun8i_dw_hdmi_pltfm_driver);
342 platform_driver_unregister(&sun8i_hdmi_phy_driver);
345 module_init(sun8i_dw_hdmi_init);
346 module_exit(sun8i_dw_hdmi_exit);
349 MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
350 MODULE_LICENSE("GPL");