sun4i_rgb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2015 Free Electrons
  3. * Copyright (C) 2015 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_of.h>
  17. #include <drm/drm_panel.h>
  18. #include "sun4i_crtc.h"
  19. #include "sun4i_tcon.h"
  20. #include "sun4i_rgb.h"
  21. struct sun4i_rgb {
  22. struct drm_connector connector;
  23. struct drm_encoder encoder;
  24. struct sun4i_tcon *tcon;
  25. };
  26. static inline struct sun4i_rgb *
  27. drm_connector_to_sun4i_rgb(struct drm_connector *connector)
  28. {
  29. return container_of(connector, struct sun4i_rgb,
  30. connector);
  31. }
  32. static inline struct sun4i_rgb *
  33. drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
  34. {
  35. return container_of(encoder, struct sun4i_rgb,
  36. encoder);
  37. }
  38. static int sun4i_rgb_get_modes(struct drm_connector *connector)
  39. {
  40. struct sun4i_rgb *rgb =
  41. drm_connector_to_sun4i_rgb(connector);
  42. struct sun4i_tcon *tcon = rgb->tcon;
  43. return drm_panel_get_modes(tcon->panel);
  44. }
  45. static int sun4i_rgb_mode_valid(struct drm_connector *connector,
  46. struct drm_display_mode *mode)
  47. {
  48. struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
  49. struct sun4i_tcon *tcon = rgb->tcon;
  50. u32 hsync = mode->hsync_end - mode->hsync_start;
  51. u32 vsync = mode->vsync_end - mode->vsync_start;
  52. unsigned long rate = mode->clock * 1000;
  53. long rounded_rate;
  54. DRM_DEBUG_DRIVER("Validating modes...\n");
  55. if (hsync < 1)
  56. return MODE_HSYNC_NARROW;
  57. if (hsync > 0x3ff)
  58. return MODE_HSYNC_WIDE;
  59. if ((mode->hdisplay < 1) || (mode->htotal < 1))
  60. return MODE_H_ILLEGAL;
  61. if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
  62. return MODE_BAD_HVALUE;
  63. DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
  64. if (vsync < 1)
  65. return MODE_VSYNC_NARROW;
  66. if (vsync > 0x3ff)
  67. return MODE_VSYNC_WIDE;
  68. if ((mode->vdisplay < 1) || (mode->vtotal < 1))
  69. return MODE_V_ILLEGAL;
  70. if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
  71. return MODE_BAD_VVALUE;
  72. DRM_DEBUG_DRIVER("Vertical parameters OK\n");
  73. rounded_rate = clk_round_rate(tcon->dclk, rate);
  74. if (rounded_rate < rate)
  75. return MODE_CLOCK_LOW;
  76. if (rounded_rate > rate)
  77. return MODE_CLOCK_HIGH;
  78. DRM_DEBUG_DRIVER("Clock rate OK\n");
  79. return MODE_OK;
  80. }
  81. static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
  82. .get_modes = sun4i_rgb_get_modes,
  83. .mode_valid = sun4i_rgb_mode_valid,
  84. };
  85. static void
  86. sun4i_rgb_connector_destroy(struct drm_connector *connector)
  87. {
  88. struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
  89. struct sun4i_tcon *tcon = rgb->tcon;
  90. drm_panel_detach(tcon->panel);
  91. drm_connector_cleanup(connector);
  92. }
  93. static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
  94. .fill_modes = drm_helper_probe_single_connector_modes,
  95. .destroy = sun4i_rgb_connector_destroy,
  96. .reset = drm_atomic_helper_connector_reset,
  97. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  98. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  99. };
  100. static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
  101. {
  102. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  103. struct sun4i_tcon *tcon = rgb->tcon;
  104. DRM_DEBUG_DRIVER("Enabling RGB output\n");
  105. if (!IS_ERR(tcon->panel)) {
  106. drm_panel_prepare(tcon->panel);
  107. drm_panel_enable(tcon->panel);
  108. }
  109. }
  110. static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
  111. {
  112. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  113. struct sun4i_tcon *tcon = rgb->tcon;
  114. DRM_DEBUG_DRIVER("Disabling RGB output\n");
  115. if (!IS_ERR(tcon->panel)) {
  116. drm_panel_disable(tcon->panel);
  117. drm_panel_unprepare(tcon->panel);
  118. }
  119. }
  120. static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
  121. .disable = sun4i_rgb_encoder_disable,
  122. .enable = sun4i_rgb_encoder_enable,
  123. };
  124. static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
  125. {
  126. drm_encoder_cleanup(encoder);
  127. }
  128. static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
  129. .destroy = sun4i_rgb_enc_destroy,
  130. };
  131. int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
  132. {
  133. struct drm_encoder *encoder;
  134. struct drm_bridge *bridge;
  135. struct sun4i_rgb *rgb;
  136. int ret;
  137. rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
  138. if (!rgb)
  139. return -ENOMEM;
  140. rgb->tcon = tcon;
  141. encoder = &rgb->encoder;
  142. ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
  143. &tcon->panel, &bridge);
  144. if (ret) {
  145. dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
  146. return 0;
  147. }
  148. drm_encoder_helper_add(&rgb->encoder,
  149. &sun4i_rgb_enc_helper_funcs);
  150. ret = drm_encoder_init(drm,
  151. &rgb->encoder,
  152. &sun4i_rgb_enc_funcs,
  153. DRM_MODE_ENCODER_NONE,
  154. NULL);
  155. if (ret) {
  156. dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
  157. goto err_out;
  158. }
  159. /* The RGB encoder can only work with the TCON channel 0 */
  160. rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
  161. if (tcon->panel) {
  162. drm_connector_helper_add(&rgb->connector,
  163. &sun4i_rgb_con_helper_funcs);
  164. ret = drm_connector_init(drm, &rgb->connector,
  165. &sun4i_rgb_con_funcs,
  166. DRM_MODE_CONNECTOR_Unknown);
  167. if (ret) {
  168. dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
  169. goto err_cleanup_connector;
  170. }
  171. drm_mode_connector_attach_encoder(&rgb->connector,
  172. &rgb->encoder);
  173. ret = drm_panel_attach(tcon->panel, &rgb->connector);
  174. if (ret) {
  175. dev_err(drm->dev, "Couldn't attach our panel\n");
  176. goto err_cleanup_connector;
  177. }
  178. }
  179. if (bridge) {
  180. ret = drm_bridge_attach(encoder, bridge, NULL);
  181. if (ret) {
  182. dev_err(drm->dev, "Couldn't attach our bridge\n");
  183. goto err_cleanup_connector;
  184. }
  185. }
  186. return 0;
  187. err_cleanup_connector:
  188. drm_encoder_cleanup(&rgb->encoder);
  189. err_out:
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(sun4i_rgb_init);