intel_crt.c 25 KB

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