nv17_tv.c 23 KB

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