sun8i_dw_hdmi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/of.h>
  8. #include <linux/platform_device.h>
  9. #include <drm/drm_modeset_helper_vtables.h>
  10. #include <drm/drm_of.h>
  11. #include <drm/drm_simple_kms_helper.h>
  12. #include "sun8i_dw_hdmi.h"
  13. #include "sun8i_tcon_top.h"
  14. static void sun8i_dw_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  15. struct drm_display_mode *mode,
  16. struct drm_display_mode *adj_mode)
  17. {
  18. struct sun8i_dw_hdmi *hdmi = encoder_to_sun8i_dw_hdmi(encoder);
  19. clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
  20. }
  21. static const struct drm_encoder_helper_funcs
  22. sun8i_dw_hdmi_encoder_helper_funcs = {
  23. .mode_set = sun8i_dw_hdmi_encoder_mode_set,
  24. };
  25. static enum drm_mode_status
  26. sun8i_dw_hdmi_mode_valid_a83t(struct dw_hdmi *hdmi, void *data,
  27. const struct drm_display_info *info,
  28. const struct drm_display_mode *mode)
  29. {
  30. if (mode->clock > 297000)
  31. return MODE_CLOCK_HIGH;
  32. return MODE_OK;
  33. }
  34. static enum drm_mode_status
  35. sun8i_dw_hdmi_mode_valid_h6(struct dw_hdmi *hdmi, void *data,
  36. const struct drm_display_info *info,
  37. const struct drm_display_mode *mode)
  38. {
  39. /*
  40. * Controller support maximum of 594 MHz, which correlates to
  41. * 4K@60Hz 4:4:4 or RGB.
  42. */
  43. if (mode->clock > 594000)
  44. return MODE_CLOCK_HIGH;
  45. return MODE_OK;
  46. }
  47. static bool sun8i_dw_hdmi_node_is_tcon_top(struct device_node *node)
  48. {
  49. return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
  50. !!of_match_node(sun8i_tcon_top_of_table, node);
  51. }
  52. static u32 sun8i_dw_hdmi_find_possible_crtcs(struct drm_device *drm,
  53. struct device_node *node)
  54. {
  55. struct device_node *port, *ep, *remote, *remote_port;
  56. u32 crtcs = 0;
  57. remote = of_graph_get_remote_node(node, 0, -1);
  58. if (!remote)
  59. return 0;
  60. if (sun8i_dw_hdmi_node_is_tcon_top(remote)) {
  61. port = of_graph_get_port_by_id(remote, 4);
  62. if (!port)
  63. goto crtcs_exit;
  64. for_each_child_of_node(port, ep) {
  65. remote_port = of_graph_get_remote_port(ep);
  66. if (remote_port) {
  67. crtcs |= drm_of_crtc_port_mask(drm, remote_port);
  68. of_node_put(remote_port);
  69. }
  70. }
  71. } else {
  72. crtcs = drm_of_find_possible_crtcs(drm, node);
  73. }
  74. crtcs_exit:
  75. of_node_put(remote);
  76. return crtcs;
  77. }
  78. static int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
  79. void *data)
  80. {
  81. struct platform_device *pdev = to_platform_device(dev);
  82. struct dw_hdmi_plat_data *plat_data;
  83. struct drm_device *drm = data;
  84. struct device_node *phy_node;
  85. struct drm_encoder *encoder;
  86. struct sun8i_dw_hdmi *hdmi;
  87. int ret;
  88. if (!pdev->dev.of_node)
  89. return -ENODEV;
  90. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  91. if (!hdmi)
  92. return -ENOMEM;
  93. plat_data = &hdmi->plat_data;
  94. hdmi->dev = &pdev->dev;
  95. encoder = &hdmi->encoder;
  96. hdmi->quirks = of_device_get_match_data(dev);
  97. encoder->possible_crtcs =
  98. sun8i_dw_hdmi_find_possible_crtcs(drm, dev->of_node);
  99. /*
  100. * If we failed to find the CRTC(s) which this encoder is
  101. * supposed to be connected to, it's because the CRTC has
  102. * not been registered yet. Defer probing, and hope that
  103. * the required CRTC is added later.
  104. */
  105. if (encoder->possible_crtcs == 0)
  106. return -EPROBE_DEFER;
  107. hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
  108. if (IS_ERR(hdmi->rst_ctrl))
  109. return dev_err_probe(dev, PTR_ERR(hdmi->rst_ctrl),
  110. "Could not get ctrl reset control\n");
  111. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  112. if (IS_ERR(hdmi->clk_tmds))
  113. return dev_err_probe(dev, PTR_ERR(hdmi->clk_tmds),
  114. "Couldn't get the tmds clock\n");
  115. hdmi->regulator = devm_regulator_get(dev, "hvcc");
  116. if (IS_ERR(hdmi->regulator))
  117. return dev_err_probe(dev, PTR_ERR(hdmi->regulator),
  118. "Couldn't get regulator\n");
  119. ret = regulator_enable(hdmi->regulator);
  120. if (ret) {
  121. dev_err(dev, "Failed to enable regulator\n");
  122. return ret;
  123. }
  124. ret = reset_control_deassert(hdmi->rst_ctrl);
  125. if (ret) {
  126. dev_err(dev, "Could not deassert ctrl reset control\n");
  127. goto err_disable_regulator;
  128. }
  129. ret = clk_prepare_enable(hdmi->clk_tmds);
  130. if (ret) {
  131. dev_err(dev, "Could not enable tmds clock\n");
  132. goto err_assert_ctrl_reset;
  133. }
  134. phy_node = of_parse_phandle(dev->of_node, "phys", 0);
  135. if (!phy_node) {
  136. dev_err(dev, "Can't found PHY phandle\n");
  137. ret = -EINVAL;
  138. goto err_disable_clk_tmds;
  139. }
  140. ret = sun8i_hdmi_phy_get(hdmi, phy_node);
  141. of_node_put(phy_node);
  142. if (ret) {
  143. dev_err(dev, "Couldn't get the HDMI PHY\n");
  144. goto err_disable_clk_tmds;
  145. }
  146. ret = sun8i_hdmi_phy_init(hdmi->phy);
  147. if (ret)
  148. goto err_disable_clk_tmds;
  149. drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
  150. drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
  151. plat_data->mode_valid = hdmi->quirks->mode_valid;
  152. plat_data->use_drm_infoframe = hdmi->quirks->use_drm_infoframe;
  153. sun8i_hdmi_phy_set_ops(hdmi->phy, plat_data);
  154. platform_set_drvdata(pdev, hdmi);
  155. hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
  156. /*
  157. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  158. * which would have called the encoder cleanup. Do it manually.
  159. */
  160. if (IS_ERR(hdmi->hdmi)) {
  161. ret = PTR_ERR(hdmi->hdmi);
  162. goto cleanup_encoder;
  163. }
  164. return 0;
  165. cleanup_encoder:
  166. drm_encoder_cleanup(encoder);
  167. err_disable_clk_tmds:
  168. clk_disable_unprepare(hdmi->clk_tmds);
  169. err_assert_ctrl_reset:
  170. reset_control_assert(hdmi->rst_ctrl);
  171. err_disable_regulator:
  172. regulator_disable(hdmi->regulator);
  173. return ret;
  174. }
  175. static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
  176. void *data)
  177. {
  178. struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
  179. dw_hdmi_unbind(hdmi->hdmi);
  180. sun8i_hdmi_phy_deinit(hdmi->phy);
  181. clk_disable_unprepare(hdmi->clk_tmds);
  182. reset_control_assert(hdmi->rst_ctrl);
  183. regulator_disable(hdmi->regulator);
  184. }
  185. static const struct component_ops sun8i_dw_hdmi_ops = {
  186. .bind = sun8i_dw_hdmi_bind,
  187. .unbind = sun8i_dw_hdmi_unbind,
  188. };
  189. static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
  190. {
  191. return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
  192. }
  193. static void sun8i_dw_hdmi_remove(struct platform_device *pdev)
  194. {
  195. component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
  196. }
  197. static const struct sun8i_dw_hdmi_quirks sun8i_a83t_quirks = {
  198. .mode_valid = sun8i_dw_hdmi_mode_valid_a83t,
  199. };
  200. static const struct sun8i_dw_hdmi_quirks sun50i_h6_quirks = {
  201. .mode_valid = sun8i_dw_hdmi_mode_valid_h6,
  202. .use_drm_infoframe = true,
  203. };
  204. static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
  205. {
  206. .compatible = "allwinner,sun8i-a83t-dw-hdmi",
  207. .data = &sun8i_a83t_quirks,
  208. },
  209. {
  210. .compatible = "allwinner,sun50i-h6-dw-hdmi",
  211. .data = &sun50i_h6_quirks,
  212. },
  213. { /* sentinel */ },
  214. };
  215. MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
  216. static struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
  217. .probe = sun8i_dw_hdmi_probe,
  218. .remove_new = sun8i_dw_hdmi_remove,
  219. .driver = {
  220. .name = "sun8i-dw-hdmi",
  221. .of_match_table = sun8i_dw_hdmi_dt_ids,
  222. },
  223. };
  224. static int __init sun8i_dw_hdmi_init(void)
  225. {
  226. int ret;
  227. ret = platform_driver_register(&sun8i_dw_hdmi_pltfm_driver);
  228. if (ret)
  229. return ret;
  230. ret = platform_driver_register(&sun8i_hdmi_phy_driver);
  231. if (ret) {
  232. platform_driver_unregister(&sun8i_dw_hdmi_pltfm_driver);
  233. return ret;
  234. }
  235. return ret;
  236. }
  237. static void __exit sun8i_dw_hdmi_exit(void)
  238. {
  239. platform_driver_unregister(&sun8i_dw_hdmi_pltfm_driver);
  240. platform_driver_unregister(&sun8i_hdmi_phy_driver);
  241. }
  242. module_init(sun8i_dw_hdmi_init);
  243. module_exit(sun8i_dw_hdmi_exit);
  244. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  245. MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
  246. MODULE_LICENSE("GPL");