gma_display.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright © 2006-2011 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. * Patrik Jakobsson <patrik.r.jakobsson@gmail.com>
  20. */
  21. #include <drm/drmP.h>
  22. #include "gma_display.h"
  23. #include "psb_intel_drv.h"
  24. #include "psb_intel_reg.h"
  25. #include "psb_drv.h"
  26. /**
  27. * Returns whether any output on the specified pipe is of the specified type
  28. */
  29. bool gma_pipe_has_type(struct drm_crtc *crtc, int type)
  30. {
  31. struct drm_device *dev = crtc->dev;
  32. struct drm_mode_config *mode_config = &dev->mode_config;
  33. struct drm_connector *l_entry;
  34. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  35. if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
  36. struct psb_intel_encoder *psb_intel_encoder =
  37. psb_intel_attached_encoder(l_entry);
  38. if (psb_intel_encoder->type == type)
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. #define GMA_PLL_INVALID(s) { /* DRM_ERROR(s); */ return false; }
  45. bool gma_pll_is_valid(struct drm_crtc *crtc,
  46. const struct gma_limit_t *limit,
  47. struct gma_clock_t *clock)
  48. {
  49. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  50. GMA_PLL_INVALID("p1 out of range");
  51. if (clock->p < limit->p.min || limit->p.max < clock->p)
  52. GMA_PLL_INVALID("p out of range");
  53. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  54. GMA_PLL_INVALID("m2 out of range");
  55. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  56. GMA_PLL_INVALID("m1 out of range");
  57. /* On CDV m1 is always 0 */
  58. if (clock->m1 <= clock->m2 && clock->m1 != 0)
  59. GMA_PLL_INVALID("m1 <= m2 && m1 != 0");
  60. if (clock->m < limit->m.min || limit->m.max < clock->m)
  61. GMA_PLL_INVALID("m out of range");
  62. if (clock->n < limit->n.min || limit->n.max < clock->n)
  63. GMA_PLL_INVALID("n out of range");
  64. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  65. GMA_PLL_INVALID("vco out of range");
  66. /* XXX: We may need to be checking "Dot clock"
  67. * depending on the multiplier, connector, etc.,
  68. * rather than just a single range.
  69. */
  70. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  71. GMA_PLL_INVALID("dot out of range");
  72. return true;
  73. }
  74. bool gma_find_best_pll(const struct gma_limit_t *limit,
  75. struct drm_crtc *crtc, int target, int refclk,
  76. struct gma_clock_t *best_clock)
  77. {
  78. struct drm_device *dev = crtc->dev;
  79. const struct gma_clock_funcs *clock_funcs =
  80. to_psb_intel_crtc(crtc)->clock_funcs;
  81. struct gma_clock_t clock;
  82. int err = target;
  83. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  84. (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
  85. /*
  86. * For LVDS, if the panel is on, just rely on its current
  87. * settings for dual-channel. We haven't figured out how to
  88. * reliably set up different single/dual channel state, if we
  89. * even can.
  90. */
  91. if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  92. LVDS_CLKB_POWER_UP)
  93. clock.p2 = limit->p2.p2_fast;
  94. else
  95. clock.p2 = limit->p2.p2_slow;
  96. } else {
  97. if (target < limit->p2.dot_limit)
  98. clock.p2 = limit->p2.p2_slow;
  99. else
  100. clock.p2 = limit->p2.p2_fast;
  101. }
  102. memset(best_clock, 0, sizeof(*best_clock));
  103. /* m1 is always 0 on CDV so the outmost loop will run just once */
  104. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  105. for (clock.m2 = limit->m2.min;
  106. (clock.m2 < clock.m1 || clock.m1 == 0) &&
  107. clock.m2 <= limit->m2.max; clock.m2++) {
  108. for (clock.n = limit->n.min;
  109. clock.n <= limit->n.max; clock.n++) {
  110. for (clock.p1 = limit->p1.min;
  111. clock.p1 <= limit->p1.max;
  112. clock.p1++) {
  113. int this_err;
  114. clock_funcs->clock(refclk, &clock);
  115. if (!clock_funcs->pll_is_valid(crtc,
  116. limit, &clock))
  117. continue;
  118. this_err = abs(clock.dot - target);
  119. if (this_err < err) {
  120. *best_clock = clock;
  121. err = this_err;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return err != target;
  128. }