sun4i_rgb.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include "sun4i_rgb.h"
  20. struct sun4i_rgb {
  21. struct drm_connector connector;
  22. struct drm_encoder encoder;
  23. struct sun4i_drv *drv;
  24. };
  25. static inline struct sun4i_rgb *
  26. drm_connector_to_sun4i_rgb(struct drm_connector *connector)
  27. {
  28. return container_of(connector, struct sun4i_rgb,
  29. connector);
  30. }
  31. static inline struct sun4i_rgb *
  32. drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
  33. {
  34. return container_of(encoder, struct sun4i_rgb,
  35. encoder);
  36. }
  37. static int sun4i_rgb_get_modes(struct drm_connector *connector)
  38. {
  39. struct sun4i_rgb *rgb =
  40. drm_connector_to_sun4i_rgb(connector);
  41. struct sun4i_drv *drv = rgb->drv;
  42. struct sun4i_tcon *tcon = drv->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_drv *drv = rgb->drv;
  50. struct sun4i_tcon *tcon = drv->tcon;
  51. u32 hsync = mode->hsync_end - mode->hsync_start;
  52. u32 vsync = mode->vsync_end - mode->vsync_start;
  53. unsigned long rate = mode->clock * 1000;
  54. long rounded_rate;
  55. DRM_DEBUG_DRIVER("Validating modes...\n");
  56. if (hsync < 1)
  57. return MODE_HSYNC_NARROW;
  58. if (hsync > 0x3ff)
  59. return MODE_HSYNC_WIDE;
  60. if ((mode->hdisplay < 1) || (mode->htotal < 1))
  61. return MODE_H_ILLEGAL;
  62. if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
  63. return MODE_BAD_HVALUE;
  64. DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
  65. if (vsync < 1)
  66. return MODE_VSYNC_NARROW;
  67. if (vsync > 0x3ff)
  68. return MODE_VSYNC_WIDE;
  69. if ((mode->vdisplay < 1) || (mode->vtotal < 1))
  70. return MODE_V_ILLEGAL;
  71. if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
  72. return MODE_BAD_VVALUE;
  73. DRM_DEBUG_DRIVER("Vertical parameters OK\n");
  74. rounded_rate = clk_round_rate(tcon->dclk, rate);
  75. if (rounded_rate < rate)
  76. return MODE_CLOCK_LOW;
  77. if (rounded_rate > rate)
  78. return MODE_CLOCK_HIGH;
  79. DRM_DEBUG_DRIVER("Clock rate OK\n");
  80. return MODE_OK;
  81. }
  82. static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
  83. .get_modes = sun4i_rgb_get_modes,
  84. .mode_valid = sun4i_rgb_mode_valid,
  85. };
  86. static enum drm_connector_status
  87. sun4i_rgb_connector_detect(struct drm_connector *connector, bool force)
  88. {
  89. return connector_status_connected;
  90. }
  91. static void
  92. sun4i_rgb_connector_destroy(struct drm_connector *connector)
  93. {
  94. struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
  95. struct sun4i_drv *drv = rgb->drv;
  96. struct sun4i_tcon *tcon = drv->tcon;
  97. drm_panel_detach(tcon->panel);
  98. drm_connector_cleanup(connector);
  99. }
  100. static struct drm_connector_funcs sun4i_rgb_con_funcs = {
  101. .dpms = drm_atomic_helper_connector_dpms,
  102. .detect = sun4i_rgb_connector_detect,
  103. .fill_modes = drm_helper_probe_single_connector_modes,
  104. .destroy = sun4i_rgb_connector_destroy,
  105. .reset = drm_atomic_helper_connector_reset,
  106. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  107. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  108. };
  109. static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
  110. struct drm_crtc_state *crtc_state,
  111. struct drm_connector_state *conn_state)
  112. {
  113. return 0;
  114. }
  115. static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
  116. {
  117. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  118. struct sun4i_drv *drv = rgb->drv;
  119. struct sun4i_tcon *tcon = drv->tcon;
  120. DRM_DEBUG_DRIVER("Enabling RGB output\n");
  121. if (!IS_ERR(tcon->panel))
  122. drm_panel_prepare(tcon->panel);
  123. sun4i_tcon_channel_enable(tcon, 0);
  124. if (!IS_ERR(tcon->panel))
  125. drm_panel_enable(tcon->panel);
  126. }
  127. static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
  128. {
  129. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  130. struct sun4i_drv *drv = rgb->drv;
  131. struct sun4i_tcon *tcon = drv->tcon;
  132. DRM_DEBUG_DRIVER("Disabling RGB output\n");
  133. if (!IS_ERR(tcon->panel))
  134. drm_panel_disable(tcon->panel);
  135. sun4i_tcon_channel_disable(tcon, 0);
  136. if (!IS_ERR(tcon->panel))
  137. drm_panel_unprepare(tcon->panel);
  138. }
  139. static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
  140. struct drm_display_mode *mode,
  141. struct drm_display_mode *adjusted_mode)
  142. {
  143. struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
  144. struct sun4i_drv *drv = rgb->drv;
  145. struct sun4i_tcon *tcon = drv->tcon;
  146. sun4i_tcon0_mode_set(tcon, mode);
  147. clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
  148. /* FIXME: This seems to be board specific */
  149. clk_set_phase(tcon->dclk, 120);
  150. }
  151. static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
  152. .atomic_check = sun4i_rgb_atomic_check,
  153. .mode_set = sun4i_rgb_encoder_mode_set,
  154. .disable = sun4i_rgb_encoder_disable,
  155. .enable = sun4i_rgb_encoder_enable,
  156. };
  157. static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
  158. {
  159. drm_encoder_cleanup(encoder);
  160. }
  161. static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
  162. .destroy = sun4i_rgb_enc_destroy,
  163. };
  164. int sun4i_rgb_init(struct drm_device *drm)
  165. {
  166. struct sun4i_drv *drv = drm->dev_private;
  167. struct sun4i_tcon *tcon = drv->tcon;
  168. struct drm_encoder *encoder;
  169. struct sun4i_rgb *rgb;
  170. int ret;
  171. rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
  172. if (!rgb)
  173. return -ENOMEM;
  174. rgb->drv = drv;
  175. encoder = &rgb->encoder;
  176. tcon->panel = sun4i_tcon_find_panel(tcon->dev->of_node);
  177. encoder->bridge = sun4i_tcon_find_bridge(tcon->dev->of_node);
  178. if (IS_ERR(tcon->panel) && IS_ERR(encoder->bridge)) {
  179. dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
  180. return 0;
  181. }
  182. drm_encoder_helper_add(&rgb->encoder,
  183. &sun4i_rgb_enc_helper_funcs);
  184. ret = drm_encoder_init(drm,
  185. &rgb->encoder,
  186. &sun4i_rgb_enc_funcs,
  187. DRM_MODE_ENCODER_NONE,
  188. NULL);
  189. if (ret) {
  190. dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
  191. goto err_out;
  192. }
  193. /* The RGB encoder can only work with the TCON channel 0 */
  194. rgb->encoder.possible_crtcs = BIT(0);
  195. if (!IS_ERR(tcon->panel)) {
  196. drm_connector_helper_add(&rgb->connector,
  197. &sun4i_rgb_con_helper_funcs);
  198. ret = drm_connector_init(drm, &rgb->connector,
  199. &sun4i_rgb_con_funcs,
  200. DRM_MODE_CONNECTOR_Unknown);
  201. if (ret) {
  202. dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
  203. goto err_cleanup_connector;
  204. }
  205. drm_mode_connector_attach_encoder(&rgb->connector,
  206. &rgb->encoder);
  207. ret = drm_panel_attach(tcon->panel, &rgb->connector);
  208. if (ret) {
  209. dev_err(drm->dev, "Couldn't attach our panel\n");
  210. goto err_cleanup_connector;
  211. }
  212. }
  213. if (!IS_ERR(encoder->bridge)) {
  214. encoder->bridge->encoder = &rgb->encoder;
  215. ret = drm_bridge_attach(drm, encoder->bridge);
  216. if (ret) {
  217. dev_err(drm->dev, "Couldn't attach our bridge\n");
  218. goto err_cleanup_connector;
  219. }
  220. } else {
  221. encoder->bridge = NULL;
  222. }
  223. return 0;
  224. err_cleanup_connector:
  225. drm_encoder_cleanup(&rgb->encoder);
  226. err_out:
  227. return ret;
  228. }
  229. EXPORT_SYMBOL(sun4i_rgb_init);