1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
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_of.h>
13 #include <drm/drm_crtc_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 if (hdmi->quirks->set_rate)
25 clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
28 static const struct drm_encoder_helper_funcs
29 sun8i_dw_hdmi_encoder_helper_funcs = {
30 .mode_set = sun8i_dw_hdmi_encoder_mode_set,
33 static const struct drm_encoder_funcs sun8i_dw_hdmi_encoder_funcs = {
34 .destroy = drm_encoder_cleanup,
37 static enum drm_mode_status
38 sun8i_dw_hdmi_mode_valid_a83t(struct drm_connector *connector,
39 const struct drm_display_mode *mode)
41 if (mode->clock > 297000)
42 return MODE_CLOCK_HIGH;
47 static enum drm_mode_status
48 sun8i_dw_hdmi_mode_valid_h6(struct drm_connector *connector,
49 const struct drm_display_mode *mode)
52 * Controller support maximum of 594 MHz, which correlates to
53 * 4K@60Hz 4:4:4 or RGB. However, for frequencies greater than
54 * 340 MHz scrambling has to be enabled. Because scrambling is
55 * not yet implemented, just limit to 340 MHz for now.
57 if (mode->clock > 340000)
58 return MODE_CLOCK_HIGH;
63 static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node *node)
65 return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
66 !!of_match_node(sun8i_tcon_top_of_table, node);
69 static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm,
70 struct device_node *node)
72 struct device_node *port, *ep, *remote, *remote_port;
75 remote = of_graph_get_remote_node(node, 0, -1);
79 if (sun8i_dw_hdmi_node_is_tcon_top(remote)) {
80 port = of_graph_get_port_by_id(remote, 4);
84 for_each_child_of_node(port, ep) {
85 remote_port = of_graph_get_remote_port(ep);
87 crtcs |= drm_of_crtc_port_mask(drm, remote_port);
88 of_node_put(remote_port);
92 crtcs = drm_of_find_possible_crtcs(drm, node);
101 static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
104 struct platform_device *pdev = to_platform_device(dev);
105 struct dw_hdmi_plat_data *plat_data;
106 struct drm_device *drm = data;
107 struct device_node *phy_node;
108 struct drm_encoder *encoder;
109 struct sun8i_dw_hdmi *hdmi;
112 if (!pdev->dev.of_node)
115 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
119 plat_data = &hdmi->plat_data;
120 hdmi->dev = &pdev->dev;
121 encoder = &hdmi->encoder;
123 hdmi->quirks = of_device_get_match_data(dev);
125 encoder->possible_crtcs =
126 sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node);
128 * If we failed to find the CRTC(s) which this encoder is
129 * supposed to be connected to, it's because the CRTC has
130 * not been registered yet. Defer probing, and hope that
131 * the required CRTC is added later.
133 if (encoder->possible_crtcs == 0)
134 return -EPROBE_DEFER;
136 hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
137 if (IS_ERR(hdmi->rst_ctrl)) {
138 dev_err(dev, "Could not get ctrl reset control\n");
139 return PTR_ERR(hdmi->rst_ctrl);
142 hdmi->clk_tmds = devm_clk_get(dev, "tmds");
143 if (IS_ERR(hdmi->clk_tmds)) {
144 dev_err(dev, "Couldn't get the tmds clock\n");
145 return PTR_ERR(hdmi->clk_tmds);
148 hdmi->regulator = devm_regulator_get(dev, "hvcc");
149 if (IS_ERR(hdmi->regulator)) {
150 dev_err(dev, "Couldn't get regulator\n");
151 return PTR_ERR(hdmi->regulator);
154 ret = regulator_enable(hdmi->regulator);
156 dev_err(dev, "Failed to enable regulator\n");
160 ret = reset_control_deassert(hdmi->rst_ctrl);
162 dev_err(dev, "Could not deassert ctrl reset control\n");
163 goto err_disable_regulator;
166 ret = clk_prepare_enable(hdmi->clk_tmds);
168 dev_err(dev, "Could not enable tmds clock\n");
169 goto err_assert_ctrl_reset;
172 phy_node = of_parse_phandle(dev->of_node, "phys", 0);
174 dev_err(dev, "Can't found PHY phandle\n");
175 goto err_disable_clk_tmds;
178 ret = sun8i_hdmi_phy_probe(hdmi, phy_node);
179 of_node_put(phy_node);
181 dev_err(dev, "Couldn't get the HDMI PHY\n");
182 goto err_disable_clk_tmds;
185 drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
186 drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
187 DRM_MODE_ENCODER_TMDS, NULL);
189 sun8i_hdmi_phy_init(hdmi->phy);
191 plat_data->mode_valid = hdmi->quirks->mode_valid;
192 sun8i_hdmi_phy_set_ops(hdmi->phy, plat_data);
194 platform_set_drvdata(pdev, hdmi);
196 hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
199 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
200 * which would have called the encoder cleanup. Do it manually.
202 if (IS_ERR(hdmi->hdmi)) {
203 ret = PTR_ERR(hdmi->hdmi);
204 goto cleanup_encoder;
210 drm_encoder_cleanup(encoder);
211 sun8i_hdmi_phy_remove(hdmi);
212 err_disable_clk_tmds:
213 clk_disable_unprepare(hdmi->clk_tmds);
214 err_assert_ctrl_reset:
215 reset_control_assert(hdmi->rst_ctrl);
216 err_disable_regulator:
217 regulator_disable(hdmi->regulator);
222 static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
225 struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
227 dw_hdmi_unbind(hdmi->hdmi);
228 sun8i_hdmi_phy_remove(hdmi);
229 clk_disable_unprepare(hdmi->clk_tmds);
230 reset_control_assert(hdmi->rst_ctrl);
231 regulator_disable(hdmi->regulator);
234 static const struct component_ops sun8i_dw_hdmi_ops = {
235 .bind = sun8i_dw_hdmi_bind,
236 .unbind = sun8i_dw_hdmi_unbind,
239 static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
241 return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
244 static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
246 component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
251 static const struct sun8i_dw_hdmi_quirks sun8i_a83t_quirks = {
252 .mode_valid = sun8i_dw_hdmi_mode_valid_a83t,
256 static const struct sun8i_dw_hdmi_quirks sun50i_h6_quirks = {
257 .mode_valid = sun8i_dw_hdmi_mode_valid_h6,
260 static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
262 .compatible = "allwinner,sun8i-a83t-dw-hdmi",
263 .data = &sun8i_a83t_quirks,
266 .compatible = "allwinner,sun50i-h6-dw-hdmi",
267 .data = &sun50i_h6_quirks,
271 MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
273 static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
274 .probe = sun8i_dw_hdmi_probe,
275 .remove = sun8i_dw_hdmi_remove,
277 .name = "sun8i-dw-hdmi",
278 .of_match_table = sun8i_dw_hdmi_dt_ids,
281 module_platform_driver(sun8i_dw_hdmi_pltfm_driver);
283 MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
284 MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
285 MODULE_LICENSE("GPL");