intel_crt.c 22 KB

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