• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * (c) 2005 Hartmut Hackmann
4  * (c) 2007 Michael Krufky
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <linux/module.h>
22 #include <asm/types.h>
23 #include <linux/dvb/frontend.h>
24 #include <linux/videodev2.h>
25 
26 #include "tda827x.h"
27 
28 static int debug;
29 module_param(debug, int, 0644);
30 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
31 
32 #define dprintk(args...) \
33 	do {					    \
34 		if (debug) printk(KERN_DEBUG "tda827x: " args); \
35 	} while (0)
36 
37 struct tda827x_priv {
38 	int i2c_addr;
39 	struct i2c_adapter *i2c_adap;
40 	struct tda827x_config *cfg;
41 
42 	unsigned int sgIF;
43 	unsigned char lpsel;
44 
45 	u32 frequency;
46 	u32 bandwidth;
47 };
48 
tda827x_set_std(struct dvb_frontend * fe,struct analog_parameters * params)49 static void tda827x_set_std(struct dvb_frontend *fe,
50 			    struct analog_parameters *params)
51 {
52 	struct tda827x_priv *priv = fe->tuner_priv;
53 	char *mode;
54 
55 	priv->lpsel = 0;
56 	if (params->std & V4L2_STD_MN) {
57 		priv->sgIF = 92;
58 		priv->lpsel = 1;
59 		mode = "MN";
60 	} else if (params->std & V4L2_STD_B) {
61 		priv->sgIF = 108;
62 		mode = "B";
63 	} else if (params->std & V4L2_STD_GH) {
64 		priv->sgIF = 124;
65 		mode = "GH";
66 	} else if (params->std & V4L2_STD_PAL_I) {
67 		priv->sgIF = 124;
68 		mode = "I";
69 	} else if (params->std & V4L2_STD_DK) {
70 		priv->sgIF = 124;
71 		mode = "DK";
72 	} else if (params->std & V4L2_STD_SECAM_L) {
73 		priv->sgIF = 124;
74 		mode = "L";
75 	} else if (params->std & V4L2_STD_SECAM_LC) {
76 		priv->sgIF = 20;
77 		mode = "LC";
78 	} else {
79 		priv->sgIF = 124;
80 		mode = "xx";
81 	}
82 
83 	if (params->mode == V4L2_TUNER_RADIO) {
84 		priv->sgIF = 88; /* if frequency is 5.5 MHz */
85 		dprintk("setting tda827x to radio FM\n");
86 	} else
87 		dprintk("setting tda827x to system %s\n", mode);
88 }
89 
90 
91 /* ------------------------------------------------------------------ */
92 
93 struct tda827x_data {
94 	u32 lomax;
95 	u8  spd;
96 	u8  bs;
97 	u8  bp;
98 	u8  cp;
99 	u8  gc3;
100 	u8 div1p5;
101 };
102 
103 static const struct tda827x_data tda827x_table[] = {
104 	{ .lomax =  62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
105 	{ .lomax =  66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
106 	{ .lomax =  76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
107 	{ .lomax =  84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
108 	{ .lomax =  93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
109 	{ .lomax =  98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
110 	{ .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
111 	{ .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
112 	{ .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
113 	{ .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
114 	{ .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
115 	{ .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
116 	{ .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
117 	{ .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
118 	{ .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
119 	{ .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
120 	{ .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
121 	{ .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
122 	{ .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
123 	{ .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
124 	{ .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
125 	{ .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
126 	{ .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
127 	{ .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
128 	{ .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
129 	{ .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
130 	{ .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
131 	{ .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
132 	{ .lomax =         0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
133 };
134 
tda827xo_set_params(struct dvb_frontend * fe,struct dvb_frontend_parameters * params)135 static int tda827xo_set_params(struct dvb_frontend *fe,
136 			       struct dvb_frontend_parameters *params)
137 {
138 	struct tda827x_priv *priv = fe->tuner_priv;
139 	u8 buf[14];
140 
141 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
142 			       .buf = buf, .len = sizeof(buf) };
143 	int i, tuner_freq, if_freq;
144 	u32 N;
145 
146 	dprintk("%s:\n", __func__);
147 	switch (params->u.ofdm.bandwidth) {
148 	case BANDWIDTH_6_MHZ:
149 		if_freq = 4000000;
150 		break;
151 	case BANDWIDTH_7_MHZ:
152 		if_freq = 4500000;
153 		break;
154 	default:		   /* 8 MHz or Auto */
155 		if_freq = 5000000;
156 		break;
157 	}
158 	tuner_freq = params->frequency + if_freq;
159 
160 	i = 0;
161 	while (tda827x_table[i].lomax < tuner_freq) {
162 		if (tda827x_table[i + 1].lomax == 0)
163 			break;
164 		i++;
165 	}
166 
167 	N = ((tuner_freq + 125000) / 250000) << (tda827x_table[i].spd + 2);
168 	buf[0] = 0;
169 	buf[1] = (N>>8) | 0x40;
170 	buf[2] = N & 0xff;
171 	buf[3] = 0;
172 	buf[4] = 0x52;
173 	buf[5] = (tda827x_table[i].spd << 6) + (tda827x_table[i].div1p5 << 5) +
174 				(tda827x_table[i].bs << 3) +
175 				tda827x_table[i].bp;
176 	buf[6] = (tda827x_table[i].gc3 << 4) + 0x8f;
177 	buf[7] = 0xbf;
178 	buf[8] = 0x2a;
179 	buf[9] = 0x05;
180 	buf[10] = 0xff;
181 	buf[11] = 0x00;
182 	buf[12] = 0x00;
183 	buf[13] = 0x40;
184 
185 	msg.len = 14;
186 	if (fe->ops.i2c_gate_ctrl)
187 		fe->ops.i2c_gate_ctrl(fe, 1);
188 	if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
189 		printk("%s: could not write to tuner at addr: 0x%02x\n",
190 		       __func__, priv->i2c_addr << 1);
191 		return -EIO;
192 	}
193 	msleep(500);
194 	/* correct CP value */
195 	buf[0] = 0x30;
196 	buf[1] = 0x50 + tda827x_table[i].cp;
197 	msg.len = 2;
198 
199 	if (fe->ops.i2c_gate_ctrl)
200 		fe->ops.i2c_gate_ctrl(fe, 1);
201 	i2c_transfer(priv->i2c_adap, &msg, 1);
202 
203 	priv->frequency = params->frequency;
204 	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
205 
206 	return 0;
207 }
208 
tda827xo_sleep(struct dvb_frontend * fe)209 static int tda827xo_sleep(struct dvb_frontend *fe)
210 {
211 	struct tda827x_priv *priv = fe->tuner_priv;
212 	static u8 buf[] = { 0x30, 0xd0 };
213 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
214 			       .buf = buf, .len = sizeof(buf) };
215 
216 	dprintk("%s:\n", __func__);
217 	if (fe->ops.i2c_gate_ctrl)
218 		fe->ops.i2c_gate_ctrl(fe, 1);
219 	i2c_transfer(priv->i2c_adap, &msg, 1);
220 
221 	if (priv->cfg && priv->cfg->sleep)
222 		priv->cfg->sleep(fe);
223 
224 	return 0;
225 }
226 
227 /* ------------------------------------------------------------------ */
228 
tda827xo_set_analog_params(struct dvb_frontend * fe,struct analog_parameters * params)229 static int tda827xo_set_analog_params(struct dvb_frontend *fe,
230 				      struct analog_parameters *params)
231 {
232 	unsigned char tuner_reg[8];
233 	unsigned char reg2[2];
234 	u32 N;
235 	int i;
236 	struct tda827x_priv *priv = fe->tuner_priv;
237 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0 };
238 	unsigned int freq = params->frequency;
239 
240 	tda827x_set_std(fe, params);
241 
242 	if (params->mode == V4L2_TUNER_RADIO)
243 		freq = freq / 1000;
244 
245 	N = freq + priv->sgIF;
246 
247 	i = 0;
248 	while (tda827x_table[i].lomax < N * 62500) {
249 		if (tda827x_table[i + 1].lomax == 0)
250 			break;
251 		i++;
252 	}
253 
254 	N = N << tda827x_table[i].spd;
255 
256 	tuner_reg[0] = 0;
257 	tuner_reg[1] = (unsigned char)(N>>8);
258 	tuner_reg[2] = (unsigned char) N;
259 	tuner_reg[3] = 0x40;
260 	tuner_reg[4] = 0x52 + (priv->lpsel << 5);
261 	tuner_reg[5] = (tda827x_table[i].spd    << 6) +
262 		       (tda827x_table[i].div1p5 << 5) +
263 		       (tda827x_table[i].bs     << 3) + tda827x_table[i].bp;
264 	tuner_reg[6] = 0x8f + (tda827x_table[i].gc3 << 4);
265 	tuner_reg[7] = 0x8f;
266 
267 	msg.buf = tuner_reg;
268 	msg.len = 8;
269 	i2c_transfer(priv->i2c_adap, &msg, 1);
270 
271 	msg.buf = reg2;
272 	msg.len = 2;
273 	reg2[0] = 0x80;
274 	reg2[1] = 0;
275 	i2c_transfer(priv->i2c_adap, &msg, 1);
276 
277 	reg2[0] = 0x60;
278 	reg2[1] = 0xbf;
279 	i2c_transfer(priv->i2c_adap, &msg, 1);
280 
281 	reg2[0] = 0x30;
282 	reg2[1] = tuner_reg[4] + 0x80;
283 	i2c_transfer(priv->i2c_adap, &msg, 1);
284 
285 	msleep(1);
286 	reg2[0] = 0x30;
287 	reg2[1] = tuner_reg[4] + 4;
288 	i2c_transfer(priv->i2c_adap, &msg, 1);
289 
290 	msleep(1);
291 	reg2[0] = 0x30;
292 	reg2[1] = tuner_reg[4];
293 	i2c_transfer(priv->i2c_adap, &msg, 1);
294 
295 	msleep(550);
296 	reg2[0] = 0x30;
297 	reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_table[i].cp;
298 	i2c_transfer(priv->i2c_adap, &msg, 1);
299 
300 	reg2[0] = 0x60;
301 	reg2[1] = 0x3f;
302 	i2c_transfer(priv->i2c_adap, &msg, 1);
303 
304 	reg2[0] = 0x80;
305 	reg2[1] = 0x08;   /* Vsync en */
306 	i2c_transfer(priv->i2c_adap, &msg, 1);
307 
308 	priv->frequency = params->frequency;
309 
310 	return 0;
311 }
312 
tda827xo_agcf(struct dvb_frontend * fe)313 static void tda827xo_agcf(struct dvb_frontend *fe)
314 {
315 	struct tda827x_priv *priv = fe->tuner_priv;
316 	unsigned char data[] = { 0x80, 0x0c };
317 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
318 			       .buf = data, .len = 2};
319 
320 	i2c_transfer(priv->i2c_adap, &msg, 1);
321 }
322 
323 /* ------------------------------------------------------------------ */
324 
325 struct tda827xa_data {
326 	u32 lomax;
327 	u8  svco;
328 	u8  spd;
329 	u8  scr;
330 	u8  sbs;
331 	u8  gc3;
332 };
333 
334 static const struct tda827xa_data tda827xa_dvbt[] = {
335 	{ .lomax =  56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
336 	{ .lomax =  67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
337 	{ .lomax =  81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
338 	{ .lomax =  97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
339 	{ .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
340 	{ .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
341 	{ .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
342 	{ .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
343 	{ .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
344 	{ .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
345 	{ .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
346 	{ .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
347 	{ .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
348 	{ .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
349 	{ .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
350 	{ .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
351 	{ .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
352 	{ .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
353 	{ .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
354 	{ .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
355 	{ .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
356 	{ .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
357 	{ .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
358 	{ .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
359 	{ .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
360 	{ .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
361 	{ .lomax =         0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
362 };
363 
364 static struct tda827xa_data tda827xa_analog[] = {
365 	{ .lomax =  56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3},
366 	{ .lomax =  67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
367 	{ .lomax =  81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
368 	{ .lomax =  97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
369 	{ .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
370 	{ .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
371 	{ .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
372 	{ .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
373 	{ .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
374 	{ .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
375 	{ .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3},
376 	{ .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3},
377 	{ .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
378 	{ .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
379 	{ .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
380 	{ .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
381 	{ .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
382 	{ .lomax = 554000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
383 	{ .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
384 	{ .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
385 	{ .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
386 	{ .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
387 	{ .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
388 	{ .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
389 	{ .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
390 	{ .lomax =         0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
391 };
392 
tda827xa_sleep(struct dvb_frontend * fe)393 static int tda827xa_sleep(struct dvb_frontend *fe)
394 {
395 	struct tda827x_priv *priv = fe->tuner_priv;
396 	static u8 buf[] = { 0x30, 0x90 };
397 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
398 			       .buf = buf, .len = sizeof(buf) };
399 
400 	dprintk("%s:\n", __func__);
401 	if (fe->ops.i2c_gate_ctrl)
402 		fe->ops.i2c_gate_ctrl(fe, 1);
403 
404 	i2c_transfer(priv->i2c_adap, &msg, 1);
405 
406 	if (fe->ops.i2c_gate_ctrl)
407 		fe->ops.i2c_gate_ctrl(fe, 0);
408 
409 	if (priv->cfg && priv->cfg->sleep)
410 		priv->cfg->sleep(fe);
411 
412 	return 0;
413 }
414 
tda827xa_lna_gain(struct dvb_frontend * fe,int high,struct analog_parameters * params)415 static void tda827xa_lna_gain(struct dvb_frontend *fe, int high,
416 			      struct analog_parameters *params)
417 {
418 	struct tda827x_priv *priv = fe->tuner_priv;
419 	unsigned char buf[] = {0x22, 0x01};
420 	int arg;
421 	int gp_func;
422 	struct i2c_msg msg = { .flags = 0, .buf = buf, .len = sizeof(buf) };
423 
424 	if (NULL == priv->cfg) {
425 		dprintk("tda827x_config not defined, cannot set LNA gain!\n");
426 		return;
427 	}
428 	msg.addr = priv->cfg->switch_addr;
429 	if (priv->cfg->config) {
430 		if (high)
431 			dprintk("setting LNA to high gain\n");
432 		else
433 			dprintk("setting LNA to low gain\n");
434 	}
435 	switch (priv->cfg->config) {
436 	case 0: /* no LNA */
437 		break;
438 	case 1: /* switch is GPIO 0 of tda8290 */
439 	case 2:
440 		if (params == NULL) {
441 			gp_func = 0;
442 			arg  = 0;
443 		} else {
444 			/* turn Vsync on */
445 			gp_func = 1;
446 			if (params->std & V4L2_STD_MN)
447 				arg = 1;
448 			else
449 				arg = 0;
450 		}
451 		if (fe->callback)
452 			fe->callback(priv->i2c_adap->algo_data,
453 				     DVB_FRONTEND_COMPONENT_TUNER,
454 				     gp_func, arg);
455 		buf[1] = high ? 0 : 1;
456 		if (priv->cfg->config == 2)
457 			buf[1] = high ? 1 : 0;
458 		i2c_transfer(priv->i2c_adap, &msg, 1);
459 		break;
460 	case 3: /* switch with GPIO of saa713x */
461 		if (fe->callback)
462 			fe->callback(priv->i2c_adap->algo_data,
463 				     DVB_FRONTEND_COMPONENT_TUNER, 0, high);
464 		break;
465 	}
466 }
467 
tda827xa_set_params(struct dvb_frontend * fe,struct dvb_frontend_parameters * params)468 static int tda827xa_set_params(struct dvb_frontend *fe,
469 			       struct dvb_frontend_parameters *params)
470 {
471 	struct tda827x_priv *priv = fe->tuner_priv;
472 	u8 buf[11];
473 
474 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
475 			       .buf = buf, .len = sizeof(buf) };
476 
477 	int i, tuner_freq, if_freq;
478 	u32 N;
479 
480 	dprintk("%s:\n", __func__);
481 
482 	tda827xa_lna_gain(fe, 1, NULL);
483 	msleep(20);
484 
485 	switch (params->u.ofdm.bandwidth) {
486 	case BANDWIDTH_6_MHZ:
487 		if_freq = 4000000;
488 		break;
489 	case BANDWIDTH_7_MHZ:
490 		if_freq = 4500000;
491 		break;
492 	default:		   /* 8 MHz or Auto */
493 		if_freq = 5000000;
494 		break;
495 	}
496 	tuner_freq = params->frequency + if_freq;
497 
498 	i = 0;
499 	while (tda827xa_dvbt[i].lomax < tuner_freq) {
500 		if(tda827xa_dvbt[i + 1].lomax == 0)
501 			break;
502 		i++;
503 	}
504 
505 	N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
506 	buf[0] = 0;            // subaddress
507 	buf[1] = N >> 8;
508 	buf[2] = N & 0xff;
509 	buf[3] = 0;
510 	buf[4] = 0x16;
511 	buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
512 			tda827xa_dvbt[i].sbs;
513 	buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
514 	buf[7] = 0x1c;
515 	buf[8] = 0x06;
516 	buf[9] = 0x24;
517 	buf[10] = 0x00;
518 	msg.len = 11;
519 	if (fe->ops.i2c_gate_ctrl)
520 		fe->ops.i2c_gate_ctrl(fe, 1);
521 	if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
522 		printk("%s: could not write to tuner at addr: 0x%02x\n",
523 		       __func__, priv->i2c_addr << 1);
524 		return -EIO;
525 	}
526 	buf[0] = 0x90;
527 	buf[1] = 0xff;
528 	buf[2] = 0x60;
529 	buf[3] = 0x00;
530 	buf[4] = 0x59;  // lpsel, for 6MHz + 2
531 	msg.len = 5;
532 	if (fe->ops.i2c_gate_ctrl)
533 		fe->ops.i2c_gate_ctrl(fe, 1);
534 	i2c_transfer(priv->i2c_adap, &msg, 1);
535 
536 	buf[0] = 0xa0;
537 	buf[1] = 0x40;
538 	msg.len = 2;
539 	if (fe->ops.i2c_gate_ctrl)
540 		fe->ops.i2c_gate_ctrl(fe, 1);
541 	i2c_transfer(priv->i2c_adap, &msg, 1);
542 
543 	msleep(11);
544 	msg.flags = I2C_M_RD;
545 	if (fe->ops.i2c_gate_ctrl)
546 		fe->ops.i2c_gate_ctrl(fe, 1);
547 	i2c_transfer(priv->i2c_adap, &msg, 1);
548 	msg.flags = 0;
549 
550 	buf[1] >>= 4;
551 	dprintk("tda8275a AGC2 gain is: %d\n", buf[1]);
552 	if ((buf[1]) < 2) {
553 		tda827xa_lna_gain(fe, 0, NULL);
554 		buf[0] = 0x60;
555 		buf[1] = 0x0c;
556 		if (fe->ops.i2c_gate_ctrl)
557 			fe->ops.i2c_gate_ctrl(fe, 1);
558 		i2c_transfer(priv->i2c_adap, &msg, 1);
559 	}
560 
561 	buf[0] = 0xc0;
562 	buf[1] = 0x99;    // lpsel, for 6MHz + 2
563 	if (fe->ops.i2c_gate_ctrl)
564 		fe->ops.i2c_gate_ctrl(fe, 1);
565 	i2c_transfer(priv->i2c_adap, &msg, 1);
566 
567 	buf[0] = 0x60;
568 	buf[1] = 0x3c;
569 	if (fe->ops.i2c_gate_ctrl)
570 		fe->ops.i2c_gate_ctrl(fe, 1);
571 	i2c_transfer(priv->i2c_adap, &msg, 1);
572 
573 	/* correct CP value */
574 	buf[0] = 0x30;
575 	buf[1] = 0x10 + tda827xa_dvbt[i].scr;
576 	if (fe->ops.i2c_gate_ctrl)
577 		fe->ops.i2c_gate_ctrl(fe, 1);
578 	i2c_transfer(priv->i2c_adap, &msg, 1);
579 
580 	msleep(163);
581 	buf[0] = 0xc0;
582 	buf[1] = 0x39;  // lpsel, for 6MHz + 2
583 	if (fe->ops.i2c_gate_ctrl)
584 		fe->ops.i2c_gate_ctrl(fe, 1);
585 	i2c_transfer(priv->i2c_adap, &msg, 1);
586 
587 	msleep(3);
588 	/* freeze AGC1 */
589 	buf[0] = 0x50;
590 	buf[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
591 	if (fe->ops.i2c_gate_ctrl)
592 		fe->ops.i2c_gate_ctrl(fe, 1);
593 	i2c_transfer(priv->i2c_adap, &msg, 1);
594 
595 	priv->frequency = params->frequency;
596 	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
597 
598 	return 0;
599 }
600 
601 
tda827xa_set_analog_params(struct dvb_frontend * fe,struct analog_parameters * params)602 static int tda827xa_set_analog_params(struct dvb_frontend *fe,
603 				      struct analog_parameters *params)
604 {
605 	unsigned char tuner_reg[11];
606 	u32 N;
607 	int i;
608 	struct tda827x_priv *priv = fe->tuner_priv;
609 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
610 			       .buf = tuner_reg, .len = sizeof(tuner_reg) };
611 	unsigned int freq = params->frequency;
612 
613 	tda827x_set_std(fe, params);
614 
615 	tda827xa_lna_gain(fe, 1, params);
616 	msleep(10);
617 
618 	if (params->mode == V4L2_TUNER_RADIO)
619 		freq = freq / 1000;
620 
621 	N = freq + priv->sgIF;
622 
623 	i = 0;
624 	while (tda827xa_analog[i].lomax < N * 62500) {
625 		if (tda827xa_analog[i + 1].lomax == 0)
626 			break;
627 		i++;
628 	}
629 
630 	N = N << tda827xa_analog[i].spd;
631 
632 	tuner_reg[0] = 0;
633 	tuner_reg[1] = (unsigned char)(N>>8);
634 	tuner_reg[2] = (unsigned char) N;
635 	tuner_reg[3] = 0;
636 	tuner_reg[4] = 0x16;
637 	tuner_reg[5] = (tda827xa_analog[i].spd << 5) +
638 		       (tda827xa_analog[i].svco << 3) +
639 			tda827xa_analog[i].sbs;
640 	tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
641 	tuner_reg[7] = 0x1c;
642 	tuner_reg[8] = 4;
643 	tuner_reg[9] = 0x20;
644 	tuner_reg[10] = 0x00;
645 	msg.len = 11;
646 	i2c_transfer(priv->i2c_adap, &msg, 1);
647 
648 	tuner_reg[0] = 0x90;
649 	tuner_reg[1] = 0xff;
650 	tuner_reg[2] = 0xe0;
651 	tuner_reg[3] = 0;
652 	tuner_reg[4] = 0x99 + (priv->lpsel << 1);
653 	msg.len = 5;
654 	i2c_transfer(priv->i2c_adap, &msg, 1);
655 
656 	tuner_reg[0] = 0xa0;
657 	tuner_reg[1] = 0xc0;
658 	msg.len = 2;
659 	i2c_transfer(priv->i2c_adap, &msg, 1);
660 
661 	tuner_reg[0] = 0x30;
662 	tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
663 	i2c_transfer(priv->i2c_adap, &msg, 1);
664 
665 	msg.flags = I2C_M_RD;
666 	i2c_transfer(priv->i2c_adap, &msg, 1);
667 	msg.flags = 0;
668 	tuner_reg[1] >>= 4;
669 	dprintk("AGC2 gain is: %d\n", tuner_reg[1]);
670 	if (tuner_reg[1] < 1)
671 		tda827xa_lna_gain(fe, 0, params);
672 
673 	msleep(100);
674 	tuner_reg[0] = 0x60;
675 	tuner_reg[1] = 0x3c;
676 	i2c_transfer(priv->i2c_adap, &msg, 1);
677 
678 	msleep(163);
679 	tuner_reg[0] = 0x50;
680 	tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
681 	i2c_transfer(priv->i2c_adap, &msg, 1);
682 
683 	tuner_reg[0] = 0x80;
684 	tuner_reg[1] = 0x28;
685 	i2c_transfer(priv->i2c_adap, &msg, 1);
686 
687 	tuner_reg[0] = 0xb0;
688 	tuner_reg[1] = 0x01;
689 	i2c_transfer(priv->i2c_adap, &msg, 1);
690 
691 	tuner_reg[0] = 0xc0;
692 	tuner_reg[1] = 0x19 + (priv->lpsel << 1);
693 	i2c_transfer(priv->i2c_adap, &msg, 1);
694 
695 	priv->frequency = params->frequency;
696 
697 	return 0;
698 }
699 
tda827xa_agcf(struct dvb_frontend * fe)700 static void tda827xa_agcf(struct dvb_frontend *fe)
701 {
702 	struct tda827x_priv *priv = fe->tuner_priv;
703 	unsigned char data[] = {0x80, 0x2c};
704 	struct i2c_msg msg = {.addr = priv->i2c_addr, .flags = 0,
705 			      .buf = data, .len = 2};
706 	i2c_transfer(priv->i2c_adap, &msg, 1);
707 }
708 
709 /* ------------------------------------------------------------------ */
710 
tda827x_release(struct dvb_frontend * fe)711 static int tda827x_release(struct dvb_frontend *fe)
712 {
713 	kfree(fe->tuner_priv);
714 	fe->tuner_priv = NULL;
715 	return 0;
716 }
717 
tda827x_get_frequency(struct dvb_frontend * fe,u32 * frequency)718 static int tda827x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
719 {
720 	struct tda827x_priv *priv = fe->tuner_priv;
721 	*frequency = priv->frequency;
722 	return 0;
723 }
724 
tda827x_get_bandwidth(struct dvb_frontend * fe,u32 * bandwidth)725 static int tda827x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
726 {
727 	struct tda827x_priv *priv = fe->tuner_priv;
728 	*bandwidth = priv->bandwidth;
729 	return 0;
730 }
731 
tda827x_init(struct dvb_frontend * fe)732 static int tda827x_init(struct dvb_frontend *fe)
733 {
734 	struct tda827x_priv *priv = fe->tuner_priv;
735 	dprintk("%s:\n", __func__);
736 	if (priv->cfg && priv->cfg->init)
737 		priv->cfg->init(fe);
738 
739 	return 0;
740 }
741 
742 static int tda827x_probe_version(struct dvb_frontend *fe);
743 
tda827x_initial_init(struct dvb_frontend * fe)744 static int tda827x_initial_init(struct dvb_frontend *fe)
745 {
746 	int ret;
747 	ret = tda827x_probe_version(fe);
748 	if (ret)
749 		return ret;
750 	return fe->ops.tuner_ops.init(fe);
751 }
752 
tda827x_initial_sleep(struct dvb_frontend * fe)753 static int tda827x_initial_sleep(struct dvb_frontend *fe)
754 {
755 	int ret;
756 	ret = tda827x_probe_version(fe);
757 	if (ret)
758 		return ret;
759 	return fe->ops.tuner_ops.sleep(fe);
760 }
761 
762 static struct dvb_tuner_ops tda827xo_tuner_ops = {
763 	.info = {
764 		.name = "Philips TDA827X",
765 		.frequency_min  =  55000000,
766 		.frequency_max  = 860000000,
767 		.frequency_step =    250000
768 	},
769 	.release = tda827x_release,
770 	.init = tda827x_initial_init,
771 	.sleep = tda827x_initial_sleep,
772 	.set_params = tda827xo_set_params,
773 	.set_analog_params = tda827xo_set_analog_params,
774 	.get_frequency = tda827x_get_frequency,
775 	.get_bandwidth = tda827x_get_bandwidth,
776 };
777 
778 static struct dvb_tuner_ops tda827xa_tuner_ops = {
779 	.info = {
780 		.name = "Philips TDA827XA",
781 		.frequency_min  =  44000000,
782 		.frequency_max  = 906000000,
783 		.frequency_step =     62500
784 	},
785 	.release = tda827x_release,
786 	.init = tda827x_init,
787 	.sleep = tda827xa_sleep,
788 	.set_params = tda827xa_set_params,
789 	.set_analog_params = tda827xa_set_analog_params,
790 	.get_frequency = tda827x_get_frequency,
791 	.get_bandwidth = tda827x_get_bandwidth,
792 };
793 
tda827x_probe_version(struct dvb_frontend * fe)794 static int tda827x_probe_version(struct dvb_frontend *fe)
795 {	u8 data;
796 	struct tda827x_priv *priv = fe->tuner_priv;
797 	struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = I2C_M_RD,
798 			       .buf = &data, .len = 1 };
799 	if (fe->ops.i2c_gate_ctrl)
800 		fe->ops.i2c_gate_ctrl(fe, 1);
801 	if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
802 		printk("%s: could not read from tuner at addr: 0x%02x\n",
803 		       __func__, msg.addr << 1);
804 		return -EIO;
805 	}
806 	if ((data & 0x3c) == 0) {
807 		dprintk("tda827x tuner found\n");
808 		fe->ops.tuner_ops.init  = tda827x_init;
809 		fe->ops.tuner_ops.sleep = tda827xo_sleep;
810 		if (priv->cfg)
811 			priv->cfg->agcf = tda827xo_agcf;
812 	} else {
813 		dprintk("tda827xa tuner found\n");
814 		memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops));
815 		if (priv->cfg)
816 			priv->cfg->agcf = tda827xa_agcf;
817 	}
818 	return 0;
819 }
820 
tda827x_attach(struct dvb_frontend * fe,int addr,struct i2c_adapter * i2c,struct tda827x_config * cfg)821 struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
822 				    struct i2c_adapter *i2c,
823 				    struct tda827x_config *cfg)
824 {
825 	struct tda827x_priv *priv = NULL;
826 
827 	dprintk("%s:\n", __func__);
828 	priv = kzalloc(sizeof(struct tda827x_priv), GFP_KERNEL);
829 	if (priv == NULL)
830 		return NULL;
831 
832 	priv->i2c_addr = addr;
833 	priv->i2c_adap = i2c;
834 	priv->cfg = cfg;
835 	memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops));
836 	fe->tuner_priv = priv;
837 
838 	dprintk("type set to %s\n", fe->ops.tuner_ops.info.name);
839 
840 	return fe;
841 }
842 EXPORT_SYMBOL_GPL(tda827x_attach);
843 
844 MODULE_DESCRIPTION("DVB TDA827x driver");
845 MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
846 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
847 MODULE_LICENSE("GPL");
848 
849 /*
850  * Overrides for Emacs so that we follow Linus's tabbing style.
851  * ---------------------------------------------------------------------------
852  * Local variables:
853  * c-basic-offset: 8
854  * End:
855  */
856