cdv_intel_crt.c 9.1 KB

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