nv50_dac.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 drm_encoder *encoder)
  38. {
  39. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  40. struct drm_device *dev = encoder->dev;
  41. struct drm_nouveau_private *dev_priv = dev->dev_private;
  42. struct nouveau_channel *evo = dev_priv->evo;
  43. int ret;
  44. if (!nv_encoder->crtc)
  45. return;
  46. nv50_crtc_blank(nouveau_crtc(nv_encoder->crtc), true);
  47. NV_DEBUG_KMS(dev, "Disconnecting DAC %d\n", nv_encoder->or);
  48. ret = RING_SPACE(evo, 4);
  49. if (ret) {
  50. NV_ERROR(dev, "no space while disconnecting DAC\n");
  51. return;
  52. }
  53. BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 1);
  54. OUT_RING (evo, 0);
  55. BEGIN_RING(evo, 0, NV50_EVO_UPDATE, 1);
  56. OUT_RING (evo, 0);
  57. nv_encoder->crtc = NULL;
  58. }
  59. static enum drm_connector_status
  60. nv50_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  61. {
  62. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  63. struct drm_device *dev = encoder->dev;
  64. struct drm_nouveau_private *dev_priv = dev->dev_private;
  65. enum drm_connector_status status = connector_status_disconnected;
  66. uint32_t dpms_state, load_pattern, load_state;
  67. int or = nv_encoder->or;
  68. nv_wr32(dev, NV50_PDISPLAY_DAC_CLK_CTRL1(or), 0x00000001);
  69. dpms_state = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or));
  70. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  71. 0x00150000 | NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  72. if (!nv_wait(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  73. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
  74. NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
  75. NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
  76. nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
  77. return status;
  78. }
  79. /* Use bios provided value if possible. */
  80. if (dev_priv->vbios.dactestval) {
  81. load_pattern = dev_priv->vbios.dactestval;
  82. NV_DEBUG_KMS(dev, "Using bios provided load_pattern of %d\n",
  83. load_pattern);
  84. } else {
  85. load_pattern = 340;
  86. NV_DEBUG_KMS(dev, "Using default load_pattern of %d\n",
  87. load_pattern);
  88. }
  89. nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or),
  90. NV50_PDISPLAY_DAC_LOAD_CTRL_ACTIVE | load_pattern);
  91. mdelay(45); /* give it some time to process */
  92. load_state = nv_rd32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or));
  93. nv_wr32(dev, NV50_PDISPLAY_DAC_LOAD_CTRL(or), 0);
  94. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), dpms_state |
  95. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  96. if ((load_state & NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT) ==
  97. NV50_PDISPLAY_DAC_LOAD_CTRL_PRESENT)
  98. status = connector_status_connected;
  99. if (status == connector_status_connected)
  100. NV_DEBUG_KMS(dev, "Load was detected on output with or %d\n", or);
  101. else
  102. NV_DEBUG_KMS(dev, "Load was not detected on output with or %d\n", or);
  103. return status;
  104. }
  105. static void
  106. nv50_dac_dpms(struct drm_encoder *encoder, int mode)
  107. {
  108. struct drm_device *dev = encoder->dev;
  109. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  110. uint32_t val;
  111. int or = nv_encoder->or;
  112. NV_DEBUG_KMS(dev, "or %d mode %d\n", or, mode);
  113. /* wait for it to be done */
  114. if (!nv_wait(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or),
  115. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING, 0)) {
  116. NV_ERROR(dev, "timeout: DAC_DPMS_CTRL_PENDING(%d) == 0\n", or);
  117. NV_ERROR(dev, "DAC_DPMS_CTRL(%d) = 0x%08x\n", or,
  118. nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)));
  119. return;
  120. }
  121. val = nv_rd32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or)) & ~0x7F;
  122. if (mode != DRM_MODE_DPMS_ON)
  123. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_BLANKED;
  124. switch (mode) {
  125. case DRM_MODE_DPMS_STANDBY:
  126. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
  127. break;
  128. case DRM_MODE_DPMS_SUSPEND:
  129. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
  130. break;
  131. case DRM_MODE_DPMS_OFF:
  132. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_OFF;
  133. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_HSYNC_OFF;
  134. val |= NV50_PDISPLAY_DAC_DPMS_CTRL_VSYNC_OFF;
  135. break;
  136. default:
  137. break;
  138. }
  139. nv_wr32(dev, NV50_PDISPLAY_DAC_DPMS_CTRL(or), val |
  140. NV50_PDISPLAY_DAC_DPMS_CTRL_PENDING);
  141. }
  142. static void
  143. nv50_dac_save(struct drm_encoder *encoder)
  144. {
  145. NV_ERROR(encoder->dev, "!!\n");
  146. }
  147. static void
  148. nv50_dac_restore(struct drm_encoder *encoder)
  149. {
  150. NV_ERROR(encoder->dev, "!!\n");
  151. }
  152. static bool
  153. nv50_dac_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  154. struct drm_display_mode *adjusted_mode)
  155. {
  156. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  157. struct nouveau_connector *connector;
  158. NV_DEBUG_KMS(encoder->dev, "or %d\n", nv_encoder->or);
  159. connector = nouveau_encoder_connector_get(nv_encoder);
  160. if (!connector) {
  161. NV_ERROR(encoder->dev, "Encoder has no connector\n");
  162. return false;
  163. }
  164. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  165. connector->native_mode) {
  166. int id = adjusted_mode->base.id;
  167. *adjusted_mode = *connector->native_mode;
  168. adjusted_mode->base.id = id;
  169. }
  170. return true;
  171. }
  172. static void
  173. nv50_dac_prepare(struct drm_encoder *encoder)
  174. {
  175. }
  176. static void
  177. nv50_dac_commit(struct drm_encoder *encoder)
  178. {
  179. }
  180. static void
  181. nv50_dac_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  182. struct drm_display_mode *adjusted_mode)
  183. {
  184. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  185. struct drm_device *dev = encoder->dev;
  186. struct drm_nouveau_private *dev_priv = dev->dev_private;
  187. struct nouveau_channel *evo = dev_priv->evo;
  188. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  189. uint32_t mode_ctl = 0, mode_ctl2 = 0;
  190. int ret;
  191. NV_DEBUG_KMS(dev, "or %d type %d crtc %d\n",
  192. nv_encoder->or, nv_encoder->dcb->type, crtc->index);
  193. nv50_dac_dpms(encoder, DRM_MODE_DPMS_ON);
  194. if (crtc->index == 1)
  195. mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC1;
  196. else
  197. mode_ctl |= NV50_EVO_DAC_MODE_CTRL_CRTC0;
  198. /* Lacking a working tv-out, this is not a 100% sure. */
  199. if (nv_encoder->dcb->type == OUTPUT_ANALOG)
  200. mode_ctl |= 0x40;
  201. else
  202. if (nv_encoder->dcb->type == OUTPUT_TV)
  203. mode_ctl |= 0x100;
  204. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  205. mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NHSYNC;
  206. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  207. mode_ctl2 |= NV50_EVO_DAC_MODE_CTRL2_NVSYNC;
  208. ret = RING_SPACE(evo, 3);
  209. if (ret) {
  210. NV_ERROR(dev, "no space while connecting DAC\n");
  211. return;
  212. }
  213. BEGIN_RING(evo, 0, NV50_EVO_DAC(nv_encoder->or, MODE_CTRL), 2);
  214. OUT_RING(evo, mode_ctl);
  215. OUT_RING(evo, mode_ctl2);
  216. nv_encoder->crtc = encoder->crtc;
  217. }
  218. static struct drm_crtc *
  219. nv50_dac_crtc_get(struct drm_encoder *encoder)
  220. {
  221. return nouveau_encoder(encoder)->crtc;
  222. }
  223. static const struct drm_encoder_helper_funcs nv50_dac_helper_funcs = {
  224. .dpms = nv50_dac_dpms,
  225. .save = nv50_dac_save,
  226. .restore = nv50_dac_restore,
  227. .mode_fixup = nv50_dac_mode_fixup,
  228. .prepare = nv50_dac_prepare,
  229. .commit = nv50_dac_commit,
  230. .mode_set = nv50_dac_mode_set,
  231. .get_crtc = nv50_dac_crtc_get,
  232. .detect = nv50_dac_detect,
  233. .disable = nv50_dac_disconnect
  234. };
  235. static void
  236. nv50_dac_destroy(struct drm_encoder *encoder)
  237. {
  238. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  239. if (!encoder)
  240. return;
  241. NV_DEBUG_KMS(encoder->dev, "\n");
  242. drm_encoder_cleanup(encoder);
  243. kfree(nv_encoder);
  244. }
  245. static const struct drm_encoder_funcs nv50_dac_encoder_funcs = {
  246. .destroy = nv50_dac_destroy,
  247. };
  248. int
  249. nv50_dac_create(struct drm_connector *connector, struct dcb_entry *entry)
  250. {
  251. struct nouveau_encoder *nv_encoder;
  252. struct drm_encoder *encoder;
  253. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  254. if (!nv_encoder)
  255. return -ENOMEM;
  256. encoder = to_drm_encoder(nv_encoder);
  257. nv_encoder->dcb = entry;
  258. nv_encoder->or = ffs(entry->or) - 1;
  259. drm_encoder_init(connector->dev, encoder, &nv50_dac_encoder_funcs,
  260. DRM_MODE_ENCODER_DAC);
  261. drm_encoder_helper_add(encoder, &nv50_dac_helper_funcs);
  262. encoder->possible_crtcs = entry->heads;
  263. encoder->possible_clones = 0;
  264. drm_mode_connector_attach_encoder(connector, encoder);
  265. return 0;
  266. }