intel_crt.c 29 KB

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