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/platform_device.h>
10 #include <drm/drm_of.h>
12 #include <drm/drm_crtc_helper.h>
14 #include "sun8i_dw_hdmi.h"
15 #include "sun8i_tcon_top.h"
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
)
21 struct sun8i_dw_hdmi
*hdmi
= encoder_to_sun8i_dw_hdmi(encoder
);
23 clk_set_rate(hdmi
->clk_tmds
, mode
->crtc_clock
* 1000);
26 static const struct drm_encoder_helper_funcs
27 sun8i_dw_hdmi_encoder_helper_funcs
= {
28 .mode_set
= sun8i_dw_hdmi_encoder_mode_set
,
31 static const struct drm_encoder_funcs sun8i_dw_hdmi_encoder_funcs
= {
32 .destroy
= drm_encoder_cleanup
,
35 static enum drm_mode_status
36 sun8i_dw_hdmi_mode_valid(struct drm_connector
*connector
,
37 const struct drm_display_mode
*mode
)
39 if (mode
->clock
> 297000)
40 return MODE_CLOCK_HIGH
;
45 static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node
*node
)
47 return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP
) &&
48 !!of_match_node(sun8i_tcon_top_of_table
, node
);
51 static u32
sun8i_dw_hdmi_find_possible_crtcs(struct drm_device
*drm
,
52 struct device_node
*node
)
54 struct device_node
*port
, *ep
, *remote
, *remote_port
;
57 remote
= of_graph_get_remote_node(node
, 0, -1);
61 if (sun8i_dw_hdmi_node_is_tcon_top(remote
)) {
62 port
= of_graph_get_port_by_id(remote
, 4);
66 for_each_child_of_node(port
, ep
) {
67 remote_port
= of_graph_get_remote_port(ep
);
69 crtcs
|= drm_of_crtc_port_mask(drm
, remote_port
);
70 of_node_put(remote_port
);
74 crtcs
= drm_of_find_possible_crtcs(drm
, node
);
83 static int sun8i_dw_hdmi_bind(struct device
*dev
, struct device
*master
,
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
;
94 if (!pdev
->dev
.of_node
)
97 hdmi
= devm_kzalloc(&pdev
->dev
, sizeof(*hdmi
), GFP_KERNEL
);
101 plat_data
= &hdmi
->plat_data
;
102 hdmi
->dev
= &pdev
->dev
;
103 encoder
= &hdmi
->encoder
;
105 encoder
->possible_crtcs
=
106 sun8i_dw_hdmi_find_possible_crtcs(drm
, dev
->of_node
);
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.
113 if (encoder
->possible_crtcs
== 0)
114 return -EPROBE_DEFER
;
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
);
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
);
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
);
134 ret
= regulator_enable(hdmi
->regulator
);
136 dev_err(dev
, "Failed to enable regulator\n");
140 ret
= reset_control_deassert(hdmi
->rst_ctrl
);
142 dev_err(dev
, "Could not deassert ctrl reset control\n");
143 goto err_disable_regulator
;
146 ret
= clk_prepare_enable(hdmi
->clk_tmds
);
148 dev_err(dev
, "Could not enable tmds clock\n");
149 goto err_assert_ctrl_reset
;
152 phy_node
= of_parse_phandle(dev
->of_node
, "phys", 0);
154 dev_err(dev
, "Can't found PHY phandle\n");
155 goto err_disable_clk_tmds
;
158 ret
= sun8i_hdmi_phy_probe(hdmi
, phy_node
);
159 of_node_put(phy_node
);
161 dev_err(dev
, "Couldn't get the HDMI PHY\n");
162 goto err_disable_clk_tmds
;
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
);
169 sun8i_hdmi_phy_init(hdmi
->phy
);
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
;
176 platform_set_drvdata(pdev
, hdmi
);
178 hdmi
->hdmi
= dw_hdmi_bind(pdev
, encoder
, plat_data
);
181 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
182 * which would have called the encoder cleanup. Do it manually.
184 if (IS_ERR(hdmi
->hdmi
)) {
185 ret
= PTR_ERR(hdmi
->hdmi
);
186 goto 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
);
204 static void sun8i_dw_hdmi_unbind(struct device
*dev
, struct device
*master
,
207 struct sun8i_dw_hdmi
*hdmi
= dev_get_drvdata(dev
);
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
);
216 static const struct component_ops sun8i_dw_hdmi_ops
= {
217 .bind
= sun8i_dw_hdmi_bind
,
218 .unbind
= sun8i_dw_hdmi_unbind
,
221 static int sun8i_dw_hdmi_probe(struct platform_device
*pdev
)
223 return component_add(&pdev
->dev
, &sun8i_dw_hdmi_ops
);
226 static int sun8i_dw_hdmi_remove(struct platform_device
*pdev
)
228 component_del(&pdev
->dev
, &sun8i_dw_hdmi_ops
);
233 static const struct of_device_id sun8i_dw_hdmi_dt_ids
[] = {
234 { .compatible
= "allwinner,sun8i-a83t-dw-hdmi" },
237 MODULE_DEVICE_TABLE(of
, sun8i_dw_hdmi_dt_ids
);
239 static struct platform_driver sun8i_dw_hdmi_pltfm_driver
= {
240 .probe
= sun8i_dw_hdmi_probe
,
241 .remove
= sun8i_dw_hdmi_remove
,
243 .name
= "sun8i-dw-hdmi",
244 .of_match_table
= sun8i_dw_hdmi_dt_ids
,
247 module_platform_driver(sun8i_dw_hdmi_pltfm_driver
);
249 MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
250 MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
251 MODULE_LICENSE("GPL");