intel_crt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. static void hsw_crt_pre_enable(struct intel_encoder *encoder)
  118. {
  119. struct drm_device *dev = encoder->base.dev;
  120. struct drm_i915_private *dev_priv = dev->dev_private;
  121. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL already enabled\n");
  122. I915_WRITE(SPLL_CTL,
  123. SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC);
  124. POSTING_READ(SPLL_CTL);
  125. udelay(20);
  126. }
  127. /* Note: The caller is required to filter out dpms modes not supported by the
  128. * platform. */
  129. static void intel_crt_set_dpms(struct intel_encoder *encoder, int mode)
  130. {
  131. struct drm_device *dev = encoder->base.dev;
  132. struct drm_i915_private *dev_priv = dev->dev_private;
  133. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  134. struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  135. struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  136. u32 adpa;
  137. if (INTEL_INFO(dev)->gen >= 5)
  138. adpa = ADPA_HOTPLUG_BITS;
  139. else
  140. adpa = 0;
  141. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  142. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  143. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  144. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  145. /* For CPT allow 3 pipe config, for others just use A or B */
  146. if (HAS_PCH_LPT(dev))
  147. ; /* Those bits don't exist here */
  148. else if (HAS_PCH_CPT(dev))
  149. adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
  150. else if (crtc->pipe == 0)
  151. adpa |= ADPA_PIPE_A_SELECT;
  152. else
  153. adpa |= ADPA_PIPE_B_SELECT;
  154. if (!HAS_PCH_SPLIT(dev))
  155. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  156. switch (mode) {
  157. case DRM_MODE_DPMS_ON:
  158. adpa |= ADPA_DAC_ENABLE;
  159. break;
  160. case DRM_MODE_DPMS_STANDBY:
  161. adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
  162. break;
  163. case DRM_MODE_DPMS_SUSPEND:
  164. adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
  165. break;
  166. case DRM_MODE_DPMS_OFF:
  167. adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
  168. break;
  169. }
  170. I915_WRITE(crt->adpa_reg, adpa);
  171. }
  172. static void intel_disable_crt(struct intel_encoder *encoder)
  173. {
  174. intel_crt_set_dpms(encoder, DRM_MODE_DPMS_OFF);
  175. }
  176. static void hsw_crt_post_disable(struct intel_encoder *encoder)
  177. {
  178. struct drm_device *dev = encoder->base.dev;
  179. struct drm_i915_private *dev_priv = dev->dev_private;
  180. uint32_t val;
  181. DRM_DEBUG_KMS("Disabling SPLL\n");
  182. val = I915_READ(SPLL_CTL);
  183. WARN_ON(!(val & SPLL_PLL_ENABLE));
  184. I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
  185. POSTING_READ(SPLL_CTL);
  186. }
  187. static void intel_enable_crt(struct intel_encoder *encoder)
  188. {
  189. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  190. intel_crt_set_dpms(encoder, crt->connector->base.dpms);
  191. }
  192. /* Special dpms function to support cloning between dvo/sdvo/crt. */
  193. static void intel_crt_dpms(struct drm_connector *connector, int mode)
  194. {
  195. struct drm_device *dev = connector->dev;
  196. struct intel_encoder *encoder = intel_attached_encoder(connector);
  197. struct drm_crtc *crtc;
  198. int old_dpms;
  199. /* PCH platforms and VLV only support on/off. */
  200. if (INTEL_INFO(dev)->gen >= 5 && mode != DRM_MODE_DPMS_ON)
  201. mode = DRM_MODE_DPMS_OFF;
  202. if (mode == connector->dpms)
  203. return;
  204. old_dpms = connector->dpms;
  205. connector->dpms = mode;
  206. /* Only need to change hw state when actually enabled */
  207. crtc = encoder->base.crtc;
  208. if (!crtc) {
  209. encoder->connectors_active = false;
  210. return;
  211. }
  212. /* We need the pipe to run for anything but OFF. */
  213. if (mode == DRM_MODE_DPMS_OFF)
  214. encoder->connectors_active = false;
  215. else
  216. encoder->connectors_active = true;
  217. /* We call connector dpms manually below in case pipe dpms doesn't
  218. * change due to cloning. */
  219. if (mode < old_dpms) {
  220. /* From off to on, enable the pipe first. */
  221. intel_crtc_update_dpms(crtc);
  222. intel_crt_set_dpms(encoder, mode);
  223. } else {
  224. intel_crt_set_dpms(encoder, mode);
  225. intel_crtc_update_dpms(crtc);
  226. }
  227. intel_modeset_check_state(connector->dev);
  228. }
  229. static enum drm_mode_status
  230. intel_crt_mode_valid(struct drm_connector *connector,
  231. struct drm_display_mode *mode)
  232. {
  233. struct drm_device *dev = connector->dev;
  234. int max_clock = 0;
  235. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  236. return MODE_NO_DBLESCAN;
  237. if (mode->clock < 25000)
  238. return MODE_CLOCK_LOW;
  239. if (IS_GEN2(dev))
  240. max_clock = 350000;
  241. else
  242. max_clock = 400000;
  243. if (mode->clock > max_clock)
  244. return MODE_CLOCK_HIGH;
  245. /* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
  246. if (HAS_PCH_LPT(dev) &&
  247. (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
  248. return MODE_CLOCK_HIGH;
  249. return MODE_OK;
  250. }
  251. static bool intel_crt_compute_config(struct intel_encoder *encoder,
  252. struct intel_crtc_state *pipe_config)
  253. {
  254. struct drm_device *dev = encoder->base.dev;
  255. if (HAS_PCH_SPLIT(dev))
  256. pipe_config->has_pch_encoder = true;
  257. /* LPT FDI RX only supports 8bpc. */
  258. if (HAS_PCH_LPT(dev))
  259. pipe_config->pipe_bpp = 24;
  260. /* FDI must always be 2.7 GHz */
  261. if (HAS_DDI(dev)) {
  262. pipe_config->ddi_pll_sel = PORT_CLK_SEL_SPLL;
  263. pipe_config->port_clock = 135000 * 2;
  264. }
  265. return true;
  266. }
  267. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  268. {
  269. struct drm_device *dev = connector->dev;
  270. struct intel_crt *crt = intel_attached_crt(connector);
  271. struct drm_i915_private *dev_priv = dev->dev_private;
  272. u32 adpa;
  273. bool ret;
  274. /* The first time through, trigger an explicit detection cycle */
  275. if (crt->force_hotplug_required) {
  276. bool turn_off_dac = HAS_PCH_SPLIT(dev);
  277. u32 save_adpa;
  278. crt->force_hotplug_required = 0;
  279. save_adpa = adpa = I915_READ(crt->adpa_reg);
  280. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  281. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  282. if (turn_off_dac)
  283. adpa &= ~ADPA_DAC_ENABLE;
  284. I915_WRITE(crt->adpa_reg, adpa);
  285. if (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
  286. 1000))
  287. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  288. if (turn_off_dac) {
  289. I915_WRITE(crt->adpa_reg, save_adpa);
  290. POSTING_READ(crt->adpa_reg);
  291. }
  292. }
  293. /* Check the status to see if both blue and green are on now */
  294. adpa = I915_READ(crt->adpa_reg);
  295. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  296. ret = true;
  297. else
  298. ret = false;
  299. DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
  300. return ret;
  301. }
  302. static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
  303. {
  304. struct drm_device *dev = connector->dev;
  305. struct intel_crt *crt = intel_attached_crt(connector);
  306. struct drm_i915_private *dev_priv = dev->dev_private;
  307. u32 adpa;
  308. bool ret;
  309. u32 save_adpa;
  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 (wait_for((I915_READ(crt->adpa_reg) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
  315. 1000)) {
  316. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  317. I915_WRITE(crt->adpa_reg, save_adpa);
  318. }
  319. /* Check the status to see if both blue and green are on now */
  320. adpa = I915_READ(crt->adpa_reg);
  321. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  322. ret = true;
  323. else
  324. ret = false;
  325. DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
  326. return ret;
  327. }
  328. /**
  329. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  330. *
  331. * Not for i915G/i915GM
  332. *
  333. * \return true if CRT is connected.
  334. * \return false if CRT is disconnected.
  335. */
  336. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  337. {
  338. struct drm_device *dev = connector->dev;
  339. struct drm_i915_private *dev_priv = dev->dev_private;
  340. u32 hotplug_en, orig, stat;
  341. bool ret = false;
  342. int i, tries = 0;
  343. if (HAS_PCH_SPLIT(dev))
  344. return intel_ironlake_crt_detect_hotplug(connector);
  345. if (IS_VALLEYVIEW(dev))
  346. return valleyview_crt_detect_hotplug(connector);
  347. /*
  348. * On 4 series desktop, CRT detect sequence need to be done twice
  349. * to get a reliable result.
  350. */
  351. if (IS_G4X(dev) && !IS_GM45(dev))
  352. tries = 2;
  353. else
  354. tries = 1;
  355. hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
  356. hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
  357. for (i = 0; i < tries ; i++) {
  358. /* turn on the FORCE_DETECT */
  359. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  360. /* wait for FORCE_DETECT to go off */
  361. if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
  362. CRT_HOTPLUG_FORCE_DETECT) == 0,
  363. 1000))
  364. DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
  365. }
  366. stat = I915_READ(PORT_HOTPLUG_STAT);
  367. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  368. ret = true;
  369. /* clear the interrupt we just generated, if any */
  370. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  371. /* and put the bits back */
  372. I915_WRITE(PORT_HOTPLUG_EN, orig);
  373. return ret;
  374. }
  375. static struct edid *intel_crt_get_edid(struct drm_connector *connector,
  376. struct i2c_adapter *i2c)
  377. {
  378. struct edid *edid;
  379. edid = drm_get_edid(connector, i2c);
  380. if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
  381. DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
  382. intel_gmbus_force_bit(i2c, true);
  383. edid = drm_get_edid(connector, i2c);
  384. intel_gmbus_force_bit(i2c, false);
  385. }
  386. return edid;
  387. }
  388. /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
  389. static int intel_crt_ddc_get_modes(struct drm_connector *connector,
  390. struct i2c_adapter *adapter)
  391. {
  392. struct edid *edid;
  393. int ret;
  394. edid = intel_crt_get_edid(connector, adapter);
  395. if (!edid)
  396. return 0;
  397. ret = intel_connector_update_modes(connector, edid);
  398. kfree(edid);
  399. return ret;
  400. }
  401. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  402. {
  403. struct intel_crt *crt = intel_attached_crt(connector);
  404. struct drm_i915_private *dev_priv = crt->base.base.dev->dev_private;
  405. struct edid *edid;
  406. struct i2c_adapter *i2c;
  407. BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
  408. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  409. edid = intel_crt_get_edid(connector, i2c);
  410. if (edid) {
  411. bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
  412. /*
  413. * This may be a DVI-I connector with a shared DDC
  414. * link between analog and digital outputs, so we
  415. * have to check the EDID input spec of the attached device.
  416. */
  417. if (!is_digital) {
  418. DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
  419. return true;
  420. }
  421. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
  422. } else {
  423. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
  424. }
  425. kfree(edid);
  426. return false;
  427. }
  428. static enum drm_connector_status
  429. intel_crt_load_detect(struct intel_crt *crt)
  430. {
  431. struct drm_device *dev = crt->base.base.dev;
  432. struct drm_i915_private *dev_priv = dev->dev_private;
  433. uint32_t pipe = to_intel_crtc(crt->base.base.crtc)->pipe;
  434. uint32_t save_bclrpat;
  435. uint32_t save_vtotal;
  436. uint32_t vtotal, vactive;
  437. uint32_t vsample;
  438. uint32_t vblank, vblank_start, vblank_end;
  439. uint32_t dsl;
  440. uint32_t bclrpat_reg;
  441. uint32_t vtotal_reg;
  442. uint32_t vblank_reg;
  443. uint32_t vsync_reg;
  444. uint32_t pipeconf_reg;
  445. uint32_t 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)) {
  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, 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 enum drm_connector_status
  534. intel_crt_detect(struct drm_connector *connector, bool force)
  535. {
  536. struct drm_device *dev = connector->dev;
  537. struct drm_i915_private *dev_priv = dev->dev_private;
  538. struct intel_crt *crt = intel_attached_crt(connector);
  539. struct intel_encoder *intel_encoder = &crt->base;
  540. enum intel_display_power_domain power_domain;
  541. enum drm_connector_status status;
  542. struct intel_load_detect_pipe tmp;
  543. struct drm_modeset_acquire_ctx ctx;
  544. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  545. connector->base.id, connector->name,
  546. force);
  547. power_domain = intel_display_port_power_domain(intel_encoder);
  548. intel_display_power_get(dev_priv, power_domain);
  549. if (I915_HAS_HOTPLUG(dev)) {
  550. /* We can not rely on the HPD pin always being correctly wired
  551. * up, for example many KVM do not pass it through, and so
  552. * only trust an assertion that the monitor is connected.
  553. */
  554. if (intel_crt_detect_hotplug(connector)) {
  555. DRM_DEBUG_KMS("CRT detected via hotplug\n");
  556. status = connector_status_connected;
  557. goto out;
  558. } else
  559. DRM_DEBUG_KMS("CRT not detected via hotplug\n");
  560. }
  561. if (intel_crt_detect_ddc(connector)) {
  562. status = connector_status_connected;
  563. goto out;
  564. }
  565. /* Load detection is broken on HPD capable machines. Whoever wants a
  566. * broken monitor (without edid) to work behind a broken kvm (that fails
  567. * to have the right resistors for HP detection) needs to fix this up.
  568. * For now just bail out. */
  569. if (I915_HAS_HOTPLUG(dev) && !i915.load_detect_test) {
  570. status = connector_status_disconnected;
  571. goto out;
  572. }
  573. if (!force) {
  574. status = connector->status;
  575. goto out;
  576. }
  577. drm_modeset_acquire_init(&ctx, 0);
  578. /* for pre-945g platforms use load detect */
  579. if (intel_get_load_detect_pipe(connector, NULL, &tmp, &ctx)) {
  580. if (intel_crt_detect_ddc(connector))
  581. status = connector_status_connected;
  582. else if (INTEL_INFO(dev)->gen < 4)
  583. status = intel_crt_load_detect(crt);
  584. else
  585. status = connector_status_unknown;
  586. intel_release_load_detect_pipe(connector, &tmp, &ctx);
  587. } else
  588. status = connector_status_unknown;
  589. drm_modeset_drop_locks(&ctx);
  590. drm_modeset_acquire_fini(&ctx);
  591. out:
  592. intel_display_power_put(dev_priv, power_domain);
  593. return status;
  594. }
  595. static void intel_crt_destroy(struct drm_connector *connector)
  596. {
  597. drm_connector_cleanup(connector);
  598. kfree(connector);
  599. }
  600. static int intel_crt_get_modes(struct drm_connector *connector)
  601. {
  602. struct drm_device *dev = connector->dev;
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. struct intel_crt *crt = intel_attached_crt(connector);
  605. struct intel_encoder *intel_encoder = &crt->base;
  606. enum intel_display_power_domain power_domain;
  607. int ret;
  608. struct i2c_adapter *i2c;
  609. power_domain = intel_display_port_power_domain(intel_encoder);
  610. intel_display_power_get(dev_priv, power_domain);
  611. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  612. ret = intel_crt_ddc_get_modes(connector, i2c);
  613. if (ret || !IS_G4X(dev))
  614. goto out;
  615. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  616. i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PORT_DPB);
  617. ret = intel_crt_ddc_get_modes(connector, i2c);
  618. out:
  619. intel_display_power_put(dev_priv, power_domain);
  620. return ret;
  621. }
  622. static int intel_crt_set_property(struct drm_connector *connector,
  623. struct drm_property *property,
  624. uint64_t value)
  625. {
  626. return 0;
  627. }
  628. static void intel_crt_reset(struct drm_connector *connector)
  629. {
  630. struct drm_device *dev = connector->dev;
  631. struct drm_i915_private *dev_priv = dev->dev_private;
  632. struct intel_crt *crt = intel_attached_crt(connector);
  633. if (INTEL_INFO(dev)->gen >= 5) {
  634. u32 adpa;
  635. adpa = I915_READ(crt->adpa_reg);
  636. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  637. adpa |= ADPA_HOTPLUG_BITS;
  638. I915_WRITE(crt->adpa_reg, adpa);
  639. POSTING_READ(crt->adpa_reg);
  640. DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
  641. crt->force_hotplug_required = 1;
  642. }
  643. }
  644. /*
  645. * Routines for controlling stuff on the analog port
  646. */
  647. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  648. .reset = intel_crt_reset,
  649. .dpms = intel_crt_dpms,
  650. .detect = intel_crt_detect,
  651. .fill_modes = drm_helper_probe_single_connector_modes,
  652. .destroy = intel_crt_destroy,
  653. .set_property = intel_crt_set_property,
  654. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  655. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  656. .atomic_get_property = intel_connector_atomic_get_property,
  657. };
  658. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  659. .mode_valid = intel_crt_mode_valid,
  660. .get_modes = intel_crt_get_modes,
  661. .best_encoder = intel_best_encoder,
  662. };
  663. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  664. .destroy = intel_encoder_destroy,
  665. };
  666. static int intel_no_crt_dmi_callback(const struct dmi_system_id *id)
  667. {
  668. DRM_INFO("Skipping CRT initialization for %s\n", id->ident);
  669. return 1;
  670. }
  671. static const struct dmi_system_id intel_no_crt[] = {
  672. {
  673. .callback = intel_no_crt_dmi_callback,
  674. .ident = "ACER ZGB",
  675. .matches = {
  676. DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
  677. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  678. },
  679. },
  680. {
  681. .callback = intel_no_crt_dmi_callback,
  682. .ident = "DELL XPS 8700",
  683. .matches = {
  684. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  685. DMI_MATCH(DMI_PRODUCT_NAME, "XPS 8700"),
  686. },
  687. },
  688. { }
  689. };
  690. void intel_crt_init(struct drm_device *dev)
  691. {
  692. struct drm_connector *connector;
  693. struct intel_crt *crt;
  694. struct intel_connector *intel_connector;
  695. struct drm_i915_private *dev_priv = dev->dev_private;
  696. /* Skip machines without VGA that falsely report hotplug events */
  697. if (dmi_check_system(intel_no_crt))
  698. return;
  699. crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
  700. if (!crt)
  701. return;
  702. intel_connector = intel_connector_alloc();
  703. if (!intel_connector) {
  704. kfree(crt);
  705. return;
  706. }
  707. connector = &intel_connector->base;
  708. crt->connector = intel_connector;
  709. drm_connector_init(dev, &intel_connector->base,
  710. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  711. drm_encoder_init(dev, &crt->base.base, &intel_crt_enc_funcs,
  712. DRM_MODE_ENCODER_DAC);
  713. intel_connector_attach_encoder(intel_connector, &crt->base);
  714. crt->base.type = INTEL_OUTPUT_ANALOG;
  715. crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
  716. if (IS_I830(dev))
  717. crt->base.crtc_mask = (1 << 0);
  718. else
  719. crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  720. if (IS_GEN2(dev))
  721. connector->interlace_allowed = 0;
  722. else
  723. connector->interlace_allowed = 1;
  724. connector->doublescan_allowed = 0;
  725. if (HAS_PCH_SPLIT(dev))
  726. crt->adpa_reg = PCH_ADPA;
  727. else if (IS_VALLEYVIEW(dev))
  728. crt->adpa_reg = VLV_ADPA;
  729. else
  730. crt->adpa_reg = ADPA;
  731. crt->base.compute_config = intel_crt_compute_config;
  732. crt->base.disable = intel_disable_crt;
  733. crt->base.enable = intel_enable_crt;
  734. if (I915_HAS_HOTPLUG(dev))
  735. crt->base.hpd_pin = HPD_CRT;
  736. if (HAS_DDI(dev)) {
  737. crt->base.get_config = hsw_crt_get_config;
  738. crt->base.get_hw_state = intel_ddi_get_hw_state;
  739. crt->base.pre_enable = hsw_crt_pre_enable;
  740. crt->base.post_disable = hsw_crt_post_disable;
  741. } else {
  742. crt->base.get_config = intel_crt_get_config;
  743. crt->base.get_hw_state = intel_crt_get_hw_state;
  744. }
  745. intel_connector->get_hw_state = intel_connector_get_hw_state;
  746. intel_connector->unregister = intel_connector_unregister;
  747. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  748. drm_connector_register(connector);
  749. if (!I915_HAS_HOTPLUG(dev))
  750. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  751. /*
  752. * Configure the automatic hotplug detection stuff
  753. */
  754. crt->force_hotplug_required = 0;
  755. /*
  756. * TODO: find a proper way to discover whether we need to set the the
  757. * polarity and link reversal bits or not, instead of relying on the
  758. * BIOS.
  759. */
  760. if (HAS_PCH_LPT(dev)) {
  761. u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
  762. FDI_RX_LINK_REVERSAL_OVERRIDE;
  763. dev_priv->fdi_rx_config = I915_READ(_FDI_RXA_CTL) & fdi_config;
  764. }
  765. intel_crt_reset(connector);
  766. }