intel_lvds.c 33 KB

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