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 <linux/dvb/frontend.h>
23 #include <asm/types.h>
28 #define dprintk(args...) \
30 if (debug) printk(KERN_DEBUG "tda827x: " args); \
35 struct i2c_adapter
*i2c_adap
;
36 struct tda827x_config
*cfg
;
51 static const struct tda827x_data tda827x_dvbt
[] = {
52 { .lomax
= 62000000, .spd
= 3, .bs
= 2, .bp
= 0, .cp
= 0, .gc3
= 3, .div1p5
= 1},
53 { .lomax
= 66000000, .spd
= 3, .bs
= 3, .bp
= 0, .cp
= 0, .gc3
= 3, .div1p5
= 1},
54 { .lomax
= 76000000, .spd
= 3, .bs
= 1, .bp
= 0, .cp
= 0, .gc3
= 3, .div1p5
= 0},
55 { .lomax
= 84000000, .spd
= 3, .bs
= 2, .bp
= 0, .cp
= 0, .gc3
= 3, .div1p5
= 0},
56 { .lomax
= 93000000, .spd
= 3, .bs
= 2, .bp
= 0, .cp
= 0, .gc3
= 1, .div1p5
= 0},
57 { .lomax
= 98000000, .spd
= 3, .bs
= 3, .bp
= 0, .cp
= 0, .gc3
= 1, .div1p5
= 0},
58 { .lomax
= 109000000, .spd
= 3, .bs
= 3, .bp
= 1, .cp
= 0, .gc3
= 1, .div1p5
= 0},
59 { .lomax
= 123000000, .spd
= 2, .bs
= 2, .bp
= 1, .cp
= 0, .gc3
= 1, .div1p5
= 1},
60 { .lomax
= 133000000, .spd
= 2, .bs
= 3, .bp
= 1, .cp
= 0, .gc3
= 1, .div1p5
= 1},
61 { .lomax
= 151000000, .spd
= 2, .bs
= 1, .bp
= 1, .cp
= 0, .gc3
= 1, .div1p5
= 0},
62 { .lomax
= 154000000, .spd
= 2, .bs
= 2, .bp
= 1, .cp
= 0, .gc3
= 1, .div1p5
= 0},
63 { .lomax
= 181000000, .spd
= 2, .bs
= 2, .bp
= 1, .cp
= 0, .gc3
= 0, .div1p5
= 0},
64 { .lomax
= 185000000, .spd
= 2, .bs
= 2, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 0},
65 { .lomax
= 217000000, .spd
= 2, .bs
= 3, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 0},
66 { .lomax
= 244000000, .spd
= 1, .bs
= 2, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 1},
67 { .lomax
= 265000000, .spd
= 1, .bs
= 3, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 1},
68 { .lomax
= 302000000, .spd
= 1, .bs
= 1, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 0},
69 { .lomax
= 324000000, .spd
= 1, .bs
= 2, .bp
= 2, .cp
= 0, .gc3
= 1, .div1p5
= 0},
70 { .lomax
= 370000000, .spd
= 1, .bs
= 2, .bp
= 3, .cp
= 0, .gc3
= 1, .div1p5
= 0},
71 { .lomax
= 454000000, .spd
= 1, .bs
= 3, .bp
= 3, .cp
= 0, .gc3
= 1, .div1p5
= 0},
72 { .lomax
= 493000000, .spd
= 0, .bs
= 2, .bp
= 3, .cp
= 0, .gc3
= 1, .div1p5
= 1},
73 { .lomax
= 530000000, .spd
= 0, .bs
= 3, .bp
= 3, .cp
= 0, .gc3
= 1, .div1p5
= 1},
74 { .lomax
= 554000000, .spd
= 0, .bs
= 1, .bp
= 3, .cp
= 0, .gc3
= 1, .div1p5
= 0},
75 { .lomax
= 604000000, .spd
= 0, .bs
= 1, .bp
= 4, .cp
= 0, .gc3
= 0, .div1p5
= 0},
76 { .lomax
= 696000000, .spd
= 0, .bs
= 2, .bp
= 4, .cp
= 0, .gc3
= 0, .div1p5
= 0},
77 { .lomax
= 740000000, .spd
= 0, .bs
= 2, .bp
= 4, .cp
= 1, .gc3
= 0, .div1p5
= 0},
78 { .lomax
= 820000000, .spd
= 0, .bs
= 3, .bp
= 4, .cp
= 0, .gc3
= 0, .div1p5
= 0},
79 { .lomax
= 865000000, .spd
= 0, .bs
= 3, .bp
= 4, .cp
= 1, .gc3
= 0, .div1p5
= 0},
80 { .lomax
= 0, .spd
= 0, .bs
= 0, .bp
= 0, .cp
= 0, .gc3
= 0, .div1p5
= 0}
83 static int tda827xo_set_params(struct dvb_frontend
*fe
,
84 struct dvb_frontend_parameters
*params
)
86 struct tda827x_priv
*priv
= fe
->tuner_priv
;
89 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= 0,
90 .buf
= buf
, .len
= sizeof(buf
) };
91 int i
, tuner_freq
, if_freq
;
94 dprintk("%s:\n", __FUNCTION__
);
95 switch (params
->u
.ofdm
.bandwidth
) {
102 default: /* 8 MHz or Auto */
106 tuner_freq
= params
->frequency
+ if_freq
;
109 while (tda827x_dvbt
[i
].lomax
< tuner_freq
) {
110 if(tda827x_dvbt
[i
+ 1].lomax
== 0)
115 N
= ((tuner_freq
+ 125000) / 250000) << (tda827x_dvbt
[i
].spd
+ 2);
117 buf
[1] = (N
>>8) | 0x40;
121 buf
[5] = (tda827x_dvbt
[i
].spd
<< 6) + (tda827x_dvbt
[i
].div1p5
<< 5) +
122 (tda827x_dvbt
[i
].bs
<< 3) + tda827x_dvbt
[i
].bp
;
123 buf
[6] = (tda827x_dvbt
[i
].gc3
<< 4) + 0x8f;
133 if (fe
->ops
.i2c_gate_ctrl
)
134 fe
->ops
.i2c_gate_ctrl(fe
, 1);
135 if (i2c_transfer(priv
->i2c_adap
, &msg
, 1) != 1) {
136 printk("%s: could not write to tuner at addr: 0x%02x\n",
137 __FUNCTION__
, priv
->i2c_addr
<< 1);
141 /* correct CP value */
143 buf
[1] = 0x50 + tda827x_dvbt
[i
].cp
;
146 if (fe
->ops
.i2c_gate_ctrl
)
147 fe
->ops
.i2c_gate_ctrl(fe
, 1);
148 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
150 priv
->frequency
= tuner_freq
- if_freq
; // FIXME
151 priv
->bandwidth
= (fe
->ops
.info
.type
== FE_OFDM
) ? params
->u
.ofdm
.bandwidth
: 0;
156 static int tda827xo_sleep(struct dvb_frontend
*fe
)
158 struct tda827x_priv
*priv
= fe
->tuner_priv
;
159 static u8 buf
[] = { 0x30, 0xd0 };
160 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= 0,
161 .buf
= buf
, .len
= sizeof(buf
) };
163 dprintk("%s:\n", __FUNCTION__
);
164 if (fe
->ops
.i2c_gate_ctrl
)
165 fe
->ops
.i2c_gate_ctrl(fe
, 1);
166 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
168 if (priv
->cfg
&& priv
->cfg
->sleep
)
169 priv
->cfg
->sleep(fe
);
174 /* ------------------------------------------------------------------ */
176 struct tda827xa_data
{
185 static const struct tda827xa_data tda827xa_dvbt
[] = {
186 { .lomax
= 56875000, .svco
= 3, .spd
= 4, .scr
= 0, .sbs
= 0, .gc3
= 1},
187 { .lomax
= 67250000, .svco
= 0, .spd
= 3, .scr
= 0, .sbs
= 0, .gc3
= 1},
188 { .lomax
= 81250000, .svco
= 1, .spd
= 3, .scr
= 0, .sbs
= 0, .gc3
= 1},
189 { .lomax
= 97500000, .svco
= 2, .spd
= 3, .scr
= 0, .sbs
= 0, .gc3
= 1},
190 { .lomax
= 113750000, .svco
= 3, .spd
= 3, .scr
= 0, .sbs
= 1, .gc3
= 1},
191 { .lomax
= 134500000, .svco
= 0, .spd
= 2, .scr
= 0, .sbs
= 1, .gc3
= 1},
192 { .lomax
= 154000000, .svco
= 1, .spd
= 2, .scr
= 0, .sbs
= 1, .gc3
= 1},
193 { .lomax
= 162500000, .svco
= 1, .spd
= 2, .scr
= 0, .sbs
= 1, .gc3
= 1},
194 { .lomax
= 183000000, .svco
= 2, .spd
= 2, .scr
= 0, .sbs
= 1, .gc3
= 1},
195 { .lomax
= 195000000, .svco
= 2, .spd
= 2, .scr
= 0, .sbs
= 2, .gc3
= 1},
196 { .lomax
= 227500000, .svco
= 3, .spd
= 2, .scr
= 0, .sbs
= 2, .gc3
= 1},
197 { .lomax
= 269000000, .svco
= 0, .spd
= 1, .scr
= 0, .sbs
= 2, .gc3
= 1},
198 { .lomax
= 290000000, .svco
= 1, .spd
= 1, .scr
= 0, .sbs
= 2, .gc3
= 1},
199 { .lomax
= 325000000, .svco
= 1, .spd
= 1, .scr
= 0, .sbs
= 3, .gc3
= 1},
200 { .lomax
= 390000000, .svco
= 2, .spd
= 1, .scr
= 0, .sbs
= 3, .gc3
= 1},
201 { .lomax
= 455000000, .svco
= 3, .spd
= 1, .scr
= 0, .sbs
= 3, .gc3
= 1},
202 { .lomax
= 520000000, .svco
= 0, .spd
= 0, .scr
= 0, .sbs
= 3, .gc3
= 1},
203 { .lomax
= 538000000, .svco
= 0, .spd
= 0, .scr
= 1, .sbs
= 3, .gc3
= 1},
204 { .lomax
= 550000000, .svco
= 1, .spd
= 0, .scr
= 0, .sbs
= 3, .gc3
= 1},
205 { .lomax
= 620000000, .svco
= 1, .spd
= 0, .scr
= 0, .sbs
= 4, .gc3
= 0},
206 { .lomax
= 650000000, .svco
= 1, .spd
= 0, .scr
= 1, .sbs
= 4, .gc3
= 0},
207 { .lomax
= 700000000, .svco
= 2, .spd
= 0, .scr
= 0, .sbs
= 4, .gc3
= 0},
208 { .lomax
= 780000000, .svco
= 2, .spd
= 0, .scr
= 1, .sbs
= 4, .gc3
= 0},
209 { .lomax
= 820000000, .svco
= 3, .spd
= 0, .scr
= 0, .sbs
= 4, .gc3
= 0},
210 { .lomax
= 870000000, .svco
= 3, .spd
= 0, .scr
= 1, .sbs
= 4, .gc3
= 0},
211 { .lomax
= 911000000, .svco
= 3, .spd
= 0, .scr
= 2, .sbs
= 4, .gc3
= 0},
212 { .lomax
= 0, .svco
= 0, .spd
= 0, .scr
= 0, .sbs
= 0, .gc3
= 0}
215 static int tda827xa_set_params(struct dvb_frontend
*fe
,
216 struct dvb_frontend_parameters
*params
)
218 struct tda827x_priv
*priv
= fe
->tuner_priv
;
221 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= 0,
222 .buf
= buf
, .len
= sizeof(buf
) };
224 int i
, tuner_freq
, if_freq
;
227 dprintk("%s:\n", __FUNCTION__
);
228 if (priv
->cfg
&& priv
->cfg
->lna_gain
)
229 priv
->cfg
->lna_gain(fe
, 1);
232 switch (params
->u
.ofdm
.bandwidth
) {
233 case BANDWIDTH_6_MHZ
:
236 case BANDWIDTH_7_MHZ
:
239 default: /* 8 MHz or Auto */
243 tuner_freq
= params
->frequency
+ if_freq
;
246 while (tda827xa_dvbt
[i
].lomax
< tuner_freq
) {
247 if(tda827xa_dvbt
[i
+ 1].lomax
== 0)
252 N
= ((tuner_freq
+ 31250) / 62500) << tda827xa_dvbt
[i
].spd
;
253 buf
[0] = 0; // subaddress
258 buf
[5] = (tda827xa_dvbt
[i
].spd
<< 5) + (tda827xa_dvbt
[i
].svco
<< 3) +
259 tda827xa_dvbt
[i
].sbs
;
260 buf
[6] = 0x4b + (tda827xa_dvbt
[i
].gc3
<< 4);
266 if (fe
->ops
.i2c_gate_ctrl
)
267 fe
->ops
.i2c_gate_ctrl(fe
, 1);
268 if (i2c_transfer(priv
->i2c_adap
, &msg
, 1) != 1) {
269 printk("%s: could not write to tuner at addr: 0x%02x\n",
270 __FUNCTION__
, priv
->i2c_addr
<< 1);
277 buf
[4] = 0x59; // lpsel, for 6MHz + 2
279 if (fe
->ops
.i2c_gate_ctrl
)
280 fe
->ops
.i2c_gate_ctrl(fe
, 1);
281 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
286 if (fe
->ops
.i2c_gate_ctrl
)
287 fe
->ops
.i2c_gate_ctrl(fe
, 1);
288 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
291 msg
.flags
= I2C_M_RD
;
292 if (fe
->ops
.i2c_gate_ctrl
)
293 fe
->ops
.i2c_gate_ctrl(fe
, 1);
294 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
298 dprintk("tda8275a AGC2 gain is: %d\n", buf
[1]);
300 if (priv
->cfg
&& priv
->cfg
->lna_gain
)
301 priv
->cfg
->lna_gain(fe
, 0);
304 if (fe
->ops
.i2c_gate_ctrl
)
305 fe
->ops
.i2c_gate_ctrl(fe
, 1);
306 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
310 buf
[1] = 0x99; // lpsel, for 6MHz + 2
311 if (fe
->ops
.i2c_gate_ctrl
)
312 fe
->ops
.i2c_gate_ctrl(fe
, 1);
313 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
317 if (fe
->ops
.i2c_gate_ctrl
)
318 fe
->ops
.i2c_gate_ctrl(fe
, 1);
319 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
321 /* correct CP value */
323 buf
[1] = 0x10 + tda827xa_dvbt
[i
].scr
;
324 if (fe
->ops
.i2c_gate_ctrl
)
325 fe
->ops
.i2c_gate_ctrl(fe
, 1);
326 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
330 buf
[1] = 0x39; // lpsel, for 6MHz + 2
331 if (fe
->ops
.i2c_gate_ctrl
)
332 fe
->ops
.i2c_gate_ctrl(fe
, 1);
333 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
338 buf
[1] = 0x4f + (tda827xa_dvbt
[i
].gc3
<< 4);
339 if (fe
->ops
.i2c_gate_ctrl
)
340 fe
->ops
.i2c_gate_ctrl(fe
, 1);
341 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
343 priv
->frequency
= tuner_freq
- if_freq
; // FIXME
344 priv
->bandwidth
= (fe
->ops
.info
.type
== FE_OFDM
) ? params
->u
.ofdm
.bandwidth
: 0;
349 static int tda827xa_sleep(struct dvb_frontend
*fe
)
351 struct tda827x_priv
*priv
= fe
->tuner_priv
;
352 static u8 buf
[] = { 0x30, 0x90 };
353 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= 0,
354 .buf
= buf
, .len
= sizeof(buf
) };
356 dprintk("%s:\n", __FUNCTION__
);
357 if (fe
->ops
.i2c_gate_ctrl
)
358 fe
->ops
.i2c_gate_ctrl(fe
, 1);
360 i2c_transfer(priv
->i2c_adap
, &msg
, 1);
362 if (fe
->ops
.i2c_gate_ctrl
)
363 fe
->ops
.i2c_gate_ctrl(fe
, 0);
365 if (priv
->cfg
&& priv
->cfg
->sleep
)
366 priv
->cfg
->sleep(fe
);
371 static int tda827x_release(struct dvb_frontend
*fe
)
373 kfree(fe
->tuner_priv
);
374 fe
->tuner_priv
= NULL
;
378 static int tda827x_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
380 struct tda827x_priv
*priv
= fe
->tuner_priv
;
381 *frequency
= priv
->frequency
;
385 static int tda827x_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
387 struct tda827x_priv
*priv
= fe
->tuner_priv
;
388 *bandwidth
= priv
->bandwidth
;
392 static int tda827x_init(struct dvb_frontend
*fe
)
394 struct tda827x_priv
*priv
= fe
->tuner_priv
;
395 dprintk("%s:\n", __FUNCTION__
);
396 if (priv
->cfg
&& priv
->cfg
->init
)
402 static int tda827x_probe_version(struct dvb_frontend
*fe
);
404 static int tda827x_initial_init(struct dvb_frontend
*fe
)
407 ret
= tda827x_probe_version(fe
);
410 return fe
->ops
.tuner_ops
.init(fe
);
413 static int tda827x_initial_sleep(struct dvb_frontend
*fe
)
416 ret
= tda827x_probe_version(fe
);
419 return fe
->ops
.tuner_ops
.sleep(fe
);
422 static struct dvb_tuner_ops tda827xo_tuner_ops
= {
424 .name
= "Philips TDA827X",
425 .frequency_min
= 55000000,
426 .frequency_max
= 860000000,
427 .frequency_step
= 250000
429 .release
= tda827x_release
,
430 .init
= tda827x_initial_init
,
431 .sleep
= tda827x_initial_sleep
,
432 .set_params
= tda827xo_set_params
,
433 .get_frequency
= tda827x_get_frequency
,
434 .get_bandwidth
= tda827x_get_bandwidth
,
437 static struct dvb_tuner_ops tda827xa_tuner_ops
= {
439 .name
= "Philips TDA827XA",
440 .frequency_min
= 44000000,
441 .frequency_max
= 906000000,
442 .frequency_step
= 62500
444 .release
= tda827x_release
,
445 .init
= tda827x_init
,
446 .sleep
= tda827xa_sleep
,
447 .set_params
= tda827xa_set_params
,
448 .get_frequency
= tda827x_get_frequency
,
449 .get_bandwidth
= tda827x_get_bandwidth
,
452 static int tda827x_probe_version(struct dvb_frontend
*fe
)
454 struct tda827x_priv
*priv
= fe
->tuner_priv
;
455 struct i2c_msg msg
= { .addr
= priv
->i2c_addr
, .flags
= I2C_M_RD
,
456 .buf
= &data
, .len
= 1 };
457 if (fe
->ops
.i2c_gate_ctrl
)
458 fe
->ops
.i2c_gate_ctrl(fe
, 1);
459 if (i2c_transfer(priv
->i2c_adap
, &msg
, 1) != 1) {
460 printk("%s: could not read from tuner at addr: 0x%02x\n",
461 __FUNCTION__
, msg
.addr
<< 1);
464 if ((data
& 0x3c) == 0) {
465 dprintk("tda827x tuner found\n");
466 fe
->ops
.tuner_ops
.init
= tda827x_init
;
467 fe
->ops
.tuner_ops
.sleep
= tda827xo_sleep
;
469 dprintk("tda827xa tuner found\n");
470 memcpy(&fe
->ops
.tuner_ops
, &tda827xa_tuner_ops
, sizeof(struct dvb_tuner_ops
));
475 struct dvb_frontend
*tda827x_attach(struct dvb_frontend
*fe
, int addr
,
476 struct i2c_adapter
*i2c
,
477 struct tda827x_config
*cfg
)
479 struct tda827x_priv
*priv
= NULL
;
481 dprintk("%s:\n", __FUNCTION__
);
482 priv
= kzalloc(sizeof(struct tda827x_priv
), GFP_KERNEL
);
486 priv
->i2c_addr
= addr
;
487 priv
->i2c_adap
= i2c
;
489 memcpy(&fe
->ops
.tuner_ops
, &tda827xo_tuner_ops
, sizeof(struct dvb_tuner_ops
));
491 fe
->tuner_priv
= priv
;
496 EXPORT_SYMBOL(tda827x_attach
);
498 module_param(debug
, int, 0644);
499 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
501 MODULE_DESCRIPTION("DVB TDA827x driver");
502 MODULE_AUTHOR("Hartmut Hackmann <hartmut.hackmann@t-online.de>");
503 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
504 MODULE_LICENSE("GPL");
507 * Overrides for Emacs so that we follow Linus's tabbing style.
508 * ---------------------------------------------------------------------------