atmel_hlcdc_output.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2014 Traphandler
  3. * Copyright (C) 2014 Free Electrons
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  7. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/of_graph.h>
  22. #include <drm/drmP.h>
  23. #include <drm/drm_of.h>
  24. #include <drm/drm_bridge.h>
  25. #include "atmel_hlcdc_dc.h"
  26. struct atmel_hlcdc_rgb_output {
  27. struct drm_encoder encoder;
  28. int bus_fmt;
  29. };
  30. static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
  31. .destroy = drm_encoder_cleanup,
  32. };
  33. static struct atmel_hlcdc_rgb_output *
  34. atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
  35. {
  36. return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
  37. }
  38. int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
  39. {
  40. struct atmel_hlcdc_rgb_output *output;
  41. output = atmel_hlcdc_encoder_to_rgb_output(encoder);
  42. return output->bus_fmt;
  43. }
  44. static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
  45. {
  46. u32 bus_width;
  47. int ret;
  48. ret = of_property_read_u32(ep, "bus-width", &bus_width);
  49. if (ret == -EINVAL)
  50. return 0;
  51. if (ret)
  52. return ret;
  53. switch (bus_width) {
  54. case 12:
  55. return MEDIA_BUS_FMT_RGB444_1X12;
  56. case 16:
  57. return MEDIA_BUS_FMT_RGB565_1X16;
  58. case 18:
  59. return MEDIA_BUS_FMT_RGB666_1X18;
  60. case 24:
  61. return MEDIA_BUS_FMT_RGB888_1X24;
  62. default:
  63. return -EINVAL;
  64. }
  65. }
  66. static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
  67. {
  68. struct atmel_hlcdc_rgb_output *output;
  69. struct device_node *ep;
  70. struct drm_panel *panel;
  71. struct drm_bridge *bridge;
  72. int ret;
  73. ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
  74. if (!ep)
  75. return -ENODEV;
  76. ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
  77. &panel, &bridge);
  78. if (ret) {
  79. of_node_put(ep);
  80. return ret;
  81. }
  82. output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
  83. if (!output) {
  84. of_node_put(ep);
  85. return -ENOMEM;
  86. }
  87. output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
  88. of_node_put(ep);
  89. if (output->bus_fmt < 0) {
  90. dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
  91. return -EINVAL;
  92. }
  93. ret = drm_encoder_init(dev, &output->encoder,
  94. &atmel_hlcdc_panel_encoder_funcs,
  95. DRM_MODE_ENCODER_NONE, NULL);
  96. if (ret)
  97. return ret;
  98. output->encoder.possible_crtcs = 0x1;
  99. if (panel) {
  100. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_Unknown);
  101. if (IS_ERR(bridge))
  102. return PTR_ERR(bridge);
  103. }
  104. if (bridge) {
  105. ret = drm_bridge_attach(&output->encoder, bridge, NULL);
  106. if (!ret)
  107. return 0;
  108. if (panel)
  109. drm_panel_bridge_remove(bridge);
  110. }
  111. drm_encoder_cleanup(&output->encoder);
  112. return ret;
  113. }
  114. int atmel_hlcdc_create_outputs(struct drm_device *dev)
  115. {
  116. int endpoint, ret = 0;
  117. int attached = 0;
  118. /*
  119. * Always scan the first few endpoints even if we get -ENODEV,
  120. * but keep going after that as long as we keep getting hits.
  121. */
  122. for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
  123. ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
  124. if (ret == -ENODEV)
  125. continue;
  126. if (ret)
  127. break;
  128. attached++;
  129. }
  130. /* At least one device was successfully attached.*/
  131. if (ret == -ENODEV && attached)
  132. return 0;
  133. return ret;
  134. }