xref: /openbmc/linux/drivers/gpu/drm/nouveau/dispnv04/tvnv17.c (revision f91ca89e924eb287915522664a31afc71a49c05b)
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/drm_crtc_helper.h>
28874ee2d6SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
29fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
304dc28134SBen Skeggs #include "nouveau_drv.h"
311a646342SBen Skeggs #include "nouveau_reg.h"
321a646342SBen Skeggs #include "nouveau_encoder.h"
331a646342SBen Skeggs #include "nouveau_connector.h"
341a646342SBen Skeggs #include "nouveau_crtc.h"
351a646342SBen Skeggs #include "hw.h"
361a646342SBen Skeggs #include "tvnv17.h"
371a646342SBen Skeggs 
381a646342SBen Skeggs MODULE_PARM_DESC(tv_norm, "Default TV norm.\n"
391a646342SBen Skeggs 		 "\t\tSupported: PAL, PAL-M, PAL-N, PAL-Nc, NTSC-M, NTSC-J,\n"
401a646342SBen Skeggs 		 "\t\t\thd480i, hd480p, hd576i, hd576p, hd720p, hd1080i.\n"
411a646342SBen Skeggs 		 "\t\tDefault: PAL\n"
421a646342SBen Skeggs 		 "\t\t*NOTE* Ignored for cards with external TV encoders.");
431a646342SBen Skeggs static char *nouveau_tv_norm;
441a646342SBen Skeggs module_param_named(tv_norm, nouveau_tv_norm, charp, 0400);
451a646342SBen Skeggs 
nv42_tv_sample_load(struct drm_encoder * encoder)461a646342SBen Skeggs static uint32_t nv42_tv_sample_load(struct drm_encoder *encoder)
471a646342SBen Skeggs {
481a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
491a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
501167c6bcSBen Skeggs 	struct nvkm_gpio *gpio = nvxx_gpio(&drm->client.device);
511a646342SBen Skeggs 	uint32_t testval, regoffset = nv04_dac_output_offset(encoder);
521a646342SBen Skeggs 	uint32_t gpio0, gpio1, fp_htotal, fp_hsync_start, fp_hsync_end,
531a646342SBen Skeggs 		fp_control, test_ctrl, dacclk, ctv_14, ctv_1c, ctv_6c;
541a646342SBen Skeggs 	uint32_t sample = 0;
551a646342SBen Skeggs 	int head;
561a646342SBen Skeggs 
571a646342SBen Skeggs #define RGB_TEST_DATA(r, g, b) (r << 0 | g << 10 | b << 20)
581a646342SBen Skeggs 	testval = RGB_TEST_DATA(0x82, 0xeb, 0x82);
591a646342SBen Skeggs 	if (drm->vbios.tvdactestval)
601a646342SBen Skeggs 		testval = drm->vbios.tvdactestval;
611a646342SBen Skeggs 
621a646342SBen Skeggs 	dacclk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset);
631a646342SBen Skeggs 	head = (dacclk & 0x100) >> 8;
641a646342SBen Skeggs 
651a646342SBen Skeggs 	/* Save the previous state. */
662ea7249fSBen Skeggs 	gpio1 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC1, 0xff);
672ea7249fSBen Skeggs 	gpio0 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC0, 0xff);
681a646342SBen Skeggs 	fp_htotal = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL);
691a646342SBen Skeggs 	fp_hsync_start = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START);
701a646342SBen Skeggs 	fp_hsync_end = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END);
711a646342SBen Skeggs 	fp_control = NVReadRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL);
721a646342SBen Skeggs 	test_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
731a646342SBen Skeggs 	ctv_1c = NVReadRAMDAC(dev, head, 0x680c1c);
741a646342SBen Skeggs 	ctv_14 = NVReadRAMDAC(dev, head, 0x680c14);
751a646342SBen Skeggs 	ctv_6c = NVReadRAMDAC(dev, head, 0x680c6c);
761a646342SBen Skeggs 
771a646342SBen Skeggs 	/* Prepare the DAC for load detection.  */
782ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, true);
792ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, true);
801a646342SBen Skeggs 
811a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, 1343);
821a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, 1047);
831a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, 1183);
841a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL,
851a646342SBen Skeggs 		      NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
861a646342SBen Skeggs 		      NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12 |
871a646342SBen Skeggs 		      NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
881a646342SBen Skeggs 		      NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS |
891a646342SBen Skeggs 		      NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS);
901a646342SBen Skeggs 
911a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, 0);
921a646342SBen Skeggs 
931a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
941a646342SBen Skeggs 		      (dacclk & ~0xff) | 0x22);
951a646342SBen Skeggs 	msleep(1);
961a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset,
971a646342SBen Skeggs 		      (dacclk & ~0xff) | 0x21);
981a646342SBen Skeggs 
991a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c1c, 1 << 20);
1001a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c14, 4 << 16);
1011a646342SBen Skeggs 
1021a646342SBen Skeggs 	/* Sample pin 0x4 (usually S-video luma). */
1031a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c6c, testval >> 10 & 0x3ff);
1041a646342SBen Skeggs 	msleep(20);
1051a646342SBen Skeggs 	sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
1061a646342SBen Skeggs 		& 0x4 << 28;
1071a646342SBen Skeggs 
1081a646342SBen Skeggs 	/* Sample the remaining pins. */
1091a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c6c, testval & 0x3ff);
1101a646342SBen Skeggs 	msleep(20);
1111a646342SBen Skeggs 	sample |= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset)
1121a646342SBen Skeggs 		& 0xa << 28;
1131a646342SBen Skeggs 
1141a646342SBen Skeggs 	/* Restore the previous state. */
1151a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c1c, ctv_1c);
1161a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c14, ctv_14);
1171a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, 0x680c6c, ctv_6c);
1181a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, dacclk);
1191a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, test_ctrl);
1201a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_TG_CONTROL, fp_control);
1211a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_END, fp_hsync_end);
1221a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HSYNC_START, fp_hsync_start);
1231a646342SBen Skeggs 	NVWriteRAMDAC(dev, head, NV_PRAMDAC_FP_HTOTAL, fp_htotal);
1242ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, gpio1);
1252ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, gpio0);
1261a646342SBen Skeggs 
1271a646342SBen Skeggs 	return sample;
1281a646342SBen Skeggs }
1291a646342SBen Skeggs 
1301a646342SBen Skeggs static bool
get_tv_detect_quirks(struct drm_device * dev,uint32_t * pin_mask)1311a646342SBen Skeggs get_tv_detect_quirks(struct drm_device *dev, uint32_t *pin_mask)
1321a646342SBen Skeggs {
1331a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
1341167c6bcSBen Skeggs 	struct nvkm_device *device = nvxx_device(&drm->client.device);
1351a646342SBen Skeggs 
136c7af0ff0SBen Skeggs 	if (device->quirk && device->quirk->tv_pin_mask) {
137c7af0ff0SBen Skeggs 		*pin_mask = device->quirk->tv_pin_mask;
1381a646342SBen Skeggs 		return false;
1391a646342SBen Skeggs 	}
1401a646342SBen Skeggs 
1411a646342SBen Skeggs 	return true;
1421a646342SBen Skeggs }
1431a646342SBen Skeggs 
1441a646342SBen Skeggs static enum drm_connector_status
nv17_tv_detect(struct drm_encoder * encoder,struct drm_connector * connector)1451a646342SBen Skeggs nv17_tv_detect(struct drm_encoder *encoder, struct drm_connector *connector)
1461a646342SBen Skeggs {
1471a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
1481a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
1491a646342SBen Skeggs 	struct drm_mode_config *conf = &dev->mode_config;
1501a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
1511a646342SBen Skeggs 	struct dcb_output *dcb = tv_enc->base.dcb;
1521a646342SBen Skeggs 	bool reliable = get_tv_detect_quirks(dev, &tv_enc->pin_mask);
1531a646342SBen Skeggs 
1541a646342SBen Skeggs 	if (nv04_dac_in_use(encoder))
1551a646342SBen Skeggs 		return connector_status_disconnected;
1561a646342SBen Skeggs 
1571a646342SBen Skeggs 	if (reliable) {
1581167c6bcSBen Skeggs 		if (drm->client.device.info.chipset == 0x42 ||
1591167c6bcSBen Skeggs 		    drm->client.device.info.chipset == 0x43)
1601a646342SBen Skeggs 			tv_enc->pin_mask =
1611a646342SBen Skeggs 				nv42_tv_sample_load(encoder) >> 28 & 0xe;
1621a646342SBen Skeggs 		else
1631a646342SBen Skeggs 			tv_enc->pin_mask =
1641a646342SBen Skeggs 				nv17_dac_sample_load(encoder) >> 28 & 0xe;
1651a646342SBen Skeggs 	}
1661a646342SBen Skeggs 
1671a646342SBen Skeggs 	switch (tv_enc->pin_mask) {
1681a646342SBen Skeggs 	case 0x2:
1691a646342SBen Skeggs 	case 0x4:
1701a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Composite;
1711a646342SBen Skeggs 		break;
1721a646342SBen Skeggs 	case 0xc:
1731a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SVIDEO;
1741a646342SBen Skeggs 		break;
1751a646342SBen Skeggs 	case 0xe:
1761a646342SBen Skeggs 		if (dcb->tvconf.has_component_output)
1771a646342SBen Skeggs 			tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Component;
1781a646342SBen Skeggs 		else
1791a646342SBen Skeggs 			tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_SCART;
1801a646342SBen Skeggs 		break;
1811a646342SBen Skeggs 	default:
1821a646342SBen Skeggs 		tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
1831a646342SBen Skeggs 		break;
1841a646342SBen Skeggs 	}
1851a646342SBen Skeggs 
1861a646342SBen Skeggs 	drm_object_property_set_value(&connector->base,
1871a646342SBen Skeggs 					 conf->tv_subconnector_property,
1881a646342SBen Skeggs 					 tv_enc->subconnector);
1891a646342SBen Skeggs 
1901a646342SBen Skeggs 	if (!reliable) {
1911a646342SBen Skeggs 		return connector_status_unknown;
1921a646342SBen Skeggs 	} else if (tv_enc->subconnector) {
1931a646342SBen Skeggs 		NV_INFO(drm, "Load detected on output %c\n",
1941a646342SBen Skeggs 			'@' + ffs(dcb->or));
1951a646342SBen Skeggs 		return connector_status_connected;
1961a646342SBen Skeggs 	} else {
1971a646342SBen Skeggs 		return connector_status_disconnected;
1981a646342SBen Skeggs 	}
1991a646342SBen Skeggs }
2001a646342SBen Skeggs 
nv17_tv_get_ld_modes(struct drm_encoder * encoder,struct drm_connector * connector)2011a646342SBen Skeggs static int nv17_tv_get_ld_modes(struct drm_encoder *encoder,
2021a646342SBen Skeggs 				struct drm_connector *connector)
2031a646342SBen Skeggs {
2041a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
2051a646342SBen Skeggs 	const struct drm_display_mode *tv_mode;
2061a646342SBen Skeggs 	int n = 0;
2071a646342SBen Skeggs 
2081a646342SBen Skeggs 	for (tv_mode = nv17_tv_modes; tv_mode->hdisplay; tv_mode++) {
2091a646342SBen Skeggs 		struct drm_display_mode *mode;
2101a646342SBen Skeggs 
2111a646342SBen Skeggs 		mode = drm_mode_duplicate(encoder->dev, tv_mode);
212cb751e48SMa Ke 		if (!mode)
213cb751e48SMa Ke 			continue;
2141a646342SBen Skeggs 
2151a646342SBen Skeggs 		mode->clock = tv_norm->tv_enc_mode.vrefresh *
2161a646342SBen Skeggs 			mode->htotal / 1000 *
2171a646342SBen Skeggs 			mode->vtotal / 1000;
2181a646342SBen Skeggs 
2191a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
2201a646342SBen Skeggs 			mode->clock *= 2;
2211a646342SBen Skeggs 
2221a646342SBen Skeggs 		if (mode->hdisplay == tv_norm->tv_enc_mode.hdisplay &&
2231a646342SBen Skeggs 		    mode->vdisplay == tv_norm->tv_enc_mode.vdisplay)
2241a646342SBen Skeggs 			mode->type |= DRM_MODE_TYPE_PREFERRED;
2251a646342SBen Skeggs 
2261a646342SBen Skeggs 		drm_mode_probed_add(connector, mode);
2271a646342SBen Skeggs 		n++;
2281a646342SBen Skeggs 	}
2291a646342SBen Skeggs 
2301a646342SBen Skeggs 	return n;
2311a646342SBen Skeggs }
2321a646342SBen Skeggs 
nv17_tv_get_hd_modes(struct drm_encoder * encoder,struct drm_connector * connector)2331a646342SBen Skeggs static int nv17_tv_get_hd_modes(struct drm_encoder *encoder,
2341a646342SBen Skeggs 				struct drm_connector *connector)
2351a646342SBen Skeggs {
2361a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
2371a646342SBen Skeggs 	struct drm_display_mode *output_mode = &tv_norm->ctv_enc_mode.mode;
2381a646342SBen Skeggs 	struct drm_display_mode *mode;
2391a646342SBen Skeggs 	const struct {
2401a646342SBen Skeggs 		int hdisplay;
2411a646342SBen Skeggs 		int vdisplay;
2421a646342SBen Skeggs 	} modes[] = {
2431a646342SBen Skeggs 		{ 640, 400 },
2441a646342SBen Skeggs 		{ 640, 480 },
2451a646342SBen Skeggs 		{ 720, 480 },
2461a646342SBen Skeggs 		{ 720, 576 },
2471a646342SBen Skeggs 		{ 800, 600 },
2481a646342SBen Skeggs 		{ 1024, 768 },
2491a646342SBen Skeggs 		{ 1280, 720 },
2501a646342SBen Skeggs 		{ 1280, 1024 },
2511a646342SBen Skeggs 		{ 1920, 1080 }
2521a646342SBen Skeggs 	};
2531a646342SBen Skeggs 	int i, n = 0;
2541a646342SBen Skeggs 
2551a646342SBen Skeggs 	for (i = 0; i < ARRAY_SIZE(modes); i++) {
2561a646342SBen Skeggs 		if (modes[i].hdisplay > output_mode->hdisplay ||
2571a646342SBen Skeggs 		    modes[i].vdisplay > output_mode->vdisplay)
2581a646342SBen Skeggs 			continue;
2591a646342SBen Skeggs 
2601a646342SBen Skeggs 		if (modes[i].hdisplay == output_mode->hdisplay &&
2611a646342SBen Skeggs 		    modes[i].vdisplay == output_mode->vdisplay) {
2621a646342SBen Skeggs 			mode = drm_mode_duplicate(encoder->dev, output_mode);
263*7ece609bSMa Ke 			if (!mode)
264*7ece609bSMa Ke 				continue;
2651a646342SBen Skeggs 			mode->type |= DRM_MODE_TYPE_PREFERRED;
2661a646342SBen Skeggs 
2671a646342SBen Skeggs 		} else {
2681a646342SBen Skeggs 			mode = drm_cvt_mode(encoder->dev, modes[i].hdisplay,
2691a646342SBen Skeggs 					    modes[i].vdisplay, 60, false,
2701a646342SBen Skeggs 					    (output_mode->flags &
2711a646342SBen Skeggs 					     DRM_MODE_FLAG_INTERLACE), false);
272*7ece609bSMa Ke 			if (!mode)
273*7ece609bSMa Ke 				continue;
2741a646342SBen Skeggs 		}
2751a646342SBen Skeggs 
2761a646342SBen Skeggs 		/* CVT modes are sometimes unsuitable... */
2771a646342SBen Skeggs 		if (output_mode->hdisplay <= 720
2781a646342SBen Skeggs 		    || output_mode->hdisplay >= 1920) {
2791a646342SBen Skeggs 			mode->htotal = output_mode->htotal;
2801a646342SBen Skeggs 			mode->hsync_start = (mode->hdisplay + (mode->htotal
2811a646342SBen Skeggs 					     - mode->hdisplay) * 9 / 10) & ~7;
2821a646342SBen Skeggs 			mode->hsync_end = mode->hsync_start + 8;
2831a646342SBen Skeggs 		}
2841a646342SBen Skeggs 
2851a646342SBen Skeggs 		if (output_mode->vdisplay >= 1024) {
2861a646342SBen Skeggs 			mode->vtotal = output_mode->vtotal;
2871a646342SBen Skeggs 			mode->vsync_start = output_mode->vsync_start;
2881a646342SBen Skeggs 			mode->vsync_end = output_mode->vsync_end;
2891a646342SBen Skeggs 		}
2901a646342SBen Skeggs 
2911a646342SBen Skeggs 		mode->type |= DRM_MODE_TYPE_DRIVER;
2921a646342SBen Skeggs 		drm_mode_probed_add(connector, mode);
2931a646342SBen Skeggs 		n++;
2941a646342SBen Skeggs 	}
2951a646342SBen Skeggs 
2961a646342SBen Skeggs 	return n;
2971a646342SBen Skeggs }
2981a646342SBen Skeggs 
nv17_tv_get_modes(struct drm_encoder * encoder,struct drm_connector * connector)2991a646342SBen Skeggs static int nv17_tv_get_modes(struct drm_encoder *encoder,
3001a646342SBen Skeggs 			     struct drm_connector *connector)
3011a646342SBen Skeggs {
3021a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3031a646342SBen Skeggs 
3041a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
3051a646342SBen Skeggs 		return nv17_tv_get_hd_modes(encoder, connector);
3061a646342SBen Skeggs 	else
3071a646342SBen Skeggs 		return nv17_tv_get_ld_modes(encoder, connector);
3081a646342SBen Skeggs }
3091a646342SBen Skeggs 
nv17_tv_mode_valid(struct drm_encoder * encoder,struct drm_display_mode * mode)3101a646342SBen Skeggs static int nv17_tv_mode_valid(struct drm_encoder *encoder,
3111a646342SBen Skeggs 			      struct drm_display_mode *mode)
3121a646342SBen Skeggs {
3131a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3141a646342SBen Skeggs 
3151a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
3161a646342SBen Skeggs 		struct drm_display_mode *output_mode =
3171a646342SBen Skeggs 						&tv_norm->ctv_enc_mode.mode;
3181a646342SBen Skeggs 
3191a646342SBen Skeggs 		if (mode->clock > 400000)
3201a646342SBen Skeggs 			return MODE_CLOCK_HIGH;
3211a646342SBen Skeggs 
3221a646342SBen Skeggs 		if (mode->hdisplay > output_mode->hdisplay ||
3231a646342SBen Skeggs 		    mode->vdisplay > output_mode->vdisplay)
3241a646342SBen Skeggs 			return MODE_BAD;
3251a646342SBen Skeggs 
3261a646342SBen Skeggs 		if ((mode->flags & DRM_MODE_FLAG_INTERLACE) !=
3271a646342SBen Skeggs 		    (output_mode->flags & DRM_MODE_FLAG_INTERLACE))
3281a646342SBen Skeggs 			return MODE_NO_INTERLACE;
3291a646342SBen Skeggs 
3301a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
3311a646342SBen Skeggs 			return MODE_NO_DBLESCAN;
3321a646342SBen Skeggs 
3331a646342SBen Skeggs 	} else {
3341a646342SBen Skeggs 		const int vsync_tolerance = 600;
3351a646342SBen Skeggs 
3361a646342SBen Skeggs 		if (mode->clock > 70000)
3371a646342SBen Skeggs 			return MODE_CLOCK_HIGH;
3381a646342SBen Skeggs 
3391a646342SBen Skeggs 		if (abs(drm_mode_vrefresh(mode) * 1000 -
3401a646342SBen Skeggs 			tv_norm->tv_enc_mode.vrefresh) > vsync_tolerance)
3411a646342SBen Skeggs 			return MODE_VSYNC;
3421a646342SBen Skeggs 
3431a646342SBen Skeggs 		/* The encoder takes care of the actual interlacing */
3441a646342SBen Skeggs 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
3451a646342SBen Skeggs 			return MODE_NO_INTERLACE;
3461a646342SBen Skeggs 	}
3471a646342SBen Skeggs 
3481a646342SBen Skeggs 	return MODE_OK;
3491a646342SBen Skeggs }
3501a646342SBen Skeggs 
nv17_tv_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)3511a646342SBen Skeggs static bool nv17_tv_mode_fixup(struct drm_encoder *encoder,
3521a646342SBen Skeggs 			       const struct drm_display_mode *mode,
3531a646342SBen Skeggs 			       struct drm_display_mode *adjusted_mode)
3541a646342SBen Skeggs {
3551a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3561a646342SBen Skeggs 
3571a646342SBen Skeggs 	if (nv04_dac_in_use(encoder))
3581a646342SBen Skeggs 		return false;
3591a646342SBen Skeggs 
3601a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
3611a646342SBen Skeggs 		adjusted_mode->clock = tv_norm->ctv_enc_mode.mode.clock;
3621a646342SBen Skeggs 	else
3631a646342SBen Skeggs 		adjusted_mode->clock = 90000;
3641a646342SBen Skeggs 
3651a646342SBen Skeggs 	return true;
3661a646342SBen Skeggs }
3671a646342SBen Skeggs 
nv17_tv_dpms(struct drm_encoder * encoder,int mode)3681a646342SBen Skeggs static void  nv17_tv_dpms(struct drm_encoder *encoder, int mode)
3691a646342SBen Skeggs {
3701a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
3711a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
3721167c6bcSBen Skeggs 	struct nvkm_gpio *gpio = nvxx_gpio(&drm->client.device);
3731a646342SBen Skeggs 	struct nv17_tv_state *regs = &to_tv_enc(encoder)->state;
3741a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
3751a646342SBen Skeggs 
3761a646342SBen Skeggs 	if (nouveau_encoder(encoder)->last_dpms == mode)
3771a646342SBen Skeggs 		return;
3781a646342SBen Skeggs 	nouveau_encoder(encoder)->last_dpms = mode;
3791a646342SBen Skeggs 
3801a646342SBen Skeggs 	NV_INFO(drm, "Setting dpms mode %d on TV encoder (output %d)\n",
3811a646342SBen Skeggs 		 mode, nouveau_encoder(encoder)->dcb->index);
3821a646342SBen Skeggs 
3831a646342SBen Skeggs 	regs->ptv_200 &= ~1;
3841a646342SBen Skeggs 
3851a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
3861a646342SBen Skeggs 		nv04_dfp_update_fp_control(encoder, mode);
3871a646342SBen Skeggs 
3881a646342SBen Skeggs 	} else {
3891a646342SBen Skeggs 		nv04_dfp_update_fp_control(encoder, DRM_MODE_DPMS_OFF);
3901a646342SBen Skeggs 
3911a646342SBen Skeggs 		if (mode == DRM_MODE_DPMS_ON)
3921a646342SBen Skeggs 			regs->ptv_200 |= 1;
3931a646342SBen Skeggs 	}
3941a646342SBen Skeggs 
3951a646342SBen Skeggs 	nv_load_ptv(dev, regs, 200);
3961a646342SBen Skeggs 
3972ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, mode == DRM_MODE_DPMS_ON);
3982ea7249fSBen Skeggs 	nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, mode == DRM_MODE_DPMS_ON);
3991a646342SBen Skeggs 
4001a646342SBen Skeggs 	nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON);
4011a646342SBen Skeggs }
4021a646342SBen Skeggs 
nv17_tv_prepare(struct drm_encoder * encoder)4031a646342SBen Skeggs static void nv17_tv_prepare(struct drm_encoder *encoder)
4041a646342SBen Skeggs {
4051a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
4061a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
407d58ded76SJani Nikula 	const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
4081a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
4091a646342SBen Skeggs 	int head = nouveau_crtc(encoder->crtc)->index;
4101a646342SBen Skeggs 	uint8_t *cr_lcd = &nv04_display(dev)->mode_reg.crtc_reg[head].CRTC[
4111a646342SBen Skeggs 							NV_CIO_CRE_LCD__INDEX];
4121a646342SBen Skeggs 	uint32_t dacclk_off = NV_PRAMDAC_DACCLK +
4131a646342SBen Skeggs 					nv04_dac_output_offset(encoder);
4141a646342SBen Skeggs 	uint32_t dacclk;
4151a646342SBen Skeggs 
4161a646342SBen Skeggs 	helper->dpms(encoder, DRM_MODE_DPMS_OFF);
4171a646342SBen Skeggs 
4181a646342SBen Skeggs 	nv04_dfp_disable(dev, head);
4191a646342SBen Skeggs 
4201a646342SBen Skeggs 	/* Unbind any FP encoders from this head if we need the FP
4211a646342SBen Skeggs 	 * stuff enabled. */
4221a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
4231a646342SBen Skeggs 		struct drm_encoder *enc;
4241a646342SBen Skeggs 
4251a646342SBen Skeggs 		list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
4261a646342SBen Skeggs 			struct dcb_output *dcb = nouveau_encoder(enc)->dcb;
4271a646342SBen Skeggs 
4281a646342SBen Skeggs 			if ((dcb->type == DCB_OUTPUT_TMDS ||
4291a646342SBen Skeggs 			     dcb->type == DCB_OUTPUT_LVDS) &&
4301a646342SBen Skeggs 			     !enc->crtc &&
4311a646342SBen Skeggs 			     nv04_dfp_get_bound_head(dev, dcb) == head) {
4321a646342SBen Skeggs 				nv04_dfp_bind_head(dev, dcb, head ^ 1,
4331a646342SBen Skeggs 						drm->vbios.fp.dual_link);
4341a646342SBen Skeggs 			}
4351a646342SBen Skeggs 		}
4361a646342SBen Skeggs 
4371a646342SBen Skeggs 	}
4381a646342SBen Skeggs 
4391a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE)
4401a646342SBen Skeggs 		*cr_lcd |= 0x1 | (head ? 0x0 : 0x8);
4411a646342SBen Skeggs 
4421a646342SBen Skeggs 	/* Set the DACCLK register */
4431a646342SBen Skeggs 	dacclk = (NVReadRAMDAC(dev, 0, dacclk_off) & ~0x30) | 0x1;
4441a646342SBen Skeggs 
4451167c6bcSBen Skeggs 	if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CURIE)
4461a646342SBen Skeggs 		dacclk |= 0x1a << 16;
4471a646342SBen Skeggs 
4481a646342SBen Skeggs 	if (tv_norm->kind == CTV_ENC_MODE) {
4491a646342SBen Skeggs 		dacclk |=  0x20;
4501a646342SBen Skeggs 
4511a646342SBen Skeggs 		if (head)
4521a646342SBen Skeggs 			dacclk |= 0x100;
4531a646342SBen Skeggs 		else
4541a646342SBen Skeggs 			dacclk &= ~0x100;
4551a646342SBen Skeggs 
4561a646342SBen Skeggs 	} else {
4571a646342SBen Skeggs 		dacclk |= 0x10;
4581a646342SBen Skeggs 
4591a646342SBen Skeggs 	}
4601a646342SBen Skeggs 
4611a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, dacclk_off, dacclk);
4621a646342SBen Skeggs }
4631a646342SBen Skeggs 
nv17_tv_mode_set(struct drm_encoder * encoder,struct drm_display_mode * drm_mode,struct drm_display_mode * adjusted_mode)4641a646342SBen Skeggs static void nv17_tv_mode_set(struct drm_encoder *encoder,
4651a646342SBen Skeggs 			     struct drm_display_mode *drm_mode,
4661a646342SBen Skeggs 			     struct drm_display_mode *adjusted_mode)
4671a646342SBen Skeggs {
4681a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
4691a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
4701a646342SBen Skeggs 	int head = nouveau_crtc(encoder->crtc)->index;
4711a646342SBen Skeggs 	struct nv04_crtc_reg *regs = &nv04_display(dev)->mode_reg.crtc_reg[head];
4721a646342SBen Skeggs 	struct nv17_tv_state *tv_regs = &to_tv_enc(encoder)->state;
4731a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
4741a646342SBen Skeggs 	int i;
4751a646342SBen Skeggs 
4761a646342SBen Skeggs 	regs->CRTC[NV_CIO_CRE_53] = 0x40; /* FP_HTIMING */
4771a646342SBen Skeggs 	regs->CRTC[NV_CIO_CRE_54] = 0; /* FP_VTIMING */
4781a646342SBen Skeggs 	regs->ramdac_630 = 0x2; /* turn off green mode (tv test pattern?) */
4791a646342SBen Skeggs 	regs->tv_setup = 1;
4801a646342SBen Skeggs 	regs->ramdac_8c0 = 0x0;
4811a646342SBen Skeggs 
4821a646342SBen Skeggs 	if (tv_norm->kind == TV_ENC_MODE) {
4831a646342SBen Skeggs 		tv_regs->ptv_200 = 0x13111100;
4841a646342SBen Skeggs 		if (head)
4851a646342SBen Skeggs 			tv_regs->ptv_200 |= 0x10;
4861a646342SBen Skeggs 
4871a646342SBen Skeggs 		tv_regs->ptv_20c = 0x808010;
4881a646342SBen Skeggs 		tv_regs->ptv_304 = 0x2d00000;
4891a646342SBen Skeggs 		tv_regs->ptv_600 = 0x0;
4901a646342SBen Skeggs 		tv_regs->ptv_60c = 0x0;
4911a646342SBen Skeggs 		tv_regs->ptv_610 = 0x1e00000;
4921a646342SBen Skeggs 
4931a646342SBen Skeggs 		if (tv_norm->tv_enc_mode.vdisplay == 576) {
4941a646342SBen Skeggs 			tv_regs->ptv_508 = 0x1200000;
4951a646342SBen Skeggs 			tv_regs->ptv_614 = 0x33;
4961a646342SBen Skeggs 
4971a646342SBen Skeggs 		} else if (tv_norm->tv_enc_mode.vdisplay == 480) {
4981a646342SBen Skeggs 			tv_regs->ptv_508 = 0xf00000;
4991a646342SBen Skeggs 			tv_regs->ptv_614 = 0x13;
5001a646342SBen Skeggs 		}
5011a646342SBen Skeggs 
5021167c6bcSBen Skeggs 		if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_RANKINE) {
5031a646342SBen Skeggs 			tv_regs->ptv_500 = 0xe8e0;
5041a646342SBen Skeggs 			tv_regs->ptv_504 = 0x1710;
5051a646342SBen Skeggs 			tv_regs->ptv_604 = 0x0;
5061a646342SBen Skeggs 			tv_regs->ptv_608 = 0x0;
5071a646342SBen Skeggs 		} else {
5081a646342SBen Skeggs 			if (tv_norm->tv_enc_mode.vdisplay == 576) {
5091a646342SBen Skeggs 				tv_regs->ptv_604 = 0x20;
5101a646342SBen Skeggs 				tv_regs->ptv_608 = 0x10;
5111a646342SBen Skeggs 				tv_regs->ptv_500 = 0x19710;
5121a646342SBen Skeggs 				tv_regs->ptv_504 = 0x68f0;
5131a646342SBen Skeggs 
5141a646342SBen Skeggs 			} else if (tv_norm->tv_enc_mode.vdisplay == 480) {
5151a646342SBen Skeggs 				tv_regs->ptv_604 = 0x10;
5161a646342SBen Skeggs 				tv_regs->ptv_608 = 0x20;
5171a646342SBen Skeggs 				tv_regs->ptv_500 = 0x4b90;
5181a646342SBen Skeggs 				tv_regs->ptv_504 = 0x1b480;
5191a646342SBen Skeggs 			}
5201a646342SBen Skeggs 		}
5211a646342SBen Skeggs 
5221a646342SBen Skeggs 		for (i = 0; i < 0x40; i++)
5231a646342SBen Skeggs 			tv_regs->tv_enc[i] = tv_norm->tv_enc_mode.tv_enc[i];
5241a646342SBen Skeggs 
5251a646342SBen Skeggs 	} else {
5261a646342SBen Skeggs 		struct drm_display_mode *output_mode =
5271a646342SBen Skeggs 						&tv_norm->ctv_enc_mode.mode;
5281a646342SBen Skeggs 
5291a646342SBen Skeggs 		/* The registers in PRAMDAC+0xc00 control some timings and CSC
5301a646342SBen Skeggs 		 * parameters for the CTV encoder (It's only used for "HD" TV
5311a646342SBen Skeggs 		 * modes, I don't think I have enough working to guess what
5321a646342SBen Skeggs 		 * they exactly mean...), it's probably connected at the
5331a646342SBen Skeggs 		 * output of the FP encoder, but it also needs the analog
5341a646342SBen Skeggs 		 * encoder in its OR enabled and routed to the head it's
5351a646342SBen Skeggs 		 * using. It's enabled with the DACCLK register, bits [5:4].
5361a646342SBen Skeggs 		 */
5371a646342SBen Skeggs 		for (i = 0; i < 38; i++)
5381a646342SBen Skeggs 			regs->ctv_regs[i] = tv_norm->ctv_enc_mode.ctv_regs[i];
5391a646342SBen Skeggs 
5401a646342SBen Skeggs 		regs->fp_horiz_regs[FP_DISPLAY_END] = output_mode->hdisplay - 1;
5411a646342SBen Skeggs 		regs->fp_horiz_regs[FP_TOTAL] = output_mode->htotal - 1;
5421a646342SBen Skeggs 		regs->fp_horiz_regs[FP_SYNC_START] =
5431a646342SBen Skeggs 						output_mode->hsync_start - 1;
5441a646342SBen Skeggs 		regs->fp_horiz_regs[FP_SYNC_END] = output_mode->hsync_end - 1;
5451a646342SBen Skeggs 		regs->fp_horiz_regs[FP_CRTC] = output_mode->hdisplay +
5461a646342SBen Skeggs 			max((output_mode->hdisplay-600)/40 - 1, 1);
5471a646342SBen Skeggs 
5481a646342SBen Skeggs 		regs->fp_vert_regs[FP_DISPLAY_END] = output_mode->vdisplay - 1;
5491a646342SBen Skeggs 		regs->fp_vert_regs[FP_TOTAL] = output_mode->vtotal - 1;
5501a646342SBen Skeggs 		regs->fp_vert_regs[FP_SYNC_START] =
5511a646342SBen Skeggs 						output_mode->vsync_start - 1;
5521a646342SBen Skeggs 		regs->fp_vert_regs[FP_SYNC_END] = output_mode->vsync_end - 1;
5531a646342SBen Skeggs 		regs->fp_vert_regs[FP_CRTC] = output_mode->vdisplay - 1;
5541a646342SBen Skeggs 
5551a646342SBen Skeggs 		regs->fp_control = NV_PRAMDAC_FP_TG_CONTROL_DISPEN_POS |
5561a646342SBen Skeggs 			NV_PRAMDAC_FP_TG_CONTROL_READ_PROG |
5571a646342SBen Skeggs 			NV_PRAMDAC_FP_TG_CONTROL_WIDTH_12;
5581a646342SBen Skeggs 
5591a646342SBen Skeggs 		if (output_mode->flags & DRM_MODE_FLAG_PVSYNC)
5601a646342SBen Skeggs 			regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_VSYNC_POS;
5611a646342SBen Skeggs 		if (output_mode->flags & DRM_MODE_FLAG_PHSYNC)
5621a646342SBen Skeggs 			regs->fp_control |= NV_PRAMDAC_FP_TG_CONTROL_HSYNC_POS;
5631a646342SBen Skeggs 
5641a646342SBen Skeggs 		regs->fp_debug_0 = NV_PRAMDAC_FP_DEBUG_0_YWEIGHT_ROUND |
5651a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XWEIGHT_ROUND |
5661a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_YINTERP_BILINEAR |
5671a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XINTERP_BILINEAR |
5681a646342SBen Skeggs 			NV_RAMDAC_FP_DEBUG_0_TMDS_ENABLED |
5691a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_YSCALE_ENABLE |
5701a646342SBen Skeggs 			NV_PRAMDAC_FP_DEBUG_0_XSCALE_ENABLE;
5711a646342SBen Skeggs 
5721a646342SBen Skeggs 		regs->fp_debug_2 = 0;
5731a646342SBen Skeggs 
5741a646342SBen Skeggs 		regs->fp_margin_color = 0x801080;
5751a646342SBen Skeggs 
5761a646342SBen Skeggs 	}
5771a646342SBen Skeggs }
5781a646342SBen Skeggs 
nv17_tv_commit(struct drm_encoder * encoder)5791a646342SBen Skeggs static void nv17_tv_commit(struct drm_encoder *encoder)
5801a646342SBen Skeggs {
5811a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
5821a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
5831a646342SBen Skeggs 	struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
5841a646342SBen Skeggs 	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
585d58ded76SJani Nikula 	const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
5861a646342SBen Skeggs 
5871a646342SBen Skeggs 	if (get_tv_norm(encoder)->kind == TV_ENC_MODE) {
5881a646342SBen Skeggs 		nv17_tv_update_rescaler(encoder);
5891a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
5901a646342SBen Skeggs 	} else {
5911a646342SBen Skeggs 		nv17_ctv_update_rescaler(encoder);
5921a646342SBen Skeggs 	}
5931a646342SBen Skeggs 
5941a646342SBen Skeggs 	nv17_tv_state_load(dev, &to_tv_enc(encoder)->state);
5951a646342SBen Skeggs 
5961a646342SBen Skeggs 	/* This could use refinement for flatpanels, but it should work */
5971167c6bcSBen Skeggs 	if (drm->client.device.info.chipset < 0x44)
5981a646342SBen Skeggs 		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
5991a646342SBen Skeggs 					nv04_dac_output_offset(encoder),
6001a646342SBen Skeggs 					0xf0000000);
6011a646342SBen Skeggs 	else
6021a646342SBen Skeggs 		NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL +
6031a646342SBen Skeggs 					nv04_dac_output_offset(encoder),
6041a646342SBen Skeggs 					0x00100000);
6051a646342SBen Skeggs 
6061a646342SBen Skeggs 	helper->dpms(encoder, DRM_MODE_DPMS_ON);
6071a646342SBen Skeggs 
6081a646342SBen Skeggs 	NV_INFO(drm, "Output %s is running on CRTC %d using output %c\n",
60909838c4eSLyude Paul 		nv04_encoder_get_connector(nv_encoder)->base.name,
6101a646342SBen Skeggs 		nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
6111a646342SBen Skeggs }
6121a646342SBen Skeggs 
nv17_tv_save(struct drm_encoder * encoder)6131a646342SBen Skeggs static void nv17_tv_save(struct drm_encoder *encoder)
6141a646342SBen Skeggs {
6151a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6161a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6171a646342SBen Skeggs 
6181a646342SBen Skeggs 	nouveau_encoder(encoder)->restore.output =
6191a646342SBen Skeggs 					NVReadRAMDAC(dev, 0,
6201a646342SBen Skeggs 					NV_PRAMDAC_DACCLK +
6211a646342SBen Skeggs 					nv04_dac_output_offset(encoder));
6221a646342SBen Skeggs 
6231a646342SBen Skeggs 	nv17_tv_state_save(dev, &tv_enc->saved_state);
6241a646342SBen Skeggs 
6251a646342SBen Skeggs 	tv_enc->state.ptv_200 = tv_enc->saved_state.ptv_200;
6261a646342SBen Skeggs }
6271a646342SBen Skeggs 
nv17_tv_restore(struct drm_encoder * encoder)6281a646342SBen Skeggs static void nv17_tv_restore(struct drm_encoder *encoder)
6291a646342SBen Skeggs {
6301a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6311a646342SBen Skeggs 
6321a646342SBen Skeggs 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK +
6331a646342SBen Skeggs 				nv04_dac_output_offset(encoder),
6341a646342SBen Skeggs 				nouveau_encoder(encoder)->restore.output);
6351a646342SBen Skeggs 
6361a646342SBen Skeggs 	nv17_tv_state_load(dev, &to_tv_enc(encoder)->saved_state);
6371a646342SBen Skeggs 
6381a646342SBen Skeggs 	nouveau_encoder(encoder)->last_dpms = NV_DPMS_CLEARED;
6391a646342SBen Skeggs }
6401a646342SBen Skeggs 
nv17_tv_create_resources(struct drm_encoder * encoder,struct drm_connector * connector)6411a646342SBen Skeggs static int nv17_tv_create_resources(struct drm_encoder *encoder,
6421a646342SBen Skeggs 				    struct drm_connector *connector)
6431a646342SBen Skeggs {
6441a646342SBen Skeggs 	struct drm_device *dev = encoder->dev;
6451a646342SBen Skeggs 	struct nouveau_drm *drm = nouveau_drm(dev);
6461a646342SBen Skeggs 	struct drm_mode_config *conf = &dev->mode_config;
6471a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6481a646342SBen Skeggs 	struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
6491a646342SBen Skeggs 	int num_tv_norms = dcb->tvconf.has_component_output ? NUM_TV_NORMS :
6501a646342SBen Skeggs 							NUM_LD_TV_NORMS;
6511a646342SBen Skeggs 	int i;
6521a646342SBen Skeggs 
6531a646342SBen Skeggs 	if (nouveau_tv_norm) {
6542574c809SYueHaibing 		i = match_string(nv17_tv_norm_names, num_tv_norms,
6552574c809SYueHaibing 				 nouveau_tv_norm);
6562574c809SYueHaibing 		if (i < 0)
6571a646342SBen Skeggs 			NV_WARN(drm, "Invalid TV norm setting \"%s\"\n",
6581a646342SBen Skeggs 				nouveau_tv_norm);
6592574c809SYueHaibing 		else
6602574c809SYueHaibing 			tv_enc->tv_norm = i;
6611a646342SBen Skeggs 	}
6621a646342SBen Skeggs 
66380ed86d4SMaxime Ripard 	drm_mode_create_tv_properties_legacy(dev, num_tv_norms, nv17_tv_norm_names);
6641a646342SBen Skeggs 
6651a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6661a646342SBen Skeggs 					conf->tv_select_subconnector_property,
6671a646342SBen Skeggs 					tv_enc->select_subconnector);
6681a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6691a646342SBen Skeggs 					conf->tv_subconnector_property,
6701a646342SBen Skeggs 					tv_enc->subconnector);
6711a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6721fd4a5a3SMaxime Ripard 					conf->legacy_tv_mode_property,
6731a646342SBen Skeggs 					tv_enc->tv_norm);
6741a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6751a646342SBen Skeggs 					conf->tv_flicker_reduction_property,
6761a646342SBen Skeggs 					tv_enc->flicker);
6771a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6781a646342SBen Skeggs 					conf->tv_saturation_property,
6791a646342SBen Skeggs 					tv_enc->saturation);
6801a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6811a646342SBen Skeggs 					conf->tv_hue_property,
6821a646342SBen Skeggs 					tv_enc->hue);
6831a646342SBen Skeggs 	drm_object_attach_property(&connector->base,
6841a646342SBen Skeggs 					conf->tv_overscan_property,
6851a646342SBen Skeggs 					tv_enc->overscan);
6861a646342SBen Skeggs 
6871a646342SBen Skeggs 	return 0;
6881a646342SBen Skeggs }
6891a646342SBen Skeggs 
nv17_tv_set_property(struct drm_encoder * encoder,struct drm_connector * connector,struct drm_property * property,uint64_t val)6901a646342SBen Skeggs static int nv17_tv_set_property(struct drm_encoder *encoder,
6911a646342SBen Skeggs 				struct drm_connector *connector,
6921a646342SBen Skeggs 				struct drm_property *property,
6931a646342SBen Skeggs 				uint64_t val)
6941a646342SBen Skeggs {
6951a646342SBen Skeggs 	struct drm_mode_config *conf = &encoder->dev->mode_config;
6961a646342SBen Skeggs 	struct drm_crtc *crtc = encoder->crtc;
6971a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
6981a646342SBen Skeggs 	struct nv17_tv_norm_params *tv_norm = get_tv_norm(encoder);
6991a646342SBen Skeggs 	bool modes_changed = false;
7001a646342SBen Skeggs 
7011a646342SBen Skeggs 	if (property == conf->tv_overscan_property) {
7021a646342SBen Skeggs 		tv_enc->overscan = val;
7031a646342SBen Skeggs 		if (encoder->crtc) {
7041a646342SBen Skeggs 			if (tv_norm->kind == CTV_ENC_MODE)
7051a646342SBen Skeggs 				nv17_ctv_update_rescaler(encoder);
7061a646342SBen Skeggs 			else
7071a646342SBen Skeggs 				nv17_tv_update_rescaler(encoder);
7081a646342SBen Skeggs 		}
7091a646342SBen Skeggs 
7101a646342SBen Skeggs 	} else if (property == conf->tv_saturation_property) {
7111a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7121a646342SBen Skeggs 			return -EINVAL;
7131a646342SBen Skeggs 
7141a646342SBen Skeggs 		tv_enc->saturation = val;
7151a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7161a646342SBen Skeggs 
7171a646342SBen Skeggs 	} else if (property == conf->tv_hue_property) {
7181a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7191a646342SBen Skeggs 			return -EINVAL;
7201a646342SBen Skeggs 
7211a646342SBen Skeggs 		tv_enc->hue = val;
7221a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7231a646342SBen Skeggs 
7241a646342SBen Skeggs 	} else if (property == conf->tv_flicker_reduction_property) {
7251a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7261a646342SBen Skeggs 			return -EINVAL;
7271a646342SBen Skeggs 
7281a646342SBen Skeggs 		tv_enc->flicker = val;
7291a646342SBen Skeggs 		if (encoder->crtc)
7301a646342SBen Skeggs 			nv17_tv_update_rescaler(encoder);
7311a646342SBen Skeggs 
7321fd4a5a3SMaxime Ripard 	} else if (property == conf->legacy_tv_mode_property) {
7331a646342SBen Skeggs 		if (connector->dpms != DRM_MODE_DPMS_OFF)
7341a646342SBen Skeggs 			return -EINVAL;
7351a646342SBen Skeggs 
7361a646342SBen Skeggs 		tv_enc->tv_norm = val;
7371a646342SBen Skeggs 
7381a646342SBen Skeggs 		modes_changed = true;
7391a646342SBen Skeggs 
7401a646342SBen Skeggs 	} else if (property == conf->tv_select_subconnector_property) {
7411a646342SBen Skeggs 		if (tv_norm->kind != TV_ENC_MODE)
7421a646342SBen Skeggs 			return -EINVAL;
7431a646342SBen Skeggs 
7441a646342SBen Skeggs 		tv_enc->select_subconnector = val;
7451a646342SBen Skeggs 		nv17_tv_update_properties(encoder);
7461a646342SBen Skeggs 
7471a646342SBen Skeggs 	} else {
7481a646342SBen Skeggs 		return -EINVAL;
7491a646342SBen Skeggs 	}
7501a646342SBen Skeggs 
7511a646342SBen Skeggs 	if (modes_changed) {
7521a646342SBen Skeggs 		drm_helper_probe_single_connector_modes(connector, 0, 0);
7531a646342SBen Skeggs 
7541a646342SBen Skeggs 		/* Disable the crtc to ensure a full modeset is
7551a646342SBen Skeggs 		 * performed whenever it's turned on again. */
7568f76f5a0SLukas Wunner 		if (crtc)
757934c5b32SDaniel Vetter 			drm_crtc_helper_set_mode(crtc, &crtc->mode,
758934c5b32SDaniel Vetter 						 crtc->x, crtc->y,
759934c5b32SDaniel Vetter 						 crtc->primary->fb);
7601a646342SBen Skeggs 	}
7611a646342SBen Skeggs 
7621a646342SBen Skeggs 	return 0;
7631a646342SBen Skeggs }
7641a646342SBen Skeggs 
nv17_tv_destroy(struct drm_encoder * encoder)7651a646342SBen Skeggs static void nv17_tv_destroy(struct drm_encoder *encoder)
7661a646342SBen Skeggs {
7671a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = to_tv_enc(encoder);
7681a646342SBen Skeggs 
7691a646342SBen Skeggs 	drm_encoder_cleanup(encoder);
7701a646342SBen Skeggs 	kfree(tv_enc);
7711a646342SBen Skeggs }
7721a646342SBen Skeggs 
773ebb79a32SVille Syrjälä static const struct drm_encoder_helper_funcs nv17_tv_helper_funcs = {
7741a646342SBen Skeggs 	.dpms = nv17_tv_dpms,
7751a646342SBen Skeggs 	.mode_fixup = nv17_tv_mode_fixup,
7761a646342SBen Skeggs 	.prepare = nv17_tv_prepare,
7771a646342SBen Skeggs 	.commit = nv17_tv_commit,
7781a646342SBen Skeggs 	.mode_set = nv17_tv_mode_set,
7791a646342SBen Skeggs 	.detect = nv17_tv_detect,
7801a646342SBen Skeggs };
7811a646342SBen Skeggs 
782ebb79a32SVille Syrjälä static const struct drm_encoder_slave_funcs nv17_tv_slave_funcs = {
7831a646342SBen Skeggs 	.get_modes = nv17_tv_get_modes,
7841a646342SBen Skeggs 	.mode_valid = nv17_tv_mode_valid,
7851a646342SBen Skeggs 	.create_resources = nv17_tv_create_resources,
7861a646342SBen Skeggs 	.set_property = nv17_tv_set_property,
7871a646342SBen Skeggs };
7881a646342SBen Skeggs 
789ebb79a32SVille Syrjälä static const struct drm_encoder_funcs nv17_tv_funcs = {
7901a646342SBen Skeggs 	.destroy = nv17_tv_destroy,
7911a646342SBen Skeggs };
7921a646342SBen Skeggs 
7931a646342SBen Skeggs int
nv17_tv_create(struct drm_connector * connector,struct dcb_output * entry)7941a646342SBen Skeggs nv17_tv_create(struct drm_connector *connector, struct dcb_output *entry)
7951a646342SBen Skeggs {
7961a646342SBen Skeggs 	struct drm_device *dev = connector->dev;
7971a646342SBen Skeggs 	struct drm_encoder *encoder;
7981a646342SBen Skeggs 	struct nv17_tv_encoder *tv_enc = NULL;
7991a646342SBen Skeggs 
8001a646342SBen Skeggs 	tv_enc = kzalloc(sizeof(*tv_enc), GFP_KERNEL);
8011a646342SBen Skeggs 	if (!tv_enc)
8021a646342SBen Skeggs 		return -ENOMEM;
8031a646342SBen Skeggs 
8041a646342SBen Skeggs 	tv_enc->overscan = 50;
8051a646342SBen Skeggs 	tv_enc->flicker = 50;
8061a646342SBen Skeggs 	tv_enc->saturation = 50;
8071a646342SBen Skeggs 	tv_enc->hue = 0;
8081a646342SBen Skeggs 	tv_enc->tv_norm = TV_NORM_PAL;
8091a646342SBen Skeggs 	tv_enc->subconnector = DRM_MODE_SUBCONNECTOR_Unknown;
8101a646342SBen Skeggs 	tv_enc->select_subconnector = DRM_MODE_SUBCONNECTOR_Automatic;
8111a646342SBen Skeggs 	tv_enc->pin_mask = 0;
8121a646342SBen Skeggs 
8131a646342SBen Skeggs 	encoder = to_drm_encoder(&tv_enc->base);
8141a646342SBen Skeggs 
8151a646342SBen Skeggs 	tv_enc->base.dcb = entry;
8161a646342SBen Skeggs 	tv_enc->base.or = ffs(entry->or) - 1;
8171a646342SBen Skeggs 
81813a3d91fSVille Syrjälä 	drm_encoder_init(dev, encoder, &nv17_tv_funcs, DRM_MODE_ENCODER_TVDAC,
81913a3d91fSVille Syrjälä 			 NULL);
8201a646342SBen Skeggs 	drm_encoder_helper_add(encoder, &nv17_tv_helper_funcs);
8211a646342SBen Skeggs 	to_encoder_slave(encoder)->slave_funcs = &nv17_tv_slave_funcs;
8221a646342SBen Skeggs 
823129b7820SDaniel Vetter 	tv_enc->base.enc_save = nv17_tv_save;
824129b7820SDaniel Vetter 	tv_enc->base.enc_restore = nv17_tv_restore;
825129b7820SDaniel Vetter 
8261a646342SBen Skeggs 	encoder->possible_crtcs = entry->heads;
8271a646342SBen Skeggs 	encoder->possible_clones = 0;
8281a646342SBen Skeggs 
8291a646342SBen Skeggs 	nv17_tv_create_resources(encoder, connector);
830cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(connector, encoder);
8311a646342SBen Skeggs 	return 0;
8321a646342SBen Skeggs }
833