sun8i_dw_hdmi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
  4. */
  5. #include <linux/component.h>
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <drm/drm_of.h>
  9. #include <drm/drmP.h>
  10. #include <drm/drm_crtc_helper.h>
  11. #include "sun8i_dw_hdmi.h"
  12. #include "sun8i_tcon_top.h"
  13. static void sun8i_dw_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  14. struct drm_display_mode *mode,
  15. struct drm_display_mode *adj_mode)
  16. {
  17. struct sun8i_dw_hdmi *hdmi = encoder_to_sun8i_dw_hdmi(encoder);
  18. clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
  19. }
  20. static const struct drm_encoder_helper_funcs
  21. sun8i_dw_hdmi_encoder_helper_funcs = {
  22. .mode_set = sun8i_dw_hdmi_encoder_mode_set,
  23. };
  24. static const struct drm_encoder_funcs sun8i_dw_hdmi_encoder_funcs = {
  25. .destroy = drm_encoder_cleanup,
  26. };
  27. static enum drm_mode_status
  28. sun8i_dw_hdmi_mode_valid(struct drm_connector *connector,
  29. const struct drm_display_mode *mode)
  30. {
  31. if (mode->clock > 297000)
  32. return MODE_CLOCK_HIGH;
  33. return MODE_OK;
  34. }
  35. static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node *node)
  36. {
  37. return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
  38. !!of_match_node(sun8i_tcon_top_of_table, node);
  39. }
  40. static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm,
  41. struct device_node *node)
  42. {
  43. struct device_node *port, *ep, *remote, *remote_port;
  44. u32 crtcs = 0;
  45. remote = of_graph_get_remote_node(node, 0, -1);
  46. if (!remote)
  47. return 0;
  48. if (sun8i_dw_hdmi_node_is_tcon_top(remote)) {
  49. port = of_graph_get_port_by_id(remote, 4);
  50. if (!port)
  51. goto crtcs_exit;
  52. for_each_child_of_node(port, ep) {
  53. remote_port = of_graph_get_remote_port(ep);
  54. if (remote_port) {
  55. crtcs |= drm_of_crtc_port_mask(drm, remote_port);
  56. of_node_put(remote_port);
  57. }
  58. }
  59. } else {
  60. crtcs = drm_of_find_possible_crtcs(drm, node);
  61. }
  62. crtcs_exit:
  63. of_node_put(remote);
  64. return crtcs;
  65. }
  66. static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
  67. void *data)
  68. {
  69. struct platform_device *pdev = to_platform_device(dev);
  70. struct dw_hdmi_plat_data *plat_data;
  71. struct drm_device *drm = data;
  72. struct device_node *phy_node;
  73. struct drm_encoder *encoder;
  74. struct sun8i_dw_hdmi *hdmi;
  75. int ret;
  76. if (!pdev->dev.of_node)
  77. return -ENODEV;
  78. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  79. if (!hdmi)
  80. return -ENOMEM;
  81. plat_data = &hdmi->plat_data;
  82. hdmi->dev = &pdev->dev;
  83. encoder = &hdmi->encoder;
  84. encoder->possible_crtcs =
  85. sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node);
  86. /*
  87. * If we failed to find the CRTC(s) which this encoder is
  88. * supposed to be connected to, it's because the CRTC has
  89. * not been registered yet. Defer probing, and hope that
  90. * the required CRTC is added later.
  91. */
  92. if (encoder->possible_crtcs == 0)
  93. return -EPROBE_DEFER;
  94. hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
  95. if (IS_ERR(hdmi->rst_ctrl)) {
  96. dev_err(dev, "Could not get ctrl reset control\n");
  97. return PTR_ERR(hdmi->rst_ctrl);
  98. }
  99. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  100. if (IS_ERR(hdmi->clk_tmds)) {
  101. dev_err(dev, "Couldn't get the tmds clock\n");
  102. return PTR_ERR(hdmi->clk_tmds);
  103. }
  104. ret = reset_control_deassert(hdmi->rst_ctrl);
  105. if (ret) {
  106. dev_err(dev, "Could not deassert ctrl reset control\n");
  107. return ret;
  108. }
  109. ret = clk_prepare_enable(hdmi->clk_tmds);
  110. if (ret) {
  111. dev_err(dev, "Could not enable tmds clock\n");
  112. goto err_assert_ctrl_reset;
  113. }
  114. phy_node = of_parse_phandle(dev->of_node, "phys", 0);
  115. if (!phy_node) {
  116. dev_err(dev, "Can't found PHY phandle\n");
  117. ret = -EINVAL;
  118. goto err_disable_clk_tmds;
  119. }
  120. ret = sun8i_hdmi_phy_probe(hdmi, phy_node);
  121. of_node_put(phy_node);
  122. if (ret) {
  123. dev_err(dev, "Couldn't get the HDMI PHY\n");
  124. goto err_disable_clk_tmds;
  125. }
  126. drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
  127. drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
  128. DRM_MODE_ENCODER_TMDS, NULL);
  129. sun8i_hdmi_phy_init(hdmi->phy);
  130. plat_data->mode_valid = &sun8i_dw_hdmi_mode_valid;
  131. plat_data->phy_ops = sun8i_hdmi_phy_get_ops();
  132. plat_data->phy_name = "sun8i_dw_hdmi_phy";
  133. plat_data->phy_data = hdmi->phy;
  134. platform_set_drvdata(pdev, hdmi);
  135. hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
  136. /*
  137. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  138. * which would have called the encoder cleanup. Do it manually.
  139. */
  140. if (IS_ERR(hdmi->hdmi)) {
  141. ret = PTR_ERR(hdmi->hdmi);
  142. goto cleanup_encoder;
  143. }
  144. return 0;
  145. cleanup_encoder:
  146. drm_encoder_cleanup(encoder);
  147. sun8i_hdmi_phy_remove(hdmi);
  148. err_disable_clk_tmds:
  149. clk_disable_unprepare(hdmi->clk_tmds);
  150. err_assert_ctrl_reset:
  151. reset_control_assert(hdmi->rst_ctrl);
  152. return ret;
  153. }
  154. static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
  155. void *data)
  156. {
  157. struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
  158. dw_hdmi_unbind(hdmi->hdmi);
  159. sun8i_hdmi_phy_remove(hdmi);
  160. clk_disable_unprepare(hdmi->clk_tmds);
  161. reset_control_assert(hdmi->rst_ctrl);
  162. }
  163. static const struct component_ops sun8i_dw_hdmi_ops = {
  164. .bind = sun8i_dw_hdmi_bind,
  165. .unbind = sun8i_dw_hdmi_unbind,
  166. };
  167. static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
  168. {
  169. return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
  170. }
  171. static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
  172. {
  173. component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
  174. return 0;
  175. }
  176. static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
  177. { .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
  178. { /* sentinel */ },
  179. };
  180. MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
  181. static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
  182. .probe = sun8i_dw_hdmi_probe,
  183. .remove = sun8i_dw_hdmi_remove,
  184. .driver = {
  185. .name = "sun8i-dw-hdmi",
  186. .of_match_table = sun8i_dw_hdmi_dt_ids,
  187. },
  188. };
  189. module_platform_driver(sun8i_dw_hdmi_pltfm_driver);
  190. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  191. MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
  192. MODULE_LICENSE("GPL");