atmel_hlcdc_output.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 Traphandler
  4. * Copyright (C) 2014 Free Electrons
  5. * Copyright (C) 2014 Atmel
  6. *
  7. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  8. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  9. */
  10. #include <linux/media-bus-format.h>
  11. #include <linux/of_graph.h>
  12. #include <drm/drm_bridge.h>
  13. #include <drm/drm_encoder.h>
  14. #include <drm/drm_of.h>
  15. #include <drm/drm_simple_kms_helper.h>
  16. #include "atmel_hlcdc_dc.h"
  17. struct atmel_hlcdc_rgb_output {
  18. struct drm_encoder encoder;
  19. int bus_fmt;
  20. };
  21. static struct atmel_hlcdc_rgb_output *
  22. atmel_hlcdc_encoder_to_rgb_output(struct drm_encoder *encoder)
  23. {
  24. return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
  25. }
  26. int atmel_hlcdc_encoder_get_bus_fmt(struct drm_encoder *encoder)
  27. {
  28. struct atmel_hlcdc_rgb_output *output;
  29. output = atmel_hlcdc_encoder_to_rgb_output(encoder);
  30. return output->bus_fmt;
  31. }
  32. static int atmel_hlcdc_of_bus_fmt(const struct device_node *ep)
  33. {
  34. u32 bus_width;
  35. int ret;
  36. ret = of_property_read_u32(ep, "bus-width", &bus_width);
  37. if (ret == -EINVAL)
  38. return 0;
  39. if (ret)
  40. return ret;
  41. switch (bus_width) {
  42. case 12:
  43. return MEDIA_BUS_FMT_RGB444_1X12;
  44. case 16:
  45. return MEDIA_BUS_FMT_RGB565_1X16;
  46. case 18:
  47. return MEDIA_BUS_FMT_RGB666_1X18;
  48. case 24:
  49. return MEDIA_BUS_FMT_RGB888_1X24;
  50. default:
  51. return -EINVAL;
  52. }
  53. }
  54. static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, int endpoint)
  55. {
  56. struct atmel_hlcdc_rgb_output *output;
  57. struct device_node *ep;
  58. struct drm_panel *panel;
  59. struct drm_bridge *bridge;
  60. int ret;
  61. ep = of_graph_get_endpoint_by_regs(dev->dev->of_node, 0, endpoint);
  62. if (!ep)
  63. return -ENODEV;
  64. ret = drm_of_find_panel_or_bridge(dev->dev->of_node, 0, endpoint,
  65. &panel, &bridge);
  66. if (ret) {
  67. of_node_put(ep);
  68. return ret;
  69. }
  70. output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
  71. if (!output) {
  72. of_node_put(ep);
  73. return -ENOMEM;
  74. }
  75. output->bus_fmt = atmel_hlcdc_of_bus_fmt(ep);
  76. of_node_put(ep);
  77. if (output->bus_fmt < 0) {
  78. dev_err(dev->dev, "endpoint %d: invalid bus width\n", endpoint);
  79. return -EINVAL;
  80. }
  81. ret = drm_simple_encoder_init(dev, &output->encoder,
  82. DRM_MODE_ENCODER_NONE);
  83. if (ret)
  84. return ret;
  85. output->encoder.possible_crtcs = 0x1;
  86. if (panel) {
  87. bridge = drm_panel_bridge_add_typed(panel,
  88. DRM_MODE_CONNECTOR_Unknown);
  89. if (IS_ERR(bridge))
  90. return PTR_ERR(bridge);
  91. }
  92. if (bridge) {
  93. ret = drm_bridge_attach(&output->encoder, bridge, NULL, 0);
  94. if (!ret)
  95. return 0;
  96. if (panel)
  97. drm_panel_bridge_remove(bridge);
  98. }
  99. drm_encoder_cleanup(&output->encoder);
  100. return ret;
  101. }
  102. int atmel_hlcdc_create_outputs(struct drm_device *dev)
  103. {
  104. int endpoint, ret = 0;
  105. int attached = 0;
  106. /*
  107. * Always scan the first few endpoints even if we get -ENODEV,
  108. * but keep going after that as long as we keep getting hits.
  109. */
  110. for (endpoint = 0; !ret || endpoint < 4; endpoint++) {
  111. ret = atmel_hlcdc_attach_endpoint(dev, endpoint);
  112. if (ret == -ENODEV)
  113. continue;
  114. if (ret)
  115. break;
  116. attached++;
  117. }
  118. /* At least one device was successfully attached.*/
  119. if (ret == -ENODEV && attached)
  120. return 0;
  121. return ret;
  122. }