sun4i_rgb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. sun4i_tcon_channel_enable(tcon, 0);
  108. if (!IS_ERR(tcon->panel))
  109. drm_panel_enable(tcon->panel);
  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. sun4i_tcon_channel_disable(tcon, 0);
  119. if (!IS_ERR(tcon->panel))
  120. drm_panel_unprepare(tcon->panel);
  121. }
  122. static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
  123. struct drm_display_mode *mode,
  124. struct drm_display_mode *adjusted_mode)
  125. {
  126. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  127. struct sun4i_tcon *tcon = rgb->tcon;
  128. sun4i_tcon0_mode_set(tcon, mode);
  129. sun4i_tcon_set_mux(tcon, 0, encoder);
  130. /* FIXME: This seems to be board specific */
  131. clk_set_phase(tcon->dclk, 120);
  132. }
  133. static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
  134. .mode_set = sun4i_rgb_encoder_mode_set,
  135. .disable = sun4i_rgb_encoder_disable,
  136. .enable = sun4i_rgb_encoder_enable,
  137. };
  138. static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
  139. {
  140. drm_encoder_cleanup(encoder);
  141. }
  142. static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
  143. .destroy = sun4i_rgb_enc_destroy,
  144. };
  145. int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
  146. {
  147. struct drm_encoder *encoder;
  148. struct drm_bridge *bridge;
  149. struct sun4i_rgb *rgb;
  150. int ret;
  151. rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
  152. if (!rgb)
  153. return -ENOMEM;
  154. rgb->tcon = tcon;
  155. encoder = &rgb->encoder;
  156. ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
  157. &tcon->panel, &bridge);
  158. if (ret) {
  159. dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
  160. return 0;
  161. }
  162. drm_encoder_helper_add(&rgb->encoder,
  163. &sun4i_rgb_enc_helper_funcs);
  164. ret = drm_encoder_init(drm,
  165. &rgb->encoder,
  166. &sun4i_rgb_enc_funcs,
  167. DRM_MODE_ENCODER_NONE,
  168. NULL);
  169. if (ret) {
  170. dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
  171. goto err_out;
  172. }
  173. /* The RGB encoder can only work with the TCON channel 0 */
  174. rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
  175. if (tcon->panel) {
  176. drm_connector_helper_add(&rgb->connector,
  177. &sun4i_rgb_con_helper_funcs);
  178. ret = drm_connector_init(drm, &rgb->connector,
  179. &sun4i_rgb_con_funcs,
  180. DRM_MODE_CONNECTOR_Unknown);
  181. if (ret) {
  182. dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
  183. goto err_cleanup_connector;
  184. }
  185. drm_mode_connector_attach_encoder(&rgb->connector,
  186. &rgb->encoder);
  187. ret = drm_panel_attach(tcon->panel, &rgb->connector);
  188. if (ret) {
  189. dev_err(drm->dev, "Couldn't attach our panel\n");
  190. goto err_cleanup_connector;
  191. }
  192. }
  193. if (bridge) {
  194. ret = drm_bridge_attach(encoder, bridge, NULL);
  195. if (ret) {
  196. dev_err(drm->dev, "Couldn't attach our bridge\n");
  197. goto err_cleanup_connector;
  198. }
  199. }
  200. return 0;
  201. err_cleanup_connector:
  202. drm_encoder_cleanup(&rgb->encoder);
  203. err_out:
  204. return ret;
  205. }
  206. EXPORT_SYMBOL(sun4i_rgb_init);