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