nv04_dfp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright 2003 NVIDIA, Corporation
  3. * Copyright 2006 Dave Airlie
  4. * Copyright 2007 Maarten Maathuis
  5. * Copyright 2007-2009 Stuart Bennett
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "drmP.h"
  27. #include "drm_crtc_helper.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_crtc.h"
  32. #include "nouveau_hw.h"
  33. #include "nvreg.h"
  34. #define FP_TG_CONTROL_ON (NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS | \
  35. NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS | \
  36. NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS)
  37. #define FP_TG_CONTROL_OFF (NV_PRAMDAC_FP_TG_CONTROL_DISPEN_DISABLE | \
  38. NV_PRAMDAC_FP_TG_CONTROL_HSYNC_DISABLE | \
  39. NV_PRAMDAC_FP_TG_CONTROL_VSYNC_DISABLE)
  40. static inline bool is_fpc_off(uint32_t fpc)
  41. {
  42. return ((fpc & (FP_TG_CONTROL_ON | FP_TG_CONTROL_OFF)) ==
  43. FP_TG_CONTROL_OFF);
  44. }
  45. int nv04_dfp_get_bound_head(struct drm_device *dev, struct dcb_entry *dcbent)
  46. {
  47. /* special case of nv_read_tmds to find crtc associated with an output.
  48. * this does not give a correct answer for off-chip dvi, but there's no
  49. * use for such an answer anyway
  50. */
  51. int ramdac = (dcbent->or & OUTPUT_C) >> 2;
  52. NVWriteRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_CONTROL,
  53. NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE | 0x4);
  54. return ((NVReadRAMDAC(dev, ramdac, NV_PRAMDAC_FP_TMDS_DATA) & 0x8) >> 3) ^ ramdac;
  55. }
  56. void nv04_dfp_bind_head(struct drm_device *dev, struct dcb_entry *dcbent,
  57. int head, bool dl)
  58. {
  59. /* The BIOS scripts don't do this for us, sadly
  60. * Luckily we do know the values ;-)
  61. *
  62. * head < 0 indicates we wish to force a setting with the overrideval
  63. * (for VT restore etc.)
  64. */
  65. int ramdac = (dcbent->or & OUTPUT_C) >> 2;
  66. uint8_t tmds04 = 0x80;
  67. if (head != ramdac)
  68. tmds04 = 0x88;
  69. if (dcbent->type == OUTPUT_LVDS)
  70. tmds04 |= 0x01;
  71. nv_write_tmds(dev, dcbent->or, 0, 0x04, tmds04);
  72. if (dl) /* dual link */
  73. nv_write_tmds(dev, dcbent->or, 1, 0x04, tmds04 ^ 0x08);
  74. }
  75. void nv04_dfp_disable(struct drm_device *dev, int head)
  76. {
  77. struct drm_nouveau_private *dev_priv = dev->dev_private;
  78. struct nv04_crtc_reg *crtcstate = dev_priv->mode_reg.crtc_reg;
  79. if (NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL) &
  80. FP_TG_CONTROL_ON) {
  81. /* digital remnants must be cleaned before new crtc
  82. * values programmed. delay is time for the vga stuff
  83. * to realise it's in control again
  84. */
  85. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL,
  86. FP_TG_CONTROL_OFF);
  87. msleep(50);
  88. }
  89. /* don't inadvertently turn it on when state written later */
  90. crtcstate[head].fp_control = FP_TG_CONTROL_OFF;
  91. }
  92. void nv04_dfp_update_fp_control(struct drm_encoder *encoder, int mode)
  93. {
  94. struct drm_device *dev = encoder->dev;
  95. struct drm_nouveau_private *dev_priv = dev->dev_private;
  96. struct drm_crtc *crtc;
  97. struct nouveau_crtc *nv_crtc;
  98. uint32_t *fpc;
  99. if (mode == DRM_MODE_DPMS_ON) {
  100. nv_crtc = nouveau_crtc(encoder->crtc);
  101. fpc = &dev_priv->mode_reg.crtc_reg[nv_crtc->index].fp_control;
  102. if (is_fpc_off(*fpc)) {
  103. /* using saved value is ok, as (is_digital && dpms_on &&
  104. * fp_control==OFF) is (at present) *only* true when
  105. * fpc's most recent change was by below "off" code
  106. */
  107. *fpc = nv_crtc->dpms_saved_fp_control;
  108. }
  109. nv_crtc->fp_users |= 1 << nouveau_encoder(encoder)->dcb->index;
  110. NVWriteRAMDAC(dev, nv_crtc->index, NV_PRAMDAC_FP_TG_CONTROL, *fpc);
  111. } else {
  112. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  113. nv_crtc = nouveau_crtc(crtc);
  114. fpc = &dev_priv->mode_reg.crtc_reg[nv_crtc->index].fp_control;
  115. nv_crtc->fp_users &= ~(1 << nouveau_encoder(encoder)->dcb->index);
  116. if (!is_fpc_off(*fpc) && !nv_crtc->fp_users) {
  117. nv_crtc->dpms_saved_fp_control = *fpc;
  118. /* cut the FP output */
  119. *fpc &= ~FP_TG_CONTROL_ON;
  120. *fpc |= FP_TG_CONTROL_OFF;
  121. NVWriteRAMDAC(dev, nv_crtc->index,
  122. NV_PRAMDAC_FP_TG_CONTROL, *fpc);
  123. }
  124. }
  125. }
  126. }
  127. static bool nv04_dfp_mode_fixup(struct drm_encoder *encoder,
  128. struct drm_display_mode *mode,
  129. struct drm_display_mode *adjusted_mode)
  130. {
  131. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  132. struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
  133. /* For internal panels and gpu scaling on DVI we need the native mode */
  134. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  135. if (!nv_connector->native_mode)
  136. return false;
  137. nv_encoder->mode = *nv_connector->native_mode;
  138. adjusted_mode->clock = nv_connector->native_mode->clock;
  139. } else {
  140. nv_encoder->mode = *adjusted_mode;
  141. }
  142. return true;
  143. }
  144. static void nv04_dfp_prepare_sel_clk(struct drm_device *dev,
  145. struct nouveau_encoder *nv_encoder, int head)
  146. {
  147. struct drm_nouveau_private *dev_priv = dev->dev_private;
  148. struct nv04_mode_state *state = &dev_priv->mode_reg;
  149. uint32_t bits1618 = nv_encoder->dcb->or & OUTPUT_A ? 0x10000 : 0x40000;
  150. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP)
  151. return;
  152. /* SEL_CLK is only used on the primary ramdac
  153. * It toggles spread spectrum PLL output and sets the bindings of PLLs
  154. * to heads on digital outputs
  155. */
  156. if (head)
  157. state->sel_clk |= bits1618;
  158. else
  159. state->sel_clk &= ~bits1618;
  160. /* nv30:
  161. * bit 0 NVClk spread spectrum on/off
  162. * bit 2 MemClk spread spectrum on/off
  163. * bit 4 PixClk1 spread spectrum on/off toggle
  164. * bit 6 PixClk2 spread spectrum on/off toggle
  165. *
  166. * nv40 (observations from bios behaviour and mmio traces):
  167. * bits 4&6 as for nv30
  168. * bits 5&7 head dependent as for bits 4&6, but do not appear with 4&6;
  169. * maybe a different spread mode
  170. * bits 8&10 seen on dual-link dvi outputs, purpose unknown (set by POST scripts)
  171. * The logic behind turning spread spectrum on/off in the first place,
  172. * and which bit-pair to use, is unclear on nv40 (for earlier cards, the fp table
  173. * entry has the necessary info)
  174. */
  175. if (nv_encoder->dcb->type == OUTPUT_LVDS && dev_priv->saved_reg.sel_clk & 0xf0) {
  176. int shift = (dev_priv->saved_reg.sel_clk & 0x50) ? 0 : 1;
  177. state->sel_clk &= ~0xf0;
  178. state->sel_clk |= (head ? 0x40 : 0x10) << shift;
  179. }
  180. }
  181. static void nv04_dfp_prepare(struct drm_encoder *encoder)
  182. {
  183. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  184. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  185. struct drm_device *dev = encoder->dev;
  186. struct drm_nouveau_private *dev_priv = dev->dev_private;
  187. int head = nouveau_crtc(encoder->crtc)->index;
  188. struct nv04_crtc_reg *crtcstate = dev_priv->mode_reg.crtc_reg;
  189. uint8_t *cr_lcd = &crtcstate[head].CRTC[NV_CIO_CRE_LCD__INDEX];
  190. uint8_t *cr_lcd_oth = &crtcstate[head ^ 1].CRTC[NV_CIO_CRE_LCD__INDEX];
  191. helper->dpms(encoder, DRM_MODE_DPMS_OFF);
  192. nv04_dfp_prepare_sel_clk(dev, nv_encoder, head);
  193. /* Some NV4x have unknown values (0x3f, 0x50, 0x54, 0x6b, 0x79, 0x7f)
  194. * at LCD__INDEX which we don't alter
  195. */
  196. if (!(*cr_lcd & 0x44)) {
  197. *cr_lcd = 0x3;
  198. if (nv_two_heads(dev)) {
  199. if (nv_encoder->dcb->location == DCB_LOC_ON_CHIP)
  200. *cr_lcd |= head ? 0x0 : 0x8;
  201. else {
  202. *cr_lcd |= (nv_encoder->dcb->or << 4) & 0x30;
  203. if (nv_encoder->dcb->type == OUTPUT_LVDS)
  204. *cr_lcd |= 0x30;
  205. if ((*cr_lcd & 0x30) == (*cr_lcd_oth & 0x30)) {
  206. /* avoid being connected to both crtcs */
  207. *cr_lcd_oth &= ~0x30;
  208. NVWriteVgaCrtc(dev, head ^ 1,
  209. NV_CIO_CRE_LCD__INDEX,
  210. *cr_lcd_oth);
  211. }
  212. }
  213. }
  214. }
  215. }
  216. static void nv04_dfp_mode_set(struct drm_encoder *encoder,
  217. struct drm_display_mode *mode,
  218. struct drm_display_mode *adjusted_mode)
  219. {
  220. struct drm_device *dev = encoder->dev;
  221. struct drm_nouveau_private *dev_priv = dev->dev_private;
  222. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  223. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  224. struct nv04_crtc_reg *savep = &dev_priv->saved_reg.crtc_reg[nv_crtc->index];
  225. struct nouveau_connector *nv_connector = nouveau_crtc_connector_get(nv_crtc);
  226. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  227. struct drm_display_mode *output_mode = &nv_encoder->mode;
  228. uint32_t mode_ratio, panel_ratio;
  229. NV_DEBUG(dev, "Output mode on CRTC %d:\n", nv_crtc->index);
  230. drm_mode_debug_printmodeline(output_mode);
  231. /* Initialize the FP registers in this CRTC. */
  232. regp->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
  233. regp->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
  234. if (!nv_gf4_disp_arch(dev) ||
  235. (output_mode->hsync_start - output_mode->hdisplay) >=
  236. dev_priv->vbios->digital_min_front_porch)
  237. regp->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay;
  238. else
  239. regp->fp_horiz_regs[FP_CRTC] = output_mode->hsync_start - dev_priv->vbios->digital_min_front_porch - 1;
  240. regp->fp_horiz_regs[FP_SYNC_START] = output_mode->hsync_start - 1;
  241. regp->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
  242. regp->fp_horiz_regs[FP_VALID_START] = output_mode->hskew;
  243. regp->fp_horiz_regs[FP_VALID_END] = output_mode->hdisplay - 1;
  244. regp->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
  245. regp->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
  246. regp->fp_vert_regs[FP_CRTC] = output_mode->vtotal - 5 - 1;
  247. regp->fp_vert_regs[FP_SYNC_START] = output_mode->vsync_start - 1;
  248. regp->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
  249. regp->fp_vert_regs[FP_VALID_START] = 0;
  250. regp->fp_vert_regs[FP_VALID_END] = output_mode->vdisplay - 1;
  251. /* bit26: a bit seen on some g7x, no as yet discernable purpose */
  252. regp->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
  253. (savep->fp_control & (1 << 26 | NV_PRAMDAC_FP_TG_CONTROL_READ_PROG));
  254. /* Deal with vsync/hsync polarity */
  255. /* LVDS screens do set this, but modes with +ve syncs are very rare */
  256. if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
  257. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
  258. if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
  259. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
  260. /* panel scaling first, as native would get set otherwise */
  261. if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE ||
  262. nv_connector->scaling_mode == DRM_MODE_SCALE_CENTER) /* panel handles it */
  263. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_CENTER;
  264. else if (adjusted_mode->hdisplay == output_mode->hdisplay &&
  265. adjusted_mode->vdisplay == output_mode->vdisplay) /* native mode */
  266. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_NATIVE;
  267. else /* gpu needs to scale */
  268. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_SCALE;
  269. if (nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) & NV_PEXTDEV_BOOT_0_STRAP_FP_IFACE_12BIT)
  270. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
  271. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP &&
  272. output_mode->clock > 165000)
  273. regp->fp_control |= (2 << 24);
  274. if (nv_encoder->dcb->type == OUTPUT_LVDS) {
  275. bool duallink, dummy;
  276. nouveau_bios_parse_lvds_table(dev, nv_connector->native_mode->
  277. clock, &duallink, &dummy);
  278. if (duallink)
  279. regp->fp_control |= (8 << 28);
  280. } else
  281. if (output_mode->clock > 165000)
  282. regp->fp_control |= (8 << 28);
  283. regp->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
  284. NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
  285. NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
  286. NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
  287. NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
  288. NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
  289. NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
  290. /* We want automatic scaling */
  291. regp->fp_debug_1 = 0;
  292. /* This can override HTOTAL and VTOTAL */
  293. regp->fp_debug_2 = 0;
  294. /* Use 20.12 fixed point format to avoid floats */
  295. mode_ratio = (1 << 12) * adjusted_mode->hdisplay / adjusted_mode->vdisplay;
  296. panel_ratio = (1 << 12) * output_mode->hdisplay / output_mode->vdisplay;
  297. /* if ratios are equal, SCALE_ASPECT will automatically (and correctly)
  298. * get treated the same as SCALE_FULLSCREEN */
  299. if (nv_connector->scaling_mode == DRM_MODE_SCALE_ASPECT &&
  300. mode_ratio != panel_ratio) {
  301. uint32_t diff, scale;
  302. bool divide_by_2 = nv_gf4_disp_arch(dev);
  303. if (mode_ratio < panel_ratio) {
  304. /* vertical needs to expand to glass size (automatic)
  305. * horizontal needs to be scaled at vertical scale factor
  306. * to maintain aspect */
  307. scale = (1 << 12) * adjusted_mode->vdisplay / output_mode->vdisplay;
  308. regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_XSCALE_TESTMODE_ENABLE |
  309. XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_XSCALE_VALUE);
  310. /* restrict area of screen used, horizontally */
  311. diff = output_mode->hdisplay -
  312. output_mode->vdisplay * mode_ratio / (1 << 12);
  313. regp->fp_horiz_regs[FP_VALID_START] += diff / 2;
  314. regp->fp_horiz_regs[FP_VALID_END] -= diff / 2;
  315. }
  316. if (mode_ratio > panel_ratio) {
  317. /* horizontal needs to expand to glass size (automatic)
  318. * vertical needs to be scaled at horizontal scale factor
  319. * to maintain aspect */
  320. scale = (1 << 12) * adjusted_mode->hdisplay / output_mode->hdisplay;
  321. regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_YSCALE_TESTMODE_ENABLE |
  322. XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_YSCALE_VALUE);
  323. /* restrict area of screen used, vertically */
  324. diff = output_mode->vdisplay -
  325. (1 << 12) * output_mode->hdisplay / mode_ratio;
  326. regp->fp_vert_regs[FP_VALID_START] += diff / 2;
  327. regp->fp_vert_regs[FP_VALID_END] -= diff / 2;
  328. }
  329. }
  330. /* Output property. */
  331. if (nv_connector->use_dithering) {
  332. if (dev_priv->chipset == 0x11)
  333. regp->dither = savep->dither | 0x00010000;
  334. else {
  335. int i;
  336. regp->dither = savep->dither | 0x00000001;
  337. for (i = 0; i < 3; i++) {
  338. regp->dither_regs[i] = 0xe4e4e4e4;
  339. regp->dither_regs[i + 3] = 0x44444444;
  340. }
  341. }
  342. } else {
  343. if (dev_priv->chipset != 0x11) {
  344. /* reset them */
  345. int i;
  346. for (i = 0; i < 3; i++) {
  347. regp->dither_regs[i] = savep->dither_regs[i];
  348. regp->dither_regs[i + 3] = savep->dither_regs[i + 3];
  349. }
  350. }
  351. regp->dither = savep->dither;
  352. }
  353. regp->fp_margin_color = 0;
  354. }
  355. static void nv04_dfp_commit(struct drm_encoder *encoder)
  356. {
  357. struct drm_device *dev = encoder->dev;
  358. struct drm_nouveau_private *dev_priv = dev->dev_private;
  359. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  360. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  361. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  362. struct dcb_entry *dcbe = nv_encoder->dcb;
  363. int head = nouveau_crtc(encoder->crtc)->index;
  364. NV_TRACE(dev, "%s called for encoder %d\n", __func__, nv_encoder->dcb->index);
  365. if (dcbe->type == OUTPUT_TMDS)
  366. run_tmds_table(dev, dcbe, head, nv_encoder->mode.clock);
  367. else if (dcbe->type == OUTPUT_LVDS)
  368. call_lvds_script(dev, dcbe, head, LVDS_RESET, nv_encoder->mode.clock);
  369. /* update fp_control state for any changes made by scripts,
  370. * so correct value is written at DPMS on */
  371. dev_priv->mode_reg.crtc_reg[head].fp_control =
  372. NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
  373. /* This could use refinement for flatpanels, but it should work this way */
  374. if (dev_priv->chipset < 0x44)
  375. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0xf0000000);
  376. else
  377. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0x00100000);
  378. helper->dpms(encoder, DRM_MODE_DPMS_ON);
  379. NV_INFO(dev, "Output %s is running on CRTC %d using output %c\n",
  380. drm_get_connector_name(&nouveau_encoder_connector_get(nv_encoder)->base),
  381. nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
  382. }
  383. static inline bool is_powersaving_dpms(int mode)
  384. {
  385. return (mode != DRM_MODE_DPMS_ON);
  386. }
  387. static void nv04_lvds_dpms(struct drm_encoder *encoder, int mode)
  388. {
  389. struct drm_device *dev = encoder->dev;
  390. struct drm_crtc *crtc = encoder->crtc;
  391. struct drm_nouveau_private *dev_priv = dev->dev_private;
  392. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  393. bool was_powersaving = is_powersaving_dpms(nv_encoder->last_dpms);
  394. if (nv_encoder->last_dpms == mode)
  395. return;
  396. nv_encoder->last_dpms = mode;
  397. NV_INFO(dev, "Setting dpms mode %d on lvds encoder (output %d)\n",
  398. mode, nv_encoder->dcb->index);
  399. if (was_powersaving && is_powersaving_dpms(mode))
  400. return;
  401. if (nv_encoder->dcb->lvdsconf.use_power_scripts) {
  402. struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
  403. /* when removing an output, crtc may not be set, but PANEL_OFF
  404. * must still be run
  405. */
  406. int head = crtc ? nouveau_crtc(crtc)->index :
  407. nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
  408. if (mode == DRM_MODE_DPMS_ON) {
  409. if (!nv_connector->native_mode) {
  410. NV_ERROR(dev, "Not turning on LVDS without native mode\n");
  411. return;
  412. }
  413. call_lvds_script(dev, nv_encoder->dcb, head,
  414. LVDS_PANEL_ON, nv_connector->native_mode->clock);
  415. } else
  416. /* pxclk of 0 is fine for PANEL_OFF, and for a
  417. * disconnected LVDS encoder there is no native_mode
  418. */
  419. call_lvds_script(dev, nv_encoder->dcb, head,
  420. LVDS_PANEL_OFF, 0);
  421. }
  422. nv04_dfp_update_fp_control(encoder, mode);
  423. if (mode == DRM_MODE_DPMS_ON)
  424. nv04_dfp_prepare_sel_clk(dev, nv_encoder, nouveau_crtc(crtc)->index);
  425. else {
  426. dev_priv->mode_reg.sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK);
  427. dev_priv->mode_reg.sel_clk &= ~0xf0;
  428. }
  429. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, dev_priv->mode_reg.sel_clk);
  430. }
  431. static void nv04_tmds_dpms(struct drm_encoder *encoder, int mode)
  432. {
  433. struct drm_device *dev = encoder->dev;
  434. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  435. if (nv_encoder->last_dpms == mode)
  436. return;
  437. nv_encoder->last_dpms = mode;
  438. NV_INFO(dev, "Setting dpms mode %d on tmds encoder (output %d)\n",
  439. mode, nv_encoder->dcb->index);
  440. nv04_dfp_update_fp_control(encoder, mode);
  441. }
  442. static void nv04_dfp_save(struct drm_encoder *encoder)
  443. {
  444. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  445. struct drm_device *dev = encoder->dev;
  446. if (nv_two_heads(dev))
  447. nv_encoder->restore.head =
  448. nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
  449. }
  450. static void nv04_dfp_restore(struct drm_encoder *encoder)
  451. {
  452. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  453. struct drm_device *dev = encoder->dev;
  454. struct drm_nouveau_private *dev_priv = dev->dev_private;
  455. int head = nv_encoder->restore.head;
  456. if (nv_encoder->dcb->type == OUTPUT_LVDS) {
  457. struct drm_display_mode *native_mode = nouveau_encoder_connector_get(nv_encoder)->native_mode;
  458. if (native_mode)
  459. call_lvds_script(dev, nv_encoder->dcb, head, LVDS_PANEL_ON,
  460. native_mode->clock);
  461. else
  462. NV_ERROR(dev, "Not restoring LVDS without native mode\n");
  463. } else if (nv_encoder->dcb->type == OUTPUT_TMDS) {
  464. int clock = nouveau_hw_pllvals_to_clk
  465. (&dev_priv->saved_reg.crtc_reg[head].pllvals);
  466. run_tmds_table(dev, nv_encoder->dcb, head, clock);
  467. }
  468. nv_encoder->last_dpms = NV_DPMS_CLEARED;
  469. }
  470. static void nv04_dfp_destroy(struct drm_encoder *encoder)
  471. {
  472. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  473. NV_DEBUG(encoder->dev, "\n");
  474. drm_encoder_cleanup(encoder);
  475. kfree(nv_encoder);
  476. }
  477. static const struct drm_encoder_helper_funcs nv04_lvds_helper_funcs = {
  478. .dpms = nv04_lvds_dpms,
  479. .save = nv04_dfp_save,
  480. .restore = nv04_dfp_restore,
  481. .mode_fixup = nv04_dfp_mode_fixup,
  482. .prepare = nv04_dfp_prepare,
  483. .commit = nv04_dfp_commit,
  484. .mode_set = nv04_dfp_mode_set,
  485. .detect = NULL,
  486. };
  487. static const struct drm_encoder_helper_funcs nv04_tmds_helper_funcs = {
  488. .dpms = nv04_tmds_dpms,
  489. .save = nv04_dfp_save,
  490. .restore = nv04_dfp_restore,
  491. .mode_fixup = nv04_dfp_mode_fixup,
  492. .prepare = nv04_dfp_prepare,
  493. .commit = nv04_dfp_commit,
  494. .mode_set = nv04_dfp_mode_set,
  495. .detect = NULL,
  496. };
  497. static const struct drm_encoder_funcs nv04_dfp_funcs = {
  498. .destroy = nv04_dfp_destroy,
  499. };
  500. int nv04_dfp_create(struct drm_device *dev, struct dcb_entry *entry)
  501. {
  502. const struct drm_encoder_helper_funcs *helper;
  503. struct drm_encoder *encoder;
  504. struct nouveau_encoder *nv_encoder = NULL;
  505. int type;
  506. switch (entry->type) {
  507. case OUTPUT_TMDS:
  508. type = DRM_MODE_ENCODER_TMDS;
  509. helper = &nv04_tmds_helper_funcs;
  510. break;
  511. case OUTPUT_LVDS:
  512. type = DRM_MODE_ENCODER_LVDS;
  513. helper = &nv04_lvds_helper_funcs;
  514. break;
  515. default:
  516. return -EINVAL;
  517. }
  518. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  519. if (!nv_encoder)
  520. return -ENOMEM;
  521. encoder = to_drm_encoder(nv_encoder);
  522. nv_encoder->dcb = entry;
  523. nv_encoder->or = ffs(entry->or) - 1;
  524. drm_encoder_init(dev, encoder, &nv04_dfp_funcs, type);
  525. drm_encoder_helper_add(encoder, helper);
  526. encoder->possible_crtcs = entry->heads;
  527. encoder->possible_clones = 0;
  528. return 0;
  529. }