intel_crt.c 23 KB

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