intel_lvds.c 30 KB

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