Home | History | Annotate | Line # | Download | only in bktr
bktr_tuner.c revision 1.4
      1 /*	$NetBSD: bktr_tuner.c,v 1.4 2000/06/25 17:47:39 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 
     62 #if (__FreeBSD_version >=300000)
     63 #include <machine/bus_memio.h>          /* for bus space */
     64 #include <machine/bus.h>
     65 #include <sys/bus.h>
     66 #endif
     67 #endif
     68 
     69 #ifdef __NetBSD__
     70 #include <dev/ic/bt8xx.h>	/* NetBSD .h file location */
     71 #include <dev/pci/bktr/bktr_reg.h>
     72 #include <dev/pci/bktr/bktr_tuner.h>
     73 #include <dev/pci/bktr/bktr_card.h>
     74 #include <dev/pci/bktr/bktr_core.h>
     75 #else
     76 #include <machine/ioctl_meteor.h>	/* Traditional .h file location */
     77 #include <machine/ioctl_bt848.h>        /* extensions to ioctl_meteor.h */
     78 #include <dev/bktr/bktr_reg.h>
     79 #include <dev/bktr/bktr_tuner.h>
     80 #include <dev/bktr/bktr_card.h>
     81 #include <dev/bktr/bktr_core.h>
     82 #endif
     83 
     84 
     85 
     86 #if defined( TUNER_AFC )
     87 #define AFC_DELAY               10000   /* 10 millisend delay */
     88 #define AFC_BITS                0x07
     89 #define AFC_FREQ_MINUS_125      0x00
     90 #define AFC_FREQ_MINUS_62       0x01
     91 #define AFC_FREQ_CENTERED       0x02
     92 #define AFC_FREQ_PLUS_62        0x03
     93 #define AFC_FREQ_PLUS_125       0x04
     94 #define AFC_MAX_STEP            (5 * FREQFACTOR) /* no more than 5 MHz */
     95 #endif /* TUNER_AFC */
     96 
     97 
     98 #define TTYPE_XXX               0
     99 #define TTYPE_NTSC              1
    100 #define TTYPE_NTSC_J            2
    101 #define TTYPE_PAL               3
    102 #define TTYPE_PAL_M             4
    103 #define TTYPE_PAL_N             5
    104 #define TTYPE_SECAM             6
    105 
    106 #define TSA552x_CB_MSB          (0x80)
    107 #define TSA552x_CB_CP           (1<<6)	/* set this for fast tuning */
    108 #define TSA552x_CB_T2           (1<<5)	/* test mode - Normally set to 0 */
    109 #define TSA552x_CB_T1           (1<<4)	/* test mode - Normally set to 0 */
    110 #define TSA552x_CB_T0           (1<<3)	/* test mode - Normally set to 1 */
    111 #define TSA552x_CB_RSA          (1<<2)	/* 0 for 31.25 khz, 1 for 62.5 kHz */
    112 #define TSA552x_CB_RSB          (1<<1)	/* 0 for FM 50kHz steps, 1 = Use RSA*/
    113 #define TSA552x_CB_OS           (1<<0)	/* Set to 0 for normal operation */
    114 
    115 #define TSA552x_RADIO           (TSA552x_CB_MSB |       \
    116                                  TSA552x_CB_T0)
    117 
    118 /* raise the charge pump voltage for fast tuning */
    119 #define TSA552x_FCONTROL        (TSA552x_CB_MSB |       \
    120                                  TSA552x_CB_CP  |       \
    121                                  TSA552x_CB_T0  |       \
    122                                  TSA552x_CB_RSA |       \
    123                                  TSA552x_CB_RSB)
    124 
    125 /* lower the charge pump voltage for better residual oscillator FM */
    126 #define TSA552x_SCONTROL        (TSA552x_CB_MSB |       \
    127                                  TSA552x_CB_T0  |       \
    128                                  TSA552x_CB_RSA |       \
    129                                  TSA552x_CB_RSB)
    130 
    131 /* The control value for the ALPS TSCH5 Tuner */
    132 #define TSCH5_FCONTROL          0x82
    133 #define TSCH5_RADIO             0x86
    134 
    135 /* The control value for the ALPS TSBH1 Tuner */
    136 #define TSBH1_FCONTROL		0xce
    137 
    138 
    139 static const struct TUNER tuners[] = {
    140 /* XXX FIXME: fill in the band-switch crosspoints */
    141 	/* NO_TUNER */
    142 	{ "<no>",				/* the 'name' */
    143 	   TTYPE_XXX,				/* input type */
    144  	   { 0x00,				/* control byte for Tuner PLL */
    145  	     0x00,
    146  	     0x00,
    147  	     0x00 },
    148 	   { 0x00, 0x00 },			/* band-switch crosspoints */
    149 	   { 0x00, 0x00, 0x00,0x00} },		/* the band-switch values */
    150 
    151 	/* TEMIC_NTSC */
    152 	{ "Temic NTSC",				/* the 'name' */
    153 	   TTYPE_NTSC,				/* input type */
    154 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
    155 	     TSA552x_SCONTROL,
    156 	     TSA552x_SCONTROL,
    157 	     0x00 },
    158 	   { 0x00, 0x00},			/* band-switch crosspoints */
    159 	   { 0x02, 0x04, 0x01, 0x00 } },	/* the band-switch values */
    160 
    161 	/* TEMIC_PAL */
    162 	{ "Temic PAL",				/* the 'name' */
    163 	   TTYPE_PAL,				/* input type */
    164 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
    165 	     TSA552x_SCONTROL,
    166 	     TSA552x_SCONTROL,
    167 	     0x00 },
    168 	   { 0x00, 0x00 },			/* band-switch crosspoints */
    169 	   { 0x02, 0x04, 0x01, 0x00 } },	/* the band-switch values */
    170 
    171 	/* TEMIC_SECAM */
    172 	{ "Temic SECAM",			/* the 'name' */
    173 	   TTYPE_SECAM,				/* input type */
    174 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
    175 	     TSA552x_SCONTROL,
    176 	     TSA552x_SCONTROL,
    177 	     0x00 },
    178 	   { 0x00, 0x00 },			/* band-switch crosspoints */
    179 	   { 0x02, 0x04, 0x01,0x00 } },		/* the band-switch values */
    180 
    181 	/* PHILIPS_NTSC */
    182 	{ "Philips NTSC",			/* the 'name' */
    183 	   TTYPE_NTSC,				/* input type */
    184 	   { TSA552x_SCONTROL,			/* control byte for Tuner PLL */
    185 	     TSA552x_SCONTROL,
    186 	     TSA552x_SCONTROL,
    187 	     0x00 },
    188 	   { 0x00, 0x00 },			/* band-switch crosspoints */
    189 	   { 0xa0, 0x90, 0x30, 0x00 } },	/* the band-switch values */
    190 
    191 	/* 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
    316 static 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
    339 static 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
    365 static 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  */
    521 static 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
    551 static 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
    576 static 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
    607 static 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
    625 static 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
    643 static 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 
    655 static 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 ]
    675 static int
    676 frequency_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	freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
    702 
    703 
    704 /* Initialise the tuner structures in the bktr_softc */
    705 /* This is needed as the tuner details are no longer globally declared */
    706 
    707 void    select_tuner( bktr_ptr_t bktr, int tuner_type ) {
    708 	if (tuner_type < Bt848_MAX_TUNER) {
    709 		bktr->card.tuner = &tuners[ tuner_type ];
    710 	} else {
    711 		bktr->card.tuner = NULL;
    712 	}
    713 }
    714 
    715 /*
    716  * Tuner Notes:
    717  * Programming the tuner properly is quite complicated.
    718  * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
    719  * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
    720  * 87.5 Mhz to 108.0 Mhz.
    721  *
    722  * RF and IF.  RF = radio frequencies, it is the transmitted signal.
    723  *             IF is the Intermediate Frequency (the offset from the base
    724  *             signal where the video, color,  audio and NICAM signals are.
    725  *
    726  * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
    727  * NICAM at 32.348 Mhz.
    728  * Strangely enough, there is an IF (intermediate frequency) for
    729  * FM Radio which is 10.7 Mhz.
    730  *
    731  * The tuner also works in Bands. Philips bands are
    732  * FM radio band 87.50 to 108.00 MHz
    733  * Low band 45.75 to 170.00 MHz
    734  * Mid band 170.00 to 450.00 MHz
    735  * High band 450.00 to 855.25 MHz
    736  *
    737  *
    738  * Now we need to set the PLL on the tuner to the required freuqncy.
    739  * It has a programmable divisor.
    740  * For TV we want
    741  *  N = 16 (freq RF(pc) + freq IF(pc))  pc is picture carrier and RF and IF
    742  *  are in MHz.
    743 
    744  * For RADIO we want a different equation.
    745  *  freq IF is 10.70 MHz (so the data sheet tells me)
    746  * N = (freq RF + freq IF) / step size
    747  * The step size must be set to 50 khz (so the data sheet tells me)
    748  * (note this is 50 kHz, the other things are in MHz)
    749  * so we end up with N = 20x(freq RF + 10.7)
    750  *
    751  */
    752 
    753 #define LOW_BAND 0
    754 #define MID_BAND 1
    755 #define HIGH_BAND 2
    756 #define FM_RADIO_BAND 3
    757 
    758 
    759 /* Check if these are correct for other than Philips PAL */
    760 #define STATUSBIT_COLD   0x80
    761 #define STATUSBIT_LOCK   0x40
    762 #define STATUSBIT_TV     0x20
    763 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
    764 #define STATUSBIT_ADC    0x07
    765 
    766 /*
    767  * set the frequency of the tuner
    768  * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
    769  * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
    770  * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
    771  *
    772  */
    773 int
    774 tv_freq( bktr_ptr_t bktr, int frequency, int type )
    775 {
    776 	const struct TUNER*	tuner;
    777 	u_char			addr;
    778 	u_char			control;
    779 	u_char			band;
    780 	int			N;
    781 	int			band_select = 0;
    782 #if defined( TEST_TUNER_AFC )
    783 	int			oldFrequency, afcDelta;
    784 #endif
    785 
    786 	tuner = bktr->card.tuner;
    787 	if ( tuner == NULL )
    788 		return( -1 );
    789 
    790 	if (type == TV_FREQUENCY) {
    791 		/*
    792 		 * select the band based on frequency
    793 		 * XXX FIXME: get the cross-over points from the tuner struct
    794 		 */
    795 		if ( frequency < (160 * FREQFACTOR  ) )
    796 		    band_select = LOW_BAND;
    797 		else if ( frequency < (454 * FREQFACTOR ) )
    798 		    band_select = MID_BAND;
    799 		else
    800 		    band_select = HIGH_BAND;
    801 
    802 #if defined( TEST_TUNER_AFC )
    803 		if ( bktr->tuner.afc )
    804 			frequency -= 4;
    805 #endif
    806 		/*
    807 		 * N = 16 * { fRF(pc) + fIF(pc) }
    808 		 * or N = 16* fRF(pc) + 16*fIF(pc) }
    809 		 * where:
    810 		 *  pc is picture carrier, fRF & fIF are in MHz
    811 		 *
    812 		 * fortunatly, frequency is passed in as MHz * 16
    813 		 * and the TBL_IF frequency is also stored in MHz * 16
    814 		 */
    815 		N = frequency + TBL_IF;
    816 
    817 		/* set the address of the PLL */
    818 		addr    = bktr->card.tuner_pllAddr;
    819 		control = tuner->pllControl[ band_select ];
    820 		band    = tuner->bandAddrs[ band_select ];
    821 
    822 		if(!(band && control))		/* Don't try to set un-	*/
    823 		  return(-1);			/* supported modes.	*/
    824 
    825 		if ( frequency > bktr->tuner.frequency ) {
    826 			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
    827 			i2cWrite( bktr, addr, control, band );
    828 	        }
    829 	        else {
    830 			i2cWrite( bktr, addr, control, band );
    831 			i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
    832        		}
    833 
    834 #if defined( TUNER_AFC )
    835 		if ( bktr->tuner.afc == TRUE ) {
    836 #if defined( TEST_TUNER_AFC )
    837 			oldFrequency = frequency;
    838 #endif
    839 			if ( (N = do_afc( bktr, addr, N )) < 0 ) {
    840 			    /* AFC failed, restore requested frequency */
    841 			    N = frequency + TBL_IF;
    842 #if defined( TEST_TUNER_AFC )
    843 			    printf("%s: do_afc: failed to lock\n",
    844 				   bktr_name(bktr));
    845 #endif
    846 			    i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
    847 			}
    848 			else
    849 			    frequency = N - TBL_IF;
    850 #if defined( TEST_TUNER_AFC )
    851  printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
    852 			    afcDelta = frequency - oldFrequency;
    853  printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
    854 #endif
    855 			}
    856 #endif /* TUNER_AFC */
    857 
    858 		bktr->tuner.frequency = frequency;
    859 	}
    860 
    861 	if ( type == FM_RADIO_FREQUENCY ) {
    862 		band_select = FM_RADIO_BAND;
    863 
    864 		/*
    865 		 * N = { fRF(pc) + fIF(pc) }/step_size
    866                  * The step size is 50kHz for FM radio.
    867 		 * (eg after 102.35MHz comes 102.40 MHz)
    868 		 * fIF is 10.7 MHz (as detailed in the specs)
    869 		 *
    870 		 * frequency is passed in as MHz * 100
    871 		 *
    872 		 * So, we have N = (frequency/100 + 10.70)  /(50/1000)
    873 		 */
    874 		N = (frequency + 1070)/5;
    875 
    876 		/* set the address of the PLL */
    877 		addr    = bktr->card.tuner_pllAddr;
    878 		control = tuner->pllControl[ band_select ];
    879 		band    = tuner->bandAddrs[ band_select ];
    880 
    881 		if(!(band && control))		/* Don't try to set un-	*/
    882 		  return(-1);			/* supported modes.	*/
    883 
    884 		band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
    885 						 * the ioctls RADIO_SETMODE
    886 						 * and RADIO_GETMODE */
    887 
    888 		i2cWrite( bktr, addr, control, band );
    889 		i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
    890 
    891 		bktr->tuner.frequency = (N * 5) - 1070;
    892 
    893 
    894 	}
    895 
    896 
    897 	return( 0 );
    898 }
    899 
    900 
    901 
    902 #if defined( TUNER_AFC )
    903 /*
    904  *
    905  */
    906 int
    907 do_afc( bktr_ptr_t bktr, int addr, int frequency )
    908 {
    909 	int step;
    910 	int status;
    911 	int origFrequency;
    912 
    913 	origFrequency = frequency;
    914 
    915 	/* wait for first setting to take effect */
    916 	tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
    917 
    918 	if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
    919 		return( -1 );
    920 
    921 #if defined( TEST_TUNER_AFC )
    922  printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
    923 #endif
    924 	for ( step = 0; step < AFC_MAX_STEP; ++step ) {
    925 		if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
    926 			goto fubar;
    927 		if ( !(status & 0x40) ) {
    928 #if defined( TEST_TUNER_AFC )
    929  printf( "%s: no lock!\n", bktr_name(bktr) );
    930 #endif
    931 			goto fubar;
    932 		}
    933 
    934 		switch( status & AFC_BITS ) {
    935 		case AFC_FREQ_CENTERED:
    936 #if defined( TEST_TUNER_AFC )
    937  printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
    938 #endif
    939 			return( frequency );
    940 
    941 		case AFC_FREQ_MINUS_125:
    942 		case AFC_FREQ_MINUS_62:
    943 #if defined( TEST_TUNER_AFC )
    944  printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
    945 #endif
    946 			--frequency;
    947 			break;
    948 
    949 		case AFC_FREQ_PLUS_62:
    950 		case AFC_FREQ_PLUS_125:
    951 #if defined( TEST_TUNER_AFC )
    952  printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
    953 #endif
    954 			++frequency;
    955 			break;
    956 		}
    957 
    958 		i2cWrite( bktr, addr,
    959 			  (frequency>>8) & 0x7f, frequency & 0xff );
    960 		DELAY( AFC_DELAY );
    961 	}
    962 
    963  fubar:
    964 	i2cWrite( bktr, addr,
    965 		  (origFrequency>>8) & 0x7f, origFrequency & 0xff );
    966 
    967 	return( -1 );
    968 }
    969 #endif /* TUNER_AFC */
    970 #undef TBL_IF
    971 
    972 
    973 /*
    974  * Get the Tuner status and signal strength
    975  */
    976 int     get_tuner_status( bktr_ptr_t bktr ) {
    977 	return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
    978 }
    979 
    980 /*
    981  * set the channel of the tuner
    982  */
    983 int
    984 tv_channel( bktr_ptr_t bktr, int channel )
    985 {
    986 	int frequency;
    987 
    988 	/* calculate the frequency according to tuner type */
    989 	if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
    990 		return( -1 );
    991 
    992 	/* set the new frequency */
    993 	if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
    994 		return( -1 );
    995 
    996 	/* OK to update records */
    997 	return( (bktr->tuner.channel = channel) );
    998 }
    999 
   1000 /*
   1001  * get channelset name
   1002  */
   1003 int
   1004 tuner_getchnlset(struct bktr_chnlset *chnlset)
   1005 {
   1006        if (( chnlset->index < CHNLSET_MIN ) ||
   1007                ( chnlset->index > CHNLSET_MAX ))
   1008                        return( EINVAL );
   1009 
   1010        memcpy(&chnlset->name, &freqTable[chnlset->index].name,
   1011                BT848_MAX_CHNLSET_NAME_LEN);
   1012 
   1013        chnlset->max_channel=freqTable[chnlset->index].ptr[0];
   1014        return( 0 );
   1015 }
   1016