dfp.c 24 KB

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