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