18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: MIT
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci#include <drm/drm_crtc_helper.h>
48c2ecf20Sopenharmony_ci#include <drm/drm_device.h>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "radeon.h"
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * Integrated TV out support based on the GATOS code by
108c2ecf20Sopenharmony_ci * Federico Ulivi <fulivi@lycos.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Limits of h/v positions (hPos & vPos)
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci#define MAX_H_POSITION 5 /* Range: [-5..5], negative is on the left, 0 is default, positive is on the right */
188c2ecf20Sopenharmony_ci#define MAX_V_POSITION 5 /* Range: [-5..5], negative is up, 0 is default, positive is down */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Unit for hPos (in TV clock periods)
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define H_POS_UNIT 10
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * Indexes in h. code timing table for horizontal line position adjustment
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define H_TABLE_POS1 6
298c2ecf20Sopenharmony_ci#define H_TABLE_POS2 8
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * Limits of hor. size (hSize)
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci#define MAX_H_SIZE 5 /* Range: [-5..5], negative is smaller, positive is larger */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* tv standard constants */
378c2ecf20Sopenharmony_ci#define NTSC_TV_CLOCK_T 233
388c2ecf20Sopenharmony_ci#define NTSC_TV_VFTOTAL 1
398c2ecf20Sopenharmony_ci#define NTSC_TV_LINES_PER_FRAME 525
408c2ecf20Sopenharmony_ci#define NTSC_TV_ZERO_H_SIZE 479166
418c2ecf20Sopenharmony_ci#define NTSC_TV_H_SIZE_UNIT 9478
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define PAL_TV_CLOCK_T 188
448c2ecf20Sopenharmony_ci#define PAL_TV_VFTOTAL 3
458c2ecf20Sopenharmony_ci#define PAL_TV_LINES_PER_FRAME 625
468c2ecf20Sopenharmony_ci#define PAL_TV_ZERO_H_SIZE 473200
478c2ecf20Sopenharmony_ci#define PAL_TV_H_SIZE_UNIT 9360
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* tv pll setting for 27 mhz ref clk */
508c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_M_27 22
518c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_N_27 175
528c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_P_27 5
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define PAL_TV_PLL_M_27 113
558c2ecf20Sopenharmony_ci#define PAL_TV_PLL_N_27 668
568c2ecf20Sopenharmony_ci#define PAL_TV_PLL_P_27 3
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* tv pll setting for 14 mhz ref clk */
598c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_M_14 33
608c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_N_14 693
618c2ecf20Sopenharmony_ci#define NTSC_TV_PLL_P_14 7
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define PAL_TV_PLL_M_14 19
648c2ecf20Sopenharmony_ci#define PAL_TV_PLL_N_14 353
658c2ecf20Sopenharmony_ci#define PAL_TV_PLL_P_14 5
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define VERT_LEAD_IN_LINES 2
688c2ecf20Sopenharmony_ci#define FRAC_BITS 0xe
698c2ecf20Sopenharmony_ci#define FRAC_MASK 0x3fff
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistruct radeon_tv_mode_constants {
728c2ecf20Sopenharmony_ci	uint16_t hor_resolution;
738c2ecf20Sopenharmony_ci	uint16_t ver_resolution;
748c2ecf20Sopenharmony_ci	enum radeon_tv_std standard;
758c2ecf20Sopenharmony_ci	uint16_t hor_total;
768c2ecf20Sopenharmony_ci	uint16_t ver_total;
778c2ecf20Sopenharmony_ci	uint16_t hor_start;
788c2ecf20Sopenharmony_ci	uint16_t hor_syncstart;
798c2ecf20Sopenharmony_ci	uint16_t ver_syncstart;
808c2ecf20Sopenharmony_ci	unsigned def_restart;
818c2ecf20Sopenharmony_ci	uint16_t crtcPLL_N;
828c2ecf20Sopenharmony_ci	uint8_t  crtcPLL_M;
838c2ecf20Sopenharmony_ci	uint8_t  crtcPLL_post_div;
848c2ecf20Sopenharmony_ci	unsigned pix_to_tv;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic const uint16_t hor_timing_NTSC[MAX_H_CODE_TIMING_LEN] = {
888c2ecf20Sopenharmony_ci	0x0007,
898c2ecf20Sopenharmony_ci	0x003f,
908c2ecf20Sopenharmony_ci	0x0263,
918c2ecf20Sopenharmony_ci	0x0a24,
928c2ecf20Sopenharmony_ci	0x2a6b,
938c2ecf20Sopenharmony_ci	0x0a36,
948c2ecf20Sopenharmony_ci	0x126d, /* H_TABLE_POS1 */
958c2ecf20Sopenharmony_ci	0x1bfe,
968c2ecf20Sopenharmony_ci	0x1a8f, /* H_TABLE_POS2 */
978c2ecf20Sopenharmony_ci	0x1ec7,
988c2ecf20Sopenharmony_ci	0x3863,
998c2ecf20Sopenharmony_ci	0x1bfe,
1008c2ecf20Sopenharmony_ci	0x1bfe,
1018c2ecf20Sopenharmony_ci	0x1a2a,
1028c2ecf20Sopenharmony_ci	0x1e95,
1038c2ecf20Sopenharmony_ci	0x0e31,
1048c2ecf20Sopenharmony_ci	0x201b,
1058c2ecf20Sopenharmony_ci	0
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic const uint16_t vert_timing_NTSC[MAX_V_CODE_TIMING_LEN] = {
1098c2ecf20Sopenharmony_ci	0x2001,
1108c2ecf20Sopenharmony_ci	0x200d,
1118c2ecf20Sopenharmony_ci	0x1006,
1128c2ecf20Sopenharmony_ci	0x0c06,
1138c2ecf20Sopenharmony_ci	0x1006,
1148c2ecf20Sopenharmony_ci	0x1818,
1158c2ecf20Sopenharmony_ci	0x21e3,
1168c2ecf20Sopenharmony_ci	0x1006,
1178c2ecf20Sopenharmony_ci	0x0c06,
1188c2ecf20Sopenharmony_ci	0x1006,
1198c2ecf20Sopenharmony_ci	0x1817,
1208c2ecf20Sopenharmony_ci	0x21d4,
1218c2ecf20Sopenharmony_ci	0x0002,
1228c2ecf20Sopenharmony_ci	0
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic const uint16_t hor_timing_PAL[MAX_H_CODE_TIMING_LEN] = {
1268c2ecf20Sopenharmony_ci	0x0007,
1278c2ecf20Sopenharmony_ci	0x0058,
1288c2ecf20Sopenharmony_ci	0x027c,
1298c2ecf20Sopenharmony_ci	0x0a31,
1308c2ecf20Sopenharmony_ci	0x2a77,
1318c2ecf20Sopenharmony_ci	0x0a95,
1328c2ecf20Sopenharmony_ci	0x124f, /* H_TABLE_POS1 */
1338c2ecf20Sopenharmony_ci	0x1bfe,
1348c2ecf20Sopenharmony_ci	0x1b22, /* H_TABLE_POS2 */
1358c2ecf20Sopenharmony_ci	0x1ef9,
1368c2ecf20Sopenharmony_ci	0x387c,
1378c2ecf20Sopenharmony_ci	0x1bfe,
1388c2ecf20Sopenharmony_ci	0x1bfe,
1398c2ecf20Sopenharmony_ci	0x1b31,
1408c2ecf20Sopenharmony_ci	0x1eb5,
1418c2ecf20Sopenharmony_ci	0x0e43,
1428c2ecf20Sopenharmony_ci	0x201b,
1438c2ecf20Sopenharmony_ci	0
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic const uint16_t vert_timing_PAL[MAX_V_CODE_TIMING_LEN] = {
1478c2ecf20Sopenharmony_ci	0x2001,
1488c2ecf20Sopenharmony_ci	0x200c,
1498c2ecf20Sopenharmony_ci	0x1005,
1508c2ecf20Sopenharmony_ci	0x0c05,
1518c2ecf20Sopenharmony_ci	0x1005,
1528c2ecf20Sopenharmony_ci	0x1401,
1538c2ecf20Sopenharmony_ci	0x1821,
1548c2ecf20Sopenharmony_ci	0x2240,
1558c2ecf20Sopenharmony_ci	0x1005,
1568c2ecf20Sopenharmony_ci	0x0c05,
1578c2ecf20Sopenharmony_ci	0x1005,
1588c2ecf20Sopenharmony_ci	0x1401,
1598c2ecf20Sopenharmony_ci	0x1822,
1608c2ecf20Sopenharmony_ci	0x2230,
1618c2ecf20Sopenharmony_ci	0x0002,
1628c2ecf20Sopenharmony_ci	0
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/**********************************************************************
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci * availableModes
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * Table of all allowed modes for tv output
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci **********************************************************************/
1728c2ecf20Sopenharmony_cistatic const struct radeon_tv_mode_constants available_tv_modes[] = {
1738c2ecf20Sopenharmony_ci	{   /* NTSC timing for 27 Mhz ref clk */
1748c2ecf20Sopenharmony_ci		800,                /* horResolution */
1758c2ecf20Sopenharmony_ci		600,                /* verResolution */
1768c2ecf20Sopenharmony_ci		TV_STD_NTSC,        /* standard */
1778c2ecf20Sopenharmony_ci		990,                /* horTotal */
1788c2ecf20Sopenharmony_ci		740,                /* verTotal */
1798c2ecf20Sopenharmony_ci		813,                /* horStart */
1808c2ecf20Sopenharmony_ci		824,                /* horSyncStart */
1818c2ecf20Sopenharmony_ci		632,                /* verSyncStart */
1828c2ecf20Sopenharmony_ci		625592,             /* defRestart */
1838c2ecf20Sopenharmony_ci		592,                /* crtcPLL_N */
1848c2ecf20Sopenharmony_ci		91,                 /* crtcPLL_M */
1858c2ecf20Sopenharmony_ci		4,                  /* crtcPLL_postDiv */
1868c2ecf20Sopenharmony_ci		1022,               /* pixToTV */
1878c2ecf20Sopenharmony_ci	},
1888c2ecf20Sopenharmony_ci	{   /* PAL timing for 27 Mhz ref clk */
1898c2ecf20Sopenharmony_ci		800,               /* horResolution */
1908c2ecf20Sopenharmony_ci		600,               /* verResolution */
1918c2ecf20Sopenharmony_ci		TV_STD_PAL,        /* standard */
1928c2ecf20Sopenharmony_ci		1144,              /* horTotal */
1938c2ecf20Sopenharmony_ci		706,               /* verTotal */
1948c2ecf20Sopenharmony_ci		812,               /* horStart */
1958c2ecf20Sopenharmony_ci		824,               /* horSyncStart */
1968c2ecf20Sopenharmony_ci		669,               /* verSyncStart */
1978c2ecf20Sopenharmony_ci		696700,            /* defRestart */
1988c2ecf20Sopenharmony_ci		1382,              /* crtcPLL_N */
1998c2ecf20Sopenharmony_ci		231,               /* crtcPLL_M */
2008c2ecf20Sopenharmony_ci		4,                 /* crtcPLL_postDiv */
2018c2ecf20Sopenharmony_ci		759,               /* pixToTV */
2028c2ecf20Sopenharmony_ci	},
2038c2ecf20Sopenharmony_ci	{   /* NTSC timing for 14 Mhz ref clk */
2048c2ecf20Sopenharmony_ci		800,                /* horResolution */
2058c2ecf20Sopenharmony_ci		600,                /* verResolution */
2068c2ecf20Sopenharmony_ci		TV_STD_NTSC,        /* standard */
2078c2ecf20Sopenharmony_ci		1018,               /* horTotal */
2088c2ecf20Sopenharmony_ci		727,                /* verTotal */
2098c2ecf20Sopenharmony_ci		813,                /* horStart */
2108c2ecf20Sopenharmony_ci		840,                /* horSyncStart */
2118c2ecf20Sopenharmony_ci		633,                /* verSyncStart */
2128c2ecf20Sopenharmony_ci		630627,             /* defRestart */
2138c2ecf20Sopenharmony_ci		347,                /* crtcPLL_N */
2148c2ecf20Sopenharmony_ci		14,                 /* crtcPLL_M */
2158c2ecf20Sopenharmony_ci		8,                  /* crtcPLL_postDiv */
2168c2ecf20Sopenharmony_ci		1022,               /* pixToTV */
2178c2ecf20Sopenharmony_ci	},
2188c2ecf20Sopenharmony_ci	{ /* PAL timing for 14 Mhz ref clk */
2198c2ecf20Sopenharmony_ci		800,                /* horResolution */
2208c2ecf20Sopenharmony_ci		600,                /* verResolution */
2218c2ecf20Sopenharmony_ci		TV_STD_PAL,         /* standard */
2228c2ecf20Sopenharmony_ci		1131,               /* horTotal */
2238c2ecf20Sopenharmony_ci		742,                /* verTotal */
2248c2ecf20Sopenharmony_ci		813,                /* horStart */
2258c2ecf20Sopenharmony_ci		840,                /* horSyncStart */
2268c2ecf20Sopenharmony_ci		633,                /* verSyncStart */
2278c2ecf20Sopenharmony_ci		708369,             /* defRestart */
2288c2ecf20Sopenharmony_ci		211,                /* crtcPLL_N */
2298c2ecf20Sopenharmony_ci		9,                  /* crtcPLL_M */
2308c2ecf20Sopenharmony_ci		8,                  /* crtcPLL_postDiv */
2318c2ecf20Sopenharmony_ci		759,                /* pixToTV */
2328c2ecf20Sopenharmony_ci	},
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define N_AVAILABLE_MODES ARRAY_SIZE(available_tv_modes)
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic const struct radeon_tv_mode_constants *radeon_legacy_tv_get_std_mode(struct radeon_encoder *radeon_encoder,
2388c2ecf20Sopenharmony_ci									    uint16_t *pll_ref_freq)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	struct drm_device *dev = radeon_encoder->base.dev;
2418c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
2428c2ecf20Sopenharmony_ci	struct radeon_crtc *radeon_crtc;
2438c2ecf20Sopenharmony_ci	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
2448c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
2458c2ecf20Sopenharmony_ci	struct radeon_pll *pll;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	radeon_crtc = to_radeon_crtc(radeon_encoder->base.crtc);
2488c2ecf20Sopenharmony_ci	if (radeon_crtc->crtc_id == 1)
2498c2ecf20Sopenharmony_ci		pll = &rdev->clock.p2pll;
2508c2ecf20Sopenharmony_ci	else
2518c2ecf20Sopenharmony_ci		pll = &rdev->clock.p1pll;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (pll_ref_freq)
2548c2ecf20Sopenharmony_ci		*pll_ref_freq = pll->reference_freq;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
2578c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
2588c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M) {
2598c2ecf20Sopenharmony_ci		if (pll->reference_freq == 2700)
2608c2ecf20Sopenharmony_ci			const_ptr = &available_tv_modes[0];
2618c2ecf20Sopenharmony_ci		else
2628c2ecf20Sopenharmony_ci			const_ptr = &available_tv_modes[2];
2638c2ecf20Sopenharmony_ci	} else {
2648c2ecf20Sopenharmony_ci		if (pll->reference_freq == 2700)
2658c2ecf20Sopenharmony_ci			const_ptr = &available_tv_modes[1];
2668c2ecf20Sopenharmony_ci		else
2678c2ecf20Sopenharmony_ci			const_ptr = &available_tv_modes[3];
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci	return const_ptr;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic long YCOEF_value[5] = { 2, 2, 0, 4, 0 };
2738c2ecf20Sopenharmony_cistatic long YCOEF_EN_value[5] = { 1, 1, 0, 1, 0 };
2748c2ecf20Sopenharmony_cistatic long SLOPE_value[5] = { 1, 2, 2, 4, 8 };
2758c2ecf20Sopenharmony_cistatic long SLOPE_limit[5] = { 6, 5, 4, 3, 2 };
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic void radeon_wait_pll_lock(struct drm_encoder *encoder, unsigned n_tests,
2788c2ecf20Sopenharmony_ci				 unsigned n_wait_loops, unsigned cnt_threshold)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	struct drm_device *dev = encoder->dev;
2818c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
2828c2ecf20Sopenharmony_ci	uint32_t save_pll_test;
2838c2ecf20Sopenharmony_ci	unsigned int i, j;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	WREG32(RADEON_TEST_DEBUG_MUX, (RREG32(RADEON_TEST_DEBUG_MUX) & 0xffff60ff) | 0x100);
2868c2ecf20Sopenharmony_ci	save_pll_test = RREG32_PLL(RADEON_PLL_TEST_CNTL);
2878c2ecf20Sopenharmony_ci	WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test & ~RADEON_PLL_MASK_READ_B);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	WREG8(RADEON_CLOCK_CNTL_INDEX, RADEON_PLL_TEST_CNTL);
2908c2ecf20Sopenharmony_ci	for (i = 0; i < n_tests; i++) {
2918c2ecf20Sopenharmony_ci		WREG8(RADEON_CLOCK_CNTL_DATA + 3, 0);
2928c2ecf20Sopenharmony_ci		for (j = 0; j < n_wait_loops; j++)
2938c2ecf20Sopenharmony_ci			if (RREG8(RADEON_CLOCK_CNTL_DATA + 3) >= cnt_threshold)
2948c2ecf20Sopenharmony_ci				break;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci	WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test);
2978c2ecf20Sopenharmony_ci	WREG32(RADEON_TEST_DEBUG_MUX, RREG32(RADEON_TEST_DEBUG_MUX) & 0xffffe0ff);
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic void radeon_legacy_tv_write_fifo(struct radeon_encoder *radeon_encoder,
3028c2ecf20Sopenharmony_ci					uint16_t addr, uint32_t value)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	struct drm_device *dev = radeon_encoder->base.dev;
3058c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
3068c2ecf20Sopenharmony_ci	uint32_t tmp;
3078c2ecf20Sopenharmony_ci	int i = 0;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_WRITE_DATA, value);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
3128c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_WT);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	do {
3158c2ecf20Sopenharmony_ci		tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
3168c2ecf20Sopenharmony_ci		if ((tmp & RADEON_HOST_FIFO_WT_ACK) == 0)
3178c2ecf20Sopenharmony_ci			break;
3188c2ecf20Sopenharmony_ci		i++;
3198c2ecf20Sopenharmony_ci	} while (i < 10000);
3208c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci#if 0 /* included for completeness */
3248c2ecf20Sopenharmony_cistatic uint32_t radeon_legacy_tv_read_fifo(struct radeon_encoder *radeon_encoder, uint16_t addr)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct drm_device *dev = radeon_encoder->base.dev;
3278c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
3288c2ecf20Sopenharmony_ci	uint32_t tmp;
3298c2ecf20Sopenharmony_ci	int i = 0;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
3328c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_RD);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	do {
3358c2ecf20Sopenharmony_ci		tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
3368c2ecf20Sopenharmony_ci		if ((tmp & RADEON_HOST_FIFO_RD_ACK) == 0)
3378c2ecf20Sopenharmony_ci			break;
3388c2ecf20Sopenharmony_ci		i++;
3398c2ecf20Sopenharmony_ci	} while (i < 10000);
3408c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
3418c2ecf20Sopenharmony_ci	return RREG32(RADEON_TV_HOST_READ_DATA);
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci#endif
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic uint16_t radeon_get_htiming_tables_addr(uint32_t tv_uv_adr)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	uint16_t h_table;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	switch ((tv_uv_adr & RADEON_HCODE_TABLE_SEL_MASK) >> RADEON_HCODE_TABLE_SEL_SHIFT) {
3508c2ecf20Sopenharmony_ci	case 0:
3518c2ecf20Sopenharmony_ci		h_table = RADEON_TV_MAX_FIFO_ADDR_INTERNAL;
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	case 1:
3548c2ecf20Sopenharmony_ci		h_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2;
3558c2ecf20Sopenharmony_ci		break;
3568c2ecf20Sopenharmony_ci	case 2:
3578c2ecf20Sopenharmony_ci		h_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2;
3588c2ecf20Sopenharmony_ci		break;
3598c2ecf20Sopenharmony_ci	default:
3608c2ecf20Sopenharmony_ci		h_table = 0;
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci	return h_table;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic uint16_t radeon_get_vtiming_tables_addr(uint32_t tv_uv_adr)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	uint16_t v_table;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	switch ((tv_uv_adr & RADEON_VCODE_TABLE_SEL_MASK) >> RADEON_VCODE_TABLE_SEL_SHIFT) {
3718c2ecf20Sopenharmony_ci	case 0:
3728c2ecf20Sopenharmony_ci		v_table = ((tv_uv_adr & RADEON_MAX_UV_ADR_MASK) >> RADEON_MAX_UV_ADR_SHIFT) * 2 + 1;
3738c2ecf20Sopenharmony_ci		break;
3748c2ecf20Sopenharmony_ci	case 1:
3758c2ecf20Sopenharmony_ci		v_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2 + 1;
3768c2ecf20Sopenharmony_ci		break;
3778c2ecf20Sopenharmony_ci	case 2:
3788c2ecf20Sopenharmony_ci		v_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2 + 1;
3798c2ecf20Sopenharmony_ci		break;
3808c2ecf20Sopenharmony_ci	default:
3818c2ecf20Sopenharmony_ci		v_table = 0;
3828c2ecf20Sopenharmony_ci		break;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci	return v_table;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic void radeon_restore_tv_timing_tables(struct radeon_encoder *radeon_encoder)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct drm_device *dev = radeon_encoder->base.dev;
3908c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
3918c2ecf20Sopenharmony_ci	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
3928c2ecf20Sopenharmony_ci	uint16_t h_table, v_table;
3938c2ecf20Sopenharmony_ci	uint32_t tmp;
3948c2ecf20Sopenharmony_ci	int i;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_UV_ADR, tv_dac->tv.tv_uv_adr);
3978c2ecf20Sopenharmony_ci	h_table = radeon_get_htiming_tables_addr(tv_dac->tv.tv_uv_adr);
3988c2ecf20Sopenharmony_ci	v_table = radeon_get_vtiming_tables_addr(tv_dac->tv.tv_uv_adr);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_H_CODE_TIMING_LEN; i += 2, h_table--) {
4018c2ecf20Sopenharmony_ci		tmp = ((uint32_t)tv_dac->tv.h_code_timing[i] << 14) | ((uint32_t)tv_dac->tv.h_code_timing[i+1]);
4028c2ecf20Sopenharmony_ci		radeon_legacy_tv_write_fifo(radeon_encoder, h_table, tmp);
4038c2ecf20Sopenharmony_ci		if (tv_dac->tv.h_code_timing[i] == 0 || tv_dac->tv.h_code_timing[i + 1] == 0)
4048c2ecf20Sopenharmony_ci			break;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_V_CODE_TIMING_LEN; i += 2, v_table++) {
4078c2ecf20Sopenharmony_ci		tmp = ((uint32_t)tv_dac->tv.v_code_timing[i+1] << 14) | ((uint32_t)tv_dac->tv.v_code_timing[i]);
4088c2ecf20Sopenharmony_ci		radeon_legacy_tv_write_fifo(radeon_encoder, v_table, tmp);
4098c2ecf20Sopenharmony_ci		if (tv_dac->tv.v_code_timing[i] == 0 || tv_dac->tv.v_code_timing[i + 1] == 0)
4108c2ecf20Sopenharmony_ci			break;
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic void radeon_legacy_write_tv_restarts(struct radeon_encoder *radeon_encoder)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct drm_device *dev = radeon_encoder->base.dev;
4178c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
4188c2ecf20Sopenharmony_ci	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
4198c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_FRESTART, tv_dac->tv.frestart);
4208c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HRESTART, tv_dac->tv.hrestart);
4218c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_VRESTART, tv_dac->tv.vrestart);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic bool radeon_legacy_tv_init_restarts(struct drm_encoder *encoder)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
4278c2ecf20Sopenharmony_ci	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
4288c2ecf20Sopenharmony_ci	int restart;
4298c2ecf20Sopenharmony_ci	unsigned int h_total, v_total, f_total;
4308c2ecf20Sopenharmony_ci	int v_offset, h_offset;
4318c2ecf20Sopenharmony_ci	u16 p1, p2, h_inc;
4328c2ecf20Sopenharmony_ci	bool h_changed;
4338c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
4368c2ecf20Sopenharmony_ci	if (!const_ptr)
4378c2ecf20Sopenharmony_ci		return false;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	h_total = const_ptr->hor_total;
4408c2ecf20Sopenharmony_ci	v_total = const_ptr->ver_total;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
4438c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
4448c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
4458c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60)
4468c2ecf20Sopenharmony_ci		f_total = NTSC_TV_VFTOTAL + 1;
4478c2ecf20Sopenharmony_ci	else
4488c2ecf20Sopenharmony_ci		f_total = PAL_TV_VFTOTAL + 1;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* adjust positions 1&2 in hor. cod timing table */
4518c2ecf20Sopenharmony_ci	h_offset = tv_dac->h_pos * H_POS_UNIT;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
4548c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
4558c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M) {
4568c2ecf20Sopenharmony_ci		h_offset -= 50;
4578c2ecf20Sopenharmony_ci		p1 = hor_timing_NTSC[H_TABLE_POS1];
4588c2ecf20Sopenharmony_ci		p2 = hor_timing_NTSC[H_TABLE_POS2];
4598c2ecf20Sopenharmony_ci	} else {
4608c2ecf20Sopenharmony_ci		p1 = hor_timing_PAL[H_TABLE_POS1];
4618c2ecf20Sopenharmony_ci		p2 = hor_timing_PAL[H_TABLE_POS2];
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	p1 = (u16)((int)p1 + h_offset);
4658c2ecf20Sopenharmony_ci	p2 = (u16)((int)p2 - h_offset);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	h_changed = (p1 != tv_dac->tv.h_code_timing[H_TABLE_POS1] ||
4688c2ecf20Sopenharmony_ci		     p2 != tv_dac->tv.h_code_timing[H_TABLE_POS2]);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	tv_dac->tv.h_code_timing[H_TABLE_POS1] = p1;
4718c2ecf20Sopenharmony_ci	tv_dac->tv.h_code_timing[H_TABLE_POS2] = p2;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/* Convert hOffset from n. of TV clock periods to n. of CRTC clock periods (CRTC pixels) */
4748c2ecf20Sopenharmony_ci	h_offset = (h_offset * (int)(const_ptr->pix_to_tv)) / 1000;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* adjust restart */
4778c2ecf20Sopenharmony_ci	restart = const_ptr->def_restart;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	/*
4808c2ecf20Sopenharmony_ci	 * convert v_pos TV lines to n. of CRTC pixels
4818c2ecf20Sopenharmony_ci	 */
4828c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
4838c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
4848c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
4858c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60)
4868c2ecf20Sopenharmony_ci		v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(NTSC_TV_LINES_PER_FRAME);
4878c2ecf20Sopenharmony_ci	else
4888c2ecf20Sopenharmony_ci		v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(PAL_TV_LINES_PER_FRAME);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	restart -= v_offset + h_offset;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	DRM_DEBUG_KMS("compute_restarts: def = %u h = %d v = %d, p1 = %04x, p2 = %04x, restart = %d\n",
4938c2ecf20Sopenharmony_ci		  const_ptr->def_restart, tv_dac->h_pos, tv_dac->v_pos, p1, p2, restart);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	tv_dac->tv.hrestart = restart % h_total;
4968c2ecf20Sopenharmony_ci	restart /= h_total;
4978c2ecf20Sopenharmony_ci	tv_dac->tv.vrestart = restart % v_total;
4988c2ecf20Sopenharmony_ci	restart /= v_total;
4998c2ecf20Sopenharmony_ci	tv_dac->tv.frestart = restart % f_total;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	DRM_DEBUG_KMS("compute_restart: F/H/V=%u,%u,%u\n",
5028c2ecf20Sopenharmony_ci		  (unsigned)tv_dac->tv.frestart,
5038c2ecf20Sopenharmony_ci		  (unsigned)tv_dac->tv.vrestart,
5048c2ecf20Sopenharmony_ci		  (unsigned)tv_dac->tv.hrestart);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* compute h_inc from hsize */
5078c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
5088c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
5098c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M)
5108c2ecf20Sopenharmony_ci		h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * NTSC_TV_CLOCK_T) /
5118c2ecf20Sopenharmony_ci			      (tv_dac->h_size * (int)(NTSC_TV_H_SIZE_UNIT) + (int)(NTSC_TV_ZERO_H_SIZE)));
5128c2ecf20Sopenharmony_ci	else
5138c2ecf20Sopenharmony_ci		h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * PAL_TV_CLOCK_T) /
5148c2ecf20Sopenharmony_ci			      (tv_dac->h_size * (int)(PAL_TV_H_SIZE_UNIT) + (int)(PAL_TV_ZERO_H_SIZE)));
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	tv_dac->tv.timing_cntl = (tv_dac->tv.timing_cntl & ~RADEON_H_INC_MASK) |
5178c2ecf20Sopenharmony_ci		((u32)h_inc << RADEON_H_INC_SHIFT);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	DRM_DEBUG_KMS("compute_restart: h_size = %d h_inc = %d\n", tv_dac->h_size, h_inc);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return h_changed;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_civoid radeon_legacy_tv_mode_set(struct drm_encoder *encoder,
5258c2ecf20Sopenharmony_ci			       struct drm_display_mode *mode,
5268c2ecf20Sopenharmony_ci			       struct drm_display_mode *adjusted_mode)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct drm_device *dev = encoder->dev;
5298c2ecf20Sopenharmony_ci	struct radeon_device *rdev = dev->dev_private;
5308c2ecf20Sopenharmony_ci	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
5318c2ecf20Sopenharmony_ci	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
5328c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
5338c2ecf20Sopenharmony_ci	struct radeon_crtc *radeon_crtc;
5348c2ecf20Sopenharmony_ci	int i;
5358c2ecf20Sopenharmony_ci	uint16_t pll_ref_freq;
5368c2ecf20Sopenharmony_ci	uint32_t vert_space, flicker_removal, tmp;
5378c2ecf20Sopenharmony_ci	uint32_t tv_master_cntl, tv_rgb_cntl, tv_dac_cntl;
5388c2ecf20Sopenharmony_ci	uint32_t tv_modulator_cntl1, tv_modulator_cntl2;
5398c2ecf20Sopenharmony_ci	uint32_t tv_vscaler_cntl1, tv_vscaler_cntl2;
5408c2ecf20Sopenharmony_ci	uint32_t tv_pll_cntl, tv_ftotal;
5418c2ecf20Sopenharmony_ci	uint32_t tv_y_fall_cntl, tv_y_rise_cntl, tv_y_saw_tooth_cntl;
5428c2ecf20Sopenharmony_ci	uint32_t m, n, p;
5438c2ecf20Sopenharmony_ci	const uint16_t *hor_timing;
5448c2ecf20Sopenharmony_ci	const uint16_t *vert_timing;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, &pll_ref_freq);
5478c2ecf20Sopenharmony_ci	if (!const_ptr)
5488c2ecf20Sopenharmony_ci		return;
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	radeon_crtc = to_radeon_crtc(encoder->crtc);
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	tv_master_cntl = (RADEON_VIN_ASYNC_RST |
5538c2ecf20Sopenharmony_ci			  RADEON_CRT_FIFO_CE_EN |
5548c2ecf20Sopenharmony_ci			  RADEON_TV_FIFO_CE_EN |
5558c2ecf20Sopenharmony_ci			  RADEON_TV_ON);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	if (!ASIC_IS_R300(rdev))
5588c2ecf20Sopenharmony_ci		tv_master_cntl |= RADEON_TVCLK_ALWAYS_ONb;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
5618c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J)
5628c2ecf20Sopenharmony_ci		tv_master_cntl |= RADEON_RESTART_PHASE_FIX;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	tv_modulator_cntl1 = (RADEON_SLEW_RATE_LIMIT |
5658c2ecf20Sopenharmony_ci			      RADEON_SYNC_TIP_LEVEL |
5668c2ecf20Sopenharmony_ci			      RADEON_YFLT_EN |
5678c2ecf20Sopenharmony_ci			      RADEON_UVFLT_EN |
5688c2ecf20Sopenharmony_ci			      (6 << RADEON_CY_FILT_BLEND_SHIFT));
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
5718c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J) {
5728c2ecf20Sopenharmony_ci		tv_modulator_cntl1 |= (0x46 << RADEON_SET_UP_LEVEL_SHIFT) |
5738c2ecf20Sopenharmony_ci			(0x3b << RADEON_BLANK_LEVEL_SHIFT);
5748c2ecf20Sopenharmony_ci		tv_modulator_cntl2 = (-111 & RADEON_TV_U_BURST_LEVEL_MASK) |
5758c2ecf20Sopenharmony_ci			((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
5768c2ecf20Sopenharmony_ci	} else if (tv_dac->tv_std == TV_STD_SCART_PAL) {
5778c2ecf20Sopenharmony_ci		tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN;
5788c2ecf20Sopenharmony_ci		tv_modulator_cntl2 = (0 & RADEON_TV_U_BURST_LEVEL_MASK) |
5798c2ecf20Sopenharmony_ci			((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
5808c2ecf20Sopenharmony_ci	} else {
5818c2ecf20Sopenharmony_ci		tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN |
5828c2ecf20Sopenharmony_ci			(0x3b << RADEON_SET_UP_LEVEL_SHIFT) |
5838c2ecf20Sopenharmony_ci			(0x3b << RADEON_BLANK_LEVEL_SHIFT);
5848c2ecf20Sopenharmony_ci		tv_modulator_cntl2 = (-78 & RADEON_TV_U_BURST_LEVEL_MASK) |
5858c2ecf20Sopenharmony_ci			((62 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	tv_rgb_cntl = (RADEON_RGB_DITHER_EN
5908c2ecf20Sopenharmony_ci		       | RADEON_TVOUT_SCALE_EN
5918c2ecf20Sopenharmony_ci		       | (0x0b << RADEON_UVRAM_READ_MARGIN_SHIFT)
5928c2ecf20Sopenharmony_ci		       | (0x07 << RADEON_FIFORAM_FFMACRO_READ_MARGIN_SHIFT)
5938c2ecf20Sopenharmony_ci		       | RADEON_RGB_ATTEN_SEL(0x3)
5948c2ecf20Sopenharmony_ci		       | RADEON_RGB_ATTEN_VAL(0xc));
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (radeon_crtc->crtc_id == 1)
5978c2ecf20Sopenharmony_ci		tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC2;
5988c2ecf20Sopenharmony_ci	else {
5998c2ecf20Sopenharmony_ci		if (radeon_crtc->rmx_type != RMX_OFF)
6008c2ecf20Sopenharmony_ci			tv_rgb_cntl |= RADEON_RGB_SRC_SEL_RMX;
6018c2ecf20Sopenharmony_ci		else
6028c2ecf20Sopenharmony_ci			tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC1;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
6068c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
6078c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
6088c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60)
6098c2ecf20Sopenharmony_ci		vert_space = const_ptr->ver_total * 2 * 10000 / NTSC_TV_LINES_PER_FRAME;
6108c2ecf20Sopenharmony_ci	else
6118c2ecf20Sopenharmony_ci		vert_space = const_ptr->ver_total * 2 * 10000 / PAL_TV_LINES_PER_FRAME;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	tmp = RREG32(RADEON_TV_VSCALER_CNTL1);
6148c2ecf20Sopenharmony_ci	tmp &= 0xe3ff0000;
6158c2ecf20Sopenharmony_ci	tmp |= (vert_space * (1 << FRAC_BITS) / 10000);
6168c2ecf20Sopenharmony_ci	tv_vscaler_cntl1 = tmp;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	if (pll_ref_freq == 2700)
6198c2ecf20Sopenharmony_ci		tv_vscaler_cntl1 |= RADEON_RESTART_FIELD;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (const_ptr->hor_resolution == 1024)
6228c2ecf20Sopenharmony_ci		tv_vscaler_cntl1 |= (4 << RADEON_Y_DEL_W_SIG_SHIFT);
6238c2ecf20Sopenharmony_ci	else
6248c2ecf20Sopenharmony_ci		tv_vscaler_cntl1 |= (2 << RADEON_Y_DEL_W_SIG_SHIFT);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/* scale up for int divide */
6278c2ecf20Sopenharmony_ci	tmp = const_ptr->ver_total * 2 * 1000;
6288c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
6298c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
6308c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
6318c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60) {
6328c2ecf20Sopenharmony_ci		tmp /= NTSC_TV_LINES_PER_FRAME;
6338c2ecf20Sopenharmony_ci	} else {
6348c2ecf20Sopenharmony_ci		tmp /= PAL_TV_LINES_PER_FRAME;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci	flicker_removal = (tmp + 500) / 1000;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (flicker_removal < 3)
6398c2ecf20Sopenharmony_ci		flicker_removal = 3;
6408c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(SLOPE_limit); ++i) {
6418c2ecf20Sopenharmony_ci		if (flicker_removal == SLOPE_limit[i])
6428c2ecf20Sopenharmony_ci			break;
6438c2ecf20Sopenharmony_ci	}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	tv_y_saw_tooth_cntl = (vert_space * SLOPE_value[i] * (1 << (FRAC_BITS - 1)) +
6468c2ecf20Sopenharmony_ci				5001) / 10000 / 8 | ((SLOPE_value[i] *
6478c2ecf20Sopenharmony_ci				(1 << (FRAC_BITS - 1)) / 8) << 16);
6488c2ecf20Sopenharmony_ci	tv_y_fall_cntl =
6498c2ecf20Sopenharmony_ci		(YCOEF_EN_value[i] << 17) | ((YCOEF_value[i] * (1 << 8) / 8) << 24) |
6508c2ecf20Sopenharmony_ci		RADEON_Y_FALL_PING_PONG | (272 * SLOPE_value[i] / 8) * (1 << (FRAC_BITS - 1)) /
6518c2ecf20Sopenharmony_ci		1024;
6528c2ecf20Sopenharmony_ci	tv_y_rise_cntl = RADEON_Y_RISE_PING_PONG|
6538c2ecf20Sopenharmony_ci		(flicker_removal * 1024 - 272) * SLOPE_value[i] / 8 * (1 << (FRAC_BITS - 1)) / 1024;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	tv_vscaler_cntl2 = RREG32(RADEON_TV_VSCALER_CNTL2) & 0x00fffff0;
6568c2ecf20Sopenharmony_ci	tv_vscaler_cntl2 |= (0x10 << 24) |
6578c2ecf20Sopenharmony_ci		RADEON_DITHER_MODE |
6588c2ecf20Sopenharmony_ci		RADEON_Y_OUTPUT_DITHER_EN |
6598c2ecf20Sopenharmony_ci		RADEON_UV_OUTPUT_DITHER_EN |
6608c2ecf20Sopenharmony_ci		RADEON_UV_TO_BUF_DITHER_EN;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	tmp = (tv_vscaler_cntl1 >> RADEON_UV_INC_SHIFT) & RADEON_UV_INC_MASK;
6638c2ecf20Sopenharmony_ci	tmp = ((16384 * 256 * 10) / tmp + 5) / 10;
6648c2ecf20Sopenharmony_ci	tmp = (tmp << RADEON_UV_OUTPUT_POST_SCALE_SHIFT) | 0x000b0000;
6658c2ecf20Sopenharmony_ci	tv_dac->tv.timing_cntl = tmp;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
6688c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
6698c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
6708c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60)
6718c2ecf20Sopenharmony_ci		tv_dac_cntl = tv_dac->ntsc_tvdac_adj;
6728c2ecf20Sopenharmony_ci	else
6738c2ecf20Sopenharmony_ci		tv_dac_cntl = tv_dac->pal_tvdac_adj;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	tv_dac_cntl |= RADEON_TV_DAC_NBLANK | RADEON_TV_DAC_NHOLD;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
6788c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J)
6798c2ecf20Sopenharmony_ci		tv_dac_cntl |= RADEON_TV_DAC_STD_NTSC;
6808c2ecf20Sopenharmony_ci	else
6818c2ecf20Sopenharmony_ci		tv_dac_cntl |= RADEON_TV_DAC_STD_PAL;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
6848c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J) {
6858c2ecf20Sopenharmony_ci		if (pll_ref_freq == 2700) {
6868c2ecf20Sopenharmony_ci			m = NTSC_TV_PLL_M_27;
6878c2ecf20Sopenharmony_ci			n = NTSC_TV_PLL_N_27;
6888c2ecf20Sopenharmony_ci			p = NTSC_TV_PLL_P_27;
6898c2ecf20Sopenharmony_ci		} else {
6908c2ecf20Sopenharmony_ci			m = NTSC_TV_PLL_M_14;
6918c2ecf20Sopenharmony_ci			n = NTSC_TV_PLL_N_14;
6928c2ecf20Sopenharmony_ci			p = NTSC_TV_PLL_P_14;
6938c2ecf20Sopenharmony_ci		}
6948c2ecf20Sopenharmony_ci	} else {
6958c2ecf20Sopenharmony_ci		if (pll_ref_freq == 2700) {
6968c2ecf20Sopenharmony_ci			m = PAL_TV_PLL_M_27;
6978c2ecf20Sopenharmony_ci			n = PAL_TV_PLL_N_27;
6988c2ecf20Sopenharmony_ci			p = PAL_TV_PLL_P_27;
6998c2ecf20Sopenharmony_ci		} else {
7008c2ecf20Sopenharmony_ci			m = PAL_TV_PLL_M_14;
7018c2ecf20Sopenharmony_ci			n = PAL_TV_PLL_N_14;
7028c2ecf20Sopenharmony_ci			p = PAL_TV_PLL_P_14;
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	tv_pll_cntl = (m & RADEON_TV_M0LO_MASK) |
7078c2ecf20Sopenharmony_ci		(((m >> 8) & RADEON_TV_M0HI_MASK) << RADEON_TV_M0HI_SHIFT) |
7088c2ecf20Sopenharmony_ci		((n & RADEON_TV_N0LO_MASK) << RADEON_TV_N0LO_SHIFT) |
7098c2ecf20Sopenharmony_ci		(((n >> 9) & RADEON_TV_N0HI_MASK) << RADEON_TV_N0HI_SHIFT) |
7108c2ecf20Sopenharmony_ci		((p & RADEON_TV_P_MASK) << RADEON_TV_P_SHIFT);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	tv_dac->tv.tv_uv_adr = 0xc8;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	if (tv_dac->tv_std == TV_STD_NTSC ||
7158c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_NTSC_J ||
7168c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_M ||
7178c2ecf20Sopenharmony_ci	    tv_dac->tv_std == TV_STD_PAL_60) {
7188c2ecf20Sopenharmony_ci		tv_ftotal = NTSC_TV_VFTOTAL;
7198c2ecf20Sopenharmony_ci		hor_timing = hor_timing_NTSC;
7208c2ecf20Sopenharmony_ci		vert_timing = vert_timing_NTSC;
7218c2ecf20Sopenharmony_ci	} else {
7228c2ecf20Sopenharmony_ci		hor_timing = hor_timing_PAL;
7238c2ecf20Sopenharmony_ci		vert_timing = vert_timing_PAL;
7248c2ecf20Sopenharmony_ci		tv_ftotal = PAL_TV_VFTOTAL;
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_H_CODE_TIMING_LEN; i++) {
7288c2ecf20Sopenharmony_ci		if ((tv_dac->tv.h_code_timing[i] = hor_timing[i]) == 0)
7298c2ecf20Sopenharmony_ci			break;
7308c2ecf20Sopenharmony_ci	}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_V_CODE_TIMING_LEN; i++) {
7338c2ecf20Sopenharmony_ci		if ((tv_dac->tv.v_code_timing[i] = vert_timing[i]) == 0)
7348c2ecf20Sopenharmony_ci			break;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	radeon_legacy_tv_init_restarts(encoder);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	/* play with DAC_CNTL */
7408c2ecf20Sopenharmony_ci	/* play with GPIOPAD_A */
7418c2ecf20Sopenharmony_ci	/* DISP_OUTPUT_CNTL */
7428c2ecf20Sopenharmony_ci	/* use reference freq */
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/* program the TV registers */
7458c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
7468c2ecf20Sopenharmony_ci				       RADEON_CRT_ASYNC_RST | RADEON_TV_FIFO_ASYNC_RST));
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	tmp = RREG32(RADEON_TV_DAC_CNTL);
7498c2ecf20Sopenharmony_ci	tmp &= ~RADEON_TV_DAC_NBLANK;
7508c2ecf20Sopenharmony_ci	tmp |= RADEON_TV_DAC_BGSLEEP |
7518c2ecf20Sopenharmony_ci		RADEON_TV_DAC_RDACPD |
7528c2ecf20Sopenharmony_ci		RADEON_TV_DAC_GDACPD |
7538c2ecf20Sopenharmony_ci		RADEON_TV_DAC_BDACPD;
7548c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_DAC_CNTL, tmp);
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	/* TV PLL */
7578c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVCLK_SRC_SEL_TVPLL);
7588c2ecf20Sopenharmony_ci	WREG32_PLL(RADEON_TV_PLL_CNTL, tv_pll_cntl);
7598c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVPLL_RESET, ~RADEON_TVPLL_RESET);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	radeon_wait_pll_lock(encoder, 200, 800, 135);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_RESET);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	radeon_wait_pll_lock(encoder, 300, 160, 27);
7668c2ecf20Sopenharmony_ci	radeon_wait_pll_lock(encoder, 200, 800, 135);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~0xf);
7698c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVCLK_SRC_SEL_TVPLL, ~RADEON_TVCLK_SRC_SEL_TVPLL);
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, (1 << RADEON_TVPDC_SHIFT), ~RADEON_TVPDC_MASK);
7728c2ecf20Sopenharmony_ci	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_SLEEP);
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	/* TV HV */
7758c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_RGB_CNTL, tv_rgb_cntl);
7768c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HTOTAL, const_ptr->hor_total - 1);
7778c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HDISP, const_ptr->hor_resolution - 1);
7788c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_HSTART, const_ptr->hor_start);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_VTOTAL, const_ptr->ver_total - 1);
7818c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_VDISP, const_ptr->ver_resolution - 1);
7828c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_FTOTAL, tv_ftotal);
7838c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_VSCALER_CNTL1, tv_vscaler_cntl1);
7848c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_VSCALER_CNTL2, tv_vscaler_cntl2);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_Y_FALL_CNTL, tv_y_fall_cntl);
7878c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_Y_RISE_CNTL, tv_y_rise_cntl);
7888c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_Y_SAW_TOOTH_CNTL, tv_y_saw_tooth_cntl);
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
7918c2ecf20Sopenharmony_ci				       RADEON_CRT_ASYNC_RST));
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	/* TV restarts */
7948c2ecf20Sopenharmony_ci	radeon_legacy_write_tv_restarts(radeon_encoder);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	/* tv timings */
7978c2ecf20Sopenharmony_ci	radeon_restore_tv_timing_tables(radeon_encoder);
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST));
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	/* tv std */
8028c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_SYNC_CNTL, (RADEON_SYNC_PUB | RADEON_TV_SYNC_IO_DRIVE));
8038c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_TIMING_CNTL, tv_dac->tv.timing_cntl);
8048c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MODULATOR_CNTL1, tv_modulator_cntl1);
8058c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MODULATOR_CNTL2, tv_modulator_cntl2);
8068c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_PRE_DAC_MUX_CNTL, (RADEON_Y_RED_EN |
8078c2ecf20Sopenharmony_ci					    RADEON_C_GRN_EN |
8088c2ecf20Sopenharmony_ci					    RADEON_CMP_BLU_EN |
8098c2ecf20Sopenharmony_ci					    RADEON_DAC_DITHER_EN));
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_CRC_CNTL, 0);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_MASTER_CNTL, tv_master_cntl);
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_GAIN_LIMIT_SETTINGS, ((0x17f << RADEON_UV_GAIN_LIMIT_SHIFT) |
8168c2ecf20Sopenharmony_ci					       (0x5ff << RADEON_Y_GAIN_LIMIT_SHIFT)));
8178c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_LINEAR_GAIN_SETTINGS, ((0x100 << RADEON_UV_GAIN_SHIFT) |
8188c2ecf20Sopenharmony_ci						(0x100 << RADEON_Y_GAIN_SHIFT)));
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_civoid radeon_legacy_tv_adjust_crtc_reg(struct drm_encoder *encoder,
8258c2ecf20Sopenharmony_ci				      uint32_t *h_total_disp, uint32_t *h_sync_strt_wid,
8268c2ecf20Sopenharmony_ci				      uint32_t *v_total_disp, uint32_t *v_sync_strt_wid)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
8298c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
8308c2ecf20Sopenharmony_ci	uint32_t tmp;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
8338c2ecf20Sopenharmony_ci	if (!const_ptr)
8348c2ecf20Sopenharmony_ci		return;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	*h_total_disp = (((const_ptr->hor_resolution / 8) - 1) << RADEON_CRTC_H_DISP_SHIFT) |
8378c2ecf20Sopenharmony_ci		(((const_ptr->hor_total / 8) - 1) << RADEON_CRTC_H_TOTAL_SHIFT);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	tmp = *h_sync_strt_wid;
8408c2ecf20Sopenharmony_ci	tmp &= ~(RADEON_CRTC_H_SYNC_STRT_PIX | RADEON_CRTC_H_SYNC_STRT_CHAR);
8418c2ecf20Sopenharmony_ci	tmp |= (((const_ptr->hor_syncstart / 8) - 1) << RADEON_CRTC_H_SYNC_STRT_CHAR_SHIFT) |
8428c2ecf20Sopenharmony_ci		(const_ptr->hor_syncstart & 7);
8438c2ecf20Sopenharmony_ci	*h_sync_strt_wid = tmp;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	*v_total_disp = ((const_ptr->ver_resolution - 1) << RADEON_CRTC_V_DISP_SHIFT) |
8468c2ecf20Sopenharmony_ci		((const_ptr->ver_total - 1) << RADEON_CRTC_V_TOTAL_SHIFT);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	tmp = *v_sync_strt_wid;
8498c2ecf20Sopenharmony_ci	tmp &= ~RADEON_CRTC_V_SYNC_STRT;
8508c2ecf20Sopenharmony_ci	tmp |= ((const_ptr->ver_syncstart - 1) << RADEON_CRTC_V_SYNC_STRT_SHIFT);
8518c2ecf20Sopenharmony_ci	*v_sync_strt_wid = tmp;
8528c2ecf20Sopenharmony_ci}
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_cistatic int get_post_div(int value)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	int post_div;
8578c2ecf20Sopenharmony_ci	switch (value) {
8588c2ecf20Sopenharmony_ci	case 1: post_div = 0; break;
8598c2ecf20Sopenharmony_ci	case 2: post_div = 1; break;
8608c2ecf20Sopenharmony_ci	case 3: post_div = 4; break;
8618c2ecf20Sopenharmony_ci	case 4: post_div = 2; break;
8628c2ecf20Sopenharmony_ci	case 6: post_div = 6; break;
8638c2ecf20Sopenharmony_ci	case 8: post_div = 3; break;
8648c2ecf20Sopenharmony_ci	case 12: post_div = 7; break;
8658c2ecf20Sopenharmony_ci	case 16:
8668c2ecf20Sopenharmony_ci	default: post_div = 5; break;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci	return post_div;
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_civoid radeon_legacy_tv_adjust_pll1(struct drm_encoder *encoder,
8728c2ecf20Sopenharmony_ci				  uint32_t *htotal_cntl, uint32_t *ppll_ref_div,
8738c2ecf20Sopenharmony_ci				  uint32_t *ppll_div_3, uint32_t *pixclks_cntl)
8748c2ecf20Sopenharmony_ci{
8758c2ecf20Sopenharmony_ci	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
8768c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
8798c2ecf20Sopenharmony_ci	if (!const_ptr)
8808c2ecf20Sopenharmony_ci		return;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	*htotal_cntl = (const_ptr->hor_total & 0x7) | RADEON_HTOT_CNTL_VGA_EN;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	*ppll_ref_div = const_ptr->crtcPLL_M;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	*ppll_div_3 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
8878c2ecf20Sopenharmony_ci	*pixclks_cntl &= ~(RADEON_PIX2CLK_SRC_SEL_MASK | RADEON_PIXCLK_TV_SRC_SEL);
8888c2ecf20Sopenharmony_ci	*pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK;
8898c2ecf20Sopenharmony_ci}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_civoid radeon_legacy_tv_adjust_pll2(struct drm_encoder *encoder,
8928c2ecf20Sopenharmony_ci				  uint32_t *htotal2_cntl, uint32_t *p2pll_ref_div,
8938c2ecf20Sopenharmony_ci				  uint32_t *p2pll_div_0, uint32_t *pixclks_cntl)
8948c2ecf20Sopenharmony_ci{
8958c2ecf20Sopenharmony_ci	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
8968c2ecf20Sopenharmony_ci	const struct radeon_tv_mode_constants *const_ptr;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
8998c2ecf20Sopenharmony_ci	if (!const_ptr)
9008c2ecf20Sopenharmony_ci		return;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	*htotal2_cntl = (const_ptr->hor_total & 0x7);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	*p2pll_ref_div = const_ptr->crtcPLL_M;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	*p2pll_div_0 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
9078c2ecf20Sopenharmony_ci	*pixclks_cntl &= ~RADEON_PIX2CLK_SRC_SEL_MASK;
9088c2ecf20Sopenharmony_ci	*pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK | RADEON_PIXCLK_TV_SRC_SEL;
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
911