summaryrefstree
path: root/kernel/drivers/media/tdmb/mtv319/mtv319_rf.c
blob: 6cd641766aa241d24891a37264b21f773a86c315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
*
* File name: mtv319_rf.c
*
* Description : MTV319 RF services source driver.
*
* Copyright (C) (2013, RAONTECH)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2.
*
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
* kind, whether express or implied; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

#include "mtv319_rf.h"
#include "mtv319_rf_adc_data.h"

/* Down conversion Signal Monitoring. */
/* #define DEBUG_A_TEST_ZERO */

static const struct RTV_REG_INIT_INFO t_TDMB_INIT[] = {
	{0x21, 0x08},
	{0x22, 0x6c},
	{0x23, 0x54},
	{0x25, 0x0F}, /* FEBD[9:8], REFD[5:0] */
	{0x26, 0xE8}, /* FEBD[7:0] */
	{0x32, 0xff},
	{0x36, 0x88},
	{0x38, 0x49},
	{0x39, 0x31},
	{0x3A, 0xD0},
	{0x3B, 0x88},
	{0x3e, 0x71},
	{0x42, 0x06},
	{0x43, 0x4F},
	{0x44, 0x60},
	{0x45, 0x9A},
	{0x46, 0xd7},
	{0x48, 0x65},
	{0x49, 0x2c},
	{0x4a, 0x24},
	{0x4e, 0x60},
	{0x4f, 0x50},
	{0x51, 0x06},
	{0x52, 0x44},
	{0x56, 0xd8},
	{0x57, 0x86},
	{0x58, 0x1a},
	{0x59, 0x20},
	{0x63, 0x0c},
	{0x64, 0x0c},
	{0x65, 0x4c},
	{0x66, 0x0c},
	{0x67, 0x0c},
	{0x68, 0x4c},
	{0x69, 0x0c},
	{0x6a, 0x0c},
	{0x6b, 0x4c},
	{0x6d, 0x1e},
	{0x6e, 0x1e},
	{0x6f, 0x1e},
	{0x70, 0x1e},
	{0x71, 0x1e},
	{0x72, 0x1e},
	{0x73, 0x1f},
	{0x74, 0x1f},
	{0x78, 0x08},
	{0x7a, 0x07},
	{0x7b, 0x03},
	{0x7d, 0x08},
	{0x86, 0x84},
	{0x87, 0x34},
	{0x8e, 0xae},
	{0x8f, 0x94},
	{0x93, 0x1b},
	{0x94, 0xc3},
	{0x95, 0x70},
	{0x97, 0x7f},
	{0x99, 0x5c},
	{0x9a, 0xbf},
	{0x9b, 0xc6},
	{0x9d, 0x96},
	{0x9f, 0xd7},
	{0xa7, 0x11}
};

UINT g_dwRtvPrevChFreqKHz;


void rtvRF_InitOfdmTnco(void)
{
	const struct RTV_ADC_CFG_INFO *ptCfgTbl	= &g_atAdcCfgTbl_TDMB[0];

	RTV_REG_SET(0x38, ptCfgTbl->dwTNCO & 0xFF);
	RTV_REG_SET(0x39, (ptCfgTbl->dwTNCO>>8) & 0xFF);
	RTV_REG_SET(0x3A, (ptCfgTbl->dwTNCO>>16) & 0xFF);
	RTV_REG_SET(0x3B, (ptCfgTbl->dwTNCO>>24) & 0xFF);
}

static INT rtvRF_ChangeAdcClock(UINT nChIdx)
{
	UINT nRetryCnt = 10;
	U8 RD02;
	const struct RTV_ADC_CFG_INFO *ptCfgTbl	= &g_atAdcCfgTbl_TDMB[nChIdx];

	RTV_REG_MAP_SEL(RF_PAGE);
	RTV_REG_SET(0x25, (((ptCfgTbl->wFEBD&0x300)>>8)<<6) | ptCfgTbl->bREFD);
	RTV_REG_SET(0x26, ptCfgTbl->wFEBD & 0xFF);

	while (1) {
		RTV_DELAY_MS(1);

		RD02 = RTV_REG_GET(0x02);
		if (RD02 & 0x80)
			break;

		if (--nRetryCnt == 0) {
			RTV_DBGMSG0("[rtvRF_ChangeAdcClock] Syn Unlocked!\n");
			return RTV_ADC_CLK_UNLOCKED;
		}
	}

	RTV_REG_MAP_SEL(OFDM_PAGE);
	RTV_REG_SET(0x38, ptCfgTbl->dwTNCO & 0xFF);
	RTV_REG_SET(0x39, (ptCfgTbl->dwTNCO>>8) & 0xFF);
	RTV_REG_SET(0x3A, (ptCfgTbl->dwTNCO>>16) & 0xFF);
	RTV_REG_SET(0x3B, (ptCfgTbl->dwTNCO>>24) & 0xFF);

	return RTV_SUCCESS;
}

