sun4i_lvds.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Free Electrons
  4. * Maxime Ripard <maxime.ripard@free-electrons.com>
  5. */
  6. #include <linux/clk.h>
  7. #include <drm/drmP.h>
  8. #include <drm/drm_atomic_helper.h>
  9. #include <drm/drm_crtc_helper.h>
  10. #include <drm/drm_of.h>
  11. #include <drm/drm_panel.h>
  12. #include "sun4i_crtc.h"
  13. #include "sun4i_tcon.h"
  14. #include "sun4i_lvds.h"
  15. struct sun4i_lvds {
  16. struct drm_connector connector;
  17. struct drm_encoder encoder;
  18. struct sun4i_tcon *tcon;
  19. };
  20. static inline struct sun4i_lvds *
  21. drm_connector_to_sun4i_lvds(struct drm_connector *connector)
  22. {
  23. return container_of(connector, struct sun4i_lvds,
  24. connector);
  25. }
  26. static inline struct sun4i_lvds *
  27. drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
  28. {
  29. return container_of(encoder, struct sun4i_lvds,
  30. encoder);
  31. }
  32. static int sun4i_lvds_get_modes(struct drm_connector *connector)
  33. {
  34. struct sun4i_lvds *lvds =
  35. drm_connector_to_sun4i_lvds(connector);
  36. struct sun4i_tcon *tcon = lvds->tcon;
  37. return drm_panel_get_modes(tcon->panel);
  38. }
  39. static struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
  40. .get_modes = sun4i_lvds_get_modes,
  41. };
  42. static void
  43. sun4i_lvds_connector_destroy(struct drm_connector *connector)
  44. {
  45. struct sun4i_lvds *lvds = drm_connector_to_sun4i_lvds(connector);
  46. struct sun4i_tcon *tcon = lvds->tcon;
  47. drm_panel_detach(tcon->panel);
  48. drm_connector_cleanup(connector);
  49. }
  50. static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
  51. .fill_modes = drm_helper_probe_single_connector_modes,
  52. .destroy = sun4i_lvds_connector_destroy,
  53. .reset = drm_atomic_helper_connector_reset,
  54. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  55. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  56. };
  57. static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
  58. {
  59. struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
  60. struct sun4i_tcon *tcon = lvds->tcon;
  61. DRM_DEBUG_DRIVER("Enabling LVDS output\n");
  62. if (!IS_ERR(tcon->panel)) {
  63. drm_panel_prepare(tcon->panel);
  64. drm_panel_enable(tcon->panel);
  65. }
  66. }
  67. static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
  68. {
  69. struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
  70. struct sun4i_tcon *tcon = lvds->tcon;
  71. DRM_DEBUG_DRIVER("Disabling LVDS output\n");
  72. if (!IS_ERR(tcon->panel)) {
  73. drm_panel_disable(tcon->panel);
  74. drm_panel_unprepare(tcon->panel);
  75. }
  76. }
  77. static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
  78. .disable = sun4i_lvds_encoder_disable,
  79. .enable = sun4i_lvds_encoder_enable,
  80. };
  81. static const struct drm_encoder_funcs sun4i_lvds_enc_funcs = {
  82. .destroy = drm_encoder_cleanup,
  83. };
  84. int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
  85. {
  86. struct drm_encoder *encoder;
  87. struct drm_bridge *bridge;
  88. struct sun4i_lvds *lvds;
  89. int ret;
  90. lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
  91. if (!lvds)
  92. return -ENOMEM;
  93. lvds->tcon = tcon;
  94. encoder = &lvds->encoder;
  95. ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
  96. &tcon->panel, &bridge);
  97. if (ret) {
  98. dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
  99. return 0;
  100. }
  101. drm_encoder_helper_add(&lvds->encoder,
  102. &sun4i_lvds_enc_helper_funcs);
  103. ret = drm_encoder_init(drm,
  104. &lvds->encoder,
  105. &sun4i_lvds_enc_funcs,
  106. DRM_MODE_ENCODER_LVDS,
  107. NULL);
  108. if (ret) {
  109. dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
  110. goto err_out;
  111. }
  112. /* The LVDS encoder can only work with the TCON channel 0 */
  113. lvds->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
  114. if (tcon->panel) {
  115. drm_connector_helper_add(&lvds->connector,
  116. &sun4i_lvds_con_helper_funcs);
  117. ret = drm_connector_init(drm, &lvds->connector,
  118. &sun4i_lvds_con_funcs,
  119. DRM_MODE_CONNECTOR_LVDS);
  120. if (ret) {
  121. dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
  122. goto err_cleanup_connector;
  123. }
  124. drm_mode_connector_attach_encoder(&lvds->connector,
  125. &lvds->encoder);
  126. ret = drm_panel_attach(tcon->panel, &lvds->connector);
  127. if (ret) {
  128. dev_err(drm->dev, "Couldn't attach our panel\n");
  129. goto err_cleanup_connector;
  130. }
  131. }
  132. if (bridge) {
  133. ret = drm_bridge_attach(encoder, bridge, NULL);
  134. if (ret) {
  135. dev_err(drm->dev, "Couldn't attach our bridge\n");
  136. goto err_cleanup_connector;
  137. }
  138. }
  139. return 0;
  140. err_cleanup_connector:
  141. drm_encoder_cleanup(&lvds->encoder);
  142. err_out:
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(sun4i_lvds_init);