intel_crt.c 22 KB

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