intel_crt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/i2c.h>
  27. #include <linux/slab.h>
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "drm_crtc.h"
  31. #include "drm_crtc_helper.h"
  32. #include "intel_drv.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. static void intel_crt_dpms(struct drm_encoder *encoder, int mode)
  36. {
  37. struct drm_device *dev = encoder->dev;
  38. struct drm_i915_private *dev_priv = dev->dev_private;
  39. u32 temp, reg;
  40. if (HAS_PCH_SPLIT(dev))
  41. reg = PCH_ADPA;
  42. else
  43. reg = ADPA;
  44. temp = I915_READ(reg);
  45. temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
  46. temp &= ~ADPA_DAC_ENABLE;
  47. switch(mode) {
  48. case DRM_MODE_DPMS_ON:
  49. temp |= ADPA_DAC_ENABLE;
  50. break;
  51. case DRM_MODE_DPMS_STANDBY:
  52. temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
  53. break;
  54. case DRM_MODE_DPMS_SUSPEND:
  55. temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
  56. break;
  57. case DRM_MODE_DPMS_OFF:
  58. temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
  59. break;
  60. }
  61. I915_WRITE(reg, temp);
  62. }
  63. static int intel_crt_mode_valid(struct drm_connector *connector,
  64. struct drm_display_mode *mode)
  65. {
  66. struct drm_device *dev = connector->dev;
  67. int max_clock = 0;
  68. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  69. return MODE_NO_DBLESCAN;
  70. if (mode->clock < 25000)
  71. return MODE_CLOCK_LOW;
  72. if (!IS_I9XX(dev))
  73. max_clock = 350000;
  74. else
  75. max_clock = 400000;
  76. if (mode->clock > max_clock)
  77. return MODE_CLOCK_HIGH;
  78. return MODE_OK;
  79. }
  80. static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
  81. struct drm_display_mode *mode,
  82. struct drm_display_mode *adjusted_mode)
  83. {
  84. return true;
  85. }
  86. static void intel_crt_mode_set(struct drm_encoder *encoder,
  87. struct drm_display_mode *mode,
  88. struct drm_display_mode *adjusted_mode)
  89. {
  90. struct drm_device *dev = encoder->dev;
  91. struct drm_crtc *crtc = encoder->crtc;
  92. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  93. struct drm_i915_private *dev_priv = dev->dev_private;
  94. int dpll_md_reg;
  95. u32 adpa, dpll_md;
  96. u32 adpa_reg;
  97. if (intel_crtc->pipe == 0)
  98. dpll_md_reg = DPLL_A_MD;
  99. else
  100. dpll_md_reg = DPLL_B_MD;
  101. if (HAS_PCH_SPLIT(dev))
  102. adpa_reg = PCH_ADPA;
  103. else
  104. adpa_reg = ADPA;
  105. /*
  106. * Disable separate mode multiplier used when cloning SDVO to CRT
  107. * XXX this needs to be adjusted when we really are cloning
  108. */
  109. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  110. dpll_md = I915_READ(dpll_md_reg);
  111. I915_WRITE(dpll_md_reg,
  112. dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
  113. }
  114. adpa = 0;
  115. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  116. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  117. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  118. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  119. if (intel_crtc->pipe == 0) {
  120. if (HAS_PCH_CPT(dev))
  121. adpa |= PORT_TRANS_A_SEL_CPT;
  122. else
  123. adpa |= ADPA_PIPE_A_SELECT;
  124. if (!HAS_PCH_SPLIT(dev))
  125. I915_WRITE(BCLRPAT_A, 0);
  126. } else {
  127. if (HAS_PCH_CPT(dev))
  128. adpa |= PORT_TRANS_B_SEL_CPT;
  129. else
  130. adpa |= ADPA_PIPE_B_SELECT;
  131. if (!HAS_PCH_SPLIT(dev))
  132. I915_WRITE(BCLRPAT_B, 0);
  133. }
  134. I915_WRITE(adpa_reg, adpa);
  135. }
  136. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  137. {
  138. struct drm_device *dev = connector->dev;
  139. struct drm_i915_private *dev_priv = dev->dev_private;
  140. u32 adpa, temp;
  141. bool ret;
  142. bool turn_off_dac = false;
  143. temp = adpa = I915_READ(PCH_ADPA);
  144. if (HAS_PCH_SPLIT(dev))
  145. turn_off_dac = true;
  146. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  147. if (turn_off_dac)
  148. adpa &= ~ADPA_DAC_ENABLE;
  149. /* disable HPD first */
  150. I915_WRITE(PCH_ADPA, adpa);
  151. (void)I915_READ(PCH_ADPA);
  152. adpa |= (ADPA_CRT_HOTPLUG_PERIOD_128 |
  153. ADPA_CRT_HOTPLUG_WARMUP_10MS |
  154. ADPA_CRT_HOTPLUG_SAMPLE_4S |
  155. ADPA_CRT_HOTPLUG_VOLTAGE_50 | /* default */
  156. ADPA_CRT_HOTPLUG_VOLREF_325MV |
  157. ADPA_CRT_HOTPLUG_ENABLE |
  158. ADPA_CRT_HOTPLUG_FORCE_TRIGGER);
  159. DRM_DEBUG_KMS("pch crt adpa 0x%x", adpa);
  160. I915_WRITE(PCH_ADPA, adpa);
  161. if (wait_for((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) == 0,
  162. 1000))
  163. DRM_ERROR("timed out waiting for FORCE_TRIGGER");
  164. if (turn_off_dac) {
  165. I915_WRITE(PCH_ADPA, temp);
  166. (void)I915_READ(PCH_ADPA);
  167. }
  168. /* Check the status to see if both blue and green are on now */
  169. adpa = I915_READ(PCH_ADPA);
  170. adpa &= ADPA_CRT_HOTPLUG_MONITOR_MASK;
  171. if ((adpa == ADPA_CRT_HOTPLUG_MONITOR_COLOR) ||
  172. (adpa == ADPA_CRT_HOTPLUG_MONITOR_MONO))
  173. ret = true;
  174. else
  175. ret = false;
  176. return ret;
  177. }
  178. /**
  179. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  180. *
  181. * Not for i915G/i915GM
  182. *
  183. * \return true if CRT is connected.
  184. * \return false if CRT is disconnected.
  185. */
  186. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  187. {
  188. struct drm_device *dev = connector->dev;
  189. struct drm_i915_private *dev_priv = dev->dev_private;
  190. u32 hotplug_en, orig, stat;
  191. bool ret = false;
  192. int i, tries = 0;
  193. if (HAS_PCH_SPLIT(dev))
  194. return intel_ironlake_crt_detect_hotplug(connector);
  195. /*
  196. * On 4 series desktop, CRT detect sequence need to be done twice
  197. * to get a reliable result.
  198. */
  199. if (IS_G4X(dev) && !IS_GM45(dev))
  200. tries = 2;
  201. else
  202. tries = 1;
  203. hotplug_en = orig = I915_READ(PORT_HOTPLUG_EN);
  204. hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
  205. for (i = 0; i < tries ; i++) {
  206. /* turn on the FORCE_DETECT */
  207. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  208. /* wait for FORCE_DETECT to go off */
  209. if (wait_for((I915_READ(PORT_HOTPLUG_EN) &
  210. CRT_HOTPLUG_FORCE_DETECT) == 0,
  211. 1000))
  212. DRM_ERROR("timed out waiting for FORCE_DETECT to go off");
  213. }
  214. stat = I915_READ(PORT_HOTPLUG_STAT);
  215. if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
  216. ret = true;
  217. /* clear the interrupt we just generated, if any */
  218. I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
  219. /* and put the bits back */
  220. I915_WRITE(PORT_HOTPLUG_EN, orig);
  221. return ret;
  222. }
  223. static bool intel_crt_detect_ddc(struct drm_encoder *encoder)
  224. {
  225. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  226. /* CRT should always be at 0, but check anyway */
  227. if (intel_encoder->type != INTEL_OUTPUT_ANALOG)
  228. return false;
  229. return intel_ddc_probe(intel_encoder);
  230. }
  231. static enum drm_connector_status
  232. intel_crt_load_detect(struct drm_crtc *crtc, struct intel_encoder *intel_encoder)
  233. {
  234. struct drm_encoder *encoder = &intel_encoder->enc;
  235. struct drm_device *dev = encoder->dev;
  236. struct drm_i915_private *dev_priv = dev->dev_private;
  237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  238. uint32_t pipe = intel_crtc->pipe;
  239. uint32_t save_bclrpat;
  240. uint32_t save_vtotal;
  241. uint32_t vtotal, vactive;
  242. uint32_t vsample;
  243. uint32_t vblank, vblank_start, vblank_end;
  244. uint32_t dsl;
  245. uint32_t bclrpat_reg;
  246. uint32_t vtotal_reg;
  247. uint32_t vblank_reg;
  248. uint32_t vsync_reg;
  249. uint32_t pipeconf_reg;
  250. uint32_t pipe_dsl_reg;
  251. uint8_t st00;
  252. enum drm_connector_status status;
  253. if (pipe == 0) {
  254. bclrpat_reg = BCLRPAT_A;
  255. vtotal_reg = VTOTAL_A;
  256. vblank_reg = VBLANK_A;
  257. vsync_reg = VSYNC_A;
  258. pipeconf_reg = PIPEACONF;
  259. pipe_dsl_reg = PIPEADSL;
  260. } else {
  261. bclrpat_reg = BCLRPAT_B;
  262. vtotal_reg = VTOTAL_B;
  263. vblank_reg = VBLANK_B;
  264. vsync_reg = VSYNC_B;
  265. pipeconf_reg = PIPEBCONF;
  266. pipe_dsl_reg = PIPEBDSL;
  267. }
  268. save_bclrpat = I915_READ(bclrpat_reg);
  269. save_vtotal = I915_READ(vtotal_reg);
  270. vblank = I915_READ(vblank_reg);
  271. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  272. vactive = (save_vtotal & 0x7ff) + 1;
  273. vblank_start = (vblank & 0xfff) + 1;
  274. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  275. /* Set the border color to purple. */
  276. I915_WRITE(bclrpat_reg, 0x500050);
  277. if (IS_I9XX(dev)) {
  278. uint32_t pipeconf = I915_READ(pipeconf_reg);
  279. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  280. POSTING_READ(pipeconf_reg);
  281. /* Wait for next Vblank to substitue
  282. * border color for Color info */
  283. intel_wait_for_vblank(dev, pipe);
  284. st00 = I915_READ8(VGA_MSR_WRITE);
  285. status = ((st00 & (1 << 4)) != 0) ?
  286. connector_status_connected :
  287. connector_status_disconnected;
  288. I915_WRITE(pipeconf_reg, pipeconf);
  289. } else {
  290. bool restore_vblank = false;
  291. int count, detect;
  292. /*
  293. * If there isn't any border, add some.
  294. * Yes, this will flicker
  295. */
  296. if (vblank_start <= vactive && vblank_end >= vtotal) {
  297. uint32_t vsync = I915_READ(vsync_reg);
  298. uint32_t vsync_start = (vsync & 0xffff) + 1;
  299. vblank_start = vsync_start;
  300. I915_WRITE(vblank_reg,
  301. (vblank_start - 1) |
  302. ((vblank_end - 1) << 16));
  303. restore_vblank = true;
  304. }
  305. /* sample in the vertical border, selecting the larger one */
  306. if (vblank_start - vactive >= vtotal - vblank_end)
  307. vsample = (vblank_start + vactive) >> 1;
  308. else
  309. vsample = (vtotal + vblank_end) >> 1;
  310. /*
  311. * Wait for the border to be displayed
  312. */
  313. while (I915_READ(pipe_dsl_reg) >= vactive)
  314. ;
  315. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  316. ;
  317. /*
  318. * Watch ST00 for an entire scanline
  319. */
  320. detect = 0;
  321. count = 0;
  322. do {
  323. count++;
  324. /* Read the ST00 VGA status register */
  325. st00 = I915_READ8(VGA_MSR_WRITE);
  326. if (st00 & (1 << 4))
  327. detect++;
  328. } while ((I915_READ(pipe_dsl_reg) == dsl));
  329. /* restore vblank if necessary */
  330. if (restore_vblank)
  331. I915_WRITE(vblank_reg, vblank);
  332. /*
  333. * If more than 3/4 of the scanline detected a monitor,
  334. * then it is assumed to be present. This works even on i830,
  335. * where there isn't any way to force the border color across
  336. * the screen
  337. */
  338. status = detect * 4 > count * 3 ?
  339. connector_status_connected :
  340. connector_status_disconnected;
  341. }
  342. /* Restore previous settings */
  343. I915_WRITE(bclrpat_reg, save_bclrpat);
  344. return status;
  345. }
  346. static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
  347. {
  348. struct drm_device *dev = connector->dev;
  349. struct drm_encoder *encoder = intel_attached_encoder(connector);
  350. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  351. struct drm_crtc *crtc;
  352. int dpms_mode;
  353. enum drm_connector_status status;
  354. if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
  355. if (intel_crt_detect_hotplug(connector))
  356. return connector_status_connected;
  357. else
  358. return connector_status_disconnected;
  359. }
  360. if (intel_crt_detect_ddc(encoder))
  361. return connector_status_connected;
  362. /* for pre-945g platforms use load detect */
  363. if (encoder->crtc && encoder->crtc->enabled) {
  364. status = intel_crt_load_detect(encoder->crtc, intel_encoder);
  365. } else {
  366. crtc = intel_get_load_detect_pipe(intel_encoder, connector,
  367. NULL, &dpms_mode);
  368. if (crtc) {
  369. status = intel_crt_load_detect(crtc, intel_encoder);
  370. intel_release_load_detect_pipe(intel_encoder,
  371. connector, dpms_mode);
  372. } else
  373. status = connector_status_unknown;
  374. }
  375. return status;
  376. }
  377. static void intel_crt_destroy(struct drm_connector *connector)
  378. {
  379. drm_sysfs_connector_remove(connector);
  380. drm_connector_cleanup(connector);
  381. kfree(connector);
  382. }
  383. static int intel_crt_get_modes(struct drm_connector *connector)
  384. {
  385. int ret;
  386. struct drm_encoder *encoder = intel_attached_encoder(connector);
  387. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  388. struct i2c_adapter *ddc_bus;
  389. struct drm_device *dev = connector->dev;
  390. ret = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
  391. if (ret || !IS_G4X(dev))
  392. goto end;
  393. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  394. ddc_bus = intel_i2c_create(connector->dev, GPIOD, "CRTDDC_D");
  395. if (!ddc_bus) {
  396. dev_printk(KERN_ERR, &connector->dev->pdev->dev,
  397. "DDC bus registration failed for CRTDDC_D.\n");
  398. goto end;
  399. }
  400. /* Try to get modes by GPIOD port */
  401. ret = intel_ddc_get_modes(connector, ddc_bus);
  402. intel_i2c_destroy(ddc_bus);
  403. end:
  404. return ret;
  405. }
  406. static int intel_crt_set_property(struct drm_connector *connector,
  407. struct drm_property *property,
  408. uint64_t value)
  409. {
  410. return 0;
  411. }
  412. /*
  413. * Routines for controlling stuff on the analog port
  414. */
  415. static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
  416. .dpms = intel_crt_dpms,
  417. .mode_fixup = intel_crt_mode_fixup,
  418. .prepare = intel_encoder_prepare,
  419. .commit = intel_encoder_commit,
  420. .mode_set = intel_crt_mode_set,
  421. };
  422. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  423. .dpms = drm_helper_connector_dpms,
  424. .detect = intel_crt_detect,
  425. .fill_modes = drm_helper_probe_single_connector_modes,
  426. .destroy = intel_crt_destroy,
  427. .set_property = intel_crt_set_property,
  428. };
  429. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  430. .mode_valid = intel_crt_mode_valid,
  431. .get_modes = intel_crt_get_modes,
  432. .best_encoder = intel_attached_encoder,
  433. };
  434. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  435. .destroy = intel_encoder_destroy,
  436. };
  437. void intel_crt_init(struct drm_device *dev)
  438. {
  439. struct drm_connector *connector;
  440. struct intel_encoder *intel_encoder;
  441. struct intel_connector *intel_connector;
  442. struct drm_i915_private *dev_priv = dev->dev_private;
  443. u32 i2c_reg;
  444. intel_encoder = kzalloc(sizeof(struct intel_encoder), GFP_KERNEL);
  445. if (!intel_encoder)
  446. return;
  447. intel_connector = kzalloc(sizeof(struct intel_connector), GFP_KERNEL);
  448. if (!intel_connector) {
  449. kfree(intel_encoder);
  450. return;
  451. }
  452. connector = &intel_connector->base;
  453. drm_connector_init(dev, &intel_connector->base,
  454. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  455. drm_encoder_init(dev, &intel_encoder->enc, &intel_crt_enc_funcs,
  456. DRM_MODE_ENCODER_DAC);
  457. drm_mode_connector_attach_encoder(&intel_connector->base,
  458. &intel_encoder->enc);
  459. /* Set up the DDC bus. */
  460. if (HAS_PCH_SPLIT(dev))
  461. i2c_reg = PCH_GPIOA;
  462. else {
  463. i2c_reg = GPIOA;
  464. /* Use VBT information for CRT DDC if available */
  465. if (dev_priv->crt_ddc_bus != 0)
  466. i2c_reg = dev_priv->crt_ddc_bus;
  467. }
  468. intel_encoder->ddc_bus = intel_i2c_create(dev, i2c_reg, "CRTDDC_A");
  469. if (!intel_encoder->ddc_bus) {
  470. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  471. "failed.\n");
  472. return;
  473. }
  474. intel_encoder->type = INTEL_OUTPUT_ANALOG;
  475. intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
  476. (1 << INTEL_ANALOG_CLONE_BIT) |
  477. (1 << INTEL_SDVO_LVDS_CLONE_BIT);
  478. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  479. connector->interlace_allowed = 1;
  480. connector->doublescan_allowed = 0;
  481. drm_encoder_helper_add(&intel_encoder->enc, &intel_crt_helper_funcs);
  482. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  483. drm_sysfs_connector_add(connector);
  484. if (I915_HAS_HOTPLUG(dev))
  485. connector->polled = DRM_CONNECTOR_POLL_HPD;
  486. else
  487. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  488. dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
  489. }