3 * i2c tv tuner chip device driver
4 * controls microtune tuners, mt2032 + mt2050 at the moment.
6 #include <linux/delay.h>
8 #include <linux/videodev.h>
9 #include <linux/moduleparam.h>
10 #include <media/tuner.h>
12 /* ---------------------------------------------------------------------- */
14 static unsigned int optimize_vco = 1;
15 module_param(optimize_vco, int, 0644);
17 static unsigned int tv_antenna = 1;
18 module_param(tv_antenna, int, 0644);
20 static unsigned int radio_antenna = 0;
21 module_param(radio_antenna, int, 0644);
23 /* ---------------------------------------------------------------------- */
30 static char *microtune_part[] = {
31 [ MT2030 ] = "MT2030",
32 [ MT2032 ] = "MT2032",
33 [ MT2040 ] = "MT2040",
34 [ MT2050 ] = "MT2050",
38 static int mt2032_spurcheck(struct i2c_client *c,
39 int f1, int f2, int spectrum_from,int spectrum_to)
41 struct tuner *t = i2c_get_clientdata(c);
44 f1=f1/1000; //scale to kHz to avoid 32bit overflows
49 tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
50 f1,f2,spectrum_from,spectrum_to);
58 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
60 if( (f>spectrum_from) && (f<spectrum_to))
61 tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);
62 } while ( (f>(f2-spectrum_to)) || (n2>-5));
69 static int mt2032_compute_freq(struct i2c_client *c,
71 unsigned int if1, unsigned int if2,
72 unsigned int spectrum_from,
73 unsigned int spectrum_to,
76 unsigned int xogc) //all in Hz
78 struct tuner *t = i2c_get_clientdata(c);
79 unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
80 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
82 fref= 5250 *1000; //5.25MHz
85 lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
89 s=rfin/1000/1000+1090;
93 else if(s>1720) sel=1;
94 else if(s>1530) sel=2;
95 else if(s>1370) sel=3;
99 if(s>1790) sel=0; // <1958
100 else if(s>1617) sel=1;
101 else if(s>1449) sel=2;
102 else if(s>1291) sel=3;
107 lo1freq=(lo1a+8*lo1n)*fref;
109 tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
110 rfin,lo1,lo1n,lo1a,sel,lo1freq);
112 desired_lo2=lo1freq-rfin-if2;
113 lo2=(desired_lo2)/fref;
116 lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
117 lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
119 tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
120 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
122 if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
123 tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
124 lo1a, lo1n, lo2a,lo2n);
128 mt2032_spurcheck(c, lo1freq, desired_lo2, spectrum_from, spectrum_to);
129 // should recalculate lo1 (one step up/down)
131 // set up MT2032 register map for transfer over i2c
133 buf[1]=lo1a | (sel<<4);
135 buf[3]=0x0f; //reserved
137 buf[5]=(lo2n-1) | (lo2a<<5);
138 if(rfin >400*1000*1000)
141 buf[6]=0xf4; // set PKEN per rev 1.2
143 buf[8]=0xc3; //reserved
144 buf[9]=0x4e; //reserved
145 buf[10]=0xec; //reserved
146 buf[11]=(lo2num&0xff);
147 buf[12]=(lo2num>>8) |0x80; // Lo2RST
152 static int mt2032_check_lo_lock(struct i2c_client *c)
154 struct tuner *t = i2c_get_clientdata(c);
156 unsigned char buf[2];
158 for(try=0;try<10;try++) {
160 i2c_master_send(c,buf,1);
161 i2c_master_recv(c,buf,1);
162 tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);
168 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
174 static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
176 struct tuner *t = i2c_get_clientdata(c);
177 unsigned char buf[2];
181 i2c_master_send(c,buf,1);
182 i2c_master_recv(c,buf,1);
183 tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);
186 if(tad1 ==0) return lock;
187 if(tad1 ==1) return lock;
201 tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
205 i2c_master_send(c,buf,2);
206 lock=mt2032_check_lo_lock(c);
211 static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin,
212 unsigned int if1, unsigned int if2,
213 unsigned int from, unsigned int to)
215 unsigned char buf[21];
216 int lint_try,ret,sel,lock=0;
217 struct tuner *t = i2c_get_clientdata(c);
219 tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
220 rfin,if1,if2,from,to);
223 ret=i2c_master_send(c,buf,1);
224 i2c_master_recv(c,buf,21);
227 ret=mt2032_compute_freq(c,rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
231 // send only the relevant registers per Rev. 1.2
233 ret=i2c_master_send(c,buf,4);
235 ret=i2c_master_send(c,buf+5,4);
237 ret=i2c_master_send(c,buf+11,3);
239 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
241 // wait for PLLs to lock (per manual), retry LINT if not.
242 for(lint_try=0; lint_try<2; lint_try++) {
243 lock=mt2032_check_lo_lock(c);
246 lock=mt2032_optimize_vco(c,sel,lock);
249 tuner_dbg("mt2032: re-init PLLs by LINT\n");
251 buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
252 i2c_master_send(c,buf,2);
255 i2c_master_send(c,buf,2);
259 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
262 buf[1]=0x20; // LOGC for optimal phase noise
263 ret=i2c_master_send(c,buf,2);
265 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
269 static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
271 struct tuner *t = i2c_get_clientdata(c);
274 // signal bandwidth and picture carrier
275 if (t->std & V4L2_STD_525_60) {
287 mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
288 1090*1000*1000, if2, from, to);
291 static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
293 struct tuner *t = i2c_get_clientdata(c);
294 int if2 = t->radio_if2;
296 // per Manual for FM tuning: first if center freq. 1085 MHz
297 mt2032_set_if_freq(c, freq * 1000 / 16,
298 1085*1000*1000,if2,if2,if2);
301 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
302 static int mt2032_init(struct i2c_client *c)
304 struct tuner *t = i2c_get_clientdata(c);
305 unsigned char buf[21];
308 // Initialize Registers per spec.
309 buf[1]=2; // Index to register 2
313 ret=i2c_master_send(c,buf+1,4);
315 buf[5]=6; // Index register 6
321 ret=i2c_master_send(c,buf+5,6);
323 buf[12]=13; // Index register 13
325 ret=i2c_master_send(c,buf+12,2);
327 // Adjust XOGC (register 7), wait for XOK
330 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
333 i2c_master_send(c,buf,1);
334 i2c_master_recv(c,buf,1);
336 tuner_dbg("mt2032: xok = 0x%02x\n",xok);
340 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
342 xogc=4; // min. 4 per spec
347 ret=i2c_master_send(c,buf,2);
349 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
353 t->tv_freq = mt2032_set_tv_freq;
354 t->radio_freq = mt2032_set_radio_freq;
358 static void mt2050_set_antenna(struct i2c_client *c, unsigned char antenna)
360 struct tuner *t = i2c_get_clientdata(c);
361 unsigned char buf[2];
365 buf[1] = antenna ? 0x11 : 0x10;
366 ret=i2c_master_send(c,buf,2);
367 tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
370 static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
372 struct tuner *t = i2c_get_clientdata(c);
373 unsigned int if1=1218*1000*1000;
374 unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
376 unsigned char buf[6];
378 tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
382 f_lo1=(f_lo1/1000000)*1000000;
384 f_lo2=f_lo1-freq-if2;
385 f_lo2=(f_lo2/50000)*50000;
390 f_lo1_modulo= f_lo1-(lo1*4000000);
391 f_lo2_modulo= f_lo2-(lo2*4000000);
393 num1=4*f_lo1_modulo/4000000;
394 num2=4096*(f_lo2_modulo/1000)/4000;
399 div1b=lo1-(div1a+1)*12;
402 div2b=lo2-(div2a+1)*8;
404 if (tuner_debug > 1) {
405 tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);
406 tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
407 num1,num2,div1a,div1b,div2a,div2b);
411 buf[1]= 4*div1b + num1;
412 if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
415 buf[3]=32*div2b + num2/256;
416 buf[4]=num2-(num2/256)*256;
418 if(num2!=0) buf[5]=buf[5]|0x40;
420 if (tuner_debug > 1) {
422 tuner_dbg("bufs is: ");
424 printk("%x ",buf[i]);
428 ret=i2c_master_send(c,buf,6);
430 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
433 static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
435 struct tuner *t = i2c_get_clientdata(c);
438 if (t->std & V4L2_STD_525_60) {
445 if (V4L2_TUNER_DIGITAL_TV == t->mode) {
446 // DVB (pinnacle 300i)
449 mt2050_set_if_freq(c, freq*62500, if2);
450 mt2050_set_antenna(c, tv_antenna);
453 static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
455 struct tuner *t = i2c_get_clientdata(c);
456 int if2 = t->radio_if2;
458 mt2050_set_if_freq(c, freq*62500, if2);
459 mt2050_set_antenna(c, radio_antenna);
462 static int mt2050_init(struct i2c_client *c)
464 struct tuner *t = i2c_get_clientdata(c);
465 unsigned char buf[2];
470 ret=i2c_master_send(c,buf,2); // power
474 ret=i2c_master_send(c,buf,2); // m1lo
477 ret=i2c_master_send(c,buf,1);
478 i2c_master_recv(c,buf,1);
480 tuner_dbg("mt2050: sro is %x\n",buf[0]);
481 t->tv_freq = mt2050_set_tv_freq;
482 t->radio_freq = mt2050_set_radio_freq;
486 int microtune_init(struct i2c_client *c)
488 struct tuner *t = i2c_get_clientdata(c);
490 unsigned char buf[21];
493 memset(buf,0,sizeof(buf));
495 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 if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
516 NULL != microtune_part[buf[0x13]])
517 name = microtune_part[buf[0x13]];
526 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
531 strlcpy(c->name, name, sizeof(c->name));
532 tuner_info("microtune %s found, OK\n",name);
537 * Overrides for Emacs so that we follow Linus's tabbing style.
538 * ---------------------------------------------------------------------------