INT rtvRF_SetFrequency(U32 dwChFreqKHz)
{
	UINT nChIdx, nRetryCnt = 10;
	U8 WR32, WR2A, RD00;
	U32 dwPLLN, dwPLLF, dwPLLNF;
	INT nRet;
	U32 PLLFREQkHz = dwChFreqKHz << 2; /* x4 */
#if (RTV_SRC_CLK_FREQ_KHz == 13000) || (RTV_SRC_CLK_FREQ_KHz == 27000)
	#define pllf_mul	1
	#define r_div		3
#else
	#define pllf_mul	0
	#define r_div		4
#endif

	switch (dwChFreqKHz) {
	case 175280: /* 7A */
	case 177008: /* 7B */
	case 178736: /* 7C */
	case 181280: /* 8A */
	case 183008: /* 8B */
	case 184736: /* 8C */
	case 187280: /* 9A */
	case 189008: /* 9B */
	case 190736: /* 9C */
	case 193280: /* 10A */
	case 195008: /* 10B */
	case 196736: /* 10C */
	case 199280: /* 11A */
	case 201008: /* 11B */
	case 202736: /* 11C */
	case 205280: /* 12A */
	case 207008: /* 12B */
	case 208736: /* 12C */
	case 211280: /* 13A */
	case 213008: /* 13B */
	case 214736: /* 13C */
		break;

	default:
		RTV_DBGMSG1("Invalid freqency(%u)\n", dwChFreqKHz);
		return RTV_INVALID_FREQENCY;
	}

	nChIdx = (dwChFreqKHz - TDMB_FREQ_START__KOREA) / TDMB_FREQ_STEP__KOREA;
	if (dwChFreqKHz >= 205280)
		nChIdx -= 2;
	else if (dwChFreqKHz >= 193280)
		nChIdx -= 1;

	nRet = rtvRF_ChangeAdcClock(nChIdx);
	if (nRet != RTV_SUCCESS)
		goto RF_SET_FREQ_ERR;

	/* Set the PLLNF and channel. */
	dwPLLN = PLLFREQkHz / RTV_SRC_CLK_FREQ_KHz;
	dwPLLF = PLLFREQkHz - (dwPLLN * RTV_SRC_CLK_FREQ_KHz);
	dwPLLNF = (dwPLLN<<20)
		+ (((dwPLLF<<16) / (RTV_SRC_CLK_FREQ_KHz>>r_div)) << pllf_mul);

	RTV_REG_MAP_SEL(RF_PAGE);
	RTV_REG_SET(0x21, (dwPLLNF>>22) & 0xFF);
	RTV_REG_SET(0x22, (dwPLLNF>>14) & 0xFF);
	RTV_REG_SET(0x23, (dwPLLNF>>6) & 0xFF);

	/* SAR */
	WR32 = RTV_REG_GET(0x32) & 0xFD;
	WR2A = RTV_REG_GET(0x2A) & 0x3F;
	RTV_REG_SET(0x32, (WR32 | 0x02)); /* ENVCOCAL_I2C = 1 */
	RTV_REG_SET(0x2A, (WR2A | 0x80)); /* RECONF_I2C = 1 */
	RTV_REG_SET(0x2A, (WR2A | 0xC0)); /* INIT_VCOCAL_I2C = 1 */
	RTV_DELAY_MS(1);
	RTV_REG_SET(0x2A, (WR2A | 0x80)); /* INIT_VCOCAL_I2C = 0 */
	RTV_REG_SET(0x2A, (WR2A | 0x00)); /* RECONF_I2C = 0 */

	while (1) {
		RTV_DELAY_MS(1);

		RD00 = RTV_REG_GET(0x00);
		if (RD00 & 0x20) {
			RTV_DELAY_MS(1);
			RD00 = RTV_REG_GET(0x00);
			if (RD00 & 0x20) {
				RTV_DELAY_MS(1);
				RD00 = RTV_REG_GET(0x00);
				if (RD00 & 0x20) {
					RTV_DELAY_MS(1);
					RD00 = RTV_REG_GET(0x00);
					if (RD00 & 0x20)
						break;
				}
			}
		}

		RTV_REG_SET(0x2A, (WR2A | 0x80)); /* RECONF_I2C = 1 */
		RTV_REG_SET(0x2A, (WR2A | 0xC0)); /* INIT_VCOCAL_I2C = 1 */
		RTV_DELAY_MS(1);
		RTV_REG_SET(0x2A, (WR2A | 0x80)); /* INIT_VCOCAL_I2C = 1 */
		RTV_REG_SET(0x2A, (WR2A | 0x00)); /* RECONF_I2C = 1 */

		if (--nRetryCnt == 0) {
			RTV_DBGMSG0("[rtvRF_SetFrequency] PLL Unlocked!\n");
			return RTV_PLL_UNLOCKED;
		}
	}

	g_dwRtvPrevChFreqKHz = dwChFreqKHz;

	return RTV_SUCCESS;

RF_SET_FREQ_ERR:
	RTV_DBGMSG1("[rtvRF_SetFrequency] Error(%d)\n", nRet);
	return nRet;
}

INT rtvRF_Initilize(void)
{
	UINT nNumTblEntry;
	const struct RTV_REG_INIT_INFO *ptInitTbl;
#ifdef DEBUG_A_TEST_ZERO
	U8 bA8;
#endif

	ptInitTbl = t_TDMB_INIT;
	nNumTblEntry = sizeof(t_TDMB_INIT) / sizeof(struct RTV_REG_INIT_INFO);

	RTV_REG_MAP_SEL(RF_PAGE);
	do {
		RTV_REG_SET(ptInitTbl->bReg, ptInitTbl->bVal);
		ptInitTbl++;
	} while (--nNumTblEntry);

#ifdef DEBUG_A_TEST_ZERO
	bA8 = RTV_REG_GET(0xA8);
	bA8 |= 0x01;
	RTV_REG_SET(0xA8, bA8);
#endif

	g_dwRtvPrevChFreqKHz = 0;

	return RTV_SUCCESS;
}