intel_crt.c 15 KB

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