intel_lvds.c 32 KB

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