rcar_du_hdmienc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <drm/drm_encoder_slave.h>
  18. #include "rcar_du_drv.h"
  19. #include "rcar_du_encoder.h"
  20. #include "rcar_du_hdmienc.h"
  21. #include "rcar_du_lvdsenc.h"
  22. struct rcar_du_hdmienc {
  23. struct rcar_du_encoder *renc;
  24. struct device *dev;
  25. bool enabled;
  26. };
  27. #define to_rcar_hdmienc(e) (to_rcar_encoder(e)->hdmi)
  28. #define to_slave_funcs(e) (to_rcar_encoder(e)->slave.slave_funcs)
  29. static void rcar_du_hdmienc_disable(struct drm_encoder *encoder)
  30. {
  31. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  32. struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);
  33. if (sfuncs->dpms)
  34. sfuncs->dpms(encoder, DRM_MODE_DPMS_OFF);
  35. if (hdmienc->renc->lvds)
  36. rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
  37. false);
  38. hdmienc->enabled = false;
  39. }
  40. static void rcar_du_hdmienc_enable(struct drm_encoder *encoder)
  41. {
  42. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  43. struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);
  44. if (hdmienc->renc->lvds)
  45. rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc,
  46. true);
  47. if (sfuncs->dpms)
  48. sfuncs->dpms(encoder, DRM_MODE_DPMS_ON);
  49. hdmienc->enabled = true;
  50. }
  51. static int rcar_du_hdmienc_atomic_check(struct drm_encoder *encoder,
  52. struct drm_crtc_state *crtc_state,
  53. struct drm_connector_state *conn_state)
  54. {
  55. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  56. struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);
  57. struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
  58. const struct drm_display_mode *mode = &crtc_state->mode;
  59. /* The internal LVDS encoder has a clock frequency operating range of
  60. * 30MHz to 150MHz. Clamp the clock accordingly.
  61. */
  62. if (hdmienc->renc->lvds)
  63. adjusted_mode->clock = clamp(adjusted_mode->clock,
  64. 30000, 150000);
  65. if (sfuncs->mode_fixup == NULL)
  66. return 0;
  67. return sfuncs->mode_fixup(encoder, mode, adjusted_mode) ? 0 : -EINVAL;
  68. }
  69. static void rcar_du_hdmienc_mode_set(struct drm_encoder *encoder,
  70. struct drm_display_mode *mode,
  71. struct drm_display_mode *adjusted_mode)
  72. {
  73. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  74. struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder);
  75. if (sfuncs->mode_set)
  76. sfuncs->mode_set(encoder, mode, adjusted_mode);
  77. rcar_du_crtc_route_output(encoder->crtc, hdmienc->renc->output);
  78. }
  79. static const struct drm_encoder_helper_funcs encoder_helper_funcs = {
  80. .mode_set = rcar_du_hdmienc_mode_set,
  81. .disable = rcar_du_hdmienc_disable,
  82. .enable = rcar_du_hdmienc_enable,
  83. .atomic_check = rcar_du_hdmienc_atomic_check,
  84. };
  85. static void rcar_du_hdmienc_cleanup(struct drm_encoder *encoder)
  86. {
  87. struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder);
  88. if (hdmienc->enabled)
  89. rcar_du_hdmienc_disable(encoder);
  90. drm_encoder_cleanup(encoder);
  91. put_device(hdmienc->dev);
  92. }
  93. static const struct drm_encoder_funcs encoder_funcs = {
  94. .destroy = rcar_du_hdmienc_cleanup,
  95. };
  96. int rcar_du_hdmienc_init(struct rcar_du_device *rcdu,
  97. struct rcar_du_encoder *renc, struct device_node *np)
  98. {
  99. struct drm_encoder *encoder = rcar_encoder_to_drm_encoder(renc);
  100. struct drm_i2c_encoder_driver *driver;
  101. struct i2c_client *i2c_slave;
  102. struct rcar_du_hdmienc *hdmienc;
  103. int ret;
  104. hdmienc = devm_kzalloc(rcdu->dev, sizeof(*hdmienc), GFP_KERNEL);
  105. if (hdmienc == NULL)
  106. return -ENOMEM;
  107. /* Locate the slave I2C device and driver. */
  108. i2c_slave = of_find_i2c_device_by_node(np);
  109. if (!i2c_slave || !i2c_get_clientdata(i2c_slave))
  110. return -EPROBE_DEFER;
  111. hdmienc->dev = &i2c_slave->dev;
  112. if (hdmienc->dev->driver == NULL) {
  113. ret = -EPROBE_DEFER;
  114. goto error;
  115. }
  116. /* Initialize the slave encoder. */
  117. driver = to_drm_i2c_encoder_driver(to_i2c_driver(hdmienc->dev->driver));
  118. ret = driver->encoder_init(i2c_slave, rcdu->ddev, &renc->slave);
  119. if (ret < 0)
  120. goto error;
  121. ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs,
  122. DRM_MODE_ENCODER_TMDS);
  123. if (ret < 0)
  124. goto error;
  125. drm_encoder_helper_add(encoder, &encoder_helper_funcs);
  126. renc->hdmi = hdmienc;
  127. hdmienc->renc = renc;
  128. return 0;
  129. error:
  130. put_device(hdmienc->dev);
  131. return ret;
  132. }