tvnv17.c 24 KB

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