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