sun4i_rgb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_panel.h>
  17. #include "sun4i_drv.h"
  18. #include "sun4i_tcon.h"
  19. struct sun4i_rgb {
  20. struct drm_connector connector;
  21. struct drm_encoder encoder;
  22. struct sun4i_drv *drv;
  23. };
  24. static inline struct sun4i_rgb *
  25. drm_connector_to_sun4i_rgb(struct drm_connector *connector)
  26. {
  27. return container_of(connector, struct sun4i_rgb,
  28. connector);
  29. }
  30. static inline struct sun4i_rgb *
  31. drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
  32. {
  33. return container_of(encoder, struct sun4i_rgb,
  34. encoder);
  35. }
  36. static int sun4i_rgb_get_modes(struct drm_connector *connector)
  37. {
  38. struct sun4i_rgb *rgb =
  39. drm_connector_to_sun4i_rgb(connector);
  40. struct sun4i_drv *drv = rgb->drv;
  41. struct sun4i_tcon *tcon = drv->tcon;
  42. return drm_panel_get_modes(tcon->panel);
  43. }
  44. static int sun4i_rgb_mode_valid(struct drm_connector *connector,
  45. struct drm_display_mode *mode)
  46. {
  47. u32 hsync = mode->hsync_end - mode->hsync_start;
  48. u32 vsync = mode->vsync_end - mode->vsync_start;
  49. DRM_DEBUG_DRIVER("Validating modes...\n");
  50. if (hsync < 1)
  51. return MODE_HSYNC_NARROW;
  52. if (hsync > 0x3ff)
  53. return MODE_HSYNC_WIDE;
  54. if ((mode->hdisplay < 1) || (mode->htotal < 1))
  55. return MODE_H_ILLEGAL;
  56. if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
  57. return MODE_BAD_HVALUE;
  58. DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
  59. if (vsync < 1)
  60. return MODE_VSYNC_NARROW;
  61. if (vsync > 0x3ff)
  62. return MODE_VSYNC_WIDE;
  63. if ((mode->vdisplay < 1) || (mode->vtotal < 1))
  64. return MODE_V_ILLEGAL;
  65. if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
  66. return MODE_BAD_VVALUE;
  67. DRM_DEBUG_DRIVER("Vertical parameters OK\n");
  68. return MODE_OK;
  69. }
  70. static struct drm_encoder *
  71. sun4i_rgb_best_encoder(struct drm_connector *connector)
  72. {
  73. struct sun4i_rgb *rgb =
  74. drm_connector_to_sun4i_rgb(connector);
  75. return &rgb->encoder;
  76. }
  77. static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
  78. .get_modes = sun4i_rgb_get_modes,
  79. .mode_valid = sun4i_rgb_mode_valid,
  80. .best_encoder = sun4i_rgb_best_encoder,
  81. };
  82. static enum drm_connector_status
  83. sun4i_rgb_connector_detect(struct drm_connector *connector, bool force)
  84. {
  85. return connector_status_connected;
  86. }
  87. static void
  88. sun4i_rgb_connector_destroy(struct drm_connector *connector)
  89. {
  90. struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
  91. struct sun4i_drv *drv = rgb->drv;
  92. struct sun4i_tcon *tcon = drv->tcon;
  93. drm_panel_detach(tcon->panel);
  94. drm_connector_cleanup(connector);
  95. }
  96. static struct drm_connector_funcs sun4i_rgb_con_funcs = {
  97. .dpms = drm_atomic_helper_connector_dpms,
  98. .detect = sun4i_rgb_connector_detect,
  99. .fill_modes = drm_helper_probe_single_connector_modes,
  100. .destroy = sun4i_rgb_connector_destroy,
  101. .reset = drm_atomic_helper_connector_reset,
  102. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  103. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  104. };
  105. static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
  106. struct drm_crtc_state *crtc_state,
  107. struct drm_connector_state *conn_state)
  108. {
  109. return 0;
  110. }
  111. static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
  112. {
  113. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  114. struct sun4i_drv *drv = rgb->drv;
  115. struct sun4i_tcon *tcon = drv->tcon;
  116. DRM_DEBUG_DRIVER("Enabling RGB output\n");
  117. drm_panel_enable(tcon->panel);
  118. sun4i_tcon_channel_enable(tcon, 0);
  119. }
  120. static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
  121. {
  122. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  123. struct sun4i_drv *drv = rgb->drv;
  124. struct sun4i_tcon *tcon = drv->tcon;
  125. DRM_DEBUG_DRIVER("Disabling RGB output\n");
  126. sun4i_tcon_channel_disable(tcon, 0);
  127. drm_panel_disable(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_drv *drv = rgb->drv;
  135. struct sun4i_tcon *tcon = drv->tcon;
  136. sun4i_tcon0_mode_set(tcon, mode);
  137. clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
  138. /* FIXME: This seems to be board specific */
  139. clk_set_phase(tcon->dclk, 120);
  140. }
  141. static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
  142. .atomic_check = sun4i_rgb_atomic_check,
  143. .mode_set = sun4i_rgb_encoder_mode_set,
  144. .disable = sun4i_rgb_encoder_disable,
  145. .enable = sun4i_rgb_encoder_enable,
  146. };
  147. static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
  148. {
  149. drm_encoder_cleanup(encoder);
  150. }
  151. static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
  152. .destroy = sun4i_rgb_enc_destroy,
  153. };
  154. int sun4i_rgb_init(struct drm_device *drm)
  155. {
  156. struct sun4i_drv *drv = drm->dev_private;
  157. struct sun4i_tcon *tcon = drv->tcon;
  158. struct sun4i_rgb *rgb;
  159. int ret;
  160. /* If we don't have a panel, there's no point in going on */
  161. if (!tcon->panel)
  162. return -ENODEV;
  163. rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
  164. if (!rgb)
  165. return -ENOMEM;
  166. rgb->drv = drv;
  167. drm_encoder_helper_add(&rgb->encoder,
  168. &sun4i_rgb_enc_helper_funcs);
  169. ret = drm_encoder_init(drm,
  170. &rgb->encoder,
  171. &sun4i_rgb_enc_funcs,
  172. DRM_MODE_ENCODER_NONE,
  173. NULL);
  174. if (ret) {
  175. dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
  176. goto err_out;
  177. }
  178. /* The RGB encoder can only work with the TCON channel 0 */
  179. rgb->encoder.possible_crtcs = BIT(0);
  180. drm_connector_helper_add(&rgb->connector,
  181. &sun4i_rgb_con_helper_funcs);
  182. ret = drm_connector_init(drm, &rgb->connector,
  183. &sun4i_rgb_con_funcs,
  184. DRM_MODE_CONNECTOR_Unknown);
  185. if (ret) {
  186. dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
  187. goto err_cleanup_connector;
  188. }
  189. drm_mode_connector_attach_encoder(&rgb->connector, &rgb->encoder);
  190. drm_panel_attach(tcon->panel, &rgb->connector);
  191. return 0;
  192. err_cleanup_connector:
  193. drm_encoder_cleanup(&rgb->encoder);
  194. err_out:
  195. return ret;
  196. }
  197. EXPORT_SYMBOL(sun4i_rgb_init);