inno_hdmi-rockchip.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Rockchip Electronics Co., Ltd.
  4. * Zheng Yang <zhengyang@rock-chips.com>
  5. * Andy Yan <andy.yan@rock-chips.com>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/hw_bitfield.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <drm/bridge/inno_hdmi.h>
  15. #include <drm/drm_bridge_connector.h>
  16. #include <drm/drm_of.h>
  17. #include "rockchip_drm_drv.h"
  18. #define HIWORD_UPDATE(val, mask) ((val) | (mask) << 16)
  19. #define RK3036_GRF_SOC_CON2 0x148
  20. #define RK3036_HDMI_PHSYNC BIT(4)
  21. #define RK3036_HDMI_PVSYNC BIT(5)
  22. enum inno_hdmi_dev_type {
  23. RK3036_HDMI,
  24. RK3128_HDMI,
  25. };
  26. struct inno_hdmi_connector_state {
  27. struct drm_connector_state base;
  28. unsigned int colorimetry;
  29. };
  30. struct rockchip_inno_hdmi {
  31. struct inno_hdmi *base;
  32. struct device *dev;
  33. struct regmap *grf;
  34. struct rockchip_encoder encoder;
  35. };
  36. static struct inno_hdmi_phy_config rk3036_hdmi_phy_configs[] = {
  37. { 74250000, 0x3f, 0xbb },
  38. { 165000000, 0x6f, 0xbb },
  39. { ~0UL, 0x00, 0x00 }
  40. };
  41. static struct inno_hdmi_phy_config rk3128_hdmi_phy_configs[] = {
  42. { 74250000, 0x3f, 0xaa },
  43. { 165000000, 0x5f, 0xaa },
  44. { ~0UL, 0x00, 0x00 }
  45. };
  46. static void inno_hdmi_rk3036_enable(struct device *dev, struct drm_display_mode *mode)
  47. {
  48. struct rockchip_inno_hdmi *hdmi = dev_get_drvdata(dev);
  49. int value, psync;
  50. psync = mode->flags & DRM_MODE_FLAG_PHSYNC ? 1 : 0;
  51. value = FIELD_PREP_WM16(RK3036_HDMI_PHSYNC, psync);
  52. psync = mode->flags & DRM_MODE_FLAG_PVSYNC ? 1 : 0;
  53. value |= FIELD_PREP_WM16(RK3036_HDMI_PVSYNC, psync);
  54. regmap_write(hdmi->grf, RK3036_GRF_SOC_CON2, value);
  55. }
  56. static int inno_hdmi_encoder_atomic_check(struct drm_encoder *encoder,
  57. struct drm_crtc_state *crtc_state,
  58. struct drm_connector_state *conn_state)
  59. {
  60. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  61. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  62. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  63. return 0;
  64. }
  65. static const struct drm_encoder_helper_funcs inno_hdmi_rockchip_encoder_helper_funcs = {
  66. .atomic_check = inno_hdmi_encoder_atomic_check,
  67. };
  68. static int inno_hdmi_rockchip_bind(struct device *dev, struct device *master, void *data)
  69. {
  70. struct drm_device *drm = data;
  71. struct drm_connector *connector;
  72. struct drm_encoder *encoder;
  73. struct rockchip_inno_hdmi *hdmi;
  74. const struct inno_hdmi_plat_data *plat_data;
  75. int ret;
  76. hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
  77. if (!hdmi)
  78. return -ENOMEM;
  79. hdmi->dev = dev;
  80. plat_data = of_device_get_match_data(hdmi->dev);
  81. if (!plat_data)
  82. return -EINVAL;
  83. if (of_device_is_compatible(dev->of_node, "rockchip,rk3036-inno-hdmi")) {
  84. hdmi->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
  85. if (IS_ERR(hdmi->grf))
  86. return dev_err_probe(dev,
  87. PTR_ERR(hdmi->grf), "Unable to get rockchip,grf\n");
  88. }
  89. encoder = &hdmi->encoder.encoder;
  90. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  91. /*
  92. * If we failed to find the CRTC(s) which this encoder is
  93. * supposed to be connected to, it's because the CRTC has
  94. * not been registered yet. Defer probing, and hope that
  95. * the required CRTC is added later.
  96. */
  97. if (encoder->possible_crtcs == 0)
  98. return -EPROBE_DEFER;
  99. ret = drmm_encoder_init(drm, encoder, NULL, DRM_MODE_ENCODER_TMDS, NULL);
  100. if (ret)
  101. return ret;
  102. drm_encoder_helper_add(encoder, &inno_hdmi_rockchip_encoder_helper_funcs);
  103. dev_set_drvdata(dev, hdmi);
  104. hdmi->base = inno_hdmi_bind(dev, encoder, plat_data);
  105. connector = drm_bridge_connector_init(drm, encoder);
  106. if (IS_ERR(connector)) {
  107. ret = PTR_ERR(connector);
  108. dev_err(hdmi->dev, "failed to init bridge connector: %d\n", ret);
  109. return ret;
  110. }
  111. return drm_connector_attach_encoder(connector, encoder);
  112. }
  113. static const struct component_ops inno_hdmi_rockchip_ops = {
  114. .bind = inno_hdmi_rockchip_bind,
  115. };
  116. static int inno_hdmi_rockchip_probe(struct platform_device *pdev)
  117. {
  118. return component_add(&pdev->dev, &inno_hdmi_rockchip_ops);
  119. }
  120. static void inno_hdmi_rockchip_remove(struct platform_device *pdev)
  121. {
  122. component_del(&pdev->dev, &inno_hdmi_rockchip_ops);
  123. }
  124. static const struct inno_hdmi_plat_ops rk3036_inno_hdmi_plat_ops = {
  125. .enable = inno_hdmi_rk3036_enable,
  126. };
  127. static const struct inno_hdmi_plat_data rk3036_inno_hdmi_plat_data = {
  128. .ops = &rk3036_inno_hdmi_plat_ops,
  129. .phy_configs = rk3036_hdmi_phy_configs,
  130. .default_phy_config = &rk3036_hdmi_phy_configs[1],
  131. };
  132. static const struct inno_hdmi_plat_data rk3128_inno_hdmi_plat_data = {
  133. .phy_configs = rk3128_hdmi_phy_configs,
  134. .default_phy_config = &rk3128_hdmi_phy_configs[1],
  135. };
  136. static const struct of_device_id inno_hdmi_rockchip_dt_ids[] = {
  137. { .compatible = "rockchip,rk3036-inno-hdmi",
  138. .data = &rk3036_inno_hdmi_plat_data,
  139. },
  140. { .compatible = "rockchip,rk3128-inno-hdmi",
  141. .data = &rk3128_inno_hdmi_plat_data,
  142. },
  143. {},
  144. };
  145. MODULE_DEVICE_TABLE(of, inno_hdmi_rockchip_dt_ids);
  146. struct platform_driver inno_hdmi_driver = {
  147. .probe = inno_hdmi_rockchip_probe,
  148. .remove = inno_hdmi_rockchip_remove,
  149. .driver = {
  150. .name = "innohdmi-rockchip",
  151. .of_match_table = inno_hdmi_rockchip_dt_ids,
  152. },
  153. };