cdv_intel_crt.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <drm/drmP.h>
  28. #include "intel_bios.h"
  29. #include "psb_drv.h"
  30. #include "psb_intel_drv.h"
  31. #include "psb_intel_reg.h"
  32. #include "power.h"
  33. #include <linux/pm_runtime.h>
  34. static void cdv_intel_crt_dpms(struct drm_encoder *encoder, int mode)
  35. {
  36. struct drm_device *dev = encoder->dev;
  37. u32 temp, reg;
  38. reg = ADPA;
  39. temp = REG_READ(reg);
  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. REG_WRITE(reg, temp);
  57. }
  58. static int cdv_intel_crt_mode_valid(struct drm_connector *connector,
  59. struct drm_display_mode *mode)
  60. {
  61. int max_clock = 0;
  62. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  63. return MODE_NO_DBLESCAN;
  64. /* The lowest clock for CDV is 20000KHz */
  65. if (mode->clock < 20000)
  66. return MODE_CLOCK_LOW;
  67. /* The max clock for CDV is 355 instead of 400 */
  68. max_clock = 355000;
  69. if (mode->clock > max_clock)
  70. return MODE_CLOCK_HIGH;
  71. if (mode->hdisplay > 1680 || mode->vdisplay > 1050)
  72. return MODE_PANEL;
  73. return MODE_OK;
  74. }
  75. static bool cdv_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 cdv_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 psb_intel_crtc *psb_intel_crtc =
  88. to_psb_intel_crtc(crtc);
  89. int dpll_md_reg;
  90. u32 adpa, dpll_md;
  91. u32 adpa_reg;
  92. if (psb_intel_crtc->pipe == 0)
  93. dpll_md_reg = DPLL_A_MD;
  94. else
  95. dpll_md_reg = DPLL_B_MD;
  96. adpa_reg = ADPA;
  97. /*
  98. * Disable separate mode multiplier used when cloning SDVO to CRT
  99. * XXX this needs to be adjusted when we really are cloning
  100. */
  101. {
  102. dpll_md = REG_READ(dpll_md_reg);
  103. REG_WRITE(dpll_md_reg,
  104. dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
  105. }
  106. adpa = 0;
  107. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  108. adpa |= ADPA_HSYNC_ACTIVE_HIGH;
  109. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  110. adpa |= ADPA_VSYNC_ACTIVE_HIGH;
  111. if (psb_intel_crtc->pipe == 0)
  112. adpa |= ADPA_PIPE_A_SELECT;
  113. else
  114. adpa |= ADPA_PIPE_B_SELECT;
  115. REG_WRITE(adpa_reg, adpa);
  116. }
  117. /**
  118. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
  119. *
  120. * \return true if CRT is connected.
  121. * \return false if CRT is disconnected.
  122. */
  123. static bool cdv_intel_crt_detect_hotplug(struct drm_connector *connector,
  124. bool force)
  125. {
  126. struct drm_device *dev = connector->dev;
  127. u32 hotplug_en;
  128. int i, tries = 0, ret = false;
  129. u32 adpa_orig;
  130. /* disable the DAC when doing the hotplug detection */
  131. adpa_orig = REG_READ(ADPA);
  132. REG_WRITE(ADPA, adpa_orig & ~(ADPA_DAC_ENABLE));
  133. /*
  134. * On a CDV thep, CRT detect sequence need to be done twice
  135. * to get a reliable result.
  136. */
  137. tries = 2;
  138. hotplug_en = REG_READ(PORT_HOTPLUG_EN);
  139. hotplug_en &= ~(CRT_HOTPLUG_DETECT_MASK);
  140. hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
  141. hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
  142. hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
  143. for (i = 0; i < tries ; i++) {
  144. unsigned long timeout;
  145. /* turn on the FORCE_DETECT */
  146. REG_WRITE(PORT_HOTPLUG_EN, hotplug_en);
  147. timeout = jiffies + msecs_to_jiffies(1000);
  148. /* wait for FORCE_DETECT to go off */
  149. do {
  150. if (!(REG_READ(PORT_HOTPLUG_EN) &
  151. CRT_HOTPLUG_FORCE_DETECT))
  152. break;
  153. msleep(1);
  154. } while (time_after(timeout, jiffies));
  155. }
  156. if ((REG_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) !=
  157. CRT_HOTPLUG_MONITOR_NONE)
  158. ret = true;
  159. /* Restore the saved ADPA */
  160. REG_WRITE(ADPA, adpa_orig);
  161. return ret;
  162. }
  163. static enum drm_connector_status cdv_intel_crt_detect(
  164. struct drm_connector *connector, bool force)
  165. {
  166. if (cdv_intel_crt_detect_hotplug(connector, force))
  167. return connector_status_connected;
  168. else
  169. return connector_status_disconnected;
  170. }
  171. static void cdv_intel_crt_destroy(struct drm_connector *connector)
  172. {
  173. struct psb_intel_encoder *psb_intel_encoder =
  174. psb_intel_attached_encoder(connector);
  175. psb_intel_i2c_destroy(psb_intel_encoder->ddc_bus);
  176. drm_sysfs_connector_remove(connector);
  177. drm_connector_cleanup(connector);
  178. kfree(connector);
  179. }
  180. static int cdv_intel_crt_get_modes(struct drm_connector *connector)
  181. {
  182. struct psb_intel_encoder *psb_intel_encoder =
  183. psb_intel_attached_encoder(connector);
  184. return psb_intel_ddc_get_modes(connector, &psb_intel_encoder->ddc_bus->adapter);
  185. }
  186. static int cdv_intel_crt_set_property(struct drm_connector *connector,
  187. struct drm_property *property,
  188. uint64_t value)
  189. {
  190. return 0;
  191. }
  192. /*
  193. * Routines for controlling stuff on the analog port
  194. */
  195. static const struct drm_encoder_helper_funcs cdv_intel_crt_helper_funcs = {
  196. .dpms = cdv_intel_crt_dpms,
  197. .mode_fixup = cdv_intel_crt_mode_fixup,
  198. .prepare = psb_intel_encoder_prepare,
  199. .commit = psb_intel_encoder_commit,
  200. .mode_set = cdv_intel_crt_mode_set,
  201. };
  202. static const struct drm_connector_funcs cdv_intel_crt_connector_funcs = {
  203. .dpms = drm_helper_connector_dpms,
  204. .detect = cdv_intel_crt_detect,
  205. .fill_modes = drm_helper_probe_single_connector_modes,
  206. .destroy = cdv_intel_crt_destroy,
  207. .set_property = cdv_intel_crt_set_property,
  208. };
  209. static const struct drm_connector_helper_funcs
  210. cdv_intel_crt_connector_helper_funcs = {
  211. .mode_valid = cdv_intel_crt_mode_valid,
  212. .get_modes = cdv_intel_crt_get_modes,
  213. .best_encoder = psb_intel_best_encoder,
  214. };
  215. static void cdv_intel_crt_enc_destroy(struct drm_encoder *encoder)
  216. {
  217. drm_encoder_cleanup(encoder);
  218. }
  219. static const struct drm_encoder_funcs cdv_intel_crt_enc_funcs = {
  220. .destroy = cdv_intel_crt_enc_destroy,
  221. };
  222. void cdv_intel_crt_init(struct drm_device *dev,
  223. struct psb_intel_mode_device *mode_dev)
  224. {
  225. struct psb_intel_connector *psb_intel_connector;
  226. struct psb_intel_encoder *psb_intel_encoder;
  227. struct drm_connector *connector;
  228. struct drm_encoder *encoder;
  229. u32 i2c_reg;
  230. psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder), GFP_KERNEL);
  231. if (!psb_intel_encoder)
  232. return;
  233. psb_intel_connector = kzalloc(sizeof(struct psb_intel_connector), GFP_KERNEL);
  234. if (!psb_intel_connector)
  235. goto failed_connector;
  236. connector = &psb_intel_connector->base;
  237. drm_connector_init(dev, connector,
  238. &cdv_intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  239. encoder = &psb_intel_encoder->base;
  240. drm_encoder_init(dev, encoder,
  241. &cdv_intel_crt_enc_funcs, DRM_MODE_ENCODER_DAC);
  242. psb_intel_connector_attach_encoder(psb_intel_connector,
  243. psb_intel_encoder);
  244. /* Set up the DDC bus. */
  245. i2c_reg = GPIOA;
  246. /* Remove the following code for CDV */
  247. /*
  248. if (dev_priv->crt_ddc_bus != 0)
  249. i2c_reg = dev_priv->crt_ddc_bus;
  250. }*/
  251. psb_intel_encoder->ddc_bus = psb_intel_i2c_create(dev,
  252. i2c_reg, "CRTDDC_A");
  253. if (!psb_intel_encoder->ddc_bus) {
  254. dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
  255. "failed.\n");
  256. goto failed_ddc;
  257. }
  258. psb_intel_encoder->type = INTEL_OUTPUT_ANALOG;
  259. /*
  260. psb_intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT);
  261. psb_intel_output->crtc_mask = (1 << 0) | (1 << 1);
  262. */
  263. connector->interlace_allowed = 0;
  264. connector->doublescan_allowed = 0;
  265. drm_encoder_helper_add(encoder, &cdv_intel_crt_helper_funcs);
  266. drm_connector_helper_add(connector,
  267. &cdv_intel_crt_connector_helper_funcs);
  268. drm_sysfs_connector_add(connector);
  269. return;
  270. failed_ddc:
  271. drm_encoder_cleanup(&psb_intel_encoder->base);
  272. drm_connector_cleanup(&psb_intel_connector->base);
  273. kfree(psb_intel_connector);
  274. failed_connector:
  275. kfree(psb_intel_encoder);
  276. return;
  277. }