intel_crt.c 26 KB

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