nv04_dfp.c 22 KB

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