1 /* $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.5.2.3 2000/10/26 16:38:46 roger Exp $ */
2 /* $DragonFly: src/sys/dev/video/bktr/bktr_tuner.c,v 1.2 2003/06/17 04:28:23 dillon Exp $ */
5 * This is part of the Driver for Video Capture Cards (Frame grabbers)
6 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
8 * Copyright Roger Hardiman and Amancio Hasty.
10 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
15 * 1. Redistributions of source code must retain the
16 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
17 * All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by Amancio Hasty and
31 * 4. The name of the author may not be used to endorse or promote products
32 * derived from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
35 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
38 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
43 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 * POSSIBILITY OF SUCH DAMAGE.
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
58 #include <pci/pcivar.h>
60 #if (__FreeBSD_version < 500000)
61 #include <machine/clock.h> /* for DELAY */
64 #if (__FreeBSD_version >=300000)
65 #include <machine/bus_memio.h> /* for bus space */
66 #include <machine/bus.h>
72 #include <dev/ic/bt8xx.h> /* NetBSD .h file location */
73 #include <dev/pci/bktr/bktr_reg.h>
74 #include <dev/pci/bktr/bktr_tuner.h>
75 #include <dev/pci/bktr/bktr_card.h>
76 #include <dev/pci/bktr/bktr_core.h>
78 #include <machine/ioctl_meteor.h> /* Traditional .h file location */
79 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
80 #include <dev/bktr/bktr_reg.h>
81 #include <dev/bktr/bktr_tuner.h>
82 #include <dev/bktr/bktr_card.h>
83 #include <dev/bktr/bktr_core.h>
88 #if defined( TUNER_AFC )
89 #define AFC_DELAY 10000 /* 10 millisend delay */
91 #define AFC_FREQ_MINUS_125 0x00
92 #define AFC_FREQ_MINUS_62 0x01
93 #define AFC_FREQ_CENTERED 0x02
94 #define AFC_FREQ_PLUS_62 0x03
95 #define AFC_FREQ_PLUS_125 0x04
96 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
97 #endif /* TUNER_AFC */
102 #define TTYPE_NTSC_J 2
104 #define TTYPE_PAL_M 4
105 #define TTYPE_PAL_N 5
106 #define TTYPE_SECAM 6
108 #define TSA552x_CB_MSB (0x80)
109 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
110 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
111 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
112 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
113 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
114 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
115 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
117 #define TSA552x_RADIO (TSA552x_CB_MSB | \
120 /* raise the charge pump voltage for fast tuning */
121 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
127 /* lower the charge pump voltage for better residual oscillator FM */
128 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
133 /* The control value for the ALPS TSCH5 Tuner */
134 #define TSCH5_FCONTROL 0x82
135 #define TSCH5_RADIO 0x86
137 /* The control value for the ALPS TSBH1 Tuner */
138 #define TSBH1_FCONTROL 0xce
141 static const struct TUNER tuners[] = {
142 /* XXX FIXME: fill in the band-switch crosspoints */
144 { "<no>", /* the 'name' */
145 TTYPE_XXX, /* input type */
146 { 0x00, /* control byte for Tuner PLL */
150 { 0x00, 0x00 }, /* band-switch crosspoints */
151 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
154 { "Temic NTSC", /* the 'name' */
155 TTYPE_NTSC, /* input type */
156 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
160 { 0x00, 0x00}, /* band-switch crosspoints */
161 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
164 { "Temic PAL", /* the 'name' */
165 TTYPE_PAL, /* input type */
166 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
170 { 0x00, 0x00 }, /* band-switch crosspoints */
171 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
174 { "Temic SECAM", /* the 'name' */
175 TTYPE_SECAM, /* input type */
176 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
180 { 0x00, 0x00 }, /* band-switch crosspoints */
181 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
184 { "Philips NTSC", /* the 'name' */
185 TTYPE_NTSC, /* input type */
186 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
190 { 0x00, 0x00 }, /* band-switch crosspoints */
191 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
194 { "Philips PAL", /* the 'name' */
195 TTYPE_PAL, /* input type */
196 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
200 { 0x00, 0x00 }, /* band-switch crosspoints */
201 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
204 { "Philips SECAM", /* the 'name' */
205 TTYPE_SECAM, /* input type */
206 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
210 { 0x00, 0x00 }, /* band-switch crosspoints */
211 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
214 { "Temic PAL I", /* the 'name' */
215 TTYPE_PAL, /* input type */
216 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
220 { 0x00, 0x00 }, /* band-switch crosspoints */
221 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
224 { "Philips PAL I", /* the 'name' */
225 TTYPE_PAL, /* input type */
226 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
230 { 0x00, 0x00 }, /* band-switch crosspoints */
231 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
233 /* PHILIPS_FR1236_NTSC */
234 { "Philips FR1236 NTSC FM", /* the 'name' */
235 TTYPE_NTSC, /* input type */
236 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
240 { 0x00, 0x00 }, /* band-switch crosspoints */
241 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
243 /* PHILIPS_FR1216_PAL */
244 { "Philips FR1216 PAL FM" , /* the 'name' */
245 TTYPE_PAL, /* input type */
246 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
250 { 0x00, 0x00 }, /* band-switch crosspoints */
251 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
253 /* PHILIPS_FR1236_SECAM */
254 { "Philips FR1236 SECAM FM", /* the 'name' */
255 TTYPE_SECAM, /* input type */
256 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
260 { 0x00, 0x00 }, /* band-switch crosspoints */
261 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
263 /* ALPS TSCH5 NTSC */
264 { "ALPS TSCH5 NTSC FM", /* the 'name' */
265 TTYPE_NTSC, /* input type */
266 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
270 { 0x00, 0x00 }, /* band-switch crosspoints */
271 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
273 /* ALPS TSBH1 NTSC */
274 { "ALPS TSBH1 NTSC", /* the 'name' */
275 TTYPE_NTSC, /* input type */
276 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
280 { 0x00, 0x00 }, /* band-switch crosspoints */
281 { 0x01, 0x02, 0x08, 0x00 } } /* the band-switch values */
285 /* scaling factor for frequencies expressed as ints */
286 #define FREQFACTOR 16
290 * entry 0: MAX legal channel
291 * entry 1: IF frequency
292 * expressed as fi{mHz} * 16,
293 * eg 45.75mHz == 45.75 * 16 = 732
294 * entry 2: [place holder/future]
295 * entry 3: base of channel record 0
296 * entry 3 + (x*3): base of channel record 'x'
297 * entry LAST: NULL channel entry marking end of records
300 * int 0: base channel
301 * int 1: frequency of base channel,
302 * expressed as fb{mHz} * 16,
303 * int 2: offset frequency between channels,
304 * expressed as fo{mHz} * 16,
308 * North American Broadcast Channels:
310 * 2: 55.25 mHz - 4: 67.25 mHz
311 * 5: 77.25 mHz - 6: 83.25 mHz
312 * 7: 175.25 mHz - 13: 211.25 mHz
313 * 14: 471.25 mHz - 83: 885.25 mHz
318 static int nabcst[] = {
319 83, (int)( 45.75 * FREQFACTOR), 0,
320 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
321 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
322 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
323 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
329 * North American Cable Channels, IRC:
331 * 2: 55.25 mHz - 4: 67.25 mHz
332 * 5: 77.25 mHz - 6: 83.25 mHz
333 * 7: 175.25 mHz - 13: 211.25 mHz
334 * 14: 121.25 mHz - 22: 169.25 mHz
335 * 23: 217.25 mHz - 94: 643.25 mHz
336 * 95: 91.25 mHz - 99: 115.25 mHz
341 static int irccable[] = {
342 116, (int)( 45.75 * FREQFACTOR), 0,
343 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
344 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
345 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
346 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
347 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
348 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
349 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
355 * North American Cable Channels, HRC:
357 * 2: 54 mHz - 4: 66 mHz
358 * 5: 78 mHz - 6: 84 mHz
359 * 7: 174 mHz - 13: 210 mHz
360 * 14: 120 mHz - 22: 168 mHz
361 * 23: 216 mHz - 94: 642 mHz
362 * 95: 90 mHz - 99: 114 mHz
367 static int hrccable[] = {
368 116, (int)( 45.75 * FREQFACTOR), 0,
369 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
370 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
371 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
372 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
373 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
374 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
375 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
381 * Western European broadcast channels:
383 * (there are others that appear to vary between countries - rmt)
385 * here's the table Philips provides:
386 * caution, some of the offsets don't compute...
495 * Channels S21 - S41 are taken from
496 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
520 * 121 3890 000 IFFREQ
523 static int weurope[] = {
524 121, (int)( 38.90 * FREQFACTOR), 0,
525 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
526 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
527 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
528 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
529 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
530 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
531 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
532 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
533 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
534 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
535 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
540 * Japanese Broadcast Channels:
542 * 1: 91.25MHz - 3: 103.25MHz
543 * 4: 171.25MHz - 7: 189.25MHz
544 * 8: 193.25MHz - 12: 217.25MHz (VHF)
545 * 13: 471.25MHz - 62: 765.25MHz (UHF)
552 #define IF_FREQ 45.75
553 static int jpnbcst[] = {
554 62, (int)(IF_FREQ * FREQFACTOR), 0,
555 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
556 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
557 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
558 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
565 * Japanese Cable Channels:
567 * 1: 91.25MHz - 3: 103.25MHz
568 * 4: 171.25MHz - 7: 189.25MHz
569 * 8: 193.25MHz - 12: 217.25MHz
570 * 13: 109.25MHz - 21: 157.25MHz
572 * 23: 223.25MHz - 63: 463.25MHz
577 #define IF_FREQ 45.75
578 static int jpncable[] = {
579 63, (int)(IF_FREQ * FREQFACTOR), 0,
580 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
581 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
582 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
583 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
584 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
585 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
592 * xUSSR Broadcast Channels:
594 * 1: 49.75MHz - 2: 59.25MHz
595 * 3: 77.25MHz - 5: 93.25MHz
596 * 6: 175.25MHz - 12: 223.25MHz
598 * 21: 471.25MHz - 34: 575.25MHz
599 * 35: 583.25MHz - 69: 855.25MHz
603 * 70: 111.25MHz - 77: 167.25MHz
604 * 78: 231.25MHz -107: 463.25MHz
608 #define IF_FREQ 38.90
609 static int xussr[] = {
610 107, (int)(IF_FREQ * FREQFACTOR), 0,
611 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
612 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
613 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
614 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
615 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
616 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
617 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
623 * Australian broadcast channels
626 #define IF_FREQ 38.90
627 static int australia[] = {
628 83, (int)(IF_FREQ * FREQFACTOR), 0,
629 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
630 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
631 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
632 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
633 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
634 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
641 * France broadcast channels
644 #define IF_FREQ 38.90
645 static int france[] = {
646 69, (int)(IF_FREQ * FREQFACTOR), 0,
647 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
648 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
649 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
650 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
651 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
659 char name[BT848_MAX_CHNLSET_NAME_LEN];
663 {irccable, "cableirc"},
664 {hrccable, "cablehrc"},
665 {weurope, "weurope"},
666 {jpnbcst, "jpnbcst"},
667 {jpncable, "jpncable"},
669 {australia, "australia"},
674 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
675 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
676 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
678 frequency_lookup( bktr_ptr_t bktr, int channel )
682 /* check for "> MAX channel" */
684 if ( channel > TBL_CHNL )
687 /* search the table for data */
688 for ( x = 3; TBL_CHNL; x += 3 ) {
689 if ( channel >= TBL_CHNL ) {
690 return( TBL_BASE_FREQ +
691 ((channel - TBL_CHNL) * TBL_OFFSET) );
695 /* not found, must be below the MIN channel */
703 #define TBL_IF freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
706 /* Initialise the tuner structures in the bktr_softc */
707 /* This is needed as the tuner details are no longer globally declared */
709 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
710 if (tuner_type < Bt848_MAX_TUNER) {
711 bktr->card.tuner = &tuners[ tuner_type ];
713 bktr->card.tuner = NULL;
719 * Programming the tuner properly is quite complicated.
720 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
721 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
722 * 87.5 Mhz to 108.0 Mhz.
724 * RF and IF. RF = radio frequencies, it is the transmitted signal.
725 * IF is the Intermediate Frequency (the offset from the base
726 * signal where the video, color, audio and NICAM signals are.
728 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
729 * NICAM at 32.348 Mhz.
730 * Strangely enough, there is an IF (intermediate frequency) for
731 * FM Radio which is 10.7 Mhz.
733 * The tuner also works in Bands. Philips bands are
734 * FM radio band 87.50 to 108.00 MHz
735 * Low band 45.75 to 170.00 MHz
736 * Mid band 170.00 to 450.00 MHz
737 * High band 450.00 to 855.25 MHz
740 * Now we need to set the PLL on the tuner to the required freuqncy.
741 * It has a programmable divisor.
743 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
746 * For RADIO we want a different equation.
747 * freq IF is 10.70 MHz (so the data sheet tells me)
748 * N = (freq RF + freq IF) / step size
749 * The step size must be set to 50 khz (so the data sheet tells me)
750 * (note this is 50 kHz, the other things are in MHz)
751 * so we end up with N = 20x(freq RF + 10.7)
758 #define FM_RADIO_BAND 3
761 /* Check if these are correct for other than Philips PAL */
762 #define STATUSBIT_COLD 0x80
763 #define STATUSBIT_LOCK 0x40
764 #define STATUSBIT_TV 0x20
765 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
766 #define STATUSBIT_ADC 0x07
769 * set the frequency of the tuner
770 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
771 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
772 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
776 tv_freq( bktr_ptr_t bktr, int frequency, int type )
778 const struct TUNER* tuner;
784 #if defined( TEST_TUNER_AFC )
785 int oldFrequency, afcDelta;
788 tuner = bktr->card.tuner;
792 if (type == TV_FREQUENCY) {
794 * select the band based on frequency
795 * XXX FIXME: get the cross-over points from the tuner struct
797 if ( frequency < (160 * FREQFACTOR ) )
798 band_select = LOW_BAND;
799 else if ( frequency < (454 * FREQFACTOR ) )
800 band_select = MID_BAND;
802 band_select = HIGH_BAND;
804 #if defined( TEST_TUNER_AFC )
805 if ( bktr->tuner.afc )
809 * N = 16 * { fRF(pc) + fIF(pc) }
810 * or N = 16* fRF(pc) + 16*fIF(pc) }
812 * pc is picture carrier, fRF & fIF are in MHz
814 * fortunatly, frequency is passed in as MHz * 16
815 * and the TBL_IF frequency is also stored in MHz * 16
817 N = frequency + TBL_IF;
819 /* set the address of the PLL */
820 addr = bktr->card.tuner_pllAddr;
821 control = tuner->pllControl[ band_select ];
822 band = tuner->bandAddrs[ band_select ];
824 if(!(band && control)) /* Don't try to set un- */
825 return(-1); /* supported modes. */
827 if ( frequency > bktr->tuner.frequency ) {
828 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
829 i2cWrite( bktr, addr, control, band );
832 i2cWrite( bktr, addr, control, band );
833 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
836 #if defined( TUNER_AFC )
837 if ( bktr->tuner.afc == TRUE ) {
838 #if defined( TEST_TUNER_AFC )
839 oldFrequency = frequency;
841 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
842 /* AFC failed, restore requested frequency */
843 N = frequency + TBL_IF;
844 #if defined( TEST_TUNER_AFC )
845 printf("%s: do_afc: failed to lock\n",
848 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
851 frequency = N - TBL_IF;
852 #if defined( TEST_TUNER_AFC )
853 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
854 afcDelta = frequency - oldFrequency;
855 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
858 #endif /* TUNER_AFC */
860 bktr->tuner.frequency = frequency;
863 if ( type == FM_RADIO_FREQUENCY ) {
864 band_select = FM_RADIO_BAND;
867 * N = { fRF(pc) + fIF(pc) }/step_size
868 * The step size is 50kHz for FM radio.
869 * (eg after 102.35MHz comes 102.40 MHz)
870 * fIF is 10.7 MHz (as detailed in the specs)
872 * frequency is passed in as MHz * 100
874 * So, we have N = (frequency/100 + 10.70) /(50/1000)
876 N = (frequency + 1070)/5;
878 /* set the address of the PLL */
879 addr = bktr->card.tuner_pllAddr;
880 control = tuner->pllControl[ band_select ];
881 band = tuner->bandAddrs[ band_select ];
883 if(!(band && control)) /* Don't try to set un- */
884 return(-1); /* supported modes. */
886 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
887 * the ioctls RADIO_SETMODE
888 * and RADIO_GETMODE */
890 i2cWrite( bktr, addr, control, band );
891 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
893 bktr->tuner.frequency = (N * 5) - 1070;
904 #if defined( TUNER_AFC )
909 do_afc( bktr_ptr_t bktr, int addr, int frequency )
915 origFrequency = frequency;
917 /* wait for first setting to take effect */
918 tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
920 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
923 #if defined( TEST_TUNER_AFC )
924 printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
926 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
927 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
929 if ( !(status & 0x40) ) {
930 #if defined( TEST_TUNER_AFC )
931 printf( "%s: no lock!\n", bktr_name(bktr) );
936 switch( status & AFC_BITS ) {
937 case AFC_FREQ_CENTERED:
938 #if defined( TEST_TUNER_AFC )
939 printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
943 case AFC_FREQ_MINUS_125:
944 case AFC_FREQ_MINUS_62:
945 #if defined( TEST_TUNER_AFC )
946 printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
951 case AFC_FREQ_PLUS_62:
952 case AFC_FREQ_PLUS_125:
953 #if defined( TEST_TUNER_AFC )
954 printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
960 i2cWrite( bktr, addr,
961 (frequency>>8) & 0x7f, frequency & 0xff );
966 i2cWrite( bktr, addr,
967 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
971 #endif /* TUNER_AFC */
976 * Get the Tuner status and signal strength
978 int get_tuner_status( bktr_ptr_t bktr ) {
979 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
983 * set the channel of the tuner
986 tv_channel( bktr_ptr_t bktr, int channel )
990 /* calculate the frequency according to tuner type */
991 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
994 /* set the new frequency */
995 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
998 /* OK to update records */
999 return( (bktr->tuner.channel = channel) );
1003 * get channelset name
1006 tuner_getchnlset(struct bktr_chnlset *chnlset)
1008 if (( chnlset->index < CHNLSET_MIN ) ||
1009 ( chnlset->index > CHNLSET_MAX ))
1012 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1013 BT848_MAX_CHNLSET_NAME_LEN);
1015 chnlset->max_channel=freqTable[chnlset->index].ptr[0];