intel_panel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 <linux/moduleparam.h>
  31. #include "intel_drv.h"
  32. #define PCI_LBPC 0xf4 /* legacy/combination backlight modes */
  33. void
  34. intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
  35. struct drm_display_mode *adjusted_mode)
  36. {
  37. adjusted_mode->hdisplay = fixed_mode->hdisplay;
  38. adjusted_mode->hsync_start = fixed_mode->hsync_start;
  39. adjusted_mode->hsync_end = fixed_mode->hsync_end;
  40. adjusted_mode->htotal = fixed_mode->htotal;
  41. adjusted_mode->vdisplay = fixed_mode->vdisplay;
  42. adjusted_mode->vsync_start = fixed_mode->vsync_start;
  43. adjusted_mode->vsync_end = fixed_mode->vsync_end;
  44. adjusted_mode->vtotal = fixed_mode->vtotal;
  45. adjusted_mode->clock = fixed_mode->clock;
  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. if (width & 1)
  76. width++;
  77. x = (adjusted_mode->hdisplay - width + 1) / 2;
  78. y = 0;
  79. height = adjusted_mode->vdisplay;
  80. } else if (scaled_width < scaled_height) { /* letter */
  81. height = scaled_width / mode->hdisplay;
  82. if (height & 1)
  83. height++;
  84. y = (adjusted_mode->vdisplay - height + 1) / 2;
  85. x = 0;
  86. width = adjusted_mode->hdisplay;
  87. } else {
  88. x = y = 0;
  89. width = adjusted_mode->hdisplay;
  90. height = adjusted_mode->vdisplay;
  91. }
  92. }
  93. break;
  94. default:
  95. case DRM_MODE_SCALE_FULLSCREEN:
  96. x = y = 0;
  97. width = adjusted_mode->hdisplay;
  98. height = adjusted_mode->vdisplay;
  99. break;
  100. }
  101. done:
  102. dev_priv->pch_pf_pos = (x << 16) | y;
  103. dev_priv->pch_pf_size = (width << 16) | height;
  104. }
  105. static int is_backlight_combination_mode(struct drm_device *dev)
  106. {
  107. struct drm_i915_private *dev_priv = dev->dev_private;
  108. if (INTEL_INFO(dev)->gen >= 4)
  109. return I915_READ(BLC_PWM_CTL2) & BLM_COMBINATION_MODE;
  110. if (IS_GEN2(dev))
  111. return I915_READ(BLC_PWM_CTL) & BLM_LEGACY_MODE;
  112. return 0;
  113. }
  114. static u32 i915_read_blc_pwm_ctl(struct drm_i915_private *dev_priv)
  115. {
  116. u32 val;
  117. /* Restore the CTL value if it lost, e.g. GPU reset */
  118. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  119. val = I915_READ(BLC_PWM_PCH_CTL2);
  120. if (dev_priv->saveBLC_PWM_CTL2 == 0) {
  121. dev_priv->saveBLC_PWM_CTL2 = val;
  122. } else if (val == 0) {
  123. I915_WRITE(BLC_PWM_PCH_CTL2,
  124. dev_priv->saveBLC_PWM_CTL2);
  125. val = dev_priv->saveBLC_PWM_CTL2;
  126. }
  127. } else {
  128. val = I915_READ(BLC_PWM_CTL);
  129. if (dev_priv->saveBLC_PWM_CTL == 0) {
  130. dev_priv->saveBLC_PWM_CTL = val;
  131. dev_priv->saveBLC_PWM_CTL2 = I915_READ(BLC_PWM_CTL2);
  132. } else if (val == 0) {
  133. I915_WRITE(BLC_PWM_CTL,
  134. dev_priv->saveBLC_PWM_CTL);
  135. I915_WRITE(BLC_PWM_CTL2,
  136. dev_priv->saveBLC_PWM_CTL2);
  137. val = dev_priv->saveBLC_PWM_CTL;
  138. }
  139. }
  140. return val;
  141. }
  142. u32 intel_panel_get_max_backlight(struct drm_device *dev)
  143. {
  144. struct drm_i915_private *dev_priv = dev->dev_private;
  145. u32 max;
  146. max = i915_read_blc_pwm_ctl(dev_priv);
  147. if (max == 0) {
  148. /* XXX add code here to query mode clock or hardware clock
  149. * and program max PWM appropriately.
  150. */
  151. printk_once(KERN_WARNING "fixme: max PWM is zero.\n");
  152. return 1;
  153. }
  154. if (HAS_PCH_SPLIT(dev)) {
  155. max >>= 16;
  156. } else {
  157. if (INTEL_INFO(dev)->gen < 4)
  158. max >>= 17;
  159. else
  160. max >>= 16;
  161. if (is_backlight_combination_mode(dev))
  162. max *= 0xff;
  163. }
  164. DRM_DEBUG_DRIVER("max backlight PWM = %d\n", max);
  165. return max;
  166. }
  167. static int i915_panel_invert_brightness;
  168. MODULE_PARM_DESC(invert_brightness, "Invert backlight brightness "
  169. "(-1 force normal, 0 machine defaults, 1 force inversion), please "
  170. "report PCI device ID, subsystem vendor and subsystem device ID "
  171. "to dri-devel@lists.freedesktop.org, if your machine needs it. "
  172. "It will then be included in an upcoming module version.");
  173. module_param_named(invert_brightness, i915_panel_invert_brightness, int, 0600);
  174. static u32 intel_panel_compute_brightness(struct drm_device *dev, u32 val)
  175. {
  176. struct drm_i915_private *dev_priv = dev->dev_private;
  177. if (i915_panel_invert_brightness < 0)
  178. return val;
  179. if (i915_panel_invert_brightness > 0 ||
  180. dev_priv->quirks & QUIRK_INVERT_BRIGHTNESS)
  181. return intel_panel_get_max_backlight(dev) - val;
  182. return val;
  183. }
  184. u32 intel_panel_get_backlight(struct drm_device *dev)
  185. {
  186. struct drm_i915_private *dev_priv = dev->dev_private;
  187. u32 val;
  188. if (HAS_PCH_SPLIT(dev)) {
  189. val = I915_READ(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  190. } else {
  191. val = I915_READ(BLC_PWM_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
  192. if (INTEL_INFO(dev)->gen < 4)
  193. val >>= 1;
  194. if (is_backlight_combination_mode(dev)) {
  195. u8 lbpc;
  196. pci_read_config_byte(dev->pdev, PCI_LBPC, &lbpc);
  197. val *= lbpc;
  198. }
  199. }
  200. val = intel_panel_compute_brightness(dev, val);
  201. DRM_DEBUG_DRIVER("get backlight PWM = %d\n", val);
  202. return val;
  203. }
  204. static void intel_pch_panel_set_backlight(struct drm_device *dev, u32 level)
  205. {
  206. struct drm_i915_private *dev_priv = dev->dev_private;
  207. u32 val = I915_READ(BLC_PWM_CPU_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  208. I915_WRITE(BLC_PWM_CPU_CTL, val | level);
  209. }
  210. static void intel_panel_actually_set_backlight(struct drm_device *dev, u32 level)
  211. {
  212. struct drm_i915_private *dev_priv = dev->dev_private;
  213. u32 tmp;
  214. DRM_DEBUG_DRIVER("set backlight PWM = %d\n", level);
  215. level = intel_panel_compute_brightness(dev, level);
  216. if (HAS_PCH_SPLIT(dev))
  217. return intel_pch_panel_set_backlight(dev, level);
  218. if (is_backlight_combination_mode(dev)) {
  219. u32 max = intel_panel_get_max_backlight(dev);
  220. u8 lbpc;
  221. lbpc = level * 0xfe / max + 1;
  222. level /= lbpc;
  223. pci_write_config_byte(dev->pdev, PCI_LBPC, lbpc);
  224. }
  225. tmp = I915_READ(BLC_PWM_CTL);
  226. if (INTEL_INFO(dev)->gen < 4)
  227. level <<= 1;
  228. tmp &= ~BACKLIGHT_DUTY_CYCLE_MASK;
  229. I915_WRITE(BLC_PWM_CTL, tmp | level);
  230. }
  231. void intel_panel_set_backlight(struct drm_device *dev, u32 level)
  232. {
  233. struct drm_i915_private *dev_priv = dev->dev_private;
  234. dev_priv->backlight_level = level;
  235. if (dev_priv->backlight_enabled)
  236. intel_panel_actually_set_backlight(dev, level);
  237. }
  238. void intel_panel_disable_backlight(struct drm_device *dev)
  239. {
  240. struct drm_i915_private *dev_priv = dev->dev_private;
  241. dev_priv->backlight_enabled = false;
  242. intel_panel_actually_set_backlight(dev, 0);
  243. }
  244. void intel_panel_enable_backlight(struct drm_device *dev)
  245. {
  246. struct drm_i915_private *dev_priv = dev->dev_private;
  247. if (dev_priv->backlight_level == 0)
  248. dev_priv->backlight_level = intel_panel_get_max_backlight(dev);
  249. dev_priv->backlight_enabled = true;
  250. intel_panel_actually_set_backlight(dev, dev_priv->backlight_level);
  251. }
  252. static void intel_panel_init_backlight(struct drm_device *dev)
  253. {
  254. struct drm_i915_private *dev_priv = dev->dev_private;
  255. dev_priv->backlight_level = intel_panel_get_backlight(dev);
  256. dev_priv->backlight_enabled = dev_priv->backlight_level != 0;
  257. }
  258. enum drm_connector_status
  259. intel_panel_detect(struct drm_device *dev)
  260. {
  261. #if 0
  262. struct drm_i915_private *dev_priv = dev->dev_private;
  263. #endif
  264. if (i915_panel_ignore_lid)
  265. return i915_panel_ignore_lid > 0 ?
  266. connector_status_connected :
  267. connector_status_disconnected;
  268. /* opregion lid state on HP 2540p is wrong at boot up,
  269. * appears to be either the BIOS or Linux ACPI fault */
  270. #if 0
  271. /* Assume that the BIOS does not lie through the OpRegion... */
  272. if (dev_priv->opregion.lid_state)
  273. return ioread32(dev_priv->opregion.lid_state) & 0x1 ?
  274. connector_status_connected :
  275. connector_status_disconnected;
  276. #endif
  277. return connector_status_unknown;
  278. }
  279. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  280. static int intel_panel_update_status(struct backlight_device *bd)
  281. {
  282. struct drm_device *dev = bl_get_data(bd);
  283. intel_panel_set_backlight(dev, bd->props.brightness);
  284. return 0;
  285. }
  286. static int intel_panel_get_brightness(struct backlight_device *bd)
  287. {
  288. struct drm_device *dev = bl_get_data(bd);
  289. struct drm_i915_private *dev_priv = dev->dev_private;
  290. return dev_priv->backlight_level;
  291. }
  292. static const struct backlight_ops intel_panel_bl_ops = {
  293. .update_status = intel_panel_update_status,
  294. .get_brightness = intel_panel_get_brightness,
  295. };
  296. int intel_panel_setup_backlight(struct drm_device *dev)
  297. {
  298. struct drm_i915_private *dev_priv = dev->dev_private;
  299. struct backlight_properties props;
  300. struct drm_connector *connector;
  301. intel_panel_init_backlight(dev);
  302. if (WARN_ON(dev_priv->backlight))
  303. return -ENODEV;
  304. if (dev_priv->int_lvds_connector)
  305. connector = dev_priv->int_lvds_connector;
  306. else if (dev_priv->int_edp_connector)
  307. connector = dev_priv->int_edp_connector;
  308. else
  309. return -ENODEV;
  310. props.type = BACKLIGHT_RAW;
  311. props.max_brightness = intel_panel_get_max_backlight(dev);
  312. dev_priv->backlight =
  313. backlight_device_register("intel_backlight",
  314. &connector->kdev, dev,
  315. &intel_panel_bl_ops, &props);
  316. if (IS_ERR(dev_priv->backlight)) {
  317. DRM_ERROR("Failed to register backlight: %ld\n",
  318. PTR_ERR(dev_priv->backlight));
  319. dev_priv->backlight = NULL;
  320. return -ENODEV;
  321. }
  322. dev_priv->backlight->props.brightness = intel_panel_get_backlight(dev);
  323. return 0;
  324. }
  325. void intel_panel_destroy_backlight(struct drm_device *dev)
  326. {
  327. struct drm_i915_private *dev_priv = dev->dev_private;
  328. if (dev_priv->backlight) {
  329. backlight_device_unregister(dev_priv->backlight);
  330. dev_priv->backlight = NULL;
  331. }
  332. }
  333. #else
  334. int intel_panel_setup_backlight(struct drm_device *dev)
  335. {
  336. intel_panel_init_backlight(dev);
  337. return 0;
  338. }
  339. void intel_panel_destroy_backlight(struct drm_device *dev)
  340. {
  341. return;
  342. }
  343. #endif