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