intel_crt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  62. return MODE_NO_DBLESCAN;
  63. if (mode->clock > 400000 || mode->clock < 25000)
  64. return MODE_CLOCK_RANGE;
  65. return MODE_OK;
  66. }
  67. static bool intel_crt_mode_fixup(struct drm_encoder *encoder,
  68. struct drm_display_mode *mode,
  69. struct drm_display_mode *adjusted_mode)
  70. {
  71. return true;
  72. }
  73. static void intel_crt_mode_set(struct drm_encoder *encoder,
  74. struct drm_display_mode *mode,
  75. struct drm_display_mode *adjusted_mode)
  76. {
  77. struct drm_device *dev = encoder->dev;
  78. struct drm_crtc *crtc = encoder->crtc;
  79. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. int dpll_md_reg;
  82. u32 adpa, dpll_md;
  83. if (intel_crtc->pipe == 0)
  84. dpll_md_reg = DPLL_A_MD;
  85. else
  86. dpll_md_reg = DPLL_B_MD;
  87. /*
  88. * Disable separate mode multiplier used when cloning SDVO to CRT
  89. * XXX this needs to be adjusted when we really are cloning
  90. */
  91. if (IS_I965G(dev)) {
  92. dpll_md = I915_READ(dpll_md_reg);
  93. I915_WRITE(dpll_md_reg,
  94. dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
  95. }
  96. adpa = 0;
  97. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  98. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  99. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  100. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  101. if (intel_crtc->pipe == 0)
  102. adpa |= ADPA_PIPE_A_SELECT;
  103. else
  104. adpa |= ADPA_PIPE_B_SELECT;
  105. I915_WRITE(ADPA, adpa);
  106. }
  107. /**
  108. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  109. *
  110. * Not for i915G/i915GM
  111. *
  112. * \return true if CRT is connected.
  113. * \return false if CRT is disconnected.
  114. */
  115. static bool intel_crt_detect_hotplug(struct drm_connector *connector)
  116. {
  117. struct drm_device *dev = connector->dev;
  118. struct drm_i915_private *dev_priv = dev->dev_private;
  119. u32 temp;
  120. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  121. temp = I915_READ(PORT_HOTPLUG_EN);
  122. I915_WRITE(PORT_HOTPLUG_EN,
  123. temp | CRT_HOTPLUG_FORCE_DETECT | (1 << 5));
  124. do {
  125. if (!(I915_READ(PORT_HOTPLUG_EN) & CRT_HOTPLUG_FORCE_DETECT))
  126. break;
  127. msleep(1);
  128. } while (time_after(timeout, jiffies));
  129. if ((I915_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) ==
  130. CRT_HOTPLUG_MONITOR_COLOR)
  131. return true;
  132. return false;
  133. }
  134. static bool intel_crt_detect_ddc(struct drm_connector *connector)
  135. {
  136. struct intel_output *intel_output = to_intel_output(connector);
  137. /* CRT should always be at 0, but check anyway */
  138. if (intel_output->type != INTEL_OUTPUT_ANALOG)
  139. return false;
  140. return intel_ddc_probe(intel_output);
  141. }
  142. static enum drm_connector_status intel_crt_detect(struct drm_connector *connector)
  143. {
  144. struct drm_device *dev = connector->dev;
  145. if (IS_I9XX(dev) && !IS_I915G(dev) && !IS_I915GM(dev)) {
  146. if (intel_crt_detect_hotplug(connector))
  147. return connector_status_connected;
  148. else
  149. return connector_status_disconnected;
  150. }
  151. if (intel_crt_detect_ddc(connector))
  152. return connector_status_connected;
  153. /* TODO use load detect */
  154. return connector_status_unknown;
  155. }
  156. static void intel_crt_destroy(struct drm_connector *connector)
  157. {
  158. struct intel_output *intel_output = to_intel_output(connector);
  159. intel_i2c_destroy(intel_output->ddc_bus);
  160. drm_sysfs_connector_remove(connector);
  161. drm_connector_cleanup(connector);
  162. kfree(connector);
  163. }
  164. static int intel_crt_get_modes(struct drm_connector *connector)
  165. {
  166. struct intel_output *intel_output = to_intel_output(connector);
  167. return intel_ddc_get_modes(intel_output);
  168. }
  169. static int intel_crt_set_property(struct drm_connector *connector,
  170. struct drm_property *property,
  171. uint64_t value)
  172. {
  173. struct drm_device *dev = connector->dev;
  174. if (property == dev->mode_config.dpms_property && connector->encoder)
  175. intel_crt_dpms(connector->encoder, (uint32_t)(value & 0xf));
  176. return 0;
  177. }
  178. /*
  179. * Routines for controlling stuff on the analog port
  180. */
  181. static const struct drm_encoder_helper_funcs intel_crt_helper_funcs = {
  182. .dpms = intel_crt_dpms,
  183. .mode_fixup = intel_crt_mode_fixup,
  184. .prepare = intel_encoder_prepare,
  185. .commit = intel_encoder_commit,
  186. .mode_set = intel_crt_mode_set,
  187. };
  188. static const struct drm_connector_funcs intel_crt_connector_funcs = {
  189. .detect = intel_crt_detect,
  190. .fill_modes = drm_helper_probe_single_connector_modes,
  191. .destroy = intel_crt_destroy,
  192. .set_property = intel_crt_set_property,
  193. };
  194. static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
  195. .mode_valid = intel_crt_mode_valid,
  196. .get_modes = intel_crt_get_modes,
  197. .best_encoder = intel_best_encoder,
  198. };
  199. static void intel_crt_enc_destroy(struct drm_encoder *encoder)
  200. {
  201. drm_encoder_cleanup(encoder);
  202. }
  203. static const struct drm_encoder_funcs intel_crt_enc_funcs = {
  204. .destroy = intel_crt_enc_destroy,
  205. };
  206. void intel_crt_init(struct drm_device *dev)
  207. {
  208. struct drm_connector *connector;
  209. struct intel_output *intel_output;
  210. intel_output = kzalloc(sizeof(struct intel_output), GFP_KERNEL);
  211. if (!intel_output)
  212. return;
  213. connector = &intel_output->base;
  214. drm_connector_init(dev, &intel_output->base,
  215. &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  216. drm_encoder_init(dev, &intel_output->enc, &intel_crt_enc_funcs,
  217. DRM_MODE_ENCODER_DAC);
  218. drm_mode_connector_attach_encoder(&intel_output->base,
  219. &intel_output->enc);
  220. /* Set up the DDC bus. */
  221. intel_output->ddc_bus = intel_i2c_create(dev, GPIOA, "CRTDDC_A");
  222. if (!intel_output->ddc_bus) {
  223. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  224. "failed.\n");
  225. return;
  226. }
  227. intel_output->type = INTEL_OUTPUT_ANALOG;
  228. connector->interlace_allowed = 0;
  229. connector->doublescan_allowed = 0;
  230. drm_encoder_helper_add(&intel_output->enc, &intel_crt_helper_funcs);
  231. drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
  232. drm_sysfs_connector_add(connector);
  233. }