intel_lvds.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Copyright © 2006-2007 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. */
  29. #include <linux/dmi.h>
  30. #include <linux/i2c.h>
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "drm_crtc.h"
  34. #include "drm_edid.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include <linux/acpi.h>
  39. #define I915_LVDS "i915_lvds"
  40. /*
  41. * the following four scaling options are defined.
  42. * #define DRM_MODE_SCALE_NON_GPU 0
  43. * #define DRM_MODE_SCALE_FULLSCREEN 1
  44. * #define DRM_MODE_SCALE_NO_SCALE 2
  45. * #define DRM_MODE_SCALE_ASPECT 3
  46. */
  47. /* Private structure for the integrated LVDS support */
  48. struct intel_lvds_priv {
  49. int fitting_mode;
  50. u32 pfit_control;
  51. u32 pfit_pgm_ratios;
  52. };
  53. /**
  54. * Sets the backlight level.
  55. *
  56. * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
  57. */
  58. static void intel_lvds_set_backlight(struct drm_device *dev, int level)
  59. {
  60. struct drm_i915_private *dev_priv = dev->dev_private;
  61. u32 blc_pwm_ctl, reg;
  62. if (IS_IGDNG(dev))
  63. reg = BLC_PWM_CPU_CTL;
  64. else
  65. reg = BLC_PWM_CTL;
  66. blc_pwm_ctl = I915_READ(reg) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  67. I915_WRITE(reg, (blc_pwm_ctl |
  68. (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
  69. }
  70. /**
  71. * Returns the maximum level of the backlight duty cycle field.
  72. */
  73. static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
  74. {
  75. struct drm_i915_private *dev_priv = dev->dev_private;
  76. u32 reg;
  77. if (IS_IGDNG(dev))
  78. reg = BLC_PWM_PCH_CTL2;
  79. else
  80. reg = BLC_PWM_CTL;
  81. return ((I915_READ(reg) & BACKLIGHT_MODULATION_FREQ_MASK) >>
  82. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  83. }
  84. /**
  85. * Sets the power state for the panel.
  86. */
  87. static void intel_lvds_set_power(struct drm_device *dev, bool on)
  88. {
  89. struct drm_i915_private *dev_priv = dev->dev_private;
  90. u32 pp_status, ctl_reg, status_reg;
  91. if (IS_IGDNG(dev)) {
  92. ctl_reg = PCH_PP_CONTROL;
  93. status_reg = PCH_PP_STATUS;
  94. } else {
  95. ctl_reg = PP_CONTROL;
  96. status_reg = PP_STATUS;
  97. }
  98. if (on) {
  99. I915_WRITE(ctl_reg, I915_READ(ctl_reg) |
  100. POWER_TARGET_ON);
  101. do {
  102. pp_status = I915_READ(status_reg);
  103. } while ((pp_status & PP_ON) == 0);
  104. intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
  105. } else {
  106. intel_lvds_set_backlight(dev, 0);
  107. I915_WRITE(ctl_reg, I915_READ(ctl_reg) &
  108. ~POWER_TARGET_ON);
  109. do {
  110. pp_status = I915_READ(status_reg);
  111. } while (pp_status & PP_ON);
  112. }
  113. }
  114. static void intel_lvds_dpms(struct drm_encoder *encoder, int mode)
  115. {
  116. struct drm_device *dev = encoder->dev;
  117. if (mode == DRM_MODE_DPMS_ON)
  118. intel_lvds_set_power(dev, true);
  119. else
  120. intel_lvds_set_power(dev, false);
  121. /* XXX: We never power down the LVDS pairs. */
  122. }
  123. static void intel_lvds_save(struct drm_connector *connector)
  124. {
  125. struct drm_device *dev = connector->dev;
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. u32 pp_on_reg, pp_off_reg, pp_ctl_reg, pp_div_reg;
  128. u32 pwm_ctl_reg;
  129. if (IS_IGDNG(dev)) {
  130. pp_on_reg = PCH_PP_ON_DELAYS;
  131. pp_off_reg = PCH_PP_OFF_DELAYS;
  132. pp_ctl_reg = PCH_PP_CONTROL;
  133. pp_div_reg = PCH_PP_DIVISOR;
  134. pwm_ctl_reg = BLC_PWM_CPU_CTL;
  135. } else {
  136. pp_on_reg = PP_ON_DELAYS;
  137. pp_off_reg = PP_OFF_DELAYS;
  138. pp_ctl_reg = PP_CONTROL;
  139. pp_div_reg = PP_DIVISOR;
  140. pwm_ctl_reg = BLC_PWM_CTL;
  141. }
  142. dev_priv->savePP_ON = I915_READ(pp_on_reg);
  143. dev_priv->savePP_OFF = I915_READ(pp_off_reg);
  144. dev_priv->savePP_CONTROL = I915_READ(pp_ctl_reg);
  145. dev_priv->savePP_DIVISOR = I915_READ(pp_div_reg);
  146. dev_priv->saveBLC_PWM_CTL = I915_READ(pwm_ctl_reg);
  147. dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
  148. BACKLIGHT_DUTY_CYCLE_MASK);
  149. /*
  150. * If the light is off at server startup, just make it full brightness
  151. */
  152. if (dev_priv->backlight_duty_cycle == 0)
  153. dev_priv->backlight_duty_cycle =
  154. intel_lvds_get_max_backlight(dev);
  155. }
  156. static void intel_lvds_restore(struct drm_connector *connector)
  157. {
  158. struct drm_device *dev = connector->dev;
  159. struct drm_i915_private *dev_priv = dev->dev_private;
  160. u32 pp_on_reg, pp_off_reg, pp_ctl_reg, pp_div_reg;
  161. u32 pwm_ctl_reg;
  162. if (IS_IGDNG(dev)) {
  163. pp_on_reg = PCH_PP_ON_DELAYS;
  164. pp_off_reg = PCH_PP_OFF_DELAYS;
  165. pp_ctl_reg = PCH_PP_CONTROL;
  166. pp_div_reg = PCH_PP_DIVISOR;
  167. pwm_ctl_reg = BLC_PWM_CPU_CTL;
  168. } else {
  169. pp_on_reg = PP_ON_DELAYS;
  170. pp_off_reg = PP_OFF_DELAYS;
  171. pp_ctl_reg = PP_CONTROL;
  172. pp_div_reg = PP_DIVISOR;
  173. pwm_ctl_reg = BLC_PWM_CTL;
  174. }
  175. I915_WRITE(pwm_ctl_reg, dev_priv->saveBLC_PWM_CTL);
  176. I915_WRITE(pp_on_reg, dev_priv->savePP_ON);
  177. I915_WRITE(pp_off_reg, dev_priv->savePP_OFF);
  178. I915_WRITE(pp_div_reg, dev_priv->savePP_DIVISOR);
  179. I915_WRITE(pp_ctl_reg, dev_priv->savePP_CONTROL);
  180. if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
  181. intel_lvds_set_power(dev, true);
  182. else
  183. intel_lvds_set_power(dev, false);
  184. }
  185. static int intel_lvds_mode_valid(struct drm_connector *connector,
  186. struct drm_display_mode *mode)
  187. {
  188. struct drm_device *dev = connector->dev;
  189. struct drm_i915_private *dev_priv = dev->dev_private;
  190. struct drm_display_mode *fixed_mode = dev_priv->panel_fixed_mode;
  191. if (fixed_mode) {
  192. if (mode->hdisplay > fixed_mode->hdisplay)
  193. return MODE_PANEL;
  194. if (mode->vdisplay > fixed_mode->vdisplay)
  195. return MODE_PANEL;
  196. }
  197. return MODE_OK;
  198. }
  199. static bool intel_lvds_mode_fixup(struct drm_encoder *encoder,
  200. struct drm_display_mode *mode,
  201. struct drm_display_mode *adjusted_mode)
  202. {
  203. /*
  204. * float point operation is not supported . So the PANEL_RATIO_FACTOR
  205. * is defined, which can avoid the float point computation when
  206. * calculating the panel ratio.
  207. */
  208. #define PANEL_RATIO_FACTOR 8192
  209. struct drm_device *dev = encoder->dev;
  210. struct drm_i915_private *dev_priv = dev->dev_private;
  211. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  212. struct drm_encoder *tmp_encoder;
  213. struct intel_output *intel_output = enc_to_intel_output(encoder);
  214. struct intel_lvds_priv *lvds_priv = intel_output->dev_priv;
  215. u32 pfit_control = 0, pfit_pgm_ratios = 0;
  216. int left_border = 0, right_border = 0, top_border = 0;
  217. int bottom_border = 0;
  218. bool border = 0;
  219. int panel_ratio, desired_ratio, vert_scale, horiz_scale;
  220. int horiz_ratio, vert_ratio;
  221. u32 hsync_width, vsync_width;
  222. u32 hblank_width, vblank_width;
  223. u32 hsync_pos, vsync_pos;
  224. /* Should never happen!! */
  225. if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
  226. DRM_ERROR("Can't support LVDS on pipe A\n");
  227. return false;
  228. }
  229. /* Should never happen!! */
  230. list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list, head) {
  231. if (tmp_encoder != encoder && tmp_encoder->crtc == encoder->crtc) {
  232. DRM_ERROR("Can't enable LVDS and another "
  233. "encoder on the same pipe\n");
  234. return false;
  235. }
  236. }
  237. /* If we don't have a panel mode, there is nothing we can do */
  238. if (dev_priv->panel_fixed_mode == NULL)
  239. return true;
  240. /*
  241. * If we have timings from the BIOS for the panel, put them in
  242. * to the adjusted mode. The CRTC will be set up for this mode,
  243. * with the panel scaling set up to source from the H/VDisplay
  244. * of the original mode.
  245. */
  246. if (dev_priv->panel_fixed_mode != NULL) {
  247. adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
  248. adjusted_mode->hsync_start =
  249. dev_priv->panel_fixed_mode->hsync_start;
  250. adjusted_mode->hsync_end =
  251. dev_priv->panel_fixed_mode->hsync_end;
  252. adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
  253. adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
  254. adjusted_mode->vsync_start =
  255. dev_priv->panel_fixed_mode->vsync_start;
  256. adjusted_mode->vsync_end =
  257. dev_priv->panel_fixed_mode->vsync_end;
  258. adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
  259. adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
  260. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  261. }
  262. /* Make sure pre-965s set dither correctly */
  263. if (!IS_I965G(dev)) {
  264. if (dev_priv->panel_wants_dither || dev_priv->lvds_dither)
  265. pfit_control |= PANEL_8TO6_DITHER_ENABLE;
  266. }
  267. /* Native modes don't need fitting */
  268. if (adjusted_mode->hdisplay == mode->hdisplay &&
  269. adjusted_mode->vdisplay == mode->vdisplay) {
  270. pfit_pgm_ratios = 0;
  271. border = 0;
  272. goto out;
  273. }
  274. /* 965+ wants fuzzy fitting */
  275. if (IS_I965G(dev))
  276. pfit_control |= (intel_crtc->pipe << PFIT_PIPE_SHIFT) |
  277. PFIT_FILTER_FUZZY;
  278. hsync_width = adjusted_mode->crtc_hsync_end -
  279. adjusted_mode->crtc_hsync_start;
  280. vsync_width = adjusted_mode->crtc_vsync_end -
  281. adjusted_mode->crtc_vsync_start;
  282. hblank_width = adjusted_mode->crtc_hblank_end -
  283. adjusted_mode->crtc_hblank_start;
  284. vblank_width = adjusted_mode->crtc_vblank_end -
  285. adjusted_mode->crtc_vblank_start;
  286. /*
  287. * Deal with panel fitting options. Figure out how to stretch the
  288. * image based on its aspect ratio & the current panel fitting mode.
  289. */
  290. panel_ratio = adjusted_mode->hdisplay * PANEL_RATIO_FACTOR /
  291. adjusted_mode->vdisplay;
  292. desired_ratio = mode->hdisplay * PANEL_RATIO_FACTOR /
  293. mode->vdisplay;
  294. /*
  295. * Enable automatic panel scaling for non-native modes so that they fill
  296. * the screen. Should be enabled before the pipe is enabled, according
  297. * to register description and PRM.
  298. * Change the value here to see the borders for debugging
  299. */
  300. I915_WRITE(BCLRPAT_A, 0);
  301. I915_WRITE(BCLRPAT_B, 0);
  302. switch (lvds_priv->fitting_mode) {
  303. case DRM_MODE_SCALE_NO_SCALE:
  304. /*
  305. * For centered modes, we have to calculate border widths &
  306. * heights and modify the values programmed into the CRTC.
  307. */
  308. left_border = (adjusted_mode->hdisplay - mode->hdisplay) / 2;
  309. right_border = left_border;
  310. if (mode->hdisplay & 1)
  311. right_border++;
  312. top_border = (adjusted_mode->vdisplay - mode->vdisplay) / 2;
  313. bottom_border = top_border;
  314. if (mode->vdisplay & 1)
  315. bottom_border++;
  316. /* Set active & border values */
  317. adjusted_mode->crtc_hdisplay = mode->hdisplay;
  318. /* Keep the boder be even */
  319. if (right_border & 1)
  320. right_border++;
  321. /* use the border directly instead of border minuse one */
  322. adjusted_mode->crtc_hblank_start = mode->hdisplay +
  323. right_border;
  324. /* keep the blank width constant */
  325. adjusted_mode->crtc_hblank_end =
  326. adjusted_mode->crtc_hblank_start + hblank_width;
  327. /* get the hsync pos relative to hblank start */
  328. hsync_pos = (hblank_width - hsync_width) / 2;
  329. /* keep the hsync pos be even */
  330. if (hsync_pos & 1)
  331. hsync_pos++;
  332. adjusted_mode->crtc_hsync_start =
  333. adjusted_mode->crtc_hblank_start + hsync_pos;
  334. /* keep the hsync width constant */
  335. adjusted_mode->crtc_hsync_end =
  336. adjusted_mode->crtc_hsync_start + hsync_width;
  337. adjusted_mode->crtc_vdisplay = mode->vdisplay;
  338. /* use the border instead of border minus one */
  339. adjusted_mode->crtc_vblank_start = mode->vdisplay +
  340. bottom_border;
  341. /* keep the vblank width constant */
  342. adjusted_mode->crtc_vblank_end =
  343. adjusted_mode->crtc_vblank_start + vblank_width;
  344. /* get the vsync start postion relative to vblank start */
  345. vsync_pos = (vblank_width - vsync_width) / 2;
  346. adjusted_mode->crtc_vsync_start =
  347. adjusted_mode->crtc_vblank_start + vsync_pos;
  348. /* keep the vsync width constant */
  349. adjusted_mode->crtc_vsync_end =
  350. adjusted_mode->crtc_vblank_start + vsync_width;
  351. border = 1;
  352. break;
  353. case DRM_MODE_SCALE_ASPECT:
  354. /* Scale but preserve the spect ratio */
  355. pfit_control |= PFIT_ENABLE;
  356. if (IS_I965G(dev)) {
  357. /* 965+ is easy, it does everything in hw */
  358. if (panel_ratio > desired_ratio)
  359. pfit_control |= PFIT_SCALING_PILLAR;
  360. else if (panel_ratio < desired_ratio)
  361. pfit_control |= PFIT_SCALING_LETTER;
  362. else
  363. pfit_control |= PFIT_SCALING_AUTO;
  364. } else {
  365. /*
  366. * For earlier chips we have to calculate the scaling
  367. * ratio by hand and program it into the
  368. * PFIT_PGM_RATIO register
  369. */
  370. u32 horiz_bits, vert_bits, bits = 12;
  371. horiz_ratio = mode->hdisplay * PANEL_RATIO_FACTOR/
  372. adjusted_mode->hdisplay;
  373. vert_ratio = mode->vdisplay * PANEL_RATIO_FACTOR/
  374. adjusted_mode->vdisplay;
  375. horiz_scale = adjusted_mode->hdisplay *
  376. PANEL_RATIO_FACTOR / mode->hdisplay;
  377. vert_scale = adjusted_mode->vdisplay *
  378. PANEL_RATIO_FACTOR / mode->vdisplay;
  379. /* retain aspect ratio */
  380. if (panel_ratio > desired_ratio) { /* Pillar */
  381. u32 scaled_width;
  382. scaled_width = mode->hdisplay * vert_scale /
  383. PANEL_RATIO_FACTOR;
  384. horiz_ratio = vert_ratio;
  385. pfit_control |= (VERT_AUTO_SCALE |
  386. VERT_INTERP_BILINEAR |
  387. HORIZ_INTERP_BILINEAR);
  388. /* Pillar will have left/right borders */
  389. left_border = (adjusted_mode->hdisplay -
  390. scaled_width) / 2;
  391. right_border = left_border;
  392. if (mode->hdisplay & 1) /* odd resolutions */
  393. right_border++;
  394. /* keep the border be even */
  395. if (right_border & 1)
  396. right_border++;
  397. adjusted_mode->crtc_hdisplay = scaled_width;
  398. /* use border instead of border minus one */
  399. adjusted_mode->crtc_hblank_start =
  400. scaled_width + right_border;
  401. /* keep the hblank width constant */
  402. adjusted_mode->crtc_hblank_end =
  403. adjusted_mode->crtc_hblank_start +
  404. hblank_width;
  405. /*
  406. * get the hsync start pos relative to
  407. * hblank start
  408. */
  409. hsync_pos = (hblank_width - hsync_width) / 2;
  410. /* keep the hsync_pos be even */
  411. if (hsync_pos & 1)
  412. hsync_pos++;
  413. adjusted_mode->crtc_hsync_start =
  414. adjusted_mode->crtc_hblank_start +
  415. hsync_pos;
  416. /* keept hsync width constant */
  417. adjusted_mode->crtc_hsync_end =
  418. adjusted_mode->crtc_hsync_start +
  419. hsync_width;
  420. border = 1;
  421. } else if (panel_ratio < desired_ratio) { /* letter */
  422. u32 scaled_height = mode->vdisplay *
  423. horiz_scale / PANEL_RATIO_FACTOR;
  424. vert_ratio = horiz_ratio;
  425. pfit_control |= (HORIZ_AUTO_SCALE |
  426. VERT_INTERP_BILINEAR |
  427. HORIZ_INTERP_BILINEAR);
  428. /* Letterbox will have top/bottom border */
  429. top_border = (adjusted_mode->vdisplay -
  430. scaled_height) / 2;
  431. bottom_border = top_border;
  432. if (mode->vdisplay & 1)
  433. bottom_border++;
  434. adjusted_mode->crtc_vdisplay = scaled_height;
  435. /* use border instead of border minus one */
  436. adjusted_mode->crtc_vblank_start =
  437. scaled_height + bottom_border;
  438. /* keep the vblank width constant */
  439. adjusted_mode->crtc_vblank_end =
  440. adjusted_mode->crtc_vblank_start +
  441. vblank_width;
  442. /*
  443. * get the vsync start pos relative to
  444. * vblank start
  445. */
  446. vsync_pos = (vblank_width - vsync_width) / 2;
  447. adjusted_mode->crtc_vsync_start =
  448. adjusted_mode->crtc_vblank_start +
  449. vsync_pos;
  450. /* keep the vsync width constant */
  451. adjusted_mode->crtc_vsync_end =
  452. adjusted_mode->crtc_vsync_start +
  453. vsync_width;
  454. border = 1;
  455. } else {
  456. /* Aspects match, Let hw scale both directions */
  457. pfit_control |= (VERT_AUTO_SCALE |
  458. HORIZ_AUTO_SCALE |
  459. VERT_INTERP_BILINEAR |
  460. HORIZ_INTERP_BILINEAR);
  461. }
  462. horiz_bits = (1 << bits) * horiz_ratio /
  463. PANEL_RATIO_FACTOR;
  464. vert_bits = (1 << bits) * vert_ratio /
  465. PANEL_RATIO_FACTOR;
  466. pfit_pgm_ratios =
  467. ((vert_bits << PFIT_VERT_SCALE_SHIFT) &
  468. PFIT_VERT_SCALE_MASK) |
  469. ((horiz_bits << PFIT_HORIZ_SCALE_SHIFT) &
  470. PFIT_HORIZ_SCALE_MASK);
  471. }
  472. break;
  473. case DRM_MODE_SCALE_FULLSCREEN:
  474. /*
  475. * Full scaling, even if it changes the aspect ratio.
  476. * Fortunately this is all done for us in hw.
  477. */
  478. pfit_control |= PFIT_ENABLE;
  479. if (IS_I965G(dev))
  480. pfit_control |= PFIT_SCALING_AUTO;
  481. else
  482. pfit_control |= (VERT_AUTO_SCALE | HORIZ_AUTO_SCALE |
  483. VERT_INTERP_BILINEAR |
  484. HORIZ_INTERP_BILINEAR);
  485. break;
  486. default:
  487. break;
  488. }
  489. out:
  490. lvds_priv->pfit_control = pfit_control;
  491. lvds_priv->pfit_pgm_ratios = pfit_pgm_ratios;
  492. /*
  493. * XXX: It would be nice to support lower refresh rates on the
  494. * panels to reduce power consumption, and perhaps match the
  495. * user's requested refresh rate.
  496. */
  497. return true;
  498. }
  499. static void intel_lvds_prepare(struct drm_encoder *encoder)
  500. {
  501. struct drm_device *dev = encoder->dev;
  502. struct drm_i915_private *dev_priv = dev->dev_private;
  503. u32 reg;
  504. if (IS_IGDNG(dev))
  505. reg = BLC_PWM_CPU_CTL;
  506. else
  507. reg = BLC_PWM_CTL;
  508. dev_priv->saveBLC_PWM_CTL = I915_READ(reg);
  509. dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
  510. BACKLIGHT_DUTY_CYCLE_MASK);
  511. intel_lvds_set_power(dev, false);
  512. }
  513. static void intel_lvds_commit( struct drm_encoder *encoder)
  514. {
  515. struct drm_device *dev = encoder->dev;
  516. struct drm_i915_private *dev_priv = dev->dev_private;
  517. if (dev_priv->backlight_duty_cycle == 0)
  518. dev_priv->backlight_duty_cycle =
  519. intel_lvds_get_max_backlight(dev);
  520. intel_lvds_set_power(dev, true);
  521. }
  522. static void intel_lvds_mode_set(struct drm_encoder *encoder,
  523. struct drm_display_mode *mode,
  524. struct drm_display_mode *adjusted_mode)
  525. {
  526. struct drm_device *dev = encoder->dev;
  527. struct drm_i915_private *dev_priv = dev->dev_private;
  528. struct intel_output *intel_output = enc_to_intel_output(encoder);
  529. struct intel_lvds_priv *lvds_priv = intel_output->dev_priv;
  530. /*
  531. * The LVDS pin pair will already have been turned on in the
  532. * intel_crtc_mode_set since it has a large impact on the DPLL
  533. * settings.
  534. */
  535. /* No panel fitting yet, fixme */
  536. if (IS_IGDNG(dev))
  537. return;
  538. /*
  539. * Enable automatic panel scaling so that non-native modes fill the
  540. * screen. Should be enabled before the pipe is enabled, according to
  541. * register description and PRM.
  542. */
  543. I915_WRITE(PFIT_PGM_RATIOS, lvds_priv->pfit_pgm_ratios);
  544. I915_WRITE(PFIT_CONTROL, lvds_priv->pfit_control);
  545. }
  546. /**
  547. * Detect the LVDS connection.
  548. *
  549. * This always returns CONNECTOR_STATUS_CONNECTED. This connector should only have
  550. * been set up if the LVDS was actually connected anyway.
  551. */
  552. static enum drm_connector_status intel_lvds_detect(struct drm_connector *connector)
  553. {
  554. return connector_status_connected;
  555. }
  556. /**
  557. * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
  558. */
  559. static int intel_lvds_get_modes(struct drm_connector *connector)
  560. {
  561. struct drm_device *dev = connector->dev;
  562. struct intel_output *intel_output = to_intel_output(connector);
  563. struct drm_i915_private *dev_priv = dev->dev_private;
  564. int ret = 0;
  565. ret = intel_ddc_get_modes(intel_output);
  566. if (ret)
  567. return ret;
  568. /* Didn't get an EDID, so
  569. * Set wide sync ranges so we get all modes
  570. * handed to valid_mode for checking
  571. */
  572. connector->display_info.min_vfreq = 0;
  573. connector->display_info.max_vfreq = 200;
  574. connector->display_info.min_hfreq = 0;
  575. connector->display_info.max_hfreq = 200;
  576. if (dev_priv->panel_fixed_mode != NULL) {
  577. struct drm_display_mode *mode;
  578. mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
  579. drm_mode_probed_add(connector, mode);
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. /**
  585. * intel_lvds_destroy - unregister and free LVDS structures
  586. * @connector: connector to free
  587. *
  588. * Unregister the DDC bus for this connector then free the driver private
  589. * structure.
  590. */
  591. static void intel_lvds_destroy(struct drm_connector *connector)
  592. {
  593. struct intel_output *intel_output = to_intel_output(connector);
  594. if (intel_output->ddc_bus)
  595. intel_i2c_destroy(intel_output->ddc_bus);
  596. drm_sysfs_connector_remove(connector);
  597. drm_connector_cleanup(connector);
  598. kfree(connector);
  599. }
  600. static int intel_lvds_set_property(struct drm_connector *connector,
  601. struct drm_property *property,
  602. uint64_t value)
  603. {
  604. struct drm_device *dev = connector->dev;
  605. struct intel_output *intel_output =
  606. to_intel_output(connector);
  607. if (property == dev->mode_config.scaling_mode_property &&
  608. connector->encoder) {
  609. struct drm_crtc *crtc = connector->encoder->crtc;
  610. struct intel_lvds_priv *lvds_priv = intel_output->dev_priv;
  611. if (value == DRM_MODE_SCALE_NON_GPU) {
  612. DRM_DEBUG_KMS(I915_LVDS,
  613. "non_GPU property is unsupported\n");
  614. return 0;
  615. }
  616. if (lvds_priv->fitting_mode == value) {
  617. /* the LVDS scaling property is not changed */
  618. return 0;
  619. }
  620. lvds_priv->fitting_mode = value;
  621. if (crtc && crtc->enabled) {
  622. /*
  623. * If the CRTC is enabled, the display will be changed
  624. * according to the new panel fitting mode.
  625. */
  626. drm_crtc_helper_set_mode(crtc, &crtc->mode,
  627. crtc->x, crtc->y, crtc->fb);
  628. }
  629. }
  630. return 0;
  631. }
  632. static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
  633. .dpms = intel_lvds_dpms,
  634. .mode_fixup = intel_lvds_mode_fixup,
  635. .prepare = intel_lvds_prepare,
  636. .mode_set = intel_lvds_mode_set,
  637. .commit = intel_lvds_commit,
  638. };
  639. static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
  640. .get_modes = intel_lvds_get_modes,
  641. .mode_valid = intel_lvds_mode_valid,
  642. .best_encoder = intel_best_encoder,
  643. };
  644. static const struct drm_connector_funcs intel_lvds_connector_funcs = {
  645. .dpms = drm_helper_connector_dpms,
  646. .save = intel_lvds_save,
  647. .restore = intel_lvds_restore,
  648. .detect = intel_lvds_detect,
  649. .fill_modes = drm_helper_probe_single_connector_modes,
  650. .set_property = intel_lvds_set_property,
  651. .destroy = intel_lvds_destroy,
  652. };
  653. static void intel_lvds_enc_destroy(struct drm_encoder *encoder)
  654. {
  655. drm_encoder_cleanup(encoder);
  656. }
  657. static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
  658. .destroy = intel_lvds_enc_destroy,
  659. };
  660. static int __init intel_no_lvds_dmi_callback(const struct dmi_system_id *id)
  661. {
  662. DRM_DEBUG_KMS(I915_LVDS,
  663. "Skipping LVDS initialization for %s\n", id->ident);
  664. return 1;
  665. }
  666. /* These systems claim to have LVDS, but really don't */
  667. static const struct dmi_system_id intel_no_lvds[] = {
  668. {
  669. .callback = intel_no_lvds_dmi_callback,
  670. .ident = "Apple Mac Mini (Core series)",
  671. .matches = {
  672. DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
  673. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
  674. },
  675. },
  676. {
  677. .callback = intel_no_lvds_dmi_callback,
  678. .ident = "Apple Mac Mini (Core 2 series)",
  679. .matches = {
  680. DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
  681. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini2,1"),
  682. },
  683. },
  684. {
  685. .callback = intel_no_lvds_dmi_callback,
  686. .ident = "MSI IM-945GSE-A",
  687. .matches = {
  688. DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
  689. DMI_MATCH(DMI_PRODUCT_NAME, "A9830IMS"),
  690. },
  691. },
  692. {
  693. .callback = intel_no_lvds_dmi_callback,
  694. .ident = "Dell Studio Hybrid",
  695. .matches = {
  696. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  697. DMI_MATCH(DMI_PRODUCT_NAME, "Studio Hybrid 140g"),
  698. },
  699. },
  700. {
  701. .callback = intel_no_lvds_dmi_callback,
  702. .ident = "AOpen Mini PC",
  703. .matches = {
  704. DMI_MATCH(DMI_SYS_VENDOR, "AOpen"),
  705. DMI_MATCH(DMI_PRODUCT_NAME, "i965GMx-IF"),
  706. },
  707. },
  708. {
  709. .callback = intel_no_lvds_dmi_callback,
  710. .ident = "Aopen i945GTt-VFA",
  711. .matches = {
  712. DMI_MATCH(DMI_PRODUCT_VERSION, "AO00001JW"),
  713. },
  714. },
  715. { } /* terminating entry */
  716. };
  717. #ifdef CONFIG_ACPI
  718. /*
  719. * check_lid_device -- check whether @handle is an ACPI LID device.
  720. * @handle: ACPI device handle
  721. * @level : depth in the ACPI namespace tree
  722. * @context: the number of LID device when we find the device
  723. * @rv: a return value to fill if desired (Not use)
  724. */
  725. static acpi_status
  726. check_lid_device(acpi_handle handle, u32 level, void *context,
  727. void **return_value)
  728. {
  729. struct acpi_device *acpi_dev;
  730. int *lid_present = context;
  731. acpi_dev = NULL;
  732. /* Get the acpi device for device handle */
  733. if (acpi_bus_get_device(handle, &acpi_dev) || !acpi_dev) {
  734. /* If there is no ACPI device for handle, return */
  735. return AE_OK;
  736. }
  737. if (!strncmp(acpi_device_hid(acpi_dev), "PNP0C0D", 7))
  738. *lid_present = 1;
  739. return AE_OK;
  740. }
  741. /**
  742. * check whether there exists the ACPI LID device by enumerating the ACPI
  743. * device tree.
  744. */
  745. static int intel_lid_present(void)
  746. {
  747. int lid_present = 0;
  748. if (acpi_disabled) {
  749. /* If ACPI is disabled, there is no ACPI device tree to
  750. * check, so assume the LID device would have been present.
  751. */
  752. return 1;
  753. }
  754. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  755. ACPI_UINT32_MAX,
  756. check_lid_device, &lid_present, NULL);
  757. return lid_present;
  758. }
  759. #else
  760. static int intel_lid_present(void)
  761. {
  762. /* In the absence of ACPI built in, assume that the LID device would
  763. * have been present.
  764. */
  765. return 1;
  766. }
  767. #endif
  768. /**
  769. * intel_lvds_init - setup LVDS connectors on this device
  770. * @dev: drm device
  771. *
  772. * Create the connector, register the LVDS DDC bus, and try to figure out what
  773. * modes we can display on the LVDS panel (if present).
  774. */
  775. void intel_lvds_init(struct drm_device *dev)
  776. {
  777. struct drm_i915_private *dev_priv = dev->dev_private;
  778. struct intel_output *intel_output;
  779. struct drm_connector *connector;
  780. struct drm_encoder *encoder;
  781. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  782. struct drm_crtc *crtc;
  783. struct intel_lvds_priv *lvds_priv;
  784. u32 lvds;
  785. int pipe, gpio = GPIOC;
  786. /* Skip init on machines we know falsely report LVDS */
  787. if (dmi_check_system(intel_no_lvds))
  788. return;
  789. /* Assume that any device without an ACPI LID device also doesn't
  790. * have an integrated LVDS. We would be better off parsing the BIOS
  791. * to get a reliable indicator, but that code isn't written yet.
  792. *
  793. * In the case of all-in-one desktops using LVDS that we've seen,
  794. * they're using SDVO LVDS.
  795. */
  796. if (!intel_lid_present())
  797. return;
  798. if (IS_IGDNG(dev)) {
  799. if ((I915_READ(PCH_LVDS) & LVDS_DETECTED) == 0)
  800. return;
  801. gpio = PCH_GPIOC;
  802. }
  803. intel_output = kzalloc(sizeof(struct intel_output) +
  804. sizeof(struct intel_lvds_priv), GFP_KERNEL);
  805. if (!intel_output) {
  806. return;
  807. }
  808. connector = &intel_output->base;
  809. encoder = &intel_output->enc;
  810. drm_connector_init(dev, &intel_output->base, &intel_lvds_connector_funcs,
  811. DRM_MODE_CONNECTOR_LVDS);
  812. drm_encoder_init(dev, &intel_output->enc, &intel_lvds_enc_funcs,
  813. DRM_MODE_ENCODER_LVDS);
  814. drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
  815. intel_output->type = INTEL_OUTPUT_LVDS;
  816. drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
  817. drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
  818. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  819. connector->interlace_allowed = false;
  820. connector->doublescan_allowed = false;
  821. lvds_priv = (struct intel_lvds_priv *)(intel_output + 1);
  822. intel_output->dev_priv = lvds_priv;
  823. /* create the scaling mode property */
  824. drm_mode_create_scaling_mode_property(dev);
  825. /*
  826. * the initial panel fitting mode will be FULL_SCREEN.
  827. */
  828. drm_connector_attach_property(&intel_output->base,
  829. dev->mode_config.scaling_mode_property,
  830. DRM_MODE_SCALE_FULLSCREEN);
  831. lvds_priv->fitting_mode = DRM_MODE_SCALE_FULLSCREEN;
  832. /*
  833. * LVDS discovery:
  834. * 1) check for EDID on DDC
  835. * 2) check for VBT data
  836. * 3) check to see if LVDS is already on
  837. * if none of the above, no panel
  838. * 4) make sure lid is open
  839. * if closed, act like it's not there for now
  840. */
  841. /* Set up the DDC bus. */
  842. intel_output->ddc_bus = intel_i2c_create(dev, gpio, "LVDSDDC_C");
  843. if (!intel_output->ddc_bus) {
  844. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  845. "failed.\n");
  846. goto failed;
  847. }
  848. /*
  849. * Attempt to get the fixed panel mode from DDC. Assume that the
  850. * preferred mode is the right one.
  851. */
  852. intel_ddc_get_modes(intel_output);
  853. list_for_each_entry(scan, &connector->probed_modes, head) {
  854. mutex_lock(&dev->mode_config.mutex);
  855. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  856. dev_priv->panel_fixed_mode =
  857. drm_mode_duplicate(dev, scan);
  858. mutex_unlock(&dev->mode_config.mutex);
  859. goto out;
  860. }
  861. mutex_unlock(&dev->mode_config.mutex);
  862. }
  863. /* Failed to get EDID, what about VBT? */
  864. if (dev_priv->lfp_lvds_vbt_mode) {
  865. mutex_lock(&dev->mode_config.mutex);
  866. dev_priv->panel_fixed_mode =
  867. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  868. mutex_unlock(&dev->mode_config.mutex);
  869. if (dev_priv->panel_fixed_mode) {
  870. dev_priv->panel_fixed_mode->type |=
  871. DRM_MODE_TYPE_PREFERRED;
  872. goto out;
  873. }
  874. }
  875. /*
  876. * If we didn't get EDID, try checking if the panel is already turned
  877. * on. If so, assume that whatever is currently programmed is the
  878. * correct mode.
  879. */
  880. /* IGDNG: FIXME if still fail, not try pipe mode now */
  881. if (IS_IGDNG(dev))
  882. goto failed;
  883. lvds = I915_READ(LVDS);
  884. pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
  885. crtc = intel_get_crtc_from_pipe(dev, pipe);
  886. if (crtc && (lvds & LVDS_PORT_EN)) {
  887. dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
  888. if (dev_priv->panel_fixed_mode) {
  889. dev_priv->panel_fixed_mode->type |=
  890. DRM_MODE_TYPE_PREFERRED;
  891. goto out;
  892. }
  893. }
  894. /* If we still don't have a mode after all that, give up. */
  895. if (!dev_priv->panel_fixed_mode)
  896. goto failed;
  897. out:
  898. if (IS_IGDNG(dev)) {
  899. u32 pwm;
  900. /* make sure PWM is enabled */
  901. pwm = I915_READ(BLC_PWM_CPU_CTL2);
  902. pwm |= (PWM_ENABLE | PWM_PIPE_B);
  903. I915_WRITE(BLC_PWM_CPU_CTL2, pwm);
  904. pwm = I915_READ(BLC_PWM_PCH_CTL1);
  905. pwm |= PWM_PCH_ENABLE;
  906. I915_WRITE(BLC_PWM_PCH_CTL1, pwm);
  907. }
  908. drm_sysfs_connector_add(connector);
  909. return;
  910. failed:
  911. DRM_DEBUG_KMS(I915_LVDS, "No LVDS modes found, disabling.\n");
  912. if (intel_output->ddc_bus)
  913. intel_i2c_destroy(intel_output->ddc_bus);
  914. drm_connector_cleanup(connector);
  915. kfree(intel_output);
  916. }