arcpgu_hdmi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * ARC PGU DRM driver.
  3. *
  4. * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_encoder_slave.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include "arcpgu.h"
  20. struct arcpgu_drm_connector {
  21. struct drm_connector connector;
  22. struct drm_encoder_slave *encoder_slave;
  23. };
  24. static int arcpgu_drm_connector_get_modes(struct drm_connector *connector)
  25. {
  26. const struct drm_encoder_slave_funcs *sfuncs;
  27. struct drm_encoder_slave *slave;
  28. struct arcpgu_drm_connector *con =
  29. container_of(connector, struct arcpgu_drm_connector, connector);
  30. slave = con->encoder_slave;
  31. if (slave == NULL) {
  32. dev_err(connector->dev->dev,
  33. "connector_get_modes: cannot find slave encoder for connector\n");
  34. return 0;
  35. }
  36. sfuncs = slave->slave_funcs;
  37. if (sfuncs->get_modes == NULL)
  38. return 0;
  39. return sfuncs->get_modes(&slave->base, connector);
  40. }
  41. static enum drm_connector_status
  42. arcpgu_drm_connector_detect(struct drm_connector *connector, bool force)
  43. {
  44. enum drm_connector_status status = connector_status_unknown;
  45. const struct drm_encoder_slave_funcs *sfuncs;
  46. struct drm_encoder_slave *slave;
  47. struct arcpgu_drm_connector *con =
  48. container_of(connector, struct arcpgu_drm_connector, connector);
  49. slave = con->encoder_slave;
  50. if (slave == NULL) {
  51. dev_err(connector->dev->dev,
  52. "connector_detect: cannot find slave encoder for connector\n");
  53. return status;
  54. }
  55. sfuncs = slave->slave_funcs;
  56. if (sfuncs && sfuncs->detect)
  57. return sfuncs->detect(&slave->base, connector);
  58. dev_err(connector->dev->dev, "connector_detect: could not detect slave funcs\n");
  59. return status;
  60. }
  61. static void arcpgu_drm_connector_destroy(struct drm_connector *connector)
  62. {
  63. drm_connector_unregister(connector);
  64. drm_connector_cleanup(connector);
  65. }
  66. static const struct drm_connector_helper_funcs
  67. arcpgu_drm_connector_helper_funcs = {
  68. .get_modes = arcpgu_drm_connector_get_modes,
  69. };
  70. static const struct drm_connector_funcs arcpgu_drm_connector_funcs = {
  71. .dpms = drm_helper_connector_dpms,
  72. .reset = drm_atomic_helper_connector_reset,
  73. .detect = arcpgu_drm_connector_detect,
  74. .fill_modes = drm_helper_probe_single_connector_modes,
  75. .destroy = arcpgu_drm_connector_destroy,
  76. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  77. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  78. };
  79. static struct drm_encoder_helper_funcs arcpgu_drm_encoder_helper_funcs = {
  80. .dpms = drm_i2c_encoder_dpms,
  81. .mode_fixup = drm_i2c_encoder_mode_fixup,
  82. .mode_set = drm_i2c_encoder_mode_set,
  83. .prepare = drm_i2c_encoder_prepare,
  84. .commit = drm_i2c_encoder_commit,
  85. .detect = drm_i2c_encoder_detect,
  86. };
  87. static struct drm_encoder_funcs arcpgu_drm_encoder_funcs = {
  88. .destroy = drm_encoder_cleanup,
  89. };
  90. int arcpgu_drm_hdmi_init(struct drm_device *drm, struct device_node *np)
  91. {
  92. struct arcpgu_drm_connector *arcpgu_connector;
  93. struct drm_i2c_encoder_driver *driver;
  94. struct drm_encoder_slave *encoder;
  95. struct drm_connector *connector;
  96. struct i2c_client *i2c_slave;
  97. int ret;
  98. encoder = devm_kzalloc(drm->dev, sizeof(*encoder), GFP_KERNEL);
  99. if (encoder == NULL)
  100. return -ENOMEM;
  101. i2c_slave = of_find_i2c_device_by_node(np);
  102. if (!i2c_slave || !i2c_get_clientdata(i2c_slave)) {
  103. dev_err(drm->dev, "failed to find i2c slave encoder\n");
  104. return -EPROBE_DEFER;
  105. }
  106. if (i2c_slave->dev.driver == NULL) {
  107. dev_err(drm->dev, "failed to find i2c slave driver\n");
  108. return -EPROBE_DEFER;
  109. }
  110. driver =
  111. to_drm_i2c_encoder_driver(to_i2c_driver(i2c_slave->dev.driver));
  112. ret = driver->encoder_init(i2c_slave, drm, encoder);
  113. if (ret) {
  114. dev_err(drm->dev, "failed to initialize i2c encoder slave\n");
  115. return ret;
  116. }
  117. encoder->base.possible_crtcs = 1;
  118. encoder->base.possible_clones = 0;
  119. ret = drm_encoder_init(drm, &encoder->base, &arcpgu_drm_encoder_funcs,
  120. DRM_MODE_ENCODER_TMDS, NULL);
  121. if (ret)
  122. return ret;
  123. drm_encoder_helper_add(&encoder->base,
  124. &arcpgu_drm_encoder_helper_funcs);
  125. arcpgu_connector = devm_kzalloc(drm->dev, sizeof(*arcpgu_connector),
  126. GFP_KERNEL);
  127. if (!arcpgu_connector) {
  128. ret = -ENOMEM;
  129. goto error_encoder_cleanup;
  130. }
  131. connector = &arcpgu_connector->connector;
  132. drm_connector_helper_add(connector, &arcpgu_drm_connector_helper_funcs);
  133. ret = drm_connector_init(drm, connector, &arcpgu_drm_connector_funcs,
  134. DRM_MODE_CONNECTOR_HDMIA);
  135. if (ret < 0) {
  136. dev_err(drm->dev, "failed to initialize drm connector\n");
  137. goto error_encoder_cleanup;
  138. }
  139. ret = drm_mode_connector_attach_encoder(connector, &encoder->base);
  140. if (ret < 0) {
  141. dev_err(drm->dev, "could not attach connector to encoder\n");
  142. drm_connector_unregister(connector);
  143. goto error_connector_cleanup;
  144. }
  145. arcpgu_connector->encoder_slave = encoder;
  146. return 0;
  147. error_connector_cleanup:
  148. drm_connector_cleanup(connector);
  149. error_encoder_cleanup:
  150. drm_encoder_cleanup(&encoder->base);
  151. return ret;
  152. }