summaryrefstree
path: root/kernel/drivers/media/isdbt/fc8300_spi/fci_tun.c
blob: b39106e16885fe00ef6079d1770361265c4c3ad7 (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
280
281
282
283
284
285
286
287
288
289
290
/*****************************************************************************
	Copyright(c) 2013 FCI Inc. All Rights Reserved

	File name : fci_tun.c

	Description : source of tuner control driver

	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; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

	History :
	----------------------------------------------------------------------
*******************************************************************************/
#include "fci_types.h"
#include "fc8300_regs.h"
#include "fci_hal.h"
#include "fci_tun.h"
#include "fci_hpi.h"
#include "fc8300_bb.h"
#include "fc8300_tun.h"
#include "fci_oal.h"

#define FC8300_TUNER_ADDR	0x58

struct I2C_DRV {
	int (*init)(HANDLE handle, DEVICEID devid,
			s32 speed, s32 slaveaddr);
	int (*read)(HANDLE handle, DEVICEID devid,
			u8 chip, u8 addr, u8 alen, u8 *data, u8 len);
	int (*write)(HANDLE handle, DEVICEID devid,
			u8 chip, u8 addr, u8 alen, u8 *data, u8 len);
	int (*deinit)(HANDLE handle, DEVICEID devid);
};

static struct I2C_DRV fcihpi = {
	&fci_hpi_init,
	&fci_hpi_read,
	&fci_hpi_write,
	&fci_hpi_deinit
};

struct TUNER_DRV {
	int (*init)(HANDLE handle, DEVICEID devid,
			enum BROADCAST_TYPE broadcast);
	int (*set_freq)(HANDLE handle, DEVICEID devid, u32 freq);
	int (*get_rssi)(HANDLE handle, DEVICEID devid, s32 *rssi);
	int (*deinit)(HANDLE handle, DEVICEID devid);
};

static struct TUNER_DRV fc8300_tuner = {
	&fc8300_tuner_init,
	&fc8300_set_freq,
	&fc8300_get_rssi,
	&fc8300_tuner_deinit
};

static u8 tuner_addr = FC8300_TUNER_ADDR;
static enum BROADCAST_TYPE broadcast_type = ISDBT_13SEG;
static enum I2C_TYPE tuner_i2c = FCI_HPI_TYPE;
static struct I2C_DRV *tuner_ctrl = &fcihpi;
static struct TUNER_DRV *tuner = &fc8300_tuner;

s32 tuner_ctrl_select(HANDLE handle, DEVICEID devid, enum I2C_TYPE type)
{
	print_log(0, " %s\n", __func__);
	switch (type) {
	case FCI_HPI_TYPE:
		print_log(0, " FCI_HPI_TYPE\n");
		tuner_ctrl = &fcihpi;
		break;
	default:
		print_log(0, " DEFAULT ERROR\n");
		return BBM_E_TN_CTRL_SELECT;
	}

	if (tuner_ctrl->init(handle, devid, 600, 0))
		return BBM_E_TN_CTRL_INIT;

	tuner_i2c = type;

	return BBM_OK;
}

s32 tuner_ctrl_deselect(HANDLE handle, DEVICEID devid)
{
	print_log(0, " %s\n", __func__);
	if (tuner_ctrl == NULL)
		return BBM_E_TN_CTRL_SELECT;

	tuner_ctrl->deinit(handle, devid);
	tuner_ctrl = NULL;

	return BBM_OK;
}

s32 tuner_i2c_read(HANDLE handle, DEVICEID devid,
		u8 addr, u8 alen, u8 *data, u8 len)
{
	if (tuner_ctrl == NULL)
		return BBM_E_TN_CTRL_SELECT;

	if (tuner_ctrl->read(handle, devid,
				tuner_addr, addr, alen, data, len))
		return BBM_E_TN_READ;

	return BBM_OK;
}

s32 tuner_i2c_write(HANDLE handle, DEVICEID devid,
		u8 addr, u8 alen, u8 *data, u8 len)
{
	if (tuner_ctrl == NULL)
		return BBM_E_TN_CTRL_SELECT;

	if (tuner_ctrl->write(handle, devid,
			tuner_addr, addr, alen, data, len))
		return BBM_E_TN_WRITE;

	return BBM_OK;
}

s32 tuner_set_freq(HANDLE handle, DEVICEID devid, u32 freq, u8 subch)
{
#ifdef BBM_I2C_TSIF
	u8 tsif_en = 0;
#endif

	if (tuner == NULL)
		return BBM_E_TN_SELECT;

#ifdef BBM_I2C_TSIF
	if (devid == DIV_MASTER || devid == DIV_BROADCAST) {
		bbm_byte_read(handle, DIV_MASTER, BBM_TS_SEL, &tsif_en);
		bbm_byte_write(handle, DIV_MASTER, BBM_TS_SEL, tsif_en & 0x7f);
	}
#endif

	fc8300_set_core_clk(handle, devid, broadcast_type, freq);

	bbm_byte_write(handle, devid, BBM_CENTER_CH_NUM, subch);

	switch (broadcast_type) {
	case ISDBT_1SEG:
	case ISDBTMM_1SEG:
	case ISDBTSB_1SEG:
	case ISDBT_CATV_1SEG:
		freq -= 380;
		break;
	case ISDBTSB_3SEG:
#if (BBM_BAND_WIDTH == 6)
		freq -= 1000;
#elif (BBM_BAND_WIDTH == 7)
		freq -= 1170;
#else /* BBM)BAND_WIDTH == 8 */
		freq -= 1330;
#endif
		break;
	case ISDBT_13SEG:
	case ISDBTMM_13SEG:
	case ISDBT_CATV_13SEG:
		break;
	}

	if (tuner->set_freq(handle, devid, freq))
		return BBM_E_TN_SET_FREQ;

	fc8300_reset(handle, devid);

#ifdef BBM_I2C_TSIF
	if (devid == DIV_MASTER || devid == DIV_BROADCAST)
		bbm_byte_write(handle, DIV_MASTER, BBM_TS_SEL, tsif_en);
#endif

	return BBM_OK;
}

s32 tuner_select(HANDLE handle, DEVICEID devid,
		enum PRODUCT_TYPE product, enum BROADCAST_TYPE broadcast)
{
	switch (product) {
	case FC8300_TUNER:
		tuner = &fc8300_tuner;
		tuner_addr = FC8300_TUNER_ADDR;
		broadcast_type = broadcast;
		break;
	}

	if (tuner == NULL) {
			print_log(0, "ERROR tuner == NULL\n");
		return BBM_E_TN_SELECT;
	}
	if (tuner->init(handle, devid, broadcast)) {
			print_log(0, "tuner->init\n");
		return BBM_E_TN_INIT;
	}
	fc8300_set_broadcast_mode(handle, devid, broadcast);

#ifdef BBM_ES

	if (product == FC8300_TUNER) {
		u8 chip_ver = 0x00;
		tuner_i2c_read(handle, devid, 0xff, 1, &chip_ver, 1);

		if (chip_ver == 0xc0)
			return BBM_OK;

		bbm_byte_write(handle, DIV_MASTER, BBM_RESYNC_ENABLE, 0xcf);
		bbm_long_write(handle, DIV_BROADCAST, BBM_MEMORY_RWM0,
							0x05555555);
		bbm_byte_write(handle, DIV_BROADCAST, BBM_SFS_FTS_ERR_MAX_1SEG,
							0x08);
		bbm_byte_write(handle, DIV_BROADCAST, BBM_SFS_FTS_ERR_MAX_3SEG,
							0x08);
		bbm_byte_write(handle, DIV_BROADCAST, BBM_PGA_GAIN_MAX, 0x0c);
		bbm_byte_write(handle, DIV_BROADCAST, BBM_CSF_GAIN_MAX, 0x09);
		bbm_byte_write(handle, DIV_MASTER, BBM_FD_OUT_MODE, 0x03);
		bbm_byte_write(handle, DIV_MASTER, BBM_DIV_START_MODE, 0x17);
		bbm_byte_write(handle, DIV_BROADCAST,
					BBM_PSAT_ON_REF_1SEG_QPSK, 0x1a);
		bbm_byte_write(handle, DIV_BROADCAST,
					BBM_PSAT_ON_REF_1SEG_16QAM, 0x1b);

		switch (broadcast) {
		case ISDBT_1SEG:
		case ISDBTMM_1SEG:
		case ISDBTSB_1SEG:
		case ISDBT_CATV_1SEG:
		case ISDBTSB_3SEG:
			bbm_byte_write(handle, DIV_BROADCAST, BBM_SFS_MTH,
							0x32);
			break;
		case ISDBT_13SEG:
		case ISDBTMM_13SEG:
		case ISDBT_CATV_13SEG:
			bbm_byte_write(handle, DIV_BROADCAST, BBM_SFS_MTH,
							0x31);
			break;
		}

#if defined(BBM_2_DIVERSITY) || defined(BBM_4_DIVERSITY)
		bbm_byte_write(handle, DIV_MASTER, BBM_XTAL_OUTBUF_EN, 0x00);
		bbm_byte_write(handle, DIV_MASTER, BBM_XTAL_OUTBUF_GAIN, 0x03);
		bbm_word_write(handle, DIV_BROADCAST, BBM_FD_RD_LATENCY_1SEG,
								0x1840);
		bbm_byte_write(handle, DIV_BROADCAST, BBM_COMB_OFF, 0x80);
#else /* SINGLE */
		bbm_word_write(handle, DIV_BROADCAST, BBM_FD_RD_LATENCY_1SEG,
								0x0002);
#endif /* #if defined(BBM_2_DIVERSITY) || defined(BBM_4_DIVERSITY) */
	}
#endif /* #ifdef BBM_ES */

	return BBM_OK;
}

s32 tuner_deselect(HANDLE handle, DEVICEID devid)
{
	if (tuner == NULL)
		return BBM_E_TN_SELECT;

	if (tuner->deinit(handle, devid))
		return BBM_NOK;

	tuner = NULL;

	return BBM_OK;
}

s32 tuner_get_rssi(HANDLE handle, DEVICEID devid, s32 *rssi)
{
	if (tuner == NULL)
		return BBM_E_TN_SELECT;

	if (tuner->get_rssi(handle, devid, rssi))
		return BBM_E_TN_RSSI;

	return BBM_OK;
}