intel_crt.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 temp;
  131. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  132. temp = I915_READ(PORT_HOTPLUG_EN);
  133. I915_WRITE(PORT_HOTPLUG_EN,
  134. temp | CRT_HOTPLUG_FORCE_DETECT | (1 << 5));
  135. do {
  136. if (!(I915_READ(PORT_HOTPLUG_EN) & CRT_HOTPLUG_FORCE_DETECT))
  137. break;
  138. msleep(1);
  139. } while (time_after(timeout, jiffies));
  140. if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
  141. CRT_HOTPLUG_MONITOR_COLOR)
  142. return true;
  143. return false;
  144. }
  145. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  146. {
  147. struct intel_output *intel_output = to_intel_output(connector);
  148. /* CRT should always be at 0, but check anyway */
  149. if (intel_output->type != INTEL_OUTPUT_ANALOG)
  150. return false;
  151. return intel_ddc_probe(intel_output);
  152. }
  153. static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
  154. {
  155. struct drm_device *dev = connector->dev;
  156. if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
  157. if (intel_crt_detect_hotplug(connector))
  158. return connector_status_connected;
  159. else
  160. return connector_status_disconnected;
  161. }
  162. if (intel_crt_detect_ddc(connector))
  163. return connector_status_connected;
  164. /* TODO use load detect */
  165. return connector_status_unknown;
  166. }
  167. static void intel_crt_destroy(struct drm_connector *connector)
  168. {
  169. struct intel_output *intel_output = to_intel_output(connector);
  170. intel_i2c_destroy(intel_output->ddc_bus);
  171. drm_sysfs_connector_remove(connector);
  172. drm_connector_cleanup(connector);
  173. kfree(connector);
  174. }
  175. static int intel_crt_get_modes(struct drm_connector *connector)
  176. {
  177. struct intel_output *intel_output = to_intel_output(connector);
  178. return intel_ddc_get_modes(intel_output);
  179. }
  180. static int intel_crt_set_property(struct drm_connector *connector,
  181. struct drm_property *property,
  182. uint64_t value)
  183. {
  184. struct drm_device *dev = connector->dev;
  185. if (property == dev->mode_config.dpms_property && connector->encoder)
  186. intel_crt_dpms(connector->encoder, (uint32_t)(value & 0xf));
  187. return 0;
  188. }
  189. /*
  190. * Routines for controlling stuff on the analog port
  191. */
  192. static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
  193. .dpms = intel_crt_dpms,
  194. .mode_fixup = intel_crt_mode_fixup,
  195. .prepare = intel_encoder_prepare,
  196. .commit = intel_encoder_commit,
  197. .mode_set = intel_crt_mode_set,
  198. };
  199. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  200. .detect = intel_crt_detect,
  201. .fill_modes = drm_helper_probe_single_connector_modes,
  202. .destroy = intel_crt_destroy,
  203. .set_property = intel_crt_set_property,
  204. };
  205. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  206. .mode_valid = intel_crt_mode_valid,
  207. .get_modes = intel_crt_get_modes,
  208. .best_encoder = intel_best_encoder,
  209. };
  210. static void intel_crt_enc_destroy(struct drm_encoder *encoder)
  211. {
  212. drm_encoder_cleanup(encoder);
  213. }
  214. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  215. .destroy = intel_crt_enc_destroy,
  216. };
  217. void intel_crt_init(struct drm_device *dev)
  218. {
  219. struct drm_connector *connector;
  220. struct intel_output *intel_output;
  221. intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  222. if (!intel_output)
  223. return;
  224. connector = &intel_output->base;
  225. drm_connector_init(dev, &intel_output->base,
  226. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  227. drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
  228. DRM_MODE_ENCODER_DAC);
  229. drm_mode_connector_attach_encoder(&intel_output->base,
  230. &intel_output->enc);
  231. /* Set up the DDC bus. */
  232. intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
  233. if (!intel_output->ddc_bus) {
  234. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  235. "failed.\n");
  236. return;
  237. }
  238. intel_output->type = INTEL_OUTPUT_ANALOG;
  239. connector->interlace_allowed = 0;
  240. connector->doublescan_allowed = 0;
  241. drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
  242. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  243. drm_sysfs_connector_add(connector);
  244. }