rcar_du_encoder.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * rcar_du_encoder.c -- R-Car Display Unit Encoder
  3. *
  4. * Copyright (C) 2013 Renesas 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 <drm/drmP.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "rcar_du_drv.h"
  17. #include "rcar_du_encoder.h"
  18. #include "rcar_du_kms.h"
  19. #include "rcar_du_lvdscon.h"
  20. #include "rcar_du_vgacon.h"
  21. /* -----------------------------------------------------------------------------
  22. * Common connector functions
  23. */
  24. struct drm_encoder *
  25. rcar_du_connector_best_encoder(struct drm_connector *connector)
  26. {
  27. struct rcar_du_connector *rcon = to_rcar_connector(connector);
  28. return &rcon->encoder->encoder;
  29. }
  30. /* -----------------------------------------------------------------------------
  31. * Encoder
  32. */
  33. static void rcar_du_encoder_dpms(struct drm_encoder *encoder, int mode)
  34. {
  35. }
  36. static bool rcar_du_encoder_mode_fixup(struct drm_encoder *encoder,
  37. const struct drm_display_mode *mode,
  38. struct drm_display_mode *adjusted_mode)
  39. {
  40. const struct drm_display_mode *panel_mode;
  41. struct drm_device *dev = encoder->dev;
  42. struct drm_connector *connector;
  43. bool found = false;
  44. /* DAC encoders have currently no restriction on the mode. */
  45. if (encoder->encoder_type == DRM_MODE_ENCODER_DAC)
  46. return true;
  47. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  48. if (connector->encoder == encoder) {
  49. found = true;
  50. break;
  51. }
  52. }
  53. if (!found) {
  54. dev_dbg(dev->dev, "mode_fixup: no connector found\n");
  55. return false;
  56. }
  57. if (list_empty(&connector->modes)) {
  58. dev_dbg(dev->dev, "mode_fixup: empty modes list\n");
  59. return false;
  60. }
  61. panel_mode = list_first_entry(&connector->modes,
  62. struct drm_display_mode, head);
  63. /* We're not allowed to modify the resolution. */
  64. if (mode->hdisplay != panel_mode->hdisplay ||
  65. mode->vdisplay != panel_mode->vdisplay)
  66. return false;
  67. /* The flat panel mode is fixed, just copy it to the adjusted mode. */
  68. drm_mode_copy(adjusted_mode, panel_mode);
  69. return true;
  70. }
  71. static void rcar_du_encoder_mode_prepare(struct drm_encoder *encoder)
  72. {
  73. }
  74. static void rcar_du_encoder_mode_commit(struct drm_encoder *encoder)
  75. {
  76. }
  77. static void rcar_du_encoder_mode_set(struct drm_encoder *encoder,
  78. struct drm_display_mode *mode,
  79. struct drm_display_mode *adjusted_mode)
  80. {
  81. struct rcar_du_encoder *renc = to_rcar_encoder(encoder);
  82. rcar_du_crtc_route_output(encoder->crtc, renc->output);
  83. }
  84. static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
  85. .dpms = rcar_du_encoder_dpms,
  86. .mode_fixup = rcar_du_encoder_mode_fixup,
  87. .prepare = rcar_du_encoder_mode_prepare,
  88. .commit = rcar_du_encoder_mode_commit,
  89. .mode_set = rcar_du_encoder_mode_set,
  90. };
  91. static const struct drm_encoder_funcs encoder_funcs = {
  92. .destroy = drm_encoder_cleanup,
  93. };
  94. int rcar_du_encoder_init(struct rcar_du_device *rcdu,
  95. enum rcar_du_encoder_type type,
  96. enum rcar_du_output output,
  97. const struct rcar_du_encoder_data *data)
  98. {
  99. struct rcar_du_encoder *renc;
  100. unsigned int encoder_type;
  101. int ret;
  102. renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL);
  103. if (renc == NULL)
  104. return -ENOMEM;
  105. renc->output = output;
  106. switch (type) {
  107. case RCAR_DU_ENCODER_VGA:
  108. encoder_type = DRM_MODE_ENCODER_DAC;
  109. break;
  110. case RCAR_DU_ENCODER_LVDS:
  111. encoder_type = DRM_MODE_ENCODER_LVDS;
  112. break;
  113. case RCAR_DU_ENCODER_NONE:
  114. default:
  115. /* No external encoder, use the internal encoder type. */
  116. encoder_type = rcdu->info->routes[output].encoder_type;
  117. break;
  118. }
  119. ret = drm_encoder_init(rcdu->ddev, &renc->encoder, &encoder_funcs,
  120. encoder_type);
  121. if (ret < 0)
  122. return ret;
  123. drm_encoder_helper_add(&renc->encoder, &encoder_helper_funcs);
  124. switch (encoder_type) {
  125. case DRM_MODE_ENCODER_LVDS:
  126. return rcar_du_lvds_connector_init(rcdu, renc,
  127. &data->connector.lvds.panel);
  128. case DRM_MODE_ENCODER_DAC:
  129. return rcar_du_vga_connector_init(rcdu, renc);
  130. default:
  131. return -EINVAL;
  132. }
  133. }