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