fsl_dcu_drm_rgb.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2015 Freescale Semiconductor, Inc.
  3. *
  4. * Freescale DCU drm device driver
  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 as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/of_graph.h>
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_of.h>
  17. #include <drm/drm_panel.h>
  18. #include "fsl_dcu_drm_drv.h"
  19. #include "fsl_tcon.h"
  20. static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder)
  21. {
  22. drm_encoder_cleanup(encoder);
  23. }
  24. static const struct drm_encoder_funcs encoder_funcs = {
  25. .destroy = fsl_dcu_drm_encoder_destroy,
  26. };
  27. int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
  28. struct drm_crtc *crtc)
  29. {
  30. struct drm_encoder *encoder = &fsl_dev->encoder;
  31. int ret;
  32. encoder->possible_crtcs = 1;
  33. /* Use bypass mode for parallel RGB/LVDS encoder */
  34. if (fsl_dev->tcon)
  35. fsl_tcon_bypass_enable(fsl_dev->tcon);
  36. ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs,
  37. DRM_MODE_ENCODER_LVDS, NULL);
  38. if (ret < 0)
  39. return ret;
  40. return 0;
  41. }
  42. static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
  43. {
  44. struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector);
  45. drm_connector_unregister(connector);
  46. drm_panel_detach(fsl_con->panel);
  47. drm_connector_cleanup(connector);
  48. }
  49. static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
  50. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  51. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  52. .destroy = fsl_dcu_drm_connector_destroy,
  53. .fill_modes = drm_helper_probe_single_connector_modes,
  54. .reset = drm_atomic_helper_connector_reset,
  55. };
  56. static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
  57. {
  58. struct fsl_dcu_drm_connector *fsl_connector;
  59. int (*get_modes)(struct drm_panel *panel);
  60. int num_modes = 0;
  61. fsl_connector = to_fsl_dcu_connector(connector);
  62. if (fsl_connector->panel && fsl_connector->panel->funcs &&
  63. fsl_connector->panel->funcs->get_modes) {
  64. get_modes = fsl_connector->panel->funcs->get_modes;
  65. num_modes = get_modes(fsl_connector->panel);
  66. }
  67. return num_modes;
  68. }
  69. static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
  70. struct drm_display_mode *mode)
  71. {
  72. if (mode->hdisplay & 0xf)
  73. return MODE_ERROR;
  74. return MODE_OK;
  75. }
  76. static const struct drm_connector_helper_funcs connector_helper_funcs = {
  77. .get_modes = fsl_dcu_drm_connector_get_modes,
  78. .mode_valid = fsl_dcu_drm_connector_mode_valid,
  79. };
  80. static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
  81. struct drm_panel *panel)
  82. {
  83. struct drm_encoder *encoder = &fsl_dev->encoder;
  84. struct drm_connector *connector = &fsl_dev->connector.base;
  85. int ret;
  86. fsl_dev->connector.encoder = encoder;
  87. ret = drm_connector_init(fsl_dev->drm, connector,
  88. &fsl_dcu_drm_connector_funcs,
  89. DRM_MODE_CONNECTOR_LVDS);
  90. if (ret < 0)
  91. return ret;
  92. drm_connector_helper_add(connector, &connector_helper_funcs);
  93. ret = drm_connector_register(connector);
  94. if (ret < 0)
  95. goto err_cleanup;
  96. ret = drm_mode_connector_attach_encoder(connector, encoder);
  97. if (ret < 0)
  98. goto err_sysfs;
  99. ret = drm_panel_attach(panel, connector);
  100. if (ret) {
  101. dev_err(fsl_dev->dev, "failed to attach panel\n");
  102. goto err_sysfs;
  103. }
  104. return 0;
  105. err_sysfs:
  106. drm_connector_unregister(connector);
  107. err_cleanup:
  108. drm_connector_cleanup(connector);
  109. return ret;
  110. }
  111. int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
  112. {
  113. struct device_node *panel_node;
  114. struct drm_panel *panel;
  115. struct drm_bridge *bridge;
  116. int ret;
  117. /* This is for backward compatibility */
  118. panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
  119. if (panel_node) {
  120. fsl_dev->connector.panel = of_drm_find_panel(panel_node);
  121. of_node_put(panel_node);
  122. if (!fsl_dev->connector.panel)
  123. return -EPROBE_DEFER;
  124. return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
  125. }
  126. ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge);
  127. if (ret)
  128. return ret;
  129. if (panel) {
  130. fsl_dev->connector.panel = panel;
  131. return fsl_dcu_attach_panel(fsl_dev, panel);
  132. }
  133. return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL);
  134. }