intel_crt.c 22 KB

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