2 * $Id: mt20xx.c,v 1.4 2005/03/04 09:24:56 kraxel Exp $
4 * i2c tv tuner chip device driver
5 * controls microtune tuners, mt2032 + mt2050 at the moment.
7 #include <linux/delay.h>
9 #include <linux/videodev.h>
10 #include <linux/moduleparam.h>
11 #include <media/tuner.h>
13 /* ---------------------------------------------------------------------- */
15 static unsigned int optimize_vco
= 1;
16 module_param(optimize_vco
, int, 0644);
18 static unsigned int tv_antenna
= 1;
19 module_param(tv_antenna
, int, 0644);
21 static unsigned int radio_antenna
= 0;
22 module_param(radio_antenna
, int, 0644);
24 /* ---------------------------------------------------------------------- */
31 static char *microtune_part
[] = {
32 [ MT2030
] = "MT2030",
33 [ MT2032
] = "MT2032",
34 [ MT2040
] = "MT2040",
35 [ MT2050
] = "MT2050",
39 static int mt2032_spurcheck(struct i2c_client
*c
,
40 int f1
, int f2
, int spectrum_from
,int spectrum_to
)
42 struct tuner
*t
= i2c_get_clientdata(c
);
45 f1
=f1
/1000; //scale to kHz to avoid 32bit overflows
50 tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
51 f1
,f2
,spectrum_from
,spectrum_to
);
59 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1
,n2
,f
);
61 if( (f
>spectrum_from
) && (f
<spectrum_to
))
62 tuner_dbg("mt2032 spurcheck triggered: %d\n",n1
);
63 } while ( (f
>(f2
-spectrum_to
)) || (n2
>-5));
70 static int mt2032_compute_freq(struct i2c_client
*c
,
72 unsigned int if1
, unsigned int if2
,
73 unsigned int spectrum_from
,
74 unsigned int spectrum_to
,
77 unsigned int xogc
) //all in Hz
79 struct tuner
*t
= i2c_get_clientdata(c
);
80 unsigned int fref
,lo1
,lo1n
,lo1a
,s
,sel
,lo1freq
, desired_lo1
,
81 desired_lo2
,lo2
,lo2n
,lo2a
,lo2num
,lo2freq
;
83 fref
= 5250 *1000; //5.25MHz
86 lo1
=(2*(desired_lo1
/1000)+(fref
/1000)) / (2*fref
/1000);
90 s
=rfin
/1000/1000+1090;
94 else if(s
>1720) sel
=1;
95 else if(s
>1530) sel
=2;
96 else if(s
>1370) sel
=3;
100 if(s
>1790) sel
=0; // <1958
101 else if(s
>1617) sel
=1;
102 else if(s
>1449) sel
=2;
103 else if(s
>1291) sel
=3;
108 lo1freq
=(lo1a
+8*lo1n
)*fref
;
110 tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
111 rfin
,lo1
,lo1n
,lo1a
,sel
,lo1freq
);
113 desired_lo2
=lo1freq
-rfin
-if2
;
114 lo2
=(desired_lo2
)/fref
;
117 lo2num
=((desired_lo2
/1000)%(fref
/1000))* 3780/(fref
/1000); //scale to fit in 32bit arith
118 lo2freq
=(lo2a
+8*lo2n
)*fref
+ lo2num
*(fref
/1000)/3780*1000;
120 tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
121 rfin
,lo2
,lo2n
,lo2a
,lo2num
,lo2freq
);
123 if(lo1a
<0 || lo1a
>7 || lo1n
<17 ||lo1n
>48 || lo2a
<0 ||lo2a
>7 ||lo2n
<17 || lo2n
>30) {
124 tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
125 lo1a
, lo1n
, lo2a
,lo2n
);
129 mt2032_spurcheck(c
, lo1freq
, desired_lo2
, spectrum_from
, spectrum_to
);
130 // should recalculate lo1 (one step up/down)
132 // set up MT2032 register map for transfer over i2c
134 buf
[1]=lo1a
| (sel
<<4);
136 buf
[3]=0x0f; //reserved
138 buf
[5]=(lo2n
-1) | (lo2a
<<5);
139 if(rfin
>400*1000*1000)
142 buf
[6]=0xf4; // set PKEN per rev 1.2
144 buf
[8]=0xc3; //reserved
145 buf
[9]=0x4e; //reserved
146 buf
[10]=0xec; //reserved
147 buf
[11]=(lo2num
&0xff);
148 buf
[12]=(lo2num
>>8) |0x80; // Lo2RST
153 static int mt2032_check_lo_lock(struct i2c_client
*c
)
155 struct tuner
*t
= i2c_get_clientdata(c
);
157 unsigned char buf
[2];
159 for(try=0;try<10;try++) {
161 i2c_master_send(c
,buf
,1);
162 i2c_master_recv(c
,buf
,1);
163 tuner_dbg("mt2032 Reg.E=0x%02x\n",buf
[0]);
169 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf
[0]);
175 static int mt2032_optimize_vco(struct i2c_client
*c
,int sel
,int lock
)
177 struct tuner
*t
= i2c_get_clientdata(c
);
178 unsigned char buf
[2];
182 i2c_master_send(c
,buf
,1);
183 i2c_master_recv(c
,buf
,1);
184 tuner_dbg("mt2032 Reg.F=0x%02x\n",buf
[0]);
187 if(tad1
==0) return lock
;
188 if(tad1
==1) return lock
;
202 tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel
);
206 i2c_master_send(c
,buf
,2);
207 lock
=mt2032_check_lo_lock(c
);
212 static void mt2032_set_if_freq(struct i2c_client
*c
, unsigned int rfin
,
213 unsigned int if1
, unsigned int if2
,
214 unsigned int from
, unsigned int to
)
216 unsigned char buf
[21];
217 int lint_try
,ret
,sel
,lock
=0;
218 struct tuner
*t
= i2c_get_clientdata(c
);
220 tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
221 rfin
,if1
,if2
,from
,to
);
224 ret
=i2c_master_send(c
,buf
,1);
225 i2c_master_recv(c
,buf
,21);
228 ret
=mt2032_compute_freq(c
,rfin
,if1
,if2
,from
,to
,&buf
[1],&sel
,t
->xogc
);
232 // send only the relevant registers per Rev. 1.2
234 ret
=i2c_master_send(c
,buf
,4);
236 ret
=i2c_master_send(c
,buf
+5,4);
238 ret
=i2c_master_send(c
,buf
+11,3);
240 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret
);
242 // wait for PLLs to lock (per manual), retry LINT if not.
243 for(lint_try
=0; lint_try
<2; lint_try
++) {
244 lock
=mt2032_check_lo_lock(c
);
247 lock
=mt2032_optimize_vco(c
,sel
,lock
);
250 tuner_dbg("mt2032: re-init PLLs by LINT\n");
252 buf
[1]=0x80 +8+t
->xogc
; // set LINT to re-init PLLs
253 i2c_master_send(c
,buf
,2);
256 i2c_master_send(c
,buf
,2);
260 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
263 buf
[1]=0x20; // LOGC for optimal phase noise
264 ret
=i2c_master_send(c
,buf
,2);
266 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret
);
270 static void mt2032_set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
272 struct tuner
*t
= i2c_get_clientdata(c
);
275 // signal bandwidth and picture carrier
276 if (t
->std
& V4L2_STD_525_60
) {
288 mt2032_set_if_freq(c
, freq
*62500 /* freq*1000*1000/16 */,
289 1090*1000*1000, if2
, from
, to
);
292 static void mt2032_set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
294 struct tuner
*t
= i2c_get_clientdata(c
);
295 int if2
= t
->radio_if2
;
297 // per Manual for FM tuning: first if center freq. 1085 MHz
298 mt2032_set_if_freq(c
, freq
*62500 /* freq*1000*1000/16 */,
299 1085*1000*1000,if2
,if2
,if2
);
302 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
303 static int mt2032_init(struct i2c_client
*c
)
305 struct tuner
*t
= i2c_get_clientdata(c
);
306 unsigned char buf
[21];
309 // Initialize Registers per spec.
310 buf
[1]=2; // Index to register 2
314 ret
=i2c_master_send(c
,buf
+1,4);
316 buf
[5]=6; // Index register 6
322 ret
=i2c_master_send(c
,buf
+5,6);
324 buf
[12]=13; // Index register 13
326 ret
=i2c_master_send(c
,buf
+12,2);
328 // Adjust XOGC (register 7), wait for XOK
331 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc
&0x07);
334 i2c_master_send(c
,buf
,1);
335 i2c_master_recv(c
,buf
,1);
337 tuner_dbg("mt2032: xok = 0x%02x\n",xok
);
341 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc
&0x07);
343 xogc
=4; // min. 4 per spec
348 ret
=i2c_master_send(c
,buf
,2);
350 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret
);
354 t
->tv_freq
= mt2032_set_tv_freq
;
355 t
->radio_freq
= mt2032_set_radio_freq
;
359 static void mt2050_set_antenna(struct i2c_client
*c
, unsigned char antenna
)
361 struct tuner
*t
= i2c_get_clientdata(c
);
362 unsigned char buf
[2];
366 buf
[1] = antenna
? 0x11 : 0x10;
367 ret
=i2c_master_send(c
,buf
,2);
368 tuner_dbg("mt2050: enabled antenna connector %d\n", antenna
);
371 static void mt2050_set_if_freq(struct i2c_client
*c
,unsigned int freq
, unsigned int if2
)
373 struct tuner
*t
= i2c_get_clientdata(c
);
374 unsigned int if1
=1218*1000*1000;
375 unsigned int f_lo1
,f_lo2
,lo1
,lo2
,f_lo1_modulo
,f_lo2_modulo
,num1
,num2
,div1a
,div1b
,div2a
,div2b
;
377 unsigned char buf
[6];
379 tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
383 f_lo1
=(f_lo1
/1000000)*1000000;
385 f_lo2
=f_lo1
-freq
-if2
;
386 f_lo2
=(f_lo2
/50000)*50000;
391 f_lo1_modulo
= f_lo1
-(lo1
*4000000);
392 f_lo2_modulo
= f_lo2
-(lo2
*4000000);
394 num1
=4*f_lo1_modulo
/4000000;
395 num2
=4096*(f_lo2_modulo
/1000)/4000;
400 div1b
=lo1
-(div1a
+1)*12;
403 div2b
=lo2
-(div2a
+1)*8;
405 if (tuner_debug
> 1) {
406 tuner_dbg("lo1 lo2 = %d %d\n", lo1
, lo2
);
407 tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
408 num1
,num2
,div1a
,div1b
,div2a
,div2b
);
412 buf
[1]= 4*div1b
+ num1
;
413 if(freq
<275*1000*1000) buf
[1] = buf
[1]|0x80;
416 buf
[3]=32*div2b
+ num2
/256;
417 buf
[4]=num2
-(num2
/256)*256;
419 if(num2
!=0) buf
[5]=buf
[5]|0x40;
421 if (tuner_debug
> 1) {
423 tuner_dbg("bufs is: ");
425 printk("%x ",buf
[i
]);
429 ret
=i2c_master_send(c
,buf
,6);
431 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret
);
434 static void mt2050_set_tv_freq(struct i2c_client
*c
, unsigned int freq
)
436 struct tuner
*t
= i2c_get_clientdata(c
);
439 if (t
->std
& V4L2_STD_525_60
) {
446 if (V4L2_TUNER_DIGITAL_TV
== t
->mode
) {
447 // DVB (pinnacle 300i)
450 mt2050_set_if_freq(c
, freq
*62500, if2
);
451 mt2050_set_antenna(c
, tv_antenna
);
454 static void mt2050_set_radio_freq(struct i2c_client
*c
, unsigned int freq
)
456 struct tuner
*t
= i2c_get_clientdata(c
);
457 int if2
= t
->radio_if2
;
459 mt2050_set_if_freq(c
, freq
*62500, if2
);
460 mt2050_set_antenna(c
, radio_antenna
);
463 static int mt2050_init(struct i2c_client
*c
)
465 struct tuner
*t
= i2c_get_clientdata(c
);
466 unsigned char buf
[2];
471 ret
=i2c_master_send(c
,buf
,2); // power
475 ret
=i2c_master_send(c
,buf
,2); // m1lo
478 ret
=i2c_master_send(c
,buf
,1);
479 i2c_master_recv(c
,buf
,1);
481 tuner_dbg("mt2050: sro is %x\n",buf
[0]);
482 t
->tv_freq
= mt2050_set_tv_freq
;
483 t
->radio_freq
= mt2050_set_radio_freq
;
487 int microtune_init(struct i2c_client
*c
)
489 struct tuner
*t
= i2c_get_clientdata(c
);
491 unsigned char buf
[21];
494 memset(buf
,0,sizeof(buf
));
496 t
->radio_freq
= NULL
;
499 i2c_master_send(c
,buf
,1);
500 i2c_master_recv(c
,buf
,21);
503 tuner_dbg("MT20xx hexdump:");
505 printk(" %02x",buf
[i
]);
506 if(((i
+1)%8)==0) printk(" ");
510 company_code
= buf
[0x11] << 8 | buf
[0x12];
511 tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",
512 company_code
,buf
[0x13],buf
[0x14]);
515 /* seems to cause more problems than it solves ... */
516 switch (company_code
) {
526 tuner_warn("tuner: microtune: unknown companycode\n");
531 if (buf
[0x13] < ARRAY_SIZE(microtune_part
) &&
532 NULL
!= microtune_part
[buf
[0x13]])
533 name
= microtune_part
[buf
[0x13]];
542 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
547 strlcpy(c
->name
, name
, sizeof(c
->name
));
548 tuner_info("microtune %s found, OK\n",name
);
553 * Overrides for Emacs so that we follow Linus's tabbing style.
554 * ---------------------------------------------------------------------------