sun4i_rgb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 enum drm_mode_status sun4i_rgb_mode_valid(struct drm_encoder *crtc,
  46. const struct drm_display_mode *mode)
  47. {
  48. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(crtc);
  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. tcon->dclk_min_div = 6;
  74. tcon->dclk_max_div = 127;
  75. rounded_rate = clk_round_rate(tcon->dclk, rate);
  76. if (rounded_rate < rate)
  77. return MODE_CLOCK_LOW;
  78. if (rounded_rate > rate)
  79. return MODE_CLOCK_HIGH;
  80. DRM_DEBUG_DRIVER("Clock rate OK\n");
  81. return MODE_OK;
  82. }
  83. static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
  84. .get_modes = sun4i_rgb_get_modes,
  85. };
  86. static void
  87. sun4i_rgb_connector_destroy(struct drm_connector *connector)
  88. {
  89. struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
  90. struct sun4i_tcon *tcon = rgb->tcon;
  91. drm_panel_detach(tcon->panel);
  92. drm_connector_cleanup(connector);
  93. }
  94. static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
  95. .fill_modes = drm_helper_probe_single_connector_modes,
  96. .destroy = sun4i_rgb_connector_destroy,
  97. .reset = drm_atomic_helper_connector_reset,
  98. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  99. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  100. };
  101. static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
  102. {
  103. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  104. struct sun4i_tcon *tcon = rgb->tcon;
  105. DRM_DEBUG_DRIVER("Enabling RGB output\n");
  106. if (!IS_ERR(tcon->panel)) {
  107. drm_panel_prepare(tcon->panel);
  108. drm_panel_enable(tcon->panel);
  109. }
  110. }
  111. static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
  112. {
  113. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  114. struct sun4i_tcon *tcon = rgb->tcon;
  115. DRM_DEBUG_DRIVER("Disabling RGB output\n");
  116. if (!IS_ERR(tcon->panel)) {
  117. drm_panel_disable(tcon->panel);
  118. drm_panel_unprepare(tcon->panel);
  119. }
  120. }
  121. static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
  122. .disable = sun4i_rgb_encoder_disable,
  123. .enable = sun4i_rgb_encoder_enable,
  124. .mode_valid = sun4i_rgb_mode_valid,
  125. };
  126. static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
  127. {
  128. drm_encoder_cleanup(encoder);
  129. }
  130. static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
  131. .destroy = sun4i_rgb_enc_destroy,
  132. };
  133. int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
  134. {
  135. struct drm_encoder *encoder;
  136. struct drm_bridge *bridge;
  137. struct sun4i_rgb *rgb;
  138. int ret;
  139. rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
  140. if (!rgb)
  141. return -ENOMEM;
  142. rgb->tcon = tcon;
  143. encoder = &rgb->encoder;
  144. ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
  145. &tcon->panel, &bridge);
  146. if (ret) {
  147. dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
  148. return 0;
  149. }
  150. drm_encoder_helper_add(&rgb->encoder,
  151. &sun4i_rgb_enc_helper_funcs);
  152. ret = drm_encoder_init(drm,
  153. &rgb->encoder,
  154. &sun4i_rgb_enc_funcs,
  155. DRM_MODE_ENCODER_NONE,
  156. NULL);
  157. if (ret) {
  158. dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
  159. goto err_out;
  160. }
  161. /* The RGB encoder can only work with the TCON channel 0 */
  162. rgb->encoder.possible_crtcs = drm_crtc_mask(&tcon->crtc->crtc);
  163. if (tcon->panel) {
  164. drm_connector_helper_add(&rgb->connector,
  165. &sun4i_rgb_con_helper_funcs);
  166. ret = drm_connector_init(drm, &rgb->connector,
  167. &sun4i_rgb_con_funcs,
  168. DRM_MODE_CONNECTOR_Unknown);
  169. if (ret) {
  170. dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
  171. goto err_cleanup_connector;
  172. }
  173. drm_connector_attach_encoder(&rgb->connector,
  174. &rgb->encoder);
  175. ret = drm_panel_attach(tcon->panel, &rgb->connector);
  176. if (ret) {
  177. dev_err(drm->dev, "Couldn't attach our panel\n");
  178. goto err_cleanup_connector;
  179. }
  180. }
  181. if (bridge) {
  182. ret = drm_bridge_attach(encoder, bridge, NULL);
  183. if (ret) {
  184. dev_err(drm->dev, "Couldn't attach our bridge\n");
  185. goto err_cleanup_connector;
  186. }
  187. }
  188. return 0;
  189. err_cleanup_connector:
  190. drm_encoder_cleanup(&rgb->encoder);
  191. err_out:
  192. return ret;
  193. }
  194. EXPORT_SYMBOL(sun4i_rgb_init);