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