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