sun4i_rgb.c 6.7 KB

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