intel_lvds.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 drm_encoder *encoder = intel_attached_encoder(connector);
  520. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  521. struct drm_i915_private *dev_priv = dev->dev_private;
  522. int ret = 0;
  523. if (dev_priv->lvds_edid_good) {
  524. ret = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
  525. if (ret)
  526. return ret;
  527. }
  528. /* Didn't get an EDID, so
  529. * Set wide sync ranges so we get all modes
  530. * handed to valid_mode for checking
  531. */
  532. connector->display_info.min_vfreq = 0;
  533. connector->display_info.max_vfreq = 200;
  534. connector->display_info.min_hfreq = 0;
  535. connector->display_info.max_hfreq = 200;
  536. if (dev_priv->panel_fixed_mode != NULL) {
  537. struct drm_display_mode *mode;
  538. mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
  539. drm_mode_probed_add(connector, mode);
  540. return 1;
  541. }
  542. return 0;
  543. }
  544. /*
  545. * Lid events. Note the use of 'modeset_on_lid':
  546. * - we set it on lid close, and reset it on open
  547. * - we use it as a "only once" bit (ie we ignore
  548. * duplicate events where it was already properly
  549. * set/reset)
  550. * - the suspend/resume paths will also set it to
  551. * zero, since they restore the mode ("lid open").
  552. */
  553. static int intel_lid_notify(struct notifier_block *nb, unsigned long val,
  554. void *unused)
  555. {
  556. struct drm_i915_private *dev_priv =
  557. container_of(nb, struct drm_i915_private, lid_notifier);
  558. struct drm_device *dev = dev_priv->dev;
  559. struct drm_connector *connector = dev_priv->int_lvds_connector;
  560. /*
  561. * check and update the status of LVDS connector after receiving
  562. * the LID nofication event.
  563. */
  564. if (connector)
  565. connector->status = connector->funcs->detect(connector);
  566. if (!acpi_lid_open()) {
  567. dev_priv->modeset_on_lid = 1;
  568. return NOTIFY_OK;
  569. }
  570. if (!dev_priv->modeset_on_lid)
  571. return NOTIFY_OK;
  572. dev_priv->modeset_on_lid = 0;
  573. mutex_lock(&dev->mode_config.mutex);
  574. drm_helper_resume_force_mode(dev);
  575. mutex_unlock(&dev->mode_config.mutex);
  576. return NOTIFY_OK;
  577. }
  578. /**
  579. * intel_lvds_destroy - unregister and free LVDS structures
  580. * @connector: connector to free
  581. *
  582. * Unregister the DDC bus for this connector then free the driver private
  583. * structure.
  584. */
  585. static void intel_lvds_destroy(struct drm_connector *connector)
  586. {
  587. struct drm_device *dev = connector->dev;
  588. struct drm_i915_private *dev_priv = dev->dev_private;
  589. if (dev_priv->lid_notifier.notifier_call)
  590. acpi_lid_notifier_unregister(&dev_priv->lid_notifier);
  591. drm_sysfs_connector_remove(connector);
  592. drm_connector_cleanup(connector);
  593. kfree(connector);
  594. }
  595. static int intel_lvds_set_property(struct drm_connector *connector,
  596. struct drm_property *property,
  597. uint64_t value)
  598. {
  599. struct drm_device *dev = connector->dev;
  600. if (property == dev->mode_config.scaling_mode_property &&
  601. connector->encoder) {
  602. struct drm_crtc *crtc = connector->encoder->crtc;
  603. struct drm_encoder *encoder = connector->encoder;
  604. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  605. struct intel_lvds_priv *lvds_priv = intel_encoder->dev_priv;
  606. if (value == DRM_MODE_SCALE_NONE) {
  607. DRM_DEBUG_KMS("no scaling not supported\n");
  608. return 0;
  609. }
  610. if (lvds_priv->fitting_mode == value) {
  611. /* the LVDS scaling property is not changed */
  612. return 0;
  613. }
  614. lvds_priv->fitting_mode = value;
  615. if (crtc && crtc->enabled) {
  616. /*
  617. * If the CRTC is enabled, the display will be changed
  618. * according to the new panel fitting mode.
  619. */
  620. drm_crtc_helper_set_mode(crtc, &crtc->mode,
  621. crtc->x, crtc->y, crtc->fb);
  622. }
  623. }
  624. return 0;
  625. }
  626. static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
  627. .dpms = intel_lvds_dpms,
  628. .mode_fixup = intel_lvds_mode_fixup,
  629. .prepare = intel_lvds_prepare,
  630. .mode_set = intel_lvds_mode_set,
  631. .commit = intel_lvds_commit,
  632. };
  633. static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
  634. .get_modes = intel_lvds_get_modes,
  635. .mode_valid = intel_lvds_mode_valid,
  636. .best_encoder = intel_attached_encoder,
  637. };
  638. static const struct drm_connector_funcs intel_lvds_connector_funcs = {
  639. .dpms = drm_helper_connector_dpms,
  640. .detect = intel_lvds_detect,
  641. .fill_modes = drm_helper_probe_single_connector_modes,
  642. .set_property = intel_lvds_set_property,
  643. .destroy = intel_lvds_destroy,
  644. };
  645. static void intel_lvds_enc_destroy(struct drm_encoder *encoder)
  646. {
  647. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  648. if (intel_encoder->ddc_bus)
  649. intel_i2c_destroy(intel_encoder->ddc_bus);
  650. drm_encoder_cleanup(encoder);
  651. kfree(intel_encoder);
  652. }
  653. static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
  654. .destroy = intel_lvds_enc_destroy,
  655. };
  656. static int __init intel_no_lvds_dmi_callback(const struct dmi_system_id *id)
  657. {
  658. DRM_DEBUG_KMS("Skipping LVDS initialization for %s\n", id->ident);
  659. return 1;
  660. }
  661. /* These systems claim to have LVDS, but really don't */
  662. static const struct dmi_system_id intel_no_lvds[] = {
  663. {
  664. .callback = intel_no_lvds_dmi_callback,
  665. .ident = "Apple Mac Mini (Core series)",
  666. .matches = {
  667. DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
  668. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini1,1"),
  669. },
  670. },
  671. {
  672. .callback = intel_no_lvds_dmi_callback,
  673. .ident = "Apple Mac Mini (Core 2 series)",
  674. .matches = {
  675. DMI_MATCH(DMI_SYS_VENDOR, "Apple"),
  676. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini2,1"),
  677. },
  678. },
  679. {
  680. .callback = intel_no_lvds_dmi_callback,
  681. .ident = "MSI IM-945GSE-A",
  682. .matches = {
  683. DMI_MATCH(DMI_SYS_VENDOR, "MSI"),
  684. DMI_MATCH(DMI_PRODUCT_NAME, "A9830IMS"),
  685. },
  686. },
  687. {
  688. .callback = intel_no_lvds_dmi_callback,
  689. .ident = "Dell Studio Hybrid",
  690. .matches = {
  691. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  692. DMI_MATCH(DMI_PRODUCT_NAME, "Studio Hybrid 140g"),
  693. },
  694. },
  695. {
  696. .callback = intel_no_lvds_dmi_callback,
  697. .ident = "AOpen Mini PC",
  698. .matches = {
  699. DMI_MATCH(DMI_SYS_VENDOR, "AOpen"),
  700. DMI_MATCH(DMI_PRODUCT_NAME, "i965GMx-IF"),
  701. },
  702. },
  703. {
  704. .callback = intel_no_lvds_dmi_callback,
  705. .ident = "AOpen Mini PC MP915",
  706. .matches = {
  707. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  708. DMI_MATCH(DMI_BOARD_NAME, "i915GMx-F"),
  709. },
  710. },
  711. {
  712. .callback = intel_no_lvds_dmi_callback,
  713. .ident = "Aopen i945GTt-VFA",
  714. .matches = {
  715. DMI_MATCH(DMI_PRODUCT_VERSION, "AO00001JW"),
  716. },
  717. },
  718. {
  719. .callback = intel_no_lvds_dmi_callback,
  720. .ident = "Clientron U800",
  721. .matches = {
  722. DMI_MATCH(DMI_SYS_VENDOR, "Clientron"),
  723. DMI_MATCH(DMI_PRODUCT_NAME, "U800"),
  724. },
  725. },
  726. { } /* terminating entry */
  727. };
  728. /**
  729. * intel_find_lvds_downclock - find the reduced downclock for LVDS in EDID
  730. * @dev: drm device
  731. * @connector: LVDS connector
  732. *
  733. * Find the reduced downclock for LVDS in EDID.
  734. */
  735. static void intel_find_lvds_downclock(struct drm_device *dev,
  736. struct drm_connector *connector)
  737. {
  738. struct drm_i915_private *dev_priv = dev->dev_private;
  739. struct drm_display_mode *scan, *panel_fixed_mode;
  740. int temp_downclock;
  741. panel_fixed_mode = dev_priv->panel_fixed_mode;
  742. temp_downclock = panel_fixed_mode->clock;
  743. mutex_lock(&dev->mode_config.mutex);
  744. list_for_each_entry(scan, &connector->probed_modes, head) {
  745. /*
  746. * If one mode has the same resolution with the fixed_panel
  747. * mode while they have the different refresh rate, it means
  748. * that the reduced downclock is found for the LVDS. In such
  749. * case we can set the different FPx0/1 to dynamically select
  750. * between low and high frequency.
  751. */
  752. if (scan->hdisplay == panel_fixed_mode->hdisplay &&
  753. scan->hsync_start == panel_fixed_mode->hsync_start &&
  754. scan->hsync_end == panel_fixed_mode->hsync_end &&
  755. scan->htotal == panel_fixed_mode->htotal &&
  756. scan->vdisplay == panel_fixed_mode->vdisplay &&
  757. scan->vsync_start == panel_fixed_mode->vsync_start &&
  758. scan->vsync_end == panel_fixed_mode->vsync_end &&
  759. scan->vtotal == panel_fixed_mode->vtotal) {
  760. if (scan->clock < temp_downclock) {
  761. /*
  762. * The downclock is already found. But we
  763. * expect to find the lower downclock.
  764. */
  765. temp_downclock = scan->clock;
  766. }
  767. }
  768. }
  769. mutex_unlock(&dev->mode_config.mutex);
  770. if (temp_downclock < panel_fixed_mode->clock &&
  771. i915_lvds_downclock) {
  772. /* We found the downclock for LVDS. */
  773. dev_priv->lvds_downclock_avail = 1;
  774. dev_priv->lvds_downclock = temp_downclock;
  775. DRM_DEBUG_KMS("LVDS downclock is found in EDID. "
  776. "Normal clock %dKhz, downclock %dKhz\n",
  777. panel_fixed_mode->clock, temp_downclock);
  778. }
  779. return;
  780. }
  781. /*
  782. * Enumerate the child dev array parsed from VBT to check whether
  783. * the LVDS is present.
  784. * If it is present, return 1.
  785. * If it is not present, return false.
  786. * If no child dev is parsed from VBT, it assumes that the LVDS is present.
  787. * Note: The addin_offset should also be checked for LVDS panel.
  788. * Only when it is non-zero, it is assumed that it is present.
  789. */
  790. static int lvds_is_present_in_vbt(struct drm_device *dev)
  791. {
  792. struct drm_i915_private *dev_priv = dev->dev_private;
  793. struct child_device_config *p_child;
  794. int i, ret;
  795. if (!dev_priv->child_dev_num)
  796. return 1;
  797. ret = 0;
  798. for (i = 0; i < dev_priv->child_dev_num; i++) {
  799. p_child = dev_priv->child_dev + i;
  800. /*
  801. * If the device type is not LFP, continue.
  802. * If the device type is 0x22, it is also regarded as LFP.
  803. */
  804. if (p_child->device_type != DEVICE_TYPE_INT_LFP &&
  805. p_child->device_type != DEVICE_TYPE_LFP)
  806. continue;
  807. /* The addin_offset should be checked. Only when it is
  808. * non-zero, it is regarded as present.
  809. */
  810. if (p_child->addin_offset) {
  811. ret = 1;
  812. break;
  813. }
  814. }
  815. return ret;
  816. }
  817. /**
  818. * intel_lvds_init - setup LVDS connectors on this device
  819. * @dev: drm device
  820. *
  821. * Create the connector, register the LVDS DDC bus, and try to figure out what
  822. * modes we can display on the LVDS panel (if present).
  823. */
  824. void intel_lvds_init(struct drm_device *dev)
  825. {
  826. struct drm_i915_private *dev_priv = dev->dev_private;
  827. struct intel_encoder *intel_encoder;
  828. struct intel_connector *intel_connector;
  829. struct drm_connector *connector;
  830. struct drm_encoder *encoder;
  831. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  832. struct drm_crtc *crtc;
  833. struct intel_lvds_priv *lvds_priv;
  834. u32 lvds;
  835. int pipe, gpio = GPIOC;
  836. /* Skip init on machines we know falsely report LVDS */
  837. if (dmi_check_system(intel_no_lvds))
  838. return;
  839. if (!lvds_is_present_in_vbt(dev)) {
  840. DRM_DEBUG_KMS("LVDS is not present in VBT\n");
  841. return;
  842. }
  843. if (HAS_PCH_SPLIT(dev)) {
  844. if ((I915_READ(PCH_LVDS) & LVDS_DETECTED) == 0)
  845. return;
  846. if (dev_priv->edp_support) {
  847. DRM_DEBUG_KMS("disable LVDS for eDP support\n");
  848. return;
  849. }
  850. gpio = PCH_GPIOC;
  851. }
  852. intel_encoder = kzalloc(sizeof(struct intel_encoder) +
  853. sizeof(struct intel_lvds_priv), GFP_KERNEL);
  854. if (!intel_encoder) {
  855. return;
  856. }
  857. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  858. if (!intel_connector) {
  859. kfree(intel_encoder);
  860. return;
  861. }
  862. connector = &intel_connector->base;
  863. encoder = &intel_encoder->enc;
  864. drm_connector_init(dev, &intel_connector->base, &intel_lvds_connector_funcs,
  865. DRM_MODE_CONNECTOR_LVDS);
  866. drm_encoder_init(dev, &intel_encoder->enc, &intel_lvds_enc_funcs,
  867. DRM_MODE_ENCODER_LVDS);
  868. drm_mode_connector_attach_encoder(&intel_connector->base, &intel_encoder->enc);
  869. intel_encoder->type = INTEL_OUTPUT_LVDS;
  870. intel_encoder->clone_mask = (1 << INTEL_LVDS_CLONE_BIT);
  871. intel_encoder->crtc_mask = (1 << 1);
  872. if (IS_I965G(dev))
  873. intel_encoder->crtc_mask |= (1 << 0);
  874. drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
  875. drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
  876. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  877. connector->interlace_allowed = false;
  878. connector->doublescan_allowed = false;
  879. lvds_priv = (struct intel_lvds_priv *)(intel_encoder + 1);
  880. intel_encoder->dev_priv = lvds_priv;
  881. /* create the scaling mode property */
  882. drm_mode_create_scaling_mode_property(dev);
  883. /*
  884. * the initial panel fitting mode will be FULL_SCREEN.
  885. */
  886. drm_connector_attach_property(&intel_connector->base,
  887. dev->mode_config.scaling_mode_property,
  888. DRM_MODE_SCALE_FULLSCREEN);
  889. lvds_priv->fitting_mode = DRM_MODE_SCALE_FULLSCREEN;
  890. /*
  891. * LVDS discovery:
  892. * 1) check for EDID on DDC
  893. * 2) check for VBT data
  894. * 3) check to see if LVDS is already on
  895. * if none of the above, no panel
  896. * 4) make sure lid is open
  897. * if closed, act like it's not there for now
  898. */
  899. /* Set up the DDC bus. */
  900. intel_encoder->ddc_bus = intel_i2c_create(dev, gpio, "LVDSDDC_C");
  901. if (!intel_encoder->ddc_bus) {
  902. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  903. "failed.\n");
  904. goto failed;
  905. }
  906. /*
  907. * Attempt to get the fixed panel mode from DDC. Assume that the
  908. * preferred mode is the right one.
  909. */
  910. dev_priv->lvds_edid_good = true;
  911. if (!intel_ddc_get_modes(connector, intel_encoder->ddc_bus))
  912. dev_priv->lvds_edid_good = false;
  913. list_for_each_entry(scan, &connector->probed_modes, head) {
  914. mutex_lock(&dev->mode_config.mutex);
  915. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  916. dev_priv->panel_fixed_mode =
  917. drm_mode_duplicate(dev, scan);
  918. mutex_unlock(&dev->mode_config.mutex);
  919. intel_find_lvds_downclock(dev, connector);
  920. goto out;
  921. }
  922. mutex_unlock(&dev->mode_config.mutex);
  923. }
  924. /* Failed to get EDID, what about VBT? */
  925. if (dev_priv->lfp_lvds_vbt_mode) {
  926. mutex_lock(&dev->mode_config.mutex);
  927. dev_priv->panel_fixed_mode =
  928. drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
  929. mutex_unlock(&dev->mode_config.mutex);
  930. if (dev_priv->panel_fixed_mode) {
  931. dev_priv->panel_fixed_mode->type |=
  932. DRM_MODE_TYPE_PREFERRED;
  933. goto out;
  934. }
  935. }
  936. /*
  937. * If we didn't get EDID, try checking if the panel is already turned
  938. * on. If so, assume that whatever is currently programmed is the
  939. * correct mode.
  940. */
  941. /* Ironlake: FIXME if still fail, not try pipe mode now */
  942. if (HAS_PCH_SPLIT(dev))
  943. goto failed;
  944. lvds = I915_READ(LVDS);
  945. pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
  946. crtc = intel_get_crtc_from_pipe(dev, pipe);
  947. if (crtc && (lvds & LVDS_PORT_EN)) {
  948. dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
  949. if (dev_priv->panel_fixed_mode) {
  950. dev_priv->panel_fixed_mode->type |=
  951. DRM_MODE_TYPE_PREFERRED;
  952. goto out;
  953. }
  954. }
  955. /* If we still don't have a mode after all that, give up. */
  956. if (!dev_priv->panel_fixed_mode)
  957. goto failed;
  958. out:
  959. if (HAS_PCH_SPLIT(dev)) {
  960. u32 pwm;
  961. /* make sure PWM is enabled */
  962. pwm = I915_READ(BLC_PWM_CPU_CTL2);
  963. pwm |= (PWM_ENABLE | PWM_PIPE_B);
  964. I915_WRITE(BLC_PWM_CPU_CTL2, pwm);
  965. pwm = I915_READ(BLC_PWM_PCH_CTL1);
  966. pwm |= PWM_PCH_ENABLE;
  967. I915_WRITE(BLC_PWM_PCH_CTL1, pwm);
  968. }
  969. dev_priv->lid_notifier.notifier_call = intel_lid_notify;
  970. if (acpi_lid_notifier_register(&dev_priv->lid_notifier)) {
  971. DRM_DEBUG_KMS("lid notifier registration failed\n");
  972. dev_priv->lid_notifier.notifier_call = NULL;
  973. }
  974. /* keep the LVDS connector */
  975. dev_priv->int_lvds_connector = connector;
  976. drm_sysfs_connector_add(connector);
  977. return;
  978. failed:
  979. DRM_DEBUG_KMS("No LVDS modes found, disabling.\n");
  980. if (intel_encoder->ddc_bus)
  981. intel_i2c_destroy(intel_encoder->ddc_bus);
  982. drm_connector_cleanup(connector);
  983. drm_encoder_cleanup(encoder);
  984. kfree(intel_encoder);
  985. kfree(intel_connector);
  986. }