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