patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / media / video / tuner.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/sched.h>
4 #include <linux/string.h>
5 #include <linux/timer.h>
6 #include <linux/delay.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/poll.h>
10 #include <linux/i2c.h>
11 #include <linux/types.h>
12 #include <linux/videodev.h>
13 #include <linux/init.h>
14
15 #include <media/tuner.h>
16 #include <media/audiochip.h>
17
18 /* Addresses to scan */
19 static unsigned short normal_i2c[] = {I2C_CLIENT_END};
20 static unsigned short normal_i2c_range[] = {0x60,0x6f,I2C_CLIENT_END};
21 I2C_CLIENT_INSMOD;
22
23 #define UNSET (-1U)
24
25 /* insmod options */
26 static unsigned int debug =  0;
27 static unsigned int type  =  UNSET;
28 static unsigned int addr  =  0;
29 static char *pal =  "b";
30 static unsigned int tv_range[2]    = { 44, 958 };
31 static unsigned int radio_range[2] = { 65, 108 };
32 MODULE_PARM(debug,"i");
33 MODULE_PARM(type,"i");
34 MODULE_PARM(addr,"i");
35 MODULE_PARM(tv_range,"2i");
36 MODULE_PARM(radio_range,"2i");
37 MODULE_PARM(pal,"s");
38
39 #define optimize_vco 1
40
41 MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
42 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
43 MODULE_LICENSE("GPL");
44
45 static int this_adap;
46 #define dprintk     if (debug) printk
47
48 struct tuner {
49         unsigned int type;            /* chip type */
50         unsigned int freq;            /* keep track of the current settings */
51         unsigned int std;
52         
53         unsigned int radio;
54         unsigned int mode;            /* current norm for multi-norm tuners */
55         unsigned int input;
56         
57         // only for MT2032
58         unsigned int xogc;
59         unsigned int radio_if2;
60
61         void (*tv_freq)(struct i2c_client *c, unsigned int freq);
62         void (*radio_freq)(struct i2c_client *c, unsigned int freq);
63 };
64
65 static struct i2c_driver driver;
66 static struct i2c_client client_template;
67
68 /* ---------------------------------------------------------------------- */
69
70 /* tv standard selection for Temic 4046 FM5
71    this value takes the low bits of control byte 2
72    from datasheet Rev.01, Feb.00 
73      standard     BG      I       L       L2      D
74      picture IF   38.9    38.9    38.9    33.95   38.9
75      sound 1      33.4    32.9    32.4    40.45   32.4
76      sound 2      33.16   
77      NICAM        33.05   32.348  33.05           33.05
78  */
79 #define TEMIC_SET_PAL_I         0x05
80 #define TEMIC_SET_PAL_DK        0x09
81 #define TEMIC_SET_PAL_L         0x0a // SECAM ?
82 #define TEMIC_SET_PAL_L2        0x0b // change IF !
83 #define TEMIC_SET_PAL_BG        0x0c
84
85 /* tv tuner system standard selection for Philips FQ1216ME
86    this value takes the low bits of control byte 2
87    from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
88      standard           BG      DK      I       L       L`
89      picture carrier    38.90   38.90   38.90   38.90   33.95
90      colour             34.47   34.47   34.47   34.47   38.38
91      sound 1            33.40   32.40   32.90   32.40   40.45
92      sound 2            33.16   -       -       -       -
93      NICAM              33.05   33.05   32.35   33.05   39.80
94  */
95 #define PHILIPS_SET_PAL_I       0x01 /* Bit 2 always zero !*/
96 #define PHILIPS_SET_PAL_BGDK    0x09
97 #define PHILIPS_SET_PAL_L2      0x0a
98 #define PHILIPS_SET_PAL_L       0x0b    
99
100 /* system switching for Philips FI1216MF MK2
101    from datasheet "1996 Jul 09",
102     standard         BG     L      L'
103     picture carrier  38.90  38.90  33.95
104     colour           34.47  34.37  38.38
105     sound 1          33.40  32.40  40.45
106     sound 2          33.16  -      -
107     NICAM            33.05  33.05  39.80
108  */
109 #define PHILIPS_MF_SET_BG       0x01 /* Bit 2 must be zero, Bit 3 is system output */
110 #define PHILIPS_MF_SET_PAL_L    0x03 // France
111 #define PHILIPS_MF_SET_PAL_L2   0x02 // L'
112
113
114 /* ---------------------------------------------------------------------- */
115
116 struct tunertype 
117 {
118         char *name;
119         unsigned char Vendor;
120         unsigned char Type;
121   
122         unsigned short thresh1;  /*  band switch VHF_LO <=> VHF_HI  */
123         unsigned short thresh2;  /*  band switch VHF_HI <=> UHF     */
124         unsigned char VHF_L;
125         unsigned char VHF_H;
126         unsigned char UHF;
127         unsigned char config; 
128         unsigned short IFPCoff; /* 622.4=16*38.90 MHz PAL, 
129                                    732  =16*45.75 NTSCi, 
130                                    940  =58.75 NTSC-Japan */
131 };
132
133 /*
134  *      The floats in the tuner struct are computed at compile time
135  *      by gcc and cast back to integers. Thus we don't violate the
136  *      "no float in kernel" rule.
137  */
138 static struct tunertype tuners[] = {
139         { "Temic PAL (4002 FH5)", TEMIC, PAL,
140           16*140.25,16*463.25,0x02,0x04,0x01,0x8e,623},
141         { "Philips PAL_I (FI1246 and compatibles)", Philips, PAL_I,
142           16*140.25,16*463.25,0xa0,0x90,0x30,0x8e,623},
143         { "Philips NTSC (FI1236,FM1236 and compatibles)", Philips, NTSC,
144           16*157.25,16*451.25,0xA0,0x90,0x30,0x8e,732},
145         { "Philips (SECAM+PAL_BG) (FI1216MF, FM1216MF, FR1216MF)", Philips, SECAM,
146           16*168.25,16*447.25,0xA7,0x97,0x37,0x8e,623},
147
148         { "NoTuner", NoTuner, NOTUNER,
149           0,0,0x00,0x00,0x00,0x00,0x00},
150         { "Philips PAL_BG (FI1216 and compatibles)", Philips, PAL,
151           16*168.25,16*447.25,0xA0,0x90,0x30,0x8e,623},
152         { "Temic NTSC (4032 FY5)", TEMIC, NTSC,
153           16*157.25,16*463.25,0x02,0x04,0x01,0x8e,732},
154         { "Temic PAL_I (4062 FY5)", TEMIC, PAL_I,
155           16*170.00,16*450.00,0x02,0x04,0x01,0x8e,623},
156
157         { "Temic NTSC (4036 FY5)", TEMIC, NTSC,
158           16*157.25,16*463.25,0xa0,0x90,0x30,0x8e,732},
159         { "Alps HSBH1", TEMIC, NTSC,
160           16*137.25,16*385.25,0x01,0x02,0x08,0x8e,732},
161         { "Alps TSBE1",TEMIC,PAL,
162           16*137.25,16*385.25,0x01,0x02,0x08,0x8e,732},
163         { "Alps TSBB5", Alps, PAL_I, /* tested (UK UHF) with Modulartech MM205 */
164           16*133.25,16*351.25,0x01,0x02,0x08,0x8e,632},
165
166         { "Alps TSBE5", Alps, PAL, /* untested - data sheet guess. Only IF differs. */
167           16*133.25,16*351.25,0x01,0x02,0x08,0x8e,622},
168         { "Alps TSBC5", Alps, PAL, /* untested - data sheet guess. Only IF differs. */
169           16*133.25,16*351.25,0x01,0x02,0x08,0x8e,608},
170         { "Temic PAL_BG (4006FH5)", TEMIC, PAL,
171           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623}, 
172         { "Alps TSCH6",Alps,NTSC,
173           16*137.25,16*385.25,0x14,0x12,0x11,0x8e,732},
174
175         { "Temic PAL_DK (4016 FY5)",TEMIC,PAL,
176           16*168.25,16*456.25,0xa0,0x90,0x30,0x8e,623},
177         { "Philips NTSC_M (MK2)",Philips,NTSC,
178           16*160.00,16*454.00,0xa0,0x90,0x30,0x8e,732},
179         { "Temic PAL_I (4066 FY5)", TEMIC, PAL_I,
180           16*169.00, 16*454.00, 0xa0,0x90,0x30,0x8e,623},
181         { "Temic PAL* auto (4006 FN5)", TEMIC, PAL,
182           16*169.00, 16*454.00, 0xa0,0x90,0x30,0x8e,623},
183
184         { "Temic PAL_BG (4009 FR5) or PAL_I (4069 FR5)", TEMIC, PAL,
185           16*141.00, 16*464.00, 0xa0,0x90,0x30,0x8e,623},
186         { "Temic NTSC (4039 FR5)", TEMIC, NTSC,
187           16*158.00, 16*453.00, 0xa0,0x90,0x30,0x8e,732},
188         { "Temic PAL/SECAM multi (4046 FM5)", TEMIC, PAL,
189           16*169.00, 16*454.00, 0xa0,0x90,0x30,0x8e,623},
190         { "Philips PAL_DK (FI1256 and compatibles)", Philips, PAL,
191           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
192
193         { "Philips PAL/SECAM multi (FQ1216ME)", Philips, PAL,
194           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
195         { "LG PAL_I+FM (TAPC-I001D)", LGINNOTEK, PAL_I,
196           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
197         { "LG PAL_I (TAPC-I701D)", LGINNOTEK, PAL_I,
198           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
199         { "LG NTSC+FM (TPI8NSR01F)", LGINNOTEK, NTSC,
200           16*210.00,16*497.00,0xa0,0x90,0x30,0x8e,732},
201
202         { "LG PAL_BG+FM (TPI8PSB01D)", LGINNOTEK, PAL,
203           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
204         { "LG PAL_BG (TPI8PSB11D)", LGINNOTEK, PAL,
205           16*170.00,16*450.00,0xa0,0x90,0x30,0x8e,623},
206         { "Temic PAL* auto + FM (4009 FN5)", TEMIC, PAL,
207           16*141.00, 16*464.00, 0xa0,0x90,0x30,0x8e,623},
208         { "SHARP NTSC_JP (2U5JF5540)", SHARP, NTSC, /* 940=16*58.75 NTSC@Japan */
209           16*137.25,16*317.25,0x01,0x02,0x08,0x8e,732 }, // Corrected to NTSC=732 (was:940)
210
211         { "Samsung PAL TCPM9091PD27", Samsung, PAL,  /* from sourceforge v3tv */
212           16*169,16*464,0xA0,0x90,0x30,0x8e,623},
213         { "MT20xx universal", Microtune,PAL|NTSC,
214                0,0,0,0,0,0,0},
215         { "Temic PAL_BG (4106 FH5)", TEMIC, PAL,
216           16*141.00, 16*464.00, 0xa0,0x90,0x30,0x8e,623},
217         { "Temic PAL_DK/SECAM_L (4012 FY5)", TEMIC, PAL,
218           16*140.25, 16*463.25, 0x02,0x04,0x01,0x8e,623},
219
220         { "Temic NTSC (4136 FY5)", TEMIC, NTSC,
221           16*158.00, 16*453.00, 0xa0,0x90,0x30,0x8e,732},
222         { "LG PAL (newer TAPC series)", LGINNOTEK, PAL,
223           16*170.00, 16*450.00, 0x01,0x02,0x08,0x8e,623},
224         { "Philips PAL/SECAM multi (FM1216ME MK3)", Philips, PAL,
225           16*160.00,16*442.00,0x01,0x02,0x04,0x8e,623 },
226         { "LG NTSC (newer TAPC series)", LGINNOTEK, NTSC,
227           16*170.00, 16*450.00, 0x01,0x02,0x08,0x8e,732},
228
229         { "HITACHI V7-J180AT", HITACHI, NTSC,
230           16*170.00, 16*450.00, 0x01,0x02,0x00,0x8e,940 },
231         { "Philips PAL_MK (FI1216 MK)", Philips, PAL,
232           16*140.25,16*463.25,0x01,0xc2,0xcf,0x8e,623},
233         { "Philips 1236D ATSC/NTSC daul in",Philips,ATSC,
234           16*157.25,16*454.00,0xa0,0x90,0x30,0x8e,732},
235         { "Philips NTSC MK3 (FM1236MK3 or FM1236/F)", Philips, NTSC,
236           16*160.00,16*442.00,0x01,0x02,0x04,0x8e,732},
237
238         { "Philips 4 in 1 (ATI TV Wonder Pro/Conexant)", Philips, NTSC,
239           16*160.00,16*442.00,0x01,0x02,0x04,0x8e,732},
240         { "Microtune 4049 FM5",Microtune,PAL,
241           16*141.00,16*464.00,0xa0,0x90,0x30,0x8e,623},
242
243 };
244 #define TUNERS ARRAY_SIZE(tuners)
245
246 /* ---------------------------------------------------------------------- */
247
248 static int tuner_getstatus(struct i2c_client *c)
249 {
250         unsigned char byte;
251
252         struct tuner *t = i2c_get_clientdata(c);
253
254         if (t->type == TUNER_MT2032)
255                 return 0;
256
257         if (1 != i2c_master_recv(c,&byte,1))
258                 return 0;
259         return byte;
260 }
261
262 #define TUNER_POR       0x80
263 #define TUNER_FL        0x40
264 #define TUNER_MODE      0x38
265 #define TUNER_AFC       0x07
266
267 #define TUNER_STEREO    0x10 /* radio mode */
268 #define TUNER_SIGNAL    0x07 /* radio mode */
269
270 static int tuner_signal(struct i2c_client *c)
271 {
272         return (tuner_getstatus(c) & TUNER_SIGNAL)<<13;
273 }
274
275 static int tuner_stereo(struct i2c_client *c)
276 {
277         return (tuner_getstatus (c) & TUNER_STEREO);
278 }
279
280 #if 0 /* unused */
281 static int tuner_islocked (struct i2c_client *c)
282 {
283         return (tuner_getstatus (c) & TUNER_FL);
284 }
285
286 static int tuner_afcstatus (struct i2c_client *c)
287 {
288         return (tuner_getstatus (c) & TUNER_AFC) - 2;
289 }
290
291 static int tuner_mode (struct i2c_client *c)
292 {
293         return (tuner_getstatus (c) & TUNER_MODE) >> 3;
294 }
295 #endif
296
297 /* ---------------------------------------------------------------------- */
298
299 #define MT2032 0x04
300 #define MT2030 0x06
301 #define MT2040 0x07
302 #define MT2050 0x42
303
304 static char *microtune_part[] = {
305         [ MT2030 ] = "MT2030",
306         [ MT2032 ] = "MT2032",
307         [ MT2040 ] = "MT2040",
308         [ MT2050 ] = "MT2050",
309 };
310
311 // IsSpurInBand()?
312 static int mt2032_spurcheck(int f1, int f2, int spectrum_from,int spectrum_to)
313 {
314         int n1=1,n2,f;
315
316         f1=f1/1000; //scale to kHz to avoid 32bit overflows
317         f2=f2/1000;
318         spectrum_from/=1000;
319         spectrum_to/=1000;
320
321         dprintk("spurcheck f1=%d f2=%d  from=%d to=%d\n",f1,f2,spectrum_from,spectrum_to);
322
323         do {
324             n2=-n1;
325             f=n1*(f1-f2);
326             do {
327                 n2--;
328                 f=f-f2;
329                 dprintk(" spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
330
331                 if( (f>spectrum_from) && (f<spectrum_to))
332                         printk("mt2032 spurcheck triggered: %d\n",n1);
333             } while ( (f>(f2-spectrum_to)) || (n2>-5));
334             n1++;
335         } while (n1<5);
336
337         return 1;
338 }
339
340 static int mt2032_compute_freq(unsigned int rfin,
341                                unsigned int if1, unsigned int if2,
342                                unsigned int spectrum_from,
343                                unsigned int spectrum_to,
344                                unsigned char *buf,
345                                int *ret_sel,
346                                unsigned int xogc) //all in Hz
347 {
348         unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
349                 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
350
351         fref= 5250 *1000; //5.25MHz
352         desired_lo1=rfin+if1;
353
354         lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
355         lo1n=lo1/8;
356         lo1a=lo1-(lo1n*8);
357
358         s=rfin/1000/1000+1090;
359
360         if(optimize_vco) {
361                 if(s>1890) sel=0;
362                 else if(s>1720) sel=1;
363                 else if(s>1530) sel=2;
364                 else if(s>1370) sel=3;
365                 else sel=4; // >1090
366         }
367         else {
368                 if(s>1790) sel=0; // <1958
369                 else if(s>1617) sel=1;
370                 else if(s>1449) sel=2;
371                 else if(s>1291) sel=3;
372                 else sel=4; // >1090
373         }
374         *ret_sel=sel;
375
376         lo1freq=(lo1a+8*lo1n)*fref;
377
378         dprintk("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
379                 rfin,lo1,lo1n,lo1a,sel,lo1freq);
380
381         desired_lo2=lo1freq-rfin-if2;
382         lo2=(desired_lo2)/fref;
383         lo2n=lo2/8;
384         lo2a=lo2-(lo2n*8);
385         lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
386         lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
387
388         dprintk("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
389                 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
390
391         if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
392                 printk("mt2032: frequency parameters out of range: %d %d %d %d\n",
393                        lo1a, lo1n, lo2a,lo2n);
394                 return(-1);
395         }
396
397         mt2032_spurcheck(lo1freq, desired_lo2,  spectrum_from, spectrum_to);
398         // should recalculate lo1 (one step up/down)
399
400         // set up MT2032 register map for transfer over i2c
401         buf[0]=lo1n-1;
402         buf[1]=lo1a | (sel<<4);
403         buf[2]=0x86; // LOGC
404         buf[3]=0x0f; //reserved
405         buf[4]=0x1f;
406         buf[5]=(lo2n-1) | (lo2a<<5);
407         if(rfin >400*1000*1000)
408                 buf[6]=0xe4;
409         else
410                 buf[6]=0xf4; // set PKEN per rev 1.2 
411         buf[7]=8+xogc;
412         buf[8]=0xc3; //reserved
413         buf[9]=0x4e; //reserved
414         buf[10]=0xec; //reserved
415         buf[11]=(lo2num&0xff);
416         buf[12]=(lo2num>>8) |0x80; // Lo2RST
417
418         return 0;
419 }
420
421 static int mt2032_check_lo_lock(struct i2c_client *c)
422 {
423         int try,lock=0;
424         unsigned char buf[2];
425         for(try=0;try<10;try++) {
426                 buf[0]=0x0e;
427                 i2c_master_send(c,buf,1);
428                 i2c_master_recv(c,buf,1);
429                 dprintk("mt2032 Reg.E=0x%02x\n",buf[0]);
430                 lock=buf[0] &0x06;
431                 
432                 if (lock==6)
433                         break;
434                 
435                 dprintk("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
436                 udelay(1000);
437         }
438         return lock;
439 }
440
441 static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
442 {
443         unsigned char buf[2];
444         int tad1;
445
446         buf[0]=0x0f;
447         i2c_master_send(c,buf,1);
448         i2c_master_recv(c,buf,1);
449         dprintk("mt2032 Reg.F=0x%02x\n",buf[0]);
450         tad1=buf[0]&0x07;
451
452         if(tad1 ==0) return lock;
453         if(tad1 ==1) return lock;
454
455         if(tad1==2) {
456                 if(sel==0) 
457                         return lock;
458                 else sel--;
459         }
460         else {
461                 if(sel<4)
462                         sel++;
463                 else
464                         return lock;
465         }
466
467         dprintk("mt2032 optimize_vco: sel=%d\n",sel);
468
469         buf[0]=0x0f;
470         buf[1]=sel;
471         i2c_master_send(c,buf,2);
472         lock=mt2032_check_lo_lock(c);
473         return lock;
474 }
475
476
477 static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin,
478                                unsigned int if1, unsigned int if2,
479                                unsigned int from, unsigned int to)
480 {
481         unsigned char buf[21];
482         int lint_try,ret,sel,lock=0;
483         struct tuner *t = i2c_get_clientdata(c);
484
485         dprintk("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",rfin,if1,if2,from,to);
486
487         buf[0]=0;
488         ret=i2c_master_send(c,buf,1);
489         i2c_master_recv(c,buf,21);
490
491         buf[0]=0;
492         ret=mt2032_compute_freq(rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
493         if (ret<0)
494                 return;
495
496         // send only the relevant registers per Rev. 1.2
497         buf[0]=0;
498         ret=i2c_master_send(c,buf,4);
499         buf[5]=5;
500         ret=i2c_master_send(c,buf+5,4);
501         buf[11]=11;
502         ret=i2c_master_send(c,buf+11,3);
503         if(ret!=3)
504                 printk("mt2032_set_if_freq failed with %d\n",ret);
505
506         // wait for PLLs to lock (per manual), retry LINT if not.
507         for(lint_try=0; lint_try<2; lint_try++) {
508                 lock=mt2032_check_lo_lock(c);
509                 
510                 if(optimize_vco)
511                         lock=mt2032_optimize_vco(c,sel,lock);
512                 if(lock==6) break;
513                 
514                 printk("mt2032: re-init PLLs by LINT\n"); 
515                 buf[0]=7; 
516                 buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
517                 i2c_master_send(c,buf,2);
518                 mdelay(10);
519                 buf[1]=8+t->xogc;
520                 i2c_master_send(c,buf,2);
521         }
522
523         if (lock!=6)
524                 printk("MT2032 Fatal Error: PLLs didn't lock.\n");
525
526         buf[0]=2;
527         buf[1]=0x20; // LOGC for optimal phase noise
528         ret=i2c_master_send(c,buf,2);
529         if (ret!=2)
530                 printk("mt2032_set_if_freq2 failed with %d\n",ret);
531 }
532
533
534 static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
535 {
536         struct tuner *t = i2c_get_clientdata(c);
537         int if2,from,to;
538
539         // signal bandwidth and picture carrier
540         if (t->mode == VIDEO_MODE_NTSC) {
541                 from=40750*1000;
542                 to=46750*1000;
543                 if2=45750*1000; 
544         } else {
545                 // Pal 
546                 from=32900*1000;
547                 to=39900*1000;
548                 if2=38900*1000;
549         }
550
551         mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
552                            1090*1000*1000, if2, from, to);
553 }
554
555 static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
556 {
557         struct tuner *t = i2c_get_clientdata(c);
558         int if2 = t->radio_if2;
559
560         // per Manual for FM tuning: first if center freq. 1085 MHz
561         mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
562                            1085*1000*1000,if2,if2,if2);
563 }
564
565 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
566 static int mt2032_init(struct i2c_client *c)
567 {
568         struct tuner *t = i2c_get_clientdata(c);
569         unsigned char buf[21];
570         int ret,xogc,xok=0;
571
572         // Initialize Registers per spec.
573         buf[1]=2; // Index to register 2
574         buf[2]=0xff;
575         buf[3]=0x0f;
576         buf[4]=0x1f;
577         ret=i2c_master_send(c,buf+1,4);
578
579         buf[5]=6; // Index register 6
580         buf[6]=0xe4;
581         buf[7]=0x8f;
582         buf[8]=0xc3;
583         buf[9]=0x4e;
584         buf[10]=0xec;
585         ret=i2c_master_send(c,buf+5,6);
586
587         buf[12]=13;  // Index register 13
588         buf[13]=0x32;
589         ret=i2c_master_send(c,buf+12,2);
590
591         // Adjust XOGC (register 7), wait for XOK
592         xogc=7;
593         do {
594                 dprintk("mt2032: xogc = 0x%02x\n",xogc&0x07);
595                 mdelay(10);
596                 buf[0]=0x0e;
597                 i2c_master_send(c,buf,1);
598                 i2c_master_recv(c,buf,1);
599                 xok=buf[0]&0x01;
600                 dprintk("mt2032: xok = 0x%02x\n",xok);
601                 if (xok == 1) break;
602
603                 xogc--;
604                 dprintk("mt2032: xogc = 0x%02x\n",xogc&0x07);
605                 if (xogc == 3) {
606                         xogc=4; // min. 4 per spec
607                         break;
608                 }
609                 buf[0]=0x07;
610                 buf[1]=0x88 + xogc;
611                 ret=i2c_master_send(c,buf,2);
612                 if (ret!=2)
613                         printk("mt2032_init failed with %d\n",ret);
614         } while (xok != 1 );
615         t->xogc=xogc;
616
617         t->tv_freq    = mt2032_set_tv_freq;
618         t->radio_freq = mt2032_set_radio_freq;
619         return(1);
620 }
621
622 static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
623 {
624         unsigned int if1=1218*1000*1000;
625         unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
626         int ret;
627         unsigned char buf[6];
628         
629         dprintk("mt2050_set_if_freq freq=%d\n",freq);
630         
631         f_lo1=freq+if1;
632         f_lo1=(f_lo1/1000000)*1000000;
633         
634         f_lo2=f_lo1-freq-if2;
635         f_lo2=(f_lo2/50000)*50000;
636         
637         lo1=f_lo1/4000000;
638         lo2=f_lo2/4000000;
639         
640         f_lo1_modulo= f_lo1-(lo1*4000000);
641         f_lo2_modulo= f_lo2-(lo2*4000000);
642         
643         num1=4*f_lo1_modulo/4000000;
644         num2=4096*(f_lo2_modulo/1000)/4000;
645         
646         // todo spurchecks
647         
648         div1a=(lo1/12)-1;
649         div1b=lo1-(div1a+1)*12;
650         
651         div2a=(lo2/8)-1;
652         div2b=lo2-(div2a+1)*8;
653         
654         dprintk("lo1 lo2 = %d %d\n", lo1, lo2);
655         dprintk("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",num1,num2,div1a,div1b,div2a,div2b);
656         
657         
658         buf[0]=1;
659         buf[1]= 4*div1b + num1;
660         if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
661         
662         buf[2]=div1a;
663         buf[3]=32*div2b + num2/256;
664         buf[4]=num2-(num2/256)*256;
665         buf[5]=div2a;
666         if(num2!=0) buf[5]=buf[5]|0x40;
667         
668         if(debug) {
669                 int i;
670                 printk("bufs is: ");
671                 for(i=0;i<6;i++)
672                         printk("%x ",buf[i]);
673                 printk("\n");
674         }
675         
676         ret=i2c_master_send(c,buf,6);
677         if (ret!=6)
678                 printk("mt2050_set_if_freq failed with %d\n",ret);
679 }
680
681 static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
682 {
683         struct tuner *t = i2c_get_clientdata(c);
684         unsigned int if2;
685         
686         if (t->mode == VIDEO_MODE_NTSC) {
687                 if2=45750*1000;
688         } else {
689                 // Pal
690                 if2=38900*1000;
691         }
692         mt2050_set_if_freq(c,freq*62500,if2);
693 }
694
695 static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
696 {
697         struct tuner *t = i2c_get_clientdata(c);
698         int if2 = t->radio_if2;
699         
700         mt2050_set_if_freq(c, freq*62500, if2);
701 }
702
703 static int mt2050_init(struct i2c_client *c)
704 {
705         struct tuner *t = i2c_get_clientdata(c);
706         unsigned char buf[2];
707         int ret;
708         
709         buf[0]=6;
710         buf[1]=0x10;
711         ret=i2c_master_send(c,buf,2); //  power
712         
713         buf[0]=0x0f;
714         buf[1]=0x0f;
715         ret=i2c_master_send(c,buf,2); // m1lo
716         
717         buf[0]=0x0d;
718         ret=i2c_master_send(c,buf,1);
719         i2c_master_recv(c,buf,1);
720         
721         dprintk("mt2050: sro is %x\n",buf[0]);
722         t->tv_freq    = mt2050_set_tv_freq;
723         t->radio_freq = mt2050_set_radio_freq;
724         return 0;
725 }
726
727 static int microtune_init(struct i2c_client *c)
728 {
729         struct tuner *t = i2c_get_clientdata(c);
730         char *name;
731         unsigned char buf[21];
732         int company_code;
733         
734         buf[0] = 0;
735         t->tv_freq = NULL;
736         t->radio_freq = NULL;
737         name = "unknown";
738
739         i2c_master_send(c,buf,1);
740         i2c_master_recv(c,buf,21);
741         if(debug) {
742                 int i;
743                 printk(KERN_DEBUG "tuner: MT2032 hexdump:\n");
744                 for(i=0;i<21;i++) {
745                         printk(" %02x",buf[i]);
746                         if(((i+1)%8)==0) printk(" ");
747                         if(((i+1)%16)==0) printk("\n ");
748                 }
749                 printk("\n ");
750         }
751         company_code = buf[0x11] << 8 | buf[0x12];
752         printk("tuner: microtune: companycode=%04x part=%02x rev=%02x\n",
753                company_code,buf[0x13],buf[0x14]);
754         switch (company_code) {
755         case 0x30bf:
756         case 0x3cbf:
757         case 0x3dbf:
758         case 0x4d54:
759         case 0x8e81:
760         case 0x8e91:
761                 /* ok (?) */
762                 break;
763         default:
764                 printk("tuner: microtune: unknown companycode\n");
765                 return 0;
766         }
767
768         if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
769             NULL != microtune_part[buf[0x13]])
770                 name = microtune_part[buf[0x13]];
771         switch (buf[0x13]) {
772         case MT2032:
773                 mt2032_init(c);
774                 break;
775         case MT2050:
776                 mt2050_init(c);
777                 break;
778         default:
779                 printk("tuner: microtune %s found, not (yet?) supported, sorry :-/\n",
780                        name);
781                 return 0;
782         }
783         printk("tuner: microtune %s found, OK\n",name);
784         return 0;
785 }
786
787 /* ---------------------------------------------------------------------- */
788
789 static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
790 {
791         struct tuner *t = i2c_get_clientdata(c);
792         u8 config;
793         u16 div;
794         struct tunertype *tun;
795         unsigned char buffer[4];
796         int rc;
797
798         tun=&tuners[t->type];
799         if (freq < tun->thresh1) 
800                 config = tun->VHF_L;
801         else if (freq < tun->thresh2) 
802                 config = tun->VHF_H;
803         else
804                 config = tun->UHF;
805
806
807         /* tv norm specific stuff for multi-norm tuners */
808         switch (t->type) {
809         case TUNER_PHILIPS_SECAM: // FI1216MF
810                 /* 0x01 -> ??? no change ??? */
811                 /* 0x02 -> PAL BDGHI / SECAM L */
812                 /* 0x04 -> ??? PAL others / SECAM others ??? */
813                 config &= ~0x02;
814                 if (t->mode == VIDEO_MODE_SECAM)
815                         config |= 0x02;
816                 break;
817
818         case TUNER_TEMIC_4046FM5:
819                 config &= ~0x0f;
820                 switch (pal[0]) {
821                 case 'i':
822                 case 'I':
823                         config |= TEMIC_SET_PAL_I;
824                         break;
825                 case 'd':
826                 case 'D':
827                         config |= TEMIC_SET_PAL_DK;
828                         break;
829                 case 'l':
830                 case 'L':
831                         config |= TEMIC_SET_PAL_L;
832                         break;
833                 case 'b':
834                 case 'B':
835                 case 'g':
836                 case 'G':
837                 default:
838                         config |= TEMIC_SET_PAL_BG;
839                         break;
840                 }
841                 break;
842
843         case TUNER_PHILIPS_FQ1216ME:
844                 config &= ~0x0f;
845                 switch (pal[0]) {
846                 case 'i':
847                 case 'I':
848                         config |= PHILIPS_SET_PAL_I;
849                         break;
850                 case 'l':
851                 case 'L':
852                         config |= PHILIPS_SET_PAL_L;
853                         break;
854                 case 'd':
855                 case 'D':
856                 case 'b':
857                 case 'B':
858                 case 'g':
859                 case 'G':
860                         config |= PHILIPS_SET_PAL_BGDK;
861                         break;
862                 }
863                 break;
864
865         case TUNER_PHILIPS_ATSC:
866                 /* 0x00 -> ATSC antenna input 1 */
867                 /* 0x01 -> ATSC antenna input 2 */
868                 /* 0x02 -> NTSC antenna input 1 */
869                 /* 0x03 -> NTSC antenna input 2 */
870                 
871                 config &= ~0x03;
872 #ifdef VIDEO_MODE_ATSC
873                 if (VIDEO_MODE_ATSC != t->mode)
874                         config |= 2;
875 #endif
876                 /* FIXME: input */
877                 break;
878         }
879
880         
881         /*
882          * Philips FI1216MK2 remark from specification :
883          * for channel selection involving band switching, and to ensure
884          * smooth tuning to the desired channel without causing
885          * unnecessary charge pump action, it is recommended to consider
886          * the difference between wanted channel frequency and the
887          * current channel frequency.  Unnecessary charge pump action
888          * will result in very low tuning voltage which may drive the
889          * oscillator to extreme conditions.
890          *
891          * Progfou: specification says to send config data before
892          * frequency in case (wanted frequency < current frequency).
893          */
894
895         div=freq + tun->IFPCoff;
896         if (t->type == TUNER_PHILIPS_SECAM && freq < t->freq) {
897                 buffer[0] = tun->config;
898                 buffer[1] = config;
899                 buffer[2] = (div>>8) & 0x7f;
900                 buffer[3] = div      & 0xff;
901         } else {
902                 buffer[0] = (div>>8) & 0x7f;
903                 buffer[1] = div      & 0xff;
904                 buffer[2] = tun->config;
905                 buffer[3] = config;
906         }
907         dprintk("tuner: tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
908                 buffer[0],buffer[1],buffer[2],buffer[3]);
909
910         if (4 != (rc = i2c_master_send(c,buffer,4)))
911                 printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc);
912
913 }
914
915 static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
916 {
917         struct tunertype *tun;
918         struct tuner *t = i2c_get_clientdata(c);
919         unsigned char buffer[4];
920         unsigned div;
921         int rc;
922
923         tun=&tuners[t->type];
924         div = freq + (int)(16*10.7);
925         buffer[0] = (div>>8) & 0x7f;
926         buffer[1] = div      & 0xff;
927         buffer[2] = tun->config;
928         switch (t->type) {
929         case TUNER_PHILIPS_FM1216ME_MK3:
930         case TUNER_PHILIPS_FM1236_MK3:
931                 buffer[3] = 0x19;
932                 break;
933         default:
934                 buffer[3] = 0xa4;
935                 break;
936         }
937
938         dprintk("tuner: radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
939                 buffer[0],buffer[1],buffer[2],buffer[3]);
940
941         if (4 != (rc = i2c_master_send(c,buffer,4)))
942                 printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc);
943 }
944
945 /* ---------------------------------------------------------------------- */
946
947 // Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz
948 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
949 {
950         struct tuner *t = i2c_get_clientdata(c);
951
952         if (t->type == UNSET) {
953                 printk("tuner: tuner type not set\n");
954                 return;
955         }
956         if (NULL == t->tv_freq) {
957                 printk("tuner: Huh? tv_set is NULL?\n");
958                 return;
959         }
960         if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
961                 /* FIXME: better do that chip-specific, but
962                    right now we don't have that in the config
963                    struct and this way is still better than no
964                    check at all */
965                 printk("tuner: TV freq (%d.%02d) out of range (%d-%d)\n",
966                        freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
967                 return;
968         }
969         t->tv_freq(c,freq);
970 }
971
972 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
973 {
974         struct tuner *t = i2c_get_clientdata(c);
975
976         if (t->type == UNSET) {
977                 printk("tuner: tuner type not set\n");
978                 return;
979         }
980         if (NULL == t->radio_freq) {
981                 printk("tuner: no radio tuning for this one, sorry.\n");
982                 return;
983         }
984         if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
985                 printk("tuner: radio freq (%d.%02d) out of range (%d-%d)\n",
986                        freq/16,freq%16*100/16,
987                        radio_range[0],radio_range[1]);
988                 return;
989         }
990         t->radio_freq(c,freq);
991 }
992
993 static void set_type(struct i2c_client *c, unsigned int type, char *source)
994 {
995         struct tuner *t = i2c_get_clientdata(c);
996
997         if (t->type != UNSET) {
998                 if (t->type != type)
999                         printk("tuner: type already set to %d, "
1000                                "ignoring request for %d\n", t->type, type);
1001                 return;
1002         }
1003         if (type >= TUNERS)
1004                 return;
1005
1006         t->type = type;
1007         printk("tuner: type set to %d (%s) by %s\n",
1008                t->type,tuners[t->type].name, source);
1009         strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
1010
1011         switch (t->type) {
1012         case TUNER_MT2032:
1013                 microtune_init(c);
1014                 break;
1015         default:
1016                 t->tv_freq    = default_set_tv_freq;
1017                 t->radio_freq = default_set_radio_freq;
1018                 break;
1019         }
1020 }
1021
1022 /* ---------------------------------------------------------------------- */
1023
1024 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
1025 {
1026         struct tuner *t;
1027         struct i2c_client *client;
1028
1029         if (this_adap > 0)
1030                 return -1;
1031         this_adap++;
1032         
1033         client_template.adapter = adap;
1034         client_template.addr = addr;
1035
1036         printk("tuner: chip found at addr 0x%x i2c-bus %s\n",
1037                addr<<1, adap->name);
1038
1039         if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
1040                 return -ENOMEM;
1041         memcpy(client,&client_template,sizeof(struct i2c_client));
1042         t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
1043         if (NULL == t) {
1044                 kfree(client);
1045                 return -ENOMEM;
1046         }
1047         memset(t,0,sizeof(struct tuner));
1048         i2c_set_clientdata(client, t);
1049         t->type       = UNSET;
1050         t->radio_if2  = 10700*1000; // 10.7MHz - FM radio
1051
1052         i2c_attach_client(client);
1053         if (type < TUNERS) {
1054                 set_type(client, type, "insmod option");
1055                 printk("tuner: The type=<n> insmod option will go away soon.\n");
1056                 printk("tuner: Please use the tuner=<n> option provided by\n");
1057                 printk("tuner: tv card core driver (bttv, saa7134, ...) instead.\n");
1058         }
1059         return 0;
1060 }
1061
1062 static int tuner_probe(struct i2c_adapter *adap)
1063 {
1064         if (0 != addr) {
1065                 normal_i2c_range[0] = addr;
1066                 normal_i2c_range[1] = addr;
1067         }
1068         this_adap = 0;
1069
1070 #ifdef I2C_CLASS_TV_ANALOG
1071         if (adap->class & I2C_CLASS_TV_ANALOG)
1072                 return i2c_probe(adap, &addr_data, tuner_attach);
1073 #else
1074         switch (adap->id) {
1075         case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3:
1076         case I2C_ALGO_BIT | I2C_HW_B_BT848:
1077         case I2C_ALGO_BIT | I2C_HW_B_RIVA:
1078         case I2C_ALGO_SAA7134:
1079         case I2C_ALGO_SAA7146:
1080                 return i2c_probe(adap, &addr_data, tuner_attach);
1081                 break;
1082         }
1083 #endif
1084         return 0;
1085 }
1086
1087 static int tuner_detach(struct i2c_client *client)
1088 {
1089         struct tuner *t = i2c_get_clientdata(client);
1090
1091         i2c_detach_client(client);
1092         kfree(t);
1093         kfree(client);
1094         return 0;
1095 }
1096
1097 static int
1098 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1099 {
1100         struct tuner *t = i2c_get_clientdata(client);
1101         unsigned int *iarg = (int*)arg;
1102
1103         switch (cmd) {
1104
1105         /* --- configuration --- */
1106         case TUNER_SET_TYPE:
1107                 set_type(client,*iarg,client->adapter->name);
1108                 break;
1109         case AUDC_SET_RADIO:
1110                 if (!t->radio) {
1111                         set_tv_freq(client,400 * 16);
1112                         t->radio = 1;
1113                 }
1114                 break;
1115         case AUDC_CONFIG_PINNACLE:
1116                 switch (*iarg) {
1117                 case 2:
1118                         dprintk("tuner: pinnacle pal\n");
1119                         t->radio_if2 = 33300 * 1000;
1120                         break;
1121                 case 3:
1122                         dprintk("tuner: pinnacle ntsc\n");
1123                         t->radio_if2 = 41300 * 1000;
1124                         break;
1125                 }
1126                 break;
1127                 
1128         /* --- v4l ioctls --- */
1129         /* take care: bttv does userspace copying, we'll get a
1130            kernel pointer here... */
1131         case VIDIOCSCHAN:
1132         {
1133                 struct video_channel *vc = arg;
1134
1135                 t->radio = 0;
1136                 t->mode = vc->norm;
1137                 if (t->freq)
1138                         set_tv_freq(client,t->freq);
1139                 return 0;
1140         }
1141         case VIDIOCSFREQ:
1142         {
1143                 unsigned long *v = arg;
1144
1145                 if (t->radio) {
1146                         dprintk("tuner: radio freq set to %lu.%02lu\n",
1147                                 (*v)/16,(*v)%16*100/16);
1148                         set_radio_freq(client,*v);
1149                 } else {
1150                         dprintk("tuner: tv freq set to %lu.%02lu\n",
1151                                 (*v)/16,(*v)%16*100/16);
1152                         set_tv_freq(client,*v);
1153                 }
1154                 t->freq = *v;
1155                 return 0;
1156         }
1157         case VIDIOCGTUNER:
1158         {
1159                 struct video_tuner *vt = arg;
1160
1161                 if (t->radio)
1162                         vt->signal = tuner_signal(client);
1163                 return 0;
1164         }
1165         case VIDIOCGAUDIO:
1166         {
1167                 struct video_audio *va = arg;
1168                 if (t->radio)
1169                         va->mode = (tuner_stereo(client) ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO);
1170                 return 0;
1171         }
1172         default:
1173                 /* nothing */
1174                 break;
1175         }
1176         
1177         return 0;
1178 }
1179
1180 /* ----------------------------------------------------------------------- */
1181
1182 static struct i2c_driver driver = {
1183         .owner          = THIS_MODULE,
1184         .name           = "i2c TV tuner driver",
1185         .id             = I2C_DRIVERID_TUNER,
1186         .flags          = I2C_DF_NOTIFY,
1187         .attach_adapter = tuner_probe,
1188         .detach_client  = tuner_detach,
1189         .command        = tuner_command,
1190 };
1191 static struct i2c_client client_template =
1192 {
1193         I2C_DEVNAME("(tuner unset)"),
1194         .flags      = I2C_CLIENT_ALLOW_USE,
1195         .driver     = &driver,
1196 };
1197
1198 static int tuner_init_module(void)
1199 {
1200         i2c_add_driver(&driver);
1201         return 0;
1202 }
1203
1204 static void tuner_cleanup_module(void)
1205 {
1206         i2c_del_driver(&driver);
1207 }
1208
1209 module_init(tuner_init_module);
1210 module_exit(tuner_cleanup_module);
1211
1212 /*
1213  * Overrides for Emacs so that we follow Linus's tabbing style.
1214  * ---------------------------------------------------------------------------
1215  * Local variables:
1216  * c-basic-offset: 8
1217  * End:
1218  */