sun8i_dw_hdmi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. hdmi->regulator = devm_regulator_get(dev, "hvcc");
  105. if (IS_ERR(hdmi->regulator)) {
  106. dev_err(dev, "Couldn't get regulator\n");
  107. return PTR_ERR(hdmi->regulator);
  108. }
  109. ret = regulator_enable(hdmi->regulator);
  110. if (ret) {
  111. dev_err(dev, "Failed to enable regulator\n");
  112. return ret;
  113. }
  114. ret = reset_control_deassert(hdmi->rst_ctrl);
  115. if (ret) {
  116. dev_err(dev, "Could not deassert ctrl reset control\n");
  117. goto err_disable_regulator;
  118. }
  119. ret = clk_prepare_enable(hdmi->clk_tmds);
  120. if (ret) {
  121. dev_err(dev, "Could not enable tmds clock\n");
  122. goto err_assert_ctrl_reset;
  123. }
  124. phy_node = of_parse_phandle(dev->of_node, "phys", 0);
  125. if (!phy_node) {
  126. dev_err(dev, "Can't found PHY phandle\n");
  127. goto err_disable_clk_tmds;
  128. }
  129. ret = sun8i_hdmi_phy_probe(hdmi, phy_node);
  130. of_node_put(phy_node);
  131. if (ret) {
  132. dev_err(dev, "Couldn't get the HDMI PHY\n");
  133. goto err_disable_clk_tmds;
  134. }
  135. drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
  136. drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
  137. DRM_MODE_ENCODER_TMDS, NULL);
  138. sun8i_hdmi_phy_init(hdmi->phy);
  139. plat_data->mode_valid = &sun8i_dw_hdmi_mode_valid;
  140. plat_data->phy_ops = sun8i_hdmi_phy_get_ops();
  141. plat_data->phy_name = "sun8i_dw_hdmi_phy";
  142. plat_data->phy_data = hdmi->phy;
  143. platform_set_drvdata(pdev, hdmi);
  144. hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
  145. /*
  146. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  147. * which would have called the encoder cleanup. Do it manually.
  148. */
  149. if (IS_ERR(hdmi->hdmi)) {
  150. ret = PTR_ERR(hdmi->hdmi);
  151. goto cleanup_encoder;
  152. }
  153. return 0;
  154. cleanup_encoder:
  155. drm_encoder_cleanup(encoder);
  156. sun8i_hdmi_phy_remove(hdmi);
  157. err_disable_clk_tmds:
  158. clk_disable_unprepare(hdmi->clk_tmds);
  159. err_assert_ctrl_reset:
  160. reset_control_assert(hdmi->rst_ctrl);
  161. err_disable_regulator:
  162. regulator_disable(hdmi->regulator);
  163. return ret;
  164. }
  165. static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
  166. void *data)
  167. {
  168. struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
  169. dw_hdmi_unbind(hdmi->hdmi);
  170. sun8i_hdmi_phy_remove(hdmi);
  171. clk_disable_unprepare(hdmi->clk_tmds);
  172. reset_control_assert(hdmi->rst_ctrl);
  173. regulator_disable(hdmi->regulator);
  174. }
  175. static const struct component_ops sun8i_dw_hdmi_ops = {
  176. .bind = sun8i_dw_hdmi_bind,
  177. .unbind = sun8i_dw_hdmi_unbind,
  178. };
  179. static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
  180. {
  181. return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
  182. }
  183. static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
  184. {
  185. component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
  186. return 0;
  187. }
  188. static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
  189. { .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
  190. { /* sentinel */ },
  191. };
  192. MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
  193. static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
  194. .probe = sun8i_dw_hdmi_probe,
  195. .remove = sun8i_dw_hdmi_remove,
  196. .driver = {
  197. .name = "sun8i-dw-hdmi",
  198. .of_match_table = sun8i_dw_hdmi_dt_ids,
  199. },
  200. };
  201. module_platform_driver(sun8i_dw_hdmi_pltfm_driver);
  202. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  203. MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
  204. MODULE_LICENSE("GPL");