sun4i_lvds.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/drm_atomic_helper.h>
  8. #include <drm/drm_bridge.h>
  9. #include <drm/drm_of.h>
  10. #include <drm/drm_panel.h>
  11. #include <drm/drm_print.h>
  12. #include <drm/drm_probe_helper.h>
  13. #include <drm/drm_simple_kms_helper.h>
  14. #include "sun4i_crtc.h"
  15. #include "sun4i_tcon.h"
  16. #include "sun4i_lvds.h"
  17. struct sun4i_lvds {
  18. struct drm_connector connector;
  19. struct drm_encoder encoder;
  20. struct drm_panel *panel;
  21. };
  22. static inline struct sun4i_lvds *
  23. drm_connector_to_sun4i_lvds(struct drm_connector *connector)
  24. {
  25. return container_of(connector, struct sun4i_lvds,
  26. connector);
  27. }
  28. static inline struct sun4i_lvds *
  29. drm_encoder_to_sun4i_lvds(struct drm_encoder *encoder)
  30. {
  31. return container_of(encoder, struct sun4i_lvds,
  32. encoder);
  33. }
  34. static int sun4i_lvds_get_modes(struct drm_connector *connector)
  35. {
  36. struct sun4i_lvds *lvds =
  37. drm_connector_to_sun4i_lvds(connector);
  38. return drm_panel_get_modes(lvds->panel, connector);
  39. }
  40. static const struct drm_connector_helper_funcs sun4i_lvds_con_helper_funcs = {
  41. .get_modes = sun4i_lvds_get_modes,
  42. };
  43. static void
  44. sun4i_lvds_connector_destroy(struct drm_connector *connector)
  45. {
  46. drm_connector_cleanup(connector);
  47. }
  48. static const struct drm_connector_funcs sun4i_lvds_con_funcs = {
  49. .fill_modes = drm_helper_probe_single_connector_modes,
  50. .destroy = sun4i_lvds_connector_destroy,
  51. .reset = drm_atomic_helper_connector_reset,
  52. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  53. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  54. };
  55. static void sun4i_lvds_encoder_enable(struct drm_encoder *encoder)
  56. {
  57. struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
  58. DRM_DEBUG_DRIVER("Enabling LVDS output\n");
  59. if (lvds->panel) {
  60. drm_panel_prepare(lvds->panel);
  61. drm_panel_enable(lvds->panel);
  62. }
  63. }
  64. static void sun4i_lvds_encoder_disable(struct drm_encoder *encoder)
  65. {
  66. struct sun4i_lvds *lvds = drm_encoder_to_sun4i_lvds(encoder);
  67. DRM_DEBUG_DRIVER("Disabling LVDS output\n");
  68. if (lvds->panel) {
  69. drm_panel_disable(lvds->panel);
  70. drm_panel_unprepare(lvds->panel);
  71. }
  72. }
  73. static const struct drm_encoder_helper_funcs sun4i_lvds_enc_helper_funcs = {
  74. .disable = sun4i_lvds_encoder_disable,
  75. .enable = sun4i_lvds_encoder_enable,
  76. };
  77. int sun4i_lvds_init(struct drm_device *drm, struct sun4i_tcon *tcon)
  78. {
  79. struct drm_encoder *encoder;
  80. struct drm_bridge *bridge;
  81. struct sun4i_lvds *lvds;
  82. int ret;
  83. lvds = devm_kzalloc(drm->dev, sizeof(*lvds), GFP_KERNEL);
  84. if (!lvds)
  85. return -ENOMEM;
  86. encoder = &lvds->encoder;
  87. ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
  88. &lvds->panel, &bridge);
  89. if (ret) {
  90. dev_info(drm->dev, "No panel or bridge found... LVDS output disabled\n");
  91. return 0;
  92. }
  93. drm_encoder_helper_add(&lvds->encoder,
  94. &sun4i_lvds_enc_helper_funcs);
  95. ret = drm_simple_encoder_init(drm, &lvds->encoder,
  96. DRM_MODE_ENCODER_LVDS);
  97. if (ret) {
  98. dev_err(drm->dev, "Couldn't initialise the lvds encoder\n");
  99. goto err_out;
  100. }
  101. /* The LVDS encoder can only work with the TCON channel 0 */
  102. lvds->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
  103. if (lvds->panel) {
  104. drm_connector_helper_add(&lvds->connector,
  105. &sun4i_lvds_con_helper_funcs);
  106. ret = drm_connector_init(drm, &lvds->connector,
  107. &sun4i_lvds_con_funcs,
  108. DRM_MODE_CONNECTOR_LVDS);
  109. if (ret) {
  110. dev_err(drm->dev, "Couldn't initialise the lvds connector\n");
  111. goto err_cleanup_connector;
  112. }
  113. drm_connector_attach_encoder(&lvds->connector,
  114. &lvds->encoder);
  115. }
  116. if (bridge) {
  117. ret = drm_bridge_attach(encoder, bridge, NULL, 0);
  118. if (ret) {
  119. dev_err(drm->dev, "Couldn't attach our bridge\n");
  120. goto err_cleanup_connector;
  121. }
  122. }
  123. return 0;
  124. err_cleanup_connector:
  125. drm_encoder_cleanup(&lvds->encoder);
  126. err_out:
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(sun4i_lvds_init);