tvnv17.c 24 KB

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