3814c8393f0cc8268dbd85b5dab584bc606e6ba5
[linux-flexiantxendom0-3.2.10.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,0x8,732},
237 };
238 #define TUNERS ARRAY_SIZE(tuners)
239
240 /* ---------------------------------------------------------------------- */
241
242 static int tuner_getstatus(struct i2c_client *c)
243 {
244         unsigned char byte;
245
246         struct tuner *t = i2c_get_clientdata(c);
247
248         if (t->type == TUNER_MT2032)
249                 return 0;
250
251         if (1 != i2c_master_recv(c,&byte,1))
252                 return 0;
253         return byte;
254 }
255
256 #define TUNER_POR       0x80
257 #define TUNER_FL        0x40
258 #define TUNER_MODE      0x38
259 #define TUNER_AFC       0x07
260
261 #define TUNER_STEREO    0x10 /* radio mode */
262 #define TUNER_SIGNAL    0x07 /* radio mode */
263
264 static int tuner_signal(struct i2c_client *c)
265 {
266         return (tuner_getstatus(c) & TUNER_SIGNAL)<<13;
267 }
268
269 static int tuner_stereo(struct i2c_client *c)
270 {
271         return (tuner_getstatus (c) & TUNER_STEREO);
272 }
273
274 #if 0 /* unused */
275 static int tuner_islocked (struct i2c_client *c)
276 {
277         return (tuner_getstatus (c) & TUNER_FL);
278 }
279
280 static int tuner_afcstatus (struct i2c_client *c)
281 {
282         return (tuner_getstatus (c) & TUNER_AFC) - 2;
283 }
284
285 static int tuner_mode (struct i2c_client *c)
286 {
287         return (tuner_getstatus (c) & TUNER_MODE) >> 3;
288 }
289 #endif
290
291 /* ---------------------------------------------------------------------- */
292
293 #define MT2032 0x04
294 #define MT2030 0x06
295 #define MT2040 0x07
296 #define MT2050 0x42
297
298 static char *microtune_part[] = {
299         [ MT2030 ] = "MT2030",
300         [ MT2032 ] = "MT2032",
301         [ MT2040 ] = "MT2040",
302         [ MT2050 ] = "MT2050",
303 };
304
305 // IsSpurInBand()?
306 static int mt2032_spurcheck(int f1, int f2, int spectrum_from,int spectrum_to)
307 {
308         int n1=1,n2,f;
309
310         f1=f1/1000; //scale to kHz to avoid 32bit overflows
311         f2=f2/1000;
312         spectrum_from/=1000;
313         spectrum_to/=1000;
314
315         dprintk("spurcheck f1=%d f2=%d  from=%d to=%d\n",f1,f2,spectrum_from,spectrum_to);
316
317         do {
318             n2=-n1;
319             f=n1*(f1-f2);
320             do {
321                 n2--;
322                 f=f-f2;
323                 dprintk(" spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);
324
325                 if( (f>spectrum_from) && (f<spectrum_to))
326                         printk("mt2032 spurcheck triggered: %d\n",n1);
327             } while ( (f>(f2-spectrum_to)) || (n2>-5));
328             n1++;
329         } while (n1<5);
330
331         return 1;
332 }
333
334 static int mt2032_compute_freq(unsigned int rfin,
335                                unsigned int if1, unsigned int if2,
336                                unsigned int spectrum_from,
337                                unsigned int spectrum_to,
338                                unsigned char *buf,
339                                int *ret_sel,
340                                unsigned int xogc) //all in Hz
341 {
342         unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,
343                 desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;
344
345         fref= 5250 *1000; //5.25MHz
346         desired_lo1=rfin+if1;
347
348         lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);
349         lo1n=lo1/8;
350         lo1a=lo1-(lo1n*8);
351
352         s=rfin/1000/1000+1090;
353
354         if(optimize_vco) {
355                 if(s>1890) sel=0;
356                 else if(s>1720) sel=1;
357                 else if(s>1530) sel=2;
358                 else if(s>1370) sel=3;
359                 else sel=4; // >1090
360         }
361         else {
362                 if(s>1790) sel=0; // <1958
363                 else if(s>1617) sel=1;
364                 else if(s>1449) sel=2;
365                 else if(s>1291) sel=3;
366                 else sel=4; // >1090
367         }
368         *ret_sel=sel;
369
370         lo1freq=(lo1a+8*lo1n)*fref;
371
372         dprintk("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",
373                 rfin,lo1,lo1n,lo1a,sel,lo1freq);
374
375         desired_lo2=lo1freq-rfin-if2;
376         lo2=(desired_lo2)/fref;
377         lo2n=lo2/8;
378         lo2a=lo2-(lo2n*8);
379         lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith
380         lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;
381
382         dprintk("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",
383                 rfin,lo2,lo2n,lo2a,lo2num,lo2freq);
384
385         if(lo1a<0 || lo1a>7 || lo1n<17 ||lo1n>48 || lo2a<0 ||lo2a >7 ||lo2n<17 || lo2n>30) {
386                 printk("mt2032: frequency parameters out of range: %d %d %d %d\n",
387                        lo1a, lo1n, lo2a,lo2n);
388                 return(-1);
389         }
390
391         mt2032_spurcheck(lo1freq, desired_lo2,  spectrum_from, spectrum_to);
392         // should recalculate lo1 (one step up/down)
393
394         // set up MT2032 register map for transfer over i2c
395         buf[0]=lo1n-1;
396         buf[1]=lo1a | (sel<<4);
397         buf[2]=0x86; // LOGC
398         buf[3]=0x0f; //reserved
399         buf[4]=0x1f;
400         buf[5]=(lo2n-1) | (lo2a<<5);
401         if(rfin >400*1000*1000)
402                 buf[6]=0xe4;
403         else
404                 buf[6]=0xf4; // set PKEN per rev 1.2 
405         buf[7]=8+xogc;
406         buf[8]=0xc3; //reserved
407         buf[9]=0x4e; //reserved
408         buf[10]=0xec; //reserved
409         buf[11]=(lo2num&0xff);
410         buf[12]=(lo2num>>8) |0x80; // Lo2RST
411
412         return 0;
413 }
414
415 static int mt2032_check_lo_lock(struct i2c_client *c)
416 {
417         int try,lock=0;
418         unsigned char buf[2];
419         for(try=0;try<10;try++) {
420                 buf[0]=0x0e;
421                 i2c_master_send(c,buf,1);
422                 i2c_master_recv(c,buf,1);
423                 dprintk("mt2032 Reg.E=0x%02x\n",buf[0]);
424                 lock=buf[0] &0x06;
425                 
426                 if (lock==6)
427                         break;
428                 
429                 dprintk("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);
430                 udelay(1000);
431         }
432         return lock;
433 }
434
435 static int mt2032_optimize_vco(struct i2c_client *c,int sel,int lock)
436 {
437         unsigned char buf[2];
438         int tad1;
439
440         buf[0]=0x0f;
441         i2c_master_send(c,buf,1);
442         i2c_master_recv(c,buf,1);
443         dprintk("mt2032 Reg.F=0x%02x\n",buf[0]);
444         tad1=buf[0]&0x07;
445
446         if(tad1 ==0) return lock;
447         if(tad1 ==1) return lock;
448
449         if(tad1==2) {
450                 if(sel==0) 
451                         return lock;
452                 else sel--;
453         }
454         else {
455                 if(sel<4)
456                         sel++;
457                 else
458                         return lock;
459         }
460
461         dprintk("mt2032 optimize_vco: sel=%d\n",sel);
462
463         buf[0]=0x0f;
464         buf[1]=sel;
465         i2c_master_send(c,buf,2);
466         lock=mt2032_check_lo_lock(c);
467         return lock;
468 }
469
470
471 static void mt2032_set_if_freq(struct i2c_client *c, unsigned int rfin,
472                                unsigned int if1, unsigned int if2,
473                                unsigned int from, unsigned int to)
474 {
475         unsigned char buf[21];
476         int lint_try,ret,sel,lock=0;
477         struct tuner *t = i2c_get_clientdata(c);
478
479         dprintk("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",rfin,if1,if2,from,to);
480
481         buf[0]=0;
482         ret=i2c_master_send(c,buf,1);
483         i2c_master_recv(c,buf,21);
484
485         buf[0]=0;
486         ret=mt2032_compute_freq(rfin,if1,if2,from,to,&buf[1],&sel,t->xogc);
487         if (ret<0)
488                 return;
489
490         // send only the relevant registers per Rev. 1.2
491         buf[0]=0;
492         ret=i2c_master_send(c,buf,4);
493         buf[5]=5;
494         ret=i2c_master_send(c,buf+5,4);
495         buf[11]=11;
496         ret=i2c_master_send(c,buf+11,3);
497         if(ret!=3)
498                 printk("mt2032_set_if_freq failed with %d\n",ret);
499
500         // wait for PLLs to lock (per manual), retry LINT if not.
501         for(lint_try=0; lint_try<2; lint_try++) {
502                 lock=mt2032_check_lo_lock(c);
503                 
504                 if(optimize_vco)
505                         lock=mt2032_optimize_vco(c,sel,lock);
506                 if(lock==6) break;
507                 
508                 printk("mt2032: re-init PLLs by LINT\n"); 
509                 buf[0]=7; 
510                 buf[1]=0x80 +8+t->xogc; // set LINT to re-init PLLs
511                 i2c_master_send(c,buf,2);
512                 mdelay(10);
513                 buf[1]=8+t->xogc;
514                 i2c_master_send(c,buf,2);
515         }
516
517         if (lock!=6)
518                 printk("MT2032 Fatal Error: PLLs didn't lock.\n");
519
520         buf[0]=2;
521         buf[1]=0x20; // LOGC for optimal phase noise
522         ret=i2c_master_send(c,buf,2);
523         if (ret!=2)
524                 printk("mt2032_set_if_freq2 failed with %d\n",ret);
525 }
526
527
528 static void mt2032_set_tv_freq(struct i2c_client *c, unsigned int freq)
529 {
530         struct tuner *t = i2c_get_clientdata(c);
531         int if2,from,to;
532
533         // signal bandwidth and picture carrier
534         if (t->mode == VIDEO_MODE_NTSC) {
535                 from=40750*1000;
536                 to=46750*1000;
537                 if2=45750*1000; 
538         } else {
539                 // Pal 
540                 from=32900*1000;
541                 to=39900*1000;
542                 if2=38900*1000;
543         }
544
545         mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
546                            1090*1000*1000, if2, from, to);
547 }
548
549 static void mt2032_set_radio_freq(struct i2c_client *c, unsigned int freq)
550 {
551         struct tuner *t = i2c_get_clientdata(c);
552         int if2 = t->radio_if2;
553
554         // per Manual for FM tuning: first if center freq. 1085 MHz
555         mt2032_set_if_freq(c, freq*62500 /* freq*1000*1000/16 */,
556                            1085*1000*1000,if2,if2,if2);
557 }
558
559 // Initalization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001
560 static int mt2032_init(struct i2c_client *c)
561 {
562         struct tuner *t = i2c_get_clientdata(c);
563         unsigned char buf[21];
564         int ret,xogc,xok=0;
565
566         // Initialize Registers per spec.
567         buf[1]=2; // Index to register 2
568         buf[2]=0xff;
569         buf[3]=0x0f;
570         buf[4]=0x1f;
571         ret=i2c_master_send(c,buf+1,4);
572
573         buf[5]=6; // Index register 6
574         buf[6]=0xe4;
575         buf[7]=0x8f;
576         buf[8]=0xc3;
577         buf[9]=0x4e;
578         buf[10]=0xec;
579         ret=i2c_master_send(c,buf+5,6);
580
581         buf[12]=13;  // Index register 13
582         buf[13]=0x32;
583         ret=i2c_master_send(c,buf+12,2);
584
585         // Adjust XOGC (register 7), wait for XOK
586         xogc=7;
587         do {
588                 dprintk("mt2032: xogc = 0x%02x\n",xogc&0x07);
589                 mdelay(10);
590                 buf[0]=0x0e;
591                 i2c_master_send(c,buf,1);
592                 i2c_master_recv(c,buf,1);
593                 xok=buf[0]&0x01;
594                 dprintk("mt2032: xok = 0x%02x\n",xok);
595                 if (xok == 1) break;
596
597                 xogc--;
598                 dprintk("mt2032: xogc = 0x%02x\n",xogc&0x07);
599                 if (xogc == 3) {
600                         xogc=4; // min. 4 per spec
601                         break;
602                 }
603                 buf[0]=0x07;
604                 buf[1]=0x88 + xogc;
605                 ret=i2c_master_send(c,buf,2);
606                 if (ret!=2)
607                         printk("mt2032_init failed with %d\n",ret);
608         } while (xok != 1 );
609         t->xogc=xogc;
610
611         t->tv_freq    = mt2032_set_tv_freq;
612         t->radio_freq = mt2032_set_radio_freq;
613         return(1);
614 }
615
616 static void mt2050_set_if_freq(struct i2c_client *c,unsigned int freq, unsigned int if2)
617 {
618         unsigned int if1=1218*1000*1000;
619         unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;
620         int ret;
621         unsigned char buf[6];
622         
623         dprintk("mt2050_set_if_freq freq=%d\n",freq);
624         
625         f_lo1=freq+if1;
626         f_lo1=(f_lo1/1000000)*1000000;
627         
628         f_lo2=f_lo1-freq-if2;
629         f_lo2=(f_lo2/50000)*50000;
630         
631         lo1=f_lo1/4000000;
632         lo2=f_lo2/4000000;
633         
634         f_lo1_modulo= f_lo1-(lo1*4000000);
635         f_lo2_modulo= f_lo2-(lo2*4000000);
636         
637         num1=4*f_lo1_modulo/4000000;
638         num2=4096*(f_lo2_modulo/1000)/4000;
639         
640         // todo spurchecks
641         
642         div1a=(lo1/12)-1;
643         div1b=lo1-(div1a+1)*12;
644         
645         div2a=(lo2/8)-1;
646         div2b=lo2-(div2a+1)*8;
647         
648         dprintk("lo1 lo2 = %d %d\n", lo1, lo2);
649         dprintk("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",num1,num2,div1a,div1b,div2a,div2b);
650         
651         
652         buf[0]=1;
653         buf[1]= 4*div1b + num1;
654         if(freq<275*1000*1000) buf[1] = buf[1]|0x80;
655         
656         buf[2]=div1a;
657         buf[3]=32*div2b + num2/256;
658         buf[4]=num2-(num2/256)*256;
659         buf[5]=div2a;
660         if(num2!=0) buf[5]=buf[5]|0x40;
661         
662         if(debug) {
663                 int i;
664                 printk("bufs is: ");
665                 for(i=0;i<6;i++)
666                         printk("%x ",buf[i]);
667                 printk("\n");
668         }
669         
670         ret=i2c_master_send(c,buf,6);
671         if (ret!=6)
672                 printk("mt2050_set_if_freq failed with %d\n",ret);
673 }
674
675 static void mt2050_set_tv_freq(struct i2c_client *c, unsigned int freq)
676 {
677         struct tuner *t = i2c_get_clientdata(c);
678         unsigned int if2;
679         
680         if (t->mode == VIDEO_MODE_NTSC) {
681                 if2=45750*1000;
682         } else {
683                 // Pal
684                 if2=38900*1000;
685         }
686         mt2050_set_if_freq(c,freq*62500,if2);
687 }
688
689 static void mt2050_set_radio_freq(struct i2c_client *c, unsigned int freq)
690 {
691         struct tuner *t = i2c_get_clientdata(c);
692         int if2 = t->radio_if2;
693         
694         mt2050_set_if_freq(c, freq*62500, if2);
695 }
696
697 static int mt2050_init(struct i2c_client *c)
698 {
699         struct tuner *t = i2c_get_clientdata(c);
700         unsigned char buf[2];
701         int ret;
702         
703         buf[0]=6;
704         buf[1]=0x10;
705         ret=i2c_master_send(c,buf,2); //  power
706         
707         buf[0]=0x0f;
708         buf[1]=0x0f;
709         ret=i2c_master_send(c,buf,2); // m1lo
710         
711         buf[0]=0x0d;
712         ret=i2c_master_send(c,buf,1);
713         i2c_master_recv(c,buf,1);
714         
715         dprintk("mt2050: sro is %x\n",buf[0]);
716         t->tv_freq    = mt2050_set_tv_freq;
717         t->radio_freq = mt2050_set_radio_freq;
718         return 0;
719 }
720
721 static int microtune_init(struct i2c_client *c)
722 {
723         struct tuner *t = i2c_get_clientdata(c);
724         char *name;
725         unsigned char buf[21];
726         int company_code;
727         
728         buf[0] = 0;
729         t->tv_freq = NULL;
730         t->radio_freq = NULL;
731         name = "unknown";
732
733         i2c_master_send(c,buf,1);
734         i2c_master_recv(c,buf,21);
735         if(debug) {
736                 int i;
737                 printk(KERN_DEBUG "tuner: MT2032 hexdump:\n");
738                 for(i=0;i<21;i++) {
739                         printk(" %02x",buf[i]);
740                         if(((i+1)%8)==0) printk(" ");
741                         if(((i+1)%16)==0) printk("\n ");
742                 }
743                 printk("\n ");
744         }
745         company_code = buf[0x11] << 8 | buf[0x12];
746         printk("tuner: microtune: companycode=%04x part=%02x rev=%02x\n",
747                company_code,buf[0x13],buf[0x14]);
748         switch (company_code) {
749         case 0x30bf:
750         case 0x3cbf:
751         case 0x3dbf:
752         case 0x4d54:
753         case 0x8e81:
754         case 0x8e91:
755                 /* ok (?) */
756                 break;
757         default:
758                 printk("tuner: microtune: unknown companycode\n");
759                 return 0;
760         }
761
762         if (buf[0x13] < ARRAY_SIZE(microtune_part) &&
763             NULL != microtune_part[buf[0x13]])
764                 name = microtune_part[buf[0x13]];
765         switch (buf[0x13]) {
766         case MT2032:
767                 mt2032_init(c);
768                 break;
769         case MT2050:
770                 mt2050_init(c);
771                 break;
772         default:
773                 printk("tuner: microtune %s found, not (yet?) supported, sorry :-/\n",
774                        name);
775                 return 0;
776         }
777         printk("tuner: microtune %s found, OK\n",name);
778         return 0;
779 }
780
781 /* ---------------------------------------------------------------------- */
782
783 static void default_set_tv_freq(struct i2c_client *c, unsigned int freq)
784 {
785         struct tuner *t = i2c_get_clientdata(c);
786         u8 config;
787         u16 div;
788         struct tunertype *tun;
789         unsigned char buffer[4];
790         int rc;
791
792         tun=&tuners[t->type];
793         if (freq < tun->thresh1) 
794                 config = tun->VHF_L;
795         else if (freq < tun->thresh2) 
796                 config = tun->VHF_H;
797         else
798                 config = tun->UHF;
799
800
801         /* tv norm specific stuff for multi-norm tuners */
802         switch (t->type) {
803         case TUNER_PHILIPS_SECAM: // FI1216MF
804                 /* 0x01 -> ??? no change ??? */
805                 /* 0x02 -> PAL BDGHI / SECAM L */
806                 /* 0x04 -> ??? PAL others / SECAM others ??? */
807                 config &= ~0x02;
808                 if (t->mode == VIDEO_MODE_SECAM)
809                         config |= 0x02;
810                 break;
811
812         case TUNER_TEMIC_4046FM5:
813                 config &= ~0x0f;
814                 switch (pal[0]) {
815                 case 'i':
816                 case 'I':
817                         config |= TEMIC_SET_PAL_I;
818                         break;
819                 case 'd':
820                 case 'D':
821                         config |= TEMIC_SET_PAL_DK;
822                         break;
823                 case 'l':
824                 case 'L':
825                         config |= TEMIC_SET_PAL_L;
826                         break;
827                 case 'b':
828                 case 'B':
829                 case 'g':
830                 case 'G':
831                 default:
832                         config |= TEMIC_SET_PAL_BG;
833                         break;
834                 }
835                 break;
836
837         case TUNER_PHILIPS_FQ1216ME:
838                 config &= ~0x0f;
839                 switch (pal[0]) {
840                 case 'i':
841                 case 'I':
842                         config |= PHILIPS_SET_PAL_I;
843                         break;
844                 case 'l':
845                 case 'L':
846                         config |= PHILIPS_SET_PAL_L;
847                         break;
848                 case 'd':
849                 case 'D':
850                 case 'b':
851                 case 'B':
852                 case 'g':
853                 case 'G':
854                         config |= PHILIPS_SET_PAL_BGDK;
855                         break;
856                 }
857                 break;
858
859         case TUNER_PHILIPS_ATSC:
860                 /* 0x00 -> ATSC antenna input 1 */
861                 /* 0x01 -> ATSC antenna input 2 */
862                 /* 0x02 -> NTSC antenna input 1 */
863                 /* 0x03 -> NTSC antenna input 2 */
864                 
865                 config &= ~0x03;
866 #ifdef VIDEO_MODE_ATSC
867                 if (VIDEO_MODE_ATSC != t->mode)
868                         config |= 2;
869 #endif
870                 /* FIXME: input */
871                 break;
872         }
873
874         
875         /*
876          * Philips FI1216MK2 remark from specification :
877          * for channel selection involving band switching, and to ensure
878          * smooth tuning to the desired channel without causing
879          * unnecessary charge pump action, it is recommended to consider
880          * the difference between wanted channel frequency and the
881          * current channel frequency.  Unnecessary charge pump action
882          * will result in very low tuning voltage which may drive the
883          * oscillator to extreme conditions.
884          *
885          * Progfou: specification says to send config data before
886          * frequency in case (wanted frequency < current frequency).
887          */
888
889         div=freq + tun->IFPCoff;
890         if (t->type == TUNER_PHILIPS_SECAM && freq < t->freq) {
891                 buffer[0] = tun->config;
892                 buffer[1] = config;
893                 buffer[2] = (div>>8) & 0x7f;
894                 buffer[3] = div      & 0xff;
895         } else {
896                 buffer[0] = (div>>8) & 0x7f;
897                 buffer[1] = div      & 0xff;
898                 buffer[2] = tun->config;
899                 buffer[3] = config;
900         }
901         dprintk("tuner: tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
902                 buffer[0],buffer[1],buffer[2],buffer[3]);
903
904         if (4 != (rc = i2c_master_send(c,buffer,4)))
905                 printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc);
906
907 }
908
909 static void default_set_radio_freq(struct i2c_client *c, unsigned int freq)
910 {
911         struct tunertype *tun;
912         struct tuner *t = i2c_get_clientdata(c);
913         unsigned char buffer[4];
914         unsigned div;
915         int rc;
916
917         tun=&tuners[t->type];
918         div = freq + (int)(16*10.7);
919         buffer[0] = (div>>8) & 0x7f;
920         buffer[1] = div      & 0xff;
921         buffer[2] = tun->config;
922         switch (t->type) {
923         case TUNER_PHILIPS_FM1216ME_MK3:
924         case TUNER_PHILIPS_FM1236_MK3:
925                 buffer[3] = 0x19;
926                 break;
927         default:
928                 buffer[3] = 0xa4;
929                 break;
930         }
931
932         dprintk("tuner: radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
933                 buffer[0],buffer[1],buffer[2],buffer[3]);
934
935         if (4 != (rc = i2c_master_send(c,buffer,4)))
936                 printk("tuner: i2c i/o error: rc == %d (should be 4)\n",rc);
937 }
938
939 /* ---------------------------------------------------------------------- */
940
941 // Set tuner frequency,  freq in Units of 62.5kHz = 1/16MHz
942 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
943 {
944         struct tuner *t = i2c_get_clientdata(c);
945
946         if (t->type == UNSET) {
947                 printk("tuner: tuner type not set\n");
948                 return;
949         }
950         if (NULL == t->tv_freq) {
951                 printk("tuner: Huh? tv_set is NULL?\n");
952                 return;
953         }
954         if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
955                 /* FIXME: better do that chip-specific, but
956                    right now we don't have that in the config
957                    struct and this way is still better than no
958                    check at all */
959                 printk("tuner: TV freq (%d.%02d) out of range (%d-%d)\n",
960                        freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
961                 return;
962         }
963         t->tv_freq(c,freq);
964 }
965
966 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
967 {
968         struct tuner *t = i2c_get_clientdata(c);
969
970         if (t->type == UNSET) {
971                 printk("tuner: tuner type not set\n");
972                 return;
973         }
974         if (NULL == t->radio_freq) {
975                 printk("tuner: no radio tuning for this one, sorry.\n");
976                 return;
977         }
978         if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
979                 printk("tuner: radio freq (%d.%02d) out of range (%d-%d)\n",
980                        freq/16,freq%16*100/16,
981                        radio_range[0],radio_range[1]);
982                 return;
983         }
984         t->radio_freq(c,freq);
985 }
986
987 static void set_type(struct i2c_client *c, unsigned int type)
988 {
989         struct tuner *t = i2c_get_clientdata(c);
990
991         if (t->type != UNSET) {
992                 printk("tuner: type already set (%d)\n",t->type);
993                 return;
994         }
995         if (type >= TUNERS)
996                 return;
997
998         t->type = type;
999         printk("tuner: type set to %d (%s)\n", t->type,tuners[t->type].name);
1000         strlcpy(c->name, tuners[t->type].name, sizeof(c->name));
1001
1002         switch (t->type) {
1003         case TUNER_MT2032:
1004                 microtune_init(c);
1005                 break;
1006         default:
1007                 t->tv_freq    = default_set_tv_freq;
1008                 t->radio_freq = default_set_radio_freq;
1009                 break;
1010         }
1011 }
1012
1013 /* ---------------------------------------------------------------------- */
1014
1015 static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
1016 {
1017         struct tuner *t;
1018         struct i2c_client *client;
1019
1020         if (this_adap > 0)
1021                 return -1;
1022         this_adap++;
1023         
1024         client_template.adapter = adap;
1025         client_template.addr = addr;
1026
1027         printk("tuner: chip found @ 0x%x\n", addr<<1);
1028
1029         if (NULL == (client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
1030                 return -ENOMEM;
1031         memcpy(client,&client_template,sizeof(struct i2c_client));
1032         t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
1033         if (NULL == t) {
1034                 kfree(client);
1035                 return -ENOMEM;
1036         }
1037         memset(t,0,sizeof(struct tuner));
1038         i2c_set_clientdata(client, t);
1039         t->type       = UNSET;
1040         t->radio_if2  = 10700*1000; // 10.7MHz - FM radio
1041
1042         i2c_attach_client(client);
1043         if (type < TUNERS) {
1044                 t->type = type;
1045                 printk("tuner: type forced to %d (%s) [insmod]\n",
1046                        t->type,tuners[t->type].name);
1047                 set_type(client,type);
1048         }
1049         return 0;
1050 }
1051
1052 static int tuner_probe(struct i2c_adapter *adap)
1053 {
1054         if (0 != addr) {
1055                 normal_i2c_range[0] = addr;
1056                 normal_i2c_range[1] = addr;
1057         }
1058         this_adap = 0;
1059
1060 #ifdef I2C_ADAP_CLASS_TV_ANALOG
1061         if (adap->class & I2C_ADAP_CLASS_TV_ANALOG)
1062                 return i2c_probe(adap, &addr_data, tuner_attach);
1063 #else
1064         switch (adap->id) {
1065         case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3:
1066         case I2C_ALGO_BIT | I2C_HW_B_BT848:
1067         case I2C_ALGO_BIT | I2C_HW_B_RIVA:
1068         case I2C_ALGO_SAA7134:
1069         case I2C_ALGO_SAA7146:
1070                 return i2c_probe(adap, &addr_data, tuner_attach);
1071                 break;
1072         }
1073 #endif
1074         return 0;
1075 }
1076
1077 static int tuner_detach(struct i2c_client *client)
1078 {
1079         struct tuner *t = i2c_get_clientdata(client);
1080
1081         i2c_detach_client(client);
1082         kfree(t);
1083         kfree(client);
1084         return 0;
1085 }
1086
1087 static int
1088 tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1089 {
1090         struct tuner *t = i2c_get_clientdata(client);
1091         unsigned int *iarg = (int*)arg;
1092
1093         switch (cmd) {
1094
1095         /* --- configuration --- */
1096         case TUNER_SET_TYPE:
1097                 set_type(client,*iarg);
1098                 break;
1099         case AUDC_SET_RADIO:
1100                 if (!t->radio) {
1101                         set_tv_freq(client,400 * 16);
1102                         t->radio = 1;
1103                 }
1104                 break;
1105         case AUDC_CONFIG_PINNACLE:
1106                 switch (*iarg) {
1107                 case 2:
1108                         dprintk("tuner: pinnacle pal\n");
1109                         t->radio_if2 = 33300 * 1000;
1110                         break;
1111                 case 3:
1112                         dprintk("tuner: pinnacle ntsc\n");
1113                         t->radio_if2 = 41300 * 1000;
1114                         break;
1115                 }
1116                 break;
1117                 
1118         /* --- v4l ioctls --- */
1119         /* take care: bttv does userspace copying, we'll get a
1120            kernel pointer here... */
1121         case VIDIOCSCHAN:
1122         {
1123                 struct video_channel *vc = arg;
1124
1125                 t->radio = 0;
1126                 t->mode = vc->norm;
1127                 if (t->freq)
1128                         set_tv_freq(client,t->freq);
1129                 return 0;
1130         }
1131         case VIDIOCSFREQ:
1132         {
1133                 unsigned long *v = arg;
1134
1135                 if (t->radio) {
1136                         dprintk("tuner: radio freq set to %lu.%02lu\n",
1137                                 (*v)/16,(*v)%16*100/16);
1138                         set_radio_freq(client,*v);
1139                 } else {
1140                         dprintk("tuner: tv freq set to %lu.%02lu\n",
1141                                 (*v)/16,(*v)%16*100/16);
1142                         set_tv_freq(client,*v);
1143                 }
1144                 t->freq = *v;
1145                 return 0;
1146         }
1147         case VIDIOCGTUNER:
1148         {
1149                 struct video_tuner *vt = arg;
1150
1151                 if (t->radio)
1152                         vt->signal = tuner_signal(client);
1153                 return 0;
1154         }
1155         case VIDIOCGAUDIO:
1156         {
1157                 struct video_audio *va = arg;
1158                 if (t->radio)
1159                         va->mode = (tuner_stereo(client) ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO);
1160                 return 0;
1161         }
1162         default:
1163                 /* nothing */
1164                 break;
1165         }
1166         
1167         return 0;
1168 }
1169
1170 /* ----------------------------------------------------------------------- */
1171
1172 static struct i2c_driver driver = {
1173         .owner          = THIS_MODULE,
1174         .name           = "i2c TV tuner driver",
1175         .id             = I2C_DRIVERID_TUNER,
1176         .flags          = I2C_DF_NOTIFY,
1177         .attach_adapter = tuner_probe,
1178         .detach_client  = tuner_detach,
1179         .command        = tuner_command,
1180 };
1181 static struct i2c_client client_template =
1182 {
1183         I2C_DEVNAME("(tuner unset)"),
1184         .flags      = I2C_CLIENT_ALLOW_USE,
1185         .driver     = &driver,
1186 };
1187
1188 static int tuner_init_module(void)
1189 {
1190         i2c_add_driver(&driver);
1191         return 0;
1192 }
1193
1194 static void tuner_cleanup_module(void)
1195 {
1196         i2c_del_driver(&driver);
1197 }
1198
1199 module_init(tuner_init_module);
1200 module_exit(tuner_cleanup_module);
1201
1202 /*
1203  * Overrides for Emacs so that we follow Linus's tabbing style.
1204  * ---------------------------------------------------------------------------
1205  * Local variables:
1206  * c-basic-offset: 8
1207  * End:
1208  */