nv50_sor.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_sor_disconnect(struct drm_encoder *encoder)
  40. {
  41. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  42. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  43. struct drm_device *dev = encoder->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 SOR %d\n", nv_encoder->or);
  50. ret = RING_SPACE(evo, 4);
  51. if (ret) {
  52. NV_ERROR(drm, "no space while disconnecting SOR\n");
  53. return;
  54. }
  55. BEGIN_NV04(evo, 0, NV50_EVO_SOR(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. nouveau_hdmi_mode_set(encoder, NULL);
  60. nv_encoder->crtc = NULL;
  61. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  62. }
  63. static void
  64. nv50_sor_dpms(struct drm_encoder *encoder, int mode)
  65. {
  66. struct nv50_display *priv = nv50_display(encoder->dev);
  67. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  68. struct drm_device *dev = encoder->dev;
  69. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  70. struct drm_encoder *enc;
  71. int or = nv_encoder->or;
  72. NV_DEBUG(drm, "or %d type %d mode %d\n", or, nv_encoder->dcb->type, mode);
  73. nv_encoder->last_dpms = mode;
  74. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  75. struct nouveau_encoder *nvenc = nouveau_encoder(enc);
  76. if (nvenc == nv_encoder ||
  77. (nvenc->dcb->type != DCB_OUTPUT_TMDS &&
  78. nvenc->dcb->type != DCB_OUTPUT_LVDS &&
  79. nvenc->dcb->type != DCB_OUTPUT_DP) ||
  80. nvenc->dcb->or != nv_encoder->dcb->or)
  81. continue;
  82. if (nvenc->last_dpms == DRM_MODE_DPMS_ON)
  83. return;
  84. }
  85. nv_call(priv->core, NV50_DISP_SOR_PWR + or, (mode == DRM_MODE_DPMS_ON));
  86. if (nv_encoder->dcb->type == DCB_OUTPUT_DP)
  87. nouveau_dp_dpms(encoder, mode, nv_encoder->dp.datarate, priv->core);
  88. }
  89. static void
  90. nv50_sor_save(struct drm_encoder *encoder)
  91. {
  92. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  93. NV_ERROR(drm, "!!\n");
  94. }
  95. static void
  96. nv50_sor_restore(struct drm_encoder *encoder)
  97. {
  98. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  99. NV_ERROR(drm, "!!\n");
  100. }
  101. static bool
  102. nv50_sor_mode_fixup(struct drm_encoder *encoder,
  103. const struct drm_display_mode *mode,
  104. struct drm_display_mode *adjusted_mode)
  105. {
  106. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  107. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  108. struct nouveau_connector *connector;
  109. NV_DEBUG(drm, "or %d\n", nv_encoder->or);
  110. connector = nouveau_encoder_connector_get(nv_encoder);
  111. if (!connector) {
  112. NV_ERROR(drm, "Encoder has no connector\n");
  113. return false;
  114. }
  115. if (connector->scaling_mode != DRM_MODE_SCALE_NONE &&
  116. connector->native_mode)
  117. drm_mode_copy(adjusted_mode, connector->native_mode);
  118. return true;
  119. }
  120. static void
  121. nv50_sor_prepare(struct drm_encoder *encoder)
  122. {
  123. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  124. nv50_sor_disconnect(encoder);
  125. if (nv_encoder->dcb->type == DCB_OUTPUT_DP) {
  126. /* avoid race between link training and supervisor intr */
  127. nv50_display_sync(encoder->dev);
  128. }
  129. }
  130. static void
  131. nv50_sor_commit(struct drm_encoder *encoder)
  132. {
  133. }
  134. static void
  135. nv50_sor_mode_set(struct drm_encoder *encoder, struct drm_display_mode *umode,
  136. struct drm_display_mode *mode)
  137. {
  138. struct nouveau_channel *evo = nv50_display(encoder->dev)->master;
  139. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  140. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  141. struct nouveau_crtc *crtc = nouveau_crtc(encoder->crtc);
  142. struct nouveau_connector *nv_connector;
  143. struct nv50_display *disp = nv50_display(encoder->dev);
  144. struct nvbios *bios = &drm->vbios;
  145. uint32_t mode_ctl = 0, script;
  146. int ret;
  147. NV_DEBUG(drm, "or %d type %d -> crtc %d\n",
  148. nv_encoder->or, nv_encoder->dcb->type, crtc->index);
  149. nv_encoder->crtc = encoder->crtc;
  150. switch (nv_encoder->dcb->type) {
  151. case DCB_OUTPUT_TMDS:
  152. if (nv_encoder->dcb->sorconf.link & 1) {
  153. if (mode->clock < 165000)
  154. mode_ctl = 0x0100;
  155. else
  156. mode_ctl = 0x0500;
  157. } else
  158. mode_ctl = 0x0200;
  159. nouveau_hdmi_mode_set(encoder, mode);
  160. break;
  161. case DCB_OUTPUT_LVDS:
  162. script = 0x0000;
  163. if (bios->fp_no_ddc) {
  164. if (bios->fp.dual_link)
  165. script |= 0x0100;
  166. if (bios->fp.if_is_24bit)
  167. script |= 0x0200;
  168. } else {
  169. /* determine number of lvds links */
  170. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  171. if (nv_connector && nv_connector->edid &&
  172. nv_connector->type == DCB_CONNECTOR_LVDS_SPWG) {
  173. /* http://www.spwg.org */
  174. if (((u8 *)nv_connector->edid)[121] == 2)
  175. script |= 0x0100;
  176. } else
  177. if (mode->clock >= bios->fp.duallink_transition_clk) {
  178. script |= 0x0100;
  179. }
  180. /* determine panel depth */
  181. if (script & 0x0100) {
  182. if (bios->fp.strapless_is_24bit & 2)
  183. script |= 0x0200;
  184. } else {
  185. if (bios->fp.strapless_is_24bit & 1)
  186. script |= 0x0200;
  187. }
  188. if (nv_connector && nv_connector->edid &&
  189. (nv_connector->edid->revision >= 4) &&
  190. (nv_connector->edid->input & 0x70) >= 0x20)
  191. script |= 0x0200;
  192. }
  193. nv_call(disp->core, NV50_DISP_SOR_LVDS_SCRIPT + nv_encoder->or, script);
  194. break;
  195. case DCB_OUTPUT_DP:
  196. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  197. if (nv_connector && nv_connector->base.display_info.bpc == 6) {
  198. nv_encoder->dp.datarate = mode->clock * 18 / 8;
  199. mode_ctl |= 0x00020000;
  200. } else {
  201. nv_encoder->dp.datarate = mode->clock * 24 / 8;
  202. mode_ctl |= 0x00050000;
  203. }
  204. if (nv_encoder->dcb->sorconf.link & 1)
  205. mode_ctl |= 0x00000800;
  206. else
  207. mode_ctl |= 0x00000900;
  208. break;
  209. default:
  210. break;
  211. }
  212. if (crtc->index == 1)
  213. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC1;
  214. else
  215. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_CRTC0;
  216. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  217. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NHSYNC;
  218. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  219. mode_ctl |= NV50_EVO_SOR_MODE_CTRL_NVSYNC;
  220. nv50_sor_dpms(encoder, DRM_MODE_DPMS_ON);
  221. ret = RING_SPACE(evo, 2);
  222. if (ret) {
  223. NV_ERROR(drm, "no space while connecting SOR\n");
  224. nv_encoder->crtc = NULL;
  225. return;
  226. }
  227. BEGIN_NV04(evo, 0, NV50_EVO_SOR(nv_encoder->or, MODE_CTRL), 1);
  228. OUT_RING(evo, mode_ctl);
  229. }
  230. static struct drm_crtc *
  231. nv50_sor_crtc_get(struct drm_encoder *encoder)
  232. {
  233. return nouveau_encoder(encoder)->crtc;
  234. }
  235. static const struct drm_encoder_helper_funcs nv50_sor_helper_funcs = {
  236. .dpms = nv50_sor_dpms,
  237. .save = nv50_sor_save,
  238. .restore = nv50_sor_restore,
  239. .mode_fixup = nv50_sor_mode_fixup,
  240. .prepare = nv50_sor_prepare,
  241. .commit = nv50_sor_commit,
  242. .mode_set = nv50_sor_mode_set,
  243. .get_crtc = nv50_sor_crtc_get,
  244. .detect = NULL,
  245. .disable = nv50_sor_disconnect
  246. };
  247. static void
  248. nv50_sor_destroy(struct drm_encoder *encoder)
  249. {
  250. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  251. drm_encoder_cleanup(encoder);
  252. kfree(nv_encoder);
  253. }
  254. static const struct drm_encoder_funcs nv50_sor_encoder_funcs = {
  255. .destroy = nv50_sor_destroy,
  256. };
  257. int
  258. nv50_sor_create(struct drm_connector *connector, struct dcb_output *entry)
  259. {
  260. struct nouveau_encoder *nv_encoder = NULL;
  261. struct drm_device *dev = connector->dev;
  262. struct drm_encoder *encoder;
  263. int type;
  264. switch (entry->type) {
  265. case DCB_OUTPUT_TMDS:
  266. case DCB_OUTPUT_DP:
  267. type = DRM_MODE_ENCODER_TMDS;
  268. break;
  269. case DCB_OUTPUT_LVDS:
  270. type = DRM_MODE_ENCODER_LVDS;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  276. if (!nv_encoder)
  277. return -ENOMEM;
  278. encoder = to_drm_encoder(nv_encoder);
  279. nv_encoder->dcb = entry;
  280. nv_encoder->or = ffs(entry->or) - 1;
  281. nv_encoder->last_dpms = DRM_MODE_DPMS_OFF;
  282. drm_encoder_init(dev, encoder, &nv50_sor_encoder_funcs, type);
  283. drm_encoder_helper_add(encoder, &nv50_sor_helper_funcs);
  284. encoder->possible_crtcs = entry->heads;
  285. encoder->possible_clones = 0;
  286. drm_mode_connector_attach_encoder(connector, encoder);
  287. return 0;
  288. }