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