intel_lvds.c 32 KB

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