intel_crt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. enum intel_display_power_domain power_domain;
  67. u32 tmp;
  68. bool ret;
  69. power_domain = intel_display_port_power_domain(encoder);
  70. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  71. return false;
  72. ret = false;
  73. tmp = I915_READ(crt->adpa_reg);
  74. if (!(tmp & ADPA_DAC_ENABLE))
  75. goto out;
  76. if (HAS_PCH_CPT(dev))
  77. *pipe = PORT_TO_PIPE_CPT(tmp);
  78. else
  79. *pipe = PORT_TO_PIPE(tmp);
  80. ret = true;
  81. out:
  82. intel_display_power_put(dev_priv, power_domain);
  83. return ret;
  84. }
  85. static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
  86. {
  87. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  88. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  89. u32 tmp, flags = 0;
  90. tmp = I915_READ(crt->adpa_reg);
  91. if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
  92. flags |= DRM_MODE_FLAG_PHSYNC;
  93. else
  94. flags |= DRM_MODE_FLAG_NHSYNC;
  95. if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
  96. flags |= DRM_MODE_FLAG_PVSYNC;
  97. else
  98. flags |= DRM_MODE_FLAG_NVSYNC;
  99. return flags;
  100. }
  101. static void intel_crt_get_config(struct intel_encoder *encoder,
  102. struct intel_crtc_state *pipe_config)
  103. {
  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, int mode)
  122. {
  123. struct drm_device *dev = encoder->base.dev;
  124. struct drm_i915_private *dev_priv = to_i915(dev);
  125. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  126. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  127. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  128. u32 adpa;
  129. if (INTEL_INFO(dev)->gen >= 5)
  130. adpa = ADPA_HOTPLUG_BITS;
  131. else
  132. adpa = 0;
  133. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  134. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  135. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  136. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  137. /* For CPT allow 3 pipe config, for others just use A or B */
  138. if (HAS_PCH_LPT(dev))
  139. ; /* Those bits don't exist here */
  140. else if (HAS_PCH_CPT(dev))
  141. adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
  142. else if (crtc->pipe == 0)
  143. adpa |= ADPA_PIPE_A_SELECT;
  144. else
  145. adpa |= ADPA_PIPE_B_SELECT;
  146. if (!HAS_PCH_SPLIT(dev))
  147. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  148. switch (mode) {
  149. case DRM_MODE_DPMS_ON:
  150. adpa |= ADPA_DAC_ENABLE;
  151. break;
  152. case DRM_MODE_DPMS_STANDBY:
  153. adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
  154. break;
  155. case DRM_MODE_DPMS_SUSPEND:
  156. adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
  157. break;
  158. case DRM_MODE_DPMS_OFF:
  159. adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
  160. break;
  161. }
  162. I915_WRITE(crt->adpa_reg, adpa);
  163. }
  164. static void intel_disable_crt(struct intel_encoder *encoder)
  165. {
  166. intel_crt_set_dpms(encoder, DRM_MODE_DPMS_OFF);
  167. }
  168. static void pch_disable_crt(struct intel_encoder *encoder)
  169. {
  170. }
  171. static void pch_post_disable_crt(struct intel_encoder *encoder)
  172. {
  173. intel_disable_crt(encoder);
  174. }
  175. static void intel_enable_crt(struct intel_encoder *encoder)
  176. {
  177. intel_crt_set_dpms(encoder, DRM_MODE_DPMS_ON);
  178. }
  179. static enum drm_mode_status
  180. intel_crt_mode_valid(struct drm_connector *connector,
  181. struct drm_display_mode *mode)
  182. {
  183. struct drm_device *dev = connector->dev;
  184. int max_dotclk = to_i915(dev)->max_dotclk_freq;
  185. int max_clock;
  186. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  187. return MODE_NO_DBLESCAN;
  188. if (mode->clock < 25000)
  189. return MODE_CLOCK_LOW;
  190. if (HAS_PCH_LPT(dev))
  191. max_clock = 180000;
  192. else if (IS_VALLEYVIEW(dev))
  193. /*
  194. * 270 MHz due to current DPLL limits,
  195. * DAC limit supposedly 355 MHz.
  196. */
  197. max_clock = 270000;
  198. else if (IS_GEN3(dev) || IS_GEN4(dev))
  199. max_clock = 400000;
  200. else
  201. max_clock = 350000;
  202. if (mode->clock > max_clock)
  203. return MODE_CLOCK_HIGH;
  204. if (mode->clock > max_dotclk)
  205. return MODE_CLOCK_HIGH;
  206. /* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
  207. if (HAS_PCH_LPT(dev) &&
  208. (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
  209. return MODE_CLOCK_HIGH;
  210. return MODE_OK;
  211. }
  212. static bool intel_crt_compute_config(struct intel_encoder *encoder,
  213. struct intel_crtc_state *pipe_config)
  214. {
  215. struct drm_device *dev = encoder->base.dev;
  216. if (HAS_PCH_SPLIT(dev))
  217. pipe_config->has_pch_encoder = true;
  218. /* LPT FDI RX only supports 8bpc. */
  219. if (HAS_PCH_LPT(dev)) {
  220. if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
  221. DRM_DEBUG_KMS("LPT only supports 24bpp\n");
  222. return false;
  223. }
  224. pipe_config->pipe_bpp = 24;
  225. }
  226. /* FDI must always be 2.7 GHz */
  227. if (HAS_DDI(dev))
  228. pipe_config->port_clock = 135000 * 2;
  229. return true;
  230. }
  231. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  232. {
  233. struct drm_device *dev = connector->dev;
  234. struct intel_crt *crt = intel_attached_crt(connector);
  235. struct drm_i915_private *dev_priv = to_i915(dev);
  236. u32 adpa;
  237. bool ret;
  238. /* The first time through, trigger an explicit detection cycle */
  239. if (crt->force_hotplug_required) {
  240. bool turn_off_dac = HAS_PCH_SPLIT(dev);
  241. u32 save_adpa;
  242. crt->force_hotplug_required = 0;
  243. save_adpa = adpa = I915_READ(crt->adpa_reg);
  244. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  245. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  246. if (turn_off_dac)
  247. adpa &= ~ADPA_DAC_ENABLE;
  248. I915_WRITE(crt->adpa_reg, adpa);
  249. if (intel_wait_for_register(dev_priv,
  250. crt->adpa_reg,
  251. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  252. 1000))
  253. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  254. if (turn_off_dac) {
  255. I915_WRITE(crt->adpa_reg, save_adpa);
  256. POSTING_READ(crt->adpa_reg);
  257. }
  258. }
  259. /* Check the status to see if both blue and green are on now */
  260. adpa = I915_READ(crt->adpa_reg);
  261. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  262. ret = true;
  263. else
  264. ret = false;
  265. DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
  266. return ret;
  267. }
  268. static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
  269. {
  270. struct drm_device *dev = connector->dev;
  271. struct intel_crt *crt = intel_attached_crt(connector);
  272. struct drm_i915_private *dev_priv = to_i915(dev);
  273. bool reenable_hpd;
  274. u32 adpa;
  275. bool ret;
  276. u32 save_adpa;
  277. /*
  278. * Doing a force trigger causes a hpd interrupt to get sent, which can
  279. * get us stuck in a loop if we're polling:
  280. * - We enable power wells and reset the ADPA
  281. * - output_poll_exec does force probe on VGA, triggering a hpd
  282. * - HPD handler waits for poll to unlock dev->mode_config.mutex
  283. * - output_poll_exec shuts off the ADPA, unlocks
  284. * dev->mode_config.mutex
  285. * - HPD handler runs, resets ADPA and brings us back to the start
  286. *
  287. * Just disable HPD interrupts here to prevent this
  288. */
  289. reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
  290. save_adpa = adpa = I915_READ(crt->adpa_reg);
  291. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  292. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  293. I915_WRITE(crt->adpa_reg, adpa);
  294. if (intel_wait_for_register(dev_priv,
  295. crt->adpa_reg,
  296. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  297. 1000)) {
  298. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  299. I915_WRITE(crt->adpa_reg, save_adpa);
  300. }
  301. /* Check the status to see if both blue and green are on now */
  302. adpa = I915_READ(crt->adpa_reg);
  303. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  304. ret = true;
  305. else
  306. ret = false;
  307. DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
  308. if (reenable_hpd)
  309. intel_hpd_enable(dev_priv, crt->base.hpd_pin);
  310. return ret;
  311. }
  312. /**
  313. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  314. *
  315. * Not for i915G/i915GM
  316. *
  317. * \return true if CRT is connected.
  318. * \return false if CRT is disconnected.
  319. */
  320. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  321. {
  322. struct drm_device *dev = connector->dev;
  323. struct drm_i915_private *dev_priv = to_i915(dev);
  324. u32 stat;
  325. bool ret = false;
  326. int i, tries = 0;
  327. if (HAS_PCH_SPLIT(dev))
  328. return intel_ironlake_crt_detect_hotplug(connector);
  329. if (IS_VALLEYVIEW(dev))
  330. return valleyview_crt_detect_hotplug(connector);
  331. /*
  332. * On 4 series desktop, CRT detect sequence need to be done twice
  333. * to get a reliable result.
  334. */
  335. if (IS_G4X(dev) && !IS_GM45(dev))
  336. tries = 2;
  337. else
  338. tries = 1;
  339. for (i = 0; i < tries ; i++) {
  340. /* turn on the FORCE_DETECT */
  341. i915_hotplug_interrupt_update(dev_priv,
  342. CRT_HOTPLUG_FORCE_DETECT,
  343. CRT_HOTPLUG_FORCE_DETECT);
  344. /* wait for FORCE_DETECT to go off */
  345. if (intel_wait_for_register(dev_priv, PORT_HOTPLUG_EN,
  346. CRT_HOTPLUG_FORCE_DETECT, 0,
  347. 1000))
  348. DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
  349. }
  350. stat = I915_READ(PORT_HOTPLUG_STAT);
  351. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  352. ret = true;
  353. /* clear the interrupt we just generated, if any */
  354. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  355. i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
  356. return ret;
  357. }
  358. static struct edid *intel_crt_get_edid(struct drm_connector *connector,
  359. struct i2c_adapter *i2c)
  360. {
  361. struct edid *edid;
  362. edid = drm_get_edid(connector, i2c);
  363. if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
  364. DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
  365. intel_gmbus_force_bit(i2c, true);
  366. edid = drm_get_edid(connector, i2c);
  367. intel_gmbus_force_bit(i2c, false);
  368. }
  369. return edid;
  370. }
  371. /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
  372. static int intel_crt_ddc_get_modes(struct drm_connector *connector,
  373. struct i2c_adapter *adapter)
  374. {
  375. struct edid *edid;
  376. int ret;
  377. edid = intel_crt_get_edid(connector, adapter);
  378. if (!edid)
  379. return 0;
  380. ret = intel_connector_update_modes(connector, edid);
  381. kfree(edid);
  382. return ret;
  383. }
  384. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  385. {
  386. struct intel_crt *crt = intel_attached_crt(connector);
  387. struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
  388. struct edid *edid;
  389. struct i2c_adapter *i2c;
  390. BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
  391. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  392. edid = intel_crt_get_edid(connector, i2c);
  393. if (edid) {
  394. bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
  395. /*
  396. * This may be a DVI-I connector with a shared DDC
  397. * link between analog and digital outputs, so we
  398. * have to check the EDID input spec of the attached device.
  399. */
  400. if (!is_digital) {
  401. DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
  402. return true;
  403. }
  404. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
  405. } else {
  406. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
  407. }
  408. kfree(edid);
  409. return false;
  410. }
  411. static enum drm_connector_status
  412. intel_crt_load_detect(struct intel_crt *crt, uint32_t pipe)
  413. {
  414. struct drm_device *dev = crt->base.base.dev;
  415. struct drm_i915_private *dev_priv = to_i915(dev);
  416. uint32_t save_bclrpat;
  417. uint32_t save_vtotal;
  418. uint32_t vtotal, vactive;
  419. uint32_t vsample;
  420. uint32_t vblank, vblank_start, vblank_end;
  421. uint32_t dsl;
  422. i915_reg_t bclrpat_reg, vtotal_reg,
  423. vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
  424. uint8_t st00;
  425. enum drm_connector_status status;
  426. DRM_DEBUG_KMS("starting load-detect on CRT\n");
  427. bclrpat_reg = BCLRPAT(pipe);
  428. vtotal_reg = VTOTAL(pipe);
  429. vblank_reg = VBLANK(pipe);
  430. vsync_reg = VSYNC(pipe);
  431. pipeconf_reg = PIPECONF(pipe);
  432. pipe_dsl_reg = PIPEDSL(pipe);
  433. save_bclrpat = I915_READ(bclrpat_reg);
  434. save_vtotal = I915_READ(vtotal_reg);
  435. vblank = I915_READ(vblank_reg);
  436. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  437. vactive = (save_vtotal & 0x7ff) + 1;
  438. vblank_start = (vblank & 0xfff) + 1;
  439. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  440. /* Set the border color to purple. */
  441. I915_WRITE(bclrpat_reg, 0x500050);
  442. if (!IS_GEN2(dev)) {
  443. uint32_t pipeconf = I915_READ(pipeconf_reg);
  444. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  445. POSTING_READ(pipeconf_reg);
  446. /* Wait for next Vblank to substitue
  447. * border color for Color info */
  448. intel_wait_for_vblank(dev, pipe);
  449. st00 = I915_READ8(_VGA_MSR_WRITE);
  450. status = ((st00 & (1 << 4)) != 0) ?
  451. connector_status_connected :
  452. connector_status_disconnected;
  453. I915_WRITE(pipeconf_reg, pipeconf);
  454. } else {
  455. bool restore_vblank = false;
  456. int count, detect;
  457. /*
  458. * If there isn't any border, add some.
  459. * Yes, this will flicker
  460. */
  461. if (vblank_start <= vactive && vblank_end >= vtotal) {
  462. uint32_t vsync = I915_READ(vsync_reg);
  463. uint32_t vsync_start = (vsync & 0xffff) + 1;
  464. vblank_start = vsync_start;
  465. I915_WRITE(vblank_reg,
  466. (vblank_start - 1) |
  467. ((vblank_end - 1) << 16));
  468. restore_vblank = true;
  469. }
  470. /* sample in the vertical border, selecting the larger one */
  471. if (vblank_start - vactive >= vtotal - vblank_end)
  472. vsample = (vblank_start + vactive) >> 1;
  473. else
  474. vsample = (vtotal + vblank_end) >> 1;
  475. /*
  476. * Wait for the border to be displayed
  477. */
  478. while (I915_READ(pipe_dsl_reg) >= vactive)
  479. ;
  480. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  481. ;
  482. /*
  483. * Watch ST00 for an entire scanline
  484. */
  485. detect = 0;
  486. count = 0;
  487. do {
  488. count++;
  489. /* Read the ST00 VGA status register */
  490. st00 = I915_READ8(_VGA_MSR_WRITE);
  491. if (st00 & (1 << 4))
  492. detect++;
  493. } while ((I915_READ(pipe_dsl_reg) == dsl));
  494. /* restore vblank if necessary */
  495. if (restore_vblank)
  496. I915_WRITE(vblank_reg, vblank);
  497. /*
  498. * If more than 3/4 of the scanline detected a monitor,
  499. * then it is assumed to be present. This works even on i830,
  500. * where there isn't any way to force the border color across
  501. * the screen
  502. */
  503. status = detect * 4 > count * 3 ?
  504. connector_status_connected :
  505. connector_status_disconnected;
  506. }
  507. /* Restore previous settings */
  508. I915_WRITE(bclrpat_reg, save_bclrpat);
  509. return status;
  510. }
  511. static enum drm_connector_status
  512. intel_crt_detect(struct drm_connector *connector, bool force)
  513. {
  514. struct drm_device *dev = connector->dev;
  515. struct drm_i915_private *dev_priv = to_i915(dev);
  516. struct intel_crt *crt = intel_attached_crt(connector);
  517. struct intel_encoder *intel_encoder = &crt->base;
  518. enum intel_display_power_domain power_domain;
  519. enum drm_connector_status status;
  520. struct intel_load_detect_pipe tmp;
  521. struct drm_modeset_acquire_ctx ctx;
  522. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  523. connector->base.id, connector->name,
  524. force);
  525. power_domain = intel_display_port_power_domain(intel_encoder);
  526. intel_display_power_get(dev_priv, power_domain);
  527. if (I915_HAS_HOTPLUG(dev)) {
  528. /* We can not rely on the HPD pin always being correctly wired
  529. * up, for example many KVM do not pass it through, and so
  530. * only trust an assertion that the monitor is connected.
  531. */
  532. if (intel_crt_detect_hotplug(connector)) {
  533. DRM_DEBUG_KMS("CRT detected via hotplug\n");
  534. status = connector_status_connected;
  535. goto out;
  536. } else
  537. DRM_DEBUG_KMS("CRT not detected via hotplug\n");
  538. }
  539. if (intel_crt_detect_ddc(connector)) {
  540. status = connector_status_connected;
  541. goto out;
  542. }
  543. /* Load detection is broken on HPD capable machines. Whoever wants a
  544. * broken monitor (without edid) to work behind a broken kvm (that fails
  545. * to have the right resistors for HP detection) needs to fix this up.
  546. * For now just bail out. */
  547. if (I915_HAS_HOTPLUG(dev) && !i915.load_detect_test) {
  548. status = connector_status_disconnected;
  549. goto out;
  550. }
  551. if (!force) {
  552. status = connector->status;
  553. goto out;
  554. }
  555. drm_modeset_acquire_init(&ctx, 0);
  556. /* for pre-945g platforms use load detect */
  557. if (intel_get_load_detect_pipe(connector, NULL, &tmp, &ctx)) {
  558. if (intel_crt_detect_ddc(connector))
  559. status = connector_status_connected;
  560. else if (INTEL_INFO(dev)->gen < 4)
  561. status = intel_crt_load_detect(crt,
  562. to_intel_crtc(connector->state->crtc)->pipe);
  563. else if (i915.load_detect_test)
  564. status = connector_status_disconnected;
  565. else
  566. status = connector_status_unknown;
  567. intel_release_load_detect_pipe(connector, &tmp, &ctx);
  568. } else
  569. status = connector_status_unknown;
  570. drm_modeset_drop_locks(&ctx);
  571. drm_modeset_acquire_fini(&ctx);
  572. out:
  573. intel_display_power_put(dev_priv, power_domain);
  574. return status;
  575. }
  576. static void intel_crt_destroy(struct drm_connector *connector)
  577. {
  578. drm_connector_cleanup(connector);
  579. kfree(connector);
  580. }
  581. static int intel_crt_get_modes(struct drm_connector *connector)
  582. {
  583. struct drm_device *dev = connector->dev;
  584. struct drm_i915_private *dev_priv = to_i915(dev);
  585. struct intel_crt *crt = intel_attached_crt(connector);
  586. struct intel_encoder *intel_encoder = &crt->base;
  587. enum intel_display_power_domain power_domain;
  588. int ret;
  589. struct i2c_adapter *i2c;
  590. power_domain = intel_display_port_power_domain(intel_encoder);
  591. intel_display_power_get(dev_priv, power_domain);
  592. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  593. ret = intel_crt_ddc_get_modes(connector, i2c);
  594. if (ret || !IS_G4X(dev))
  595. goto out;
  596. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  597. i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
  598. ret = intel_crt_ddc_get_modes(connector, i2c);
  599. out:
  600. intel_display_power_put(dev_priv, power_domain);
  601. return ret;
  602. }
  603. static int intel_crt_set_property(struct drm_connector *connector,
  604. struct drm_property *property,
  605. uint64_t value)
  606. {
  607. return 0;
  608. }
  609. void intel_crt_reset(struct drm_encoder *encoder)
  610. {
  611. struct drm_device *dev = encoder->dev;
  612. struct drm_i915_private *dev_priv = to_i915(dev);
  613. struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
  614. if (INTEL_INFO(dev)->gen >= 5) {
  615. u32 adpa;
  616. adpa = I915_READ(crt->adpa_reg);
  617. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  618. adpa |= ADPA_HOTPLUG_BITS;
  619. I915_WRITE(crt->adpa_reg, adpa);
  620. POSTING_READ(crt->adpa_reg);
  621. DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
  622. crt->force_hotplug_required = 1;
  623. }
  624. }
  625. /*
  626. * Routines for controlling stuff on the analog port
  627. */
  628. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  629. .dpms = drm_atomic_helper_connector_dpms,
  630. .detect = intel_crt_detect,
  631. .fill_modes = drm_helper_probe_single_connector_modes,
  632. .late_register = intel_connector_register,
  633. .early_unregister = intel_connector_unregister,
  634. .destroy = intel_crt_destroy,
  635. .set_property = intel_crt_set_property,
  636. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  637. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  638. .atomic_get_property = intel_connector_atomic_get_property,
  639. };
  640. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  641. .mode_valid = intel_crt_mode_valid,
  642. .get_modes = intel_crt_get_modes,
  643. };
  644. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  645. .reset = intel_crt_reset,
  646. .destroy = intel_encoder_destroy,
  647. };
  648. static int intel_no_crt_dmi_callback(const struct dmi_system_id *id)
  649. {
  650. DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
  651. return 1;
  652. }
  653. static const struct dmi_system_id intel_no_crt[] = {
  654. {
  655. .callback = intel_no_crt_dmi_callback,
  656. .ident = "ACER ZGB",
  657. .matches = {
  658. DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
  659. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  660. },
  661. },
  662. {
  663. .callback = intel_no_crt_dmi_callback,
  664. .ident = "DELL XPS 8700",
  665. .matches = {
  666. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  667. DMI_MATCH(DMI_PRODUCT_NAME, "XPS 8700"),
  668. },
  669. },
  670. { }
  671. };
  672. void intel_crt_init(struct drm_device *dev)
  673. {
  674. struct drm_connector *connector;
  675. struct intel_crt *crt;
  676. struct intel_connector *intel_connector;
  677. struct drm_i915_private *dev_priv = to_i915(dev);
  678. i915_reg_t adpa_reg;
  679. u32 adpa;
  680. /* Skip machines without VGA that falsely report hotplug events */
  681. if (dmi_check_system(intel_no_crt))
  682. return;
  683. if (HAS_PCH_SPLIT(dev))
  684. adpa_reg = PCH_ADPA;
  685. else if (IS_VALLEYVIEW(dev))
  686. adpa_reg = VLV_ADPA;
  687. else
  688. adpa_reg = ADPA;
  689. adpa = I915_READ(adpa_reg);
  690. if ((adpa & ADPA_DAC_ENABLE) == 0) {
  691. /*
  692. * On some machines (some IVB at least) CRT can be
  693. * fused off, but there's no known fuse bit to
  694. * indicate that. On these machine the ADPA register
  695. * works normally, except the DAC enable bit won't
  696. * take. So the only way to tell is attempt to enable
  697. * it and see what happens.
  698. */
  699. I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
  700. ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
  701. if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
  702. return;
  703. I915_WRITE(adpa_reg, adpa);
  704. }
  705. crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
  706. if (!crt)
  707. return;
  708. intel_connector = intel_connector_alloc();
  709. if (!intel_connector) {
  710. kfree(crt);
  711. return;
  712. }
  713. connector = &intel_connector->base;
  714. crt->connector = intel_connector;
  715. drm_connector_init(dev, &intel_connector->base,
  716. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  717. drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
  718. DRM_MODE_ENCODER_DAC, "CRT");
  719. intel_connector_attach_encoder(intel_connector, &crt->base);
  720. crt->base.type = INTEL_OUTPUT_ANALOG;
  721. crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
  722. if (IS_I830(dev))
  723. crt->base.crtc_mask = (1 << 0);
  724. else
  725. crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  726. if (IS_GEN2(dev))
  727. connector->interlace_allowed = 0;
  728. else
  729. connector->interlace_allowed = 1;
  730. connector->doublescan_allowed = 0;
  731. crt->adpa_reg = adpa_reg;
  732. crt->base.compute_config = intel_crt_compute_config;
  733. if (HAS_PCH_SPLIT(dev)) {
  734. crt->base.disable = pch_disable_crt;
  735. crt->base.post_disable = pch_post_disable_crt;
  736. } else {
  737. crt->base.disable = intel_disable_crt;
  738. }
  739. crt->base.enable = intel_enable_crt;
  740. if (I915_HAS_HOTPLUG(dev))
  741. crt->base.hpd_pin = HPD_CRT;
  742. if (HAS_DDI(dev)) {
  743. crt->base.get_config = hsw_crt_get_config;
  744. crt->base.get_hw_state = intel_ddi_get_hw_state;
  745. } else {
  746. crt->base.get_config = intel_crt_get_config;
  747. crt->base.get_hw_state = intel_crt_get_hw_state;
  748. }
  749. intel_connector->get_hw_state = intel_connector_get_hw_state;
  750. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  751. if (!I915_HAS_HOTPLUG(dev))
  752. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  753. /*
  754. * Configure the automatic hotplug detection stuff
  755. */
  756. crt->force_hotplug_required = 0;
  757. /*
  758. * TODO: find a proper way to discover whether we need to set the the
  759. * polarity and link reversal bits or not, instead of relying on the
  760. * BIOS.
  761. */
  762. if (HAS_PCH_LPT(dev)) {
  763. u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
  764. FDI_RX_LINK_REVERSAL_OVERRIDE;
  765. dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
  766. }
  767. intel_crt_reset(&crt->base.base);
  768. }