intel_lvds.c 32 KB

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