intel_crt.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. return true;
  280. }
  281. static bool pch_crt_compute_config(struct intel_encoder *encoder,
  282. struct intel_crtc_state *pipe_config,
  283. struct drm_connector_state *conn_state)
  284. {
  285. pipe_config->has_pch_encoder = true;
  286. return true;
  287. }
  288. static bool hsw_crt_compute_config(struct intel_encoder *encoder,
  289. struct intel_crtc_state *pipe_config,
  290. struct drm_connector_state *conn_state)
  291. {
  292. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  293. pipe_config->has_pch_encoder = true;
  294. /* LPT FDI RX only supports 8bpc. */
  295. if (HAS_PCH_LPT(dev_priv)) {
  296. if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
  297. DRM_DEBUG_KMS("LPT only supports 24bpp\n");
  298. return false;
  299. }
  300. pipe_config->pipe_bpp = 24;
  301. }
  302. /* FDI must always be 2.7 GHz */
  303. pipe_config->port_clock = 135000 * 2;
  304. return true;
  305. }
  306. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  307. {
  308. struct drm_device *dev = connector->dev;
  309. struct intel_crt *crt = intel_attached_crt(connector);
  310. struct drm_i915_private *dev_priv = to_i915(dev);
  311. u32 adpa;
  312. bool ret;
  313. /* The first time through, trigger an explicit detection cycle */
  314. if (crt->force_hotplug_required) {
  315. bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
  316. u32 save_adpa;
  317. crt->force_hotplug_required = 0;
  318. save_adpa = adpa = I915_READ(crt->adpa_reg);
  319. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  320. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  321. if (turn_off_dac)
  322. adpa &= ~ADPA_DAC_ENABLE;
  323. I915_WRITE(crt->adpa_reg, adpa);
  324. if (intel_wait_for_register(dev_priv,
  325. crt->adpa_reg,
  326. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  327. 1000))
  328. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  329. if (turn_off_dac) {
  330. I915_WRITE(crt->adpa_reg, save_adpa);
  331. POSTING_READ(crt->adpa_reg);
  332. }
  333. }
  334. /* Check the status to see if both blue and green are on now */
  335. adpa = I915_READ(crt->adpa_reg);
  336. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  337. ret = true;
  338. else
  339. ret = false;
  340. DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
  341. return ret;
  342. }
  343. static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
  344. {
  345. struct drm_device *dev = connector->dev;
  346. struct intel_crt *crt = intel_attached_crt(connector);
  347. struct drm_i915_private *dev_priv = to_i915(dev);
  348. bool reenable_hpd;
  349. u32 adpa;
  350. bool ret;
  351. u32 save_adpa;
  352. /*
  353. * Doing a force trigger causes a hpd interrupt to get sent, which can
  354. * get us stuck in a loop if we're polling:
  355. * - We enable power wells and reset the ADPA
  356. * - output_poll_exec does force probe on VGA, triggering a hpd
  357. * - HPD handler waits for poll to unlock dev->mode_config.mutex
  358. * - output_poll_exec shuts off the ADPA, unlocks
  359. * dev->mode_config.mutex
  360. * - HPD handler runs, resets ADPA and brings us back to the start
  361. *
  362. * Just disable HPD interrupts here to prevent this
  363. */
  364. reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
  365. save_adpa = adpa = I915_READ(crt->adpa_reg);
  366. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  367. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  368. I915_WRITE(crt->adpa_reg, adpa);
  369. if (intel_wait_for_register(dev_priv,
  370. crt->adpa_reg,
  371. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  372. 1000)) {
  373. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  374. I915_WRITE(crt->adpa_reg, save_adpa);
  375. }
  376. /* Check the status to see if both blue and green are on now */
  377. adpa = I915_READ(crt->adpa_reg);
  378. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  379. ret = true;
  380. else
  381. ret = false;
  382. DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
  383. if (reenable_hpd)
  384. intel_hpd_enable(dev_priv, crt->base.hpd_pin);
  385. return ret;
  386. }
  387. /**
  388. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  389. *
  390. * Not for i915G/i915GM
  391. *
  392. * \return true if CRT is connected.
  393. * \return false if CRT is disconnected.
  394. */
  395. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  396. {
  397. struct drm_device *dev = connector->dev;
  398. struct drm_i915_private *dev_priv = to_i915(dev);
  399. u32 stat;
  400. bool ret = false;
  401. int i, tries = 0;
  402. if (HAS_PCH_SPLIT(dev_priv))
  403. return intel_ironlake_crt_detect_hotplug(connector);
  404. if (IS_VALLEYVIEW(dev_priv))
  405. return valleyview_crt_detect_hotplug(connector);
  406. /*
  407. * On 4 series desktop, CRT detect sequence need to be done twice
  408. * to get a reliable result.
  409. */
  410. if (IS_G4X(dev_priv) && !IS_GM45(dev_priv))
  411. tries = 2;
  412. else
  413. tries = 1;
  414. for (i = 0; i < tries ; i++) {
  415. /* turn on the FORCE_DETECT */
  416. i915_hotplug_interrupt_update(dev_priv,
  417. CRT_HOTPLUG_FORCE_DETECT,
  418. CRT_HOTPLUG_FORCE_DETECT);
  419. /* wait for FORCE_DETECT to go off */
  420. if (intel_wait_for_register(dev_priv, PORT_HOTPLUG_EN,
  421. CRT_HOTPLUG_FORCE_DETECT, 0,
  422. 1000))
  423. DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
  424. }
  425. stat = I915_READ(PORT_HOTPLUG_STAT);
  426. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  427. ret = true;
  428. /* clear the interrupt we just generated, if any */
  429. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  430. i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
  431. return ret;
  432. }
  433. static struct edid *intel_crt_get_edid(struct drm_connector *connector,
  434. struct i2c_adapter *i2c)
  435. {
  436. struct edid *edid;
  437. edid = drm_get_edid(connector, i2c);
  438. if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
  439. DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
  440. intel_gmbus_force_bit(i2c, true);
  441. edid = drm_get_edid(connector, i2c);
  442. intel_gmbus_force_bit(i2c, false);
  443. }
  444. return edid;
  445. }
  446. /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
  447. static int intel_crt_ddc_get_modes(struct drm_connector *connector,
  448. struct i2c_adapter *adapter)
  449. {
  450. struct edid *edid;
  451. int ret;
  452. edid = intel_crt_get_edid(connector, adapter);
  453. if (!edid)
  454. return 0;
  455. ret = intel_connector_update_modes(connector, edid);
  456. kfree(edid);
  457. return ret;
  458. }
  459. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  460. {
  461. struct intel_crt *crt = intel_attached_crt(connector);
  462. struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
  463. struct edid *edid;
  464. struct i2c_adapter *i2c;
  465. bool ret = false;
  466. BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
  467. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  468. edid = intel_crt_get_edid(connector, i2c);
  469. if (edid) {
  470. bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
  471. /*
  472. * This may be a DVI-I connector with a shared DDC
  473. * link between analog and digital outputs, so we
  474. * have to check the EDID input spec of the attached device.
  475. */
  476. if (!is_digital) {
  477. DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
  478. ret = true;
  479. } else {
  480. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
  481. }
  482. } else {
  483. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
  484. }
  485. kfree(edid);
  486. return ret;
  487. }
  488. static enum drm_connector_status
  489. intel_crt_load_detect(struct intel_crt *crt, uint32_t pipe)
  490. {
  491. struct drm_device *dev = crt->base.base.dev;
  492. struct drm_i915_private *dev_priv = to_i915(dev);
  493. uint32_t save_bclrpat;
  494. uint32_t save_vtotal;
  495. uint32_t vtotal, vactive;
  496. uint32_t vsample;
  497. uint32_t vblank, vblank_start, vblank_end;
  498. uint32_t dsl;
  499. i915_reg_t bclrpat_reg, vtotal_reg,
  500. vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
  501. uint8_t st00;
  502. enum drm_connector_status status;
  503. DRM_DEBUG_KMS("starting load-detect on CRT\n");
  504. bclrpat_reg = BCLRPAT(pipe);
  505. vtotal_reg = VTOTAL(pipe);
  506. vblank_reg = VBLANK(pipe);
  507. vsync_reg = VSYNC(pipe);
  508. pipeconf_reg = PIPECONF(pipe);
  509. pipe_dsl_reg = PIPEDSL(pipe);
  510. save_bclrpat = I915_READ(bclrpat_reg);
  511. save_vtotal = I915_READ(vtotal_reg);
  512. vblank = I915_READ(vblank_reg);
  513. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  514. vactive = (save_vtotal & 0x7ff) + 1;
  515. vblank_start = (vblank & 0xfff) + 1;
  516. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  517. /* Set the border color to purple. */
  518. I915_WRITE(bclrpat_reg, 0x500050);
  519. if (!IS_GEN2(dev_priv)) {
  520. uint32_t pipeconf = I915_READ(pipeconf_reg);
  521. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  522. POSTING_READ(pipeconf_reg);
  523. /* Wait for next Vblank to substitue
  524. * border color for Color info */
  525. intel_wait_for_vblank(dev_priv, pipe);
  526. st00 = I915_READ8(_VGA_MSR_WRITE);
  527. status = ((st00 & (1 << 4)) != 0) ?
  528. connector_status_connected :
  529. connector_status_disconnected;
  530. I915_WRITE(pipeconf_reg, pipeconf);
  531. } else {
  532. bool restore_vblank = false;
  533. int count, detect;
  534. /*
  535. * If there isn't any border, add some.
  536. * Yes, this will flicker
  537. */
  538. if (vblank_start <= vactive && vblank_end >= vtotal) {
  539. uint32_t vsync = I915_READ(vsync_reg);
  540. uint32_t vsync_start = (vsync & 0xffff) + 1;
  541. vblank_start = vsync_start;
  542. I915_WRITE(vblank_reg,
  543. (vblank_start - 1) |
  544. ((vblank_end - 1) << 16));
  545. restore_vblank = true;
  546. }
  547. /* sample in the vertical border, selecting the larger one */
  548. if (vblank_start - vactive >= vtotal - vblank_end)
  549. vsample = (vblank_start + vactive) >> 1;
  550. else
  551. vsample = (vtotal + vblank_end) >> 1;
  552. /*
  553. * Wait for the border to be displayed
  554. */
  555. while (I915_READ(pipe_dsl_reg) >= vactive)
  556. ;
  557. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  558. ;
  559. /*
  560. * Watch ST00 for an entire scanline
  561. */
  562. detect = 0;
  563. count = 0;
  564. do {
  565. count++;
  566. /* Read the ST00 VGA status register */
  567. st00 = I915_READ8(_VGA_MSR_WRITE);
  568. if (st00 & (1 << 4))
  569. detect++;
  570. } while ((I915_READ(pipe_dsl_reg) == dsl));
  571. /* restore vblank if necessary */
  572. if (restore_vblank)
  573. I915_WRITE(vblank_reg, vblank);
  574. /*
  575. * If more than 3/4 of the scanline detected a monitor,
  576. * then it is assumed to be present. This works even on i830,
  577. * where there isn't any way to force the border color across
  578. * the screen
  579. */
  580. status = detect * 4 > count * 3 ?
  581. connector_status_connected :
  582. connector_status_disconnected;
  583. }
  584. /* Restore previous settings */
  585. I915_WRITE(bclrpat_reg, save_bclrpat);
  586. return status;
  587. }
  588. static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
  589. {
  590. DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
  591. return 1;
  592. }
  593. static const struct dmi_system_id intel_spurious_crt_detect[] = {
  594. {
  595. .callback = intel_spurious_crt_detect_dmi_callback,
  596. .ident = "ACER ZGB",
  597. .matches = {
  598. DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
  599. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  600. },
  601. },
  602. {
  603. .callback = intel_spurious_crt_detect_dmi_callback,
  604. .ident = "Intel DZ77BH-55K",
  605. .matches = {
  606. DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
  607. DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
  608. },
  609. },
  610. { }
  611. };
  612. static int
  613. intel_crt_detect(struct drm_connector *connector,
  614. struct drm_modeset_acquire_ctx *ctx,
  615. bool force)
  616. {
  617. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  618. struct intel_crt *crt = intel_attached_crt(connector);
  619. struct intel_encoder *intel_encoder = &crt->base;
  620. int status, ret;
  621. struct intel_load_detect_pipe tmp;
  622. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  623. connector->base.id, connector->name,
  624. force);
  625. /* Skip machines without VGA that falsely report hotplug events */
  626. if (dmi_check_system(intel_spurious_crt_detect))
  627. return connector_status_disconnected;
  628. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  629. if (I915_HAS_HOTPLUG(dev_priv)) {
  630. /* We can not rely on the HPD pin always being correctly wired
  631. * up, for example many KVM do not pass it through, and so
  632. * only trust an assertion that the monitor is connected.
  633. */
  634. if (intel_crt_detect_hotplug(connector)) {
  635. DRM_DEBUG_KMS("CRT detected via hotplug\n");
  636. status = connector_status_connected;
  637. goto out;
  638. } else
  639. DRM_DEBUG_KMS("CRT not detected via hotplug\n");
  640. }
  641. if (intel_crt_detect_ddc(connector)) {
  642. status = connector_status_connected;
  643. goto out;
  644. }
  645. /* Load detection is broken on HPD capable machines. Whoever wants a
  646. * broken monitor (without edid) to work behind a broken kvm (that fails
  647. * to have the right resistors for HP detection) needs to fix this up.
  648. * For now just bail out. */
  649. if (I915_HAS_HOTPLUG(dev_priv) && !i915_modparams.load_detect_test) {
  650. status = connector_status_disconnected;
  651. goto out;
  652. }
  653. if (!force) {
  654. status = connector->status;
  655. goto out;
  656. }
  657. /* for pre-945g platforms use load detect */
  658. ret = intel_get_load_detect_pipe(connector, NULL, &tmp, ctx);
  659. if (ret > 0) {
  660. if (intel_crt_detect_ddc(connector))
  661. status = connector_status_connected;
  662. else if (INTEL_GEN(dev_priv) < 4)
  663. status = intel_crt_load_detect(crt,
  664. to_intel_crtc(connector->state->crtc)->pipe);
  665. else if (i915_modparams.load_detect_test)
  666. status = connector_status_disconnected;
  667. else
  668. status = connector_status_unknown;
  669. intel_release_load_detect_pipe(connector, &tmp, ctx);
  670. } else if (ret == 0)
  671. status = connector_status_unknown;
  672. else if (ret < 0)
  673. status = ret;
  674. out:
  675. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  676. return status;
  677. }
  678. static void intel_crt_destroy(struct drm_connector *connector)
  679. {
  680. drm_connector_cleanup(connector);
  681. kfree(connector);
  682. }
  683. static int intel_crt_get_modes(struct drm_connector *connector)
  684. {
  685. struct drm_device *dev = connector->dev;
  686. struct drm_i915_private *dev_priv = to_i915(dev);
  687. struct intel_crt *crt = intel_attached_crt(connector);
  688. struct intel_encoder *intel_encoder = &crt->base;
  689. int ret;
  690. struct i2c_adapter *i2c;
  691. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  692. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  693. ret = intel_crt_ddc_get_modes(connector, i2c);
  694. if (ret || !IS_G4X(dev_priv))
  695. goto out;
  696. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  697. i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
  698. ret = intel_crt_ddc_get_modes(connector, i2c);
  699. out:
  700. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  701. return ret;
  702. }
  703. void intel_crt_reset(struct drm_encoder *encoder)
  704. {
  705. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  706. struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
  707. if (INTEL_GEN(dev_priv) >= 5) {
  708. u32 adpa;
  709. adpa = I915_READ(crt->adpa_reg);
  710. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  711. adpa |= ADPA_HOTPLUG_BITS;
  712. I915_WRITE(crt->adpa_reg, adpa);
  713. POSTING_READ(crt->adpa_reg);
  714. DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
  715. crt->force_hotplug_required = 1;
  716. }
  717. }
  718. /*
  719. * Routines for controlling stuff on the analog port
  720. */
  721. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  722. .fill_modes = drm_helper_probe_single_connector_modes,
  723. .late_register = intel_connector_register,
  724. .early_unregister = intel_connector_unregister,
  725. .destroy = intel_crt_destroy,
  726. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  727. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  728. };
  729. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  730. .detect_ctx = intel_crt_detect,
  731. .mode_valid = intel_crt_mode_valid,
  732. .get_modes = intel_crt_get_modes,
  733. };
  734. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  735. .reset = intel_crt_reset,
  736. .destroy = intel_encoder_destroy,
  737. };
  738. void intel_crt_init(struct drm_i915_private *dev_priv)
  739. {
  740. struct drm_connector *connector;
  741. struct intel_crt *crt;
  742. struct intel_connector *intel_connector;
  743. i915_reg_t adpa_reg;
  744. u32 adpa;
  745. if (HAS_PCH_SPLIT(dev_priv))
  746. adpa_reg = PCH_ADPA;
  747. else if (IS_VALLEYVIEW(dev_priv))
  748. adpa_reg = VLV_ADPA;
  749. else
  750. adpa_reg = ADPA;
  751. adpa = I915_READ(adpa_reg);
  752. if ((adpa & ADPA_DAC_ENABLE) == 0) {
  753. /*
  754. * On some machines (some IVB at least) CRT can be
  755. * fused off, but there's no known fuse bit to
  756. * indicate that. On these machine the ADPA register
  757. * works normally, except the DAC enable bit won't
  758. * take. So the only way to tell is attempt to enable
  759. * it and see what happens.
  760. */
  761. I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
  762. ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
  763. if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
  764. return;
  765. I915_WRITE(adpa_reg, adpa);
  766. }
  767. crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
  768. if (!crt)
  769. return;
  770. intel_connector = intel_connector_alloc();
  771. if (!intel_connector) {
  772. kfree(crt);
  773. return;
  774. }
  775. connector = &intel_connector->base;
  776. crt->connector = intel_connector;
  777. drm_connector_init(&dev_priv->drm, &intel_connector->base,
  778. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  779. drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
  780. DRM_MODE_ENCODER_DAC, "CRT");
  781. intel_connector_attach_encoder(intel_connector, &crt->base);
  782. crt->base.type = INTEL_OUTPUT_ANALOG;
  783. crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
  784. if (IS_I830(dev_priv))
  785. crt->base.crtc_mask = (1 << 0);
  786. else
  787. crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  788. if (IS_GEN2(dev_priv))
  789. connector->interlace_allowed = 0;
  790. else
  791. connector->interlace_allowed = 1;
  792. connector->doublescan_allowed = 0;
  793. crt->adpa_reg = adpa_reg;
  794. crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
  795. if (I915_HAS_HOTPLUG(dev_priv) &&
  796. !dmi_check_system(intel_spurious_crt_detect))
  797. crt->base.hpd_pin = HPD_CRT;
  798. if (HAS_DDI(dev_priv)) {
  799. crt->base.port = PORT_E;
  800. crt->base.get_config = hsw_crt_get_config;
  801. crt->base.get_hw_state = intel_ddi_get_hw_state;
  802. crt->base.compute_config = hsw_crt_compute_config;
  803. crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
  804. crt->base.pre_enable = hsw_pre_enable_crt;
  805. crt->base.enable = hsw_enable_crt;
  806. crt->base.disable = hsw_disable_crt;
  807. crt->base.post_disable = hsw_post_disable_crt;
  808. } else {
  809. if (HAS_PCH_SPLIT(dev_priv)) {
  810. crt->base.compute_config = pch_crt_compute_config;
  811. crt->base.disable = pch_disable_crt;
  812. crt->base.post_disable = pch_post_disable_crt;
  813. } else {
  814. crt->base.compute_config = intel_crt_compute_config;
  815. crt->base.disable = intel_disable_crt;
  816. }
  817. crt->base.port = PORT_NONE;
  818. crt->base.get_config = intel_crt_get_config;
  819. crt->base.get_hw_state = intel_crt_get_hw_state;
  820. crt->base.enable = intel_enable_crt;
  821. }
  822. intel_connector->get_hw_state = intel_connector_get_hw_state;
  823. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  824. if (!I915_HAS_HOTPLUG(dev_priv))
  825. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  826. /*
  827. * Configure the automatic hotplug detection stuff
  828. */
  829. crt->force_hotplug_required = 0;
  830. /*
  831. * TODO: find a proper way to discover whether we need to set the the
  832. * polarity and link reversal bits or not, instead of relying on the
  833. * BIOS.
  834. */
  835. if (HAS_PCH_LPT(dev_priv)) {
  836. u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
  837. FDI_RX_LINK_REVERSAL_OVERRIDE;
  838. dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
  839. }
  840. intel_crt_reset(&crt->base.base);
  841. }