nv17_tv.c 24 KB

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