nv50_dac.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include "drmP.h"
  27. #include "drm_crtc_helper.h"
  28. #define NOUVEAU_DMA_DEBUG (nouveau_reg_debug & NOUVEAU_REG_DEBUG_EVO)
  29. #include "nouveau_reg.h"
  30. #include "nouveau_drv.h"
  31. #include "nouveau_dma.h"
  32. #include "nouveau_encoder.h"
  33. #include "nouveau_connector.h"
  34. #include "nouveau_crtc.h"
  35. #include "nv50_display.h"
  36. static void
  37. nv50_dac_disconnect(struct nouveau_encoder *nv_encoder)
  38. {
  39. struct drm_device *dev = to_drm_encoder(nv_encoder)->dev;
  40. struct drm_nouveau_private *dev_priv = dev->dev_private;
  41. struct nouveau_channel *evo = dev_priv->evo;
  42. int ret;
  43. NV_DEBUG_KMS(dev, "Disconnecting DAC %d\n", nv_encoder->or);
  44. ret = RING_SPACE(evo, 2);
  45. if (ret) {
  46. NV_ERROR(dev, "no space while disconnecting DAC\n");
  47. return;
  48. }
  49. BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 1);
  50. OUT_RING(evo, 0);
  51. }
  52. static enum drm_connector_status
  53. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  54. {
  55. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  56. struct drm_device *dev = encoder->dev;
  57. struct drm_nouveau_private *dev_priv = dev->dev_private;
  58. enum drm_connector_status status = connector_status_disconnected;
  59. uint32_t dpms_state, load_pattern, load_state;
  60. int or = nv_encoder->or;
  61. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(or), 0x00000001);
  62. dpms_state = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or));
  63. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  64. 0x00150000 | NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  65. if (!nv_wait(NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  66. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
  67. NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
  68. NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
  69. nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
  70. return status;
  71. }
  72. /* Use bios provided value if possible. */
  73. if (dev_priv->vbios->dactestval) {
  74. load_pattern = dev_priv->vbios->dactestval;
  75. NV_DEBUG_KMS(dev, "Using bios provided load_pattern of %d\n",
  76. load_pattern);
  77. } else {
  78. load_pattern = 340;
  79. NV_DEBUG_KMS(dev, "Using default load_pattern of %d\n",
  80. load_pattern);
  81. }
  82. nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or),
  83. NV50_PDISPLAY_DAC_LOAD_CTRL_ACTIVE | load_pattern);
  84. mdelay(45); /* give it some time to process */
  85. load_state = nv_rd32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or));
  86. nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or), 0);
  87. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), dpms_state |
  88. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  89. if ((load_state & NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT) ==
  90. NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT)
  91. status = connector_status_connected;
  92. if (status == connector_status_connected)
  93. NV_DEBUG_KMS(dev, "Load was detected on output with or %d\n", or);
  94. else
  95. NV_DEBUG_KMS(dev, "Load was not detected on output with or %d\n", or);
  96. return status;
  97. }
  98. static void
  99. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  100. {
  101. struct drm_device *dev = encoder->dev;
  102. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  103. uint32_t val;
  104. int or = nv_encoder->or;
  105. NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
  106. /* wait for it to be done */
  107. if (!nv_wait(NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  108. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
  109. NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
  110. NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
  111. nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
  112. return;
  113. }
  114. val = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)) & ~0x7F;
  115. if (mode != DRM_MODE_DPMS_ON)
  116. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_BLANKED;
  117. switch (mode) {
  118. case DRM_MODE_DPMS_STANDBY:
  119. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
  120. break;
  121. case DRM_MODE_DPMS_SUSPEND:
  122. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
  123. break;
  124. case DRM_MODE_DPMS_OFF:
  125. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_OFF;
  126. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
  127. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
  128. break;
  129. default:
  130. break;
  131. }
  132. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), val |
  133. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  134. }
  135. static void
  136. nv50_dac_save(struct drm_encoder *encoder)
  137. {
  138. NV_ERROR(encoder->dev, "!!\n");
  139. }
  140. static void
  141. nv50_dac_restore(struct drm_encoder *encoder)
  142. {
  143. NV_ERROR(encoder->dev, "!!\n");
  144. }
  145. static bool
  146. nv50_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  147. struct drm_display_mode *adjusted_mode)
  148. {
  149. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  150. struct nouveau_connector *connector;
  151. NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
  152. connector = nouveau_encoder_connector_get(nv_encoder);
  153. if (!connector) {
  154. NV_ERROR(encoder->dev, "Encoder has no connector\n");
  155. return false;
  156. }
  157. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  158. connector->native_mode) {
  159. int id = adjusted_mode->base.id;
  160. *adjusted_mode = *connector->native_mode;
  161. adjusted_mode->base.id = id;
  162. }
  163. return true;
  164. }
  165. static void
  166. nv50_dac_prepare(struct drm_encoder *encoder)
  167. {
  168. }
  169. static void
  170. nv50_dac_commit(struct drm_encoder *encoder)
  171. {
  172. }
  173. static void
  174. nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  175. struct drm_display_mode *adjusted_mode)
  176. {
  177. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  178. struct drm_device *dev = encoder->dev;
  179. struct drm_nouveau_private *dev_priv = dev->dev_private;
  180. struct nouveau_channel *evo = dev_priv->evo;
  181. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  182. uint32_t mode_ctl = 0, mode_ctl2 = 0;
  183. int ret;
  184. NV_DEBUG_KMS(dev, "or %d\n", nv_encoder->or);
  185. nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  186. if (crtc->index == 1)
  187. mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC1;
  188. else
  189. mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC0;
  190. /* Lacking a working tv-out, this is not a 100% sure. */
  191. if (nv_encoder->dcb->type == OUTPUT_ANALOG)
  192. mode_ctl |= 0x40;
  193. else
  194. if (nv_encoder->dcb->type == OUTPUT_TV)
  195. mode_ctl |= 0x100;
  196. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  197. mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NHSYNC;
  198. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  199. mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NVSYNC;
  200. ret = RING_SPACE(evo, 3);
  201. if (ret) {
  202. NV_ERROR(dev, "no space while connecting DAC\n");
  203. return;
  204. }
  205. BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 2);
  206. OUT_RING(evo, mode_ctl);
  207. OUT_RING(evo, mode_ctl2);
  208. }
  209. static const struct drm_encoder_helper_funcs nv50_dac_helper_funcs = {
  210. .dpms = nv50_dac_dpms,
  211. .save = nv50_dac_save,
  212. .restore = nv50_dac_restore,
  213. .mode_fixup = nv50_dac_mode_fixup,
  214. .prepare = nv50_dac_prepare,
  215. .commit = nv50_dac_commit,
  216. .mode_set = nv50_dac_mode_set,
  217. .detect = nv50_dac_detect
  218. };
  219. static void
  220. nv50_dac_destroy(struct drm_encoder *encoder)
  221. {
  222. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  223. if (!encoder)
  224. return;
  225. NV_DEBUG_KMS(encoder->dev, "\n");
  226. drm_encoder_cleanup(encoder);
  227. kfree(nv_encoder);
  228. }
  229. static const struct drm_encoder_funcs nv50_dac_encoder_funcs = {
  230. .destroy = nv50_dac_destroy,
  231. };
  232. int
  233. nv50_dac_create(struct drm_device *dev, struct dcb_entry *entry)
  234. {
  235. struct nouveau_encoder *nv_encoder;
  236. struct drm_encoder *encoder;
  237. NV_DEBUG_KMS(dev, "\n");
  238. NV_INFO(dev, "Detected a DAC output\n");
  239. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  240. if (!nv_encoder)
  241. return -ENOMEM;
  242. encoder = to_drm_encoder(nv_encoder);
  243. nv_encoder->dcb = entry;
  244. nv_encoder->or = ffs(entry->or) - 1;
  245. nv_encoder->disconnect = nv50_dac_disconnect;
  246. drm_encoder_init(dev, encoder, &nv50_dac_encoder_funcs,
  247. DRM_MODE_ENCODER_DAC);
  248. drm_encoder_helper_add(encoder, &nv50_dac_helper_funcs);
  249. encoder->possible_crtcs = entry->heads;
  250. encoder->possible_clones = 0;
  251. return 0;
  252. }