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 "tuner-driver.h"
11 /* ---------------------------------------------------------------------- */
13 static unsigned int optimize_vco = 1;
14 module_param(optimize_vco, int, 0644);
16 static unsigned int tv_antenna = 1;
17 module_param(tv_antenna, int, 0644);
19 static unsigned int radio_antenna = 0;
20 module_param(radio_antenna, int, 0644);
22 /* from tuner-core.c */
23 extern int tuner_debug;
25 /* ---------------------------------------------------------------------- */
32 static char *microtune_part[] = {
33 [ MT2030 ] = "MT2030",
34 [ MT2032 ] = "MT2032",
35 [ MT2040 ] = "MT2040",
36 [ MT2050 ] = "MT2050",
39 struct microtune_priv {
40 struct tuner_i2c_props i2c_props;
43 unsigned int radio_if2;
46 static void microtune_release(struct tuner *t)
53 static int mt2032_spurcheck(struct tuner *t,
54 int f1, int f2, int spectrum_from,int spectrum_to)
58 f1=f1/1000; //scale to kHz to avoid 32bit overflows
63 tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
64 f1,f2,spectrum_from,spectrum_to);
72 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
74 if( (f>spectrum_from) && (f<spectrum_to))
75 tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);
76 } while ( (f>(f2-spectrum_to)) || (n2>-5));
83 static int mt2032_compute_freq(struct tuner *t,
85 unsigned int if1, unsigned int if2,
86 unsigned int spectrum_from,
87 unsigned int spectrum_to,
90 unsigned int xogc) //all in Hz
92 unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
93 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
95 fref= 5250 *1000; //5.25MHz
98 lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
102 s=rfin/1000/1000+1090;
106 else if(s>1720) sel=1;
107 else if(s>1530) sel=2;
108 else if(s>1370) sel=3;
112 if(s>1790) sel=0; // <1958
113 else if(s>1617) sel=1;
114 else if(s>1449) sel=2;
115 else if(s>1291) sel=3;
120 lo1freq=(lo1a+8*lo1n)*fref;
122 tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
123 rfin,lo1,lo1n,lo1a,sel,lo1freq);
125 desired_lo2=lo1freq-rfin-if2;
126 lo2=(desired_lo2)/fref;
129 lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
130 lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
132 tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
133 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
135 if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
136 tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
137 lo1a, lo1n, lo2a,lo2n);
141 mt2032_spurcheck(t, lo1freq, desired_lo2, spectrum_from, spectrum_to);
142 // should recalculate lo1 (one step up/down)
144 // set up MT2032 register map for transfer over i2c
146 buf[1]=lo1a | (sel<<4);
148 buf[3]=0x0f; //reserved
150 buf[5]=(lo2n-1) | (lo2a<<5);
151 if(rfin >400*1000*1000)
154 buf[6]=0xf4; // set PKEN per rev 1.2
156 buf[8]=0xc3; //reserved
157 buf[9]=0x4e; //reserved
158 buf[10]=0xec; //reserved
159 buf[11]=(lo2num&0xff);
160 buf[12]=(lo2num>>8) |0x80; // Lo2RST
165 static int mt2032_check_lo_lock(struct tuner *t)
167 struct microtune_priv *priv = t->priv;
169 unsigned char buf[2];
171 for(try=0;try<10;try++) {
173 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
174 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
175 tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);
181 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
187 static int mt2032_optimize_vco(struct tuner *t,int sel,int lock)
189 struct microtune_priv *priv = t->priv;
190 unsigned char buf[2];
194 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
195 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
196 tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);
199 if(tad1 ==0) return lock;
200 if(tad1 ==1) return lock;
214 tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
218 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
219 lock=mt2032_check_lo_lock(t);
224 static void mt2032_set_if_freq(struct tuner *t, unsigned int rfin,
225 unsigned int if1, unsigned int if2,
226 unsigned int from, unsigned int to)
228 unsigned char buf[21];
229 int lint_try,ret,sel,lock=0;
230 struct microtune_priv *priv = t->priv;
232 tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
233 rfin,if1,if2,from,to);
236 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
237 tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);
240 ret=mt2032_compute_freq(t,rfin,if1,if2,from,to,&buf[1],&sel,priv->xogc);
244 // send only the relevant registers per Rev. 1.2
246 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,4);
248 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,4);
250 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+11,3);
252 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
254 // wait for PLLs to lock (per manual), retry LINT if not.
255 for(lint_try=0; lint_try<2; lint_try++) {
256 lock=mt2032_check_lo_lock(t);
259 lock=mt2032_optimize_vco(t,sel,lock);
262 tuner_dbg("mt2032: re-init PLLs by LINT\n");
264 buf[1]=0x80 +8+priv->xogc; // set LINT to re-init PLLs
265 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
268 tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
272 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
275 buf[1]=0x20; // LOGC for optimal phase noise
276 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
278 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
282 static void mt2032_set_tv_freq(struct tuner *t, unsigned int freq)
286 // signal bandwidth and picture carrier
287 if (t->std & V4L2_STD_525_60) {
299 mt2032_set_if_freq(t, freq*62500 /* freq*1000*1000/16 */,
300 1090*1000*1000, if2, from, to);
303 static void mt2032_set_radio_freq(struct tuner *t, unsigned int freq)
305 struct microtune_priv *priv = t->priv;
306 int if2 = priv->radio_if2;
308 // per Manual for FM tuning: first if center freq. 1085 MHz
309 mt2032_set_if_freq(t, freq * 1000 / 16,
310 1085*1000*1000,if2,if2,if2);
313 static struct tuner_operations mt2032_tuner_ops = {
314 .set_tv_freq = mt2032_set_tv_freq,
315 .set_radio_freq = mt2032_set_radio_freq,
316 .release = microtune_release,
319 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
320 static int mt2032_init(struct tuner *t)
322 struct microtune_priv *priv = t->priv;
323 unsigned char buf[21];
326 // Initialize Registers per spec.
327 buf[1]=2; // Index to register 2
331 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+1,4);
333 buf[5]=6; // Index register 6
339 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,6);
341 buf[12]=13; // Index register 13
343 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+12,2);
345 // Adjust XOGC (register 7), wait for XOK
348 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
351 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
352 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
354 tuner_dbg("mt2032: xok = 0x%02x\n",xok);
358 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
360 xogc=4; // min. 4 per spec
365 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
367 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
371 memcpy(&t->ops, &mt2032_tuner_ops, sizeof(struct tuner_operations));
376 static void mt2050_set_antenna(struct tuner *t, unsigned char antenna)
378 struct microtune_priv *priv = t->priv;
379 unsigned char buf[2];
383 buf[1] = antenna ? 0x11 : 0x10;
384 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);
385 tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
388 static void mt2050_set_if_freq(struct tuner *t,unsigned int freq, unsigned int if2)
390 struct microtune_priv *priv = t->priv;
391 unsigned int if1=1218*1000*1000;
392 unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
394 unsigned char buf[6];
396 tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
400 f_lo1=(f_lo1/1000000)*1000000;
402 f_lo2=f_lo1-freq-if2;
403 f_lo2=(f_lo2/50000)*50000;
408 f_lo1_modulo= f_lo1-(lo1*4000000);
409 f_lo2_modulo= f_lo2-(lo2*4000000);
411 num1=4*f_lo1_modulo/4000000;
412 num2=4096*(f_lo2_modulo/1000)/4000;
417 div1b=lo1-(div1a+1)*12;
420 div2b=lo2-(div2a+1)*8;
422 if (tuner_debug > 1) {
423 tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);
424 tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
425 num1,num2,div1a,div1b,div2a,div2b);
429 buf[1]= 4*div1b + num1;
430 if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
433 buf[3]=32*div2b + num2/256;
434 buf[4]=num2-(num2/256)*256;
436 if(num2!=0) buf[5]=buf[5]|0x40;
438 if (tuner_debug > 1) {
440 tuner_dbg("bufs is: ");
442 printk("%x ",buf[i]);
446 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,6);
448 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
451 static void mt2050_set_tv_freq(struct tuner *t, unsigned int freq)
455 if (t->std & V4L2_STD_525_60) {
462 if (V4L2_TUNER_DIGITAL_TV == t->mode) {
463 // DVB (pinnacle 300i)
466 mt2050_set_if_freq(t, freq*62500, if2);
467 mt2050_set_antenna(t, tv_antenna);
470 static void mt2050_set_radio_freq(struct tuner *t, unsigned int freq)
472 struct microtune_priv *priv = t->priv;
473 int if2 = priv->radio_if2;
475 mt2050_set_if_freq(t, freq * 1000 / 16, if2);
476 mt2050_set_antenna(t, radio_antenna);
479 static struct tuner_operations mt2050_tuner_ops = {
480 .set_tv_freq = mt2050_set_tv_freq,
481 .set_radio_freq = mt2050_set_radio_freq,
482 .release = microtune_release,
485 static int mt2050_init(struct tuner *t)
487 struct microtune_priv *priv = t->priv;
488 unsigned char buf[2];
493 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // power
497 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // m1lo
500 ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
501 tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);
503 tuner_dbg("mt2050: sro is %x\n",buf[0]);
505 memcpy(&t->ops, &mt2050_tuner_ops, sizeof(struct tuner_operations));
510 int microtune_init(struct tuner *t)
512 struct microtune_priv *priv = NULL;
514 unsigned char buf[21];
517 priv = kzalloc(sizeof(struct microtune_priv), GFP_KERNEL);
522 priv->i2c_props.addr = t->i2c.addr;
523 priv->i2c_props.adap = t->i2c.adapter;
525 priv->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
527 memset(buf,0,sizeof(buf));
529 if (t->std & V4L2_STD_525_60) {
530 tuner_dbg("pinnacle ntsc\n");
531 priv->radio_if2 = 41300 * 1000;
533 tuner_dbg("pinnacle pal\n");
534 priv->radio_if2 = 33300 * 1000;
538 tuner_i2c_xfer_send(&priv->i2c_props,buf,1);
539 tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);
542 tuner_dbg("MT20xx hexdump:");
544 printk(" %02x",buf[i]);
545 if(((i+1)%8)==0) printk(" ");
549 company_code = buf[0x11] << 8 | buf[0x12];
550 tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",
551 company_code,buf[0x13],buf[0x14]);
554 if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
555 NULL != microtune_part[buf[0x13]])
556 name = microtune_part[buf[0x13]];
565 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
570 strlcpy(t->i2c.name, name, sizeof(t->i2c.name));
571 tuner_info("microtune %s found, OK\n",name);
576 * Overrides for Emacs so that we follow Linus's tabbing style.
577 * ---------------------------------------------------------------------------