xref: /netbsd/sys/dev/pci/bktr/bktr_tuner.c (revision bf9ec67e)
1 /*	$NetBSD: bktr_tuner.c,v 1.9 2001/11/13 07:29:37 lukem Exp $	*/
2 
3 /* FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.9 2000/10/19 07:33:28 roger Exp */
4 
5 /*
6  * This is part of the Driver for Video Capture Cards (Frame grabbers)
7  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
8  * chipset.
9  * Copyright Roger Hardiman and Amancio Hasty.
10  *
11  * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
12  *
13  */
14 
15 /*
16  * 1. Redistributions of source code must retain the
17  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. All advertising materials mentioning features or use of this software
29  *    must display the following acknowledgement:
30  *      This product includes software developed by Amancio Hasty and
31  *      Roger Hardiman
32  * 4. The name of the author may not be used to endorse or promote products
33  *    derived from this software without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
37  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
39  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
44  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: bktr_tuner.c,v 1.9 2001/11/13 07:29:37 lukem Exp $");
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/vnode.h>
55 #ifdef __NetBSD__
56 #include <sys/proc.h>
57 #endif
58 
59 #ifdef __FreeBSD__
60 #include <pci/pcivar.h>
61 
62 #if (__FreeBSD_version < 500000)
63 #include <machine/clock.h>              /* for DELAY */
64 #endif
65 
66 #if (__FreeBSD_version >=300000)
67 #include <machine/bus_memio.h>          /* for bus space */
68 #include <machine/bus.h>
69 #include <sys/bus.h>
70 #endif
71 #endif
72 
73 #ifdef __NetBSD__
74 #include <dev/ic/bt8xx.h>	/* NetBSD .h file location */
75 #include <dev/pci/bktr/bktr_reg.h>
76 #include <dev/pci/bktr/bktr_tuner.h>
77 #include <dev/pci/bktr/bktr_card.h>
78 #include <dev/pci/bktr/bktr_core.h>
79 #else
80 #include <machine/ioctl_meteor.h>	/* Traditional .h file location */
81 #include <machine/ioctl_bt848.h>        /* extensions to ioctl_meteor.h */
82 #include <dev/bktr/bktr_reg.h>
83 #include <dev/bktr/bktr_tuner.h>
84 #include <dev/bktr/bktr_card.h>
85 #include <dev/bktr/bktr_core.h>
86 #endif
87 
88 
89 
90 #if defined( TUNER_AFC )
91 #define AFC_DELAY               10000   /* 10 millisend delay */
92 #define AFC_BITS                0x07
93 #define AFC_FREQ_MINUS_125      0x00
94 #define AFC_FREQ_MINUS_62       0x01
95 #define AFC_FREQ_CENTERED       0x02
96 #define AFC_FREQ_PLUS_62        0x03
97 #define AFC_FREQ_PLUS_125       0x04
98 #define AFC_MAX_STEP            (5 * FREQFACTOR) /* no more than 5 MHz */
99 #endif /* TUNER_AFC */
100 
101 
102 #define TTYPE_XXX               0
103 #define TTYPE_NTSC              1
104 #define TTYPE_NTSC_J            2
105 #define TTYPE_PAL               3
106 #define TTYPE_PAL_M             4
107 #define TTYPE_PAL_N             5
108 #define TTYPE_SECAM             6
109 
110 #define TSA552x_CB_MSB          (0x80)
111 #define TSA552x_CB_CP           (1<<6)	/* set this for fast tuning */
112 #define TSA552x_CB_T2           (1<<5)	/* test mode - Normally set to 0 */
113 #define TSA552x_CB_T1           (1<<4)	/* test mode - Normally set to 0 */
114 #define TSA552x_CB_T0           (1<<3)	/* test mode - Normally set to 1 */
115 #define TSA552x_CB_RSA          (1<<2)	/* 0 for 31.25 khz, 1 for 62.5 kHz */
116 #define TSA552x_CB_RSB          (1<<1)	/* 0 for FM 50kHz steps, 1 = Use RSA*/
117 #define TSA552x_CB_OS           (1<<0)	/* Set to 0 for normal operation */
118 
119 #define TSA552x_RADIO           (TSA552x_CB_MSB |       \
120                                  TSA552x_CB_T0)
121 
122 /* raise the charge pump voltage for fast tuning */
123 #define TSA552x_FCONTROL        (TSA552x_CB_MSB |       \
124                                  TSA552x_CB_CP  |       \
125                                  TSA552x_CB_T0  |       \
126                                  TSA552x_CB_RSA |       \
127                                  TSA552x_CB_RSB)
128 
129 /* lower the charge pump voltage for better residual oscillator FM */
130 #define TSA552x_SCONTROL        (TSA552x_CB_MSB |       \
131                                  TSA552x_CB_T0  |       \
132                                  TSA552x_CB_RSA |       \
133                                  TSA552x_CB_RSB)
134 
135 /* The control value for the ALPS TSCH5 Tuner */
136 #define TSCH5_FCONTROL          0x82
137 #define TSCH5_RADIO             0x86
138 
139 /* The control value for the ALPS TSBH1 Tuner */
140 #define TSBH1_FCONTROL		0xce
141 
142 
143 static const struct TUNER tuners[] = {
144 /* XXX FIXME: fill in the band-switch crosspoints */
145 	/* NO_TUNER */
146 	{ "<no>",				/* the 'name' */
147 	   TTYPE_XXX,				/* input type */
148  	   { 0x00,				/* control byte for Tuner PLL */
149  	     0x00,
150  	     0x00,
151  	     0x00 },
152 	   { 0x00, 0x00 },			/* band-switch crosspoints */
153 	   { 0x00, 0x00, 0x00,0x00} },		/* the band-switch values */
154 
155 	/* TEMIC_NTSC */
156 	{ "Temic NTSC",				/* the 'name' */
157 	   TTYPE_NTSC,				/* input type */
158 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
159 	     TSA552x_SCONTROL,
160 	     TSA552x_SCONTROL,
161 	     0x00 },
162 	   { 0x00, 0x00},			/* band-switch crosspoints */
163 	   { 0x02, 0x04, 0x01, 0x00 } },	/* the band-switch values */
164 
165 	/* TEMIC_PAL */
166 	{ "Temic PAL",				/* the 'name' */
167 	   TTYPE_PAL,				/* input type */
168 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
169 	     TSA552x_SCONTROL,
170 	     TSA552x_SCONTROL,
171 	     0x00 },
172 	   { 0x00, 0x00 },			/* band-switch crosspoints */
173 	   { 0x02, 0x04, 0x01, 0x00 } },	/* the band-switch values */
174 
175 	/* TEMIC_SECAM */
176 	{ "Temic SECAM",			/* the 'name' */
177 	   TTYPE_SECAM,				/* input type */
178 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
179 	     TSA552x_SCONTROL,
180 	     TSA552x_SCONTROL,
181 	     0x00 },
182 	   { 0x00, 0x00 },			/* band-switch crosspoints */
183 	   { 0x02, 0x04, 0x01,0x00 } },		/* the band-switch values */
184 
185 	/* PHILIPS_NTSC */
186 	{ "Philips NTSC",			/* the 'name' */
187 	   TTYPE_NTSC,				/* input type */
188 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
189 	     TSA552x_SCONTROL,
190 	     TSA552x_SCONTROL,
191 	     0x00 },
192 	   { 0x00, 0x00 },			/* band-switch crosspoints */
193 	   { 0xa0, 0x90, 0x30, 0x00 } },	/* the band-switch values */
194 
195 	/* PHILIPS_PAL */
196 	{ "Philips PAL",			/* the 'name' */
197 	   TTYPE_PAL,				/* input type */
198 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
199 	     TSA552x_SCONTROL,
200 	     TSA552x_SCONTROL,
201 	     0x00 },
202 	   { 0x00, 0x00 },			/* band-switch crosspoints */
203 	   { 0xa0, 0x90, 0x30, 0x00 } },	/* the band-switch values */
204 
205 	/* PHILIPS_SECAM */
206 	{ "Philips SECAM",			/* the 'name' */
207 	   TTYPE_SECAM,				/* input type */
208 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
209 	     TSA552x_SCONTROL,
210 	     TSA552x_SCONTROL,
211 	     0x00 },
212 	   { 0x00, 0x00 },			/* band-switch crosspoints */
213 	   { 0xa7, 0x97, 0x37, 0x00 } },	/* the band-switch values */
214 
215 	/* TEMIC_PAL I */
216 	{ "Temic PAL I",			/* the 'name' */
217 	   TTYPE_PAL,				/* input type */
218 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
219 	     TSA552x_SCONTROL,
220 	     TSA552x_SCONTROL,
221 	     0x00 },
222 	   { 0x00, 0x00 },			/* band-switch crosspoints */
223 	   { 0x02, 0x04, 0x01,0x00 } },		/* the band-switch values */
224 
225 	/* PHILIPS_PALI */
226 	{ "Philips PAL I",			/* the 'name' */
227 	   TTYPE_PAL,				/* input type */
228 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
229 	     TSA552x_SCONTROL,
230 	     TSA552x_SCONTROL,
231 	     0x00 },
232           { 0x00, 0x00 },                      /* band-switch crosspoints */
233           { 0xa0, 0x90, 0x30,0x00 } },         /* the band-switch values */
234 
235        /* PHILIPS_FR1236_NTSC */
236        { "Philips FR1236 NTSC FM",             /* the 'name' */
237           TTYPE_NTSC,                          /* input type */
238 	  { TSA552x_FCONTROL,			/* control byte for Tuner PLL */
239 	    TSA552x_FCONTROL,
240 	    TSA552x_FCONTROL,
241 	    TSA552x_RADIO  },
242           { 0x00, 0x00 },			/* band-switch crosspoints */
243 	  { 0xa0, 0x90, 0x30,0xa4 } },		/* the band-switch values */
244 
245 	/* PHILIPS_FR1216_PAL */
246 	{ "Philips FR1216 PAL FM" ,		/* the 'name' */
247 	   TTYPE_PAL,				/* input type */
248 	   { TSA552x_FCONTROL,			/* control byte for Tuner PLL */
249 	     TSA552x_FCONTROL,
250 	     TSA552x_FCONTROL,
251 	     TSA552x_RADIO },
252 	   { 0x00, 0x00 },			/* band-switch crosspoints */
253 	   { 0xa0, 0x90, 0x30, 0xa4 } },	/* the band-switch values */
254 
255 	/* PHILIPS_FR1236_SECAM */
256 	{ "Philips FR1236 SECAM FM",		/* the 'name' */
257 	   TTYPE_SECAM,				/* input type */
258 	   { TSA552x_FCONTROL,			/* control byte for Tuner PLL */
259 	     TSA552x_FCONTROL,
260 	     TSA552x_FCONTROL,
261 	     TSA552x_RADIO },
262 	   { 0x00, 0x00 },			/* band-switch crosspoints */
263 	   { 0xa7, 0x97, 0x37, 0xa4 } },	/* the band-switch values */
264 
265         /* ALPS TSCH5 NTSC */
266         { "ALPS TSCH5 NTSC FM",                 /* the 'name' */
267            TTYPE_NTSC,                          /* input type */
268            { TSCH5_FCONTROL,                    /* control byte for Tuner PLL */
269              TSCH5_FCONTROL,
270              TSCH5_FCONTROL,
271              TSCH5_RADIO },
272            { 0x00, 0x00 },                      /* band-switch crosspoints */
273            { 0x14, 0x12, 0x11, 0x04 } },        /* the band-switch values */
274 
275         /* ALPS TSBH1 NTSC */
276         { "ALPS TSBH1 NTSC",                    /* the 'name' */
277            TTYPE_NTSC,                          /* input type */
278            { TSBH1_FCONTROL,                    /* control byte for Tuner PLL */
279              TSBH1_FCONTROL,
280              TSBH1_FCONTROL,
281              0x00 },
282            { 0x00, 0x00 },                      /* band-switch crosspoints */
283            { 0x01, 0x02, 0x08, 0x00 } }         /* the band-switch values */
284 };
285 
286 
287 /* scaling factor for frequencies expressed as ints */
288 #define FREQFACTOR		16
289 
290 /*
291  * Format:
292  *	entry 0:         MAX legal channel
293  *	entry 1:         IF frequency
294  *			 expressed as fi{mHz} * 16,
295  *			 eg 45.75mHz == 45.75 * 16 = 732
296  *	entry 2:         [place holder/future]
297  *	entry 3:         base of channel record 0
298  *	entry 3 + (x*3): base of channel record 'x'
299  *	entry LAST:      NULL channel entry marking end of records
300  *
301  * Record:
302  *	int 0:		base channel
303  *	int 1:		frequency of base channel,
304  *			 expressed as fb{mHz} * 16,
305  *	int 2:		offset frequency between channels,
306  *			 expressed as fo{mHz} * 16,
307  */
308 
309 /*
310  * North American Broadcast Channels:
311  *
312  *  2:  55.25 mHz -  4:  67.25 mHz
313  *  5:  77.25 mHz -  6:	 83.25 mHz
314  *  7: 175.25 mHz - 13:	211.25 mHz
315  * 14: 471.25 mHz - 83:	885.25 mHz
316  *
317  * IF freq: 45.75 mHz
318  */
319 #define OFFSET	6.00
320 static const int nabcst[] = {
321 	83,	(int)( 45.75 * FREQFACTOR),	0,
322 	14,	(int)(471.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
323 	 7,	(int)(175.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
324 	 5,	(int)( 77.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
325 	 2,	(int)( 55.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
326 	 0
327 };
328 #undef OFFSET
329 
330 /*
331  * North American Cable Channels, IRC:
332  *
333  *  2:  55.25 mHz -  4:  67.25 mHz
334  *  5:  77.25 mHz -  6:  83.25 mHz
335  *  7: 175.25 mHz - 13: 211.25 mHz
336  * 14: 121.25 mHz - 22: 169.25 mHz
337  * 23: 217.25 mHz - 94: 643.25 mHz
338  * 95:  91.25 mHz - 99: 115.25 mHz
339  *
340  * IF freq: 45.75 mHz
341  */
342 #define OFFSET	6.00
343 static const int irccable[] = {
344 	116,    (int)( 45.75 * FREQFACTOR),     0,
345 	100,    (int)(649.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
346 	95,	(int)( 91.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
347 	23,	(int)(217.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
348 	14,	(int)(121.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
349 	 7,	(int)(175.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
350 	 5,	(int)( 77.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
351 	 2,	(int)( 55.25 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
352 	 0
353 };
354 #undef OFFSET
355 
356 /*
357  * North American Cable Channels, HRC:
358  *
359  * 2:   54 mHz  - 4:    66 mHz
360  * 5:   78 mHz  - 6:    84 mHz
361  * 7:  174 mHz  - 13:  210 mHz
362  * 14: 120 mHz  - 22:  168 mHz
363  * 23: 216 mHz  - 94:  642 mHz
364  * 95:  90 mHz  - 99:  114 mHz
365  *
366  * IF freq: 45.75 mHz
367  */
368 #define OFFSET  6.00
369 static const int hrccable[] = {
370 	116,    (int)( 45.75 * FREQFACTOR),     0,
371 	100,    (int)(648.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
372 	95,	(int)( 90.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
373 	23,	(int)(216.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
374 	14,	(int)(120.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
375 	7,	(int)(174.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
376 	5,	(int)( 78.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
377 	2,	(int)( 54.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
378 	0
379 };
380 #undef OFFSET
381 
382 /*
383  * Western European broadcast channels:
384  *
385  * (there are others that appear to vary between countries - rmt)
386  *
387  * here's the table Philips provides:
388  * caution, some of the offsets don't compute...
389  *
390  *  1	 4525	700	N21
391  *
392  *  2	 4825	700	E2
393  *  3	 5525	700	E3
394  *  4	 6225	700	E4
395  *
396  *  5	17525	700	E5
397  *  6	18225	700	E6
398  *  7	18925	700	E7
399  *  8	19625	700	E8
400  *  9	20325	700	E9
401  * 10	21025	700	E10
402  * 11	21725	700	E11
403  * 12	22425	700	E12
404  *
405  * 13	 5375	700	ITA
406  * 14	 6225	700	ITB
407  *
408  * 15	 8225	700	ITC
409  *
410  * 16	17525	700	ITD
411  * 17	18325	700	ITE
412  *
413  * 18	19225	700	ITF
414  * 19	20125	700	ITG
415  * 20	21025	700	ITH
416  *
417  * 21	47125	800	E21
418  * 22	47925	800	E22
419  * 23	48725	800	E23
420  * 24	49525	800	E24
421  * 25	50325	800	E25
422  * 26	51125	800	E26
423  * 27	51925	800	E27
424  * 28	52725	800	E28
425  * 29	53525	800	E29
426  * 30	54325	800	E30
427  * 31	55125	800	E31
428  * 32	55925	800	E32
429  * 33	56725	800	E33
430  * 34	57525	800	E34
431  * 35	58325	800	E35
432  * 36	59125	800	E36
433  * 37	59925	800	E37
434  * 38	60725	800	E38
435  * 39	61525	800	E39
436  * 40	62325	800	E40
437  * 41	63125	800	E41
438  * 42	63925	800	E42
439  * 43	64725	800	E43
440  * 44	65525	800	E44
441  * 45	66325	800	E45
442  * 46	67125	800	E46
443  * 47	67925	800	E47
444  * 48	68725	800	E48
445  * 49	69525	800	E49
446  * 50	70325	800	E50
447  * 51	71125	800	E51
448  * 52	71925	800	E52
449  * 53	72725	800	E53
450  * 54	73525	800	E54
451  * 55	74325	800	E55
452  * 56	75125	800	E56
453  * 57	75925	800	E57
454  * 58	76725	800	E58
455  * 59	77525	800	E59
456  * 60	78325	800	E60
457  * 61	79125	800	E61
458  * 62	79925	800	E62
459  * 63	80725	800	E63
460  * 64	81525	800	E64
461  * 65	82325	800	E65
462  * 66	83125	800	E66
463  * 67	83925	800	E67
464  * 68	84725	800	E68
465  * 69	85525	800	E69
466  *
467  * 70	 4575	800	IA
468  * 71	 5375	800	IB
469  * 72	 6175	800	IC
470  *
471  * 74	 6925	700	S01
472  * 75	 7625	700	S02
473  * 76	 8325	700	S03
474  *
475  * 80	10525	700	S1
476  * 81	11225	700	S2
477  * 82	11925	700	S3
478  * 83	12625	700	S4
479  * 84	13325	700	S5
480  * 85	14025	700	S6
481  * 86	14725	700	S7
482  * 87	15425	700	S8
483  * 88	16125	700	S9
484  * 89	16825	700	S10
485  * 90	23125	700	S11
486  * 91	23825	700	S12
487  * 92	24525	700	S13
488  * 93	25225	700	S14
489  * 94	25925	700	S15
490  * 95	26625	700	S16
491  * 96	27325	700	S17
492  * 97	28025	700	S18
493  * 98	28725	700	S19
494  * 99	29425	700	S20
495  *
496  *
497  * Channels S21 - S41 are taken from
498  * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
499  *
500  * 100	30325	800	S21
501  * 101	31125	800	S22
502  * 102	31925	800	S23
503  * 103	32725	800	S24
504  * 104	33525	800	S25
505  * 105	34325	800	S26
506  * 106	35125	800	S27
507  * 107	35925	800	S28
508  * 108	36725	800	S29
509  * 109	37525	800	S30
510  * 110	38325	800	S31
511  * 111	39125	800	S32
512  * 112	39925	800	S33
513  * 113	40725	800	S34
514  * 114	41525	800	S35
515  * 115	42325	800	S36
516  * 116	43125	800	S37
517  * 117	43925	800	S38
518  * 118	44725	800	S39
519  * 119	45525	800	S40
520  * 120	46325	800	S41
521  *
522  * 121	 3890	000	IFFREQ
523  *
524  */
525 static const int weurope[] = {
526        121,     (int)( 38.90 * FREQFACTOR),     0,
527        100,     (int)(303.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
528         90,     (int)(231.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
529         80,     (int)(105.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
530         74,     (int)( 69.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
531         21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
532         17,     (int)(183.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
533         16,     (int)(175.25 * FREQFACTOR),     (int)(9.00 * FREQFACTOR),
534         15,     (int)(82.25 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
535         13,     (int)(53.75 * FREQFACTOR),      (int)(8.50 * FREQFACTOR),
536          5,     (int)(175.25 * FREQFACTOR),     (int)(7.00 * FREQFACTOR),
537          2,     (int)(48.25 * FREQFACTOR),      (int)(7.00 * FREQFACTOR),
538 	 0
539 };
540 
541 /*
542  * Japanese Broadcast Channels:
543  *
544  *  1:  91.25MHz -  3: 103.25MHz
545  *  4: 171.25MHz -  7: 189.25MHz
546  *  8: 193.25MHz - 12: 217.25MHz  (VHF)
547  * 13: 471.25MHz - 62: 765.25MHz  (UHF)
548  *
549  * IF freq: 45.75 mHz
550  *  OR
551  * IF freq: 58.75 mHz
552  */
553 #define OFFSET  6.00
554 #define IF_FREQ 45.75
555 static const int jpnbcst[] = {
556 	62,     (int)(IF_FREQ * FREQFACTOR),    0,
557 	13,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
558 	 8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
559 	 4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
560 	 1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
561 	 0
562 };
563 #undef IF_FREQ
564 #undef OFFSET
565 
566 /*
567  * Japanese Cable Channels:
568  *
569  *  1:  91.25MHz -  3: 103.25MHz
570  *  4: 171.25MHz -  7: 189.25MHz
571  *  8: 193.25MHz - 12: 217.25MHz
572  * 13: 109.25MHz - 21: 157.25MHz
573  * 22: 165.25MHz
574  * 23: 223.25MHz - 63: 463.25MHz
575  *
576  * IF freq: 45.75 mHz
577  */
578 #define OFFSET  6.00
579 #define IF_FREQ 45.75
580 static const int jpncable[] = {
581 	63,     (int)(IF_FREQ * FREQFACTOR),    0,
582 	23,     (int)(223.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
583 	22,     (int)(165.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
584 	13,     (int)(109.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
585 	 8,     (int)(193.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
586 	 4,     (int)(171.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
587 	 1,     (int)( 91.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
588 	 0
589 };
590 #undef IF_FREQ
591 #undef OFFSET
592 
593 /*
594  * xUSSR Broadcast Channels:
595  *
596  *  1:  49.75MHz -  2:  59.25MHz
597  *  3:  77.25MHz -  5:  93.25MHz
598  *  6: 175.25MHz - 12: 223.25MHz
599  * 13-20 - not exist
600  * 21: 471.25MHz - 34: 575.25MHz
601  * 35: 583.25MHz - 69: 855.25MHz
602  *
603  * Cable channels
604  *
605  * 70: 111.25MHz - 77: 167.25MHz
606  * 78: 231.25MHz -107: 463.25MHz
607  *
608  * IF freq: 38.90 MHz
609  */
610 #define IF_FREQ 38.90
611 static const int xussr[] = {
612       107,     (int)(IF_FREQ * FREQFACTOR),    0,
613        78,     (int)(231.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
614        70,     (int)(111.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
615        35,     (int)(583.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
616        21,     (int)(471.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
617         6,     (int)(175.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
618         3,     (int)( 77.25 * FREQFACTOR),     (int)(8.00 * FREQFACTOR),
619         1,     (int)( 49.75 * FREQFACTOR),     (int)(9.50 * FREQFACTOR),
620         0
621 };
622 #undef IF_FREQ
623 
624 /*
625  * Australian broadcast channels
626  */
627 #define OFFSET	7.00
628 #define IF_FREQ 38.90
629 static const int australia[] = {
630        83,     (int)(IF_FREQ * FREQFACTOR),    0,
631        28,     (int)(527.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
632        10,     (int)(209.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
633         6,     (int)(175.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
634         4,     (int)( 95.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
635         3,     (int)( 86.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
636         1,     (int)( 57.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR),
637         0
638 };
639 #undef OFFSET
640 #undef IF_FREQ
641 
642 /*
643  * France broadcast channels
644  */
645 #define OFFSET 8.00
646 #define IF_FREQ 38.90
647 static const int france[] = {
648         69,     (int)(IF_FREQ * FREQFACTOR),     0,
649         21,     (int)(471.25 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
650          5,     (int)(176.00 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
651          4,     (int)( 63.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 4    */
652          3,     (int)( 60.50 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 3    */
653          1,     (int)( 47.75 * FREQFACTOR),     (int)(OFFSET * FREQFACTOR), /* 1  2 */
654          0
655 };
656 #undef OFFSET
657 #undef IF_FREQ
658 
659 static const struct {
660         const int     *ptr;
661         const char    name[BT848_MAX_CHNLSET_NAME_LEN];
662 } freqTable[] = {
663         {NULL,          ""},
664         {nabcst,        "nabcst"},
665         {irccable,      "cableirc"},
666         {hrccable,      "cablehrc"},
667         {weurope,       "weurope"},
668         {jpnbcst,       "jpnbcst"},
669         {jpncable,      "jpncable"},
670         {xussr,         "xussr"},
671         {australia,     "australia"},
672         {france,        "france"},
673 
674 };
675 
676 #define TBL_CHNL	freqTable[ bktr->tuner.chnlset ].ptr[ x ]
677 #define TBL_BASE_FREQ	freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
678 #define TBL_OFFSET	freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
679 static int
680 frequency_lookup( bktr_ptr_t bktr, int channel )
681 {
682 	int	x;
683 
684 	/* check for "> MAX channel" */
685 	x = 0;
686 	if ( channel > TBL_CHNL )
687 		return( -1 );
688 
689 	/* search the table for data */
690 	for ( x = 3; TBL_CHNL; x += 3 ) {
691 		if ( channel >= TBL_CHNL ) {
692 			return( TBL_BASE_FREQ +
693 				 ((channel - TBL_CHNL) * TBL_OFFSET) );
694 		}
695 	}
696 
697 	/* not found, must be below the MIN channel */
698 	return( -1 );
699 }
700 #undef TBL_OFFSET
701 #undef TBL_BASE_FREQ
702 #undef TBL_CHNL
703 
704 
705 #define TBL_IF	freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
706 
707 
708 /* Initialise the tuner structures in the bktr_softc */
709 /* This is needed as the tuner details are no longer globally declared */
710 
711 void    select_tuner( bktr_ptr_t bktr, int tuner_type ) {
712 	if (tuner_type < Bt848_MAX_TUNER) {
713 		bktr->card.tuner = &tuners[ tuner_type ];
714 	} else {
715 		bktr->card.tuner = NULL;
716 	}
717 }
718 
719 /*
720  * Tuner Notes:
721  * Programming the tuner properly is quite complicated.
722  * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
723  * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
724  * 87.5 Mhz to 108.0 Mhz.
725  *
726  * RF and IF.  RF = radio frequencies, it is the transmitted signal.
727  *             IF is the Intermediate Frequency (the offset from the base
728  *             signal where the video, color,  audio and NICAM signals are.
729  *
730  * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
731  * NICAM at 32.348 Mhz.
732  * Strangely enough, there is an IF (intermediate frequency) for
733  * FM Radio which is 10.7 Mhz.
734  *
735  * The tuner also works in Bands. Philips bands are
736  * FM radio band 87.50 to 108.00 MHz
737  * Low band 45.75 to 170.00 MHz
738  * Mid band 170.00 to 450.00 MHz
739  * High band 450.00 to 855.25 MHz
740  *
741  *
742  * Now we need to set the PLL on the tuner to the required freuqncy.
743  * It has a programmable divisor.
744  * For TV we want
745  *  N = 16 (freq RF(pc) + freq IF(pc))  pc is picture carrier and RF and IF
746  *  are in MHz.
747 
748  * For RADIO we want a different equation.
749  *  freq IF is 10.70 MHz (so the data sheet tells me)
750  * N = (freq RF + freq IF) / step size
751  * The step size must be set to 50 khz (so the data sheet tells me)
752  * (note this is 50 kHz, the other things are in MHz)
753  * so we end up with N = 20x(freq RF + 10.7)
754  *
755  */
756 
757 #define LOW_BAND 0
758 #define MID_BAND 1
759 #define HIGH_BAND 2
760 #define FM_RADIO_BAND 3
761 
762 
763 /* Check if these are correct for other than Philips PAL */
764 #define STATUSBIT_COLD   0x80
765 #define STATUSBIT_LOCK   0x40
766 #define STATUSBIT_TV     0x20
767 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
768 #define STATUSBIT_ADC    0x07
769 
770 /*
771  * set the frequency of the tuner
772  * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
773  * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
774  * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
775  *
776  */
777 int
778 tv_freq( bktr_ptr_t bktr, int frequency, int type )
779 {
780 	const struct TUNER*	tuner;
781 	u_char			addr;
782 	u_char			control;
783 	u_char			band;
784 	int			N;
785 	int			band_select = 0;
786 #if defined( TEST_TUNER_AFC )
787 	int			oldFrequency, afcDelta;
788 #endif
789 
790 	tuner = bktr->card.tuner;
791 	if ( tuner == NULL )
792 		return( -1 );
793 
794 	if (type == TV_FREQUENCY) {
795 		/*
796 		 * select the band based on frequency
797 		 * XXX FIXME: get the cross-over points from the tuner struct
798 		 */
799 		if ( frequency < (160 * FREQFACTOR  ) )
800 		    band_select = LOW_BAND;
801 		else if ( frequency < (454 * FREQFACTOR ) )
802 		    band_select = MID_BAND;
803 		else
804 		    band_select = HIGH_BAND;
805 
806 #if defined( TEST_TUNER_AFC )
807 		if ( bktr->tuner.afc )
808 			frequency -= 4;
809 #endif
810 		/*
811 		 * N = 16 * { fRF(pc) + fIF(pc) }
812 		 * or N = 16* fRF(pc) + 16*fIF(pc) }
813 		 * where:
814 		 *  pc is picture carrier, fRF & fIF are in MHz
815 		 *
816 		 * fortunatly, frequency is passed in as MHz * 16
817 		 * and the TBL_IF frequency is also stored in MHz * 16
818 		 */
819 		N = frequency + TBL_IF;
820 
821 		/* set the address of the PLL */
822 		addr    = bktr->card.tuner_pllAddr;
823 		control = tuner->pllControl[ band_select ];
824 		band    = tuner->bandAddrs[ band_select ];
825 
826 		if(!(band && control))		/* Don't try to set un-	*/
827 		  return(-1);			/* supported modes.	*/
828 
829 		if ( frequency > bktr->tuner.frequency ) {
830 			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
831 			i2cWrite( bktr, addr, control, band );
832 	        }
833 	        else {
834 			i2cWrite( bktr, addr, control, band );
835 			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
836        		}
837 
838 #if defined( TUNER_AFC )
839 		if ( bktr->tuner.afc == TRUE ) {
840 #if defined( TEST_TUNER_AFC )
841 			oldFrequency = frequency;
842 #endif
843 			if ( (N = do_afc( bktr, addr, N )) < 0 ) {
844 			    /* AFC failed, restore requested frequency */
845 			    N = frequency + TBL_IF;
846 #if defined( TEST_TUNER_AFC )
847 			    printf("%s: do_afc: failed to lock\n",
848 				   bktr_name(bktr));
849 #endif
850 			    i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
851 			}
852 			else
853 			    frequency = N - TBL_IF;
854 #if defined( TEST_TUNER_AFC )
855  printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
856 			    afcDelta = frequency - oldFrequency;
857  printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
858 #endif
859 			}
860 #endif /* TUNER_AFC */
861 
862 		bktr->tuner.frequency = frequency;
863 	}
864 
865 	if ( type == FM_RADIO_FREQUENCY ) {
866 		band_select = FM_RADIO_BAND;
867 
868 		/*
869 		 * N = { fRF(pc) + fIF(pc) }/step_size
870                  * The step size is 50kHz for FM radio.
871 		 * (eg after 102.35MHz comes 102.40 MHz)
872 		 * fIF is 10.7 MHz (as detailed in the specs)
873 		 *
874 		 * frequency is passed in as MHz * 100
875 		 *
876 		 * So, we have N = (frequency/100 + 10.70)  /(50/1000)
877 		 */
878 		N = (frequency + 1070)/5;
879 
880 		/* set the address of the PLL */
881 		addr    = bktr->card.tuner_pllAddr;
882 		control = tuner->pllControl[ band_select ];
883 		band    = tuner->bandAddrs[ band_select ];
884 
885 		if(!(band && control))		/* Don't try to set un-	*/
886 		  return(-1);			/* supported modes.	*/
887 
888 		band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
889 						 * the ioctls RADIO_SETMODE
890 						 * and RADIO_GETMODE */
891 
892 		i2cWrite( bktr, addr, control, band );
893 		i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
894 
895 		bktr->tuner.frequency = (N * 5) - 1070;
896 
897 
898 	}
899 
900 
901 	return( 0 );
902 }
903 
904 
905 
906 #if defined( TUNER_AFC )
907 /*
908  *
909  */
910 int
911 do_afc( bktr_ptr_t bktr, int addr, int frequency )
912 {
913 	int step;
914 	int status;
915 	int origFrequency;
916 
917 	origFrequency = frequency;
918 
919 	/* wait for first setting to take effect */
920 	tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
921 
922 	if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
923 		return( -1 );
924 
925 #if defined( TEST_TUNER_AFC )
926  printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
927 #endif
928 	for ( step = 0; step < AFC_MAX_STEP; ++step ) {
929 		if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
930 			goto fubar;
931 		if ( !(status & 0x40) ) {
932 #if defined( TEST_TUNER_AFC )
933  printf( "%s: no lock!\n", bktr_name(bktr) );
934 #endif
935 			goto fubar;
936 		}
937 
938 		switch( status & AFC_BITS ) {
939 		case AFC_FREQ_CENTERED:
940 #if defined( TEST_TUNER_AFC )
941  printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
942 #endif
943 			return( frequency );
944 
945 		case AFC_FREQ_MINUS_125:
946 		case AFC_FREQ_MINUS_62:
947 #if defined( TEST_TUNER_AFC )
948  printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
949 #endif
950 			--frequency;
951 			break;
952 
953 		case AFC_FREQ_PLUS_62:
954 		case AFC_FREQ_PLUS_125:
955 #if defined( TEST_TUNER_AFC )
956  printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
957 #endif
958 			++frequency;
959 			break;
960 		}
961 
962 		i2cWrite( bktr, addr,
963 			  (frequency>>8) & 0x7f, frequency & 0xff );
964 		DELAY( AFC_DELAY );
965 	}
966 
967  fubar:
968 	i2cWrite( bktr, addr,
969 		  (origFrequency>>8) & 0x7f, origFrequency & 0xff );
970 
971 	return( -1 );
972 }
973 #endif /* TUNER_AFC */
974 #undef TBL_IF
975 
976 
977 /*
978  * Get the Tuner status and signal strength
979  */
980 int     get_tuner_status( bktr_ptr_t bktr ) {
981 	return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
982 }
983 
984 /*
985  * set the channel of the tuner
986  */
987 int
988 tv_channel( bktr_ptr_t bktr, int channel )
989 {
990 	int frequency;
991 
992 	/* calculate the frequency according to tuner type */
993 	if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
994 		return( -1 );
995 
996 	/* set the new frequency */
997 	if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
998 		return( -1 );
999 
1000 	/* OK to update records */
1001 	return( (bktr->tuner.channel = channel) );
1002 }
1003 
1004 /*
1005  * get channelset name
1006  */
1007 int
1008 tuner_getchnlset(struct bktr_chnlset *chnlset)
1009 {
1010        if (( chnlset->index < CHNLSET_MIN ) ||
1011                ( chnlset->index > CHNLSET_MAX ))
1012                        return( EINVAL );
1013 
1014        memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1015                BT848_MAX_CHNLSET_NAME_LEN);
1016 
1017        chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1018        return( 0 );
1019 }
1020