rcar_du_hdmienc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * R-Car Display Unit HDMI Encoder
  3. *
  4. * Copyright (C) 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/slab.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_hdmienc.h"
  20. #include "rcar_du_lvdsenc.h"
  21. struct rcar_du_hdmienc {
  22. struct rcar_du_encoder *renc;
  23. bool enabled;
  24. };
  25. #define to_rcar_hdmienc(e) (to_rcar_encoder(e)->hdmi)
  26. static void rcar_du_hdmienc_disable(struct drm_encoder *encoder)
  27. {
  28. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  29. if (hdmienc->renc->lvds)
  30. rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
  31. false);
  32. hdmienc->enabled = false;
  33. }
  34. static void rcar_du_hdmienc_enable(struct drm_encoder *encoder)
  35. {
  36. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  37. if (hdmienc->renc->lvds)
  38. rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
  39. true);
  40. hdmienc->enabled = true;
  41. }
  42. static int rcar_du_hdmienc_atomic_check(struct drm_encoder *encoder,
  43. struct drm_crtc_state *crtc_state,
  44. struct drm_connector_state *conn_state)
  45. {
  46. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  47. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  48. if (hdmienc->renc->lvds)
  49. rcar_du_lvdsenc_atomic_check(hdmienc->renc->lvds,
  50. adjusted_mode);
  51. return 0;
  52. }
  53. static void rcar_du_hdmienc_mode_set(struct drm_encoder *encoder,
  54. struct drm_display_mode *mode,
  55. struct drm_display_mode *adjusted_mode)
  56. {
  57. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  58. rcar_du_crtc_route_output(encoder->crtc, hdmienc->renc->output);
  59. }
  60. static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
  61. .mode_set = rcar_du_hdmienc_mode_set,
  62. .disable = rcar_du_hdmienc_disable,
  63. .enable = rcar_du_hdmienc_enable,
  64. .atomic_check = rcar_du_hdmienc_atomic_check,
  65. };
  66. static void rcar_du_hdmienc_cleanup(struct drm_encoder *encoder)
  67. {
  68. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  69. if (hdmienc->enabled)
  70. rcar_du_hdmienc_disable(encoder);
  71. drm_encoder_cleanup(encoder);
  72. }
  73. static const struct drm_encoder_funcs encoder_funcs = {
  74. .destroy = rcar_du_hdmienc_cleanup,
  75. };
  76. int rcar_du_hdmienc_init(struct rcar_du_device *rcdu,
  77. struct rcar_du_encoder *renc, struct device_node *np)
  78. {
  79. struct drm_encoder *encoder = rcar_encoder_to_drm_encoder(renc);
  80. struct drm_bridge *bridge;
  81. struct rcar_du_hdmienc *hdmienc;
  82. int ret;
  83. hdmienc = devm_kzalloc(rcdu->dev, sizeof(*hdmienc), GFP_KERNEL);
  84. if (hdmienc == NULL)
  85. return -ENOMEM;
  86. /* Locate the DRM bridge from the HDMI encoder DT node. */
  87. bridge = of_drm_find_bridge(np);
  88. if (!bridge)
  89. return -EPROBE_DEFER;
  90. ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
  91. DRM_MODE_ENCODER_TMDS, NULL);
  92. if (ret < 0)
  93. return ret;
  94. drm_encoder_helper_add(encoder, &encoder_helper_funcs);
  95. renc->hdmi = hdmienc;
  96. hdmienc->renc = renc;
  97. /* Link the bridge to the encoder. */
  98. ret = drm_bridge_attach(encoder, bridge, NULL);
  99. if (ret) {
  100. drm_encoder_cleanup(encoder);
  101. return ret;
  102. }
  103. return 0;
  104. }