Staging: hv: code reduction from Vmbus.c
[linux/fpc-iii.git] / drivers / media / common / tuners / tda827x.c
blob36a7bc7585ab6c12473033a12ceeb72cb12e4861
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;
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 */
85 dprintk("setting tda827x to radio FM\n");
86 } else
87 dprintk("setting tda827x to system %s\n", mode);
91 /* ------------------------------------------------------------------ */
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;
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}
135 static int tuner_transfer(struct dvb_frontend *fe,
136 struct i2c_msg *msg,
137 const int size)
139 int rc;
140 struct tda827x_priv *priv = fe->tuner_priv;
142 if (fe->ops.i2c_gate_ctrl)
143 fe->ops.i2c_gate_ctrl(fe, 1);
144 rc = i2c_transfer(priv->i2c_adap, msg, size);
145 if (fe->ops.i2c_gate_ctrl)
146 fe->ops.i2c_gate_ctrl(fe, 0);
148 if (rc >= 0 && rc != size)
149 return -EIO;
151 return rc;
154 static int tda827xo_set_params(struct dvb_frontend *fe,
155 struct dvb_frontend_parameters *params)
157 struct tda827x_priv *priv = fe->tuner_priv;
158 u8 buf[14];
159 int rc;
161 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
162 .buf = buf, .len = sizeof(buf) };
163 int i, tuner_freq, if_freq;
164 u32 N;
166 dprintk("%s:\n", __func__);
167 switch (params->u.ofdm.bandwidth) {
168 case BANDWIDTH_6_MHZ:
169 if_freq = 4000000;
170 break;
171 case BANDWIDTH_7_MHZ:
172 if_freq = 4500000;
173 break;
174 default: /* 8 MHz or Auto */
175 if_freq = 5000000;
176 break;
178 tuner_freq = params->frequency + if_freq;
180 i = 0;
181 while (tda827x_table[i].lomax < tuner_freq) {
182 if (tda827x_table[i + 1].lomax == 0)
183 break;
184 i++;
187 N = ((tuner_freq + 125000) / 250000) << (tda827x_table[i].spd + 2);
188 buf[0] = 0;
189 buf[1] = (N>>8) | 0x40;
190 buf[2] = N & 0xff;
191 buf[3] = 0;
192 buf[4] = 0x52;
193 buf[5] = (tda827x_table[i].spd << 6) + (tda827x_table[i].div1p5 << 5) +
194 (tda827x_table[i].bs << 3) +
195 tda827x_table[i].bp;
196 buf[6] = (tda827x_table[i].gc3 << 4) + 0x8f;
197 buf[7] = 0xbf;
198 buf[8] = 0x2a;
199 buf[9] = 0x05;
200 buf[10] = 0xff;
201 buf[11] = 0x00;
202 buf[12] = 0x00;
203 buf[13] = 0x40;
205 msg.len = 14;
206 rc = tuner_transfer(fe, &msg, 1);
207 if (rc < 0)
208 goto err;
210 msleep(500);
211 /* correct CP value */
212 buf[0] = 0x30;
213 buf[1] = 0x50 + tda827x_table[i].cp;
214 msg.len = 2;
216 rc = tuner_transfer(fe, &msg, 1);
217 if (rc < 0)
218 goto err;
220 priv->frequency = params->frequency;
221 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
223 return 0;
225 err:
226 printk(KERN_ERR "%s: could not write to tuner at addr: 0x%02x\n",
227 __func__, priv->i2c_addr << 1);
228 return rc;
231 static int tda827xo_sleep(struct dvb_frontend *fe)
233 struct tda827x_priv *priv = fe->tuner_priv;
234 static u8 buf[] = { 0x30, 0xd0 };
235 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
236 .buf = buf, .len = sizeof(buf) };
238 dprintk("%s:\n", __func__);
239 tuner_transfer(fe, &msg, 1);
241 if (priv->cfg && priv->cfg->sleep)
242 priv->cfg->sleep(fe);
244 return 0;
247 /* ------------------------------------------------------------------ */
249 static int tda827xo_set_analog_params(struct dvb_frontend *fe,
250 struct analog_parameters *params)
252 unsigned char tuner_reg[8];
253 unsigned char reg2[2];
254 u32 N;
255 int i;
256 struct tda827x_priv *priv = fe->tuner_priv;
257 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0 };
258 unsigned int freq = params->frequency;
260 tda827x_set_std(fe, params);
262 if (params->mode == V4L2_TUNER_RADIO)
263 freq = freq / 1000;
265 N = freq + priv->sgIF;
267 i = 0;
268 while (tda827x_table[i].lomax < N * 62500) {
269 if (tda827x_table[i + 1].lomax == 0)
270 break;
271 i++;
274 N = N << tda827x_table[i].spd;
276 tuner_reg[0] = 0;
277 tuner_reg[1] = (unsigned char)(N>>8);
278 tuner_reg[2] = (unsigned char) N;
279 tuner_reg[3] = 0x40;
280 tuner_reg[4] = 0x52 + (priv->lpsel << 5);
281 tuner_reg[5] = (tda827x_table[i].spd << 6) +
282 (tda827x_table[i].div1p5 << 5) +
283 (tda827x_table[i].bs << 3) + tda827x_table[i].bp;
284 tuner_reg[6] = 0x8f + (tda827x_table[i].gc3 << 4);
285 tuner_reg[7] = 0x8f;
287 msg.buf = tuner_reg;
288 msg.len = 8;
289 tuner_transfer(fe, &msg, 1);
291 msg.buf = reg2;
292 msg.len = 2;
293 reg2[0] = 0x80;
294 reg2[1] = 0;
295 tuner_transfer(fe, &msg, 1);
297 reg2[0] = 0x60;
298 reg2[1] = 0xbf;
299 tuner_transfer(fe, &msg, 1);
301 reg2[0] = 0x30;
302 reg2[1] = tuner_reg[4] + 0x80;
303 tuner_transfer(fe, &msg, 1);
305 msleep(1);
306 reg2[0] = 0x30;
307 reg2[1] = tuner_reg[4] + 4;
308 tuner_transfer(fe, &msg, 1);
310 msleep(1);
311 reg2[0] = 0x30;
312 reg2[1] = tuner_reg[4];
313 tuner_transfer(fe, &msg, 1);
315 msleep(550);
316 reg2[0] = 0x30;
317 reg2[1] = (tuner_reg[4] & 0xfc) + tda827x_table[i].cp;
318 tuner_transfer(fe, &msg, 1);
320 reg2[0] = 0x60;
321 reg2[1] = 0x3f;
322 tuner_transfer(fe, &msg, 1);
324 reg2[0] = 0x80;
325 reg2[1] = 0x08; /* Vsync en */
326 tuner_transfer(fe, &msg, 1);
328 priv->frequency = params->frequency;
330 return 0;
333 static void tda827xo_agcf(struct dvb_frontend *fe)
335 struct tda827x_priv *priv = fe->tuner_priv;
336 unsigned char data[] = { 0x80, 0x0c };
337 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
338 .buf = data, .len = 2};
340 tuner_transfer(fe, &msg, 1);
343 /* ------------------------------------------------------------------ */
345 struct tda827xa_data {
346 u32 lomax;
347 u8 svco;
348 u8 spd;
349 u8 scr;
350 u8 sbs;
351 u8 gc3;
354 static struct tda827xa_data tda827xa_dvbt[] = {
355 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 1},
356 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
357 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
358 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 1},
359 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
360 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
361 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
362 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
363 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
364 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
365 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
366 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
367 { .lomax = 290000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
368 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
369 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
370 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 1},
371 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
372 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
373 { .lomax = 550000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
374 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
375 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
376 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
377 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
378 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
379 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
380 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
381 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
384 static struct tda827xa_data tda827xa_dvbc[] = {
385 { .lomax = 50125000, .svco = 2, .spd = 4, .scr = 2, .sbs = 0, .gc3 = 3},
386 { .lomax = 58500000, .svco = 3, .spd = 4, .scr = 2, .sbs = 0, .gc3 = 3},
387 { .lomax = 69250000, .svco = 0, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
388 { .lomax = 83625000, .svco = 1, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
389 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 2, .sbs = 0, .gc3 = 3},
390 { .lomax = 100250000, .svco = 2, .spd = 3, .scr = 2, .sbs = 1, .gc3 = 1},
391 { .lomax = 117000000, .svco = 3, .spd = 3, .scr = 2, .sbs = 1, .gc3 = 1},
392 { .lomax = 138500000, .svco = 0, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
393 { .lomax = 167250000, .svco = 1, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
394 { .lomax = 187000000, .svco = 2, .spd = 2, .scr = 2, .sbs = 1, .gc3 = 1},
395 { .lomax = 200500000, .svco = 2, .spd = 2, .scr = 2, .sbs = 2, .gc3 = 1},
396 { .lomax = 234000000, .svco = 3, .spd = 2, .scr = 2, .sbs = 2, .gc3 = 3},
397 { .lomax = 277000000, .svco = 0, .spd = 1, .scr = 2, .sbs = 2, .gc3 = 3},
398 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 2, .sbs = 2, .gc3 = 1},
399 { .lomax = 334500000, .svco = 1, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 3},
400 { .lomax = 401000000, .svco = 2, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 3},
401 { .lomax = 468000000, .svco = 3, .spd = 1, .scr = 2, .sbs = 3, .gc3 = 1},
402 { .lomax = 535000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
403 { .lomax = 554000000, .svco = 0, .spd = 0, .scr = 2, .sbs = 3, .gc3 = 1},
404 { .lomax = 638000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
405 { .lomax = 669000000, .svco = 1, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
406 { .lomax = 720000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
407 { .lomax = 802000000, .svco = 2, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
408 { .lomax = 835000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
409 { .lomax = 885000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 1},
410 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 1},
411 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
414 static struct tda827xa_data tda827xa_analog[] = {
415 { .lomax = 56875000, .svco = 3, .spd = 4, .scr = 0, .sbs = 0, .gc3 = 3},
416 { .lomax = 67250000, .svco = 0, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
417 { .lomax = 81250000, .svco = 1, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
418 { .lomax = 97500000, .svco = 2, .spd = 3, .scr = 0, .sbs = 0, .gc3 = 3},
419 { .lomax = 113750000, .svco = 3, .spd = 3, .scr = 0, .sbs = 1, .gc3 = 1},
420 { .lomax = 134500000, .svco = 0, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
421 { .lomax = 154000000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
422 { .lomax = 162500000, .svco = 1, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
423 { .lomax = 183000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 1, .gc3 = 1},
424 { .lomax = 195000000, .svco = 2, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 1},
425 { .lomax = 227500000, .svco = 3, .spd = 2, .scr = 0, .sbs = 2, .gc3 = 3},
426 { .lomax = 269000000, .svco = 0, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 3},
427 { .lomax = 325000000, .svco = 1, .spd = 1, .scr = 0, .sbs = 2, .gc3 = 1},
428 { .lomax = 390000000, .svco = 2, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
429 { .lomax = 455000000, .svco = 3, .spd = 1, .scr = 0, .sbs = 3, .gc3 = 3},
430 { .lomax = 520000000, .svco = 0, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
431 { .lomax = 538000000, .svco = 0, .spd = 0, .scr = 1, .sbs = 3, .gc3 = 1},
432 { .lomax = 554000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 3, .gc3 = 1},
433 { .lomax = 620000000, .svco = 1, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
434 { .lomax = 650000000, .svco = 1, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
435 { .lomax = 700000000, .svco = 2, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
436 { .lomax = 780000000, .svco = 2, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
437 { .lomax = 820000000, .svco = 3, .spd = 0, .scr = 0, .sbs = 4, .gc3 = 0},
438 { .lomax = 870000000, .svco = 3, .spd = 0, .scr = 1, .sbs = 4, .gc3 = 0},
439 { .lomax = 911000000, .svco = 3, .spd = 0, .scr = 2, .sbs = 4, .gc3 = 0},
440 { .lomax = 0, .svco = 0, .spd = 0, .scr = 0, .sbs = 0, .gc3 = 0}
443 static int tda827xa_sleep(struct dvb_frontend *fe)
445 struct tda827x_priv *priv = fe->tuner_priv;
446 static u8 buf[] = { 0x30, 0x90 };
447 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
448 .buf = buf, .len = sizeof(buf) };
450 dprintk("%s:\n", __func__);
452 tuner_transfer(fe, &msg, 1);
454 if (priv->cfg && priv->cfg->sleep)
455 priv->cfg->sleep(fe);
457 return 0;
460 static void tda827xa_lna_gain(struct dvb_frontend *fe, int high,
461 struct analog_parameters *params)
463 struct tda827x_priv *priv = fe->tuner_priv;
464 unsigned char buf[] = {0x22, 0x01};
465 int arg;
466 int gp_func;
467 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = sizeof(buf) };
469 if (NULL == priv->cfg) {
470 dprintk("tda827x_config not defined, cannot set LNA gain!\n");
471 return;
473 msg.addr = priv->cfg->switch_addr;
474 if (priv->cfg->config) {
475 if (high)
476 dprintk("setting LNA to high gain\n");
477 else
478 dprintk("setting LNA to low gain\n");
480 switch (priv->cfg->config) {
481 case 0: /* no LNA */
482 break;
483 case 1: /* switch is GPIO 0 of tda8290 */
484 case 2:
485 if (params == NULL) {
486 gp_func = 0;
487 arg = 0;
488 } else {
489 /* turn Vsync on */
490 gp_func = 1;
491 if (params->std & V4L2_STD_MN)
492 arg = 1;
493 else
494 arg = 0;
496 if (fe->callback)
497 fe->callback(priv->i2c_adap->algo_data,
498 DVB_FRONTEND_COMPONENT_TUNER,
499 gp_func, arg);
500 buf[1] = high ? 0 : 1;
501 if (priv->cfg->config == 2)
502 buf[1] = high ? 1 : 0;
503 tuner_transfer(fe, &msg, 1);
504 break;
505 case 3: /* switch with GPIO of saa713x */
506 if (fe->callback)
507 fe->callback(priv->i2c_adap->algo_data,
508 DVB_FRONTEND_COMPONENT_TUNER, 0, high);
509 break;
513 static int tda827xa_set_params(struct dvb_frontend *fe,
514 struct dvb_frontend_parameters *params)
516 struct tda827x_priv *priv = fe->tuner_priv;
517 struct tda827xa_data *frequency_map = tda827xa_dvbt;
518 u8 buf[11];
520 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
521 .buf = buf, .len = sizeof(buf) };
523 int i, tuner_freq, if_freq, rc;
524 u32 N;
526 dprintk("%s:\n", __func__);
528 tda827xa_lna_gain(fe, 1, NULL);
529 msleep(20);
531 switch (params->u.ofdm.bandwidth) {
532 case BANDWIDTH_6_MHZ:
533 if_freq = 4000000;
534 break;
535 case BANDWIDTH_7_MHZ:
536 if_freq = 4500000;
537 break;
538 default: /* 8 MHz or Auto */
539 if_freq = 5000000;
540 break;
542 tuner_freq = params->frequency + if_freq;
544 if (fe->ops.info.type == FE_QAM) {
545 dprintk("%s select tda827xa_dvbc\n", __func__);
546 frequency_map = tda827xa_dvbc;
549 i = 0;
550 while (frequency_map[i].lomax < tuner_freq) {
551 if (frequency_map[i + 1].lomax == 0)
552 break;
553 i++;
556 N = ((tuner_freq + 31250) / 62500) << frequency_map[i].spd;
557 buf[0] = 0; // subaddress
558 buf[1] = N >> 8;
559 buf[2] = N & 0xff;
560 buf[3] = 0;
561 buf[4] = 0x16;
562 buf[5] = (frequency_map[i].spd << 5) + (frequency_map[i].svco << 3) +
563 frequency_map[i].sbs;
564 buf[6] = 0x4b + (frequency_map[i].gc3 << 4);
565 buf[7] = 0x1c;
566 buf[8] = 0x06;
567 buf[9] = 0x24;
568 buf[10] = 0x00;
569 msg.len = 11;
570 rc = tuner_transfer(fe, &msg, 1);
571 if (rc < 0)
572 goto err;
574 buf[0] = 0x90;
575 buf[1] = 0xff;
576 buf[2] = 0x60;
577 buf[3] = 0x00;
578 buf[4] = 0x59; // lpsel, for 6MHz + 2
579 msg.len = 5;
580 rc = tuner_transfer(fe, &msg, 1);
581 if (rc < 0)
582 goto err;
584 buf[0] = 0xa0;
585 buf[1] = 0x40;
586 msg.len = 2;
587 rc = tuner_transfer(fe, &msg, 1);
588 if (rc < 0)
589 goto err;
591 msleep(11);
592 msg.flags = I2C_M_RD;
593 rc = tuner_transfer(fe, &msg, 1);
594 if (rc < 0)
595 goto err;
596 msg.flags = 0;
598 buf[1] >>= 4;
599 dprintk("tda8275a AGC2 gain is: %d\n", buf[1]);
600 if ((buf[1]) < 2) {
601 tda827xa_lna_gain(fe, 0, NULL);
602 buf[0] = 0x60;
603 buf[1] = 0x0c;
604 rc = tuner_transfer(fe, &msg, 1);
605 if (rc < 0)
606 goto err;
609 buf[0] = 0xc0;
610 buf[1] = 0x99; // lpsel, for 6MHz + 2
611 rc = tuner_transfer(fe, &msg, 1);
612 if (rc < 0)
613 goto err;
615 buf[0] = 0x60;
616 buf[1] = 0x3c;
617 rc = tuner_transfer(fe, &msg, 1);
618 if (rc < 0)
619 goto err;
621 /* correct CP value */
622 buf[0] = 0x30;
623 buf[1] = 0x10 + frequency_map[i].scr;
624 rc = tuner_transfer(fe, &msg, 1);
625 if (rc < 0)
626 goto err;
628 msleep(163);
629 buf[0] = 0xc0;
630 buf[1] = 0x39; // lpsel, for 6MHz + 2
631 rc = tuner_transfer(fe, &msg, 1);
632 if (rc < 0)
633 goto err;
635 msleep(3);
636 /* freeze AGC1 */
637 buf[0] = 0x50;
638 buf[1] = 0x4f + (frequency_map[i].gc3 << 4);
639 rc = tuner_transfer(fe, &msg, 1);
640 if (rc < 0)
641 goto err;
643 priv->frequency = params->frequency;
644 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
647 return 0;
649 err:
650 printk(KERN_ERR "%s: could not write to tuner at addr: 0x%02x\n",
651 __func__, priv->i2c_addr << 1);
652 return rc;
656 static int tda827xa_set_analog_params(struct dvb_frontend *fe,
657 struct analog_parameters *params)
659 unsigned char tuner_reg[11];
660 u32 N;
661 int i;
662 struct tda827x_priv *priv = fe->tuner_priv;
663 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
664 .buf = tuner_reg, .len = sizeof(tuner_reg) };
665 unsigned int freq = params->frequency;
667 tda827x_set_std(fe, params);
669 tda827xa_lna_gain(fe, 1, params);
670 msleep(10);
672 if (params->mode == V4L2_TUNER_RADIO)
673 freq = freq / 1000;
675 N = freq + priv->sgIF;
677 i = 0;
678 while (tda827xa_analog[i].lomax < N * 62500) {
679 if (tda827xa_analog[i + 1].lomax == 0)
680 break;
681 i++;
684 N = N << tda827xa_analog[i].spd;
686 tuner_reg[0] = 0;
687 tuner_reg[1] = (unsigned char)(N>>8);
688 tuner_reg[2] = (unsigned char) N;
689 tuner_reg[3] = 0;
690 tuner_reg[4] = 0x16;
691 tuner_reg[5] = (tda827xa_analog[i].spd << 5) +
692 (tda827xa_analog[i].svco << 3) +
693 tda827xa_analog[i].sbs;
694 tuner_reg[6] = 0x8b + (tda827xa_analog[i].gc3 << 4);
695 tuner_reg[7] = 0x1c;
696 tuner_reg[8] = 4;
697 tuner_reg[9] = 0x20;
698 tuner_reg[10] = 0x00;
699 msg.len = 11;
700 tuner_transfer(fe, &msg, 1);
702 tuner_reg[0] = 0x90;
703 tuner_reg[1] = 0xff;
704 tuner_reg[2] = 0xe0;
705 tuner_reg[3] = 0;
706 tuner_reg[4] = 0x99 + (priv->lpsel << 1);
707 msg.len = 5;
708 tuner_transfer(fe, &msg, 1);
710 tuner_reg[0] = 0xa0;
711 tuner_reg[1] = 0xc0;
712 msg.len = 2;
713 tuner_transfer(fe, &msg, 1);
715 tuner_reg[0] = 0x30;
716 tuner_reg[1] = 0x10 + tda827xa_analog[i].scr;
717 tuner_transfer(fe, &msg, 1);
719 msg.flags = I2C_M_RD;
720 tuner_transfer(fe, &msg, 1);
721 msg.flags = 0;
722 tuner_reg[1] >>= 4;
723 dprintk("AGC2 gain is: %d\n", tuner_reg[1]);
724 if (tuner_reg[1] < 1)
725 tda827xa_lna_gain(fe, 0, params);
727 msleep(100);
728 tuner_reg[0] = 0x60;
729 tuner_reg[1] = 0x3c;
730 tuner_transfer(fe, &msg, 1);
732 msleep(163);
733 tuner_reg[0] = 0x50;
734 tuner_reg[1] = 0x8f + (tda827xa_analog[i].gc3 << 4);
735 tuner_transfer(fe, &msg, 1);
737 tuner_reg[0] = 0x80;
738 tuner_reg[1] = 0x28;
739 tuner_transfer(fe, &msg, 1);
741 tuner_reg[0] = 0xb0;
742 tuner_reg[1] = 0x01;
743 tuner_transfer(fe, &msg, 1);
745 tuner_reg[0] = 0xc0;
746 tuner_reg[1] = 0x19 + (priv->lpsel << 1);
747 tuner_transfer(fe, &msg, 1);
749 priv->frequency = params->frequency;
751 return 0;
754 static void tda827xa_agcf(struct dvb_frontend *fe)
756 struct tda827x_priv *priv = fe->tuner_priv;
757 unsigned char data[] = {0x80, 0x2c};
758 struct i2c_msg msg = {.addr = priv->i2c_addr, .flags = 0,
759 .buf = data, .len = 2};
760 tuner_transfer(fe, &msg, 1);
763 /* ------------------------------------------------------------------ */
765 static int tda827x_release(struct dvb_frontend *fe)
767 kfree(fe->tuner_priv);
768 fe->tuner_priv = NULL;
769 return 0;
772 static int tda827x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
774 struct tda827x_priv *priv = fe->tuner_priv;
775 *frequency = priv->frequency;
776 return 0;
779 static int tda827x_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
781 struct tda827x_priv *priv = fe->tuner_priv;
782 *bandwidth = priv->bandwidth;
783 return 0;
786 static int tda827x_init(struct dvb_frontend *fe)
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);
793 return 0;
796 static int tda827x_probe_version(struct dvb_frontend *fe);
798 static int tda827x_initial_init(struct dvb_frontend *fe)
800 int ret;
801 ret = tda827x_probe_version(fe);
802 if (ret)
803 return ret;
804 return fe->ops.tuner_ops.init(fe);
807 static int tda827x_initial_sleep(struct dvb_frontend *fe)
809 int ret;
810 ret = tda827x_probe_version(fe);
811 if (ret)
812 return ret;
813 return fe->ops.tuner_ops.sleep(fe);
816 static struct dvb_tuner_ops tda827xo_tuner_ops = {
817 .info = {
818 .name = "Philips TDA827X",
819 .frequency_min = 55000000,
820 .frequency_max = 860000000,
821 .frequency_step = 250000
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,
832 static struct dvb_tuner_ops tda827xa_tuner_ops = {
833 .info = {
834 .name = "Philips TDA827XA",
835 .frequency_min = 44000000,
836 .frequency_max = 906000000,
837 .frequency_step = 62500
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,
848 static int tda827x_probe_version(struct dvb_frontend *fe)
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 };
856 rc = tuner_transfer(fe, &msg, 1);
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;
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;
875 return 0;
878 struct dvb_frontend *tda827x_attach(struct dvb_frontend *fe, int addr,
879 struct i2c_adapter *i2c,
880 struct tda827x_config *cfg)
882 struct tda827x_priv *priv = NULL;
884 dprintk("%s:\n", __func__);
885 priv = kzalloc(sizeof(struct tda827x_priv), GFP_KERNEL);
886 if (priv == NULL)
887 return NULL;
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;
895 dprintk("type set to %s\n", fe->ops.tuner_ops.info.name);
897 return fe;
899 EXPORT_SYMBOL_GPL(tda827x_attach);
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");
907 * Overrides for Emacs so that we follow Linus's tabbing style.
908 * ---------------------------------------------------------------------------
909 * Local variables:
910 * c-basic-offset: 8
911 * End: