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