V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / media / dvb / frontends / tda827x.c
blob229b11987a589fed652c00934c91101f59867eeb
1 /*
3 * (c) 2005 Hartmut Hackmann
4 * (c) 2007 Michael Krufky
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.
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.
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.
21 #include <linux/module.h>
22 #include <asm/types.h>
23 #include <linux/dvb/frontend.h>
24 #include <linux/videodev2.h>
26 #include "tda827x.h"
28 static int debug = 0;
29 module_param(debug, int, 0644);
30 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
32 #define dprintk(args...) \
33 do { \
34 if (debug) printk(KERN_DEBUG "tda827x: " args); \
35 } while (0)
37 struct tda827x_priv {
38 int i2c_addr;
39 struct i2c_adapter *i2c_adap;
40 struct tda827x_config *cfg;
42 unsigned int sgIF;
43 unsigned char lpsel;
45 u32 frequency;
46 u32 bandwidth;
49 static void tda827x_set_std(struct dvb_frontend *fe,
50 struct analog_parameters *params)
52 struct tda827x_priv *priv = fe->tuner_priv;
53 char *mode;
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";
83 if (params->mode == V4L2_TUNER_RADIO)
84 priv->sgIF = 88; /* if frequency is 5.5 MHz */
86 dprintk("setting tda827x to system %s\n", mode);
90 /* ------------------------------------------------------------------ */
92 struct tda827x_data {
93 u32 lomax;
94 u8 spd;
95 u8 bs;
96 u8 bp;
97 u8 cp;
98 u8 gc3;
99 u8 div1p5;
102 static const struct tda827x_data tda827x_table[] = {
103 { .lomax = 62000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
104 { .lomax = 66000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 1},
105 { .lomax = 76000000, .spd = 3, .bs = 1, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
106 { .lomax = 84000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 3, .div1p5 = 0},
107 { .lomax = 93000000, .spd = 3, .bs = 2, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
108 { .lomax = 98000000, .spd = 3, .bs = 3, .bp = 0, .cp = 0, .gc3 = 1, .div1p5 = 0},
109 { .lomax = 109000000, .spd = 3, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
110 { .lomax = 123000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
111 { .lomax = 133000000, .spd = 2, .bs = 3, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 1},
112 { .lomax = 151000000, .spd = 2, .bs = 1, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
113 { .lomax = 154000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 1, .div1p5 = 0},
114 { .lomax = 181000000, .spd = 2, .bs = 2, .bp = 1, .cp = 0, .gc3 = 0, .div1p5 = 0},
115 { .lomax = 185000000, .spd = 2, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
116 { .lomax = 217000000, .spd = 2, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
117 { .lomax = 244000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
118 { .lomax = 265000000, .spd = 1, .bs = 3, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 1},
119 { .lomax = 302000000, .spd = 1, .bs = 1, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
120 { .lomax = 324000000, .spd = 1, .bs = 2, .bp = 2, .cp = 0, .gc3 = 1, .div1p5 = 0},
121 { .lomax = 370000000, .spd = 1, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
122 { .lomax = 454000000, .spd = 1, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
123 { .lomax = 493000000, .spd = 0, .bs = 2, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
124 { .lomax = 530000000, .spd = 0, .bs = 3, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 1},
125 { .lomax = 554000000, .spd = 0, .bs = 1, .bp = 3, .cp = 0, .gc3 = 1, .div1p5 = 0},
126 { .lomax = 604000000, .spd = 0, .bs = 1, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
127 { .lomax = 696000000, .spd = 0, .bs = 2, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
128 { .lomax = 740000000, .spd = 0, .bs = 2, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
129 { .lomax = 820000000, .spd = 0, .bs = 3, .bp = 4, .cp = 0, .gc3 = 0, .div1p5 = 0},
130 { .lomax = 865000000, .spd = 0, .bs = 3, .bp = 4, .cp = 1, .gc3 = 0, .div1p5 = 0},
131 { .lomax = 0, .spd = 0, .bs = 0, .bp = 0, .cp = 0, .gc3 = 0, .div1p5 = 0}
134 static int tda827xo_set_params(struct dvb_frontend *fe,
135 struct dvb_frontend_parameters *params)
137 struct tda827x_priv *priv = fe->tuner_priv;
138 u8 buf[14];
140 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
141 .buf = buf, .len = sizeof(buf) };
142 int i, tuner_freq, if_freq;
143 u32 N;
145 dprintk("%s:\n", __FUNCTION__);
146 switch (params->u.ofdm.bandwidth) {
147 case BANDWIDTH_6_MHZ:
148 if_freq = 4000000;
149 break;
150 case BANDWIDTH_7_MHZ:
151 if_freq = 4500000;
152 break;
153 default: /* 8 MHz or Auto */
154 if_freq = 5000000;
155 break;
157 tuner_freq = params->frequency + if_freq;
159 i = 0;
160 while (tda827x_table[i].lomax < tuner_freq) {
161 if (tda827x_table[i + 1].lomax == 0)
162 break;
163 i++;
166 N = ((tuner_freq + 125000) / 250000) << (tda827x_table[i].spd + 2);
167 buf[0] = 0;
168 buf[1] = (N>>8) | 0x40;
169 buf[2] = N & 0xff;
170 buf[3] = 0;
171 buf[4] = 0x52;
172 buf[5] = (tda827x_table[i].spd << 6) + (tda827x_table[i].div1p5 << 5) +
173 (tda827x_table[i].bs << 3) +
174 tda827x_table[i].bp;
175 buf[6] = (tda827x_table[i].gc3 << 4) + 0x8f;
176 buf[7] = 0xbf;
177 buf[8] = 0x2a;
178 buf[9] = 0x05;
179 buf[10] = 0xff;
180 buf[11] = 0x00;
181 buf[12] = 0x00;
182 buf[13] = 0x40;
184 msg.len = 14;
185 if (fe->ops.i2c_gate_ctrl)
186 fe->ops.i2c_gate_ctrl(fe, 1);
187 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
188 printk("%s: could not write to tuner at addr: 0x%02x\n",
189 __FUNCTION__, priv->i2c_addr << 1);
190 return -EIO;
192 msleep(500);
193 /* correct CP value */
194 buf[0] = 0x30;
195 buf[1] = 0x50 + tda827x_table[i].cp;
196 msg.len = 2;
198 if (fe->ops.i2c_gate_ctrl)
199 fe->ops.i2c_gate_ctrl(fe, 1);
200 i2c_transfer(priv->i2c_adap, &msg, 1);
202 priv->frequency = tuner_freq - if_freq; // FIXME
203 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
205 return 0;
208 static int tda827xo_sleep(struct dvb_frontend *fe)
210 struct tda827x_priv *priv = fe->tuner_priv;
211 static u8 buf[] = { 0x30, 0xd0 };
212 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
213 .buf = buf, .len = sizeof(buf) };
215 dprintk("%s:\n", __FUNCTION__);
216 if (fe->ops.i2c_gate_ctrl)
217 fe->ops.i2c_gate_ctrl(fe, 1);
218 i2c_transfer(priv->i2c_adap, &msg, 1);
220 if (priv->cfg && priv->cfg->sleep)
221 priv->cfg->sleep(fe);
223 return 0;
226 /* ------------------------------------------------------------------ */
228 static int tda827xo_set_analog_params(struct dvb_frontend *fe,
229 struct analog_parameters *params)
231 unsigned char tuner_reg[8];
232 unsigned char reg2[2];
233 u32 N;
234 int i;
235 struct tda827x_priv *priv = fe->tuner_priv;
236 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0 };
237 unsigned int freq = params->frequency;
239 tda827x_set_std(fe, params);
241 if (params->mode == V4L2_TUNER_RADIO)
242 freq = freq / 1000;
244 N = freq + priv->sgIF;
246 i = 0;
247 while (tda827x_table[i].lomax < N * 62500) {
248 if (tda827x_table[i + 1].lomax == 0)
249 break;
250 i++;
253 N = N << tda827x_table[i].spd;
255 tuner_reg[0] = 0;
256 tuner_reg[1] = (unsigned char)(N>>8);
257 tuner_reg[2] = (unsigned char) N;
258 tuner_reg[3] = 0x40;
259 tuner_reg[4] = 0x52 + (priv->lpsel << 5);
260 tuner_reg[5] = (tda827x_table[i].spd << 6) +
261 (tda827x_table[i].div1p5 << 5) +
262 (tda827x_table[i].bs << 3) + tda827x_table[i].bp;
263 tuner_reg[6] = 0x8f + (tda827x_table[i].gc3 << 4);
264 tuner_reg[7] = 0x8f;
266 msg.buf = tuner_reg;
267 msg.len = 8;
268 i2c_transfer(priv->i2c_adap, &msg, 1);
270 msg.buf = reg2;
271 msg.len = 2;
272 reg2[0] = 0x80;
273 reg2[1] = 0;
274 i2c_transfer(priv->i2c_adap, &msg, 1);
276 reg2[0] = 0x60;
277 reg2[1] = 0xbf;
278 i2c_transfer(priv->i2c_adap, &msg, 1);
280 reg2[0] = 0x30;
281 reg2[1] = tuner_reg[4] + 0x80;
282 i2c_transfer(priv->i2c_adap, &msg, 1);
284 msleep(1);
285 reg2[0] = 0x30;
286 reg2[1] = tuner_reg[4] + 4;
287 i2c_transfer(priv->i2c_adap, &msg, 1);
289 msleep(1);
290 reg2[0] = 0x30;
291 reg2[1] = tuner_reg[4];
292 i2c_transfer(priv->i2c_adap, &msg, 1);
294 msleep(550);
295 reg2[0] = 0x30;
296 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_table[i].cp;
297 i2c_transfer(priv->i2c_adap, &msg, 1);
299 reg2[0] = 0x60;
300 reg2[1] = 0x3f;
301 i2c_transfer(priv->i2c_adap, &msg, 1);
303 reg2[0] = 0x80;
304 reg2[1] = 0x08; /* Vsync en */
305 i2c_transfer(priv->i2c_adap, &msg, 1);
307 priv->frequency = freq * 62500;
309 return 0;
312 static void tda827xo_agcf(struct dvb_frontend *fe)
314 struct tda827x_priv *priv = fe->tuner_priv;
315 unsigned char data[] = { 0x80, 0x0c };
316 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
317 .buf = data, .len = 2};
319 i2c_transfer(priv->i2c_adap, &msg, 1);
322 /* ------------------------------------------------------------------ */
324 struct tda827xa_data {
325 u32 lomax;
326 u8 svco;
327 u8 spd;
328 u8 scr;
329 u8 sbs;
330 u8 gc3;
333 static const struct tda827xa_data tda827xa_dvbt[] = {
334 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
335 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
336 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
337 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
338 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
339 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
340 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
341 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
342 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
343 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
344 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
345 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
346 { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
347 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
348 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
349 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
350 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
351 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
352 { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
353 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
354 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
355 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
356 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
357 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
358 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
359 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
360 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
363 static struct tda827xa_data tda827xa_analog[] = {
364 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3},
365 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
366 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
367 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
368 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
369 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
370 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
371 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
372 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
373 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
374 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3},
375 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3},
376 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
377 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
378 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
379 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
380 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
381 { .lomax = 554000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
382 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
383 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
384 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
385 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
386 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
387 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
388 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
389 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
392 static int tda827xa_set_params(struct dvb_frontend *fe,
393 struct dvb_frontend_parameters *params)
395 struct tda827x_priv *priv = fe->tuner_priv;
396 u8 buf[11];
398 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
399 .buf = buf, .len = sizeof(buf) };
401 int i, tuner_freq, if_freq;
402 u32 N;
404 dprintk("%s:\n", __FUNCTION__);
405 if (priv->cfg && priv->cfg->lna_gain)
406 priv->cfg->lna_gain(fe, 1);
407 msleep(20);
409 switch (params->u.ofdm.bandwidth) {
410 case BANDWIDTH_6_MHZ:
411 if_freq = 4000000;
412 break;
413 case BANDWIDTH_7_MHZ:
414 if_freq = 4500000;
415 break;
416 default: /* 8 MHz or Auto */
417 if_freq = 5000000;
418 break;
420 tuner_freq = params->frequency + if_freq;
422 i = 0;
423 while (tda827xa_dvbt[i].lomax < tuner_freq) {
424 if(tda827xa_dvbt[i + 1].lomax == 0)
425 break;
426 i++;
429 N = ((tuner_freq + 31250) / 62500) << tda827xa_dvbt[i].spd;
430 buf[0] = 0; // subaddress
431 buf[1] = N >> 8;
432 buf[2] = N & 0xff;
433 buf[3] = 0;
434 buf[4] = 0x16;
435 buf[5] = (tda827xa_dvbt[i].spd << 5) + (tda827xa_dvbt[i].svco << 3) +
436 tda827xa_dvbt[i].sbs;
437 buf[6] = 0x4b + (tda827xa_dvbt[i].gc3 << 4);
438 buf[7] = 0x1c;
439 buf[8] = 0x06;
440 buf[9] = 0x24;
441 buf[10] = 0x00;
442 msg.len = 11;
443 if (fe->ops.i2c_gate_ctrl)
444 fe->ops.i2c_gate_ctrl(fe, 1);
445 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
446 printk("%s: could not write to tuner at addr: 0x%02x\n",
447 __FUNCTION__, priv->i2c_addr << 1);
448 return -EIO;
450 buf[0] = 0x90;
451 buf[1] = 0xff;
452 buf[2] = 0x60;
453 buf[3] = 0x00;
454 buf[4] = 0x59; // lpsel, for 6MHz + 2
455 msg.len = 5;
456 if (fe->ops.i2c_gate_ctrl)
457 fe->ops.i2c_gate_ctrl(fe, 1);
458 i2c_transfer(priv->i2c_adap, &msg, 1);
460 buf[0] = 0xa0;
461 buf[1] = 0x40;
462 msg.len = 2;
463 if (fe->ops.i2c_gate_ctrl)
464 fe->ops.i2c_gate_ctrl(fe, 1);
465 i2c_transfer(priv->i2c_adap, &msg, 1);
467 msleep(11);
468 msg.flags = I2C_M_RD;
469 if (fe->ops.i2c_gate_ctrl)
470 fe->ops.i2c_gate_ctrl(fe, 1);
471 i2c_transfer(priv->i2c_adap, &msg, 1);
472 msg.flags = 0;
474 buf[1] >>= 4;
475 dprintk("tda8275a AGC2 gain is: %d\n", buf[1]);
476 if ((buf[1]) < 2) {
477 if (priv->cfg && priv->cfg->lna_gain)
478 priv->cfg->lna_gain(fe, 0);
479 buf[0] = 0x60;
480 buf[1] = 0x0c;
481 if (fe->ops.i2c_gate_ctrl)
482 fe->ops.i2c_gate_ctrl(fe, 1);
483 i2c_transfer(priv->i2c_adap, &msg, 1);
486 buf[0] = 0xc0;
487 buf[1] = 0x99; // lpsel, for 6MHz + 2
488 if (fe->ops.i2c_gate_ctrl)
489 fe->ops.i2c_gate_ctrl(fe, 1);
490 i2c_transfer(priv->i2c_adap, &msg, 1);
492 buf[0] = 0x60;
493 buf[1] = 0x3c;
494 if (fe->ops.i2c_gate_ctrl)
495 fe->ops.i2c_gate_ctrl(fe, 1);
496 i2c_transfer(priv->i2c_adap, &msg, 1);
498 /* correct CP value */
499 buf[0] = 0x30;
500 buf[1] = 0x10 + tda827xa_dvbt[i].scr;
501 if (fe->ops.i2c_gate_ctrl)
502 fe->ops.i2c_gate_ctrl(fe, 1);
503 i2c_transfer(priv->i2c_adap, &msg, 1);
505 msleep(163);
506 buf[0] = 0xc0;
507 buf[1] = 0x39; // lpsel, for 6MHz + 2
508 if (fe->ops.i2c_gate_ctrl)
509 fe->ops.i2c_gate_ctrl(fe, 1);
510 i2c_transfer(priv->i2c_adap, &msg, 1);
512 msleep(3);
513 /* freeze AGC1 */
514 buf[0] = 0x50;
515 buf[1] = 0x4f + (tda827xa_dvbt[i].gc3 << 4);
516 if (fe->ops.i2c_gate_ctrl)
517 fe->ops.i2c_gate_ctrl(fe, 1);
518 i2c_transfer(priv->i2c_adap, &msg, 1);
520 priv->frequency = tuner_freq - if_freq; // FIXME
521 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
523 return 0;
526 static int tda827xa_sleep(struct dvb_frontend *fe)
528 struct tda827x_priv *priv = fe->tuner_priv;
529 static u8 buf[] = { 0x30, 0x90 };
530 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
531 .buf = buf, .len = sizeof(buf) };
533 dprintk("%s:\n", __FUNCTION__);
534 if (fe->ops.i2c_gate_ctrl)
535 fe->ops.i2c_gate_ctrl(fe, 1);
537 i2c_transfer(priv->i2c_adap, &msg, 1);
539 if (fe->ops.i2c_gate_ctrl)
540 fe->ops.i2c_gate_ctrl(fe, 0);
542 if (priv->cfg && priv->cfg->sleep)
543 priv->cfg->sleep(fe);
545 return 0;
548 /* ------------------------------------------------------------------ */
550 static void tda827xa_lna_gain(struct dvb_frontend *fe, int high,
551 struct analog_parameters *params)
553 struct tda827x_priv *priv = fe->tuner_priv;
554 unsigned char buf[] = {0x22, 0x01};
555 int arg;
556 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
557 .buf = buf, .len = sizeof(buf) };
559 if (NULL == priv->cfg) {
560 dprintk("tda827x_config not defined, cannot set LNA gain!\n");
561 return;
564 if (priv->cfg->config) {
565 if (high)
566 dprintk("setting LNA to high gain\n");
567 else
568 dprintk("setting LNA to low gain\n");
570 switch (*priv->cfg->config) {
571 case 0: /* no LNA */
572 break;
573 case 1: /* switch is GPIO 0 of tda8290 */
574 case 2:
575 /* turn Vsync on */
576 if (params->std & V4L2_STD_MN)
577 arg = 1;
578 else
579 arg = 0;
580 if (priv->cfg->tuner_callback)
581 priv->cfg->tuner_callback(priv->i2c_adap->algo_data,
582 1, arg);
583 buf[1] = high ? 0 : 1;
584 if (*priv->cfg->config == 2)
585 buf[1] = high ? 1 : 0;
586 i2c_transfer(priv->i2c_adap, &msg, 1);
587 break;
588 case 3: /* switch with GPIO of saa713x */
589 if (priv->cfg->tuner_callback)
590 priv->cfg->tuner_callback(priv->i2c_adap->algo_data,
591 0, high);
592 break;
596 static int tda827xa_set_analog_params(struct dvb_frontend *fe,
597 struct analog_parameters *params)
599 unsigned char tuner_reg[11];
600 u32 N;
601 int i;
602 struct tda827x_priv *priv = fe->tuner_priv;
603 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
604 .buf = tuner_reg, .len = sizeof(tuner_reg) };
605 unsigned int freq = params->frequency;
607 tda827x_set_std(fe, params);
609 tda827xa_lna_gain(fe, 1, params);
610 msleep(10);
612 if (params->mode == V4L2_TUNER_RADIO)
613 freq = freq / 1000;
615 N = freq + priv->sgIF;
617 i = 0;
618 while (tda827xa_analog[i].lomax < N * 62500) {
619 if (tda827xa_analog[i + 1].lomax == 0)
620 break;
621 i++;
624 N = N << tda827xa_analog[i].spd;
626 tuner_reg[0] = 0;
627 tuner_reg[1] = (unsigned char)(N>>8);
628 tuner_reg[2] = (unsigned char) N;
629 tuner_reg[3] = 0;
630 tuner_reg[4] = 0x16;
631 tuner_reg[5] = (tda827xa_analog[i].spd << 5) +
632 (tda827xa_analog[i].svco << 3) +
633 tda827xa_analog[i].sbs;
634 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
635 tuner_reg[7] = 0x1c;
636 tuner_reg[8] = 4;
637 tuner_reg[9] = 0x20;
638 tuner_reg[10] = 0x00;
639 msg.len = 11;
640 i2c_transfer(priv->i2c_adap, &msg, 1);
642 tuner_reg[0] = 0x90;
643 tuner_reg[1] = 0xff;
644 tuner_reg[2] = 0xe0;
645 tuner_reg[3] = 0;
646 tuner_reg[4] = 0x99 + (priv->lpsel << 1);
647 msg.len = 5;
648 i2c_transfer(priv->i2c_adap, &msg, 1);
650 tuner_reg[0] = 0xa0;
651 tuner_reg[1] = 0xc0;
652 msg.len = 2;
653 i2c_transfer(priv->i2c_adap, &msg, 1);
655 tuner_reg[0] = 0x30;
656 tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
657 i2c_transfer(priv->i2c_adap, &msg, 1);
659 msg.flags = I2C_M_RD;
660 i2c_transfer(priv->i2c_adap, &msg, 1);
661 msg.flags = 0;
662 tuner_reg[1] >>= 4;
663 dprintk("AGC2 gain is: %d\n", tuner_reg[1]);
664 if (tuner_reg[1] < 1)
665 tda827xa_lna_gain(fe, 0, params);
667 msleep(100);
668 tuner_reg[0] = 0x60;
669 tuner_reg[1] = 0x3c;
670 i2c_transfer(priv->i2c_adap, &msg, 1);
672 msleep(163);
673 tuner_reg[0] = 0x50;
674 tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
675 i2c_transfer(priv->i2c_adap, &msg, 1);
677 tuner_reg[0] = 0x80;
678 tuner_reg[1] = 0x28;
679 i2c_transfer(priv->i2c_adap, &msg, 1);
681 tuner_reg[0] = 0xb0;
682 tuner_reg[1] = 0x01;
683 i2c_transfer(priv->i2c_adap, &msg, 1);
685 tuner_reg[0] = 0xc0;
686 tuner_reg[1] = 0x19 + (priv->lpsel << 1);
687 i2c_transfer(priv->i2c_adap, &msg, 1);
689 priv->frequency = freq * 62500;
691 return 0;
694 static void tda827xa_agcf(struct dvb_frontend *fe)
696 struct tda827x_priv *priv = fe->tuner_priv;
697 unsigned char data[] = {0x80, 0x2c};
698 struct i2c_msg msg = {.addr = priv->i2c_addr, .flags = 0,
699 .buf = data, .len = 2};
700 i2c_transfer(priv->i2c_adap, &msg, 1);
703 /* ------------------------------------------------------------------ */
705 static int tda827x_release(struct dvb_frontend *fe)
707 kfree(fe->tuner_priv);
708 fe->tuner_priv = NULL;
709 return 0;
712 static int tda827x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
714 struct tda827x_priv *priv = fe->tuner_priv;
715 *frequency = priv->frequency;
716 return 0;
719 static int tda827x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
721 struct tda827x_priv *priv = fe->tuner_priv;
722 *bandwidth = priv->bandwidth;
723 return 0;
726 static int tda827x_init(struct dvb_frontend *fe)
728 struct tda827x_priv *priv = fe->tuner_priv;
729 dprintk("%s:\n", __FUNCTION__);
730 if (priv->cfg && priv->cfg->init)
731 priv->cfg->init(fe);
733 return 0;
736 static int tda827x_probe_version(struct dvb_frontend *fe);
738 static int tda827x_initial_init(struct dvb_frontend *fe)
740 int ret;
741 ret = tda827x_probe_version(fe);
742 if (ret)
743 return ret;
744 return fe->ops.tuner_ops.init(fe);
747 static int tda827x_initial_sleep(struct dvb_frontend *fe)
749 int ret;
750 ret = tda827x_probe_version(fe);
751 if (ret)
752 return ret;
753 return fe->ops.tuner_ops.sleep(fe);
756 static struct dvb_tuner_ops tda827xo_tuner_ops = {
757 .info = {
758 .name = "Philips TDA827X",
759 .frequency_min = 55000000,
760 .frequency_max = 860000000,
761 .frequency_step = 250000
763 .release = tda827x_release,
764 .init = tda827x_initial_init,
765 .sleep = tda827x_initial_sleep,
766 .set_params = tda827xo_set_params,
767 .set_analog_params = tda827xo_set_analog_params,
768 .get_frequency = tda827x_get_frequency,
769 .get_bandwidth = tda827x_get_bandwidth,
772 static struct dvb_tuner_ops tda827xa_tuner_ops = {
773 .info = {
774 .name = "Philips TDA827XA",
775 .frequency_min = 44000000,
776 .frequency_max = 906000000,
777 .frequency_step = 62500
779 .release = tda827x_release,
780 .init = tda827x_init,
781 .sleep = tda827xa_sleep,
782 .set_params = tda827xa_set_params,
783 .set_analog_params = tda827xa_set_analog_params,
784 .get_frequency = tda827x_get_frequency,
785 .get_bandwidth = tda827x_get_bandwidth,
788 static int tda827x_probe_version(struct dvb_frontend *fe)
789 { u8 data;
790 struct tda827x_priv *priv = fe->tuner_priv;
791 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = I2C_M_RD,
792 .buf = &data, .len = 1 };
793 if (fe->ops.i2c_gate_ctrl)
794 fe->ops.i2c_gate_ctrl(fe, 1);
795 if (i2c_transfer(priv->i2c_adap, &msg, 1) != 1) {
796 printk("%s: could not read from tuner at addr: 0x%02x\n",
797 __FUNCTION__, msg.addr << 1);
798 return -EIO;
800 if ((data & 0x3c) == 0) {
801 dprintk("tda827x tuner found\n");
802 fe->ops.tuner_ops.init = tda827x_init;
803 fe->ops.tuner_ops.sleep = tda827xo_sleep;
804 if (priv->cfg)
805 priv->cfg->agcf = tda827xo_agcf;
806 } else {
807 dprintk("tda827xa tuner found\n");
808 memcpy(&fe->ops.tuner_ops, &tda827xa_tuner_ops, sizeof(struct dvb_tuner_ops));
809 if (priv->cfg)
810 priv->cfg->agcf = tda827xa_agcf;
812 return 0;
815 struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
816 struct i2c_adapter *i2c,
817 struct tda827x_config *cfg)
819 struct tda827x_priv *priv = NULL;
821 dprintk("%s:\n", __FUNCTION__);
822 priv = kzalloc(sizeof(struct tda827x_priv), GFP_KERNEL);
823 if (priv == NULL)
824 return NULL;
826 priv->i2c_addr = addr;
827 priv->i2c_adap = i2c;
828 priv->cfg = cfg;
829 memcpy(&fe->ops.tuner_ops, &tda827xo_tuner_ops, sizeof(struct dvb_tuner_ops));
830 fe->tuner_priv = priv;
832 dprintk("type set to %s\n", fe->ops.tuner_ops.info.name);
834 return fe;
836 EXPORT_SYMBOL_GPL(tda827x_attach);
838 MODULE_DESCRIPTION("DVB TDA827x driver");
839 MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
840 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
841 MODULE_LICENSE("GPL");
844 * Overrides for Emacs so that we follow Linus's tabbing style.
845 * ---------------------------------------------------------------------------
846 * Local variables:
847 * c-basic-offset: 8
848 * End: