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 !!of_match_node(sun8i_tcon_top_of_table, node); 48 } 49 50 static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm, 51 struct device_node *node) 52 { 53 struct device_node *port, *ep, *remote, *remote_port; 54 u32 crtcs = 0; 55 56 port = of_graph_get_port_by_id(node, 0); 57 if (!port) 58 return 0; 59 60 ep = of_get_next_available_child(port, NULL); 61 if (!ep) 62 return 0; 63 64 remote = of_graph_get_remote_port_parent(ep); 65 if (!remote) 66 return 0; 67 68 if (sun8i_dw_hdmi_node_is_tcon_top(remote)) { 69 port = of_graph_get_port_by_id(remote, 4); 70 if (!port) 71 return 0; 72 73 for_each_child_of_node(port, ep) { 74 remote_port = of_graph_get_remote_port(ep); 75 if (remote_port) { 76 crtcs |= drm_of_crtc_port_mask(drm, remote_port); 77 of_node_put(remote_port); 78 } 79 } 80 } else { 81 crtcs = drm_of_find_possible_crtcs(drm, node); 82 } 83 84 return crtcs; 85 } 86 87 static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master, 88 void *data) 89 { 90 struct platform_device *pdev = to_platform_device(dev); 91 struct dw_hdmi_plat_data *plat_data; 92 struct drm_device *drm = data; 93 struct device_node *phy_node; 94 struct drm_encoder *encoder; 95 struct sun8i_dw_hdmi *hdmi; 96 int ret; 97 98 if (!pdev->dev.of_node) 99 return -ENODEV; 100 101 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL); 102 if (!hdmi) 103 return -ENOMEM; 104 105 plat_data = &hdmi->plat_data; 106 hdmi->dev = &pdev->dev; 107 encoder = &hdmi->encoder; 108 109 encoder->possible_crtcs = 110 sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node); 111 /* 112 * If we failed to find the CRTC(s) which this encoder is 113 * supposed to be connected to, it's because the CRTC has 114 * not been registered yet. Defer probing, and hope that 115 * the required CRTC is added later. 116 */ 117 if (encoder->possible_crtcs == 0) 118 return -EPROBE_DEFER; 119 120 hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl"); 121 if (IS_ERR(hdmi->rst_ctrl)) { 122 dev_err(dev, "Could not get ctrl reset control\n"); 123 return PTR_ERR(hdmi->rst_ctrl); 124 } 125 126 hdmi->clk_tmds = devm_clk_get(dev, "tmds"); 127 if (IS_ERR(hdmi->clk_tmds)) { 128 dev_err(dev, "Couldn't get the tmds clock\n"); 129 return PTR_ERR(hdmi->clk_tmds); 130 } 131 132 ret = reset_control_deassert(hdmi->rst_ctrl); 133 if (ret) { 134 dev_err(dev, "Could not deassert ctrl reset control\n"); 135 return ret; 136 } 137 138 ret = clk_prepare_enable(hdmi->clk_tmds); 139 if (ret) { 140 dev_err(dev, "Could not enable tmds clock\n"); 141 goto err_assert_ctrl_reset; 142 } 143 144 phy_node = of_parse_phandle(dev->of_node, "phys", 0); 145 if (!phy_node) { 146 dev_err(dev, "Can't found PHY phandle\n"); 147 goto err_disable_clk_tmds; 148 } 149 150 ret = sun8i_hdmi_phy_probe(hdmi, phy_node); 151 of_node_put(phy_node); 152 if (ret) { 153 dev_err(dev, "Couldn't get the HDMI PHY\n"); 154 goto err_disable_clk_tmds; 155 } 156 157 drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs); 158 drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs, 159 DRM_MODE_ENCODER_TMDS, NULL); 160 161 sun8i_hdmi_phy_init(hdmi->phy); 162 163 plat_data->mode_valid = &sun8i_dw_hdmi_mode_valid; 164 plat_data->phy_ops = sun8i_hdmi_phy_get_ops(); 165 plat_data->phy_name = "sun8i_dw_hdmi_phy"; 166 plat_data->phy_data = hdmi->phy; 167 168 platform_set_drvdata(pdev, hdmi); 169 170 hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data); 171 172 /* 173 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(), 174 * which would have called the encoder cleanup. Do it manually. 175 */ 176 if (IS_ERR(hdmi->hdmi)) { 177 ret = PTR_ERR(hdmi->hdmi); 178 goto cleanup_encoder; 179 } 180 181 return 0; 182 183 cleanup_encoder: 184 drm_encoder_cleanup(encoder); 185 sun8i_hdmi_phy_remove(hdmi); 186 err_disable_clk_tmds: 187 clk_disable_unprepare(hdmi->clk_tmds); 188 err_assert_ctrl_reset: 189 reset_control_assert(hdmi->rst_ctrl); 190 191 return ret; 192 } 193 194 static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master, 195 void *data) 196 { 197 struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev); 198 199 dw_hdmi_unbind(hdmi->hdmi); 200 sun8i_hdmi_phy_remove(hdmi); 201 clk_disable_unprepare(hdmi->clk_tmds); 202 reset_control_assert(hdmi->rst_ctrl); 203 } 204 205 static const struct component_ops sun8i_dw_hdmi_ops = { 206 .bind = sun8i_dw_hdmi_bind, 207 .unbind = sun8i_dw_hdmi_unbind, 208 }; 209 210 static int sun8i_dw_hdmi_probe(struct platform_device *pdev) 211 { 212 return component_add(&pdev->dev, &sun8i_dw_hdmi_ops); 213 } 214 215 static int sun8i_dw_hdmi_remove(struct platform_device *pdev) 216 { 217 component_del(&pdev->dev, &sun8i_dw_hdmi_ops); 218 219 return 0; 220 } 221 222 static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = { 223 { .compatible = "allwinner,sun8i-a83t-dw-hdmi" }, 224 { /* sentinel */ }, 225 }; 226 MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids); 227 228 struct platform_driver sun8i_dw_hdmi_pltfm_driver = { 229 .probe = sun8i_dw_hdmi_probe, 230 .remove = sun8i_dw_hdmi_remove, 231 .driver = { 232 .name = "sun8i-dw-hdmi", 233 .of_match_table = sun8i_dw_hdmi_dt_ids, 234 }, 235 }; 236 module_platform_driver(sun8i_dw_hdmi_pltfm_driver); 237 238 MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>"); 239 MODULE_DESCRIPTION("Allwinner DW HDMI bridge"); 240 MODULE_LICENSE("GPL"); 241