intel_lvds.c 29 KB

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