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