intel_crt.c 12 KB

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