bktr_tuner.c revision 51943
1/* $FreeBSD: head/sys/dev/bktr/bktr_tuner.c 51943 1999-10-05 01:49:43Z 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	   { 0xa0, 0x90, 0x30, 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	   { 0xa0, 0x90, 0x30, 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
602static int australia[] = {
603	83,	(int)( 45.00 * FREQFACTOR),	0,
604	28,	(int)(521.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
605	/* ch 14?? what's this, cable? Air channels are 0-11 and 28-69 */
606	14,	(int)(471.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
607	10,	(int)(203.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
608	 6,	(int)(169.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
609	 4,	(int)( 89.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
610	 3,	(int)( 80.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
611	 1,	(int)( 51.00 * FREQFACTOR),	(int)(OFFSET * FREQFACTOR),
612	 0
613};
614#undef OFFSET
615
616static struct {
617        int     *ptr;
618        char    name[BT848_MAX_CHNLSET_NAME_LEN];
619} freqTable[] = {
620        {NULL,          ""},
621        {nabcst,        "nabcst"},
622        {irccable,      "cableirc"},
623        {hrccable,      "cablehrc"},
624        {weurope,       "weurope"},
625        {jpnbcst,       "jpnbcst"},
626        {jpncable,      "jpncable"},
627        {xussr,         "xussr"},
628        {australia,     "australia"},
629
630};
631
632#define TBL_CHNL	freqTable[ bktr->tuner.chnlset ].ptr[ x ]
633#define TBL_BASE_FREQ	freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
634#define TBL_OFFSET	freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
635static int
636frequency_lookup( bktr_ptr_t bktr, int channel )
637{
638	int	x;
639
640	/* check for "> MAX channel" */
641	x = 0;
642	if ( channel > TBL_CHNL )
643		return( -1 );
644
645	/* search the table for data */
646	for ( x = 3; TBL_CHNL; x += 3 ) {
647		if ( channel >= TBL_CHNL ) {
648			return( TBL_BASE_FREQ +
649				 ((channel - TBL_CHNL) * TBL_OFFSET) );
650		}
651	}
652
653	/* not found, must be below the MIN channel */
654	return( -1 );
655}
656#undef TBL_OFFSET
657#undef TBL_BASE_FREQ
658#undef TBL_CHNL
659
660
661#define TBL_IF	freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
662
663
664/* Initialise the tuner structures in the bktr_softc */
665/* This is needed as the tuner details are no longer globally declared */
666
667void    select_tuner( bktr_ptr_t bktr, int tuner_type ) {
668	if (tuner_type < Bt848_MAX_TUNER) {
669		bktr->card.tuner = &tuners[ tuner_type ];
670	} else {
671		bktr->card.tuner = NULL;
672	}
673}
674
675/*
676 * Tuner Notes:
677 * Programming the tuner properly is quite complicated.
678 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
679 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
680 * 87.5 Mhz to 108.0 Mhz.
681 *
682 * RF and IF.  RF = radio frequencies, it is the transmitted signal.
683 *             IF is the Intermediate Frequency (the offset from the base
684 *             signal where the video, color,  audio and NICAM signals are.
685 *
686 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
687 * NICAM at 32.348 Mhz.
688 * Strangely enough, there is an IF (intermediate frequency) for
689 * FM Radio which is 10.7 Mhz.
690 *
691 * The tuner also works in Bands. Philips bands are
692 * FM radio band 87.50 to 108.00 MHz
693 * Low band 45.75 to 170.00 MHz
694 * Mid band 170.00 to 450.00 MHz
695 * High band 450.00 to 855.25 MHz
696 *
697 *
698 * Now we need to set the PLL on the tuner to the required freuqncy.
699 * It has a programmable divisor.
700 * For TV we want
701 *  N = 16 (freq RF(pc) + freq IF(pc))  pc is picture carrier and RF and IF
702 *  are in MHz.
703
704 * For RADIO we want a different equation.
705 *  freq IF is 10.70 MHz (so the data sheet tells me)
706 * N = (freq RF + freq IF) / step size
707 * The step size must be set to 50 khz (so the data sheet tells me)
708 * (note this is 50 kHz, the other things are in MHz)
709 * so we end up with N = 20x(freq RF + 10.7)
710 *
711 */
712
713#define LOW_BAND 0
714#define MID_BAND 1
715#define HIGH_BAND 2
716#define FM_RADIO_BAND 3
717
718
719/* Check if these are correct for other than Philips PAL */
720#define STATUSBIT_COLD   0x80
721#define STATUSBIT_LOCK   0x40
722#define STATUSBIT_TV     0x20
723#define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
724#define STATUSBIT_ADC    0x07
725
726/*
727 * set the frequency of the tuner
728 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
729 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
730 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
731 *
732 */
733int
734tv_freq( bktr_ptr_t bktr, int frequency, int type )
735{
736	const struct TUNER*	tuner;
737	u_char			addr;
738	u_char			control;
739	u_char			band;
740	int			N;
741	int			band_select = 0;
742#if defined( TEST_TUNER_AFC )
743	int			oldFrequency, afcDelta;
744#endif
745
746	tuner = bktr->card.tuner;
747	if ( tuner == NULL )
748		return( -1 );
749
750	if (type == TV_FREQUENCY) {
751		/*
752		 * select the band based on frequency
753		 * XXX FIXME: get the cross-over points from the tuner struct
754		 */
755		if ( frequency < (160 * FREQFACTOR  ) )
756		    band_select = LOW_BAND;
757		else if ( frequency < (454 * FREQFACTOR ) )
758		    band_select = MID_BAND;
759		else
760		    band_select = HIGH_BAND;
761
762#if defined( TEST_TUNER_AFC )
763		if ( bktr->tuner.afc )
764			frequency -= 4;
765#endif
766		/*
767		 * N = 16 * { fRF(pc) + fIF(pc) }
768		 * or N = 16* fRF(pc) + 16*fIF(pc) }
769		 * where:
770		 *  pc is picture carrier, fRF & fIF are in MHz
771		 *
772		 * fortunatly, frequency is passed in as MHz * 16
773		 * and the TBL_IF frequency is also stored in MHz * 16
774		 */
775		N = frequency + TBL_IF;
776
777		/* set the address of the PLL */
778		addr    = bktr->card.tuner_pllAddr;
779		control = tuner->pllControl[ band_select ];
780		band    = tuner->bandAddrs[ band_select ];
781
782		if(!(band && control))		/* Don't try to set un-	*/
783		  return(-1);			/* supported modes.	*/
784
785		if ( frequency > bktr->tuner.frequency ) {
786			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
787			i2cWrite( bktr, addr, control, band );
788	        }
789	        else {
790			i2cWrite( bktr, addr, control, band );
791			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
792       		}
793
794#if defined( TUNER_AFC )
795		if ( bktr->tuner.afc == TRUE ) {
796#if defined( TEST_TUNER_AFC )
797			oldFrequency = frequency;
798#endif
799			if ( (N = do_afc( bktr, addr, N )) < 0 ) {
800			    /* AFC failed, restore requested frequency */
801			    N = frequency + TBL_IF;
802#if defined( TEST_TUNER_AFC )
803			    printf("do_afc: failed to lock\n");
804#endif
805			    i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
806			}
807			else
808			    frequency = N - TBL_IF;
809#if defined( TEST_TUNER_AFC )
810 printf("do_afc: returned freq %d (%d %% %d)\n", frequency, frequency / 16, frequency % 16);
811			    afcDelta = frequency - oldFrequency;
812 printf("changed by: %d clicks (%d mod %d)\n", afcDelta, afcDelta / 16, afcDelta % 16);
813#endif
814			}
815#endif /* TUNER_AFC */
816
817		bktr->tuner.frequency = frequency;
818	}
819
820	if ( type == FM_RADIO_FREQUENCY ) {
821		band_select = FM_RADIO_BAND;
822
823		/*
824		 * N = { fRF(pc) + fIF(pc) }/step_size
825                 * The step size is 50kHz for FM radio.
826		 * (eg after 102.35MHz comes 102.40 MHz)
827		 * fIF is 10.7 MHz (as detailed in the specs)
828		 *
829		 * frequency is passed in as MHz * 100
830		 *
831		 * So, we have N = (frequency/100 + 10.70)  /(50/1000)
832		 */
833		N = (frequency + 1070)/5;
834
835		/* set the address of the PLL */
836		addr    = bktr->card.tuner_pllAddr;
837		control = tuner->pllControl[ band_select ];
838		band    = tuner->bandAddrs[ band_select ];
839
840		if(!(band && control))		/* Don't try to set un-	*/
841		  return(-1);			/* supported modes.	*/
842
843		band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
844						 * the ioctls RADIO_SETMODE
845						 * and RADIO_GETMODE */
846
847		i2cWrite( bktr, addr, control, band );
848		i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
849
850		bktr->tuner.frequency = (N * 5) - 1070;
851
852
853	}
854
855
856	return( 0 );
857}
858
859
860
861#if defined( TUNER_AFC )
862/*
863 *
864 */
865int
866do_afc( bktr_ptr_t bktr, int addr, int frequency )
867{
868	int step;
869	int status;
870	int origFrequency;
871
872	origFrequency = frequency;
873
874	/* wait for first setting to take effect */
875	tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
876
877	if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
878		return( -1 );
879
880#if defined( TEST_TUNER_AFC )
881 printf( "\nOriginal freq: %d, status: 0x%02x\n", frequency, status );
882#endif
883	for ( step = 0; step < AFC_MAX_STEP; ++step ) {
884		if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
885			goto fubar;
886		if ( !(status & 0x40) ) {
887#if defined( TEST_TUNER_AFC )
888 printf( "no lock!\n" );
889#endif
890			goto fubar;
891		}
892
893		switch( status & AFC_BITS ) {
894		case AFC_FREQ_CENTERED:
895#if defined( TEST_TUNER_AFC )
896 printf( "Centered, freq: %d, status: 0x%02x\n", frequency, status );
897#endif
898			return( frequency );
899
900		case AFC_FREQ_MINUS_125:
901		case AFC_FREQ_MINUS_62:
902#if defined( TEST_TUNER_AFC )
903 printf( "Low, freq: %d, status: 0x%02x\n", frequency, status );
904#endif
905			--frequency;
906			break;
907
908		case AFC_FREQ_PLUS_62:
909		case AFC_FREQ_PLUS_125:
910#if defined( TEST_TUNER_AFC )
911 printf( "Hi, freq: %d, status: 0x%02x\n", frequency, status );
912#endif
913			++frequency;
914			break;
915		}
916
917		i2cWrite( bktr, addr,
918			  (frequency>>8) & 0x7f, frequency & 0xff );
919		DELAY( AFC_DELAY );
920	}
921
922 fubar:
923	i2cWrite( bktr, addr,
924		  (origFrequency>>8) & 0x7f, origFrequency & 0xff );
925
926	return( -1 );
927}
928#endif /* TUNER_AFC */
929#undef TBL_IF
930
931
932/*
933 * Get the Tuner status and signal strength
934 */
935int     get_tuner_status( bktr_ptr_t bktr ) {
936	return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
937}
938
939/*
940 * set the channel of the tuner
941 */
942int
943tv_channel( bktr_ptr_t bktr, int channel )
944{
945	int frequency;
946
947	/* calculate the frequency according to tuner type */
948	if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
949		return( -1 );
950
951	/* set the new frequency */
952	if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
953		return( -1 );
954
955	/* OK to update records */
956	return( (bktr->tuner.channel = channel) );
957}
958
959/*
960 * get channelset name
961 */
962int
963tuner_getchnlset(struct bktr_chnlset *chnlset)
964{
965       if (( chnlset->index < CHNLSET_MIN ) ||
966               ( chnlset->index > CHNLSET_MAX ))
967                       return( EINVAL );
968
969       memcpy(&chnlset->name, &freqTable[chnlset->index].name,
970               BT848_MAX_CHNLSET_NAME_LEN);
971
972       chnlset->max_channel=freqTable[chnlset->index].ptr[0];
973       return( 0 );
974}
975