rcar_du_encoder.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * rcar_du_encoder.c -- R-Car Display Unit Encoder
  3. *
  4. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/export.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include "rcar_du_drv.h"
  18. #include "rcar_du_encoder.h"
  19. #include "rcar_du_hdmicon.h"
  20. #include "rcar_du_hdmienc.h"
  21. #include "rcar_du_kms.h"
  22. #include "rcar_du_lvdscon.h"
  23. #include "rcar_du_lvdsenc.h"
  24. #include "rcar_du_vgacon.h"
  25. /* -----------------------------------------------------------------------------
  26. * Common connector functions
  27. */
  28. struct drm_encoder *
  29. rcar_du_connector_best_encoder(struct drm_connector *connector)
  30. {
  31. struct rcar_du_connector *rcon = to_rcar_connector(connector);
  32. return rcar_encoder_to_drm_encoder(rcon->encoder);
  33. }
  34. /* -----------------------------------------------------------------------------
  35. * Encoder
  36. */
  37. static void rcar_du_encoder_disable(struct drm_encoder *encoder)
  38. {
  39. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  40. if (renc->lvds)
  41. rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, false);
  42. }
  43. static void rcar_du_encoder_enable(struct drm_encoder *encoder)
  44. {
  45. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  46. if (renc->lvds)
  47. rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, true);
  48. }
  49. static int rcar_du_encoder_atomic_check(struct drm_encoder *encoder,
  50. struct drm_crtc_state *crtc_state,
  51. struct drm_connector_state *conn_state)
  52. {
  53. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  54. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  55. const struct drm_display_mode *mode = &crtc_state->mode;
  56. const struct drm_display_mode *panel_mode;
  57. struct drm_connector *connector = conn_state->connector;
  58. struct drm_device *dev = encoder->dev;
  59. /* DAC encoders have currently no restriction on the mode. */
  60. if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
  61. return 0;
  62. if (list_empty(&connector->modes)) {
  63. dev_dbg(dev->dev, "encoder: empty modes list\n");
  64. return -EINVAL;
  65. }
  66. panel_mode = list_first_entry(&connector->modes,
  67. struct drm_display_mode, head);
  68. /* We're not allowed to modify the resolution. */
  69. if (mode->hdisplay != panel_mode->hdisplay ||
  70. mode->vdisplay != panel_mode->vdisplay)
  71. return -EINVAL;
  72. /* The flat panel mode is fixed, just copy it to the adjusted mode. */
  73. drm_mode_copy(adjusted_mode, panel_mode);
  74. /* The internal LVDS encoder has a clock frequency operating range of
  75. * 30MHz to 150MHz. Clamp the clock accordingly.
  76. */
  77. if (renc->lvds)
  78. adjusted_mode->clock = clamp(adjusted_mode->clock,
  79. 30000, 150000);
  80. return 0;
  81. }
  82. static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
  83. struct drm_display_mode *mode,
  84. struct drm_display_mode *adjusted_mode)
  85. {
  86. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  87. rcar_du_crtc_route_output(encoder->crtc, renc->output);
  88. }
  89. static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
  90. .mode_set = rcar_du_encoder_mode_set,
  91. .disable = rcar_du_encoder_disable,
  92. .enable = rcar_du_encoder_enable,
  93. .atomic_check = rcar_du_encoder_atomic_check,
  94. };
  95. static const struct drm_encoder_funcs encoder_funcs = {
  96. .destroy = drm_encoder_cleanup,
  97. };
  98. int rcar_du_encoder_init(struct rcar_du_device *rcdu,
  99. enum rcar_du_encoder_type type,
  100. enum rcar_du_output output,
  101. struct device_node *enc_node,
  102. struct device_node *con_node)
  103. {
  104. struct rcar_du_encoder *renc;
  105. struct drm_encoder *encoder;
  106. unsigned int encoder_type;
  107. int ret;
  108. renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
  109. if (renc == NULL)
  110. return -ENOMEM;
  111. renc->output = output;
  112. encoder = rcar_encoder_to_drm_encoder(renc);
  113. switch (output) {
  114. case RCAR_DU_OUTPUT_LVDS0:
  115. renc->lvds = rcdu->lvds[0];
  116. break;
  117. case RCAR_DU_OUTPUT_LVDS1:
  118. renc->lvds = rcdu->lvds[1];
  119. break;
  120. default:
  121. break;
  122. }
  123. switch (type) {
  124. case RCAR_DU_ENCODER_VGA:
  125. encoder_type = DRM_MODE_ENCODER_DAC;
  126. break;
  127. case RCAR_DU_ENCODER_LVDS:
  128. encoder_type = DRM_MODE_ENCODER_LVDS;
  129. break;
  130. case RCAR_DU_ENCODER_HDMI:
  131. encoder_type = DRM_MODE_ENCODER_TMDS;
  132. break;
  133. case RCAR_DU_ENCODER_NONE:
  134. default:
  135. /* No external encoder, use the internal encoder type. */
  136. encoder_type = rcdu->info->routes[output].encoder_type;
  137. break;
  138. }
  139. if (type == RCAR_DU_ENCODER_HDMI) {
  140. ret = rcar_du_hdmienc_init(rcdu, renc, enc_node);
  141. if (ret < 0)
  142. goto done;
  143. } else {
  144. ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
  145. encoder_type);
  146. if (ret < 0)
  147. goto done;
  148. drm_encoder_helper_add(encoder, &encoder_helper_funcs);
  149. }
  150. switch (encoder_type) {
  151. case DRM_MODE_ENCODER_LVDS:
  152. ret = rcar_du_lvds_connector_init(rcdu, renc, con_node);
  153. break;
  154. case DRM_MODE_ENCODER_DAC:
  155. ret = rcar_du_vga_connector_init(rcdu, renc);
  156. break;
  157. case DRM_MODE_ENCODER_TMDS:
  158. ret = rcar_du_hdmi_connector_init(rcdu, renc);
  159. break;
  160. default:
  161. ret = -EINVAL;
  162. break;
  163. }
  164. done:
  165. if (ret < 0) {
  166. if (encoder->name)
  167. encoder->funcs->destroy(encoder);
  168. devm_kfree(rcdu->dev, renc);
  169. }
  170. return ret;
  171. }