nv04_dfp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 struct drm_encoder *get_tmds_slave(struct drm_encoder *encoder)
  129. {
  130. struct drm_device *dev = encoder->dev;
  131. struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
  132. struct drm_encoder *slave;
  133. if (dcb->type != OUTPUT_TMDS || dcb->location == DCB_LOC_ON_CHIP)
  134. return NULL;
  135. /* Some BIOSes (e.g. the one in a Quadro FX1000) report several
  136. * TMDS transmitters at the same I2C address, in the same I2C
  137. * bus. This can still work because in that case one of them is
  138. * always hard-wired to a reasonable configuration using straps,
  139. * and the other one needs to be programmed.
  140. *
  141. * I don't think there's a way to know which is which, even the
  142. * blob programs the one exposed via I2C for *both* heads, so
  143. * let's do the same.
  144. */
  145. list_for_each_entry(slave, &dev->mode_config.encoder_list, head) {
  146. struct dcb_entry *slave_dcb = nouveau_encoder(slave)->dcb;
  147. if (slave_dcb->type == OUTPUT_TMDS && get_slave_funcs(slave) &&
  148. slave_dcb->tmdsconf.slave_addr == dcb->tmdsconf.slave_addr)
  149. return slave;
  150. }
  151. return NULL;
  152. }
  153. static bool nv04_dfp_mode_fixup(struct drm_encoder *encoder,
  154. struct drm_display_mode *mode,
  155. struct drm_display_mode *adjusted_mode)
  156. {
  157. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  158. struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
  159. /* For internal panels and gpu scaling on DVI we need the native mode */
  160. if (nv_connector->scaling_mode != DRM_MODE_SCALE_NONE) {
  161. if (!nv_connector->native_mode)
  162. return false;
  163. nv_encoder->mode = *nv_connector->native_mode;
  164. adjusted_mode->clock = nv_connector->native_mode->clock;
  165. } else {
  166. nv_encoder->mode = *adjusted_mode;
  167. }
  168. return true;
  169. }
  170. static void nv04_dfp_prepare_sel_clk(struct drm_device *dev,
  171. struct nouveau_encoder *nv_encoder, int head)
  172. {
  173. struct drm_nouveau_private *dev_priv = dev->dev_private;
  174. struct nv04_mode_state *state = &dev_priv->mode_reg;
  175. uint32_t bits1618 = nv_encoder->dcb->or & OUTPUT_A ? 0x10000 : 0x40000;
  176. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP)
  177. return;
  178. /* SEL_CLK is only used on the primary ramdac
  179. * It toggles spread spectrum PLL output and sets the bindings of PLLs
  180. * to heads on digital outputs
  181. */
  182. if (head)
  183. state->sel_clk |= bits1618;
  184. else
  185. state->sel_clk &= ~bits1618;
  186. /* nv30:
  187. * bit 0 NVClk spread spectrum on/off
  188. * bit 2 MemClk spread spectrum on/off
  189. * bit 4 PixClk1 spread spectrum on/off toggle
  190. * bit 6 PixClk2 spread spectrum on/off toggle
  191. *
  192. * nv40 (observations from bios behaviour and mmio traces):
  193. * bits 4&6 as for nv30
  194. * bits 5&7 head dependent as for bits 4&6, but do not appear with 4&6;
  195. * maybe a different spread mode
  196. * bits 8&10 seen on dual-link dvi outputs, purpose unknown (set by POST scripts)
  197. * The logic behind turning spread spectrum on/off in the first place,
  198. * and which bit-pair to use, is unclear on nv40 (for earlier cards, the fp table
  199. * entry has the necessary info)
  200. */
  201. if (nv_encoder->dcb->type == OUTPUT_LVDS && dev_priv->saved_reg.sel_clk & 0xf0) {
  202. int shift = (dev_priv->saved_reg.sel_clk & 0x50) ? 0 : 1;
  203. state->sel_clk &= ~0xf0;
  204. state->sel_clk |= (head ? 0x40 : 0x10) << shift;
  205. }
  206. }
  207. static void nv04_dfp_prepare(struct drm_encoder *encoder)
  208. {
  209. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  210. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  211. struct drm_device *dev = encoder->dev;
  212. struct drm_nouveau_private *dev_priv = dev->dev_private;
  213. int head = nouveau_crtc(encoder->crtc)->index;
  214. struct nv04_crtc_reg *crtcstate = dev_priv->mode_reg.crtc_reg;
  215. uint8_t *cr_lcd = &crtcstate[head].CRTC[NV_CIO_CRE_LCD__INDEX];
  216. uint8_t *cr_lcd_oth = &crtcstate[head ^ 1].CRTC[NV_CIO_CRE_LCD__INDEX];
  217. helper->dpms(encoder, DRM_MODE_DPMS_OFF);
  218. nv04_dfp_prepare_sel_clk(dev, nv_encoder, head);
  219. /* Some NV4x have unknown values (0x3f, 0x50, 0x54, 0x6b, 0x79, 0x7f)
  220. * at LCD__INDEX which we don't alter
  221. */
  222. if (!(*cr_lcd & 0x44)) {
  223. *cr_lcd = 0x3;
  224. if (nv_two_heads(dev)) {
  225. if (nv_encoder->dcb->location == DCB_LOC_ON_CHIP)
  226. *cr_lcd |= head ? 0x0 : 0x8;
  227. else {
  228. *cr_lcd |= (nv_encoder->dcb->or << 4) & 0x30;
  229. if (nv_encoder->dcb->type == OUTPUT_LVDS)
  230. *cr_lcd |= 0x30;
  231. if ((*cr_lcd & 0x30) == (*cr_lcd_oth & 0x30)) {
  232. /* avoid being connected to both crtcs */
  233. *cr_lcd_oth &= ~0x30;
  234. NVWriteVgaCrtc(dev, head ^ 1,
  235. NV_CIO_CRE_LCD__INDEX,
  236. *cr_lcd_oth);
  237. }
  238. }
  239. }
  240. }
  241. }
  242. static void nv04_dfp_mode_set(struct drm_encoder *encoder,
  243. struct drm_display_mode *mode,
  244. struct drm_display_mode *adjusted_mode)
  245. {
  246. struct drm_device *dev = encoder->dev;
  247. struct drm_nouveau_private *dev_priv = dev->dev_private;
  248. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  249. struct nv04_crtc_reg *regp = &dev_priv->mode_reg.crtc_reg[nv_crtc->index];
  250. struct nv04_crtc_reg *savep = &dev_priv->saved_reg.crtc_reg[nv_crtc->index];
  251. struct nouveau_connector *nv_connector = nouveau_crtc_connector_get(nv_crtc);
  252. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  253. struct drm_display_mode *output_mode = &nv_encoder->mode;
  254. uint32_t mode_ratio, panel_ratio;
  255. NV_DEBUG_KMS(dev, "Output mode on CRTC %d:\n", nv_crtc->index);
  256. drm_mode_debug_printmodeline(output_mode);
  257. /* Initialize the FP registers in this CRTC. */
  258. regp->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
  259. regp->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
  260. if (!nv_gf4_disp_arch(dev) ||
  261. (output_mode->hsync_start - output_mode->hdisplay) >=
  262. dev_priv->vbios.digital_min_front_porch)
  263. regp->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay;
  264. else
  265. regp->fp_horiz_regs[FP_CRTC] = output_mode->hsync_start - dev_priv->vbios.digital_min_front_porch - 1;
  266. regp->fp_horiz_regs[FP_SYNC_START] = output_mode->hsync_start - 1;
  267. regp->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
  268. regp->fp_horiz_regs[FP_VALID_START] = output_mode->hskew;
  269. regp->fp_horiz_regs[FP_VALID_END] = output_mode->hdisplay - 1;
  270. regp->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
  271. regp->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
  272. regp->fp_vert_regs[FP_CRTC] = output_mode->vtotal - 5 - 1;
  273. regp->fp_vert_regs[FP_SYNC_START] = output_mode->vsync_start - 1;
  274. regp->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
  275. regp->fp_vert_regs[FP_VALID_START] = 0;
  276. regp->fp_vert_regs[FP_VALID_END] = output_mode->vdisplay - 1;
  277. /* bit26: a bit seen on some g7x, no as yet discernable purpose */
  278. regp->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
  279. (savep->fp_control & (1 << 26 | NV_PRAMDAC_FP_TG_CONTROL_READ_PROG));
  280. /* Deal with vsync/hsync polarity */
  281. /* LVDS screens do set this, but modes with +ve syncs are very rare */
  282. if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
  283. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
  284. if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
  285. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
  286. /* panel scaling first, as native would get set otherwise */
  287. if (nv_connector->scaling_mode == DRM_MODE_SCALE_NONE ||
  288. nv_connector->scaling_mode == DRM_MODE_SCALE_CENTER) /* panel handles it */
  289. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_CENTER;
  290. else if (adjusted_mode->hdisplay == output_mode->hdisplay &&
  291. adjusted_mode->vdisplay == output_mode->vdisplay) /* native mode */
  292. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_NATIVE;
  293. else /* gpu needs to scale */
  294. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_MODE_SCALE;
  295. if (nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) & NV_PEXTDEV_BOOT_0_STRAP_FP_IFACE_12BIT)
  296. regp->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
  297. if (nv_encoder->dcb->location != DCB_LOC_ON_CHIP &&
  298. output_mode->clock > 165000)
  299. regp->fp_control |= (2 << 24);
  300. if (nv_encoder->dcb->type == OUTPUT_LVDS) {
  301. bool duallink, dummy;
  302. nouveau_bios_parse_lvds_table(dev, nv_connector->native_mode->
  303. clock, &duallink, &dummy);
  304. if (duallink)
  305. regp->fp_control |= (8 << 28);
  306. } else
  307. if (output_mode->clock > 165000)
  308. regp->fp_control |= (8 << 28);
  309. regp->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
  310. NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
  311. NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
  312. NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
  313. NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
  314. NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
  315. NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
  316. /* We want automatic scaling */
  317. regp->fp_debug_1 = 0;
  318. /* This can override HTOTAL and VTOTAL */
  319. regp->fp_debug_2 = 0;
  320. /* Use 20.12 fixed point format to avoid floats */
  321. mode_ratio = (1 << 12) * adjusted_mode->hdisplay / adjusted_mode->vdisplay;
  322. panel_ratio = (1 << 12) * output_mode->hdisplay / output_mode->vdisplay;
  323. /* if ratios are equal, SCALE_ASPECT will automatically (and correctly)
  324. * get treated the same as SCALE_FULLSCREEN */
  325. if (nv_connector->scaling_mode == DRM_MODE_SCALE_ASPECT &&
  326. mode_ratio != panel_ratio) {
  327. uint32_t diff, scale;
  328. bool divide_by_2 = nv_gf4_disp_arch(dev);
  329. if (mode_ratio < panel_ratio) {
  330. /* vertical needs to expand to glass size (automatic)
  331. * horizontal needs to be scaled at vertical scale factor
  332. * to maintain aspect */
  333. scale = (1 << 12) * adjusted_mode->vdisplay / output_mode->vdisplay;
  334. regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_XSCALE_TESTMODE_ENABLE |
  335. XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_XSCALE_VALUE);
  336. /* restrict area of screen used, horizontally */
  337. diff = output_mode->hdisplay -
  338. output_mode->vdisplay * mode_ratio / (1 << 12);
  339. regp->fp_horiz_regs[FP_VALID_START] += diff / 2;
  340. regp->fp_horiz_regs[FP_VALID_END] -= diff / 2;
  341. }
  342. if (mode_ratio > panel_ratio) {
  343. /* horizontal needs to expand to glass size (automatic)
  344. * vertical needs to be scaled at horizontal scale factor
  345. * to maintain aspect */
  346. scale = (1 << 12) * adjusted_mode->hdisplay / output_mode->hdisplay;
  347. regp->fp_debug_1 = NV_PRAMDAC_FP_DEBUG_1_YSCALE_TESTMODE_ENABLE |
  348. XLATE(scale, divide_by_2, NV_PRAMDAC_FP_DEBUG_1_YSCALE_VALUE);
  349. /* restrict area of screen used, vertically */
  350. diff = output_mode->vdisplay -
  351. (1 << 12) * output_mode->hdisplay / mode_ratio;
  352. regp->fp_vert_regs[FP_VALID_START] += diff / 2;
  353. regp->fp_vert_regs[FP_VALID_END] -= diff / 2;
  354. }
  355. }
  356. /* Output property. */
  357. if (nv_connector->use_dithering) {
  358. if (dev_priv->chipset == 0x11)
  359. regp->dither = savep->dither | 0x00010000;
  360. else {
  361. int i;
  362. regp->dither = savep->dither | 0x00000001;
  363. for (i = 0; i < 3; i++) {
  364. regp->dither_regs[i] = 0xe4e4e4e4;
  365. regp->dither_regs[i + 3] = 0x44444444;
  366. }
  367. }
  368. } else {
  369. if (dev_priv->chipset != 0x11) {
  370. /* reset them */
  371. int i;
  372. for (i = 0; i < 3; i++) {
  373. regp->dither_regs[i] = savep->dither_regs[i];
  374. regp->dither_regs[i + 3] = savep->dither_regs[i + 3];
  375. }
  376. }
  377. regp->dither = savep->dither;
  378. }
  379. regp->fp_margin_color = 0;
  380. }
  381. static void nv04_dfp_commit(struct drm_encoder *encoder)
  382. {
  383. struct drm_device *dev = encoder->dev;
  384. struct drm_nouveau_private *dev_priv = dev->dev_private;
  385. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  386. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  387. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  388. struct dcb_entry *dcbe = nv_encoder->dcb;
  389. int head = nouveau_crtc(encoder->crtc)->index;
  390. if (dcbe->type == OUTPUT_TMDS)
  391. run_tmds_table(dev, dcbe, head, nv_encoder->mode.clock);
  392. else if (dcbe->type == OUTPUT_LVDS)
  393. call_lvds_script(dev, dcbe, head, LVDS_RESET, nv_encoder->mode.clock);
  394. /* update fp_control state for any changes made by scripts,
  395. * so correct value is written at DPMS on */
  396. dev_priv->mode_reg.crtc_reg[head].fp_control =
  397. NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
  398. /* This could use refinement for flatpanels, but it should work this way */
  399. if (dev_priv->chipset < 0x44)
  400. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0xf0000000);
  401. else
  402. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0x00100000);
  403. /* Init external transmitters */
  404. if (get_tmds_slave(encoder))
  405. get_slave_funcs(get_tmds_slave(encoder))->mode_set(
  406. encoder, &nv_encoder->mode, &nv_encoder->mode);
  407. helper->dpms(encoder, DRM_MODE_DPMS_ON);
  408. NV_INFO(dev, "Output %s is running on CRTC %d using output %c\n",
  409. drm_get_connector_name(&nouveau_encoder_connector_get(nv_encoder)->base),
  410. nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
  411. }
  412. static inline bool is_powersaving_dpms(int mode)
  413. {
  414. return (mode != DRM_MODE_DPMS_ON);
  415. }
  416. static void nv04_lvds_dpms(struct drm_encoder *encoder, int mode)
  417. {
  418. struct drm_device *dev = encoder->dev;
  419. struct drm_crtc *crtc = encoder->crtc;
  420. struct drm_nouveau_private *dev_priv = dev->dev_private;
  421. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  422. bool was_powersaving = is_powersaving_dpms(nv_encoder->last_dpms);
  423. if (nv_encoder->last_dpms == mode)
  424. return;
  425. nv_encoder->last_dpms = mode;
  426. NV_INFO(dev, "Setting dpms mode %d on lvds encoder (output %d)\n",
  427. mode, nv_encoder->dcb->index);
  428. if (was_powersaving && is_powersaving_dpms(mode))
  429. return;
  430. if (nv_encoder->dcb->lvdsconf.use_power_scripts) {
  431. struct nouveau_connector *nv_connector = nouveau_encoder_connector_get(nv_encoder);
  432. /* when removing an output, crtc may not be set, but PANEL_OFF
  433. * must still be run
  434. */
  435. int head = crtc ? nouveau_crtc(crtc)->index :
  436. nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
  437. if (mode == DRM_MODE_DPMS_ON) {
  438. if (!nv_connector->native_mode) {
  439. NV_ERROR(dev, "Not turning on LVDS without native mode\n");
  440. return;
  441. }
  442. call_lvds_script(dev, nv_encoder->dcb, head,
  443. LVDS_PANEL_ON, nv_connector->native_mode->clock);
  444. } else
  445. /* pxclk of 0 is fine for PANEL_OFF, and for a
  446. * disconnected LVDS encoder there is no native_mode
  447. */
  448. call_lvds_script(dev, nv_encoder->dcb, head,
  449. LVDS_PANEL_OFF, 0);
  450. }
  451. nv04_dfp_update_fp_control(encoder, mode);
  452. if (mode == DRM_MODE_DPMS_ON)
  453. nv04_dfp_prepare_sel_clk(dev, nv_encoder, nouveau_crtc(crtc)->index);
  454. else {
  455. dev_priv->mode_reg.sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK);
  456. dev_priv->mode_reg.sel_clk &= ~0xf0;
  457. }
  458. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, dev_priv->mode_reg.sel_clk);
  459. }
  460. static void nv04_tmds_dpms(struct drm_encoder *encoder, int mode)
  461. {
  462. struct drm_device *dev = encoder->dev;
  463. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  464. if (nv_encoder->last_dpms == mode)
  465. return;
  466. nv_encoder->last_dpms = mode;
  467. NV_INFO(dev, "Setting dpms mode %d on tmds encoder (output %d)\n",
  468. mode, nv_encoder->dcb->index);
  469. nv04_dfp_update_fp_control(encoder, mode);
  470. }
  471. static void nv04_dfp_save(struct drm_encoder *encoder)
  472. {
  473. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  474. struct drm_device *dev = encoder->dev;
  475. if (nv_two_heads(dev))
  476. nv_encoder->restore.head =
  477. nv04_dfp_get_bound_head(dev, nv_encoder->dcb);
  478. }
  479. static void nv04_dfp_restore(struct drm_encoder *encoder)
  480. {
  481. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  482. struct drm_device *dev = encoder->dev;
  483. struct drm_nouveau_private *dev_priv = dev->dev_private;
  484. int head = nv_encoder->restore.head;
  485. if (nv_encoder->dcb->type == OUTPUT_LVDS) {
  486. struct drm_display_mode *native_mode = nouveau_encoder_connector_get(nv_encoder)->native_mode;
  487. if (native_mode)
  488. call_lvds_script(dev, nv_encoder->dcb, head, LVDS_PANEL_ON,
  489. native_mode->clock);
  490. else
  491. NV_ERROR(dev, "Not restoring LVDS without native mode\n");
  492. } else if (nv_encoder->dcb->type == OUTPUT_TMDS) {
  493. int clock = nouveau_hw_pllvals_to_clk
  494. (&dev_priv->saved_reg.crtc_reg[head].pllvals);
  495. run_tmds_table(dev, nv_encoder->dcb, head, clock);
  496. }
  497. nv_encoder->last_dpms = NV_DPMS_CLEARED;
  498. }
  499. static void nv04_dfp_destroy(struct drm_encoder *encoder)
  500. {
  501. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  502. NV_DEBUG_KMS(encoder->dev, "\n");
  503. if (get_slave_funcs(encoder))
  504. get_slave_funcs(encoder)->destroy(encoder);
  505. drm_encoder_cleanup(encoder);
  506. kfree(nv_encoder);
  507. }
  508. static void nv04_tmds_slave_init(struct drm_encoder *encoder)
  509. {
  510. struct drm_device *dev = encoder->dev;
  511. struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
  512. struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, 2);
  513. struct i2c_board_info info[] = {
  514. {
  515. .type = "sil164",
  516. .addr = (dcb->tmdsconf.slave_addr == 0x7 ? 0x3a : 0x38),
  517. .platform_data = &(struct sil164_encoder_params) {
  518. SIL164_INPUT_EDGE_RISING
  519. }
  520. },
  521. { }
  522. };
  523. int type;
  524. if (!nv_gf4_disp_arch(dev) || !i2c ||
  525. get_tmds_slave(encoder))
  526. return;
  527. type = nouveau_i2c_identify(dev, "TMDS transmitter", info, 2);
  528. if (type < 0)
  529. return;
  530. drm_i2c_encoder_init(dev, to_encoder_slave(encoder),
  531. &i2c->adapter, &info[type]);
  532. }
  533. static const struct drm_encoder_helper_funcs nv04_lvds_helper_funcs = {
  534. .dpms = nv04_lvds_dpms,
  535. .save = nv04_dfp_save,
  536. .restore = nv04_dfp_restore,
  537. .mode_fixup = nv04_dfp_mode_fixup,
  538. .prepare = nv04_dfp_prepare,
  539. .commit = nv04_dfp_commit,
  540. .mode_set = nv04_dfp_mode_set,
  541. .detect = NULL,
  542. };
  543. static const struct drm_encoder_helper_funcs nv04_tmds_helper_funcs = {
  544. .dpms = nv04_tmds_dpms,
  545. .save = nv04_dfp_save,
  546. .restore = nv04_dfp_restore,
  547. .mode_fixup = nv04_dfp_mode_fixup,
  548. .prepare = nv04_dfp_prepare,
  549. .commit = nv04_dfp_commit,
  550. .mode_set = nv04_dfp_mode_set,
  551. .detect = NULL,
  552. };
  553. static const struct drm_encoder_funcs nv04_dfp_funcs = {
  554. .destroy = nv04_dfp_destroy,
  555. };
  556. int
  557. nv04_dfp_create(struct drm_connector *connector, struct dcb_entry *entry)
  558. {
  559. const struct drm_encoder_helper_funcs *helper;
  560. struct nouveau_encoder *nv_encoder = NULL;
  561. struct drm_encoder *encoder;
  562. int type;
  563. switch (entry->type) {
  564. case OUTPUT_TMDS:
  565. type = DRM_MODE_ENCODER_TMDS;
  566. helper = &nv04_tmds_helper_funcs;
  567. break;
  568. case OUTPUT_LVDS:
  569. type = DRM_MODE_ENCODER_LVDS;
  570. helper = &nv04_lvds_helper_funcs;
  571. break;
  572. default:
  573. return -EINVAL;
  574. }
  575. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  576. if (!nv_encoder)
  577. return -ENOMEM;
  578. encoder = to_drm_encoder(nv_encoder);
  579. nv_encoder->dcb = entry;
  580. nv_encoder->or = ffs(entry->or) - 1;
  581. drm_encoder_init(connector->dev, encoder, &nv04_dfp_funcs, type);
  582. drm_encoder_helper_add(encoder, helper);
  583. encoder->possible_crtcs = entry->heads;
  584. encoder->possible_clones = 0;
  585. if (entry->type == OUTPUT_TMDS &&
  586. entry->location != DCB_LOC_ON_CHIP)
  587. nv04_tmds_slave_init(encoder);
  588. drm_mode_connector_attach_encoder(connector, encoder);
  589. return 0;
  590. }