lsdc_output_7a1000.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2023 Loongson Technology Corporation Limited
  4. */
  5. #include <drm/drm_atomic_helper.h>
  6. #include <drm/drm_edid.h>
  7. #include <drm/drm_probe_helper.h>
  8. #include "lsdc_drv.h"
  9. #include "lsdc_output.h"
  10. /*
  11. * The display controller in the LS7A1000 exports two DVO interfaces, thus
  12. * external encoder is required, except connected to the DPI panel directly.
  13. *
  14. * ___________________ _________
  15. * | -------| | |
  16. * | CRTC0 --> | DVO0 ----> Encoder0 ---> Connector0 ---> | Display |
  17. * | _ _ -------| ^ ^ |_________|
  18. * | | | | | +------+ | | |
  19. * | |_| |_| | i2c6 | <--------+-------------+
  20. * | +------+ |
  21. * | |
  22. * | DC in LS7A1000 |
  23. * | |
  24. * | _ _ +------+ |
  25. * | | | | | | i2c7 | <--------+-------------+
  26. * | |_| |_| +------+ | | | _________
  27. * | -------| | | | |
  28. * | CRTC1 --> | DVO1 ----> Encoder1 ---> Connector1 ---> | Panel |
  29. * | -------| |_________|
  30. * |___________________|
  31. *
  32. * Currently, we assume the external encoders connected to the DVO are
  33. * transparent. Loongson's DVO interface can directly drive RGB888 panels.
  34. *
  35. * TODO: Add support for non-transparent encoders
  36. */
  37. static int ls7a1000_dpi_connector_get_modes(struct drm_connector *conn)
  38. {
  39. int num;
  40. if (conn->ddc) {
  41. const struct drm_edid *drm_edid;
  42. drm_edid = drm_edid_read(conn);
  43. drm_edid_connector_update(conn, drm_edid);
  44. num = drm_edid_connector_add_modes(conn);
  45. drm_edid_free(drm_edid);
  46. return num;
  47. }
  48. num = drm_add_modes_noedid(conn, 1920, 1200);
  49. drm_set_preferred_mode(conn, 1024, 768);
  50. return num;
  51. }
  52. static struct drm_encoder *
  53. ls7a1000_dpi_connector_get_best_encoder(struct drm_connector *connector,
  54. struct drm_atomic_state *state)
  55. {
  56. struct lsdc_output *output = connector_to_lsdc_output(connector);
  57. return &output->encoder;
  58. }
  59. static const struct drm_connector_helper_funcs
  60. ls7a1000_dpi_connector_helpers = {
  61. .atomic_best_encoder = ls7a1000_dpi_connector_get_best_encoder,
  62. .get_modes = ls7a1000_dpi_connector_get_modes,
  63. };
  64. static enum drm_connector_status
  65. ls7a1000_dpi_connector_detect(struct drm_connector *connector, bool force)
  66. {
  67. struct i2c_adapter *ddc = connector->ddc;
  68. if (ddc) {
  69. if (drm_probe_ddc(ddc))
  70. return connector_status_connected;
  71. return connector_status_disconnected;
  72. }
  73. return connector_status_unknown;
  74. }
  75. static const struct drm_connector_funcs ls7a1000_dpi_connector_funcs = {
  76. .detect = ls7a1000_dpi_connector_detect,
  77. .fill_modes = drm_helper_probe_single_connector_modes,
  78. .destroy = drm_connector_cleanup,
  79. .reset = drm_atomic_helper_connector_reset,
  80. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  81. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state
  82. };
  83. static void ls7a1000_pipe0_encoder_reset(struct drm_encoder *encoder)
  84. {
  85. struct drm_device *ddev = encoder->dev;
  86. struct lsdc_device *ldev = to_lsdc(ddev);
  87. /*
  88. * We need this for S3 support, screen will not lightup if don't set
  89. * this register correctly.
  90. */
  91. lsdc_wreg32(ldev, LSDC_CRTC0_DVO_CONF_REG,
  92. PHY_CLOCK_POL | PHY_CLOCK_EN | PHY_DATA_EN);
  93. }
  94. static void ls7a1000_pipe1_encoder_reset(struct drm_encoder *encoder)
  95. {
  96. struct drm_device *ddev = encoder->dev;
  97. struct lsdc_device *ldev = to_lsdc(ddev);
  98. /*
  99. * We need this for S3 support, screen will not lightup if don't set
  100. * this register correctly.
  101. */
  102. /* DVO */
  103. lsdc_wreg32(ldev, LSDC_CRTC1_DVO_CONF_REG,
  104. BIT(31) | PHY_CLOCK_POL | PHY_CLOCK_EN | PHY_DATA_EN);
  105. }
  106. static const struct drm_encoder_funcs ls7a1000_encoder_funcs[2] = {
  107. {
  108. .reset = ls7a1000_pipe0_encoder_reset,
  109. .destroy = drm_encoder_cleanup,
  110. },
  111. {
  112. .reset = ls7a1000_pipe1_encoder_reset,
  113. .destroy = drm_encoder_cleanup,
  114. },
  115. };
  116. int ls7a1000_output_init(struct drm_device *ddev,
  117. struct lsdc_display_pipe *dispipe,
  118. struct i2c_adapter *ddc,
  119. unsigned int index)
  120. {
  121. struct lsdc_output *output = &dispipe->output;
  122. struct drm_encoder *encoder = &output->encoder;
  123. struct drm_connector *connector = &output->connector;
  124. int ret;
  125. ret = drm_encoder_init(ddev, encoder, &ls7a1000_encoder_funcs[index],
  126. DRM_MODE_ENCODER_TMDS, "encoder-%u", index);
  127. if (ret)
  128. return ret;
  129. encoder->possible_crtcs = BIT(index);
  130. ret = drm_connector_init_with_ddc(ddev, connector,
  131. &ls7a1000_dpi_connector_funcs,
  132. DRM_MODE_CONNECTOR_DPI, ddc);
  133. if (ret)
  134. return ret;
  135. drm_info(ddev, "display pipe-%u has a DVO\n", index);
  136. drm_connector_helper_add(connector, &ls7a1000_dpi_connector_helpers);
  137. drm_connector_attach_encoder(connector, encoder);
  138. connector->polled = DRM_CONNECTOR_POLL_CONNECT |
  139. DRM_CONNECTOR_POLL_DISCONNECT;
  140. connector->interlace_allowed = 0;
  141. connector->doublescan_allowed = 0;
  142. return 0;
  143. }