nv04_dfp.c 24 KB

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