intel_crt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. adpa |= ADPA_PIPE_A_SELECT;
  121. if (!HAS_PCH_SPLIT(dev))
  122. I915_WRITE(BCLRPAT_A, 0);
  123. } else {
  124. adpa |= ADPA_PIPE_B_SELECT;
  125. if (!HAS_PCH_SPLIT(dev))
  126. I915_WRITE(BCLRPAT_B, 0);
  127. }
  128. I915_WRITE(adpa_reg, adpa);
  129. }
  130. static bool intel_ironlake_crt_detect_hotplug(struct drm_connector *connector)
  131. {
  132. struct drm_device *dev = connector->dev;
  133. struct drm_i915_private *dev_priv = dev->dev_private;
  134. u32 adpa;
  135. bool ret;
  136. adpa = I915_READ(PCH_ADPA);
  137. adpa &= ~ADPA_CRT_HOTPLUG_MASK;
  138. /* disable HPD first */
  139. I915_WRITE(PCH_ADPA, adpa);
  140. (void)I915_READ(PCH_ADPA);
  141. adpa |= (ADPA_CRT_HOTPLUG_PERIOD_128 |
  142. ADPA_CRT_HOTPLUG_WARMUP_10MS |
  143. ADPA_CRT_HOTPLUG_SAMPLE_4S |
  144. ADPA_CRT_HOTPLUG_VOLTAGE_50 | /* default */
  145. ADPA_CRT_HOTPLUG_VOLREF_325MV |
  146. ADPA_CRT_HOTPLUG_ENABLE |
  147. ADPA_CRT_HOTPLUG_FORCE_TRIGGER);
  148. DRM_DEBUG_KMS("pch crt adpa 0x%x", adpa);
  149. I915_WRITE(PCH_ADPA, adpa);
  150. while ((I915_READ(PCH_ADPA) & ADPA_CRT_HOTPLUG_FORCE_TRIGGER) != 0)
  151. ;
  152. /* Check the status to see if both blue and green are on now */
  153. adpa = I915_READ(PCH_ADPA);
  154. adpa &= ADPA_CRT_HOTPLUG_MONITOR_MASK;
  155. if ((adpa == ADPA_CRT_HOTPLUG_MONITOR_COLOR) ||
  156. (adpa == ADPA_CRT_HOTPLUG_MONITOR_MONO))
  157. ret = true;
  158. else
  159. ret = false;
  160. return ret;
  161. }
  162. /**
  163. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  164. *
  165. * Not for i915G/i915GM
  166. *
  167. * \return true if CRT is connected.
  168. * \return false if CRT is disconnected.
  169. */
  170. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  171. {
  172. struct drm_device *dev = connector->dev;
  173. struct drm_i915_private *dev_priv = dev->dev_private;
  174. u32 hotplug_en;
  175. int i, tries = 0;
  176. if (HAS_PCH_SPLIT(dev))
  177. return intel_ironlake_crt_detect_hotplug(connector);
  178. /*
  179. * On 4 series desktop, CRT detect sequence need to be done twice
  180. * to get a reliable result.
  181. */
  182. if (IS_G4X(dev) && !IS_GM45(dev))
  183. tries = 2;
  184. else
  185. tries = 1;
  186. hotplug_en = I915_READ(PORT_HOTPLUG_EN);
  187. hotplug_en &= CRT_FORCE_HOTPLUG_MASK;
  188. hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
  189. if (IS_G4X(dev))
  190. hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
  191. hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
  192. for (i = 0; i < tries ; i++) {
  193. unsigned long timeout;
  194. /* turn on the FORCE_DETECT */
  195. I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  196. timeout = jiffies + msecs_to_jiffies(1000);
  197. /* wait for FORCE_DETECT to go off */
  198. do {
  199. if (!(I915_READ(PORT_HOTPLUG_EN) &
  200. CRT_HOTPLUG_FORCE_DETECT))
  201. break;
  202. msleep(1);
  203. } while (time_after(timeout, jiffies));
  204. }
  205. if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) !=
  206. CRT_HOTPLUG_MONITOR_NONE)
  207. return true;
  208. return false;
  209. }
  210. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  211. {
  212. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  213. /* CRT should always be at 0, but check anyway */
  214. if (intel_encoder->type != INTEL_OUTPUT_ANALOG)
  215. return false;
  216. return intel_ddc_probe(intel_encoder);
  217. }
  218. static enum drm_connector_status
  219. intel_crt_load_detect(struct drm_crtc *crtc, struct intel_encoder *intel_encoder)
  220. {
  221. struct drm_encoder *encoder = &intel_encoder->enc;
  222. struct drm_device *dev = encoder->dev;
  223. struct drm_i915_private *dev_priv = dev->dev_private;
  224. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  225. uint32_t pipe = intel_crtc->pipe;
  226. uint32_t save_bclrpat;
  227. uint32_t save_vtotal;
  228. uint32_t vtotal, vactive;
  229. uint32_t vsample;
  230. uint32_t vblank, vblank_start, vblank_end;
  231. uint32_t dsl;
  232. uint32_t bclrpat_reg;
  233. uint32_t vtotal_reg;
  234. uint32_t vblank_reg;
  235. uint32_t vsync_reg;
  236. uint32_t pipeconf_reg;
  237. uint32_t pipe_dsl_reg;
  238. uint8_t st00;
  239. enum drm_connector_status status;
  240. if (pipe == 0) {
  241. bclrpat_reg = BCLRPAT_A;
  242. vtotal_reg = VTOTAL_A;
  243. vblank_reg = VBLANK_A;
  244. vsync_reg = VSYNC_A;
  245. pipeconf_reg = PIPEACONF;
  246. pipe_dsl_reg = PIPEADSL;
  247. } else {
  248. bclrpat_reg = BCLRPAT_B;
  249. vtotal_reg = VTOTAL_B;
  250. vblank_reg = VBLANK_B;
  251. vsync_reg = VSYNC_B;
  252. pipeconf_reg = PIPEBCONF;
  253. pipe_dsl_reg = PIPEBDSL;
  254. }
  255. save_bclrpat = I915_READ(bclrpat_reg);
  256. save_vtotal = I915_READ(vtotal_reg);
  257. vblank = I915_READ(vblank_reg);
  258. vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
  259. vactive = (save_vtotal & 0x7ff) + 1;
  260. vblank_start = (vblank & 0xfff) + 1;
  261. vblank_end = ((vblank >> 16) & 0xfff) + 1;
  262. /* Set the border color to purple. */
  263. I915_WRITE(bclrpat_reg, 0x500050);
  264. if (IS_I9XX(dev)) {
  265. uint32_t pipeconf = I915_READ(pipeconf_reg);
  266. I915_WRITE(pipeconf_reg, pipeconf | PIPECONF_FORCE_BORDER);
  267. /* Wait for next Vblank to substitue
  268. * border color for Color info */
  269. intel_wait_for_vblank(dev);
  270. st00 = I915_READ8(VGA_MSR_WRITE);
  271. status = ((st00 & (1 << 4)) != 0) ?
  272. connector_status_connected :
  273. connector_status_disconnected;
  274. I915_WRITE(pipeconf_reg, pipeconf);
  275. } else {
  276. bool restore_vblank = false;
  277. int count, detect;
  278. /*
  279. * If there isn't any border, add some.
  280. * Yes, this will flicker
  281. */
  282. if (vblank_start <= vactive && vblank_end >= vtotal) {
  283. uint32_t vsync = I915_READ(vsync_reg);
  284. uint32_t vsync_start = (vsync & 0xffff) + 1;
  285. vblank_start = vsync_start;
  286. I915_WRITE(vblank_reg,
  287. (vblank_start - 1) |
  288. ((vblank_end - 1) << 16));
  289. restore_vblank = true;
  290. }
  291. /* sample in the vertical border, selecting the larger one */
  292. if (vblank_start - vactive >= vtotal - vblank_end)
  293. vsample = (vblank_start + vactive) >> 1;
  294. else
  295. vsample = (vtotal + vblank_end) >> 1;
  296. /*
  297. * Wait for the border to be displayed
  298. */
  299. while (I915_READ(pipe_dsl_reg) >= vactive)
  300. ;
  301. while ((dsl = I915_READ(pipe_dsl_reg)) <= vsample)
  302. ;
  303. /*
  304. * Watch ST00 for an entire scanline
  305. */
  306. detect = 0;
  307. count = 0;
  308. do {
  309. count++;
  310. /* Read the ST00 VGA status register */
  311. st00 = I915_READ8(VGA_MSR_WRITE);
  312. if (st00 & (1 << 4))
  313. detect++;
  314. } while ((I915_READ(pipe_dsl_reg) == dsl));
  315. /* restore vblank if necessary */
  316. if (restore_vblank)
  317. I915_WRITE(vblank_reg, vblank);
  318. /*
  319. * If more than 3/4 of the scanline detected a monitor,
  320. * then it is assumed to be present. This works even on i830,
  321. * where there isn't any way to force the border color across
  322. * the screen
  323. */
  324. status = detect * 4 > count * 3 ?
  325. connector_status_connected :
  326. connector_status_disconnected;
  327. }
  328. /* Restore previous settings */
  329. I915_WRITE(bclrpat_reg, save_bclrpat);
  330. return status;
  331. }
  332. static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
  333. {
  334. struct drm_device *dev = connector->dev;
  335. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  336. struct drm_encoder *encoder = &intel_encoder->enc;
  337. struct drm_crtc *crtc;
  338. int dpms_mode;
  339. enum drm_connector_status status;
  340. if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
  341. if (intel_crt_detect_hotplug(connector))
  342. return connector_status_connected;
  343. else
  344. return connector_status_disconnected;
  345. }
  346. if (intel_crt_detect_ddc(connector))
  347. return connector_status_connected;
  348. /* for pre-945g platforms use load detect */
  349. if (encoder->crtc && encoder->crtc->enabled) {
  350. status = intel_crt_load_detect(encoder->crtc, intel_encoder);
  351. } else {
  352. crtc = intel_get_load_detect_pipe(intel_encoder,
  353. NULL, &dpms_mode);
  354. if (crtc) {
  355. status = intel_crt_load_detect(crtc, intel_encoder);
  356. intel_release_load_detect_pipe(intel_encoder, dpms_mode);
  357. } else
  358. status = connector_status_unknown;
  359. }
  360. return status;
  361. }
  362. static void intel_crt_destroy(struct drm_connector *connector)
  363. {
  364. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  365. intel_i2c_destroy(intel_encoder->ddc_bus);
  366. drm_sysfs_connector_remove(connector);
  367. drm_connector_cleanup(connector);
  368. kfree(connector);
  369. }
  370. static int intel_crt_get_modes(struct drm_connector *connector)
  371. {
  372. int ret;
  373. struct intel_encoder *intel_encoder = to_intel_encoder(connector);
  374. struct i2c_adapter *ddcbus;
  375. struct drm_device *dev = connector->dev;
  376. ret = intel_ddc_get_modes(intel_encoder);
  377. if (ret || !IS_G4X(dev))
  378. goto end;
  379. ddcbus = intel_encoder->ddc_bus;
  380. /* Try to probe digital port for output in DVI-I -> VGA mode. */
  381. intel_encoder->ddc_bus =
  382. intel_i2c_create(connector->dev, GPIOD, "CRTDDC_D");
  383. if (!intel_encoder->ddc_bus) {
  384. intel_encoder->ddc_bus = ddcbus;
  385. dev_printk(KERN_ERR, &connector->dev->pdev->dev,
  386. "DDC bus registration failed for CRTDDC_D.\n");
  387. goto end;
  388. }
  389. /* Try to get modes by GPIOD port */
  390. ret = intel_ddc_get_modes(intel_encoder);
  391. intel_i2c_destroy(ddcbus);
  392. end:
  393. return ret;
  394. }
  395. static int intel_crt_set_property(struct drm_connector *connector,
  396. struct drm_property *property,
  397. uint64_t value)
  398. {
  399. return 0;
  400. }
  401. /*
  402. * Routines for controlling stuff on the analog port
  403. */
  404. static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
  405. .dpms = intel_crt_dpms,
  406. .mode_fixup = intel_crt_mode_fixup,
  407. .prepare = intel_encoder_prepare,
  408. .commit = intel_encoder_commit,
  409. .mode_set = intel_crt_mode_set,
  410. };
  411. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  412. .dpms = drm_helper_connector_dpms,
  413. .detect = intel_crt_detect,
  414. .fill_modes = drm_helper_probe_single_connector_modes,
  415. .destroy = intel_crt_destroy,
  416. .set_property = intel_crt_set_property,
  417. };
  418. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  419. .mode_valid = intel_crt_mode_valid,
  420. .get_modes = intel_crt_get_modes,
  421. .best_encoder = intel_best_encoder,
  422. };
  423. static void intel_crt_enc_destroy(struct drm_encoder *encoder)
  424. {
  425. drm_encoder_cleanup(encoder);
  426. }
  427. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  428. .destroy = intel_crt_enc_destroy,
  429. };
  430. void intel_crt_init(struct drm_device *dev)
  431. {
  432. struct drm_connector *connector;
  433. struct intel_encoder *intel_encoder;
  434. struct drm_i915_private *dev_priv = dev->dev_private;
  435. u32 i2c_reg;
  436. intel_encoder = kzalloc(sizeof(struct intel_encoder), GFP_KERNEL);
  437. if (!intel_encoder)
  438. return;
  439. connector = &intel_encoder->base;
  440. drm_connector_init(dev, &intel_encoder->base,
  441. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  442. drm_encoder_init(dev, &intel_encoder->enc, &intel_crt_enc_funcs,
  443. DRM_MODE_ENCODER_DAC);
  444. drm_mode_connector_attach_encoder(&intel_encoder->base,
  445. &intel_encoder->enc);
  446. /* Set up the DDC bus. */
  447. if (HAS_PCH_SPLIT(dev))
  448. i2c_reg = PCH_GPIOA;
  449. else {
  450. i2c_reg = GPIOA;
  451. /* Use VBT information for CRT DDC if available */
  452. if (dev_priv->crt_ddc_bus != 0)
  453. i2c_reg = dev_priv->crt_ddc_bus;
  454. }
  455. intel_encoder->ddc_bus = intel_i2c_create(dev, i2c_reg, "CRTDDC_A");
  456. if (!intel_encoder->ddc_bus) {
  457. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  458. "failed.\n");
  459. return;
  460. }
  461. intel_encoder->type = INTEL_OUTPUT_ANALOG;
  462. intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
  463. (1 << INTEL_ANALOG_CLONE_BIT) |
  464. (1 << INTEL_SDVO_LVDS_CLONE_BIT);
  465. intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  466. connector->interlace_allowed = 0;
  467. connector->doublescan_allowed = 0;
  468. drm_encoder_helper_add(&intel_encoder->enc, &intel_crt_helper_funcs);
  469. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  470. drm_sysfs_connector_add(connector);
  471. dev_priv->hotplug_supported_mask |= CRT_HOTPLUG_INT_STATUS;
  472. }