11a646342SBen Skeggs /* 21a646342SBen Skeggs * Copyright (C) 2009 Francisco Jerez. 31a646342SBen Skeggs * All Rights Reserved. 41a646342SBen Skeggs * 51a646342SBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining 61a646342SBen Skeggs * a copy of this software and associated documentation files (the 71a646342SBen Skeggs * "Software"), to deal in the Software without restriction, including 81a646342SBen Skeggs * without limitation the rights to use, copy, modify, merge, publish, 91a646342SBen Skeggs * distribute, sublicense, and/or sell copies of the Software, and to 101a646342SBen Skeggs * permit persons to whom the Software is furnished to do so, subject to 111a646342SBen Skeggs * the following conditions: 121a646342SBen Skeggs * 131a646342SBen Skeggs * The above copyright notice and this permission notice (including the 141a646342SBen Skeggs * next paragraph) shall be included in all copies or substantial 151a646342SBen Skeggs * portions of the Software. 161a646342SBen Skeggs * 171a646342SBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 181a646342SBen Skeggs * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 191a646342SBen Skeggs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 201a646342SBen Skeggs * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 211a646342SBen Skeggs * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 221a646342SBen Skeggs * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 231a646342SBen Skeggs * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 241a646342SBen Skeggs * 251a646342SBen Skeggs */ 261a646342SBen Skeggs 271a646342SBen Skeggs #include <drm/drmP.h> 281a646342SBen Skeggs #include <drm/drm_crtc_helper.h> 291a646342SBen Skeggs #include "nouveau_drm.h" 301a646342SBen Skeggs #include "nouveau_reg.h" 311a646342SBen Skeggs #include "nouveau_encoder.h" 321a646342SBen Skeggs #include "nouveau_connector.h" 331a646342SBen Skeggs #include "nouveau_crtc.h" 341a646342SBen Skeggs #include "hw.h" 351a646342SBen Skeggs #include "tvnv17.h" 361a646342SBen Skeggs 371a646342SBen Skeggs MODULE_PARM_DESC(tv_norm, "Default TV norm.\n" 381a646342SBen Skeggs "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, NTSC-M, NTSC-J,\n" 391a646342SBen Skeggs "\t\t\thd480i, hd480p, hd576i, hd576p, hd720p, hd1080i.\n" 401a646342SBen Skeggs "\t\tDefault: PAL\n" 411a646342SBen Skeggs "\t\t*NOTE* Ignored for cards with external TV encoders."); 421a646342SBen Skeggs static char *nouveau_tv_norm; 431a646342SBen Skeggs module_param_named(tv_norm, nouveau_tv_norm, charp, 0400); 441a646342SBen Skeggs 451a646342SBen Skeggs static uint32_t nv42_tv_sample_load(struct drm_encoder *encoder) 461a646342SBen Skeggs { 471a646342SBen Skeggs struct drm_device *dev = encoder->dev; 481a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 49be83cd4eSBen Skeggs struct nvkm_gpio *gpio = nvxx_gpio(&drm->device); 501a646342SBen Skeggs uint32_t testval, regoffset = nv04_dac_output_offset(encoder); 511a646342SBen Skeggs uint32_t gpio0, gpio1, fp_htotal, fp_hsync_start, fp_hsync_end, 521a646342SBen Skeggs fp_control, test_ctrl, dacclk, ctv_14, ctv_1c, ctv_6c; 531a646342SBen Skeggs uint32_t sample = 0; 541a646342SBen Skeggs int head; 551a646342SBen Skeggs 561a646342SBen Skeggs #define RGB_TEST_DATA(r, g, b) (r << 0 | g << 10 | b << 20) 571a646342SBen Skeggs testval = RGB_TEST_DATA(0x82, 0xeb, 0x82); 581a646342SBen Skeggs if (drm->vbios.tvdactestval) 591a646342SBen Skeggs testval = drm->vbios.tvdactestval; 601a646342SBen Skeggs 611a646342SBen Skeggs dacclk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset); 621a646342SBen Skeggs head = (dacclk & 0x100) >> 8; 631a646342SBen Skeggs 641a646342SBen Skeggs /* Save the previous state. */ 65*2ea7249fSBen Skeggs gpio1 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC1, 0xff); 66*2ea7249fSBen Skeggs gpio0 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC0, 0xff); 671a646342SBen Skeggs fp_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL); 681a646342SBen Skeggs fp_hsync_start = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START); 691a646342SBen Skeggs fp_hsync_end = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END); 701a646342SBen Skeggs fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL); 711a646342SBen Skeggs test_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset); 721a646342SBen Skeggs ctv_1c = NVReadRAMDAC(dev, head, 0x680c1c); 731a646342SBen Skeggs ctv_14 = NVReadRAMDAC(dev, head, 0x680c14); 741a646342SBen Skeggs ctv_6c = NVReadRAMDAC(dev, head, 0x680c6c); 751a646342SBen Skeggs 761a646342SBen Skeggs /* Prepare the DAC for load detection. */ 77*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, true); 78*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, true); 791a646342SBen Skeggs 801a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, 1343); 811a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, 1047); 821a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, 1183); 831a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, 841a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS | 851a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12 | 861a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_READ_PROG | 871a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS | 881a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS); 891a646342SBen Skeggs 901a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, 0); 911a646342SBen Skeggs 921a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, 931a646342SBen Skeggs (dacclk & ~0xff) | 0x22); 941a646342SBen Skeggs msleep(1); 951a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, 961a646342SBen Skeggs (dacclk & ~0xff) | 0x21); 971a646342SBen Skeggs 981a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c1c, 1 << 20); 991a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c14, 4 << 16); 1001a646342SBen Skeggs 1011a646342SBen Skeggs /* Sample pin 0x4 (usually S-video luma). */ 1021a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c6c, testval >> 10 & 0x3ff); 1031a646342SBen Skeggs msleep(20); 1041a646342SBen Skeggs sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset) 1051a646342SBen Skeggs & 0x4 << 28; 1061a646342SBen Skeggs 1071a646342SBen Skeggs /* Sample the remaining pins. */ 1081a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c6c, testval & 0x3ff); 1091a646342SBen Skeggs msleep(20); 1101a646342SBen Skeggs sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset) 1111a646342SBen Skeggs & 0xa << 28; 1121a646342SBen Skeggs 1131a646342SBen Skeggs /* Restore the previous state. */ 1141a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c1c, ctv_1c); 1151a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c14, ctv_14); 1161a646342SBen Skeggs NVWriteRAMDAC(dev, head, 0x680c6c, ctv_6c); 1171a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, dacclk); 1181a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, test_ctrl); 1191a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, fp_control); 1201a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, fp_hsync_end); 1211a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, fp_hsync_start); 1221a646342SBen Skeggs NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, fp_htotal); 123*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, gpio1); 124*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, gpio0); 1251a646342SBen Skeggs 1261a646342SBen Skeggs return sample; 1271a646342SBen Skeggs } 1281a646342SBen Skeggs 1291a646342SBen Skeggs static bool 1301a646342SBen Skeggs get_tv_detect_quirks(struct drm_device *dev, uint32_t *pin_mask) 1311a646342SBen Skeggs { 1321a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 133d351b856SBen Skeggs struct nvkm_device *device = nvxx_device(&drm->device); 1341a646342SBen Skeggs 1351a646342SBen Skeggs /* Zotac FX5200 */ 136d351b856SBen Skeggs if (nv_device_match(device, 0x0322, 0x19da, 0x1035) || 137d351b856SBen Skeggs nv_device_match(device, 0x0322, 0x19da, 0x2035)) { 1381a646342SBen Skeggs *pin_mask = 0xc; 1391a646342SBen Skeggs return false; 1401a646342SBen Skeggs } 1411a646342SBen Skeggs 1421a646342SBen Skeggs /* MSI nForce2 IGP */ 143d351b856SBen Skeggs if (nv_device_match(device, 0x01f0, 0x1462, 0x5710)) { 1441a646342SBen Skeggs *pin_mask = 0xc; 1451a646342SBen Skeggs return false; 1461a646342SBen Skeggs } 1471a646342SBen Skeggs 1481a646342SBen Skeggs return true; 1491a646342SBen Skeggs } 1501a646342SBen Skeggs 1511a646342SBen Skeggs static enum drm_connector_status 1521a646342SBen Skeggs nv17_tv_detect(struct drm_encoder *encoder, struct drm_connector *connector) 1531a646342SBen Skeggs { 1541a646342SBen Skeggs struct drm_device *dev = encoder->dev; 1551a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 1561a646342SBen Skeggs struct drm_mode_config *conf = &dev->mode_config; 1571a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder); 1581a646342SBen Skeggs struct dcb_output *dcb = tv_enc->base.dcb; 1591a646342SBen Skeggs bool reliable = get_tv_detect_quirks(dev, &tv_enc->pin_mask); 1601a646342SBen Skeggs 1611a646342SBen Skeggs if (nv04_dac_in_use(encoder)) 1621a646342SBen Skeggs return connector_status_disconnected; 1631a646342SBen Skeggs 1641a646342SBen Skeggs if (reliable) { 165967e7bdeSBen Skeggs if (drm->device.info.chipset == 0x42 || 166967e7bdeSBen Skeggs drm->device.info.chipset == 0x43) 1671a646342SBen Skeggs tv_enc->pin_mask = 1681a646342SBen Skeggs nv42_tv_sample_load(encoder) >> 28 & 0xe; 1691a646342SBen Skeggs else 1701a646342SBen Skeggs tv_enc->pin_mask = 1711a646342SBen Skeggs nv17_dac_sample_load(encoder) >> 28 & 0xe; 1721a646342SBen Skeggs } 1731a646342SBen Skeggs 1741a646342SBen Skeggs switch (tv_enc->pin_mask) { 1751a646342SBen Skeggs case 0x2: 1761a646342SBen Skeggs case 0x4: 1771a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Composite; 1781a646342SBen Skeggs break; 1791a646342SBen Skeggs case 0xc: 1801a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO; 1811a646342SBen Skeggs break; 1821a646342SBen Skeggs case 0xe: 1831a646342SBen Skeggs if (dcb->tvconf.has_component_output) 1841a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Component; 1851a646342SBen Skeggs else 1861a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SCART; 1871a646342SBen Skeggs break; 1881a646342SBen Skeggs default: 1891a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown; 1901a646342SBen Skeggs break; 1911a646342SBen Skeggs } 1921a646342SBen Skeggs 1931a646342SBen Skeggs drm_object_property_set_value(&connector->base, 1941a646342SBen Skeggs conf->tv_subconnector_property, 1951a646342SBen Skeggs tv_enc->subconnector); 1961a646342SBen Skeggs 1971a646342SBen Skeggs if (!reliable) { 1981a646342SBen Skeggs return connector_status_unknown; 1991a646342SBen Skeggs } else if (tv_enc->subconnector) { 2001a646342SBen Skeggs NV_INFO(drm, "Load detected on output %c\n", 2011a646342SBen Skeggs '@' + ffs(dcb->or)); 2021a646342SBen Skeggs return connector_status_connected; 2031a646342SBen Skeggs } else { 2041a646342SBen Skeggs return connector_status_disconnected; 2051a646342SBen Skeggs } 2061a646342SBen Skeggs } 2071a646342SBen Skeggs 2081a646342SBen Skeggs static int nv17_tv_get_ld_modes(struct drm_encoder *encoder, 2091a646342SBen Skeggs struct drm_connector *connector) 2101a646342SBen Skeggs { 2111a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 2121a646342SBen Skeggs const struct drm_display_mode *tv_mode; 2131a646342SBen Skeggs int n = 0; 2141a646342SBen Skeggs 2151a646342SBen Skeggs for (tv_mode = nv17_tv_modes; tv_mode->hdisplay; tv_mode++) { 2161a646342SBen Skeggs struct drm_display_mode *mode; 2171a646342SBen Skeggs 2181a646342SBen Skeggs mode = drm_mode_duplicate(encoder->dev, tv_mode); 2191a646342SBen Skeggs 2201a646342SBen Skeggs mode->clock = tv_norm->tv_enc_mode.vrefresh * 2211a646342SBen Skeggs mode->htotal / 1000 * 2221a646342SBen Skeggs mode->vtotal / 1000; 2231a646342SBen Skeggs 2241a646342SBen Skeggs if (mode->flags & DRM_MODE_FLAG_DBLSCAN) 2251a646342SBen Skeggs mode->clock *= 2; 2261a646342SBen Skeggs 2271a646342SBen Skeggs if (mode->hdisplay == tv_norm->tv_enc_mode.hdisplay && 2281a646342SBen Skeggs mode->vdisplay == tv_norm->tv_enc_mode.vdisplay) 2291a646342SBen Skeggs mode->type |= DRM_MODE_TYPE_PREFERRED; 2301a646342SBen Skeggs 2311a646342SBen Skeggs drm_mode_probed_add(connector, mode); 2321a646342SBen Skeggs n++; 2331a646342SBen Skeggs } 2341a646342SBen Skeggs 2351a646342SBen Skeggs return n; 2361a646342SBen Skeggs } 2371a646342SBen Skeggs 2381a646342SBen Skeggs static int nv17_tv_get_hd_modes(struct drm_encoder *encoder, 2391a646342SBen Skeggs struct drm_connector *connector) 2401a646342SBen Skeggs { 2411a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 2421a646342SBen Skeggs struct drm_display_mode *output_mode = &tv_norm->ctv_enc_mode.mode; 2431a646342SBen Skeggs struct drm_display_mode *mode; 2441a646342SBen Skeggs const struct { 2451a646342SBen Skeggs int hdisplay; 2461a646342SBen Skeggs int vdisplay; 2471a646342SBen Skeggs } modes[] = { 2481a646342SBen Skeggs { 640, 400 }, 2491a646342SBen Skeggs { 640, 480 }, 2501a646342SBen Skeggs { 720, 480 }, 2511a646342SBen Skeggs { 720, 576 }, 2521a646342SBen Skeggs { 800, 600 }, 2531a646342SBen Skeggs { 1024, 768 }, 2541a646342SBen Skeggs { 1280, 720 }, 2551a646342SBen Skeggs { 1280, 1024 }, 2561a646342SBen Skeggs { 1920, 1080 } 2571a646342SBen Skeggs }; 2581a646342SBen Skeggs int i, n = 0; 2591a646342SBen Skeggs 2601a646342SBen Skeggs for (i = 0; i < ARRAY_SIZE(modes); i++) { 2611a646342SBen Skeggs if (modes[i].hdisplay > output_mode->hdisplay || 2621a646342SBen Skeggs modes[i].vdisplay > output_mode->vdisplay) 2631a646342SBen Skeggs continue; 2641a646342SBen Skeggs 2651a646342SBen Skeggs if (modes[i].hdisplay == output_mode->hdisplay && 2661a646342SBen Skeggs modes[i].vdisplay == output_mode->vdisplay) { 2671a646342SBen Skeggs mode = drm_mode_duplicate(encoder->dev, output_mode); 2681a646342SBen Skeggs mode->type |= DRM_MODE_TYPE_PREFERRED; 2691a646342SBen Skeggs 2701a646342SBen Skeggs } else { 2711a646342SBen Skeggs mode = drm_cvt_mode(encoder->dev, modes[i].hdisplay, 2721a646342SBen Skeggs modes[i].vdisplay, 60, false, 2731a646342SBen Skeggs (output_mode->flags & 2741a646342SBen Skeggs DRM_MODE_FLAG_INTERLACE), false); 2751a646342SBen Skeggs } 2761a646342SBen Skeggs 2771a646342SBen Skeggs /* CVT modes are sometimes unsuitable... */ 2781a646342SBen Skeggs if (output_mode->hdisplay <= 720 2791a646342SBen Skeggs || output_mode->hdisplay >= 1920) { 2801a646342SBen Skeggs mode->htotal = output_mode->htotal; 2811a646342SBen Skeggs mode->hsync_start = (mode->hdisplay + (mode->htotal 2821a646342SBen Skeggs - mode->hdisplay) * 9 / 10) & ~7; 2831a646342SBen Skeggs mode->hsync_end = mode->hsync_start + 8; 2841a646342SBen Skeggs } 2851a646342SBen Skeggs 2861a646342SBen Skeggs if (output_mode->vdisplay >= 1024) { 2871a646342SBen Skeggs mode->vtotal = output_mode->vtotal; 2881a646342SBen Skeggs mode->vsync_start = output_mode->vsync_start; 2891a646342SBen Skeggs mode->vsync_end = output_mode->vsync_end; 2901a646342SBen Skeggs } 2911a646342SBen Skeggs 2921a646342SBen Skeggs mode->type |= DRM_MODE_TYPE_DRIVER; 2931a646342SBen Skeggs drm_mode_probed_add(connector, mode); 2941a646342SBen Skeggs n++; 2951a646342SBen Skeggs } 2961a646342SBen Skeggs 2971a646342SBen Skeggs return n; 2981a646342SBen Skeggs } 2991a646342SBen Skeggs 3001a646342SBen Skeggs static int nv17_tv_get_modes(struct drm_encoder *encoder, 3011a646342SBen Skeggs struct drm_connector *connector) 3021a646342SBen Skeggs { 3031a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 3041a646342SBen Skeggs 3051a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) 3061a646342SBen Skeggs return nv17_tv_get_hd_modes(encoder, connector); 3071a646342SBen Skeggs else 3081a646342SBen Skeggs return nv17_tv_get_ld_modes(encoder, connector); 3091a646342SBen Skeggs } 3101a646342SBen Skeggs 3111a646342SBen Skeggs static int nv17_tv_mode_valid(struct drm_encoder *encoder, 3121a646342SBen Skeggs struct drm_display_mode *mode) 3131a646342SBen Skeggs { 3141a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 3151a646342SBen Skeggs 3161a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) { 3171a646342SBen Skeggs struct drm_display_mode *output_mode = 3181a646342SBen Skeggs &tv_norm->ctv_enc_mode.mode; 3191a646342SBen Skeggs 3201a646342SBen Skeggs if (mode->clock > 400000) 3211a646342SBen Skeggs return MODE_CLOCK_HIGH; 3221a646342SBen Skeggs 3231a646342SBen Skeggs if (mode->hdisplay > output_mode->hdisplay || 3241a646342SBen Skeggs mode->vdisplay > output_mode->vdisplay) 3251a646342SBen Skeggs return MODE_BAD; 3261a646342SBen Skeggs 3271a646342SBen Skeggs if ((mode->flags & DRM_MODE_FLAG_INTERLACE) != 3281a646342SBen Skeggs (output_mode->flags & DRM_MODE_FLAG_INTERLACE)) 3291a646342SBen Skeggs return MODE_NO_INTERLACE; 3301a646342SBen Skeggs 3311a646342SBen Skeggs if (mode->flags & DRM_MODE_FLAG_DBLSCAN) 3321a646342SBen Skeggs return MODE_NO_DBLESCAN; 3331a646342SBen Skeggs 3341a646342SBen Skeggs } else { 3351a646342SBen Skeggs const int vsync_tolerance = 600; 3361a646342SBen Skeggs 3371a646342SBen Skeggs if (mode->clock > 70000) 3381a646342SBen Skeggs return MODE_CLOCK_HIGH; 3391a646342SBen Skeggs 3401a646342SBen Skeggs if (abs(drm_mode_vrefresh(mode) * 1000 - 3411a646342SBen Skeggs tv_norm->tv_enc_mode.vrefresh) > vsync_tolerance) 3421a646342SBen Skeggs return MODE_VSYNC; 3431a646342SBen Skeggs 3441a646342SBen Skeggs /* The encoder takes care of the actual interlacing */ 3451a646342SBen Skeggs if (mode->flags & DRM_MODE_FLAG_INTERLACE) 3461a646342SBen Skeggs return MODE_NO_INTERLACE; 3471a646342SBen Skeggs } 3481a646342SBen Skeggs 3491a646342SBen Skeggs return MODE_OK; 3501a646342SBen Skeggs } 3511a646342SBen Skeggs 3521a646342SBen Skeggs static bool nv17_tv_mode_fixup(struct drm_encoder *encoder, 3531a646342SBen Skeggs const struct drm_display_mode *mode, 3541a646342SBen Skeggs struct drm_display_mode *adjusted_mode) 3551a646342SBen Skeggs { 3561a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 3571a646342SBen Skeggs 3581a646342SBen Skeggs if (nv04_dac_in_use(encoder)) 3591a646342SBen Skeggs return false; 3601a646342SBen Skeggs 3611a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) 3621a646342SBen Skeggs adjusted_mode->clock = tv_norm->ctv_enc_mode.mode.clock; 3631a646342SBen Skeggs else 3641a646342SBen Skeggs adjusted_mode->clock = 90000; 3651a646342SBen Skeggs 3661a646342SBen Skeggs return true; 3671a646342SBen Skeggs } 3681a646342SBen Skeggs 3691a646342SBen Skeggs static void nv17_tv_dpms(struct drm_encoder *encoder, int mode) 3701a646342SBen Skeggs { 3711a646342SBen Skeggs struct drm_device *dev = encoder->dev; 3721a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 373be83cd4eSBen Skeggs struct nvkm_gpio *gpio = nvxx_gpio(&drm->device); 3741a646342SBen Skeggs struct nv17_tv_state *regs = &to_tv_enc(encoder)->state; 3751a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 3761a646342SBen Skeggs 3771a646342SBen Skeggs if (nouveau_encoder(encoder)->last_dpms == mode) 3781a646342SBen Skeggs return; 3791a646342SBen Skeggs nouveau_encoder(encoder)->last_dpms = mode; 3801a646342SBen Skeggs 3811a646342SBen Skeggs NV_INFO(drm, "Setting dpms mode %d on TV encoder (output %d)\n", 3821a646342SBen Skeggs mode, nouveau_encoder(encoder)->dcb->index); 3831a646342SBen Skeggs 3841a646342SBen Skeggs regs->ptv_200 &= ~1; 3851a646342SBen Skeggs 3861a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) { 3871a646342SBen Skeggs nv04_dfp_update_fp_control(encoder, mode); 3881a646342SBen Skeggs 3891a646342SBen Skeggs } else { 3901a646342SBen Skeggs nv04_dfp_update_fp_control(encoder, DRM_MODE_DPMS_OFF); 3911a646342SBen Skeggs 3921a646342SBen Skeggs if (mode == DRM_MODE_DPMS_ON) 3931a646342SBen Skeggs regs->ptv_200 |= 1; 3941a646342SBen Skeggs } 3951a646342SBen Skeggs 3961a646342SBen Skeggs nv_load_ptv(dev, regs, 200); 3971a646342SBen Skeggs 398*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, mode == DRM_MODE_DPMS_ON); 399*2ea7249fSBen Skeggs nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, mode == DRM_MODE_DPMS_ON); 4001a646342SBen Skeggs 4011a646342SBen Skeggs nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON); 4021a646342SBen Skeggs } 4031a646342SBen Skeggs 4041a646342SBen Skeggs static void nv17_tv_prepare(struct drm_encoder *encoder) 4051a646342SBen Skeggs { 4061a646342SBen Skeggs struct drm_device *dev = encoder->dev; 4071a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 408d58ded76SJani Nikula const struct drm_encoder_helper_funcs *helper = encoder->helper_private; 4091a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 4101a646342SBen Skeggs int head = nouveau_crtc(encoder->crtc)->index; 4111a646342SBen Skeggs uint8_t *cr_lcd = &nv04_display(dev)->mode_reg.crtc_reg[head].CRTC[ 4121a646342SBen Skeggs NV_CIO_CRE_LCD__INDEX]; 4131a646342SBen Skeggs uint32_t dacclk_off = NV_PRAMDAC_DACCLK + 4141a646342SBen Skeggs nv04_dac_output_offset(encoder); 4151a646342SBen Skeggs uint32_t dacclk; 4161a646342SBen Skeggs 4171a646342SBen Skeggs helper->dpms(encoder, DRM_MODE_DPMS_OFF); 4181a646342SBen Skeggs 4191a646342SBen Skeggs nv04_dfp_disable(dev, head); 4201a646342SBen Skeggs 4211a646342SBen Skeggs /* Unbind any FP encoders from this head if we need the FP 4221a646342SBen Skeggs * stuff enabled. */ 4231a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) { 4241a646342SBen Skeggs struct drm_encoder *enc; 4251a646342SBen Skeggs 4261a646342SBen Skeggs list_for_each_entry(enc, &dev->mode_config.encoder_list, head) { 4271a646342SBen Skeggs struct dcb_output *dcb = nouveau_encoder(enc)->dcb; 4281a646342SBen Skeggs 4291a646342SBen Skeggs if ((dcb->type == DCB_OUTPUT_TMDS || 4301a646342SBen Skeggs dcb->type == DCB_OUTPUT_LVDS) && 4311a646342SBen Skeggs !enc->crtc && 4321a646342SBen Skeggs nv04_dfp_get_bound_head(dev, dcb) == head) { 4331a646342SBen Skeggs nv04_dfp_bind_head(dev, dcb, head ^ 1, 4341a646342SBen Skeggs drm->vbios.fp.dual_link); 4351a646342SBen Skeggs } 4361a646342SBen Skeggs } 4371a646342SBen Skeggs 4381a646342SBen Skeggs } 4391a646342SBen Skeggs 4401a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) 4411a646342SBen Skeggs *cr_lcd |= 0x1 | (head ? 0x0 : 0x8); 4421a646342SBen Skeggs 4431a646342SBen Skeggs /* Set the DACCLK register */ 4441a646342SBen Skeggs dacclk = (NVReadRAMDAC(dev, 0, dacclk_off) & ~0x30) | 0x1; 4451a646342SBen Skeggs 446967e7bdeSBen Skeggs if (drm->device.info.family == NV_DEVICE_INFO_V0_CURIE) 4471a646342SBen Skeggs dacclk |= 0x1a << 16; 4481a646342SBen Skeggs 4491a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) { 4501a646342SBen Skeggs dacclk |= 0x20; 4511a646342SBen Skeggs 4521a646342SBen Skeggs if (head) 4531a646342SBen Skeggs dacclk |= 0x100; 4541a646342SBen Skeggs else 4551a646342SBen Skeggs dacclk &= ~0x100; 4561a646342SBen Skeggs 4571a646342SBen Skeggs } else { 4581a646342SBen Skeggs dacclk |= 0x10; 4591a646342SBen Skeggs 4601a646342SBen Skeggs } 4611a646342SBen Skeggs 4621a646342SBen Skeggs NVWriteRAMDAC(dev, 0, dacclk_off, dacclk); 4631a646342SBen Skeggs } 4641a646342SBen Skeggs 4651a646342SBen Skeggs static void nv17_tv_mode_set(struct drm_encoder *encoder, 4661a646342SBen Skeggs struct drm_display_mode *drm_mode, 4671a646342SBen Skeggs struct drm_display_mode *adjusted_mode) 4681a646342SBen Skeggs { 4691a646342SBen Skeggs struct drm_device *dev = encoder->dev; 4701a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 4711a646342SBen Skeggs int head = nouveau_crtc(encoder->crtc)->index; 4721a646342SBen Skeggs struct nv04_crtc_reg *regs = &nv04_display(dev)->mode_reg.crtc_reg[head]; 4731a646342SBen Skeggs struct nv17_tv_state *tv_regs = &to_tv_enc(encoder)->state; 4741a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 4751a646342SBen Skeggs int i; 4761a646342SBen Skeggs 4771a646342SBen Skeggs regs->CRTC[NV_CIO_CRE_53] = 0x40; /* FP_HTIMING */ 4781a646342SBen Skeggs regs->CRTC[NV_CIO_CRE_54] = 0; /* FP_VTIMING */ 4791a646342SBen Skeggs regs->ramdac_630 = 0x2; /* turn off green mode (tv test pattern?) */ 4801a646342SBen Skeggs regs->tv_setup = 1; 4811a646342SBen Skeggs regs->ramdac_8c0 = 0x0; 4821a646342SBen Skeggs 4831a646342SBen Skeggs if (tv_norm->kind == TV_ENC_MODE) { 4841a646342SBen Skeggs tv_regs->ptv_200 = 0x13111100; 4851a646342SBen Skeggs if (head) 4861a646342SBen Skeggs tv_regs->ptv_200 |= 0x10; 4871a646342SBen Skeggs 4881a646342SBen Skeggs tv_regs->ptv_20c = 0x808010; 4891a646342SBen Skeggs tv_regs->ptv_304 = 0x2d00000; 4901a646342SBen Skeggs tv_regs->ptv_600 = 0x0; 4911a646342SBen Skeggs tv_regs->ptv_60c = 0x0; 4921a646342SBen Skeggs tv_regs->ptv_610 = 0x1e00000; 4931a646342SBen Skeggs 4941a646342SBen Skeggs if (tv_norm->tv_enc_mode.vdisplay == 576) { 4951a646342SBen Skeggs tv_regs->ptv_508 = 0x1200000; 4961a646342SBen Skeggs tv_regs->ptv_614 = 0x33; 4971a646342SBen Skeggs 4981a646342SBen Skeggs } else if (tv_norm->tv_enc_mode.vdisplay == 480) { 4991a646342SBen Skeggs tv_regs->ptv_508 = 0xf00000; 5001a646342SBen Skeggs tv_regs->ptv_614 = 0x13; 5011a646342SBen Skeggs } 5021a646342SBen Skeggs 503967e7bdeSBen Skeggs if (drm->device.info.family >= NV_DEVICE_INFO_V0_RANKINE) { 5041a646342SBen Skeggs tv_regs->ptv_500 = 0xe8e0; 5051a646342SBen Skeggs tv_regs->ptv_504 = 0x1710; 5061a646342SBen Skeggs tv_regs->ptv_604 = 0x0; 5071a646342SBen Skeggs tv_regs->ptv_608 = 0x0; 5081a646342SBen Skeggs } else { 5091a646342SBen Skeggs if (tv_norm->tv_enc_mode.vdisplay == 576) { 5101a646342SBen Skeggs tv_regs->ptv_604 = 0x20; 5111a646342SBen Skeggs tv_regs->ptv_608 = 0x10; 5121a646342SBen Skeggs tv_regs->ptv_500 = 0x19710; 5131a646342SBen Skeggs tv_regs->ptv_504 = 0x68f0; 5141a646342SBen Skeggs 5151a646342SBen Skeggs } else if (tv_norm->tv_enc_mode.vdisplay == 480) { 5161a646342SBen Skeggs tv_regs->ptv_604 = 0x10; 5171a646342SBen Skeggs tv_regs->ptv_608 = 0x20; 5181a646342SBen Skeggs tv_regs->ptv_500 = 0x4b90; 5191a646342SBen Skeggs tv_regs->ptv_504 = 0x1b480; 5201a646342SBen Skeggs } 5211a646342SBen Skeggs } 5221a646342SBen Skeggs 5231a646342SBen Skeggs for (i = 0; i < 0x40; i++) 5241a646342SBen Skeggs tv_regs->tv_enc[i] = tv_norm->tv_enc_mode.tv_enc[i]; 5251a646342SBen Skeggs 5261a646342SBen Skeggs } else { 5271a646342SBen Skeggs struct drm_display_mode *output_mode = 5281a646342SBen Skeggs &tv_norm->ctv_enc_mode.mode; 5291a646342SBen Skeggs 5301a646342SBen Skeggs /* The registers in PRAMDAC+0xc00 control some timings and CSC 5311a646342SBen Skeggs * parameters for the CTV encoder (It's only used for "HD" TV 5321a646342SBen Skeggs * modes, I don't think I have enough working to guess what 5331a646342SBen Skeggs * they exactly mean...), it's probably connected at the 5341a646342SBen Skeggs * output of the FP encoder, but it also needs the analog 5351a646342SBen Skeggs * encoder in its OR enabled and routed to the head it's 5361a646342SBen Skeggs * using. It's enabled with the DACCLK register, bits [5:4]. 5371a646342SBen Skeggs */ 5381a646342SBen Skeggs for (i = 0; i < 38; i++) 5391a646342SBen Skeggs regs->ctv_regs[i] = tv_norm->ctv_enc_mode.ctv_regs[i]; 5401a646342SBen Skeggs 5411a646342SBen Skeggs regs->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1; 5421a646342SBen Skeggs regs->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1; 5431a646342SBen Skeggs regs->fp_horiz_regs[FP_SYNC_START] = 5441a646342SBen Skeggs output_mode->hsync_start - 1; 5451a646342SBen Skeggs regs->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1; 5461a646342SBen Skeggs regs->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay + 5471a646342SBen Skeggs max((output_mode->hdisplay-600)/40 - 1, 1); 5481a646342SBen Skeggs 5491a646342SBen Skeggs regs->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1; 5501a646342SBen Skeggs regs->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1; 5511a646342SBen Skeggs regs->fp_vert_regs[FP_SYNC_START] = 5521a646342SBen Skeggs output_mode->vsync_start - 1; 5531a646342SBen Skeggs regs->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1; 5541a646342SBen Skeggs regs->fp_vert_regs[FP_CRTC] = output_mode->vdisplay - 1; 5551a646342SBen Skeggs 5561a646342SBen Skeggs regs->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS | 5571a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_READ_PROG | 5581a646342SBen Skeggs NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12; 5591a646342SBen Skeggs 5601a646342SBen Skeggs if (output_mode->flags & DRM_MODE_FLAG_PVSYNC) 5611a646342SBen Skeggs regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS; 5621a646342SBen Skeggs if (output_mode->flags & DRM_MODE_FLAG_PHSYNC) 5631a646342SBen Skeggs regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS; 5641a646342SBen Skeggs 5651a646342SBen Skeggs regs->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND | 5661a646342SBen Skeggs NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND | 5671a646342SBen Skeggs NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR | 5681a646342SBen Skeggs NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR | 5691a646342SBen Skeggs NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED | 5701a646342SBen Skeggs NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE | 5711a646342SBen Skeggs NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE; 5721a646342SBen Skeggs 5731a646342SBen Skeggs regs->fp_debug_2 = 0; 5741a646342SBen Skeggs 5751a646342SBen Skeggs regs->fp_margin_color = 0x801080; 5761a646342SBen Skeggs 5771a646342SBen Skeggs } 5781a646342SBen Skeggs } 5791a646342SBen Skeggs 5801a646342SBen Skeggs static void nv17_tv_commit(struct drm_encoder *encoder) 5811a646342SBen Skeggs { 5821a646342SBen Skeggs struct drm_device *dev = encoder->dev; 5831a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 5841a646342SBen Skeggs struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc); 5851a646342SBen Skeggs struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder); 586d58ded76SJani Nikula const struct drm_encoder_helper_funcs *helper = encoder->helper_private; 5871a646342SBen Skeggs 5881a646342SBen Skeggs if (get_tv_norm(encoder)->kind == TV_ENC_MODE) { 5891a646342SBen Skeggs nv17_tv_update_rescaler(encoder); 5901a646342SBen Skeggs nv17_tv_update_properties(encoder); 5911a646342SBen Skeggs } else { 5921a646342SBen Skeggs nv17_ctv_update_rescaler(encoder); 5931a646342SBen Skeggs } 5941a646342SBen Skeggs 5951a646342SBen Skeggs nv17_tv_state_load(dev, &to_tv_enc(encoder)->state); 5961a646342SBen Skeggs 5971a646342SBen Skeggs /* This could use refinement for flatpanels, but it should work */ 598967e7bdeSBen Skeggs if (drm->device.info.chipset < 0x44) 5991a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + 6001a646342SBen Skeggs nv04_dac_output_offset(encoder), 6011a646342SBen Skeggs 0xf0000000); 6021a646342SBen Skeggs else 6031a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + 6041a646342SBen Skeggs nv04_dac_output_offset(encoder), 6051a646342SBen Skeggs 0x00100000); 6061a646342SBen Skeggs 6071a646342SBen Skeggs helper->dpms(encoder, DRM_MODE_DPMS_ON); 6081a646342SBen Skeggs 6091a646342SBen Skeggs NV_INFO(drm, "Output %s is running on CRTC %d using output %c\n", 6108c6c361aSJani Nikula nouveau_encoder_connector_get(nv_encoder)->base.name, 6111a646342SBen Skeggs nv_crtc->index, '@' + ffs(nv_encoder->dcb->or)); 6121a646342SBen Skeggs } 6131a646342SBen Skeggs 6141a646342SBen Skeggs static void nv17_tv_save(struct drm_encoder *encoder) 6151a646342SBen Skeggs { 6161a646342SBen Skeggs struct drm_device *dev = encoder->dev; 6171a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder); 6181a646342SBen Skeggs 6191a646342SBen Skeggs nouveau_encoder(encoder)->restore.output = 6201a646342SBen Skeggs NVReadRAMDAC(dev, 0, 6211a646342SBen Skeggs NV_PRAMDAC_DACCLK + 6221a646342SBen Skeggs nv04_dac_output_offset(encoder)); 6231a646342SBen Skeggs 6241a646342SBen Skeggs nv17_tv_state_save(dev, &tv_enc->saved_state); 6251a646342SBen Skeggs 6261a646342SBen Skeggs tv_enc->state.ptv_200 = tv_enc->saved_state.ptv_200; 6271a646342SBen Skeggs } 6281a646342SBen Skeggs 6291a646342SBen Skeggs static void nv17_tv_restore(struct drm_encoder *encoder) 6301a646342SBen Skeggs { 6311a646342SBen Skeggs struct drm_device *dev = encoder->dev; 6321a646342SBen Skeggs 6331a646342SBen Skeggs NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + 6341a646342SBen Skeggs nv04_dac_output_offset(encoder), 6351a646342SBen Skeggs nouveau_encoder(encoder)->restore.output); 6361a646342SBen Skeggs 6371a646342SBen Skeggs nv17_tv_state_load(dev, &to_tv_enc(encoder)->saved_state); 6381a646342SBen Skeggs 6391a646342SBen Skeggs nouveau_encoder(encoder)->last_dpms = NV_DPMS_CLEARED; 6401a646342SBen Skeggs } 6411a646342SBen Skeggs 6421a646342SBen Skeggs static int nv17_tv_create_resources(struct drm_encoder *encoder, 6431a646342SBen Skeggs struct drm_connector *connector) 6441a646342SBen Skeggs { 6451a646342SBen Skeggs struct drm_device *dev = encoder->dev; 6461a646342SBen Skeggs struct nouveau_drm *drm = nouveau_drm(dev); 6471a646342SBen Skeggs struct drm_mode_config *conf = &dev->mode_config; 6481a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder); 6491a646342SBen Skeggs struct dcb_output *dcb = nouveau_encoder(encoder)->dcb; 6501a646342SBen Skeggs int num_tv_norms = dcb->tvconf.has_component_output ? NUM_TV_NORMS : 6511a646342SBen Skeggs NUM_LD_TV_NORMS; 6521a646342SBen Skeggs int i; 6531a646342SBen Skeggs 6541a646342SBen Skeggs if (nouveau_tv_norm) { 6551a646342SBen Skeggs for (i = 0; i < num_tv_norms; i++) { 6561a646342SBen Skeggs if (!strcmp(nv17_tv_norm_names[i], nouveau_tv_norm)) { 6571a646342SBen Skeggs tv_enc->tv_norm = i; 6581a646342SBen Skeggs break; 6591a646342SBen Skeggs } 6601a646342SBen Skeggs } 6611a646342SBen Skeggs 6621a646342SBen Skeggs if (i == num_tv_norms) 6631a646342SBen Skeggs NV_WARN(drm, "Invalid TV norm setting \"%s\"\n", 6641a646342SBen Skeggs nouveau_tv_norm); 6651a646342SBen Skeggs } 6661a646342SBen Skeggs 6671a646342SBen Skeggs drm_mode_create_tv_properties(dev, num_tv_norms, nv17_tv_norm_names); 6681a646342SBen Skeggs 6691a646342SBen Skeggs drm_object_attach_property(&connector->base, 6701a646342SBen Skeggs conf->tv_select_subconnector_property, 6711a646342SBen Skeggs tv_enc->select_subconnector); 6721a646342SBen Skeggs drm_object_attach_property(&connector->base, 6731a646342SBen Skeggs conf->tv_subconnector_property, 6741a646342SBen Skeggs tv_enc->subconnector); 6751a646342SBen Skeggs drm_object_attach_property(&connector->base, 6761a646342SBen Skeggs conf->tv_mode_property, 6771a646342SBen Skeggs tv_enc->tv_norm); 6781a646342SBen Skeggs drm_object_attach_property(&connector->base, 6791a646342SBen Skeggs conf->tv_flicker_reduction_property, 6801a646342SBen Skeggs tv_enc->flicker); 6811a646342SBen Skeggs drm_object_attach_property(&connector->base, 6821a646342SBen Skeggs conf->tv_saturation_property, 6831a646342SBen Skeggs tv_enc->saturation); 6841a646342SBen Skeggs drm_object_attach_property(&connector->base, 6851a646342SBen Skeggs conf->tv_hue_property, 6861a646342SBen Skeggs tv_enc->hue); 6871a646342SBen Skeggs drm_object_attach_property(&connector->base, 6881a646342SBen Skeggs conf->tv_overscan_property, 6891a646342SBen Skeggs tv_enc->overscan); 6901a646342SBen Skeggs 6911a646342SBen Skeggs return 0; 6921a646342SBen Skeggs } 6931a646342SBen Skeggs 6941a646342SBen Skeggs static int nv17_tv_set_property(struct drm_encoder *encoder, 6951a646342SBen Skeggs struct drm_connector *connector, 6961a646342SBen Skeggs struct drm_property *property, 6971a646342SBen Skeggs uint64_t val) 6981a646342SBen Skeggs { 6991a646342SBen Skeggs struct drm_mode_config *conf = &encoder->dev->mode_config; 7001a646342SBen Skeggs struct drm_crtc *crtc = encoder->crtc; 7011a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder); 7021a646342SBen Skeggs struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder); 7031a646342SBen Skeggs bool modes_changed = false; 7041a646342SBen Skeggs 7051a646342SBen Skeggs if (property == conf->tv_overscan_property) { 7061a646342SBen Skeggs tv_enc->overscan = val; 7071a646342SBen Skeggs if (encoder->crtc) { 7081a646342SBen Skeggs if (tv_norm->kind == CTV_ENC_MODE) 7091a646342SBen Skeggs nv17_ctv_update_rescaler(encoder); 7101a646342SBen Skeggs else 7111a646342SBen Skeggs nv17_tv_update_rescaler(encoder); 7121a646342SBen Skeggs } 7131a646342SBen Skeggs 7141a646342SBen Skeggs } else if (property == conf->tv_saturation_property) { 7151a646342SBen Skeggs if (tv_norm->kind != TV_ENC_MODE) 7161a646342SBen Skeggs return -EINVAL; 7171a646342SBen Skeggs 7181a646342SBen Skeggs tv_enc->saturation = val; 7191a646342SBen Skeggs nv17_tv_update_properties(encoder); 7201a646342SBen Skeggs 7211a646342SBen Skeggs } else if (property == conf->tv_hue_property) { 7221a646342SBen Skeggs if (tv_norm->kind != TV_ENC_MODE) 7231a646342SBen Skeggs return -EINVAL; 7241a646342SBen Skeggs 7251a646342SBen Skeggs tv_enc->hue = val; 7261a646342SBen Skeggs nv17_tv_update_properties(encoder); 7271a646342SBen Skeggs 7281a646342SBen Skeggs } else if (property == conf->tv_flicker_reduction_property) { 7291a646342SBen Skeggs if (tv_norm->kind != TV_ENC_MODE) 7301a646342SBen Skeggs return -EINVAL; 7311a646342SBen Skeggs 7321a646342SBen Skeggs tv_enc->flicker = val; 7331a646342SBen Skeggs if (encoder->crtc) 7341a646342SBen Skeggs nv17_tv_update_rescaler(encoder); 7351a646342SBen Skeggs 7361a646342SBen Skeggs } else if (property == conf->tv_mode_property) { 7371a646342SBen Skeggs if (connector->dpms != DRM_MODE_DPMS_OFF) 7381a646342SBen Skeggs return -EINVAL; 7391a646342SBen Skeggs 7401a646342SBen Skeggs tv_enc->tv_norm = val; 7411a646342SBen Skeggs 7421a646342SBen Skeggs modes_changed = true; 7431a646342SBen Skeggs 7441a646342SBen Skeggs } else if (property == conf->tv_select_subconnector_property) { 7451a646342SBen Skeggs if (tv_norm->kind != TV_ENC_MODE) 7461a646342SBen Skeggs return -EINVAL; 7471a646342SBen Skeggs 7481a646342SBen Skeggs tv_enc->select_subconnector = val; 7491a646342SBen Skeggs nv17_tv_update_properties(encoder); 7501a646342SBen Skeggs 7511a646342SBen Skeggs } else { 7521a646342SBen Skeggs return -EINVAL; 7531a646342SBen Skeggs } 7541a646342SBen Skeggs 7551a646342SBen Skeggs if (modes_changed) { 7561a646342SBen Skeggs drm_helper_probe_single_connector_modes(connector, 0, 0); 7571a646342SBen Skeggs 7581a646342SBen Skeggs /* Disable the crtc to ensure a full modeset is 7591a646342SBen Skeggs * performed whenever it's turned on again. */ 7601a646342SBen Skeggs if (crtc) { 7611a646342SBen Skeggs struct drm_mode_set modeset = { 7621a646342SBen Skeggs .crtc = crtc, 7631a646342SBen Skeggs }; 7641a646342SBen Skeggs 7651a646342SBen Skeggs drm_mode_set_config_internal(&modeset); 7661a646342SBen Skeggs } 7671a646342SBen Skeggs } 7681a646342SBen Skeggs 7691a646342SBen Skeggs return 0; 7701a646342SBen Skeggs } 7711a646342SBen Skeggs 7721a646342SBen Skeggs static void nv17_tv_destroy(struct drm_encoder *encoder) 7731a646342SBen Skeggs { 7741a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder); 7751a646342SBen Skeggs 7761a646342SBen Skeggs drm_encoder_cleanup(encoder); 7771a646342SBen Skeggs kfree(tv_enc); 7781a646342SBen Skeggs } 7791a646342SBen Skeggs 7801a646342SBen Skeggs static struct drm_encoder_helper_funcs nv17_tv_helper_funcs = { 7811a646342SBen Skeggs .dpms = nv17_tv_dpms, 7821a646342SBen Skeggs .save = nv17_tv_save, 7831a646342SBen Skeggs .restore = nv17_tv_restore, 7841a646342SBen Skeggs .mode_fixup = nv17_tv_mode_fixup, 7851a646342SBen Skeggs .prepare = nv17_tv_prepare, 7861a646342SBen Skeggs .commit = nv17_tv_commit, 7871a646342SBen Skeggs .mode_set = nv17_tv_mode_set, 7881a646342SBen Skeggs .detect = nv17_tv_detect, 7891a646342SBen Skeggs }; 7901a646342SBen Skeggs 7911a646342SBen Skeggs static struct drm_encoder_slave_funcs nv17_tv_slave_funcs = { 7921a646342SBen Skeggs .get_modes = nv17_tv_get_modes, 7931a646342SBen Skeggs .mode_valid = nv17_tv_mode_valid, 7941a646342SBen Skeggs .create_resources = nv17_tv_create_resources, 7951a646342SBen Skeggs .set_property = nv17_tv_set_property, 7961a646342SBen Skeggs }; 7971a646342SBen Skeggs 7981a646342SBen Skeggs static struct drm_encoder_funcs nv17_tv_funcs = { 7991a646342SBen Skeggs .destroy = nv17_tv_destroy, 8001a646342SBen Skeggs }; 8011a646342SBen Skeggs 8021a646342SBen Skeggs int 8031a646342SBen Skeggs nv17_tv_create(struct drm_connector *connector, struct dcb_output *entry) 8041a646342SBen Skeggs { 8051a646342SBen Skeggs struct drm_device *dev = connector->dev; 8061a646342SBen Skeggs struct drm_encoder *encoder; 8071a646342SBen Skeggs struct nv17_tv_encoder *tv_enc = NULL; 8081a646342SBen Skeggs 8091a646342SBen Skeggs tv_enc = kzalloc(sizeof(*tv_enc), GFP_KERNEL); 8101a646342SBen Skeggs if (!tv_enc) 8111a646342SBen Skeggs return -ENOMEM; 8121a646342SBen Skeggs 8131a646342SBen Skeggs tv_enc->overscan = 50; 8141a646342SBen Skeggs tv_enc->flicker = 50; 8151a646342SBen Skeggs tv_enc->saturation = 50; 8161a646342SBen Skeggs tv_enc->hue = 0; 8171a646342SBen Skeggs tv_enc->tv_norm = TV_NORM_PAL; 8181a646342SBen Skeggs tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown; 8191a646342SBen Skeggs tv_enc->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic; 8201a646342SBen Skeggs tv_enc->pin_mask = 0; 8211a646342SBen Skeggs 8221a646342SBen Skeggs encoder = to_drm_encoder(&tv_enc->base); 8231a646342SBen Skeggs 8241a646342SBen Skeggs tv_enc->base.dcb = entry; 8251a646342SBen Skeggs tv_enc->base.or = ffs(entry->or) - 1; 8261a646342SBen Skeggs 8271a646342SBen Skeggs drm_encoder_init(dev, encoder, &nv17_tv_funcs, DRM_MODE_ENCODER_TVDAC); 8281a646342SBen Skeggs drm_encoder_helper_add(encoder, &nv17_tv_helper_funcs); 8291a646342SBen Skeggs to_encoder_slave(encoder)->slave_funcs = &nv17_tv_slave_funcs; 8301a646342SBen Skeggs 8311a646342SBen Skeggs encoder->possible_crtcs = entry->heads; 8321a646342SBen Skeggs encoder->possible_clones = 0; 8331a646342SBen Skeggs 8341a646342SBen Skeggs nv17_tv_create_resources(encoder, connector); 8351a646342SBen Skeggs drm_mode_connector_attach_encoder(connector, encoder); 8361a646342SBen Skeggs return 0; 8371a646342SBen Skeggs } 838