intel_crt.c 23 KB

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