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 ret
= reset_control_deassert(hdmi
->rst_ctrl
);
130 dev_err(dev
, "Could not deassert ctrl reset control\n");
134 ret
= clk_prepare_enable(hdmi
->clk_tmds
);
136 dev_err(dev
, "Could not enable tmds clock\n");
137 goto err_assert_ctrl_reset
;
140 phy_node
= of_parse_phandle(dev
->of_node
, "phys", 0);
142 dev_err(dev
, "Can't found PHY phandle\n");
143 goto err_disable_clk_tmds
;
146 ret
= sun8i_hdmi_phy_probe(hdmi
, phy_node
);
147 of_node_put(phy_node
);
149 dev_err(dev
, "Couldn't get the HDMI PHY\n");
150 goto err_disable_clk_tmds
;
153 drm_encoder_helper_add(encoder
, &sun8i_dw_hdmi_encoder_helper_funcs
);
154 drm_encoder_init(drm
, encoder
, &sun8i_dw_hdmi_encoder_funcs
,
155 DRM_MODE_ENCODER_TMDS
, NULL
);
157 sun8i_hdmi_phy_init(hdmi
->phy
);
159 plat_data
->mode_valid
= &sun8i_dw_hdmi_mode_valid
;
160 plat_data
->phy_ops
= sun8i_hdmi_phy_get_ops();
161 plat_data
->phy_name
= "sun8i_dw_hdmi_phy";
162 plat_data
->phy_data
= hdmi
->phy
;
164 platform_set_drvdata(pdev
, hdmi
);
166 hdmi
->hdmi
= dw_hdmi_bind(pdev
, encoder
, plat_data
);
169 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
170 * which would have called the encoder cleanup. Do it manually.
172 if (IS_ERR(hdmi
->hdmi
)) {
173 ret
= PTR_ERR(hdmi
->hdmi
);
174 goto cleanup_encoder
;
180 drm_encoder_cleanup(encoder
);
181 sun8i_hdmi_phy_remove(hdmi
);
182 err_disable_clk_tmds
:
183 clk_disable_unprepare(hdmi
->clk_tmds
);
184 err_assert_ctrl_reset
:
185 reset_control_assert(hdmi
->rst_ctrl
);
190 static void sun8i_dw_hdmi_unbind(struct device
*dev
, struct device
*master
,
193 struct sun8i_dw_hdmi
*hdmi
= dev_get_drvdata(dev
);
195 dw_hdmi_unbind(hdmi
->hdmi
);
196 sun8i_hdmi_phy_remove(hdmi
);
197 clk_disable_unprepare(hdmi
->clk_tmds
);
198 reset_control_assert(hdmi
->rst_ctrl
);
201 static const struct component_ops sun8i_dw_hdmi_ops
= {
202 .bind
= sun8i_dw_hdmi_bind
,
203 .unbind
= sun8i_dw_hdmi_unbind
,
206 static int sun8i_dw_hdmi_probe(struct platform_device
*pdev
)
208 return component_add(&pdev
->dev
, &sun8i_dw_hdmi_ops
);
211 static int sun8i_dw_hdmi_remove(struct platform_device
*pdev
)
213 component_del(&pdev
->dev
, &sun8i_dw_hdmi_ops
);
218 static const struct of_device_id sun8i_dw_hdmi_dt_ids
[] = {
219 { .compatible
= "allwinner,sun8i-a83t-dw-hdmi" },
222 MODULE_DEVICE_TABLE(of
, sun8i_dw_hdmi_dt_ids
);
224 static struct platform_driver sun8i_dw_hdmi_pltfm_driver
= {
225 .probe
= sun8i_dw_hdmi_probe
,
226 .remove
= sun8i_dw_hdmi_remove
,
228 .name
= "sun8i-dw-hdmi",
229 .of_match_table
= sun8i_dw_hdmi_dt_ids
,
232 module_platform_driver(sun8i_dw_hdmi_pltfm_driver
);
234 MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
235 MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
236 MODULE_LICENSE("GPL");