nv50_dac.c 8.7 KB

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