intel_crt.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_atomic_helper.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_edid.h>
  34. #include "intel_drv.h"
  35. #include <drm/i915_drm.h>
  36. #include "i915_drv.h"
  37. /* Here's the desired hotplug mode */
  38. #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \
  39. ADPA_CRT_HOTPLUG_WARMUP_10MS | \
  40. ADPA_CRT_HOTPLUG_SAMPLE_4S | \
  41. ADPA_CRT_HOTPLUG_VOLTAGE_50 | \
  42. ADPA_CRT_HOTPLUG_VOLREF_325MV | \
  43. ADPA_CRT_HOTPLUG_ENABLE)
  44. struct intel_crt {
  45. struct intel_encoder base;
  46. /* DPMS state is stored in the connector, which we need in the
  47. * encoder's enable/disable callbacks */
  48. struct intel_connector *connector;
  49. bool force_hotplug_required;
  50. i915_reg_t adpa_reg;
  51. };
  52. static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
  53. {
  54. return container_of(encoder, struct intel_crt, base);
  55. }
  56. static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
  57. {
  58. return intel_encoder_to_crt(intel_attached_encoder(connector));
  59. }
  60. static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
  61. enum pipe *pipe)
  62. {
  63. struct drm_device *dev = encoder->base.dev;
  64. struct drm_i915_private *dev_priv = to_i915(dev);
  65. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  66. u32 tmp;
  67. bool ret;
  68. if (!intel_display_power_get_if_enabled(dev_priv,
  69. encoder->power_domain))
  70. return false;
  71. ret = false;
  72. tmp = I915_READ(crt->adpa_reg);
  73. if (!(tmp & ADPA_DAC_ENABLE))
  74. goto out;
  75. if (HAS_PCH_CPT(dev_priv))
  76. *pipe = PORT_TO_PIPE_CPT(tmp);
  77. else
  78. *pipe = PORT_TO_PIPE(tmp);
  79. ret = true;
  80. out:
  81. intel_display_power_put(dev_priv, encoder->power_domain);
  82. return ret;
  83. }
  84. static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
  85. {
  86. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  87. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  88. u32 tmp, flags = 0;
  89. tmp = I915_READ(crt->adpa_reg);
  90. if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
  91. flags |= DRM_MODE_FLAG_PHSYNC;
  92. else
  93. flags |= DRM_MODE_FLAG_NHSYNC;
  94. if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
  95. flags |= DRM_MODE_FLAG_PVSYNC;
  96. else
  97. flags |= DRM_MODE_FLAG_NVSYNC;
  98. return flags;
  99. }
  100. static void intel_crt_get_config(struct intel_encoder *encoder,
  101. struct intel_crtc_state *pipe_config)
  102. {
  103. pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
  104. pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
  105. pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
  106. }
  107. static void hsw_crt_get_config(struct intel_encoder *encoder,
  108. struct intel_crtc_state *pipe_config)
  109. {
  110. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  111. intel_ddi_get_config(encoder, pipe_config);
  112. pipe_config->base.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
  113. DRM_MODE_FLAG_NHSYNC |
  114. DRM_MODE_FLAG_PVSYNC |
  115. DRM_MODE_FLAG_NVSYNC);
  116. pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
  117. pipe_config->base.adjusted_mode.crtc_clock = lpt_get_iclkip(dev_priv);
  118. }
  119. /* Note: The caller is required to filter out dpms modes not supported by the
  120. * platform. */
  121. static void intel_crt_set_dpms(struct intel_encoder *encoder,
  122. const struct intel_crtc_state *crtc_state,
  123. int mode)
  124. {
  125. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  126. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  127. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  128. const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
  129. u32 adpa;
  130. if (INTEL_GEN(dev_priv) >= 5)
  131. adpa = ADPA_HOTPLUG_BITS;
  132. else
  133. adpa = 0;
  134. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  135. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  136. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  137. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  138. /* For CPT allow 3 pipe config, for others just use A or B */
  139. if (HAS_PCH_LPT(dev_priv))
  140. ; /* Those bits don't exist here */
  141. else if (HAS_PCH_CPT(dev_priv))
  142. adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
  143. else if (crtc->pipe == 0)
  144. adpa |= ADPA_PIPE_A_SELECT;
  145. else
  146. adpa |= ADPA_PIPE_B_SELECT;
  147. if (!HAS_PCH_SPLIT(dev_priv))
  148. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  149. switch (mode) {
  150. case DRM_MODE_DPMS_ON:
  151. adpa |= ADPA_DAC_ENABLE;
  152. break;
  153. case DRM_MODE_DPMS_STANDBY:
  154. adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
  155. break;
  156. case DRM_MODE_DPMS_SUSPEND:
  157. adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
  158. break;
  159. case DRM_MODE_DPMS_OFF:
  160. adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
  161. break;
  162. }
  163. I915_WRITE(crt->adpa_reg, adpa);
  164. }
  165. static void intel_disable_crt(struct intel_encoder *encoder,
  166. const struct intel_crtc_state *old_crtc_state,
  167. const struct drm_connector_state *old_conn_state)
  168. {
  169. intel_crt_set_dpms(encoder, old_crtc_state, DRM_MODE_DPMS_OFF);
  170. }
  171. static void pch_disable_crt(struct intel_encoder *encoder,
  172. const struct intel_crtc_state *old_crtc_state,
  173. const struct drm_connector_state *old_conn_state)
  174. {
  175. }
  176. static void pch_post_disable_crt(struct intel_encoder *encoder,
  177. const struct intel_crtc_state *old_crtc_state,
  178. const struct drm_connector_state *old_conn_state)
  179. {
  180. intel_disable_crt(encoder, old_crtc_state, old_conn_state);
  181. }
  182. static void hsw_disable_crt(struct intel_encoder *encoder,
  183. const struct intel_crtc_state *old_crtc_state,
  184. const struct drm_connector_state *old_conn_state)
  185. {
  186. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  187. WARN_ON(!old_crtc_state->has_pch_encoder);
  188. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  189. }
  190. static void hsw_post_disable_crt(struct intel_encoder *encoder,
  191. const struct intel_crtc_state *old_crtc_state,
  192. const struct drm_connector_state *old_conn_state)
  193. {
  194. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  195. pch_post_disable_crt(encoder, old_crtc_state, old_conn_state);
  196. lpt_disable_pch_transcoder(dev_priv);
  197. lpt_disable_iclkip(dev_priv);
  198. intel_ddi_fdi_post_disable(encoder, old_crtc_state, old_conn_state);
  199. WARN_ON(!old_crtc_state->has_pch_encoder);
  200. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  201. }
  202. static void hsw_pre_pll_enable_crt(struct intel_encoder *encoder,
  203. const struct intel_crtc_state *crtc_state,
  204. const struct drm_connector_state *conn_state)
  205. {
  206. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  207. WARN_ON(!crtc_state->has_pch_encoder);
  208. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  209. }
  210. static void hsw_pre_enable_crt(struct intel_encoder *encoder,
  211. const struct intel_crtc_state *crtc_state,
  212. const struct drm_connector_state *conn_state)
  213. {
  214. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  215. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  216. enum pipe pipe = crtc->pipe;
  217. WARN_ON(!crtc_state->has_pch_encoder);
  218. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  219. dev_priv->display.fdi_link_train(crtc, crtc_state);
  220. }
  221. static void hsw_enable_crt(struct intel_encoder *encoder,
  222. const struct intel_crtc_state *crtc_state,
  223. const struct drm_connector_state *conn_state)
  224. {
  225. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  226. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  227. enum pipe pipe = crtc->pipe;
  228. WARN_ON(!crtc_state->has_pch_encoder);
  229. intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
  230. intel_wait_for_vblank(dev_priv, pipe);
  231. intel_wait_for_vblank(dev_priv, pipe);
  232. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  233. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  234. }
  235. static void intel_enable_crt(struct intel_encoder *encoder,
  236. const struct intel_crtc_state *crtc_state,
  237. const struct drm_connector_state *conn_state)
  238. {
  239. intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
  240. }
  241. static enum drm_mode_status
  242. intel_crt_mode_valid(struct drm_connector *connector,
  243. struct drm_display_mode *mode)
  244. {
  245. struct drm_device *dev = connector->dev;
  246. struct drm_i915_private *dev_priv = to_i915(dev);
  247. int max_dotclk = dev_priv->max_dotclk_freq;
  248. int max_clock;
  249. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  250. return MODE_NO_DBLESCAN;
  251. if (mode->clock < 25000)
  252. return MODE_CLOCK_LOW;
  253. if (HAS_PCH_LPT(dev_priv))
  254. max_clock = 180000;
  255. else if (IS_VALLEYVIEW(dev_priv))
  256. /*
  257. * 270 MHz due to current DPLL limits,
  258. * DAC limit supposedly 355 MHz.
  259. */
  260. max_clock = 270000;
  261. else if (IS_GEN3(dev_priv) || IS_GEN4(dev_priv))
  262. max_clock = 400000;
  263. else
  264. max_clock = 350000;
  265. if (mode->clock > max_clock)
  266. return MODE_CLOCK_HIGH;
  267. if (mode->clock > max_dotclk)
  268. return MODE_CLOCK_HIGH;
  269. /* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
  270. if (HAS_PCH_LPT(dev_priv) &&
  271. (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
  272. return MODE_CLOCK_HIGH;
  273. return MODE_OK;
  274. }
  275. static bool intel_crt_compute_config(struct intel_encoder *encoder,
  276. struct intel_crtc_state *pipe_config,
  277. struct drm_connector_state *conn_state)
  278. {
  279. struct drm_display_mode *adjusted_mode =
  280. &pipe_config->base.adjusted_mode;
  281. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
  282. return false;
  283. return true;
  284. }
  285. static bool pch_crt_compute_config(struct intel_encoder *encoder,
  286. struct intel_crtc_state *pipe_config,
  287. struct drm_connector_state *conn_state)
  288. {
  289. struct drm_display_mode *adjusted_mode =
  290. &pipe_config->base.adjusted_mode;
  291. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
  292. return false;
  293. pipe_config->has_pch_encoder = true;
  294. return true;
  295. }
  296. static bool hsw_crt_compute_config(struct intel_encoder *encoder,
  297. struct intel_crtc_state *pipe_config,
  298. struct drm_connector_state *conn_state)
  299. {
  300. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  301. struct drm_display_mode *adjusted_mode =
  302. &pipe_config->base.adjusted_mode;
  303. if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
  304. return false;
  305. pipe_config->has_pch_encoder = true;
  306. /* LPT FDI RX only supports 8bpc. */
  307. if (HAS_PCH_LPT(dev_priv)) {
  308. if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
  309. DRM_DEBUG_KMS("LPT only supports 24bpp\n");
  310. return false;
  311. }
  312. pipe_config->pipe_bpp = 24;
  313. }
  314. /* FDI must always be 2.7 GHz */
  315. pipe_config->port_clock = 135000 * 2;
  316. return true;
  317. }
  318. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  319. {
  320. struct drm_device *dev = connector->dev;
  321. struct intel_crt *crt = intel_attached_crt(connector);
  322. struct drm_i915_private *dev_priv = to_i915(dev);
  323. u32 adpa;
  324. bool ret;
  325. /* The first time through, trigger an explicit detection cycle */
  326. if (crt->force_hotplug_required) {
  327. bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
  328. u32 save_adpa;
  329. crt->force_hotplug_required = 0;
  330. save_adpa = adpa = I915_READ(crt->adpa_reg);
  331. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  332. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  333. if (turn_off_dac)
  334. adpa &= ~ADPA_DAC_ENABLE;
  335. I915_WRITE(crt->adpa_reg, adpa);
  336. if (intel_wait_for_register(dev_priv,
  337. crt->adpa_reg,
  338. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  339. 1000))
  340. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  341. if (turn_off_dac) {
  342. I915_WRITE(crt->adpa_reg, save_adpa);
  343. POSTING_READ(crt->adpa_reg);
  344. }
  345. }
  346. /* Check the status to see if both blue and green are on now */
  347. adpa = I915_READ(crt->adpa_reg);
  348. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  349. ret = true;
  350. else
  351. ret = false;
  352. DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
  353. return ret;
  354. }
  355. static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
  356. {
  357. struct drm_device *dev = connector->dev;
  358. struct intel_crt *crt = intel_attached_crt(connector);
  359. struct drm_i915_private *dev_priv = to_i915(dev);
  360. bool reenable_hpd;
  361. u32 adpa;
  362. bool ret;
  363. u32 save_adpa;
  364. /*
  365. * Doing a force trigger causes a hpd interrupt to get sent, which can
  366. * get us stuck in a loop if we're polling:
  367. * - We enable power wells and reset the ADPA
  368. * - output_poll_exec does force probe on VGA, triggering a hpd
  369. * - HPD handler waits for poll to unlock dev->mode_config.mutex
  370. * - output_poll_exec shuts off the ADPA, unlocks
  371. * dev->mode_config.mutex
  372. * - HPD handler runs, resets ADPA and brings us back to the start
  373. *
  374. * Just disable HPD interrupts here to prevent this
  375. */
  376. reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
  377. save_adpa = adpa = I915_READ(crt->adpa_reg);
  378. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  379. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  380. I915_WRITE(crt->adpa_reg, adpa);
  381. if (intel_wait_for_register(dev_priv,
  382. crt->adpa_reg,
  383. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  384. 1000)) {
  385. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  386. I915_WRITE(crt->adpa_reg, save_adpa);
  387. }
  388. /* Check the status to see if both blue and green are on now */
  389. adpa = I915_READ(crt->adpa_reg);
  390. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  391. ret = true;
  392. else
  393. ret = false;
  394. DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
  395. if (reenable_hpd)
  396. intel_hpd_enable(dev_priv, crt->base.hpd_pin);
  397. return ret;
  398. }
  399. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  400. {
  401. struct drm_device *dev = connector->dev;
  402. struct drm_i915_private *dev_priv = to_i915(dev);
  403. u32 stat;
  404. bool ret = false;
  405. int i, tries = 0;
  406. if (HAS_PCH_SPLIT(dev_priv))
  407. return intel_ironlake_crt_detect_hotplug(connector);
  408. if (IS_VALLEYVIEW(dev_priv))
  409. return valleyview_crt_detect_hotplug(connector);
  410. /*
  411. * On 4 series desktop, CRT detect sequence need to be done twice
  412. * to get a reliable result.
  413. */
  414. if (IS_G4X(dev_priv) && !IS_GM45(dev_priv))
  415. tries = 2;
  416. else
  417. tries = 1;
  418. for (i = 0; i < tries ; i++) {
  419. /* turn on the FORCE_DETECT */
  420. i915_hotplug_interrupt_update(dev_priv,
  421. CRT_HOTPLUG_FORCE_DETECT,
  422. CRT_HOTPLUG_FORCE_DETECT);
  423. /* wait for FORCE_DETECT to go off */
  424. if (intel_wait_for_register(dev_priv, PORT_HOTPLUG_EN,
  425. CRT_HOTPLUG_FORCE_DETECT, 0,
  426. 1000))
  427. DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
  428. }
  429. stat = I915_READ(PORT_HOTPLUG_STAT);
  430. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  431. ret = true;
  432. /* clear the interrupt we just generated, if any */
  433. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  434. i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
  435. return ret;
  436. }
  437. static struct edid *intel_crt_get_edid(struct drm_connector *connector,
  438. struct i2c_adapter *i2c)
  439. {
  440. struct edid *edid;
  441. edid = drm_get_edid(connector, i2c);
  442. if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
  443. DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
  444. intel_gmbus_force_bit(i2c, true);
  445. edid = drm_get_edid(connector, i2c);
  446. intel_gmbus_force_bit(i2c, false);
  447. }
  448. return edid;
  449. }
  450. /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
  451. static int intel_crt_ddc_get_modes(struct drm_connector *connector,
  452. struct i2c_adapter *adapter)
  453. {
  454. struct edid *edid;
  455. int ret;
  456. edid = intel_crt_get_edid(connector, adapter);
  457. if (!edid)
  458. return 0;
  459. ret = intel_connector_update_modes(connector, edid);
  460. kfree(edid);
  461. return ret;
  462. }
  463. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  464. {
  465. struct intel_crt *crt = intel_attached_crt(connector);
  466. struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
  467. struct edid *edid;
  468. struct i2c_adapter *i2c;
  469. bool ret = false;
  470. BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
  471. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  472. edid = intel_crt_get_edid(connector, i2c);
  473. if (edid) {
  474. bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
  475. /*
  476. * This may be a DVI-I connector with a shared DDC
  477. * link between analog and digital outputs, so we
  478. * have to check the EDID input spec of the attached device.
  479. */
  480. if (!is_digital) {
  481. DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
  482. ret = true;
  483. } else {
  484. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
  485. }
  486. } else {
  487. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
  488. }
  489. kfree(edid);
  490. return ret;
  491. }
  492. static enum drm_connector_status
  493. intel_crt_load_detect(struct intel_crt *crt, uint32_t pipe)
  494. {
  495. struct drm_device *dev = crt->base.base.dev;
  496. struct drm_i915_private *dev_priv = to_i915(dev);
  497. uint32_t save_bclrpat;
  498. uint32_t save_vtotal;
  499. uint32_t vtotal, vactive;
  500. uint32_t vsample;
  501. uint32_t vblank, vblank_start, vblank_end;
  502. uint32_t dsl;
  503. i915_reg_t bclrpat_reg, vtotal_reg,
  504. vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
  505. uint8_t st00;
  506. enum drm_connector_status status;
  507. DRM_DEBUG_KMS("starting load-detect on CRT\n");
  508. bclrpat_reg = BCLRPAT(pipe);
  509. vtotal_reg = VTOTAL(pipe);
  510. vblank_reg = VBLANK(pipe);
  511. vsync_reg = VSYNC(pipe);
  512. pipeconf_reg = PIPECONF(pipe);
  513. pipe_dsl_reg = PIPEDSL(pipe);
  514. save_bclrpat = I915_READ(bclrpat_reg);
  515. save_vtotal = I915_READ(vtotal_reg);
  516. vblank = I915_READ(vblank_reg);
  517. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  518. vactive = (save_vtotal & 0x7ff) + 1;
  519. vblank_start = (vblank & 0xfff) + 1;
  520. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  521. /* Set the border color to purple. */
  522. I915_WRITE(bclrpat_reg, 0x500050);
  523. if (!IS_GEN2(dev_priv)) {
  524. uint32_t pipeconf = I915_READ(pipeconf_reg);
  525. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  526. POSTING_READ(pipeconf_reg);
  527. /* Wait for next Vblank to substitue
  528. * border color for Color info */
  529. intel_wait_for_vblank(dev_priv, pipe);
  530. st00 = I915_READ8(_VGA_MSR_WRITE);
  531. status = ((st00 & (1 << 4)) != 0) ?
  532. connector_status_connected :
  533. connector_status_disconnected;
  534. I915_WRITE(pipeconf_reg, pipeconf);
  535. } else {
  536. bool restore_vblank = false;
  537. int count, detect;
  538. /*
  539. * If there isn't any border, add some.
  540. * Yes, this will flicker
  541. */
  542. if (vblank_start <= vactive && vblank_end >= vtotal) {
  543. uint32_t vsync = I915_READ(vsync_reg);
  544. uint32_t vsync_start = (vsync & 0xffff) + 1;
  545. vblank_start = vsync_start;
  546. I915_WRITE(vblank_reg,
  547. (vblank_start - 1) |
  548. ((vblank_end - 1) << 16));
  549. restore_vblank = true;
  550. }
  551. /* sample in the vertical border, selecting the larger one */
  552. if (vblank_start - vactive >= vtotal - vblank_end)
  553. vsample = (vblank_start + vactive) >> 1;
  554. else
  555. vsample = (vtotal + vblank_end) >> 1;
  556. /*
  557. * Wait for the border to be displayed
  558. */
  559. while (I915_READ(pipe_dsl_reg) >= vactive)
  560. ;
  561. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  562. ;
  563. /*
  564. * Watch ST00 for an entire scanline
  565. */
  566. detect = 0;
  567. count = 0;
  568. do {
  569. count++;
  570. /* Read the ST00 VGA status register */
  571. st00 = I915_READ8(_VGA_MSR_WRITE);
  572. if (st00 & (1 << 4))
  573. detect++;
  574. } while ((I915_READ(pipe_dsl_reg) == dsl));
  575. /* restore vblank if necessary */
  576. if (restore_vblank)
  577. I915_WRITE(vblank_reg, vblank);
  578. /*
  579. * If more than 3/4 of the scanline detected a monitor,
  580. * then it is assumed to be present. This works even on i830,
  581. * where there isn't any way to force the border color across
  582. * the screen
  583. */
  584. status = detect * 4 > count * 3 ?
  585. connector_status_connected :
  586. connector_status_disconnected;
  587. }
  588. /* Restore previous settings */
  589. I915_WRITE(bclrpat_reg, save_bclrpat);
  590. return status;
  591. }
  592. static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
  593. {
  594. DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
  595. return 1;
  596. }
  597. static const struct dmi_system_id intel_spurious_crt_detect[] = {
  598. {
  599. .callback = intel_spurious_crt_detect_dmi_callback,
  600. .ident = "ACER ZGB",
  601. .matches = {
  602. DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
  603. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  604. },
  605. },
  606. {
  607. .callback = intel_spurious_crt_detect_dmi_callback,
  608. .ident = "Intel DZ77BH-55K",
  609. .matches = {
  610. DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
  611. DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
  612. },
  613. },
  614. { }
  615. };
  616. static int
  617. intel_crt_detect(struct drm_connector *connector,
  618. struct drm_modeset_acquire_ctx *ctx,
  619. bool force)
  620. {
  621. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  622. struct intel_crt *crt = intel_attached_crt(connector);
  623. struct intel_encoder *intel_encoder = &crt->base;
  624. int status, ret;
  625. struct intel_load_detect_pipe tmp;
  626. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  627. connector->base.id, connector->name,
  628. force);
  629. if (i915_modparams.load_detect_test) {
  630. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  631. goto load_detect;
  632. }
  633. /* Skip machines without VGA that falsely report hotplug events */
  634. if (dmi_check_system(intel_spurious_crt_detect))
  635. return connector_status_disconnected;
  636. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  637. if (I915_HAS_HOTPLUG(dev_priv)) {
  638. /* We can not rely on the HPD pin always being correctly wired
  639. * up, for example many KVM do not pass it through, and so
  640. * only trust an assertion that the monitor is connected.
  641. */
  642. if (intel_crt_detect_hotplug(connector)) {
  643. DRM_DEBUG_KMS("CRT detected via hotplug\n");
  644. status = connector_status_connected;
  645. goto out;
  646. } else
  647. DRM_DEBUG_KMS("CRT not detected via hotplug\n");
  648. }
  649. if (intel_crt_detect_ddc(connector)) {
  650. status = connector_status_connected;
  651. goto out;
  652. }
  653. /* Load detection is broken on HPD capable machines. Whoever wants a
  654. * broken monitor (without edid) to work behind a broken kvm (that fails
  655. * to have the right resistors for HP detection) needs to fix this up.
  656. * For now just bail out. */
  657. if (I915_HAS_HOTPLUG(dev_priv)) {
  658. status = connector_status_disconnected;
  659. goto out;
  660. }
  661. load_detect:
  662. if (!force) {
  663. status = connector->status;
  664. goto out;
  665. }
  666. /* for pre-945g platforms use load detect */
  667. ret = intel_get_load_detect_pipe(connector, NULL, &tmp, ctx);
  668. if (ret > 0) {
  669. if (intel_crt_detect_ddc(connector))
  670. status = connector_status_connected;
  671. else if (INTEL_GEN(dev_priv) < 4)
  672. status = intel_crt_load_detect(crt,
  673. to_intel_crtc(connector->state->crtc)->pipe);
  674. else if (i915_modparams.load_detect_test)
  675. status = connector_status_disconnected;
  676. else
  677. status = connector_status_unknown;
  678. intel_release_load_detect_pipe(connector, &tmp, ctx);
  679. } else if (ret == 0) {
  680. status = connector_status_unknown;
  681. } else {
  682. status = ret;
  683. }
  684. out:
  685. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  686. return status;
  687. }
  688. static void intel_crt_destroy(struct drm_connector *connector)
  689. {
  690. drm_connector_cleanup(connector);
  691. kfree(connector);
  692. }
  693. static int intel_crt_get_modes(struct drm_connector *connector)
  694. {
  695. struct drm_device *dev = connector->dev;
  696. struct drm_i915_private *dev_priv = to_i915(dev);
  697. struct intel_crt *crt = intel_attached_crt(connector);
  698. struct intel_encoder *intel_encoder = &crt->base;
  699. int ret;
  700. struct i2c_adapter *i2c;
  701. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  702. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  703. ret = intel_crt_ddc_get_modes(connector, i2c);
  704. if (ret || !IS_G4X(dev_priv))
  705. goto out;
  706. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  707. i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
  708. ret = intel_crt_ddc_get_modes(connector, i2c);
  709. out:
  710. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  711. return ret;
  712. }
  713. void intel_crt_reset(struct drm_encoder *encoder)
  714. {
  715. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  716. struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
  717. if (INTEL_GEN(dev_priv) >= 5) {
  718. u32 adpa;
  719. adpa = I915_READ(crt->adpa_reg);
  720. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  721. adpa |= ADPA_HOTPLUG_BITS;
  722. I915_WRITE(crt->adpa_reg, adpa);
  723. POSTING_READ(crt->adpa_reg);
  724. DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
  725. crt->force_hotplug_required = 1;
  726. }
  727. }
  728. /*
  729. * Routines for controlling stuff on the analog port
  730. */
  731. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  732. .fill_modes = drm_helper_probe_single_connector_modes,
  733. .late_register = intel_connector_register,
  734. .early_unregister = intel_connector_unregister,
  735. .destroy = intel_crt_destroy,
  736. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  737. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  738. };
  739. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  740. .detect_ctx = intel_crt_detect,
  741. .mode_valid = intel_crt_mode_valid,
  742. .get_modes = intel_crt_get_modes,
  743. };
  744. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  745. .reset = intel_crt_reset,
  746. .destroy = intel_encoder_destroy,
  747. };
  748. void intel_crt_init(struct drm_i915_private *dev_priv)
  749. {
  750. struct drm_connector *connector;
  751. struct intel_crt *crt;
  752. struct intel_connector *intel_connector;
  753. i915_reg_t adpa_reg;
  754. u32 adpa;
  755. if (HAS_PCH_SPLIT(dev_priv))
  756. adpa_reg = PCH_ADPA;
  757. else if (IS_VALLEYVIEW(dev_priv))
  758. adpa_reg = VLV_ADPA;
  759. else
  760. adpa_reg = ADPA;
  761. adpa = I915_READ(adpa_reg);
  762. if ((adpa & ADPA_DAC_ENABLE) == 0) {
  763. /*
  764. * On some machines (some IVB at least) CRT can be
  765. * fused off, but there's no known fuse bit to
  766. * indicate that. On these machine the ADPA register
  767. * works normally, except the DAC enable bit won't
  768. * take. So the only way to tell is attempt to enable
  769. * it and see what happens.
  770. */
  771. I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
  772. ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
  773. if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
  774. return;
  775. I915_WRITE(adpa_reg, adpa);
  776. }
  777. crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
  778. if (!crt)
  779. return;
  780. intel_connector = intel_connector_alloc();
  781. if (!intel_connector) {
  782. kfree(crt);
  783. return;
  784. }
  785. connector = &intel_connector->base;
  786. crt->connector = intel_connector;
  787. drm_connector_init(&dev_priv->drm, &intel_connector->base,
  788. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  789. drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
  790. DRM_MODE_ENCODER_DAC, "CRT");
  791. intel_connector_attach_encoder(intel_connector, &crt->base);
  792. crt->base.type = INTEL_OUTPUT_ANALOG;
  793. crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
  794. if (IS_I830(dev_priv))
  795. crt->base.crtc_mask = (1 << 0);
  796. else
  797. crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  798. if (IS_GEN2(dev_priv))
  799. connector->interlace_allowed = 0;
  800. else
  801. connector->interlace_allowed = 1;
  802. connector->doublescan_allowed = 0;
  803. crt->adpa_reg = adpa_reg;
  804. crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
  805. if (I915_HAS_HOTPLUG(dev_priv) &&
  806. !dmi_check_system(intel_spurious_crt_detect)) {
  807. crt->base.hpd_pin = HPD_CRT;
  808. crt->base.hotplug = intel_encoder_hotplug;
  809. }
  810. if (HAS_DDI(dev_priv)) {
  811. crt->base.port = PORT_E;
  812. crt->base.get_config = hsw_crt_get_config;
  813. crt->base.get_hw_state = intel_ddi_get_hw_state;
  814. crt->base.compute_config = hsw_crt_compute_config;
  815. crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
  816. crt->base.pre_enable = hsw_pre_enable_crt;
  817. crt->base.enable = hsw_enable_crt;
  818. crt->base.disable = hsw_disable_crt;
  819. crt->base.post_disable = hsw_post_disable_crt;
  820. } else {
  821. if (HAS_PCH_SPLIT(dev_priv)) {
  822. crt->base.compute_config = pch_crt_compute_config;
  823. crt->base.disable = pch_disable_crt;
  824. crt->base.post_disable = pch_post_disable_crt;
  825. } else {
  826. crt->base.compute_config = intel_crt_compute_config;
  827. crt->base.disable = intel_disable_crt;
  828. }
  829. crt->base.port = PORT_NONE;
  830. crt->base.get_config = intel_crt_get_config;
  831. crt->base.get_hw_state = intel_crt_get_hw_state;
  832. crt->base.enable = intel_enable_crt;
  833. }
  834. intel_connector->get_hw_state = intel_connector_get_hw_state;
  835. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  836. if (!I915_HAS_HOTPLUG(dev_priv))
  837. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  838. /*
  839. * Configure the automatic hotplug detection stuff
  840. */
  841. crt->force_hotplug_required = 0;
  842. /*
  843. * TODO: find a proper way to discover whether we need to set the the
  844. * polarity and link reversal bits or not, instead of relying on the
  845. * BIOS.
  846. */
  847. if (HAS_PCH_LPT(dev_priv)) {
  848. u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
  849. FDI_RX_LINK_REVERSAL_OVERRIDE;
  850. dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
  851. }
  852. intel_crt_reset(&crt->base.base);
  853. }