Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / media / video / mt20xx.c
blobc7c9f3f8715cbd62b96491c2965baef70520b763
1 /*
3 * i2c tv tuner chip device driver
4 * controls microtune tuners, mt2032 + mt2050 at the moment.
5 */
6 #include <linux/delay.h>
7 #include <linux/i2c.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 /* from tuner-core.c */
24 extern int tuner_debug;
26 /* ---------------------------------------------------------------------- */
28 #define MT2032 0x04
29 #define MT2030 0x06
30 #define MT2040 0x07
31 #define MT2050 0x42
33 static char *microtune_part[] = {
34 [ MT2030 ] = "MT2030",
35 [ MT2032 ] = "MT2032",
36 [ MT2040 ] = "MT2040",
37 [ MT2050 ] = "MT2050",
40 // IsSpurInBand()?
41 static int mt2032_spurcheck(struct i2c_client *c,
42 int f1, int f2, int spectrum_from,int spectrum_to)
44 struct tuner *t = i2c_get_clientdata(c);
45 int n1=1,n2,f;
47 f1=f1/1000; //scale to kHz to avoid 32bit overflows
48 f2=f2/1000;
49 spectrum_from/=1000;
50 spectrum_to/=1000;
52 tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",
53 f1,f2,spectrum_from,spectrum_to);
55 do {
56 n2=-n1;
57 f=n1*(f1-f2);
58 do {
59 n2--;
60 f=f-f2;
61 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
63 if( (f>spectrum_from) && (f<spectrum_to))
64 tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);
65 } while ( (f>(f2-spectrum_to)) || (n2>-5));
66 n1++;
67 } while (n1<5);
69 return 1;
72 static int mt2032_compute_freq(struct i2c_client *c,
73 unsigned int rfin,
74 unsigned int if1, unsigned int if2,
75 unsigned int spectrum_from,
76 unsigned int spectrum_to,
77 unsigned char *buf,
78 int *ret_sel,
79 unsigned int xogc) //all in Hz
81 struct tuner *t = i2c_get_clientdata(c);
82 unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
83 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
85 fref= 5250 *1000; //5.25MHz
86 desired_lo1=rfin+if1;
88 lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
89 lo1n=lo1/8;
90 lo1a=lo1-(lo1n*8);
92 s=rfin/1000/1000+1090;
94 if(optimize_vco) {
95 if(s>1890) sel=0;
96 else if(s>1720) sel=1;
97 else if(s>1530) sel=2;
98 else if(s>1370) sel=3;
99 else sel=4; // >1090
101 else {
102 if(s>1790) sel=0; // <1958
103 else if(s>1617) sel=1;
104 else if(s>1449) sel=2;
105 else if(s>1291) sel=3;
106 else sel=4; // >1090
108 *ret_sel=sel;
110 lo1freq=(lo1a+8*lo1n)*fref;
112 tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
113 rfin,lo1,lo1n,lo1a,sel,lo1freq);
115 desired_lo2=lo1freq-rfin-if2;
116 lo2=(desired_lo2)/fref;
117 lo2n=lo2/8;
118 lo2a=lo2-(lo2n*8);
119 lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
120 lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
122 tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
123 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
125 if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
126 tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",
127 lo1a, lo1n, lo2a,lo2n);
128 return(-1);
131 mt2032_spurcheck(c, lo1freq, desired_lo2, spectrum_from, spectrum_to);
132 // should recalculate lo1 (one step up/down)
134 // set up MT2032 register map for transfer over i2c
135 buf[0]=lo1n-1;
136 buf[1]=lo1a | (sel<<4);
137 buf[2]=0x86; // LOGC
138 buf[3]=0x0f; //reserved
139 buf[4]=0x1f;
140 buf[5]=(lo2n-1) | (lo2a<<5);
141 if(rfin >400*1000*1000)
142 buf[6]=0xe4;
143 else
144 buf[6]=0xf4; // set PKEN per rev 1.2
145 buf[7]=8+xogc;
146 buf[8]=0xc3; //reserved
147 buf[9]=0x4e; //reserved
148 buf[10]=0xec; //reserved
149 buf[11]=(lo2num&0xff);
150 buf[12]=(lo2num>>8) |0x80; // Lo2RST
152 return 0;
155 static int mt2032_check_lo_lock(struct i2c_client *c)
157 struct tuner *t = i2c_get_clientdata(c);
158 int try,lock=0;
159 unsigned char buf[2];
161 for(try=0;try<10;try++) {
162 buf[0]=0x0e;
163 i2c_master_send(c,buf,1);
164 i2c_master_recv(c,buf,1);
165 tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);
166 lock=buf[0] &0x06;
168 if (lock==6)
169 break;
171 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
172 udelay(1000);
174 return lock;
177 static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
179 struct tuner *t = i2c_get_clientdata(c);
180 unsigned char buf[2];
181 int tad1;
183 buf[0]=0x0f;
184 i2c_master_send(c,buf,1);
185 i2c_master_recv(c,buf,1);
186 tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);
187 tad1=buf[0]&0x07;
189 if(tad1 ==0) return lock;
190 if(tad1 ==1) return lock;
192 if(tad1==2) {
193 if(sel==0)
194 return lock;
195 else sel--;
197 else {
198 if(sel<4)
199 sel++;
200 else
201 return lock;
204 tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
206 buf[0]=0x0f;
207 buf[1]=sel;
208 i2c_master_send(c,buf,2);
209 lock=mt2032_check_lo_lock(c);
210 return lock;
214 static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin,
215 unsigned int if1, unsigned int if2,
216 unsigned int from, unsigned int to)
218 unsigned char buf[21];
219 int lint_try,ret,sel,lock=0;
220 struct tuner *t = i2c_get_clientdata(c);
222 tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
223 rfin,if1,if2,from,to);
225 buf[0]=0;
226 ret=i2c_master_send(c,buf,1);
227 i2c_master_recv(c,buf,21);
229 buf[0]=0;
230 ret=mt2032_compute_freq(c,rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
231 if (ret<0)
232 return;
234 // send only the relevant registers per Rev. 1.2
235 buf[0]=0;
236 ret=i2c_master_send(c,buf,4);
237 buf[5]=5;
238 ret=i2c_master_send(c,buf+5,4);
239 buf[11]=11;
240 ret=i2c_master_send(c,buf+11,3);
241 if(ret!=3)
242 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
244 // wait for PLLs to lock (per manual), retry LINT if not.
245 for(lint_try=0; lint_try<2; lint_try++) {
246 lock=mt2032_check_lo_lock(c);
248 if(optimize_vco)
249 lock=mt2032_optimize_vco(c,sel,lock);
250 if(lock==6) break;
252 tuner_dbg("mt2032: re-init PLLs by LINT\n");
253 buf[0]=7;
254 buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
255 i2c_master_send(c,buf,2);
256 mdelay(10);
257 buf[1]=8+t->xogc;
258 i2c_master_send(c,buf,2);
261 if (lock!=6)
262 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
264 buf[0]=2;
265 buf[1]=0x20; // LOGC for optimal phase noise
266 ret=i2c_master_send(c,buf,2);
267 if (ret!=2)
268 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
272 static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
274 struct tuner *t = i2c_get_clientdata(c);
275 int if2,from,to;
277 // signal bandwidth and picture carrier
278 if (t->std & V4L2_STD_525_60) {
279 // NTSC
280 from = 40750*1000;
281 to = 46750*1000;
282 if2 = 45750*1000;
283 } else {
284 // PAL
285 from = 32900*1000;
286 to = 39900*1000;
287 if2 = 38900*1000;
290 mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
291 1090*1000*1000, if2, from, to);
294 static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
296 struct tuner *t = i2c_get_clientdata(c);
297 int if2 = t->radio_if2;
299 // per Manual for FM tuning: first if center freq. 1085 MHz
300 mt2032_set_if_freq(c, freq * 1000 / 16,
301 1085*1000*1000,if2,if2,if2);
304 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
305 static int mt2032_init(struct i2c_client *c)
307 struct tuner *t = i2c_get_clientdata(c);
308 unsigned char buf[21];
309 int ret,xogc,xok=0;
311 // Initialize Registers per spec.
312 buf[1]=2; // Index to register 2
313 buf[2]=0xff;
314 buf[3]=0x0f;
315 buf[4]=0x1f;
316 ret=i2c_master_send(c,buf+1,4);
318 buf[5]=6; // Index register 6
319 buf[6]=0xe4;
320 buf[7]=0x8f;
321 buf[8]=0xc3;
322 buf[9]=0x4e;
323 buf[10]=0xec;
324 ret=i2c_master_send(c,buf+5,6);
326 buf[12]=13; // Index register 13
327 buf[13]=0x32;
328 ret=i2c_master_send(c,buf+12,2);
330 // Adjust XOGC (register 7), wait for XOK
331 xogc=7;
332 do {
333 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
334 mdelay(10);
335 buf[0]=0x0e;
336 i2c_master_send(c,buf,1);
337 i2c_master_recv(c,buf,1);
338 xok=buf[0]&0x01;
339 tuner_dbg("mt2032: xok = 0x%02x\n",xok);
340 if (xok == 1) break;
342 xogc--;
343 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
344 if (xogc == 3) {
345 xogc=4; // min. 4 per spec
346 break;
348 buf[0]=0x07;
349 buf[1]=0x88 + xogc;
350 ret=i2c_master_send(c,buf,2);
351 if (ret!=2)
352 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
353 } while (xok != 1 );
354 t->xogc=xogc;
356 t->set_tv_freq = mt2032_set_tv_freq;
357 t->set_radio_freq = mt2032_set_radio_freq;
358 return(1);
361 static void mt2050_set_antenna(struct i2c_client *c, unsigned char antenna)
363 struct tuner *t = i2c_get_clientdata(c);
364 unsigned char buf[2];
365 int ret;
367 buf[0] = 6;
368 buf[1] = antenna ? 0x11 : 0x10;
369 ret=i2c_master_send(c,buf,2);
370 tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
373 static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
375 struct tuner *t = i2c_get_clientdata(c);
376 unsigned int if1=1218*1000*1000;
377 unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
378 int ret;
379 unsigned char buf[6];
381 tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
382 freq,if1,if2);
384 f_lo1=freq+if1;
385 f_lo1=(f_lo1/1000000)*1000000;
387 f_lo2=f_lo1-freq-if2;
388 f_lo2=(f_lo2/50000)*50000;
390 lo1=f_lo1/4000000;
391 lo2=f_lo2/4000000;
393 f_lo1_modulo= f_lo1-(lo1*4000000);
394 f_lo2_modulo= f_lo2-(lo2*4000000);
396 num1=4*f_lo1_modulo/4000000;
397 num2=4096*(f_lo2_modulo/1000)/4000;
399 // todo spurchecks
401 div1a=(lo1/12)-1;
402 div1b=lo1-(div1a+1)*12;
404 div2a=(lo2/8)-1;
405 div2b=lo2-(div2a+1)*8;
407 if (tuner_debug > 1) {
408 tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);
409 tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",
410 num1,num2,div1a,div1b,div2a,div2b);
413 buf[0]=1;
414 buf[1]= 4*div1b + num1;
415 if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
417 buf[2]=div1a;
418 buf[3]=32*div2b + num2/256;
419 buf[4]=num2-(num2/256)*256;
420 buf[5]=div2a;
421 if(num2!=0) buf[5]=buf[5]|0x40;
423 if (tuner_debug > 1) {
424 int i;
425 tuner_dbg("bufs is: ");
426 for(i=0;i<6;i++)
427 printk("%x ",buf[i]);
428 printk("\n");
431 ret=i2c_master_send(c,buf,6);
432 if (ret!=6)
433 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
436 static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
438 struct tuner *t = i2c_get_clientdata(c);
439 unsigned int if2;
441 if (t->std & V4L2_STD_525_60) {
442 // NTSC
443 if2 = 45750*1000;
444 } else {
445 // PAL
446 if2 = 38900*1000;
448 if (V4L2_TUNER_DIGITAL_TV == t->mode) {
449 // DVB (pinnacle 300i)
450 if2 = 36150*1000;
452 mt2050_set_if_freq(c, freq*62500, if2);
453 mt2050_set_antenna(c, tv_antenna);
456 static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
458 struct tuner *t = i2c_get_clientdata(c);
459 int if2 = t->radio_if2;
461 mt2050_set_if_freq(c, freq * 1000 / 16, if2);
462 mt2050_set_antenna(c, radio_antenna);
465 static int mt2050_init(struct i2c_client *c)
467 struct tuner *t = i2c_get_clientdata(c);
468 unsigned char buf[2];
469 int ret;
471 buf[0]=6;
472 buf[1]=0x10;
473 ret=i2c_master_send(c,buf,2); // power
475 buf[0]=0x0f;
476 buf[1]=0x0f;
477 ret=i2c_master_send(c,buf,2); // m1lo
479 buf[0]=0x0d;
480 ret=i2c_master_send(c,buf,1);
481 i2c_master_recv(c,buf,1);
483 tuner_dbg("mt2050: sro is %x\n",buf[0]);
484 t->set_tv_freq = mt2050_set_tv_freq;
485 t->set_radio_freq = mt2050_set_radio_freq;
486 return 0;
489 int microtune_init(struct i2c_client *c)
491 struct tuner *t = i2c_get_clientdata(c);
492 char *name;
493 unsigned char buf[21];
494 int company_code;
496 memset(buf,0,sizeof(buf));
497 t->set_tv_freq = NULL;
498 t->set_radio_freq = NULL;
499 t->standby = NULL;
500 if (t->std & V4L2_STD_525_60) {
501 tuner_dbg("pinnacle ntsc\n");
502 t->radio_if2 = 41300 * 1000;
503 } else {
504 tuner_dbg("pinnacle pal\n");
505 t->radio_if2 = 33300 * 1000;
507 name = "unknown";
509 i2c_master_send(c,buf,1);
510 i2c_master_recv(c,buf,21);
511 if (tuner_debug) {
512 int i;
513 tuner_dbg("MT20xx hexdump:");
514 for(i=0;i<21;i++) {
515 printk(" %02x",buf[i]);
516 if(((i+1)%8)==0) printk(" ");
518 printk("\n");
520 company_code = buf[0x11] << 8 | buf[0x12];
521 tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",
522 company_code,buf[0x13],buf[0x14]);
525 if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
526 NULL != microtune_part[buf[0x13]])
527 name = microtune_part[buf[0x13]];
528 switch (buf[0x13]) {
529 case MT2032:
530 mt2032_init(c);
531 break;
532 case MT2050:
533 mt2050_init(c);
534 break;
535 default:
536 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
537 name);
538 return 0;
541 strlcpy(c->name, name, sizeof(c->name));
542 tuner_info("microtune %s found, OK\n",name);
543 return 0;
547 * Overrides for Emacs so that we follow Linus's tabbing style.
548 * ---------------------------------------------------------------------------
549 * Local variables:
550 * c-basic-offset: 8
551 * End: