sun8i_dw_hdmi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. static void sun8i_dw_hdmi_encoder_mode_set(struct drm_encoder *encoder,
  13. struct drm_display_mode *mode,
  14. struct drm_display_mode *adj_mode)
  15. {
  16. struct sun8i_dw_hdmi *hdmi = encoder_to_sun8i_dw_hdmi(encoder);
  17. clk_set_rate(hdmi->clk_tmds, mode->crtc_clock * 1000);
  18. }
  19. static const struct drm_encoder_helper_funcs
  20. sun8i_dw_hdmi_encoder_helper_funcs = {
  21. .mode_set = sun8i_dw_hdmi_encoder_mode_set,
  22. };
  23. static const struct drm_encoder_funcs sun8i_dw_hdmi_encoder_funcs = {
  24. .destroy = drm_encoder_cleanup,
  25. };
  26. static enum drm_mode_status
  27. sun8i_dw_hdmi_mode_valid(struct drm_connector *connector,
  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 int sun8i_dw_hdmi_bind(struct device *dev, struct device *master,
  35. void *data)
  36. {
  37. struct platform_device *pdev = to_platform_device(dev);
  38. struct dw_hdmi_plat_data *plat_data;
  39. struct drm_device *drm = data;
  40. struct device_node *phy_node;
  41. struct drm_encoder *encoder;
  42. struct sun8i_dw_hdmi *hdmi;
  43. int ret;
  44. if (!pdev->dev.of_node)
  45. return -ENODEV;
  46. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  47. if (!hdmi)
  48. return -ENOMEM;
  49. plat_data = &hdmi->plat_data;
  50. hdmi->dev = &pdev->dev;
  51. encoder = &hdmi->encoder;
  52. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  53. /*
  54. * If we failed to find the CRTC(s) which this encoder is
  55. * supposed to be connected to, it's because the CRTC has
  56. * not been registered yet. Defer probing, and hope that
  57. * the required CRTC is added later.
  58. */
  59. if (encoder->possible_crtcs == 0)
  60. return -EPROBE_DEFER;
  61. hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
  62. if (IS_ERR(hdmi->rst_ctrl)) {
  63. dev_err(dev, "Could not get ctrl reset control\n");
  64. return PTR_ERR(hdmi->rst_ctrl);
  65. }
  66. hdmi->clk_tmds = devm_clk_get(dev, "tmds");
  67. if (IS_ERR(hdmi->clk_tmds)) {
  68. dev_err(dev, "Couldn't get the tmds clock\n");
  69. return PTR_ERR(hdmi->clk_tmds);
  70. }
  71. ret = reset_control_deassert(hdmi->rst_ctrl);
  72. if (ret) {
  73. dev_err(dev, "Could not deassert ctrl reset control\n");
  74. return ret;
  75. }
  76. ret = clk_prepare_enable(hdmi->clk_tmds);
  77. if (ret) {
  78. dev_err(dev, "Could not enable tmds clock\n");
  79. goto err_assert_ctrl_reset;
  80. }
  81. phy_node = of_parse_phandle(dev->of_node, "phys", 0);
  82. if (!phy_node) {
  83. dev_err(dev, "Can't found PHY phandle\n");
  84. goto err_disable_clk_tmds;
  85. }
  86. ret = sun8i_hdmi_phy_probe(hdmi, phy_node);
  87. of_node_put(phy_node);
  88. if (ret) {
  89. dev_err(dev, "Couldn't get the HDMI PHY\n");
  90. goto err_disable_clk_tmds;
  91. }
  92. drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
  93. drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
  94. DRM_MODE_ENCODER_TMDS, NULL);
  95. sun8i_hdmi_phy_init(hdmi->phy);
  96. plat_data->mode_valid = &sun8i_dw_hdmi_mode_valid;
  97. plat_data->phy_ops = sun8i_hdmi_phy_get_ops();
  98. plat_data->phy_name = "sun8i_dw_hdmi_phy";
  99. plat_data->phy_data = hdmi->phy;
  100. platform_set_drvdata(pdev, hdmi);
  101. hdmi->hdmi = dw_hdmi_bind(pdev, encoder, plat_data);
  102. /*
  103. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  104. * which would have called the encoder cleanup. Do it manually.
  105. */
  106. if (IS_ERR(hdmi->hdmi)) {
  107. ret = PTR_ERR(hdmi->hdmi);
  108. goto cleanup_encoder;
  109. }
  110. return 0;
  111. cleanup_encoder:
  112. drm_encoder_cleanup(encoder);
  113. sun8i_hdmi_phy_remove(hdmi);
  114. err_disable_clk_tmds:
  115. clk_disable_unprepare(hdmi->clk_tmds);
  116. err_assert_ctrl_reset:
  117. reset_control_assert(hdmi->rst_ctrl);
  118. return ret;
  119. }
  120. static void sun8i_dw_hdmi_unbind(struct device *dev, struct device *master,
  121. void *data)
  122. {
  123. struct sun8i_dw_hdmi *hdmi = dev_get_drvdata(dev);
  124. dw_hdmi_unbind(hdmi->hdmi);
  125. sun8i_hdmi_phy_remove(hdmi);
  126. clk_disable_unprepare(hdmi->clk_tmds);
  127. reset_control_assert(hdmi->rst_ctrl);
  128. }
  129. static const struct component_ops sun8i_dw_hdmi_ops = {
  130. .bind = sun8i_dw_hdmi_bind,
  131. .unbind = sun8i_dw_hdmi_unbind,
  132. };
  133. static int sun8i_dw_hdmi_probe(struct platform_device *pdev)
  134. {
  135. return component_add(&pdev->dev, &sun8i_dw_hdmi_ops);
  136. }
  137. static int sun8i_dw_hdmi_remove(struct platform_device *pdev)
  138. {
  139. component_del(&pdev->dev, &sun8i_dw_hdmi_ops);
  140. return 0;
  141. }
  142. static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
  143. { .compatible = "allwinner,sun8i-a83t-dw-hdmi" },
  144. { /* sentinel */ },
  145. };
  146. MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
  147. struct platform_driver sun8i_dw_hdmi_pltfm_driver = {
  148. .probe = sun8i_dw_hdmi_probe,
  149. .remove = sun8i_dw_hdmi_remove,
  150. .driver = {
  151. .name = "sun8i-dw-hdmi",
  152. .of_match_table = sun8i_dw_hdmi_dt_ids,
  153. },
  154. };
  155. module_platform_driver(sun8i_dw_hdmi_pltfm_driver);
  156. MODULE_AUTHOR("Jernej Skrabec <jernej.skrabec@siol.net>");
  157. MODULE_DESCRIPTION("Allwinner DW HDMI bridge");
  158. MODULE_LICENSE("GPL");