intel_panel.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright © 2006-2010 Intel Corporation
  3. * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors:
  25. * Eric Anholt <eric@anholt.net>
  26. * Dave Airlie <airlied@linux.ie>
  27. * Jesse Barnes <jesse.barnes@intel.com>
  28. * Chris Wilson <chris@chris-wilson.co.uk>
  29. */
  30. #include "intel_drv.h"
  31. #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
  32. void
  33. intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
  34. struct drm_display_mode *adjusted_mode)
  35. {
  36. adjusted_mode->hdisplay = fixed_mode->hdisplay;
  37. adjusted_mode->hsync_start = fixed_mode->hsync_start;
  38. adjusted_mode->hsync_end = fixed_mode->hsync_end;
  39. adjusted_mode->htotal = fixed_mode->htotal;
  40. adjusted_mode->vdisplay = fixed_mode->vdisplay;
  41. adjusted_mode->vsync_start = fixed_mode->vsync_start;
  42. adjusted_mode->vsync_end = fixed_mode->vsync_end;
  43. adjusted_mode->vtotal = fixed_mode->vtotal;
  44. adjusted_mode->clock = fixed_mode->clock;
  45. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  46. }
  47. /* adjusted_mode has been preset to be the panel's fixed mode */
  48. void
  49. intel_pch_panel_fitting(struct drm_device *dev,
  50. int fitting_mode,
  51. struct drm_display_mode *mode,
  52. struct drm_display_mode *adjusted_mode)
  53. {
  54. struct drm_i915_private *dev_priv = dev->dev_private;
  55. int x, y, width, height;
  56. x = y = width = height = 0;
  57. /* Native modes don't need fitting */
  58. if (adjusted_mode->hdisplay == mode->hdisplay &&
  59. adjusted_mode->vdisplay == mode->vdisplay)
  60. goto done;
  61. switch (fitting_mode) {
  62. case DRM_MODE_SCALE_CENTER:
  63. width = mode->hdisplay;
  64. height = mode->vdisplay;
  65. x = (adjusted_mode->hdisplay - width + 1)/2;
  66. y = (adjusted_mode->vdisplay - height + 1)/2;
  67. break;
  68. case DRM_MODE_SCALE_ASPECT:
  69. /* Scale but preserve the aspect ratio */
  70. {
  71. u32 scaled_width = adjusted_mode->hdisplay * mode->vdisplay;
  72. u32 scaled_height = mode->hdisplay * adjusted_mode->vdisplay;
  73. if (scaled_width > scaled_height) { /* pillar */
  74. width = scaled_height / mode->vdisplay;
  75. x = (adjusted_mode->hdisplay - width + 1) / 2;
  76. y = 0;
  77. height = adjusted_mode->vdisplay;
  78. } else if (scaled_width < scaled_height) { /* letter */
  79. height = scaled_width / mode->hdisplay;
  80. y = (adjusted_mode->vdisplay - height + 1) / 2;
  81. x = 0;
  82. width = adjusted_mode->hdisplay;
  83. } else {
  84. x = y = 0;
  85. width = adjusted_mode->hdisplay;
  86. height = adjusted_mode->vdisplay;
  87. }
  88. }
  89. break;
  90. default:
  91. case DRM_MODE_SCALE_FULLSCREEN:
  92. x = y = 0;
  93. width = adjusted_mode->hdisplay;
  94. height = adjusted_mode->vdisplay;
  95. break;
  96. }
  97. done:
  98. dev_priv->pch_pf_pos = (x << 16) | y;
  99. dev_priv->pch_pf_size = (width << 16) | height;
  100. }
  101. static int is_backlight_combination_mode(struct drm_device *dev)
  102. {
  103. struct drm_i915_private *dev_priv = dev->dev_private;
  104. if (INTEL_INFO(dev)->gen >= 4)
  105. return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
  106. if (IS_GEN2(dev))
  107. return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
  108. return 0;
  109. }
  110. u32 intel_panel_get_max_backlight(struct drm_device *dev)
  111. {
  112. struct drm_i915_private *dev_priv = dev->dev_private;
  113. u32 max;
  114. if (HAS_PCH_SPLIT(dev)) {
  115. max = I915_READ(BLC_PWM_PCH_CTL2) >> 16;
  116. } else {
  117. max = I915_READ(BLC_PWM_CTL);
  118. if (IS_PINEVIEW(dev)) {
  119. max >>= 17;
  120. } else {
  121. max >>= 16;
  122. if (INTEL_INFO(dev)->gen < 4)
  123. max &= ~1;
  124. }
  125. if (is_backlight_combination_mode(dev))
  126. max *= 0xff;
  127. }
  128. if (max == 0) {
  129. /* XXX add code here to query mode clock or hardware clock
  130. * and program max PWM appropriately.
  131. */
  132. DRM_ERROR("fixme: max PWM is zero.\n");
  133. max = 1;
  134. }
  135. DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
  136. return max;
  137. }
  138. u32 intel_panel_get_backlight(struct drm_device *dev)
  139. {
  140. struct drm_i915_private *dev_priv = dev->dev_private;
  141. u32 val;
  142. if (HAS_PCH_SPLIT(dev)) {
  143. val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  144. } else {
  145. val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  146. if (IS_PINEVIEW(dev))
  147. val >>= 1;
  148. if (is_backlight_combination_mode(dev)){
  149. u8 lbpc;
  150. val &= ~1;
  151. pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
  152. val *= lbpc;
  153. val >>= 1;
  154. }
  155. }
  156. DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
  157. return val;
  158. }
  159. static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
  160. {
  161. struct drm_i915_private *dev_priv = dev->dev_private;
  162. u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  163. I915_WRITE(BLC_PWM_CPU_CTL, val | level);
  164. }
  165. void intel_panel_set_backlight(struct drm_device *dev, u32 level)
  166. {
  167. struct drm_i915_private *dev_priv = dev->dev_private;
  168. u32 tmp;
  169. DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
  170. if (HAS_PCH_SPLIT(dev))
  171. return intel_pch_panel_set_backlight(dev, level);
  172. if (is_backlight_combination_mode(dev)){
  173. u32 max = intel_panel_get_max_backlight(dev);
  174. u8 lpbc;
  175. lpbc = level * 0xfe / max + 1;
  176. level /= lpbc;
  177. pci_write_config_byte(dev->pdev, PCI_LBPC, lpbc);
  178. }
  179. tmp = I915_READ(BLC_PWM_CTL);
  180. if (IS_PINEVIEW(dev)) {
  181. tmp &= ~(BACKLIGHT_DUTY_CYCLE_MASK - 1);
  182. level <<= 1;
  183. } else
  184. tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
  185. I915_WRITE(BLC_PWM_CTL, tmp | level);
  186. }