intel_lvds.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/i2c.h>
  30. #include "drmP.h"
  31. #include "drm.h"
  32. #include "drm_crtc.h"
  33. #include "drm_edid.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. /**
  38. * Sets the backlight level.
  39. *
  40. * \param level backlight level, from 0 to intel_lvds_get_max_backlight().
  41. */
  42. static void intel_lvds_set_backlight(struct drm_device *dev, int level)
  43. {
  44. struct drm_i915_private *dev_priv = dev->dev_private;
  45. u32 blc_pwm_ctl;
  46. blc_pwm_ctl = I915_READ(BLC_PWM_CTL) & ~BACKLIGHT_DUTY_CYCLE_MASK;
  47. I915_WRITE(BLC_PWM_CTL, (blc_pwm_ctl |
  48. (level << BACKLIGHT_DUTY_CYCLE_SHIFT)));
  49. }
  50. /**
  51. * Returns the maximum level of the backlight duty cycle field.
  52. */
  53. static u32 intel_lvds_get_max_backlight(struct drm_device *dev)
  54. {
  55. struct drm_i915_private *dev_priv = dev->dev_private;
  56. return ((I915_READ(BLC_PWM_CTL) & BACKLIGHT_MODULATION_FREQ_MASK) >>
  57. BACKLIGHT_MODULATION_FREQ_SHIFT) * 2;
  58. }
  59. /**
  60. * Sets the power state for the panel.
  61. */
  62. static void intel_lvds_set_power(struct drm_device *dev, bool on)
  63. {
  64. struct drm_i915_private *dev_priv = dev->dev_private;
  65. u32 pp_status;
  66. if (on) {
  67. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  68. POWER_TARGET_ON);
  69. do {
  70. pp_status = I915_READ(PP_STATUS);
  71. } while ((pp_status & PP_ON) == 0);
  72. intel_lvds_set_backlight(dev, dev_priv->backlight_duty_cycle);
  73. } else {
  74. intel_lvds_set_backlight(dev, 0);
  75. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) &
  76. ~POWER_TARGET_ON);
  77. do {
  78. pp_status = I915_READ(PP_STATUS);
  79. } while (pp_status & PP_ON);
  80. }
  81. }
  82. static void intel_lvds_dpms(struct drm_encoder *encoder, int mode)
  83. {
  84. struct drm_device *dev = encoder->dev;
  85. if (mode == DRM_MODE_DPMS_ON)
  86. intel_lvds_set_power(dev, true);
  87. else
  88. intel_lvds_set_power(dev, false);
  89. /* XXX: We never power down the LVDS pairs. */
  90. }
  91. static void intel_lvds_save(struct drm_connector *connector)
  92. {
  93. struct drm_device *dev = connector->dev;
  94. struct drm_i915_private *dev_priv = dev->dev_private;
  95. dev_priv->savePP_ON = I915_READ(PP_ON_DELAYS);
  96. dev_priv->savePP_OFF = I915_READ(PP_OFF_DELAYS);
  97. dev_priv->savePP_CONTROL = I915_READ(PP_CONTROL);
  98. dev_priv->savePP_DIVISOR = I915_READ(PP_DIVISOR);
  99. dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
  100. dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
  101. BACKLIGHT_DUTY_CYCLE_MASK);
  102. /*
  103. * If the light is off at server startup, just make it full brightness
  104. */
  105. if (dev_priv->backlight_duty_cycle == 0)
  106. dev_priv->backlight_duty_cycle =
  107. intel_lvds_get_max_backlight(dev);
  108. }
  109. static void intel_lvds_restore(struct drm_connector *connector)
  110. {
  111. struct drm_device *dev = connector->dev;
  112. struct drm_i915_private *dev_priv = dev->dev_private;
  113. I915_WRITE(BLC_PWM_CTL, dev_priv->saveBLC_PWM_CTL);
  114. I915_WRITE(PP_ON_DELAYS, dev_priv->savePP_ON);
  115. I915_WRITE(PP_OFF_DELAYS, dev_priv->savePP_OFF);
  116. I915_WRITE(PP_DIVISOR, dev_priv->savePP_DIVISOR);
  117. I915_WRITE(PP_CONTROL, dev_priv->savePP_CONTROL);
  118. if (dev_priv->savePP_CONTROL & POWER_TARGET_ON)
  119. intel_lvds_set_power(dev, true);
  120. else
  121. intel_lvds_set_power(dev, false);
  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. struct drm_device *dev = encoder->dev;
  142. struct drm_i915_private *dev_priv = dev->dev_private;
  143. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  144. struct drm_encoder *tmp_encoder;
  145. /* Should never happen!! */
  146. if (!IS_I965G(dev) && intel_crtc->pipe == 0) {
  147. printk(KERN_ERR "Can't support LVDS on pipe A\n");
  148. return false;
  149. }
  150. /* Should never happen!! */
  151. list_for_each_entry(tmp_encoder, &dev->mode_config.encoder_list, head) {
  152. if (tmp_encoder != encoder && tmp_encoder->crtc == encoder->crtc) {
  153. printk(KERN_ERR "Can't enable LVDS and another "
  154. "encoder on the same pipe\n");
  155. return false;
  156. }
  157. }
  158. /*
  159. * If we have timings from the BIOS for the panel, put them in
  160. * to the adjusted mode. The CRTC will be set up for this mode,
  161. * with the panel scaling set up to source from the H/VDisplay
  162. * of the original mode.
  163. */
  164. if (dev_priv->panel_fixed_mode != NULL) {
  165. adjusted_mode->hdisplay = dev_priv->panel_fixed_mode->hdisplay;
  166. adjusted_mode->hsync_start =
  167. dev_priv->panel_fixed_mode->hsync_start;
  168. adjusted_mode->hsync_end =
  169. dev_priv->panel_fixed_mode->hsync_end;
  170. adjusted_mode->htotal = dev_priv->panel_fixed_mode->htotal;
  171. adjusted_mode->vdisplay = dev_priv->panel_fixed_mode->vdisplay;
  172. adjusted_mode->vsync_start =
  173. dev_priv->panel_fixed_mode->vsync_start;
  174. adjusted_mode->vsync_end =
  175. dev_priv->panel_fixed_mode->vsync_end;
  176. adjusted_mode->vtotal = dev_priv->panel_fixed_mode->vtotal;
  177. adjusted_mode->clock = dev_priv->panel_fixed_mode->clock;
  178. drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
  179. }
  180. /*
  181. * XXX: It would be nice to support lower refresh rates on the
  182. * panels to reduce power consumption, and perhaps match the
  183. * user's requested refresh rate.
  184. */
  185. return true;
  186. }
  187. static void intel_lvds_prepare(struct drm_encoder *encoder)
  188. {
  189. struct drm_device *dev = encoder->dev;
  190. struct drm_i915_private *dev_priv = dev->dev_private;
  191. dev_priv->saveBLC_PWM_CTL = I915_READ(BLC_PWM_CTL);
  192. dev_priv->backlight_duty_cycle = (dev_priv->saveBLC_PWM_CTL &
  193. BACKLIGHT_DUTY_CYCLE_MASK);
  194. intel_lvds_set_power(dev, false);
  195. }
  196. static void intel_lvds_commit( struct drm_encoder *encoder)
  197. {
  198. struct drm_device *dev = encoder->dev;
  199. struct drm_i915_private *dev_priv = dev->dev_private;
  200. if (dev_priv->backlight_duty_cycle == 0)
  201. dev_priv->backlight_duty_cycle =
  202. intel_lvds_get_max_backlight(dev);
  203. intel_lvds_set_power(dev, true);
  204. }
  205. static void intel_lvds_mode_set(struct drm_encoder *encoder,
  206. struct drm_display_mode *mode,
  207. struct drm_display_mode *adjusted_mode)
  208. {
  209. struct drm_device *dev = encoder->dev;
  210. struct drm_i915_private *dev_priv = dev->dev_private;
  211. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  212. u32 pfit_control;
  213. /*
  214. * The LVDS pin pair will already have been turned on in the
  215. * intel_crtc_mode_set since it has a large impact on the DPLL
  216. * settings.
  217. */
  218. /*
  219. * Enable automatic panel scaling so that non-native modes fill the
  220. * screen. Should be enabled before the pipe is enabled, according to
  221. * register description and PRM.
  222. */
  223. if (mode->hdisplay != adjusted_mode->hdisplay ||
  224. mode->vdisplay != adjusted_mode->vdisplay)
  225. pfit_control = (PFIT_ENABLE | VERT_AUTO_SCALE |
  226. HORIZ_AUTO_SCALE | VERT_INTERP_BILINEAR |
  227. HORIZ_INTERP_BILINEAR);
  228. else
  229. pfit_control = 0;
  230. if (!IS_I965G(dev)) {
  231. if (dev_priv->panel_wants_dither)
  232. pfit_control |= PANEL_8TO6_DITHER_ENABLE;
  233. }
  234. else
  235. pfit_control |= intel_crtc->pipe << PFIT_PIPE_SHIFT;
  236. I915_WRITE(PFIT_CONTROL, pfit_control);
  237. }
  238. /**
  239. * Detect the LVDS connection.
  240. *
  241. * This always returns CONNECTOR_STATUS_CONNECTED. This connector should only have
  242. * been set up if the LVDS was actually connected anyway.
  243. */
  244. static enum drm_connector_status intel_lvds_detect(struct drm_connector *connector)
  245. {
  246. return connector_status_connected;
  247. }
  248. /**
  249. * Return the list of DDC modes if available, or the BIOS fixed mode otherwise.
  250. */
  251. static int intel_lvds_get_modes(struct drm_connector *connector)
  252. {
  253. struct drm_device *dev = connector->dev;
  254. struct intel_output *intel_output = to_intel_output(connector);
  255. struct drm_i915_private *dev_priv = dev->dev_private;
  256. int ret = 0;
  257. ret = intel_ddc_get_modes(intel_output);
  258. if (ret)
  259. return ret;
  260. /* Didn't get an EDID, so
  261. * Set wide sync ranges so we get all modes
  262. * handed to valid_mode for checking
  263. */
  264. connector->display_info.min_vfreq = 0;
  265. connector->display_info.max_vfreq = 200;
  266. connector->display_info.min_hfreq = 0;
  267. connector->display_info.max_hfreq = 200;
  268. if (dev_priv->panel_fixed_mode != NULL) {
  269. struct drm_display_mode *mode;
  270. mutex_unlock(&dev->mode_config.mutex);
  271. mode = drm_mode_duplicate(dev, dev_priv->panel_fixed_mode);
  272. drm_mode_probed_add(connector, mode);
  273. mutex_unlock(&dev->mode_config.mutex);
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. /**
  279. * intel_lvds_destroy - unregister and free LVDS structures
  280. * @connector: connector to free
  281. *
  282. * Unregister the DDC bus for this connector then free the driver private
  283. * structure.
  284. */
  285. static void intel_lvds_destroy(struct drm_connector *connector)
  286. {
  287. struct intel_output *intel_output = to_intel_output(connector);
  288. if (intel_output->ddc_bus)
  289. intel_i2c_destroy(intel_output->ddc_bus);
  290. drm_sysfs_connector_remove(connector);
  291. drm_connector_cleanup(connector);
  292. kfree(connector);
  293. }
  294. static const struct drm_encoder_helper_funcs intel_lvds_helper_funcs = {
  295. .dpms = intel_lvds_dpms,
  296. .mode_fixup = intel_lvds_mode_fixup,
  297. .prepare = intel_lvds_prepare,
  298. .mode_set = intel_lvds_mode_set,
  299. .commit = intel_lvds_commit,
  300. };
  301. static const struct drm_connector_helper_funcs intel_lvds_connector_helper_funcs = {
  302. .get_modes = intel_lvds_get_modes,
  303. .mode_valid = intel_lvds_mode_valid,
  304. .best_encoder = intel_best_encoder,
  305. };
  306. static const struct drm_connector_funcs intel_lvds_connector_funcs = {
  307. .save = intel_lvds_save,
  308. .restore = intel_lvds_restore,
  309. .detect = intel_lvds_detect,
  310. .fill_modes = drm_helper_probe_single_connector_modes,
  311. .destroy = intel_lvds_destroy,
  312. };
  313. static void intel_lvds_enc_destroy(struct drm_encoder *encoder)
  314. {
  315. drm_encoder_cleanup(encoder);
  316. }
  317. static const struct drm_encoder_funcs intel_lvds_enc_funcs = {
  318. .destroy = intel_lvds_enc_destroy,
  319. };
  320. /**
  321. * intel_lvds_init - setup LVDS connectors on this device
  322. * @dev: drm device
  323. *
  324. * Create the connector, register the LVDS DDC bus, and try to figure out what
  325. * modes we can display on the LVDS panel (if present).
  326. */
  327. void intel_lvds_init(struct drm_device *dev)
  328. {
  329. struct drm_i915_private *dev_priv = dev->dev_private;
  330. struct intel_output *intel_output;
  331. struct drm_connector *connector;
  332. struct drm_encoder *encoder;
  333. struct drm_display_mode *scan; /* *modes, *bios_mode; */
  334. struct drm_crtc *crtc;
  335. u32 lvds;
  336. int pipe;
  337. intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  338. if (!intel_output) {
  339. return;
  340. }
  341. connector = &intel_output->base;
  342. encoder = &intel_output->enc;
  343. drm_connector_init(dev, &intel_output->base, &intel_lvds_connector_funcs,
  344. DRM_MODE_CONNECTOR_LVDS);
  345. drm_encoder_init(dev, &intel_output->enc, &intel_lvds_enc_funcs,
  346. DRM_MODE_ENCODER_LVDS);
  347. drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
  348. intel_output->type = INTEL_OUTPUT_LVDS;
  349. drm_encoder_helper_add(encoder, &intel_lvds_helper_funcs);
  350. drm_connector_helper_add(connector, &intel_lvds_connector_helper_funcs);
  351. connector->display_info.subpixel_order = SubPixelHorizontalRGB;
  352. connector->interlace_allowed = false;
  353. connector->doublescan_allowed = false;
  354. /*
  355. * LVDS discovery:
  356. * 1) check for EDID on DDC
  357. * 2) check for VBT data
  358. * 3) check to see if LVDS is already on
  359. * if none of the above, no panel
  360. * 4) make sure lid is open
  361. * if closed, act like it's not there for now
  362. */
  363. /* Set up the DDC bus. */
  364. intel_output->ddc_bus = intel_i2c_create(dev, GPIOC, "LVDSDDC_C");
  365. if (!intel_output->ddc_bus) {
  366. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  367. "failed.\n");
  368. goto failed;
  369. }
  370. /*
  371. * Attempt to get the fixed panel mode from DDC. Assume that the
  372. * preferred mode is the right one.
  373. */
  374. intel_ddc_get_modes(intel_output);
  375. list_for_each_entry(scan, &connector->probed_modes, head) {
  376. mutex_lock(&dev->mode_config.mutex);
  377. if (scan->type & DRM_MODE_TYPE_PREFERRED) {
  378. dev_priv->panel_fixed_mode =
  379. drm_mode_duplicate(dev, scan);
  380. mutex_unlock(&dev->mode_config.mutex);
  381. goto out; /* FIXME: check for quirks */
  382. }
  383. mutex_unlock(&dev->mode_config.mutex);
  384. }
  385. /* Failed to get EDID, what about VBT? */
  386. if (dev_priv->vbt_mode) {
  387. mutex_lock(&dev->mode_config.mutex);
  388. dev_priv->panel_fixed_mode =
  389. drm_mode_duplicate(dev, dev_priv->vbt_mode);
  390. mutex_unlock(&dev->mode_config.mutex);
  391. }
  392. /*
  393. * If we didn't get EDID, try checking if the panel is already turned
  394. * on. If so, assume that whatever is currently programmed is the
  395. * correct mode.
  396. */
  397. lvds = I915_READ(LVDS);
  398. pipe = (lvds & LVDS_PIPEB_SELECT) ? 1 : 0;
  399. crtc = intel_get_crtc_from_pipe(dev, pipe);
  400. if (crtc && (lvds & LVDS_PORT_EN)) {
  401. dev_priv->panel_fixed_mode = intel_crtc_mode_get(dev, crtc);
  402. if (dev_priv->panel_fixed_mode) {
  403. dev_priv->panel_fixed_mode->type |=
  404. DRM_MODE_TYPE_PREFERRED;
  405. goto out; /* FIXME: check for quirks */
  406. }
  407. }
  408. /* If we still don't have a mode after all that, give up. */
  409. if (!dev_priv->panel_fixed_mode)
  410. goto failed;
  411. /* FIXME: detect aopen & mac mini type stuff automatically? */
  412. /*
  413. * Blacklist machines with BIOSes that list an LVDS panel without
  414. * actually having one.
  415. */
  416. if (IS_I945GM(dev)) {
  417. /* aopen mini pc */
  418. if (dev->pdev->subsystem_vendor == 0xa0a0)
  419. goto failed;
  420. if ((dev->pdev->subsystem_vendor == 0x8086) &&
  421. (dev->pdev->subsystem_device == 0x7270)) {
  422. /* It's a Mac Mini or Macbook Pro.
  423. *
  424. * Apple hardware is out to get us. The macbook pro
  425. * has a real LVDS panel, but the mac mini does not,
  426. * and they have the same device IDs. We'll
  427. * distinguish by panel size, on the assumption
  428. * that Apple isn't about to make any machines with an
  429. * 800x600 display.
  430. */
  431. if (dev_priv->panel_fixed_mode != NULL &&
  432. dev_priv->panel_fixed_mode->hdisplay == 800 &&
  433. dev_priv->panel_fixed_mode->vdisplay == 600) {
  434. DRM_DEBUG("Suspected Mac Mini, ignoring the LVDS\n");
  435. goto failed;
  436. }
  437. }
  438. }
  439. out:
  440. drm_sysfs_connector_add(connector);
  441. return;
  442. failed:
  443. DRM_DEBUG("No LVDS modes found, disabling.\n");
  444. if (intel_output->ddc_bus)
  445. intel_i2c_destroy(intel_output->ddc_bus);
  446. drm_connector_cleanup(connector);
  447. kfree(connector);
  448. }