sun4i_rgb.c 7.0 KB

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