nv17_tv.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Copyright (C) 2009 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  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 "nv17_tv.h"
  34. static uint32_t nv42_tv_sample_load(struct drm_encoder *encoder)
  35. {
  36. struct drm_device *dev = encoder->dev;
  37. struct drm_nouveau_private *dev_priv = dev->dev_private;
  38. struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
  39. uint32_t testval, regoffset = nv04_dac_output_offset(encoder);
  40. uint32_t gpio0, gpio1, fp_htotal, fp_hsync_start, fp_hsync_end,
  41. fp_control, test_ctrl, dacclk, ctv_14, ctv_1c, ctv_6c;
  42. uint32_t sample = 0;
  43. int head;
  44. #define RGB_TEST_DATA(r, g, b) (r << 0 | g << 10 | b << 20)
  45. testval = RGB_TEST_DATA(0x82, 0xeb, 0x82);
  46. if (dev_priv->vbios.tvdactestval)
  47. testval = dev_priv->vbios.tvdactestval;
  48. dacclk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset);
  49. head = (dacclk & 0x100) >> 8;
  50. /* Save the previous state. */
  51. gpio1 = gpio->get(dev, DCB_GPIO_TVDAC1);
  52. gpio0 = gpio->get(dev, DCB_GPIO_TVDAC0);
  53. fp_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL);
  54. fp_hsync_start = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START);
  55. fp_hsync_end = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END);
  56. fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
  57. test_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
  58. ctv_1c = NVReadRAMDAC(dev, head, 0x680c1c);
  59. ctv_14 = NVReadRAMDAC(dev, head, 0x680c14);
  60. ctv_6c = NVReadRAMDAC(dev, head, 0x680c6c);
  61. /* Prepare the DAC for load detection. */
  62. gpio->set(dev, DCB_GPIO_TVDAC1, true);
  63. gpio->set(dev, DCB_GPIO_TVDAC0, true);
  64. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, 1343);
  65. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, 1047);
  66. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, 1183);
  67. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL,
  68. NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
  69. NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12 |
  70. NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
  71. NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS |
  72. NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS);
  73. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, 0);
  74. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
  75. (dacclk & ~0xff) | 0x22);
  76. msleep(1);
  77. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
  78. (dacclk & ~0xff) | 0x21);
  79. NVWriteRAMDAC(dev, head, 0x680c1c, 1 << 20);
  80. NVWriteRAMDAC(dev, head, 0x680c14, 4 << 16);
  81. /* Sample pin 0x4 (usually S-video luma). */
  82. NVWriteRAMDAC(dev, head, 0x680c6c, testval >> 10 & 0x3ff);
  83. msleep(20);
  84. sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
  85. & 0x4 << 28;
  86. /* Sample the remaining pins. */
  87. NVWriteRAMDAC(dev, head, 0x680c6c, testval & 0x3ff);
  88. msleep(20);
  89. sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
  90. & 0xa << 28;
  91. /* Restore the previous state. */
  92. NVWriteRAMDAC(dev, head, 0x680c1c, ctv_1c);
  93. NVWriteRAMDAC(dev, head, 0x680c14, ctv_14);
  94. NVWriteRAMDAC(dev, head, 0x680c6c, ctv_6c);
  95. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, dacclk);
  96. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, test_ctrl);
  97. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, fp_control);
  98. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, fp_hsync_end);
  99. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, fp_hsync_start);
  100. NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, fp_htotal);
  101. gpio->set(dev, DCB_GPIO_TVDAC1, gpio1);
  102. gpio->set(dev, DCB_GPIO_TVDAC0, gpio0);
  103. return sample;
  104. }
  105. static bool
  106. get_tv_detect_quirks(struct drm_device *dev, uint32_t *pin_mask)
  107. {
  108. /* Zotac FX5200 */
  109. if (nv_match_device(dev, 0x0322, 0x19da, 0x1035) ||
  110. nv_match_device(dev, 0x0322, 0x19da, 0x2035)) {
  111. *pin_mask = 0xc;
  112. return false;
  113. }
  114. /* MSI nForce2 IGP */
  115. if (nv_match_device(dev, 0x01f0, 0x1462, 0x5710)) {
  116. *pin_mask = 0xc;
  117. return false;
  118. }
  119. return true;
  120. }
  121. static enum drm_connector_status
  122. nv17_tv_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  123. {
  124. struct drm_device *dev = encoder->dev;
  125. struct drm_nouveau_private *dev_priv = dev->dev_private;
  126. struct drm_mode_config *conf = &dev->mode_config;
  127. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  128. struct dcb_entry *dcb = tv_enc->base.dcb;
  129. bool reliable = get_tv_detect_quirks(dev, &tv_enc->pin_mask);
  130. if (nv04_dac_in_use(encoder))
  131. return connector_status_disconnected;
  132. if (reliable) {
  133. if (dev_priv->chipset == 0x42 ||
  134. dev_priv->chipset == 0x43)
  135. tv_enc->pin_mask =
  136. nv42_tv_sample_load(encoder) >> 28 & 0xe;
  137. else
  138. tv_enc->pin_mask =
  139. nv17_dac_sample_load(encoder) >> 28 & 0xe;
  140. }
  141. switch (tv_enc->pin_mask) {
  142. case 0x2:
  143. case 0x4:
  144. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Composite;
  145. break;
  146. case 0xc:
  147. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO;
  148. break;
  149. case 0xe:
  150. if (dcb->tvconf.has_component_output)
  151. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Component;
  152. else
  153. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SCART;
  154. break;
  155. default:
  156. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  157. break;
  158. }
  159. drm_connector_property_set_value(connector,
  160. conf->tv_subconnector_property,
  161. tv_enc->subconnector);
  162. if (!reliable) {
  163. return connector_status_unknown;
  164. } else if (tv_enc->subconnector) {
  165. NV_INFO(dev, "Load detected on output %c\n",
  166. '@' + ffs(dcb->or));
  167. return connector_status_connected;
  168. } else {
  169. return connector_status_disconnected;
  170. }
  171. }
  172. static int nv17_tv_get_ld_modes(struct drm_encoder *encoder,
  173. struct drm_connector *connector)
  174. {
  175. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  176. const struct drm_display_mode *tv_mode;
  177. int n = 0;
  178. for (tv_mode = nv17_tv_modes; tv_mode->hdisplay; tv_mode++) {
  179. struct drm_display_mode *mode;
  180. mode = drm_mode_duplicate(encoder->dev, tv_mode);
  181. mode->clock = tv_norm->tv_enc_mode.vrefresh *
  182. mode->htotal / 1000 *
  183. mode->vtotal / 1000;
  184. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  185. mode->clock *= 2;
  186. if (mode->hdisplay == tv_norm->tv_enc_mode.hdisplay &&
  187. mode->vdisplay == tv_norm->tv_enc_mode.vdisplay)
  188. mode->type |= DRM_MODE_TYPE_PREFERRED;
  189. drm_mode_probed_add(connector, mode);
  190. n++;
  191. }
  192. return n;
  193. }
  194. static int nv17_tv_get_hd_modes(struct drm_encoder *encoder,
  195. struct drm_connector *connector)
  196. {
  197. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  198. struct drm_display_mode *output_mode = &tv_norm->ctv_enc_mode.mode;
  199. struct drm_display_mode *mode;
  200. const struct {
  201. int hdisplay;
  202. int vdisplay;
  203. } modes[] = {
  204. { 640, 400 },
  205. { 640, 480 },
  206. { 720, 480 },
  207. { 720, 576 },
  208. { 800, 600 },
  209. { 1024, 768 },
  210. { 1280, 720 },
  211. { 1280, 1024 },
  212. { 1920, 1080 }
  213. };
  214. int i, n = 0;
  215. for (i = 0; i < ARRAY_SIZE(modes); i++) {
  216. if (modes[i].hdisplay > output_mode->hdisplay ||
  217. modes[i].vdisplay > output_mode->vdisplay)
  218. continue;
  219. if (modes[i].hdisplay == output_mode->hdisplay &&
  220. modes[i].vdisplay == output_mode->vdisplay) {
  221. mode = drm_mode_duplicate(encoder->dev, output_mode);
  222. mode->type |= DRM_MODE_TYPE_PREFERRED;
  223. } else {
  224. mode = drm_cvt_mode(encoder->dev, modes[i].hdisplay,
  225. modes[i].vdisplay, 60, false,
  226. (output_mode->flags &
  227. DRM_MODE_FLAG_INTERLACE), false);
  228. }
  229. /* CVT modes are sometimes unsuitable... */
  230. if (output_mode->hdisplay <= 720
  231. || output_mode->hdisplay >= 1920) {
  232. mode->htotal = output_mode->htotal;
  233. mode->hsync_start = (mode->hdisplay + (mode->htotal
  234. - mode->hdisplay) * 9 / 10) & ~7;
  235. mode->hsync_end = mode->hsync_start + 8;
  236. }
  237. if (output_mode->vdisplay >= 1024) {
  238. mode->vtotal = output_mode->vtotal;
  239. mode->vsync_start = output_mode->vsync_start;
  240. mode->vsync_end = output_mode->vsync_end;
  241. }
  242. mode->type |= DRM_MODE_TYPE_DRIVER;
  243. drm_mode_probed_add(connector, mode);
  244. n++;
  245. }
  246. return n;
  247. }
  248. static int nv17_tv_get_modes(struct drm_encoder *encoder,
  249. struct drm_connector *connector)
  250. {
  251. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  252. if (tv_norm->kind == CTV_ENC_MODE)
  253. return nv17_tv_get_hd_modes(encoder, connector);
  254. else
  255. return nv17_tv_get_ld_modes(encoder, connector);
  256. }
  257. static int nv17_tv_mode_valid(struct drm_encoder *encoder,
  258. struct drm_display_mode *mode)
  259. {
  260. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  261. if (tv_norm->kind == CTV_ENC_MODE) {
  262. struct drm_display_mode *output_mode =
  263. &tv_norm->ctv_enc_mode.mode;
  264. if (mode->clock > 400000)
  265. return MODE_CLOCK_HIGH;
  266. if (mode->hdisplay > output_mode->hdisplay ||
  267. mode->vdisplay > output_mode->vdisplay)
  268. return MODE_BAD;
  269. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) !=
  270. (output_mode->flags & DRM_MODE_FLAG_INTERLACE))
  271. return MODE_NO_INTERLACE;
  272. if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  273. return MODE_NO_DBLESCAN;
  274. } else {
  275. const int vsync_tolerance = 600;
  276. if (mode->clock > 70000)
  277. return MODE_CLOCK_HIGH;
  278. if (abs(drm_mode_vrefresh(mode) * 1000 -
  279. tv_norm->tv_enc_mode.vrefresh) > vsync_tolerance)
  280. return MODE_VSYNC;
  281. /* The encoder takes care of the actual interlacing */
  282. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  283. return MODE_NO_INTERLACE;
  284. }
  285. return MODE_OK;
  286. }
  287. static bool nv17_tv_mode_fixup(struct drm_encoder *encoder,
  288. struct drm_display_mode *mode,
  289. struct drm_display_mode *adjusted_mode)
  290. {
  291. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  292. if (nv04_dac_in_use(encoder))
  293. return false;
  294. if (tv_norm->kind == CTV_ENC_MODE)
  295. adjusted_mode->clock = tv_norm->ctv_enc_mode.mode.clock;
  296. else
  297. adjusted_mode->clock = 90000;
  298. return true;
  299. }
  300. static void nv17_tv_dpms(struct drm_encoder *encoder, int mode)
  301. {
  302. struct drm_device *dev = encoder->dev;
  303. struct drm_nouveau_private *dev_priv = dev->dev_private;
  304. struct nouveau_gpio_engine *gpio = &dev_priv->engine.gpio;
  305. struct nv17_tv_state *regs = &to_tv_enc(encoder)->state;
  306. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  307. if (nouveau_encoder(encoder)->last_dpms == mode)
  308. return;
  309. nouveau_encoder(encoder)->last_dpms = mode;
  310. NV_INFO(dev, "Setting dpms mode %d on TV encoder (output %d)\n",
  311. mode, nouveau_encoder(encoder)->dcb->index);
  312. regs->ptv_200 &= ~1;
  313. if (tv_norm->kind == CTV_ENC_MODE) {
  314. nv04_dfp_update_fp_control(encoder, mode);
  315. } else {
  316. nv04_dfp_update_fp_control(encoder, DRM_MODE_DPMS_OFF);
  317. if (mode == DRM_MODE_DPMS_ON)
  318. regs->ptv_200 |= 1;
  319. }
  320. nv_load_ptv(dev, regs, 200);
  321. gpio->set(dev, DCB_GPIO_TVDAC1, mode == DRM_MODE_DPMS_ON);
  322. gpio->set(dev, DCB_GPIO_TVDAC0, mode == DRM_MODE_DPMS_ON);
  323. nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON);
  324. }
  325. static void nv17_tv_prepare(struct drm_encoder *encoder)
  326. {
  327. struct drm_device *dev = encoder->dev;
  328. struct drm_nouveau_private *dev_priv = dev->dev_private;
  329. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  330. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  331. int head = nouveau_crtc(encoder->crtc)->index;
  332. uint8_t *cr_lcd = &dev_priv->mode_reg.crtc_reg[head].CRTC[
  333. NV_CIO_CRE_LCD__INDEX];
  334. uint32_t dacclk_off = NV_PRAMDAC_DACCLK +
  335. nv04_dac_output_offset(encoder);
  336. uint32_t dacclk;
  337. helper->dpms(encoder, DRM_MODE_DPMS_OFF);
  338. nv04_dfp_disable(dev, head);
  339. /* Unbind any FP encoders from this head if we need the FP
  340. * stuff enabled. */
  341. if (tv_norm->kind == CTV_ENC_MODE) {
  342. struct drm_encoder *enc;
  343. list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
  344. struct dcb_entry *dcb = nouveau_encoder(enc)->dcb;
  345. if ((dcb->type == OUTPUT_TMDS ||
  346. dcb->type == OUTPUT_LVDS) &&
  347. !enc->crtc &&
  348. nv04_dfp_get_bound_head(dev, dcb) == head) {
  349. nv04_dfp_bind_head(dev, dcb, head ^ 1,
  350. dev_priv->vbios.fp.dual_link);
  351. }
  352. }
  353. }
  354. if (tv_norm->kind == CTV_ENC_MODE)
  355. *cr_lcd |= 0x1 | (head ? 0x0 : 0x8);
  356. /* Set the DACCLK register */
  357. dacclk = (NVReadRAMDAC(dev, 0, dacclk_off) & ~0x30) | 0x1;
  358. if (dev_priv->card_type == NV_40)
  359. dacclk |= 0x1a << 16;
  360. if (tv_norm->kind == CTV_ENC_MODE) {
  361. dacclk |= 0x20;
  362. if (head)
  363. dacclk |= 0x100;
  364. else
  365. dacclk &= ~0x100;
  366. } else {
  367. dacclk |= 0x10;
  368. }
  369. NVWriteRAMDAC(dev, 0, dacclk_off, dacclk);
  370. }
  371. static void nv17_tv_mode_set(struct drm_encoder *encoder,
  372. struct drm_display_mode *drm_mode,
  373. struct drm_display_mode *adjusted_mode)
  374. {
  375. struct drm_device *dev = encoder->dev;
  376. struct drm_nouveau_private *dev_priv = dev->dev_private;
  377. int head = nouveau_crtc(encoder->crtc)->index;
  378. struct nv04_crtc_reg *regs = &dev_priv->mode_reg.crtc_reg[head];
  379. struct nv17_tv_state *tv_regs = &to_tv_enc(encoder)->state;
  380. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  381. int i;
  382. regs->CRTC[NV_CIO_CRE_53] = 0x40; /* FP_HTIMING */
  383. regs->CRTC[NV_CIO_CRE_54] = 0; /* FP_VTIMING */
  384. regs->ramdac_630 = 0x2; /* turn off green mode (tv test pattern?) */
  385. regs->tv_setup = 1;
  386. regs->ramdac_8c0 = 0x0;
  387. if (tv_norm->kind == TV_ENC_MODE) {
  388. tv_regs->ptv_200 = 0x13111100;
  389. if (head)
  390. tv_regs->ptv_200 |= 0x10;
  391. tv_regs->ptv_20c = 0x808010;
  392. tv_regs->ptv_304 = 0x2d00000;
  393. tv_regs->ptv_600 = 0x0;
  394. tv_regs->ptv_60c = 0x0;
  395. tv_regs->ptv_610 = 0x1e00000;
  396. if (tv_norm->tv_enc_mode.vdisplay == 576) {
  397. tv_regs->ptv_508 = 0x1200000;
  398. tv_regs->ptv_614 = 0x33;
  399. } else if (tv_norm->tv_enc_mode.vdisplay == 480) {
  400. tv_regs->ptv_508 = 0xf00000;
  401. tv_regs->ptv_614 = 0x13;
  402. }
  403. if (dev_priv->card_type >= NV_30) {
  404. tv_regs->ptv_500 = 0xe8e0;
  405. tv_regs->ptv_504 = 0x1710;
  406. tv_regs->ptv_604 = 0x0;
  407. tv_regs->ptv_608 = 0x0;
  408. } else {
  409. if (tv_norm->tv_enc_mode.vdisplay == 576) {
  410. tv_regs->ptv_604 = 0x20;
  411. tv_regs->ptv_608 = 0x10;
  412. tv_regs->ptv_500 = 0x19710;
  413. tv_regs->ptv_504 = 0x68f0;
  414. } else if (tv_norm->tv_enc_mode.vdisplay == 480) {
  415. tv_regs->ptv_604 = 0x10;
  416. tv_regs->ptv_608 = 0x20;
  417. tv_regs->ptv_500 = 0x4b90;
  418. tv_regs->ptv_504 = 0x1b480;
  419. }
  420. }
  421. for (i = 0; i < 0x40; i++)
  422. tv_regs->tv_enc[i] = tv_norm->tv_enc_mode.tv_enc[i];
  423. } else {
  424. struct drm_display_mode *output_mode =
  425. &tv_norm->ctv_enc_mode.mode;
  426. /* The registers in PRAMDAC+0xc00 control some timings and CSC
  427. * parameters for the CTV encoder (It's only used for "HD" TV
  428. * modes, I don't think I have enough working to guess what
  429. * they exactly mean...), it's probably connected at the
  430. * output of the FP encoder, but it also needs the analog
  431. * encoder in its OR enabled and routed to the head it's
  432. * using. It's enabled with the DACCLK register, bits [5:4].
  433. */
  434. for (i = 0; i < 38; i++)
  435. regs->ctv_regs[i] = tv_norm->ctv_enc_mode.ctv_regs[i];
  436. regs->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
  437. regs->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
  438. regs->fp_horiz_regs[FP_SYNC_START] =
  439. output_mode->hsync_start - 1;
  440. regs->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
  441. regs->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay +
  442. max((output_mode->hdisplay-600)/40 - 1, 1);
  443. regs->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
  444. regs->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
  445. regs->fp_vert_regs[FP_SYNC_START] =
  446. output_mode->vsync_start - 1;
  447. regs->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
  448. regs->fp_vert_regs[FP_CRTC] = output_mode->vdisplay - 1;
  449. regs->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
  450. NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
  451. NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
  452. if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
  453. regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
  454. if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
  455. regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
  456. regs->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
  457. NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
  458. NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
  459. NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
  460. NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
  461. NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
  462. NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
  463. regs->fp_debug_2 = 0;
  464. regs->fp_margin_color = 0x801080;
  465. }
  466. }
  467. static void nv17_tv_commit(struct drm_encoder *encoder)
  468. {
  469. struct drm_device *dev = encoder->dev;
  470. struct drm_nouveau_private *dev_priv = dev->dev_private;
  471. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  472. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  473. struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  474. if (get_tv_norm(encoder)->kind == TV_ENC_MODE) {
  475. nv17_tv_update_rescaler(encoder);
  476. nv17_tv_update_properties(encoder);
  477. } else {
  478. nv17_ctv_update_rescaler(encoder);
  479. }
  480. nv17_tv_state_load(dev, &to_tv_enc(encoder)->state);
  481. /* This could use refinement for flatpanels, but it should work */
  482. if (dev_priv->chipset < 0x44)
  483. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
  484. nv04_dac_output_offset(encoder),
  485. 0xf0000000);
  486. else
  487. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
  488. nv04_dac_output_offset(encoder),
  489. 0x00100000);
  490. helper->dpms(encoder, DRM_MODE_DPMS_ON);
  491. NV_INFO(dev, "Output %s is running on CRTC %d using output %c\n",
  492. drm_get_connector_name(
  493. &nouveau_encoder_connector_get(nv_encoder)->base),
  494. nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
  495. }
  496. static void nv17_tv_save(struct drm_encoder *encoder)
  497. {
  498. struct drm_device *dev = encoder->dev;
  499. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  500. nouveau_encoder(encoder)->restore.output =
  501. NVReadRAMDAC(dev, 0,
  502. NV_PRAMDAC_DACCLK +
  503. nv04_dac_output_offset(encoder));
  504. nv17_tv_state_save(dev, &tv_enc->saved_state);
  505. tv_enc->state.ptv_200 = tv_enc->saved_state.ptv_200;
  506. }
  507. static void nv17_tv_restore(struct drm_encoder *encoder)
  508. {
  509. struct drm_device *dev = encoder->dev;
  510. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK +
  511. nv04_dac_output_offset(encoder),
  512. nouveau_encoder(encoder)->restore.output);
  513. nv17_tv_state_load(dev, &to_tv_enc(encoder)->saved_state);
  514. nouveau_encoder(encoder)->last_dpms = NV_DPMS_CLEARED;
  515. }
  516. static int nv17_tv_create_resources(struct drm_encoder *encoder,
  517. struct drm_connector *connector)
  518. {
  519. struct drm_device *dev = encoder->dev;
  520. struct drm_mode_config *conf = &dev->mode_config;
  521. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  522. struct dcb_entry *dcb = nouveau_encoder(encoder)->dcb;
  523. int num_tv_norms = dcb->tvconf.has_component_output ? NUM_TV_NORMS :
  524. NUM_LD_TV_NORMS;
  525. int i;
  526. if (nouveau_tv_norm) {
  527. for (i = 0; i < num_tv_norms; i++) {
  528. if (!strcmp(nv17_tv_norm_names[i], nouveau_tv_norm)) {
  529. tv_enc->tv_norm = i;
  530. break;
  531. }
  532. }
  533. if (i == num_tv_norms)
  534. NV_WARN(dev, "Invalid TV norm setting \"%s\"\n",
  535. nouveau_tv_norm);
  536. }
  537. drm_mode_create_tv_properties(dev, num_tv_norms, nv17_tv_norm_names);
  538. drm_connector_attach_property(connector,
  539. conf->tv_select_subconnector_property,
  540. tv_enc->select_subconnector);
  541. drm_connector_attach_property(connector,
  542. conf->tv_subconnector_property,
  543. tv_enc->subconnector);
  544. drm_connector_attach_property(connector,
  545. conf->tv_mode_property,
  546. tv_enc->tv_norm);
  547. drm_connector_attach_property(connector,
  548. conf->tv_flicker_reduction_property,
  549. tv_enc->flicker);
  550. drm_connector_attach_property(connector,
  551. conf->tv_saturation_property,
  552. tv_enc->saturation);
  553. drm_connector_attach_property(connector,
  554. conf->tv_hue_property,
  555. tv_enc->hue);
  556. drm_connector_attach_property(connector,
  557. conf->tv_overscan_property,
  558. tv_enc->overscan);
  559. return 0;
  560. }
  561. static int nv17_tv_set_property(struct drm_encoder *encoder,
  562. struct drm_connector *connector,
  563. struct drm_property *property,
  564. uint64_t val)
  565. {
  566. struct drm_mode_config *conf = &encoder->dev->mode_config;
  567. struct drm_crtc *crtc = encoder->crtc;
  568. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  569. struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
  570. bool modes_changed = false;
  571. if (property == conf->tv_overscan_property) {
  572. tv_enc->overscan = val;
  573. if (encoder->crtc) {
  574. if (tv_norm->kind == CTV_ENC_MODE)
  575. nv17_ctv_update_rescaler(encoder);
  576. else
  577. nv17_tv_update_rescaler(encoder);
  578. }
  579. } else if (property == conf->tv_saturation_property) {
  580. if (tv_norm->kind != TV_ENC_MODE)
  581. return -EINVAL;
  582. tv_enc->saturation = val;
  583. nv17_tv_update_properties(encoder);
  584. } else if (property == conf->tv_hue_property) {
  585. if (tv_norm->kind != TV_ENC_MODE)
  586. return -EINVAL;
  587. tv_enc->hue = val;
  588. nv17_tv_update_properties(encoder);
  589. } else if (property == conf->tv_flicker_reduction_property) {
  590. if (tv_norm->kind != TV_ENC_MODE)
  591. return -EINVAL;
  592. tv_enc->flicker = val;
  593. if (encoder->crtc)
  594. nv17_tv_update_rescaler(encoder);
  595. } else if (property == conf->tv_mode_property) {
  596. if (connector->dpms != DRM_MODE_DPMS_OFF)
  597. return -EINVAL;
  598. tv_enc->tv_norm = val;
  599. modes_changed = true;
  600. } else if (property == conf->tv_select_subconnector_property) {
  601. if (tv_norm->kind != TV_ENC_MODE)
  602. return -EINVAL;
  603. tv_enc->select_subconnector = val;
  604. nv17_tv_update_properties(encoder);
  605. } else {
  606. return -EINVAL;
  607. }
  608. if (modes_changed) {
  609. drm_helper_probe_single_connector_modes(connector, 0, 0);
  610. /* Disable the crtc to ensure a full modeset is
  611. * performed whenever it's turned on again. */
  612. if (crtc) {
  613. struct drm_mode_set modeset = {
  614. .crtc = crtc,
  615. };
  616. crtc->funcs->set_config(&modeset);
  617. }
  618. }
  619. return 0;
  620. }
  621. static void nv17_tv_destroy(struct drm_encoder *encoder)
  622. {
  623. struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
  624. NV_DEBUG_KMS(encoder->dev, "\n");
  625. drm_encoder_cleanup(encoder);
  626. kfree(tv_enc);
  627. }
  628. static struct drm_encoder_helper_funcs nv17_tv_helper_funcs = {
  629. .dpms = nv17_tv_dpms,
  630. .save = nv17_tv_save,
  631. .restore = nv17_tv_restore,
  632. .mode_fixup = nv17_tv_mode_fixup,
  633. .prepare = nv17_tv_prepare,
  634. .commit = nv17_tv_commit,
  635. .mode_set = nv17_tv_mode_set,
  636. .detect = nv17_tv_detect,
  637. };
  638. static struct drm_encoder_slave_funcs nv17_tv_slave_funcs = {
  639. .get_modes = nv17_tv_get_modes,
  640. .mode_valid = nv17_tv_mode_valid,
  641. .create_resources = nv17_tv_create_resources,
  642. .set_property = nv17_tv_set_property,
  643. };
  644. static struct drm_encoder_funcs nv17_tv_funcs = {
  645. .destroy = nv17_tv_destroy,
  646. };
  647. int
  648. nv17_tv_create(struct drm_connector *connector, struct dcb_entry *entry)
  649. {
  650. struct drm_device *dev = connector->dev;
  651. struct drm_encoder *encoder;
  652. struct nv17_tv_encoder *tv_enc = NULL;
  653. tv_enc = kzalloc(sizeof(*tv_enc), GFP_KERNEL);
  654. if (!tv_enc)
  655. return -ENOMEM;
  656. tv_enc->overscan = 50;
  657. tv_enc->flicker = 50;
  658. tv_enc->saturation = 50;
  659. tv_enc->hue = 0;
  660. tv_enc->tv_norm = TV_NORM_PAL;
  661. tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
  662. tv_enc->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
  663. tv_enc->pin_mask = 0;
  664. encoder = to_drm_encoder(&tv_enc->base);
  665. tv_enc->base.dcb = entry;
  666. tv_enc->base.or = ffs(entry->or) - 1;
  667. drm_encoder_init(dev, encoder, &nv17_tv_funcs, DRM_MODE_ENCODER_TVDAC);
  668. drm_encoder_helper_add(encoder, &nv17_tv_helper_funcs);
  669. to_encoder_slave(encoder)->slave_funcs = &nv17_tv_slave_funcs;
  670. encoder->possible_crtcs = entry->heads;
  671. encoder->possible_clones = 0;
  672. nv17_tv_create_resources(encoder, connector);
  673. drm_mode_connector_attach_encoder(connector, encoder);
  674. return 0;
  675. }