intel_crt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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_atomic_helper.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_edid.h>
  34. #include "intel_drv.h"
  35. #include <drm/i915_drm.h>
  36. #include "i915_drv.h"
  37. /* Here's the desired hotplug mode */
  38. #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 | \
  39. ADPA_CRT_HOTPLUG_WARMUP_10MS | \
  40. ADPA_CRT_HOTPLUG_SAMPLE_4S | \
  41. ADPA_CRT_HOTPLUG_VOLTAGE_50 | \
  42. ADPA_CRT_HOTPLUG_VOLREF_325MV | \
  43. ADPA_CRT_HOTPLUG_ENABLE)
  44. struct intel_crt {
  45. struct intel_encoder base;
  46. /* DPMS state is stored in the connector, which we need in the
  47. * encoder's enable/disable callbacks */
  48. struct intel_connector *connector;
  49. bool force_hotplug_required;
  50. i915_reg_t adpa_reg;
  51. };
  52. static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
  53. {
  54. return container_of(encoder, struct intel_crt, base);
  55. }
  56. static struct intel_crt *intel_attached_crt(struct drm_connector *connector)
  57. {
  58. return intel_encoder_to_crt(intel_attached_encoder(connector));
  59. }
  60. static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
  61. enum pipe *pipe)
  62. {
  63. struct drm_device *dev = encoder->base.dev;
  64. struct drm_i915_private *dev_priv = to_i915(dev);
  65. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  66. u32 tmp;
  67. bool ret;
  68. if (!intel_display_power_get_if_enabled(dev_priv,
  69. encoder->power_domain))
  70. return false;
  71. ret = false;
  72. tmp = I915_READ(crt->adpa_reg);
  73. if (!(tmp & ADPA_DAC_ENABLE))
  74. goto out;
  75. if (HAS_PCH_CPT(dev_priv))
  76. *pipe = PORT_TO_PIPE_CPT(tmp);
  77. else
  78. *pipe = PORT_TO_PIPE(tmp);
  79. ret = true;
  80. out:
  81. intel_display_power_put(dev_priv, encoder->power_domain);
  82. return ret;
  83. }
  84. static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
  85. {
  86. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  87. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  88. u32 tmp, flags = 0;
  89. tmp = I915_READ(crt->adpa_reg);
  90. if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
  91. flags |= DRM_MODE_FLAG_PHSYNC;
  92. else
  93. flags |= DRM_MODE_FLAG_NHSYNC;
  94. if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
  95. flags |= DRM_MODE_FLAG_PVSYNC;
  96. else
  97. flags |= DRM_MODE_FLAG_NVSYNC;
  98. return flags;
  99. }
  100. static void intel_crt_get_config(struct intel_encoder *encoder,
  101. struct intel_crtc_state *pipe_config)
  102. {
  103. pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
  104. pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
  105. pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
  106. }
  107. static void hsw_crt_get_config(struct intel_encoder *encoder,
  108. struct intel_crtc_state *pipe_config)
  109. {
  110. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  111. intel_ddi_get_config(encoder, pipe_config);
  112. pipe_config->base.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
  113. DRM_MODE_FLAG_NHSYNC |
  114. DRM_MODE_FLAG_PVSYNC |
  115. DRM_MODE_FLAG_NVSYNC);
  116. pipe_config->base.adjusted_mode.flags |= intel_crt_get_flags(encoder);
  117. pipe_config->base.adjusted_mode.crtc_clock = lpt_get_iclkip(dev_priv);
  118. }
  119. /* Note: The caller is required to filter out dpms modes not supported by the
  120. * platform. */
  121. static void intel_crt_set_dpms(struct intel_encoder *encoder,
  122. const struct intel_crtc_state *crtc_state,
  123. int mode)
  124. {
  125. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  126. struct intel_crt *crt = intel_encoder_to_crt(encoder);
  127. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  128. const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
  129. u32 adpa;
  130. if (INTEL_GEN(dev_priv) >= 5)
  131. adpa = ADPA_HOTPLUG_BITS;
  132. else
  133. adpa = 0;
  134. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  135. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  136. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  137. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  138. /* For CPT allow 3 pipe config, for others just use A or B */
  139. if (HAS_PCH_LPT(dev_priv))
  140. ; /* Those bits don't exist here */
  141. else if (HAS_PCH_CPT(dev_priv))
  142. adpa |= PORT_TRANS_SEL_CPT(crtc->pipe);
  143. else if (crtc->pipe == 0)
  144. adpa |= ADPA_PIPE_A_SELECT;
  145. else
  146. adpa |= ADPA_PIPE_B_SELECT;
  147. if (!HAS_PCH_SPLIT(dev_priv))
  148. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  149. switch (mode) {
  150. case DRM_MODE_DPMS_ON:
  151. adpa |= ADPA_DAC_ENABLE;
  152. break;
  153. case DRM_MODE_DPMS_STANDBY:
  154. adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
  155. break;
  156. case DRM_MODE_DPMS_SUSPEND:
  157. adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
  158. break;
  159. case DRM_MODE_DPMS_OFF:
  160. adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
  161. break;
  162. }
  163. I915_WRITE(crt->adpa_reg, adpa);
  164. }
  165. static void intel_disable_crt(struct intel_encoder *encoder,
  166. const struct intel_crtc_state *old_crtc_state,
  167. const struct drm_connector_state *old_conn_state)
  168. {
  169. intel_crt_set_dpms(encoder, old_crtc_state, DRM_MODE_DPMS_OFF);
  170. }
  171. static void pch_disable_crt(struct intel_encoder *encoder,
  172. const struct intel_crtc_state *old_crtc_state,
  173. const struct drm_connector_state *old_conn_state)
  174. {
  175. }
  176. static void pch_post_disable_crt(struct intel_encoder *encoder,
  177. const struct intel_crtc_state *old_crtc_state,
  178. const struct drm_connector_state *old_conn_state)
  179. {
  180. intel_disable_crt(encoder, old_crtc_state, old_conn_state);
  181. }
  182. static void hsw_disable_crt(struct intel_encoder *encoder,
  183. const struct intel_crtc_state *old_crtc_state,
  184. const struct drm_connector_state *old_conn_state)
  185. {
  186. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  187. WARN_ON(!old_crtc_state->has_pch_encoder);
  188. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  189. }
  190. static void hsw_post_disable_crt(struct intel_encoder *encoder,
  191. const struct intel_crtc_state *old_crtc_state,
  192. const struct drm_connector_state *old_conn_state)
  193. {
  194. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  195. pch_post_disable_crt(encoder, old_crtc_state, old_conn_state);
  196. lpt_disable_pch_transcoder(dev_priv);
  197. lpt_disable_iclkip(dev_priv);
  198. intel_ddi_fdi_post_disable(encoder, old_crtc_state, old_conn_state);
  199. WARN_ON(!old_crtc_state->has_pch_encoder);
  200. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  201. }
  202. static void hsw_pre_pll_enable_crt(struct intel_encoder *encoder,
  203. const struct intel_crtc_state *crtc_state,
  204. const struct drm_connector_state *conn_state)
  205. {
  206. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  207. WARN_ON(!crtc_state->has_pch_encoder);
  208. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
  209. }
  210. static void hsw_pre_enable_crt(struct intel_encoder *encoder,
  211. const struct intel_crtc_state *crtc_state,
  212. const struct drm_connector_state *conn_state)
  213. {
  214. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  215. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  216. enum pipe pipe = crtc->pipe;
  217. WARN_ON(!crtc_state->has_pch_encoder);
  218. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  219. dev_priv->display.fdi_link_train(crtc, crtc_state);
  220. }
  221. static void hsw_enable_crt(struct intel_encoder *encoder,
  222. const struct intel_crtc_state *crtc_state,
  223. const struct drm_connector_state *conn_state)
  224. {
  225. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  226. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  227. enum pipe pipe = crtc->pipe;
  228. WARN_ON(!crtc_state->has_pch_encoder);
  229. intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
  230. intel_wait_for_vblank(dev_priv, pipe);
  231. intel_wait_for_vblank(dev_priv, pipe);
  232. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  233. intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
  234. }
  235. static void intel_enable_crt(struct intel_encoder *encoder,
  236. const struct intel_crtc_state *crtc_state,
  237. const struct drm_connector_state *conn_state)
  238. {
  239. intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
  240. }
  241. static enum drm_mode_status
  242. intel_crt_mode_valid(struct drm_connector *connector,
  243. struct drm_display_mode *mode)
  244. {
  245. struct drm_device *dev = connector->dev;
  246. struct drm_i915_private *dev_priv = to_i915(dev);
  247. int max_dotclk = dev_priv->max_dotclk_freq;
  248. int max_clock;
  249. if (mode->clock < 25000)
  250. return MODE_CLOCK_LOW;
  251. if (HAS_PCH_LPT(dev_priv))
  252. max_clock = 180000;
  253. else if (IS_VALLEYVIEW(dev_priv))
  254. /*
  255. * 270 MHz due to current DPLL limits,
  256. * DAC limit supposedly 355 MHz.
  257. */
  258. max_clock = 270000;
  259. else if (IS_GEN3(dev_priv) || IS_GEN4(dev_priv))
  260. max_clock = 400000;
  261. else
  262. max_clock = 350000;
  263. if (mode->clock > max_clock)
  264. return MODE_CLOCK_HIGH;
  265. if (mode->clock > max_dotclk)
  266. return MODE_CLOCK_HIGH;
  267. /* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
  268. if (HAS_PCH_LPT(dev_priv) &&
  269. (ironlake_get_lanes_required(mode->clock, 270000, 24) > 2))
  270. return MODE_CLOCK_HIGH;
  271. return MODE_OK;
  272. }
  273. static bool intel_crt_compute_config(struct intel_encoder *encoder,
  274. struct intel_crtc_state *pipe_config,
  275. struct drm_connector_state *conn_state)
  276. {
  277. return true;
  278. }
  279. static bool pch_crt_compute_config(struct intel_encoder *encoder,
  280. struct intel_crtc_state *pipe_config,
  281. struct drm_connector_state *conn_state)
  282. {
  283. pipe_config->has_pch_encoder = true;
  284. return true;
  285. }
  286. static bool hsw_crt_compute_config(struct intel_encoder *encoder,
  287. struct intel_crtc_state *pipe_config,
  288. struct drm_connector_state *conn_state)
  289. {
  290. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  291. pipe_config->has_pch_encoder = true;
  292. /* LPT FDI RX only supports 8bpc. */
  293. if (HAS_PCH_LPT(dev_priv)) {
  294. if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
  295. DRM_DEBUG_KMS("LPT only supports 24bpp\n");
  296. return false;
  297. }
  298. pipe_config->pipe_bpp = 24;
  299. }
  300. /* FDI must always be 2.7 GHz */
  301. pipe_config->port_clock = 135000 * 2;
  302. return true;
  303. }
  304. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  305. {
  306. struct drm_device *dev = connector->dev;
  307. struct intel_crt *crt = intel_attached_crt(connector);
  308. struct drm_i915_private *dev_priv = to_i915(dev);
  309. u32 adpa;
  310. bool ret;
  311. /* The first time through, trigger an explicit detection cycle */
  312. if (crt->force_hotplug_required) {
  313. bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
  314. u32 save_adpa;
  315. crt->force_hotplug_required = 0;
  316. save_adpa = adpa = I915_READ(crt->adpa_reg);
  317. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  318. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  319. if (turn_off_dac)
  320. adpa &= ~ADPA_DAC_ENABLE;
  321. I915_WRITE(crt->adpa_reg, adpa);
  322. if (intel_wait_for_register(dev_priv,
  323. crt->adpa_reg,
  324. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  325. 1000))
  326. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  327. if (turn_off_dac) {
  328. I915_WRITE(crt->adpa_reg, save_adpa);
  329. POSTING_READ(crt->adpa_reg);
  330. }
  331. }
  332. /* Check the status to see if both blue and green are on now */
  333. adpa = I915_READ(crt->adpa_reg);
  334. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  335. ret = true;
  336. else
  337. ret = false;
  338. DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
  339. return ret;
  340. }
  341. static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
  342. {
  343. struct drm_device *dev = connector->dev;
  344. struct intel_crt *crt = intel_attached_crt(connector);
  345. struct drm_i915_private *dev_priv = to_i915(dev);
  346. bool reenable_hpd;
  347. u32 adpa;
  348. bool ret;
  349. u32 save_adpa;
  350. /*
  351. * Doing a force trigger causes a hpd interrupt to get sent, which can
  352. * get us stuck in a loop if we're polling:
  353. * - We enable power wells and reset the ADPA
  354. * - output_poll_exec does force probe on VGA, triggering a hpd
  355. * - HPD handler waits for poll to unlock dev->mode_config.mutex
  356. * - output_poll_exec shuts off the ADPA, unlocks
  357. * dev->mode_config.mutex
  358. * - HPD handler runs, resets ADPA and brings us back to the start
  359. *
  360. * Just disable HPD interrupts here to prevent this
  361. */
  362. reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
  363. save_adpa = adpa = I915_READ(crt->adpa_reg);
  364. DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
  365. adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
  366. I915_WRITE(crt->adpa_reg, adpa);
  367. if (intel_wait_for_register(dev_priv,
  368. crt->adpa_reg,
  369. ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 0,
  370. 1000)) {
  371. DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
  372. I915_WRITE(crt->adpa_reg, save_adpa);
  373. }
  374. /* Check the status to see if both blue and green are on now */
  375. adpa = I915_READ(crt->adpa_reg);
  376. if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
  377. ret = true;
  378. else
  379. ret = false;
  380. DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
  381. if (reenable_hpd)
  382. intel_hpd_enable(dev_priv, crt->base.hpd_pin);
  383. return ret;
  384. }
  385. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  386. {
  387. struct drm_device *dev = connector->dev;
  388. struct drm_i915_private *dev_priv = to_i915(dev);
  389. u32 stat;
  390. bool ret = false;
  391. int i, tries = 0;
  392. if (HAS_PCH_SPLIT(dev_priv))
  393. return intel_ironlake_crt_detect_hotplug(connector);
  394. if (IS_VALLEYVIEW(dev_priv))
  395. return valleyview_crt_detect_hotplug(connector);
  396. /*
  397. * On 4 series desktop, CRT detect sequence need to be done twice
  398. * to get a reliable result.
  399. */
  400. if (IS_G4X(dev_priv) && !IS_GM45(dev_priv))
  401. tries = 2;
  402. else
  403. tries = 1;
  404. for (i = 0; i < tries ; i++) {
  405. /* turn on the FORCE_DETECT */
  406. i915_hotplug_interrupt_update(dev_priv,
  407. CRT_HOTPLUG_FORCE_DETECT,
  408. CRT_HOTPLUG_FORCE_DETECT);
  409. /* wait for FORCE_DETECT to go off */
  410. if (intel_wait_for_register(dev_priv, PORT_HOTPLUG_EN,
  411. CRT_HOTPLUG_FORCE_DETECT, 0,
  412. 1000))
  413. DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
  414. }
  415. stat = I915_READ(PORT_HOTPLUG_STAT);
  416. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  417. ret = true;
  418. /* clear the interrupt we just generated, if any */
  419. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  420. i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
  421. return ret;
  422. }
  423. static struct edid *intel_crt_get_edid(struct drm_connector *connector,
  424. struct i2c_adapter *i2c)
  425. {
  426. struct edid *edid;
  427. edid = drm_get_edid(connector, i2c);
  428. if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
  429. DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
  430. intel_gmbus_force_bit(i2c, true);
  431. edid = drm_get_edid(connector, i2c);
  432. intel_gmbus_force_bit(i2c, false);
  433. }
  434. return edid;
  435. }
  436. /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
  437. static int intel_crt_ddc_get_modes(struct drm_connector *connector,
  438. struct i2c_adapter *adapter)
  439. {
  440. struct edid *edid;
  441. int ret;
  442. edid = intel_crt_get_edid(connector, adapter);
  443. if (!edid)
  444. return 0;
  445. ret = intel_connector_update_modes(connector, edid);
  446. kfree(edid);
  447. return ret;
  448. }
  449. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  450. {
  451. struct intel_crt *crt = intel_attached_crt(connector);
  452. struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
  453. struct edid *edid;
  454. struct i2c_adapter *i2c;
  455. bool ret = false;
  456. BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
  457. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  458. edid = intel_crt_get_edid(connector, i2c);
  459. if (edid) {
  460. bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
  461. /*
  462. * This may be a DVI-I connector with a shared DDC
  463. * link between analog and digital outputs, so we
  464. * have to check the EDID input spec of the attached device.
  465. */
  466. if (!is_digital) {
  467. DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
  468. ret = true;
  469. } else {
  470. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
  471. }
  472. } else {
  473. DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
  474. }
  475. kfree(edid);
  476. return ret;
  477. }
  478. static enum drm_connector_status
  479. intel_crt_load_detect(struct intel_crt *crt, uint32_t pipe)
  480. {
  481. struct drm_device *dev = crt->base.base.dev;
  482. struct drm_i915_private *dev_priv = to_i915(dev);
  483. uint32_t save_bclrpat;
  484. uint32_t save_vtotal;
  485. uint32_t vtotal, vactive;
  486. uint32_t vsample;
  487. uint32_t vblank, vblank_start, vblank_end;
  488. uint32_t dsl;
  489. i915_reg_t bclrpat_reg, vtotal_reg,
  490. vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
  491. uint8_t st00;
  492. enum drm_connector_status status;
  493. DRM_DEBUG_KMS("starting load-detect on CRT\n");
  494. bclrpat_reg = BCLRPAT(pipe);
  495. vtotal_reg = VTOTAL(pipe);
  496. vblank_reg = VBLANK(pipe);
  497. vsync_reg = VSYNC(pipe);
  498. pipeconf_reg = PIPECONF(pipe);
  499. pipe_dsl_reg = PIPEDSL(pipe);
  500. save_bclrpat = I915_READ(bclrpat_reg);
  501. save_vtotal = I915_READ(vtotal_reg);
  502. vblank = I915_READ(vblank_reg);
  503. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  504. vactive = (save_vtotal & 0x7ff) + 1;
  505. vblank_start = (vblank & 0xfff) + 1;
  506. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  507. /* Set the border color to purple. */
  508. I915_WRITE(bclrpat_reg, 0x500050);
  509. if (!IS_GEN2(dev_priv)) {
  510. uint32_t pipeconf = I915_READ(pipeconf_reg);
  511. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  512. POSTING_READ(pipeconf_reg);
  513. /* Wait for next Vblank to substitue
  514. * border color for Color info */
  515. intel_wait_for_vblank(dev_priv, pipe);
  516. st00 = I915_READ8(_VGA_MSR_WRITE);
  517. status = ((st00 & (1 << 4)) != 0) ?
  518. connector_status_connected :
  519. connector_status_disconnected;
  520. I915_WRITE(pipeconf_reg, pipeconf);
  521. } else {
  522. bool restore_vblank = false;
  523. int count, detect;
  524. /*
  525. * If there isn't any border, add some.
  526. * Yes, this will flicker
  527. */
  528. if (vblank_start <= vactive && vblank_end >= vtotal) {
  529. uint32_t vsync = I915_READ(vsync_reg);
  530. uint32_t vsync_start = (vsync & 0xffff) + 1;
  531. vblank_start = vsync_start;
  532. I915_WRITE(vblank_reg,
  533. (vblank_start - 1) |
  534. ((vblank_end - 1) << 16));
  535. restore_vblank = true;
  536. }
  537. /* sample in the vertical border, selecting the larger one */
  538. if (vblank_start - vactive >= vtotal - vblank_end)
  539. vsample = (vblank_start + vactive) >> 1;
  540. else
  541. vsample = (vtotal + vblank_end) >> 1;
  542. /*
  543. * Wait for the border to be displayed
  544. */
  545. while (I915_READ(pipe_dsl_reg) >= vactive)
  546. ;
  547. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  548. ;
  549. /*
  550. * Watch ST00 for an entire scanline
  551. */
  552. detect = 0;
  553. count = 0;
  554. do {
  555. count++;
  556. /* Read the ST00 VGA status register */
  557. st00 = I915_READ8(_VGA_MSR_WRITE);
  558. if (st00 & (1 << 4))
  559. detect++;
  560. } while ((I915_READ(pipe_dsl_reg) == dsl));
  561. /* restore vblank if necessary */
  562. if (restore_vblank)
  563. I915_WRITE(vblank_reg, vblank);
  564. /*
  565. * If more than 3/4 of the scanline detected a monitor,
  566. * then it is assumed to be present. This works even on i830,
  567. * where there isn't any way to force the border color across
  568. * the screen
  569. */
  570. status = detect * 4 > count * 3 ?
  571. connector_status_connected :
  572. connector_status_disconnected;
  573. }
  574. /* Restore previous settings */
  575. I915_WRITE(bclrpat_reg, save_bclrpat);
  576. return status;
  577. }
  578. static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
  579. {
  580. DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
  581. return 1;
  582. }
  583. static const struct dmi_system_id intel_spurious_crt_detect[] = {
  584. {
  585. .callback = intel_spurious_crt_detect_dmi_callback,
  586. .ident = "ACER ZGB",
  587. .matches = {
  588. DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
  589. DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
  590. },
  591. },
  592. {
  593. .callback = intel_spurious_crt_detect_dmi_callback,
  594. .ident = "Intel DZ77BH-55K",
  595. .matches = {
  596. DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
  597. DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
  598. },
  599. },
  600. { }
  601. };
  602. static int
  603. intel_crt_detect(struct drm_connector *connector,
  604. struct drm_modeset_acquire_ctx *ctx,
  605. bool force)
  606. {
  607. struct drm_i915_private *dev_priv = to_i915(connector->dev);
  608. struct intel_crt *crt = intel_attached_crt(connector);
  609. struct intel_encoder *intel_encoder = &crt->base;
  610. int status, ret;
  611. struct intel_load_detect_pipe tmp;
  612. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
  613. connector->base.id, connector->name,
  614. force);
  615. if (i915_modparams.load_detect_test) {
  616. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  617. goto load_detect;
  618. }
  619. /* Skip machines without VGA that falsely report hotplug events */
  620. if (dmi_check_system(intel_spurious_crt_detect))
  621. return connector_status_disconnected;
  622. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  623. if (I915_HAS_HOTPLUG(dev_priv)) {
  624. /* We can not rely on the HPD pin always being correctly wired
  625. * up, for example many KVM do not pass it through, and so
  626. * only trust an assertion that the monitor is connected.
  627. */
  628. if (intel_crt_detect_hotplug(connector)) {
  629. DRM_DEBUG_KMS("CRT detected via hotplug\n");
  630. status = connector_status_connected;
  631. goto out;
  632. } else
  633. DRM_DEBUG_KMS("CRT not detected via hotplug\n");
  634. }
  635. if (intel_crt_detect_ddc(connector)) {
  636. status = connector_status_connected;
  637. goto out;
  638. }
  639. /* Load detection is broken on HPD capable machines. Whoever wants a
  640. * broken monitor (without edid) to work behind a broken kvm (that fails
  641. * to have the right resistors for HP detection) needs to fix this up.
  642. * For now just bail out. */
  643. if (I915_HAS_HOTPLUG(dev_priv)) {
  644. status = connector_status_disconnected;
  645. goto out;
  646. }
  647. load_detect:
  648. if (!force) {
  649. status = connector->status;
  650. goto out;
  651. }
  652. /* for pre-945g platforms use load detect */
  653. ret = intel_get_load_detect_pipe(connector, NULL, &tmp, ctx);
  654. if (ret > 0) {
  655. if (intel_crt_detect_ddc(connector))
  656. status = connector_status_connected;
  657. else if (INTEL_GEN(dev_priv) < 4)
  658. status = intel_crt_load_detect(crt,
  659. to_intel_crtc(connector->state->crtc)->pipe);
  660. else if (i915_modparams.load_detect_test)
  661. status = connector_status_disconnected;
  662. else
  663. status = connector_status_unknown;
  664. intel_release_load_detect_pipe(connector, &tmp, ctx);
  665. } else if (ret == 0) {
  666. status = connector_status_unknown;
  667. } else {
  668. status = ret;
  669. }
  670. out:
  671. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  672. return status;
  673. }
  674. static void intel_crt_destroy(struct drm_connector *connector)
  675. {
  676. drm_connector_cleanup(connector);
  677. kfree(connector);
  678. }
  679. static int intel_crt_get_modes(struct drm_connector *connector)
  680. {
  681. struct drm_device *dev = connector->dev;
  682. struct drm_i915_private *dev_priv = to_i915(dev);
  683. struct intel_crt *crt = intel_attached_crt(connector);
  684. struct intel_encoder *intel_encoder = &crt->base;
  685. int ret;
  686. struct i2c_adapter *i2c;
  687. intel_display_power_get(dev_priv, intel_encoder->power_domain);
  688. i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
  689. ret = intel_crt_ddc_get_modes(connector, i2c);
  690. if (ret || !IS_G4X(dev_priv))
  691. goto out;
  692. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  693. i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
  694. ret = intel_crt_ddc_get_modes(connector, i2c);
  695. out:
  696. intel_display_power_put(dev_priv, intel_encoder->power_domain);
  697. return ret;
  698. }
  699. void intel_crt_reset(struct drm_encoder *encoder)
  700. {
  701. struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  702. struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
  703. if (INTEL_GEN(dev_priv) >= 5) {
  704. u32 adpa;
  705. adpa = I915_READ(crt->adpa_reg);
  706. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  707. adpa |= ADPA_HOTPLUG_BITS;
  708. I915_WRITE(crt->adpa_reg, adpa);
  709. POSTING_READ(crt->adpa_reg);
  710. DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
  711. crt->force_hotplug_required = 1;
  712. }
  713. }
  714. /*
  715. * Routines for controlling stuff on the analog port
  716. */
  717. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  718. .fill_modes = drm_helper_probe_single_connector_modes,
  719. .late_register = intel_connector_register,
  720. .early_unregister = intel_connector_unregister,
  721. .destroy = intel_crt_destroy,
  722. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  723. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  724. };
  725. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  726. .detect_ctx = intel_crt_detect,
  727. .mode_valid = intel_crt_mode_valid,
  728. .get_modes = intel_crt_get_modes,
  729. };
  730. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  731. .reset = intel_crt_reset,
  732. .destroy = intel_encoder_destroy,
  733. };
  734. void intel_crt_init(struct drm_i915_private *dev_priv)
  735. {
  736. struct drm_connector *connector;
  737. struct intel_crt *crt;
  738. struct intel_connector *intel_connector;
  739. i915_reg_t adpa_reg;
  740. u32 adpa;
  741. if (HAS_PCH_SPLIT(dev_priv))
  742. adpa_reg = PCH_ADPA;
  743. else if (IS_VALLEYVIEW(dev_priv))
  744. adpa_reg = VLV_ADPA;
  745. else
  746. adpa_reg = ADPA;
  747. adpa = I915_READ(adpa_reg);
  748. if ((adpa & ADPA_DAC_ENABLE) == 0) {
  749. /*
  750. * On some machines (some IVB at least) CRT can be
  751. * fused off, but there's no known fuse bit to
  752. * indicate that. On these machine the ADPA register
  753. * works normally, except the DAC enable bit won't
  754. * take. So the only way to tell is attempt to enable
  755. * it and see what happens.
  756. */
  757. I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
  758. ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
  759. if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
  760. return;
  761. I915_WRITE(adpa_reg, adpa);
  762. }
  763. crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
  764. if (!crt)
  765. return;
  766. intel_connector = intel_connector_alloc();
  767. if (!intel_connector) {
  768. kfree(crt);
  769. return;
  770. }
  771. connector = &intel_connector->base;
  772. crt->connector = intel_connector;
  773. drm_connector_init(&dev_priv->drm, &intel_connector->base,
  774. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  775. drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
  776. DRM_MODE_ENCODER_DAC, "CRT");
  777. intel_connector_attach_encoder(intel_connector, &crt->base);
  778. crt->base.type = INTEL_OUTPUT_ANALOG;
  779. crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
  780. if (IS_I830(dev_priv))
  781. crt->base.crtc_mask = (1 << 0);
  782. else
  783. crt->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  784. if (IS_GEN2(dev_priv))
  785. connector->interlace_allowed = 0;
  786. else
  787. connector->interlace_allowed = 1;
  788. connector->doublescan_allowed = 0;
  789. crt->adpa_reg = adpa_reg;
  790. crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
  791. if (I915_HAS_HOTPLUG(dev_priv) &&
  792. !dmi_check_system(intel_spurious_crt_detect)) {
  793. crt->base.hpd_pin = HPD_CRT;
  794. crt->base.hotplug = intel_encoder_hotplug;
  795. }
  796. if (HAS_DDI(dev_priv)) {
  797. crt->base.port = PORT_E;
  798. crt->base.get_config = hsw_crt_get_config;
  799. crt->base.get_hw_state = intel_ddi_get_hw_state;
  800. crt->base.compute_config = hsw_crt_compute_config;
  801. crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
  802. crt->base.pre_enable = hsw_pre_enable_crt;
  803. crt->base.enable = hsw_enable_crt;
  804. crt->base.disable = hsw_disable_crt;
  805. crt->base.post_disable = hsw_post_disable_crt;
  806. } else {
  807. if (HAS_PCH_SPLIT(dev_priv)) {
  808. crt->base.compute_config = pch_crt_compute_config;
  809. crt->base.disable = pch_disable_crt;
  810. crt->base.post_disable = pch_post_disable_crt;
  811. } else {
  812. crt->base.compute_config = intel_crt_compute_config;
  813. crt->base.disable = intel_disable_crt;
  814. }
  815. crt->base.port = PORT_NONE;
  816. crt->base.get_config = intel_crt_get_config;
  817. crt->base.get_hw_state = intel_crt_get_hw_state;
  818. crt->base.enable = intel_enable_crt;
  819. }
  820. intel_connector->get_hw_state = intel_connector_get_hw_state;
  821. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  822. if (!I915_HAS_HOTPLUG(dev_priv))
  823. intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  824. /*
  825. * Configure the automatic hotplug detection stuff
  826. */
  827. crt->force_hotplug_required = 0;
  828. /*
  829. * TODO: find a proper way to discover whether we need to set the the
  830. * polarity and link reversal bits or not, instead of relying on the
  831. * BIOS.
  832. */
  833. if (HAS_PCH_LPT(dev_priv)) {
  834. u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
  835. FDI_RX_LINK_REVERSAL_OVERRIDE;
  836. dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
  837. }
  838. intel_crt_reset(&crt->base.base);
  839. }