xref: /openbmc/linux/drivers/gpu/drm/nouveau/dispnv04/tvnv17.c (revision 4dc28134a8c124aa01b441e1e5b8b54312edc5dd)
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>
29*4dc28134SBen Skeggs #include "nouveau_drv.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. */
652ea7249fSBen Skeggs 	gpio1 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC1, 0xff);
662ea7249fSBen 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.  */
772ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, true);
782ea7249fSBen 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);
1232ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, gpio1);
1242ea7249fSBen 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 
135c7af0ff0SBen Skeggs 	if (device->quirk && device->quirk->tv_pin_mask) {
136c7af0ff0SBen Skeggs 		*pin_mask = device->quirk->tv_pin_mask;
1371a646342SBen Skeggs 		return false;
1381a646342SBen Skeggs 	}
1391a646342SBen Skeggs 
1401a646342SBen Skeggs 	return true;
1411a646342SBen Skeggs }
1421a646342SBen Skeggs 
1431a646342SBen Skeggs static enum drm_connector_status
1441a646342SBen Skeggs nv17_tv_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1451a646342SBen Skeggs {
1461a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
1471a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
1481a646342SBen Skeggs 	struct drm_mode_config *conf = &dev->mode_config;
1491a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
1501a646342SBen Skeggs 	struct dcb_output *dcb = tv_enc->base.dcb;
1511a646342SBen Skeggs 	bool reliable = get_tv_detect_quirks(dev, &tv_enc->pin_mask);
1521a646342SBen Skeggs 
1531a646342SBen Skeggs 	if (nv04_dac_in_use(encoder))
1541a646342SBen Skeggs 		return connector_status_disconnected;
1551a646342SBen Skeggs 
1561a646342SBen Skeggs 	if (reliable) {
157967e7bdeSBen Skeggs 		if (drm->device.info.chipset == 0x42 ||
158967e7bdeSBen Skeggs 		    drm->device.info.chipset == 0x43)
1591a646342SBen Skeggs 			tv_enc->pin_mask =
1601a646342SBen Skeggs 				nv42_tv_sample_load(encoder) >> 28 & 0xe;
1611a646342SBen Skeggs 		else
1621a646342SBen Skeggs 			tv_enc->pin_mask =
1631a646342SBen Skeggs 				nv17_dac_sample_load(encoder) >> 28 & 0xe;
1641a646342SBen Skeggs 	}
1651a646342SBen Skeggs 
1661a646342SBen Skeggs 	switch (tv_enc->pin_mask) {
1671a646342SBen Skeggs 	case 0x2:
1681a646342SBen Skeggs 	case 0x4:
1691a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Composite;
1701a646342SBen Skeggs 		break;
1711a646342SBen Skeggs 	case 0xc:
1721a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO;
1731a646342SBen Skeggs 		break;
1741a646342SBen Skeggs 	case 0xe:
1751a646342SBen Skeggs 		if (dcb->tvconf.has_component_output)
1761a646342SBen Skeggs 			tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Component;
1771a646342SBen Skeggs 		else
1781a646342SBen Skeggs 			tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SCART;
1791a646342SBen Skeggs 		break;
1801a646342SBen Skeggs 	default:
1811a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1821a646342SBen Skeggs 		break;
1831a646342SBen Skeggs 	}
1841a646342SBen Skeggs 
1851a646342SBen Skeggs 	drm_object_property_set_value(&connector->base,
1861a646342SBen Skeggs 					 conf->tv_subconnector_property,
1871a646342SBen Skeggs 					 tv_enc->subconnector);
1881a646342SBen Skeggs 
1891a646342SBen Skeggs 	if (!reliable) {
1901a646342SBen Skeggs 		return connector_status_unknown;
1911a646342SBen Skeggs 	} else if (tv_enc->subconnector) {
1921a646342SBen Skeggs 		NV_INFO(drm, "Load detected on output %c\n",
1931a646342SBen Skeggs 			'@' + ffs(dcb->or));
1941a646342SBen Skeggs 		return connector_status_connected;
1951a646342SBen Skeggs 	} else {
1961a646342SBen Skeggs 		return connector_status_disconnected;
1971a646342SBen Skeggs 	}
1981a646342SBen Skeggs }
1991a646342SBen Skeggs 
2001a646342SBen Skeggs static int nv17_tv_get_ld_modes(struct drm_encoder *encoder,
2011a646342SBen Skeggs 				struct drm_connector *connector)
2021a646342SBen Skeggs {
2031a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
2041a646342SBen Skeggs 	const struct drm_display_mode *tv_mode;
2051a646342SBen Skeggs 	int n = 0;
2061a646342SBen Skeggs 
2071a646342SBen Skeggs 	for (tv_mode = nv17_tv_modes; tv_mode->hdisplay; tv_mode++) {
2081a646342SBen Skeggs 		struct drm_display_mode *mode;
2091a646342SBen Skeggs 
2101a646342SBen Skeggs 		mode = drm_mode_duplicate(encoder->dev, tv_mode);
2111a646342SBen Skeggs 
2121a646342SBen Skeggs 		mode->clock = tv_norm->tv_enc_mode.vrefresh *
2131a646342SBen Skeggs 			mode->htotal / 1000 *
2141a646342SBen Skeggs 			mode->vtotal / 1000;
2151a646342SBen Skeggs 
2161a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
2171a646342SBen Skeggs 			mode->clock *= 2;
2181a646342SBen Skeggs 
2191a646342SBen Skeggs 		if (mode->hdisplay == tv_norm->tv_enc_mode.hdisplay &&
2201a646342SBen Skeggs 		    mode->vdisplay == tv_norm->tv_enc_mode.vdisplay)
2211a646342SBen Skeggs 			mode->type |= DRM_MODE_TYPE_PREFERRED;
2221a646342SBen Skeggs 
2231a646342SBen Skeggs 		drm_mode_probed_add(connector, mode);
2241a646342SBen Skeggs 		n++;
2251a646342SBen Skeggs 	}
2261a646342SBen Skeggs 
2271a646342SBen Skeggs 	return n;
2281a646342SBen Skeggs }
2291a646342SBen Skeggs 
2301a646342SBen Skeggs static int nv17_tv_get_hd_modes(struct drm_encoder *encoder,
2311a646342SBen Skeggs 				struct drm_connector *connector)
2321a646342SBen Skeggs {
2331a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
2341a646342SBen Skeggs 	struct drm_display_mode *output_mode = &tv_norm->ctv_enc_mode.mode;
2351a646342SBen Skeggs 	struct drm_display_mode *mode;
2361a646342SBen Skeggs 	const struct {
2371a646342SBen Skeggs 		int hdisplay;
2381a646342SBen Skeggs 		int vdisplay;
2391a646342SBen Skeggs 	} modes[] = {
2401a646342SBen Skeggs 		{ 640, 400 },
2411a646342SBen Skeggs 		{ 640, 480 },
2421a646342SBen Skeggs 		{ 720, 480 },
2431a646342SBen Skeggs 		{ 720, 576 },
2441a646342SBen Skeggs 		{ 800, 600 },
2451a646342SBen Skeggs 		{ 1024, 768 },
2461a646342SBen Skeggs 		{ 1280, 720 },
2471a646342SBen Skeggs 		{ 1280, 1024 },
2481a646342SBen Skeggs 		{ 1920, 1080 }
2491a646342SBen Skeggs 	};
2501a646342SBen Skeggs 	int i, n = 0;
2511a646342SBen Skeggs 
2521a646342SBen Skeggs 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
2531a646342SBen Skeggs 		if (modes[i].hdisplay > output_mode->hdisplay ||
2541a646342SBen Skeggs 		    modes[i].vdisplay > output_mode->vdisplay)
2551a646342SBen Skeggs 			continue;
2561a646342SBen Skeggs 
2571a646342SBen Skeggs 		if (modes[i].hdisplay == output_mode->hdisplay &&
2581a646342SBen Skeggs 		    modes[i].vdisplay == output_mode->vdisplay) {
2591a646342SBen Skeggs 			mode = drm_mode_duplicate(encoder->dev, output_mode);
2601a646342SBen Skeggs 			mode->type |= DRM_MODE_TYPE_PREFERRED;
2611a646342SBen Skeggs 
2621a646342SBen Skeggs 		} else {
2631a646342SBen Skeggs 			mode = drm_cvt_mode(encoder->dev, modes[i].hdisplay,
2641a646342SBen Skeggs 					    modes[i].vdisplay, 60, false,
2651a646342SBen Skeggs 					    (output_mode->flags &
2661a646342SBen Skeggs 					     DRM_MODE_FLAG_INTERLACE), false);
2671a646342SBen Skeggs 		}
2681a646342SBen Skeggs 
2691a646342SBen Skeggs 		/* CVT modes are sometimes unsuitable... */
2701a646342SBen Skeggs 		if (output_mode->hdisplay <= 720
2711a646342SBen Skeggs 		    || output_mode->hdisplay >= 1920) {
2721a646342SBen Skeggs 			mode->htotal = output_mode->htotal;
2731a646342SBen Skeggs 			mode->hsync_start = (mode->hdisplay + (mode->htotal
2741a646342SBen Skeggs 					     - mode->hdisplay) * 9 / 10) & ~7;
2751a646342SBen Skeggs 			mode->hsync_end = mode->hsync_start + 8;
2761a646342SBen Skeggs 		}
2771a646342SBen Skeggs 
2781a646342SBen Skeggs 		if (output_mode->vdisplay >= 1024) {
2791a646342SBen Skeggs 			mode->vtotal = output_mode->vtotal;
2801a646342SBen Skeggs 			mode->vsync_start = output_mode->vsync_start;
2811a646342SBen Skeggs 			mode->vsync_end = output_mode->vsync_end;
2821a646342SBen Skeggs 		}
2831a646342SBen Skeggs 
2841a646342SBen Skeggs 		mode->type |= DRM_MODE_TYPE_DRIVER;
2851a646342SBen Skeggs 		drm_mode_probed_add(connector, mode);
2861a646342SBen Skeggs 		n++;
2871a646342SBen Skeggs 	}
2881a646342SBen Skeggs 
2891a646342SBen Skeggs 	return n;
2901a646342SBen Skeggs }
2911a646342SBen Skeggs 
2921a646342SBen Skeggs static int nv17_tv_get_modes(struct drm_encoder *encoder,
2931a646342SBen Skeggs 			     struct drm_connector *connector)
2941a646342SBen Skeggs {
2951a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
2961a646342SBen Skeggs 
2971a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
2981a646342SBen Skeggs 		return nv17_tv_get_hd_modes(encoder, connector);
2991a646342SBen Skeggs 	else
3001a646342SBen Skeggs 		return nv17_tv_get_ld_modes(encoder, connector);
3011a646342SBen Skeggs }
3021a646342SBen Skeggs 
3031a646342SBen Skeggs static int nv17_tv_mode_valid(struct drm_encoder *encoder,
3041a646342SBen Skeggs 			      struct drm_display_mode *mode)
3051a646342SBen Skeggs {
3061a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3071a646342SBen Skeggs 
3081a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
3091a646342SBen Skeggs 		struct drm_display_mode *output_mode =
3101a646342SBen Skeggs 						&tv_norm->ctv_enc_mode.mode;
3111a646342SBen Skeggs 
3121a646342SBen Skeggs 		if (mode->clock > 400000)
3131a646342SBen Skeggs 			return MODE_CLOCK_HIGH;
3141a646342SBen Skeggs 
3151a646342SBen Skeggs 		if (mode->hdisplay > output_mode->hdisplay ||
3161a646342SBen Skeggs 		    mode->vdisplay > output_mode->vdisplay)
3171a646342SBen Skeggs 			return MODE_BAD;
3181a646342SBen Skeggs 
3191a646342SBen Skeggs 		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) !=
3201a646342SBen Skeggs 		    (output_mode->flags & DRM_MODE_FLAG_INTERLACE))
3211a646342SBen Skeggs 			return MODE_NO_INTERLACE;
3221a646342SBen Skeggs 
3231a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
3241a646342SBen Skeggs 			return MODE_NO_DBLESCAN;
3251a646342SBen Skeggs 
3261a646342SBen Skeggs 	} else {
3271a646342SBen Skeggs 		const int vsync_tolerance = 600;
3281a646342SBen Skeggs 
3291a646342SBen Skeggs 		if (mode->clock > 70000)
3301a646342SBen Skeggs 			return MODE_CLOCK_HIGH;
3311a646342SBen Skeggs 
3321a646342SBen Skeggs 		if (abs(drm_mode_vrefresh(mode) * 1000 -
3331a646342SBen Skeggs 			tv_norm->tv_enc_mode.vrefresh) > vsync_tolerance)
3341a646342SBen Skeggs 			return MODE_VSYNC;
3351a646342SBen Skeggs 
3361a646342SBen Skeggs 		/* The encoder takes care of the actual interlacing */
3371a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3381a646342SBen Skeggs 			return MODE_NO_INTERLACE;
3391a646342SBen Skeggs 	}
3401a646342SBen Skeggs 
3411a646342SBen Skeggs 	return MODE_OK;
3421a646342SBen Skeggs }
3431a646342SBen Skeggs 
3441a646342SBen Skeggs static bool nv17_tv_mode_fixup(struct drm_encoder *encoder,
3451a646342SBen Skeggs 			       const struct drm_display_mode *mode,
3461a646342SBen Skeggs 			       struct drm_display_mode *adjusted_mode)
3471a646342SBen Skeggs {
3481a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3491a646342SBen Skeggs 
3501a646342SBen Skeggs 	if (nv04_dac_in_use(encoder))
3511a646342SBen Skeggs 		return false;
3521a646342SBen Skeggs 
3531a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
3541a646342SBen Skeggs 		adjusted_mode->clock = tv_norm->ctv_enc_mode.mode.clock;
3551a646342SBen Skeggs 	else
3561a646342SBen Skeggs 		adjusted_mode->clock = 90000;
3571a646342SBen Skeggs 
3581a646342SBen Skeggs 	return true;
3591a646342SBen Skeggs }
3601a646342SBen Skeggs 
3611a646342SBen Skeggs static void  nv17_tv_dpms(struct drm_encoder *encoder, int mode)
3621a646342SBen Skeggs {
3631a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
3641a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
365be83cd4eSBen Skeggs 	struct nvkm_gpio *gpio = nvxx_gpio(&drm->device);
3661a646342SBen Skeggs 	struct nv17_tv_state *regs = &to_tv_enc(encoder)->state;
3671a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3681a646342SBen Skeggs 
3691a646342SBen Skeggs 	if (nouveau_encoder(encoder)->last_dpms == mode)
3701a646342SBen Skeggs 		return;
3711a646342SBen Skeggs 	nouveau_encoder(encoder)->last_dpms = mode;
3721a646342SBen Skeggs 
3731a646342SBen Skeggs 	NV_INFO(drm, "Setting dpms mode %d on TV encoder (output %d)\n",
3741a646342SBen Skeggs 		 mode, nouveau_encoder(encoder)->dcb->index);
3751a646342SBen Skeggs 
3761a646342SBen Skeggs 	regs->ptv_200 &= ~1;
3771a646342SBen Skeggs 
3781a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
3791a646342SBen Skeggs 		nv04_dfp_update_fp_control(encoder, mode);
3801a646342SBen Skeggs 
3811a646342SBen Skeggs 	} else {
3821a646342SBen Skeggs 		nv04_dfp_update_fp_control(encoder, DRM_MODE_DPMS_OFF);
3831a646342SBen Skeggs 
3841a646342SBen Skeggs 		if (mode == DRM_MODE_DPMS_ON)
3851a646342SBen Skeggs 			regs->ptv_200 |= 1;
3861a646342SBen Skeggs 	}
3871a646342SBen Skeggs 
3881a646342SBen Skeggs 	nv_load_ptv(dev, regs, 200);
3891a646342SBen Skeggs 
3902ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, mode == DRM_MODE_DPMS_ON);
3912ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, mode == DRM_MODE_DPMS_ON);
3921a646342SBen Skeggs 
3931a646342SBen Skeggs 	nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON);
3941a646342SBen Skeggs }
3951a646342SBen Skeggs 
3961a646342SBen Skeggs static void nv17_tv_prepare(struct drm_encoder *encoder)
3971a646342SBen Skeggs {
3981a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
3991a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
400d58ded76SJani Nikula 	const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
4011a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
4021a646342SBen Skeggs 	int head = nouveau_crtc(encoder->crtc)->index;
4031a646342SBen Skeggs 	uint8_t *cr_lcd = &nv04_display(dev)->mode_reg.crtc_reg[head].CRTC[
4041a646342SBen Skeggs 							NV_CIO_CRE_LCD__INDEX];
4051a646342SBen Skeggs 	uint32_t dacclk_off = NV_PRAMDAC_DACCLK +
4061a646342SBen Skeggs 					nv04_dac_output_offset(encoder);
4071a646342SBen Skeggs 	uint32_t dacclk;
4081a646342SBen Skeggs 
4091a646342SBen Skeggs 	helper->dpms(encoder, DRM_MODE_DPMS_OFF);
4101a646342SBen Skeggs 
4111a646342SBen Skeggs 	nv04_dfp_disable(dev, head);
4121a646342SBen Skeggs 
4131a646342SBen Skeggs 	/* Unbind any FP encoders from this head if we need the FP
4141a646342SBen Skeggs 	 * stuff enabled. */
4151a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
4161a646342SBen Skeggs 		struct drm_encoder *enc;
4171a646342SBen Skeggs 
4181a646342SBen Skeggs 		list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
4191a646342SBen Skeggs 			struct dcb_output *dcb = nouveau_encoder(enc)->dcb;
4201a646342SBen Skeggs 
4211a646342SBen Skeggs 			if ((dcb->type == DCB_OUTPUT_TMDS ||
4221a646342SBen Skeggs 			     dcb->type == DCB_OUTPUT_LVDS) &&
4231a646342SBen Skeggs 			     !enc->crtc &&
4241a646342SBen Skeggs 			     nv04_dfp_get_bound_head(dev, dcb) == head) {
4251a646342SBen Skeggs 				nv04_dfp_bind_head(dev, dcb, head ^ 1,
4261a646342SBen Skeggs 						drm->vbios.fp.dual_link);
4271a646342SBen Skeggs 			}
4281a646342SBen Skeggs 		}
4291a646342SBen Skeggs 
4301a646342SBen Skeggs 	}
4311a646342SBen Skeggs 
4321a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
4331a646342SBen Skeggs 		*cr_lcd |= 0x1 | (head ? 0x0 : 0x8);
4341a646342SBen Skeggs 
4351a646342SBen Skeggs 	/* Set the DACCLK register */
4361a646342SBen Skeggs 	dacclk = (NVReadRAMDAC(dev, 0, dacclk_off) & ~0x30) | 0x1;
4371a646342SBen Skeggs 
438967e7bdeSBen Skeggs 	if (drm->device.info.family == NV_DEVICE_INFO_V0_CURIE)
4391a646342SBen Skeggs 		dacclk |= 0x1a << 16;
4401a646342SBen Skeggs 
4411a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
4421a646342SBen Skeggs 		dacclk |=  0x20;
4431a646342SBen Skeggs 
4441a646342SBen Skeggs 		if (head)
4451a646342SBen Skeggs 			dacclk |= 0x100;
4461a646342SBen Skeggs 		else
4471a646342SBen Skeggs 			dacclk &= ~0x100;
4481a646342SBen Skeggs 
4491a646342SBen Skeggs 	} else {
4501a646342SBen Skeggs 		dacclk |= 0x10;
4511a646342SBen Skeggs 
4521a646342SBen Skeggs 	}
4531a646342SBen Skeggs 
4541a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, dacclk_off, dacclk);
4551a646342SBen Skeggs }
4561a646342SBen Skeggs 
4571a646342SBen Skeggs static void nv17_tv_mode_set(struct drm_encoder *encoder,
4581a646342SBen Skeggs 			     struct drm_display_mode *drm_mode,
4591a646342SBen Skeggs 			     struct drm_display_mode *adjusted_mode)
4601a646342SBen Skeggs {
4611a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
4621a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
4631a646342SBen Skeggs 	int head = nouveau_crtc(encoder->crtc)->index;
4641a646342SBen Skeggs 	struct nv04_crtc_reg *regs = &nv04_display(dev)->mode_reg.crtc_reg[head];
4651a646342SBen Skeggs 	struct nv17_tv_state *tv_regs = &to_tv_enc(encoder)->state;
4661a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
4671a646342SBen Skeggs 	int i;
4681a646342SBen Skeggs 
4691a646342SBen Skeggs 	regs->CRTC[NV_CIO_CRE_53] = 0x40; /* FP_HTIMING */
4701a646342SBen Skeggs 	regs->CRTC[NV_CIO_CRE_54] = 0; /* FP_VTIMING */
4711a646342SBen Skeggs 	regs->ramdac_630 = 0x2; /* turn off green mode (tv test pattern?) */
4721a646342SBen Skeggs 	regs->tv_setup = 1;
4731a646342SBen Skeggs 	regs->ramdac_8c0 = 0x0;
4741a646342SBen Skeggs 
4751a646342SBen Skeggs 	if (tv_norm->kind == TV_ENC_MODE) {
4761a646342SBen Skeggs 		tv_regs->ptv_200 = 0x13111100;
4771a646342SBen Skeggs 		if (head)
4781a646342SBen Skeggs 			tv_regs->ptv_200 |= 0x10;
4791a646342SBen Skeggs 
4801a646342SBen Skeggs 		tv_regs->ptv_20c = 0x808010;
4811a646342SBen Skeggs 		tv_regs->ptv_304 = 0x2d00000;
4821a646342SBen Skeggs 		tv_regs->ptv_600 = 0x0;
4831a646342SBen Skeggs 		tv_regs->ptv_60c = 0x0;
4841a646342SBen Skeggs 		tv_regs->ptv_610 = 0x1e00000;
4851a646342SBen Skeggs 
4861a646342SBen Skeggs 		if (tv_norm->tv_enc_mode.vdisplay == 576) {
4871a646342SBen Skeggs 			tv_regs->ptv_508 = 0x1200000;
4881a646342SBen Skeggs 			tv_regs->ptv_614 = 0x33;
4891a646342SBen Skeggs 
4901a646342SBen Skeggs 		} else if (tv_norm->tv_enc_mode.vdisplay == 480) {
4911a646342SBen Skeggs 			tv_regs->ptv_508 = 0xf00000;
4921a646342SBen Skeggs 			tv_regs->ptv_614 = 0x13;
4931a646342SBen Skeggs 		}
4941a646342SBen Skeggs 
495967e7bdeSBen Skeggs 		if (drm->device.info.family >= NV_DEVICE_INFO_V0_RANKINE) {
4961a646342SBen Skeggs 			tv_regs->ptv_500 = 0xe8e0;
4971a646342SBen Skeggs 			tv_regs->ptv_504 = 0x1710;
4981a646342SBen Skeggs 			tv_regs->ptv_604 = 0x0;
4991a646342SBen Skeggs 			tv_regs->ptv_608 = 0x0;
5001a646342SBen Skeggs 		} else {
5011a646342SBen Skeggs 			if (tv_norm->tv_enc_mode.vdisplay == 576) {
5021a646342SBen Skeggs 				tv_regs->ptv_604 = 0x20;
5031a646342SBen Skeggs 				tv_regs->ptv_608 = 0x10;
5041a646342SBen Skeggs 				tv_regs->ptv_500 = 0x19710;
5051a646342SBen Skeggs 				tv_regs->ptv_504 = 0x68f0;
5061a646342SBen Skeggs 
5071a646342SBen Skeggs 			} else if (tv_norm->tv_enc_mode.vdisplay == 480) {
5081a646342SBen Skeggs 				tv_regs->ptv_604 = 0x10;
5091a646342SBen Skeggs 				tv_regs->ptv_608 = 0x20;
5101a646342SBen Skeggs 				tv_regs->ptv_500 = 0x4b90;
5111a646342SBen Skeggs 				tv_regs->ptv_504 = 0x1b480;
5121a646342SBen Skeggs 			}
5131a646342SBen Skeggs 		}
5141a646342SBen Skeggs 
5151a646342SBen Skeggs 		for (i = 0; i < 0x40; i++)
5161a646342SBen Skeggs 			tv_regs->tv_enc[i] = tv_norm->tv_enc_mode.tv_enc[i];
5171a646342SBen Skeggs 
5181a646342SBen Skeggs 	} else {
5191a646342SBen Skeggs 		struct drm_display_mode *output_mode =
5201a646342SBen Skeggs 						&tv_norm->ctv_enc_mode.mode;
5211a646342SBen Skeggs 
5221a646342SBen Skeggs 		/* The registers in PRAMDAC+0xc00 control some timings and CSC
5231a646342SBen Skeggs 		 * parameters for the CTV encoder (It's only used for "HD" TV
5241a646342SBen Skeggs 		 * modes, I don't think I have enough working to guess what
5251a646342SBen Skeggs 		 * they exactly mean...), it's probably connected at the
5261a646342SBen Skeggs 		 * output of the FP encoder, but it also needs the analog
5271a646342SBen Skeggs 		 * encoder in its OR enabled and routed to the head it's
5281a646342SBen Skeggs 		 * using. It's enabled with the DACCLK register, bits [5:4].
5291a646342SBen Skeggs 		 */
5301a646342SBen Skeggs 		for (i = 0; i < 38; i++)
5311a646342SBen Skeggs 			regs->ctv_regs[i] = tv_norm->ctv_enc_mode.ctv_regs[i];
5321a646342SBen Skeggs 
5331a646342SBen Skeggs 		regs->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
5341a646342SBen Skeggs 		regs->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
5351a646342SBen Skeggs 		regs->fp_horiz_regs[FP_SYNC_START] =
5361a646342SBen Skeggs 						output_mode->hsync_start - 1;
5371a646342SBen Skeggs 		regs->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
5381a646342SBen Skeggs 		regs->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay +
5391a646342SBen Skeggs 			max((output_mode->hdisplay-600)/40 - 1, 1);
5401a646342SBen Skeggs 
5411a646342SBen Skeggs 		regs->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
5421a646342SBen Skeggs 		regs->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
5431a646342SBen Skeggs 		regs->fp_vert_regs[FP_SYNC_START] =
5441a646342SBen Skeggs 						output_mode->vsync_start - 1;
5451a646342SBen Skeggs 		regs->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
5461a646342SBen Skeggs 		regs->fp_vert_regs[FP_CRTC] = output_mode->vdisplay - 1;
5471a646342SBen Skeggs 
5481a646342SBen Skeggs 		regs->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
5491a646342SBen Skeggs 			NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
5501a646342SBen Skeggs 			NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
5511a646342SBen Skeggs 
5521a646342SBen Skeggs 		if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
5531a646342SBen Skeggs 			regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
5541a646342SBen Skeggs 		if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
5551a646342SBen Skeggs 			regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
5561a646342SBen Skeggs 
5571a646342SBen Skeggs 		regs->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
5581a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
5591a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
5601a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
5611a646342SBen Skeggs 			NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
5621a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
5631a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
5641a646342SBen Skeggs 
5651a646342SBen Skeggs 		regs->fp_debug_2 = 0;
5661a646342SBen Skeggs 
5671a646342SBen Skeggs 		regs->fp_margin_color = 0x801080;
5681a646342SBen Skeggs 
5691a646342SBen Skeggs 	}
5701a646342SBen Skeggs }
5711a646342SBen Skeggs 
5721a646342SBen Skeggs static void nv17_tv_commit(struct drm_encoder *encoder)
5731a646342SBen Skeggs {
5741a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
5751a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
5761a646342SBen Skeggs 	struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
5771a646342SBen Skeggs 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
578d58ded76SJani Nikula 	const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
5791a646342SBen Skeggs 
5801a646342SBen Skeggs 	if (get_tv_norm(encoder)->kind == TV_ENC_MODE) {
5811a646342SBen Skeggs 		nv17_tv_update_rescaler(encoder);
5821a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
5831a646342SBen Skeggs 	} else {
5841a646342SBen Skeggs 		nv17_ctv_update_rescaler(encoder);
5851a646342SBen Skeggs 	}
5861a646342SBen Skeggs 
5871a646342SBen Skeggs 	nv17_tv_state_load(dev, &to_tv_enc(encoder)->state);
5881a646342SBen Skeggs 
5891a646342SBen Skeggs 	/* This could use refinement for flatpanels, but it should work */
590967e7bdeSBen Skeggs 	if (drm->device.info.chipset < 0x44)
5911a646342SBen Skeggs 		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
5921a646342SBen Skeggs 					nv04_dac_output_offset(encoder),
5931a646342SBen Skeggs 					0xf0000000);
5941a646342SBen Skeggs 	else
5951a646342SBen Skeggs 		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
5961a646342SBen Skeggs 					nv04_dac_output_offset(encoder),
5971a646342SBen Skeggs 					0x00100000);
5981a646342SBen Skeggs 
5991a646342SBen Skeggs 	helper->dpms(encoder, DRM_MODE_DPMS_ON);
6001a646342SBen Skeggs 
6011a646342SBen Skeggs 	NV_INFO(drm, "Output %s is running on CRTC %d using output %c\n",
6028c6c361aSJani Nikula 		nouveau_encoder_connector_get(nv_encoder)->base.name,
6031a646342SBen Skeggs 		nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
6041a646342SBen Skeggs }
6051a646342SBen Skeggs 
6061a646342SBen Skeggs static void nv17_tv_save(struct drm_encoder *encoder)
6071a646342SBen Skeggs {
6081a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6091a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6101a646342SBen Skeggs 
6111a646342SBen Skeggs 	nouveau_encoder(encoder)->restore.output =
6121a646342SBen Skeggs 					NVReadRAMDAC(dev, 0,
6131a646342SBen Skeggs 					NV_PRAMDAC_DACCLK +
6141a646342SBen Skeggs 					nv04_dac_output_offset(encoder));
6151a646342SBen Skeggs 
6161a646342SBen Skeggs 	nv17_tv_state_save(dev, &tv_enc->saved_state);
6171a646342SBen Skeggs 
6181a646342SBen Skeggs 	tv_enc->state.ptv_200 = tv_enc->saved_state.ptv_200;
6191a646342SBen Skeggs }
6201a646342SBen Skeggs 
6211a646342SBen Skeggs static void nv17_tv_restore(struct drm_encoder *encoder)
6221a646342SBen Skeggs {
6231a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6241a646342SBen Skeggs 
6251a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK +
6261a646342SBen Skeggs 				nv04_dac_output_offset(encoder),
6271a646342SBen Skeggs 				nouveau_encoder(encoder)->restore.output);
6281a646342SBen Skeggs 
6291a646342SBen Skeggs 	nv17_tv_state_load(dev, &to_tv_enc(encoder)->saved_state);
6301a646342SBen Skeggs 
6311a646342SBen Skeggs 	nouveau_encoder(encoder)->last_dpms = NV_DPMS_CLEARED;
6321a646342SBen Skeggs }
6331a646342SBen Skeggs 
6341a646342SBen Skeggs static int nv17_tv_create_resources(struct drm_encoder *encoder,
6351a646342SBen Skeggs 				    struct drm_connector *connector)
6361a646342SBen Skeggs {
6371a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6381a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
6391a646342SBen Skeggs 	struct drm_mode_config *conf = &dev->mode_config;
6401a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6411a646342SBen Skeggs 	struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
6421a646342SBen Skeggs 	int num_tv_norms = dcb->tvconf.has_component_output ? NUM_TV_NORMS :
6431a646342SBen Skeggs 							NUM_LD_TV_NORMS;
6441a646342SBen Skeggs 	int i;
6451a646342SBen Skeggs 
6461a646342SBen Skeggs 	if (nouveau_tv_norm) {
6471a646342SBen Skeggs 		for (i = 0; i < num_tv_norms; i++) {
6481a646342SBen Skeggs 			if (!strcmp(nv17_tv_norm_names[i], nouveau_tv_norm)) {
6491a646342SBen Skeggs 				tv_enc->tv_norm = i;
6501a646342SBen Skeggs 				break;
6511a646342SBen Skeggs 			}
6521a646342SBen Skeggs 		}
6531a646342SBen Skeggs 
6541a646342SBen Skeggs 		if (i == num_tv_norms)
6551a646342SBen Skeggs 			NV_WARN(drm, "Invalid TV norm setting \"%s\"\n",
6561a646342SBen Skeggs 				nouveau_tv_norm);
6571a646342SBen Skeggs 	}
6581a646342SBen Skeggs 
6591a646342SBen Skeggs 	drm_mode_create_tv_properties(dev, num_tv_norms, nv17_tv_norm_names);
6601a646342SBen Skeggs 
6611a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6621a646342SBen Skeggs 					conf->tv_select_subconnector_property,
6631a646342SBen Skeggs 					tv_enc->select_subconnector);
6641a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6651a646342SBen Skeggs 					conf->tv_subconnector_property,
6661a646342SBen Skeggs 					tv_enc->subconnector);
6671a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6681a646342SBen Skeggs 					conf->tv_mode_property,
6691a646342SBen Skeggs 					tv_enc->tv_norm);
6701a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6711a646342SBen Skeggs 					conf->tv_flicker_reduction_property,
6721a646342SBen Skeggs 					tv_enc->flicker);
6731a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6741a646342SBen Skeggs 					conf->tv_saturation_property,
6751a646342SBen Skeggs 					tv_enc->saturation);
6761a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6771a646342SBen Skeggs 					conf->tv_hue_property,
6781a646342SBen Skeggs 					tv_enc->hue);
6791a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6801a646342SBen Skeggs 					conf->tv_overscan_property,
6811a646342SBen Skeggs 					tv_enc->overscan);
6821a646342SBen Skeggs 
6831a646342SBen Skeggs 	return 0;
6841a646342SBen Skeggs }
6851a646342SBen Skeggs 
6861a646342SBen Skeggs static int nv17_tv_set_property(struct drm_encoder *encoder,
6871a646342SBen Skeggs 				struct drm_connector *connector,
6881a646342SBen Skeggs 				struct drm_property *property,
6891a646342SBen Skeggs 				uint64_t val)
6901a646342SBen Skeggs {
6911a646342SBen Skeggs 	struct drm_mode_config *conf = &encoder->dev->mode_config;
6921a646342SBen Skeggs 	struct drm_crtc *crtc = encoder->crtc;
6931a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6941a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
6951a646342SBen Skeggs 	bool modes_changed = false;
6961a646342SBen Skeggs 
6971a646342SBen Skeggs 	if (property == conf->tv_overscan_property) {
6981a646342SBen Skeggs 		tv_enc->overscan = val;
6991a646342SBen Skeggs 		if (encoder->crtc) {
7001a646342SBen Skeggs 			if (tv_norm->kind == CTV_ENC_MODE)
7011a646342SBen Skeggs 				nv17_ctv_update_rescaler(encoder);
7021a646342SBen Skeggs 			else
7031a646342SBen Skeggs 				nv17_tv_update_rescaler(encoder);
7041a646342SBen Skeggs 		}
7051a646342SBen Skeggs 
7061a646342SBen Skeggs 	} else if (property == conf->tv_saturation_property) {
7071a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7081a646342SBen Skeggs 			return -EINVAL;
7091a646342SBen Skeggs 
7101a646342SBen Skeggs 		tv_enc->saturation = val;
7111a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7121a646342SBen Skeggs 
7131a646342SBen Skeggs 	} else if (property == conf->tv_hue_property) {
7141a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7151a646342SBen Skeggs 			return -EINVAL;
7161a646342SBen Skeggs 
7171a646342SBen Skeggs 		tv_enc->hue = val;
7181a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7191a646342SBen Skeggs 
7201a646342SBen Skeggs 	} else if (property == conf->tv_flicker_reduction_property) {
7211a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7221a646342SBen Skeggs 			return -EINVAL;
7231a646342SBen Skeggs 
7241a646342SBen Skeggs 		tv_enc->flicker = val;
7251a646342SBen Skeggs 		if (encoder->crtc)
7261a646342SBen Skeggs 			nv17_tv_update_rescaler(encoder);
7271a646342SBen Skeggs 
7281a646342SBen Skeggs 	} else if (property == conf->tv_mode_property) {
7291a646342SBen Skeggs 		if (connector->dpms != DRM_MODE_DPMS_OFF)
7301a646342SBen Skeggs 			return -EINVAL;
7311a646342SBen Skeggs 
7321a646342SBen Skeggs 		tv_enc->tv_norm = val;
7331a646342SBen Skeggs 
7341a646342SBen Skeggs 		modes_changed = true;
7351a646342SBen Skeggs 
7361a646342SBen Skeggs 	} else if (property == conf->tv_select_subconnector_property) {
7371a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7381a646342SBen Skeggs 			return -EINVAL;
7391a646342SBen Skeggs 
7401a646342SBen Skeggs 		tv_enc->select_subconnector = val;
7411a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7421a646342SBen Skeggs 
7431a646342SBen Skeggs 	} else {
7441a646342SBen Skeggs 		return -EINVAL;
7451a646342SBen Skeggs 	}
7461a646342SBen Skeggs 
7471a646342SBen Skeggs 	if (modes_changed) {
7481a646342SBen Skeggs 		drm_helper_probe_single_connector_modes(connector, 0, 0);
7491a646342SBen Skeggs 
7501a646342SBen Skeggs 		/* Disable the crtc to ensure a full modeset is
7511a646342SBen Skeggs 		 * performed whenever it's turned on again. */
7521a646342SBen Skeggs 		if (crtc) {
7531a646342SBen Skeggs 			struct drm_mode_set modeset = {
7541a646342SBen Skeggs 				.crtc = crtc,
7551a646342SBen Skeggs 			};
7561a646342SBen Skeggs 
7571a646342SBen Skeggs 			drm_mode_set_config_internal(&modeset);
7581a646342SBen Skeggs 		}
7591a646342SBen Skeggs 	}
7601a646342SBen Skeggs 
7611a646342SBen Skeggs 	return 0;
7621a646342SBen Skeggs }
7631a646342SBen Skeggs 
7641a646342SBen Skeggs static void nv17_tv_destroy(struct drm_encoder *encoder)
7651a646342SBen Skeggs {
7661a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
7671a646342SBen Skeggs 
7681a646342SBen Skeggs 	drm_encoder_cleanup(encoder);
7691a646342SBen Skeggs 	kfree(tv_enc);
7701a646342SBen Skeggs }
7711a646342SBen Skeggs 
772ebb79a32SVille Syrjälä static const struct drm_encoder_helper_funcs nv17_tv_helper_funcs = {
7731a646342SBen Skeggs 	.dpms = nv17_tv_dpms,
7741a646342SBen Skeggs 	.mode_fixup = nv17_tv_mode_fixup,
7751a646342SBen Skeggs 	.prepare = nv17_tv_prepare,
7761a646342SBen Skeggs 	.commit = nv17_tv_commit,
7771a646342SBen Skeggs 	.mode_set = nv17_tv_mode_set,
7781a646342SBen Skeggs 	.detect = nv17_tv_detect,
7791a646342SBen Skeggs };
7801a646342SBen Skeggs 
781ebb79a32SVille Syrjälä static const struct drm_encoder_slave_funcs nv17_tv_slave_funcs = {
7821a646342SBen Skeggs 	.get_modes = nv17_tv_get_modes,
7831a646342SBen Skeggs 	.mode_valid = nv17_tv_mode_valid,
7841a646342SBen Skeggs 	.create_resources = nv17_tv_create_resources,
7851a646342SBen Skeggs 	.set_property = nv17_tv_set_property,
7861a646342SBen Skeggs };
7871a646342SBen Skeggs 
788ebb79a32SVille Syrjälä static const struct drm_encoder_funcs nv17_tv_funcs = {
7891a646342SBen Skeggs 	.destroy = nv17_tv_destroy,
7901a646342SBen Skeggs };
7911a646342SBen Skeggs 
7921a646342SBen Skeggs int
7931a646342SBen Skeggs nv17_tv_create(struct drm_connector *connector, struct dcb_output *entry)
7941a646342SBen Skeggs {
7951a646342SBen Skeggs 	struct drm_device *dev = connector->dev;
7961a646342SBen Skeggs 	struct drm_encoder *encoder;
7971a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = NULL;
7981a646342SBen Skeggs 
7991a646342SBen Skeggs 	tv_enc = kzalloc(sizeof(*tv_enc), GFP_KERNEL);
8001a646342SBen Skeggs 	if (!tv_enc)
8011a646342SBen Skeggs 		return -ENOMEM;
8021a646342SBen Skeggs 
8031a646342SBen Skeggs 	tv_enc->overscan = 50;
8041a646342SBen Skeggs 	tv_enc->flicker = 50;
8051a646342SBen Skeggs 	tv_enc->saturation = 50;
8061a646342SBen Skeggs 	tv_enc->hue = 0;
8071a646342SBen Skeggs 	tv_enc->tv_norm = TV_NORM_PAL;
8081a646342SBen Skeggs 	tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
8091a646342SBen Skeggs 	tv_enc->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
8101a646342SBen Skeggs 	tv_enc->pin_mask = 0;
8111a646342SBen Skeggs 
8121a646342SBen Skeggs 	encoder = to_drm_encoder(&tv_enc->base);
8131a646342SBen Skeggs 
8141a646342SBen Skeggs 	tv_enc->base.dcb = entry;
8151a646342SBen Skeggs 	tv_enc->base.or = ffs(entry->or) - 1;
8161a646342SBen Skeggs 
81713a3d91fSVille Syrjälä 	drm_encoder_init(dev, encoder, &nv17_tv_funcs, DRM_MODE_ENCODER_TVDAC,
81813a3d91fSVille Syrjälä 			 NULL);
8191a646342SBen Skeggs 	drm_encoder_helper_add(encoder, &nv17_tv_helper_funcs);
8201a646342SBen Skeggs 	to_encoder_slave(encoder)->slave_funcs = &nv17_tv_slave_funcs;
8211a646342SBen Skeggs 
822129b7820SDaniel Vetter 	tv_enc->base.enc_save = nv17_tv_save;
823129b7820SDaniel Vetter 	tv_enc->base.enc_restore = nv17_tv_restore;
824129b7820SDaniel Vetter 
8251a646342SBen Skeggs 	encoder->possible_crtcs = entry->heads;
8261a646342SBen Skeggs 	encoder->possible_clones = 0;
8271a646342SBen Skeggs 
8281a646342SBen Skeggs 	nv17_tv_create_resources(encoder, connector);
8291a646342SBen Skeggs 	drm_mode_connector_attach_encoder(connector, encoder);
8301a646342SBen Skeggs 	return 0;
8311a646342SBen Skeggs }
832