]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/video/mt20xx.c
13b61c4dcbf856d2d0164cd049de4899347803ac
[karo-tx-linux.git] / drivers / media / video / mt20xx.c
1 /*
2  *
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>
11
12 /* ---------------------------------------------------------------------- */
13
14 static unsigned int optimize_vco  = 1;
15 module_param(optimize_vco,      int, 0644);
16
17 static unsigned int tv_antenna    = 1;
18 module_param(tv_antenna,        int, 0644);
19
20 static unsigned int radio_antenna = 0;
21 module_param(radio_antenna,     int, 0644);
22
23 /* ---------------------------------------------------------------------- */
24
25 #define MT2032 0x04
26 #define MT2030 0x06
27 #define MT2040 0x07
28 #define MT2050 0x42
29
30 static char *microtune_part[] = {
31         [ MT2030 ] = "MT2030",
32         [ MT2032 ] = "MT2032",
33         [ MT2040 ] = "MT2040",
34         [ MT2050 ] = "MT2050",
35 };
36
37 // IsSpurInBand()?
38 static int mt2032_spurcheck(struct i2c_client *c,
39                             int f1, int f2, int spectrum_from,int spectrum_to)
40 {
41         struct tuner *t = i2c_get_clientdata(c);
42         int n1=1,n2,f;
43
44         f1=f1/1000; //scale to kHz to avoid 32bit overflows
45         f2=f2/1000;
46         spectrum_from/=1000;
47         spectrum_to/=1000;
48
49         tuner_dbg("spurcheck f1=%d f2=%d  from=%d to=%d\n",
50                   f1,f2,spectrum_from,spectrum_to);
51
52         do {
53             n2=-n1;
54             f=n1*(f1-f2);
55             do {
56                 n2--;
57                 f=f-f2;
58                 tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
59
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));
63             n1++;
64         } while (n1<5);
65
66         return 1;
67 }
68
69 static int mt2032_compute_freq(struct i2c_client *c,
70                                unsigned int rfin,
71                                unsigned int if1, unsigned int if2,
72                                unsigned int spectrum_from,
73                                unsigned int spectrum_to,
74                                unsigned char *buf,
75                                int *ret_sel,
76                                unsigned int xogc) //all in Hz
77 {
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;
81
82         fref= 5250 *1000; //5.25MHz
83         desired_lo1=rfin+if1;
84
85         lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
86         lo1n=lo1/8;
87         lo1a=lo1-(lo1n*8);
88
89         s=rfin/1000/1000+1090;
90
91         if(optimize_vco) {
92                 if(s>1890) sel=0;
93                 else if(s>1720) sel=1;
94                 else if(s>1530) sel=2;
95                 else if(s>1370) sel=3;
96                 else sel=4; // >1090
97         }
98         else {
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;
103                 else sel=4; // >1090
104         }
105         *ret_sel=sel;
106
107         lo1freq=(lo1a+8*lo1n)*fref;
108
109         tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
110                   rfin,lo1,lo1n,lo1a,sel,lo1freq);
111
112         desired_lo2=lo1freq-rfin-if2;
113         lo2=(desired_lo2)/fref;
114         lo2n=lo2/8;
115         lo2a=lo2-(lo2n*8);
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;
118
119         tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
120                   rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
121
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);
125                 return(-1);
126         }
127
128         mt2032_spurcheck(c, lo1freq, desired_lo2,  spectrum_from, spectrum_to);
129         // should recalculate lo1 (one step up/down)
130
131         // set up MT2032 register map for transfer over i2c
132         buf[0]=lo1n-1;
133         buf[1]=lo1a | (sel<<4);
134         buf[2]=0x86; // LOGC
135         buf[3]=0x0f; //reserved
136         buf[4]=0x1f;
137         buf[5]=(lo2n-1) | (lo2a<<5);
138         if(rfin >400*1000*1000)
139                 buf[6]=0xe4;
140         else
141                 buf[6]=0xf4; // set PKEN per rev 1.2
142         buf[7]=8+xogc;
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
148
149         return 0;
150 }
151
152 static int mt2032_check_lo_lock(struct i2c_client *c)
153 {
154         struct tuner *t = i2c_get_clientdata(c);
155         int try,lock=0;
156         unsigned char buf[2];
157
158         for(try=0;try<10;try++) {
159                 buf[0]=0x0e;
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]);
163                 lock=buf[0] &0x06;
164
165                 if (lock==6)
166                         break;
167
168                 tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
169                 udelay(1000);
170         }
171         return lock;
172 }
173
174 static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
175 {
176         struct tuner *t = i2c_get_clientdata(c);
177         unsigned char buf[2];
178         int tad1;
179
180         buf[0]=0x0f;
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]);
184         tad1=buf[0]&0x07;
185
186         if(tad1 ==0) return lock;
187         if(tad1 ==1) return lock;
188
189         if(tad1==2) {
190                 if(sel==0)
191                         return lock;
192                 else sel--;
193         }
194         else {
195                 if(sel<4)
196                         sel++;
197                 else
198                         return lock;
199         }
200
201         tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);
202
203         buf[0]=0x0f;
204         buf[1]=sel;
205         i2c_master_send(c,buf,2);
206         lock=mt2032_check_lo_lock(c);
207         return lock;
208 }
209
210
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)
214 {
215         unsigned char buf[21];
216         int lint_try,ret,sel,lock=0;
217         struct tuner *t = i2c_get_clientdata(c);
218
219         tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",
220                   rfin,if1,if2,from,to);
221
222         buf[0]=0;
223         ret=i2c_master_send(c,buf,1);
224         i2c_master_recv(c,buf,21);
225
226         buf[0]=0;
227         ret=mt2032_compute_freq(c,rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
228         if (ret<0)
229                 return;
230
231         // send only the relevant registers per Rev. 1.2
232         buf[0]=0;
233         ret=i2c_master_send(c,buf,4);
234         buf[5]=5;
235         ret=i2c_master_send(c,buf+5,4);
236         buf[11]=11;
237         ret=i2c_master_send(c,buf+11,3);
238         if(ret!=3)
239                 tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);
240
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);
244
245                 if(optimize_vco)
246                         lock=mt2032_optimize_vco(c,sel,lock);
247                 if(lock==6) break;
248
249                 tuner_dbg("mt2032: re-init PLLs by LINT\n");
250                 buf[0]=7;
251                 buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
252                 i2c_master_send(c,buf,2);
253                 mdelay(10);
254                 buf[1]=8+t->xogc;
255                 i2c_master_send(c,buf,2);
256         }
257
258         if (lock!=6)
259                 tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");
260
261         buf[0]=2;
262         buf[1]=0x20; // LOGC for optimal phase noise
263         ret=i2c_master_send(c,buf,2);
264         if (ret!=2)
265                 tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
266 }
267
268
269 static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
270 {
271         struct tuner *t = i2c_get_clientdata(c);
272         int if2,from,to;
273
274         // signal bandwidth and picture carrier
275         if (t->std & V4L2_STD_525_60) {
276                 // NTSC
277                 from = 40750*1000;
278                 to   = 46750*1000;
279                 if2  = 45750*1000;
280         } else {
281                 // PAL
282                 from = 32900*1000;
283                 to   = 39900*1000;
284                 if2  = 38900*1000;
285         }
286
287         mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
288                            1090*1000*1000, if2, from, to);
289 }
290
291 static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
292 {
293         struct tuner *t = i2c_get_clientdata(c);
294         int if2 = t->radio_if2;
295
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);
299 }
300
301 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
302 static int mt2032_init(struct i2c_client *c)
303 {
304         struct tuner *t = i2c_get_clientdata(c);
305         unsigned char buf[21];
306         int ret,xogc,xok=0;
307
308         // Initialize Registers per spec.
309         buf[1]=2; // Index to register 2
310         buf[2]=0xff;
311         buf[3]=0x0f;
312         buf[4]=0x1f;
313         ret=i2c_master_send(c,buf+1,4);
314
315         buf[5]=6; // Index register 6
316         buf[6]=0xe4;
317         buf[7]=0x8f;
318         buf[8]=0xc3;
319         buf[9]=0x4e;
320         buf[10]=0xec;
321         ret=i2c_master_send(c,buf+5,6);
322
323         buf[12]=13;  // Index register 13
324         buf[13]=0x32;
325         ret=i2c_master_send(c,buf+12,2);
326
327         // Adjust XOGC (register 7), wait for XOK
328         xogc=7;
329         do {
330                 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
331                 mdelay(10);
332                 buf[0]=0x0e;
333                 i2c_master_send(c,buf,1);
334                 i2c_master_recv(c,buf,1);
335                 xok=buf[0]&0x01;
336                 tuner_dbg("mt2032: xok = 0x%02x\n",xok);
337                 if (xok == 1) break;
338
339                 xogc--;
340                 tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);
341                 if (xogc == 3) {
342                         xogc=4; // min. 4 per spec
343                         break;
344                 }
345                 buf[0]=0x07;
346                 buf[1]=0x88 + xogc;
347                 ret=i2c_master_send(c,buf,2);
348                 if (ret!=2)
349                         tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);
350         } while (xok != 1 );
351         t->xogc=xogc;
352
353         t->tv_freq    = mt2032_set_tv_freq;
354         t->radio_freq = mt2032_set_radio_freq;
355         return(1);
356 }
357
358 static void mt2050_set_antenna(struct i2c_client *c, unsigned char antenna)
359 {
360         struct tuner *t = i2c_get_clientdata(c);
361        unsigned char buf[2];
362        int ret;
363
364        buf[0] = 6;
365        buf[1] = antenna ? 0x11 : 0x10;
366        ret=i2c_master_send(c,buf,2);
367        tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);
368 }
369
370 static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
371 {
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;
375         int ret;
376         unsigned char buf[6];
377
378         tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",
379                   freq,if1,if2);
380
381         f_lo1=freq+if1;
382         f_lo1=(f_lo1/1000000)*1000000;
383
384         f_lo2=f_lo1-freq-if2;
385         f_lo2=(f_lo2/50000)*50000;
386
387         lo1=f_lo1/4000000;
388         lo2=f_lo2/4000000;
389
390         f_lo1_modulo= f_lo1-(lo1*4000000);
391         f_lo2_modulo= f_lo2-(lo2*4000000);
392
393         num1=4*f_lo1_modulo/4000000;
394         num2=4096*(f_lo2_modulo/1000)/4000;
395
396         // todo spurchecks
397
398         div1a=(lo1/12)-1;
399         div1b=lo1-(div1a+1)*12;
400
401         div2a=(lo2/8)-1;
402         div2b=lo2-(div2a+1)*8;
403
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);
408         }
409
410         buf[0]=1;
411         buf[1]= 4*div1b + num1;
412         if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
413
414         buf[2]=div1a;
415         buf[3]=32*div2b + num2/256;
416         buf[4]=num2-(num2/256)*256;
417         buf[5]=div2a;
418         if(num2!=0) buf[5]=buf[5]|0x40;
419
420         if (tuner_debug > 1) {
421                 int i;
422                 tuner_dbg("bufs is: ");
423                 for(i=0;i<6;i++)
424                         printk("%x ",buf[i]);
425                 printk("\n");
426         }
427
428         ret=i2c_master_send(c,buf,6);
429         if (ret!=6)
430                 tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);
431 }
432
433 static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
434 {
435         struct tuner *t = i2c_get_clientdata(c);
436         unsigned int if2;
437
438         if (t->std & V4L2_STD_525_60) {
439                 // NTSC
440                 if2 = 45750*1000;
441         } else {
442                 // PAL
443                 if2 = 38900*1000;
444         }
445         if (V4L2_TUNER_DIGITAL_TV == t->mode) {
446                 // DVB (pinnacle 300i)
447                 if2 = 36150*1000;
448         }
449         mt2050_set_if_freq(c, freq*62500, if2);
450         mt2050_set_antenna(c, tv_antenna);
451 }
452
453 static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
454 {
455         struct tuner *t = i2c_get_clientdata(c);
456         int if2 = t->radio_if2;
457
458         mt2050_set_if_freq(c, freq*62500, if2);
459         mt2050_set_antenna(c, radio_antenna);
460 }
461
462 static int mt2050_init(struct i2c_client *c)
463 {
464         struct tuner *t = i2c_get_clientdata(c);
465         unsigned char buf[2];
466         int ret;
467
468         buf[0]=6;
469         buf[1]=0x10;
470         ret=i2c_master_send(c,buf,2); //  power
471
472         buf[0]=0x0f;
473         buf[1]=0x0f;
474         ret=i2c_master_send(c,buf,2); // m1lo
475
476         buf[0]=0x0d;
477         ret=i2c_master_send(c,buf,1);
478         i2c_master_recv(c,buf,1);
479
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;
483         return 0;
484 }
485
486 int microtune_init(struct i2c_client *c)
487 {
488         struct tuner *t = i2c_get_clientdata(c);
489         char *name;
490         unsigned char buf[21];
491         int company_code;
492
493         memset(buf,0,sizeof(buf));
494         t->tv_freq    = NULL;
495         t->radio_freq = NULL;
496         t->standby    = NULL;
497         name = "unknown";
498
499         i2c_master_send(c,buf,1);
500         i2c_master_recv(c,buf,21);
501         if (tuner_debug) {
502                 int i;
503                 tuner_dbg("MT20xx hexdump:");
504                 for(i=0;i<21;i++) {
505                         printk(" %02x",buf[i]);
506                         if(((i+1)%8)==0) printk(" ");
507                 }
508                 printk("\n");
509         }
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]);
513
514
515         if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
516             NULL != microtune_part[buf[0x13]])
517                 name = microtune_part[buf[0x13]];
518         switch (buf[0x13]) {
519         case MT2032:
520                 mt2032_init(c);
521                 break;
522         case MT2050:
523                 mt2050_init(c);
524                 break;
525         default:
526                 tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",
527                            name);
528                 return 0;
529         }
530
531         strlcpy(c->name, name, sizeof(c->name));
532         tuner_info("microtune %s found, OK\n",name);
533         return 0;
534 }
535
536 /*
537  * Overrides for Emacs so that we follow Linus's tabbing style.
538  * ---------------------------------------------------------------------------
539  * Local variables:
540  * c-basic-offset: 8
541  * End:
542  */