1 /* $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.5.2.3 2000/10/26 16:38:46 roger Exp $ */
4 * This is part of the Driver for Video Capture Cards (Frame grabbers)
5 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
7 * Copyright Roger Hardiman and Amancio Hasty.
9 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
14 * 1. Redistributions of source code must retain the
15 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by Amancio Hasty and
30 * 4. The name of the author may not be used to endorse or promote products
31 * derived from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
42 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 * POSSIBILITY OF SUCH DAMAGE.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/vnode.h>
57 #include <pci/pcivar.h>
59 #if (__FreeBSD_version < 500000)
60 #include <machine/clock.h> /* for DELAY */
63 #if (__FreeBSD_version >=300000)
64 #include <machine/bus_memio.h> /* for bus space */
65 #include <machine/bus.h>
71 #include <dev/ic/bt8xx.h> /* NetBSD .h file location */
72 #include <dev/pci/bktr/bktr_reg.h>
73 #include <dev/pci/bktr/bktr_tuner.h>
74 #include <dev/pci/bktr/bktr_card.h>
75 #include <dev/pci/bktr/bktr_core.h>
77 #include <machine/ioctl_meteor.h> /* Traditional .h file location */
78 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
79 #include <dev/bktr/bktr_reg.h>
80 #include <dev/bktr/bktr_tuner.h>
81 #include <dev/bktr/bktr_card.h>
82 #include <dev/bktr/bktr_core.h>
87 #if defined( TUNER_AFC )
88 #define AFC_DELAY 10000 /* 10 millisend delay */
90 #define AFC_FREQ_MINUS_125 0x00
91 #define AFC_FREQ_MINUS_62 0x01
92 #define AFC_FREQ_CENTERED 0x02
93 #define AFC_FREQ_PLUS_62 0x03
94 #define AFC_FREQ_PLUS_125 0x04
95 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
96 #endif /* TUNER_AFC */
101 #define TTYPE_NTSC_J 2
103 #define TTYPE_PAL_M 4
104 #define TTYPE_PAL_N 5
105 #define TTYPE_SECAM 6
107 #define TSA552x_CB_MSB (0x80)
108 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
109 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
110 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
111 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
112 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
113 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
114 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
116 #define TSA552x_RADIO (TSA552x_CB_MSB | \
119 /* raise the charge pump voltage for fast tuning */
120 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
126 /* lower the charge pump voltage for better residual oscillator FM */
127 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
132 /* The control value for the ALPS TSCH5 Tuner */
133 #define TSCH5_FCONTROL 0x82
134 #define TSCH5_RADIO 0x86
136 /* The control value for the ALPS TSBH1 Tuner */
137 #define TSBH1_FCONTROL 0xce
140 static const struct TUNER tuners[] = {
141 /* XXX FIXME: fill in the band-switch crosspoints */
143 { "<no>", /* the 'name' */
144 TTYPE_XXX, /* input type */
145 { 0x00, /* control byte for Tuner PLL */
149 { 0x00, 0x00 }, /* band-switch crosspoints */
150 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
153 { "Temic NTSC", /* the 'name' */
154 TTYPE_NTSC, /* input type */
155 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
159 { 0x00, 0x00}, /* band-switch crosspoints */
160 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
163 { "Temic PAL", /* the 'name' */
164 TTYPE_PAL, /* input type */
165 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
169 { 0x00, 0x00 }, /* band-switch crosspoints */
170 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
173 { "Temic SECAM", /* the 'name' */
174 TTYPE_SECAM, /* input type */
175 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
179 { 0x00, 0x00 }, /* band-switch crosspoints */
180 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
183 { "Philips NTSC", /* the 'name' */
184 TTYPE_NTSC, /* input type */
185 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
189 { 0x00, 0x00 }, /* band-switch crosspoints */
190 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
193 { "Philips PAL", /* the 'name' */
194 TTYPE_PAL, /* input type */
195 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
199 { 0x00, 0x00 }, /* band-switch crosspoints */
200 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
203 { "Philips SECAM", /* the 'name' */
204 TTYPE_SECAM, /* input type */
205 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
209 { 0x00, 0x00 }, /* band-switch crosspoints */
210 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
213 { "Temic PAL I", /* the 'name' */
214 TTYPE_PAL, /* input type */
215 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
219 { 0x00, 0x00 }, /* band-switch crosspoints */
220 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
223 { "Philips PAL I", /* the 'name' */
224 TTYPE_PAL, /* input type */
225 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
229 { 0x00, 0x00 }, /* band-switch crosspoints */
230 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
232 /* PHILIPS_FR1236_NTSC */
233 { "Philips FR1236 NTSC FM", /* the 'name' */
234 TTYPE_NTSC, /* input type */
235 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
239 { 0x00, 0x00 }, /* band-switch crosspoints */
240 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
242 /* PHILIPS_FR1216_PAL */
243 { "Philips FR1216 PAL FM" , /* the 'name' */
244 TTYPE_PAL, /* input type */
245 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
249 { 0x00, 0x00 }, /* band-switch crosspoints */
250 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
252 /* PHILIPS_FR1236_SECAM */
253 { "Philips FR1236 SECAM FM", /* the 'name' */
254 TTYPE_SECAM, /* input type */
255 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
259 { 0x00, 0x00 }, /* band-switch crosspoints */
260 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
262 /* ALPS TSCH5 NTSC */
263 { "ALPS TSCH5 NTSC FM", /* the 'name' */
264 TTYPE_NTSC, /* input type */
265 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
269 { 0x00, 0x00 }, /* band-switch crosspoints */
270 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
272 /* ALPS TSBH1 NTSC */
273 { "ALPS TSBH1 NTSC", /* the 'name' */
274 TTYPE_NTSC, /* input type */
275 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
279 { 0x00, 0x00 }, /* band-switch crosspoints */
280 { 0x01, 0x02, 0x08, 0x00 } } /* the band-switch values */
284 /* scaling factor for frequencies expressed as ints */
285 #define FREQFACTOR 16
289 * entry 0: MAX legal channel
290 * entry 1: IF frequency
291 * expressed as fi{mHz} * 16,
292 * eg 45.75mHz == 45.75 * 16 = 732
293 * entry 2: [place holder/future]
294 * entry 3: base of channel record 0
295 * entry 3 + (x*3): base of channel record 'x'
296 * entry LAST: NULL channel entry marking end of records
299 * int 0: base channel
300 * int 1: frequency of base channel,
301 * expressed as fb{mHz} * 16,
302 * int 2: offset frequency between channels,
303 * expressed as fo{mHz} * 16,
307 * North American Broadcast Channels:
309 * 2: 55.25 mHz - 4: 67.25 mHz
310 * 5: 77.25 mHz - 6: 83.25 mHz
311 * 7: 175.25 mHz - 13: 211.25 mHz
312 * 14: 471.25 mHz - 83: 885.25 mHz
317 static int nabcst[] = {
318 83, (int)( 45.75 * FREQFACTOR), 0,
319 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
320 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
321 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
322 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
328 * North American Cable Channels, IRC:
330 * 2: 55.25 mHz - 4: 67.25 mHz
331 * 5: 77.25 mHz - 6: 83.25 mHz
332 * 7: 175.25 mHz - 13: 211.25 mHz
333 * 14: 121.25 mHz - 22: 169.25 mHz
334 * 23: 217.25 mHz - 94: 643.25 mHz
335 * 95: 91.25 mHz - 99: 115.25 mHz
340 static int irccable[] = {
341 116, (int)( 45.75 * FREQFACTOR), 0,
342 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
343 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
344 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
345 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
346 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
347 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
348 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
354 * North American Cable Channels, HRC:
356 * 2: 54 mHz - 4: 66 mHz
357 * 5: 78 mHz - 6: 84 mHz
358 * 7: 174 mHz - 13: 210 mHz
359 * 14: 120 mHz - 22: 168 mHz
360 * 23: 216 mHz - 94: 642 mHz
361 * 95: 90 mHz - 99: 114 mHz
366 static int hrccable[] = {
367 116, (int)( 45.75 * FREQFACTOR), 0,
368 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
369 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
370 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
371 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
372 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
373 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
374 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
380 * Western European broadcast channels:
382 * (there are others that appear to vary between countries - rmt)
384 * here's the table Philips provides:
385 * caution, some of the offsets don't compute...
494 * Channels S21 - S41 are taken from
495 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
519 * 121 3890 000 IFFREQ
522 static int weurope[] = {
523 121, (int)( 38.90 * FREQFACTOR), 0,
524 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
525 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
526 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
527 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
528 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
529 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
530 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
531 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
532 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
533 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
534 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
539 * Japanese Broadcast Channels:
541 * 1: 91.25MHz - 3: 103.25MHz
542 * 4: 171.25MHz - 7: 189.25MHz
543 * 8: 193.25MHz - 12: 217.25MHz (VHF)
544 * 13: 471.25MHz - 62: 765.25MHz (UHF)
551 #define IF_FREQ 45.75
552 static int jpnbcst[] = {
553 62, (int)(IF_FREQ * FREQFACTOR), 0,
554 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
555 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
556 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
557 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
564 * Japanese Cable Channels:
566 * 1: 91.25MHz - 3: 103.25MHz
567 * 4: 171.25MHz - 7: 189.25MHz
568 * 8: 193.25MHz - 12: 217.25MHz
569 * 13: 109.25MHz - 21: 157.25MHz
571 * 23: 223.25MHz - 63: 463.25MHz
576 #define IF_FREQ 45.75
577 static int jpncable[] = {
578 63, (int)(IF_FREQ * FREQFACTOR), 0,
579 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
580 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
581 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
582 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
583 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
584 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
591 * xUSSR Broadcast Channels:
593 * 1: 49.75MHz - 2: 59.25MHz
594 * 3: 77.25MHz - 5: 93.25MHz
595 * 6: 175.25MHz - 12: 223.25MHz
597 * 21: 471.25MHz - 34: 575.25MHz
598 * 35: 583.25MHz - 69: 855.25MHz
602 * 70: 111.25MHz - 77: 167.25MHz
603 * 78: 231.25MHz -107: 463.25MHz
607 #define IF_FREQ 38.90
608 static int xussr[] = {
609 107, (int)(IF_FREQ * FREQFACTOR), 0,
610 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
611 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
612 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
613 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
614 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
615 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
616 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
622 * Australian broadcast channels
625 #define IF_FREQ 38.90
626 static int australia[] = {
627 83, (int)(IF_FREQ * FREQFACTOR), 0,
628 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
629 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
630 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
631 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
632 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
633 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
640 * France broadcast channels
643 #define IF_FREQ 38.90
644 static int france[] = {
645 69, (int)(IF_FREQ * FREQFACTOR), 0,
646 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
647 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
648 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
649 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
650 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
658 char name[BT848_MAX_CHNLSET_NAME_LEN];
662 {irccable, "cableirc"},
663 {hrccable, "cablehrc"},
664 {weurope, "weurope"},
665 {jpnbcst, "jpnbcst"},
666 {jpncable, "jpncable"},
668 {australia, "australia"},
673 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
674 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
675 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
677 frequency_lookup( bktr_ptr_t bktr, int channel )
681 /* check for "> MAX channel" */
683 if ( channel > TBL_CHNL )
686 /* search the table for data */
687 for ( x = 3; TBL_CHNL; x += 3 ) {
688 if ( channel >= TBL_CHNL ) {
689 return( TBL_BASE_FREQ +
690 ((channel - TBL_CHNL) * TBL_OFFSET) );
694 /* not found, must be below the MIN channel */
702 #define TBL_IF freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
705 /* Initialise the tuner structures in the bktr_softc */
706 /* This is needed as the tuner details are no longer globally declared */
708 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
709 if (tuner_type < Bt848_MAX_TUNER) {
710 bktr->card.tuner = &tuners[ tuner_type ];
712 bktr->card.tuner = NULL;
718 * Programming the tuner properly is quite complicated.
719 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
720 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
721 * 87.5 Mhz to 108.0 Mhz.
723 * RF and IF. RF = radio frequencies, it is the transmitted signal.
724 * IF is the Intermediate Frequency (the offset from the base
725 * signal where the video, color, audio and NICAM signals are.
727 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
728 * NICAM at 32.348 Mhz.
729 * Strangely enough, there is an IF (intermediate frequency) for
730 * FM Radio which is 10.7 Mhz.
732 * The tuner also works in Bands. Philips bands are
733 * FM radio band 87.50 to 108.00 MHz
734 * Low band 45.75 to 170.00 MHz
735 * Mid band 170.00 to 450.00 MHz
736 * High band 450.00 to 855.25 MHz
739 * Now we need to set the PLL on the tuner to the required freuqncy.
740 * It has a programmable divisor.
742 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
745 * For RADIO we want a different equation.
746 * freq IF is 10.70 MHz (so the data sheet tells me)
747 * N = (freq RF + freq IF) / step size
748 * The step size must be set to 50 khz (so the data sheet tells me)
749 * (note this is 50 kHz, the other things are in MHz)
750 * so we end up with N = 20x(freq RF + 10.7)
757 #define FM_RADIO_BAND 3
760 /* Check if these are correct for other than Philips PAL */
761 #define STATUSBIT_COLD 0x80
762 #define STATUSBIT_LOCK 0x40
763 #define STATUSBIT_TV 0x20
764 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
765 #define STATUSBIT_ADC 0x07
768 * set the frequency of the tuner
769 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
770 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
771 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
775 tv_freq( bktr_ptr_t bktr, int frequency, int type )
777 const struct TUNER* tuner;
783 #if defined( TEST_TUNER_AFC )
784 int oldFrequency, afcDelta;
787 tuner = bktr->card.tuner;
791 if (type == TV_FREQUENCY) {
793 * select the band based on frequency
794 * XXX FIXME: get the cross-over points from the tuner struct
796 if ( frequency < (160 * FREQFACTOR ) )
797 band_select = LOW_BAND;
798 else if ( frequency < (454 * FREQFACTOR ) )
799 band_select = MID_BAND;
801 band_select = HIGH_BAND;
803 #if defined( TEST_TUNER_AFC )
804 if ( bktr->tuner.afc )
808 * N = 16 * { fRF(pc) + fIF(pc) }
809 * or N = 16* fRF(pc) + 16*fIF(pc) }
811 * pc is picture carrier, fRF & fIF are in MHz
813 * fortunatly, frequency is passed in as MHz * 16
814 * and the TBL_IF frequency is also stored in MHz * 16
816 N = frequency + TBL_IF;
818 /* set the address of the PLL */
819 addr = bktr->card.tuner_pllAddr;
820 control = tuner->pllControl[ band_select ];
821 band = tuner->bandAddrs[ band_select ];
823 if(!(band && control)) /* Don't try to set un- */
824 return(-1); /* supported modes. */
826 if ( frequency > bktr->tuner.frequency ) {
827 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
828 i2cWrite( bktr, addr, control, band );
831 i2cWrite( bktr, addr, control, band );
832 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
835 #if defined( TUNER_AFC )
836 if ( bktr->tuner.afc == TRUE ) {
837 #if defined( TEST_TUNER_AFC )
838 oldFrequency = frequency;
840 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
841 /* AFC failed, restore requested frequency */
842 N = frequency + TBL_IF;
843 #if defined( TEST_TUNER_AFC )
844 printf("%s: do_afc: failed to lock\n",
847 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
850 frequency = N - TBL_IF;
851 #if defined( TEST_TUNER_AFC )
852 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
853 afcDelta = frequency - oldFrequency;
854 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
857 #endif /* TUNER_AFC */
859 bktr->tuner.frequency = frequency;
862 if ( type == FM_RADIO_FREQUENCY ) {
863 band_select = FM_RADIO_BAND;
866 * N = { fRF(pc) + fIF(pc) }/step_size
867 * The step size is 50kHz for FM radio.
868 * (eg after 102.35MHz comes 102.40 MHz)
869 * fIF is 10.7 MHz (as detailed in the specs)
871 * frequency is passed in as MHz * 100
873 * So, we have N = (frequency/100 + 10.70) /(50/1000)
875 N = (frequency + 1070)/5;
877 /* set the address of the PLL */
878 addr = bktr->card.tuner_pllAddr;
879 control = tuner->pllControl[ band_select ];
880 band = tuner->bandAddrs[ band_select ];
882 if(!(band && control)) /* Don't try to set un- */
883 return(-1); /* supported modes. */
885 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
886 * the ioctls RADIO_SETMODE
887 * and RADIO_GETMODE */
889 i2cWrite( bktr, addr, control, band );
890 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
892 bktr->tuner.frequency = (N * 5) - 1070;
903 #if defined( TUNER_AFC )
908 do_afc( bktr_ptr_t bktr, int addr, int frequency )
914 origFrequency = frequency;
916 /* wait for first setting to take effect */
917 tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
919 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
922 #if defined( TEST_TUNER_AFC )
923 printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
925 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
926 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
928 if ( !(status & 0x40) ) {
929 #if defined( TEST_TUNER_AFC )
930 printf( "%s: no lock!\n", bktr_name(bktr) );
935 switch( status & AFC_BITS ) {
936 case AFC_FREQ_CENTERED:
937 #if defined( TEST_TUNER_AFC )
938 printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
942 case AFC_FREQ_MINUS_125:
943 case AFC_FREQ_MINUS_62:
944 #if defined( TEST_TUNER_AFC )
945 printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
950 case AFC_FREQ_PLUS_62:
951 case AFC_FREQ_PLUS_125:
952 #if defined( TEST_TUNER_AFC )
953 printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
959 i2cWrite( bktr, addr,
960 (frequency>>8) & 0x7f, frequency & 0xff );
965 i2cWrite( bktr, addr,
966 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
970 #endif /* TUNER_AFC */
975 * Get the Tuner status and signal strength
977 int get_tuner_status( bktr_ptr_t bktr ) {
978 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
982 * set the channel of the tuner
985 tv_channel( bktr_ptr_t bktr, int channel )
989 /* calculate the frequency according to tuner type */
990 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
993 /* set the new frequency */
994 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
997 /* OK to update records */
998 return( (bktr->tuner.channel = channel) );
1002 * get channelset name
1005 tuner_getchnlset(struct bktr_chnlset *chnlset)
1007 if (( chnlset->index < CHNLSET_MIN ) ||
1008 ( chnlset->index > CHNLSET_MAX ))
1011 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1012 BT848_MAX_CHNLSET_NAME_LEN);
1014 chnlset->max_channel=freqTable[chnlset->index].ptr[0];