intel_crt.c 24 KB

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