1 /* $Sourceforge: bktr_tuner.c,v 1.5 2003/03/13 22:21:04 emsi Exp $ */
3 /* $NetBSD: bktr_tuner.c,v 1.11 2005/12/28 20:41:56 wiz Exp $ */
4 /* $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.9 2000/10/19 07:33:28 roger Exp$ */
7 * This is part of the Driver for Video Capture Cards (Frame grabbers)
8 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
10 * Copyright Roger Hardiman and Amancio Hasty.
12 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
17 * 1. Redistributions of source code must retain the
18 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * 3. All advertising materials mentioning features or use of this software
30 * must display the following acknowledgement:
31 * This product includes software developed by Amancio Hasty and
33 * 4. The name of the author may not be used to endorse or promote products
34 * derived from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
37 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
40 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
45 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: bktr_tuner.c,v 1.11 2005/12/28 20:41:56 wiz Exp $");
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
61 #include <pci/pcivar.h>
63 #if (__FreeBSD_version < 500000)
64 #include <machine/clock.h> /* for DELAY */
67 #if (__FreeBSD_version >=300000)
68 #include <machine/bus_memio.h> /* for bus space */
75 #include <dev/ic/bt8xx.h> /* NetBSD .h file location */
76 #include <dev/pci/bktr/bktr_reg.h>
77 #include <dev/pci/bktr/bktr_tuner.h>
78 #include <dev/pci/bktr/bktr_card.h>
79 #include <dev/pci/bktr/bktr_core.h>
81 #include <machine/ioctl_meteor.h> /* Traditional .h file location */
82 #include <machine/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
83 #include <dev/bktr/bktr_reg.h>
84 #include <dev/bktr/bktr_tuner.h>
85 #include <dev/bktr/bktr_card.h>
86 #include <dev/bktr/bktr_core.h>
91 #if defined(TUNER_AFC)
92 #define AFC_DELAY 10000 /* 10 millisend delay */
94 #define AFC_FREQ_MINUS_125 0x00
95 #define AFC_FREQ_MINUS_62 0x01
96 #define AFC_FREQ_CENTERED 0x02
97 #define AFC_FREQ_PLUS_62 0x03
98 #define AFC_FREQ_PLUS_125 0x04
99 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
100 #endif /* TUNER_AFC */
105 #define TTYPE_NTSC_J 2
107 #define TTYPE_PAL_M 4
108 #define TTYPE_PAL_N 5
109 #define TTYPE_SECAM 6
111 #define TSA552x_CB_MSB (0x80)
112 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
113 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
114 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
115 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
116 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 kHz, 1 for 62.5 kHz */
117 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
118 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
120 #define TSA552x_RADIO (TSA552x_CB_MSB | \
123 /* raise the charge pump voltage for fast tuning */
124 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
130 /* lower the charge pump voltage for better residual oscillator FM */
131 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
136 /* The control value for the ALPS TSCH5 Tuner */
137 #define TSCH5_FCONTROL 0x82
138 #define TSCH5_RADIO 0x86
140 /* The control value for the ALPS TSBH1 Tuner */
141 #define TSBH1_FCONTROL 0xce
144 static const struct TUNER tuners
[] = {
145 /* XXX FIXME: fill in the band-switch crosspoints */
147 { "<no>", /* the 'name' */
148 TTYPE_XXX
, /* input type */
149 { 0x00, /* control byte for Tuner PLL */
153 { 160, 454 }, /* band-switch crosspoints */
154 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
157 { "Temic NTSC", /* the 'name' */
158 TTYPE_NTSC
, /* input type */
159 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
163 { 160, 454 }, /* band-switch crosspoints */
164 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
167 { "Temic PAL", /* the 'name' */
168 TTYPE_PAL
, /* input type */
169 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
173 { 160, 454 }, /* band-switch crosspoints */
174 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
177 { "Temic SECAM", /* the 'name' */
178 TTYPE_SECAM
, /* input type */
179 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
183 { 160, 454 }, /* band-switch crosspoints */
184 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
187 { "Philips NTSC", /* the 'name' */
188 TTYPE_NTSC
, /* input type */
189 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
193 { 160, 454 }, /* band-switch crosspoints */
194 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
197 { "Philips PAL", /* the 'name' */
198 TTYPE_PAL
, /* input type */
199 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
203 { 160, 454 }, /* band-switch crosspoints */
204 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
207 { "Philips SECAM", /* the 'name' */
208 TTYPE_SECAM
, /* input type */
209 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
213 { 160, 454 }, /* band-switch crosspoints */
214 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
217 { "Temic PAL I", /* the 'name' */
218 TTYPE_PAL
, /* input type */
219 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
223 { 160, 454 }, /* band-switch crosspoints */
224 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
227 { "Philips PAL I", /* the 'name' */
228 TTYPE_PAL
, /* input type */
229 { TSA552x_SCONTROL
, /* control byte for Tuner PLL */
233 { 160, 454 }, /* band-switch crosspoints */
234 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
236 /* PHILIPS_FR1236_NTSC */
237 { "Philips FR1236 NTSC FM", /* the 'name' */
238 TTYPE_NTSC
, /* input type */
239 { TSA552x_FCONTROL
, /* control byte for Tuner PLL */
243 { 160, 454 }, /* band-switch crosspoints */
244 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
246 /* PHILIPS_FR1216_PAL */
247 { "Philips FR1216 PAL FM" , /* the 'name' */
248 TTYPE_PAL
, /* input type */
249 { TSA552x_FCONTROL
, /* control byte for Tuner PLL */
253 { 160, 454 }, /* band-switch crosspoints */
254 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
256 /* PHILIPS_FR1236_SECAM */
257 { "Philips FR1236 SECAM FM", /* the 'name' */
258 TTYPE_SECAM
, /* input type */
259 { TSA552x_FCONTROL
, /* control byte for Tuner PLL */
263 { 160, 454 }, /* band-switch crosspoints */
264 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
266 /* ALPS TSCH5 NTSC */
267 { "ALPS TSCH5 NTSC FM", /* the 'name' */
268 TTYPE_NTSC
, /* input type */
269 { TSCH5_FCONTROL
, /* control byte for Tuner PLL */
273 { 160, 454 }, /* band-switch crosspoints */
274 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
276 /* ALPS TSBH1 NTSC */
277 { "ALPS TSBH1 NTSC", /* the 'name' */
278 TTYPE_NTSC
, /* input type */
279 { TSBH1_FCONTROL
, /* control byte for Tuner PLL */
283 { 160, 454 }, /* band-switch crosspoints */
284 { 0x01, 0x02, 0x08, 0x00 } } /* the band-switch values */
288 /* scaling factor for frequencies expressed as ints */
289 #define FREQFACTOR 16
293 * entry 0: MAX legal channel
294 * entry 1: IF frequency
295 * expressed as fi{MHz} * 16,
296 * eg 45.75MHz == 45.75 * 16 = 732
297 * entry 2: [place holder/future]
298 * entry 3: base of channel record 0
299 * entry 3 + (x*3): base of channel record 'x'
300 * entry LAST: NULL channel entry marking end of records
303 * int 0: base channel
304 * int 1: frequency of base channel,
305 * expressed as fb{MHz} * 16,
306 * int 2: offset frequency between channels,
307 * expressed as fo{MHz} * 16,
311 * North American Broadcast Channels:
313 * 2: 55.25 MHz - 4: 67.25 MHz
314 * 5: 77.25 MHz - 6: 83.25 MHz
315 * 7: 175.25 MHz - 13: 211.25 MHz
316 * 14: 471.25 MHz - 83: 885.25 MHz
321 static const int nabcst
[] = {
322 83, (int)( 45.75 * FREQFACTOR
), 0,
323 14, (int)(471.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
324 7, (int)(175.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
325 5, (int)( 77.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
326 2, (int)( 55.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
332 * North American Cable Channels, IRC:
334 * 2: 55.25 MHz - 4: 67.25 MHz
335 * 5: 77.25 MHz - 6: 83.25 MHz
336 * 7: 175.25 MHz - 13: 211.25 MHz
337 * 14: 121.25 MHz - 22: 169.25 MHz
338 * 23: 217.25 MHz - 94: 643.25 MHz
339 * 95: 91.25 MHz - 99: 115.25 MHz
344 static const int irccable
[] = {
345 116, (int)( 45.75 * FREQFACTOR
), 0,
346 100, (int)(649.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
347 95, (int)( 91.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
348 23, (int)(217.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
349 14, (int)(121.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
350 7, (int)(175.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
351 5, (int)( 77.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
352 2, (int)( 55.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
358 * North American Cable Channels, HRC:
360 * 2: 54 MHz - 4: 66 MMz
361 * 5: 78 MHz - 6: 84 MHz
362 * 7: 174 MHz - 13: 210 MHz
363 * 14: 120 MHz - 22: 168 MHz
364 * 23: 216 MHz - 94: 642 MHz
365 * 95: 90 MHz - 99: 114 MHz
370 static const int hrccable
[] = {
371 116, (int)( 45.75 * FREQFACTOR
), 0,
372 100, (int)(648.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
373 95, (int)( 90.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
374 23, (int)(216.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
375 14, (int)(120.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
376 7, (int)(174.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
377 5, (int)( 78.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
378 2, (int)( 54.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
384 * Western European broadcast channels:
386 * (there are others that appear to vary between countries - rmt)
388 * here's the table Philips provides:
389 * caution, some of the offsets don't compute...
498 * Channels S21 - S41 are taken from
499 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
523 * 121 3890 000 IFFREQ
526 static const int weurope
[] = {
527 121, (int)( 38.90 * FREQFACTOR
), 0,
528 100, (int)(303.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
529 90, (int)(231.25 * FREQFACTOR
), (int)(7.00 * FREQFACTOR
),
530 80, (int)(105.25 * FREQFACTOR
), (int)(7.00 * FREQFACTOR
),
531 74, (int)( 69.25 * FREQFACTOR
), (int)(7.00 * FREQFACTOR
),
532 21, (int)(471.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
533 17, (int)(183.25 * FREQFACTOR
), (int)(9.00 * FREQFACTOR
),
534 16, (int)(175.25 * FREQFACTOR
), (int)(9.00 * FREQFACTOR
),
535 15, (int)( 82.25 * FREQFACTOR
), (int)(8.50 * FREQFACTOR
),
536 13, (int)( 53.75 * FREQFACTOR
), (int)(8.50 * FREQFACTOR
),
537 5, (int)(175.25 * FREQFACTOR
), (int)(7.00 * FREQFACTOR
),
538 2, (int)( 48.25 * FREQFACTOR
), (int)(7.00 * FREQFACTOR
),
543 * Japanese Broadcast Channels:
545 * 1: 91.25MHz - 3: 103.25MHz
546 * 4: 171.25MHz - 7: 189.25MHz
547 * 8: 193.25MHz - 12: 217.25MHz (VHF)
548 * 13: 471.25MHz - 62: 765.25MHz (UHF)
555 #define IF_FREQ 45.75
556 static const int jpnbcst
[] = {
557 62, (int)(IF_FREQ
* FREQFACTOR
), 0,
558 13, (int)(471.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
559 8, (int)(193.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
560 4, (int)(171.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
561 1, (int)( 91.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
568 * Japanese Cable Channels:
570 * 1: 91.25MHz - 3: 103.25MHz
571 * 4: 171.25MHz - 7: 189.25MHz
572 * 8: 193.25MHz - 12: 217.25MHz
573 * 13: 109.25MHz - 21: 157.25MHz
575 * 23: 223.25MHz - 63: 463.25MHz
580 #define IF_FREQ 45.75
581 static const int jpncable
[] = {
582 63, (int)(IF_FREQ
* FREQFACTOR
), 0,
583 23, (int)(223.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
584 22, (int)(165.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
585 13, (int)(109.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
586 8, (int)(193.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
587 4, (int)(171.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
588 1, (int)( 91.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
595 * xUSSR Broadcast Channels:
597 * 1: 49.75MHz - 2: 59.25MHz
598 * 3: 77.25MHz - 5: 93.25MHz
599 * 6: 175.25MHz - 12: 223.25MHz
601 * 21: 471.25MHz - 34: 575.25MHz
602 * 35: 583.25MHz - 69: 855.25MHz
606 * 70: 111.25MHz - 77: 167.25MHz
607 * 78: 231.25MHz -107: 463.25MHz
611 #define IF_FREQ 38.90
612 static const int xussr
[] = {
613 107, (int)(IF_FREQ
* FREQFACTOR
), 0,
614 78, (int)(231.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
615 70, (int)(111.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
616 35, (int)(583.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
617 21, (int)(471.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
618 6, (int)(175.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
619 3, (int)( 77.25 * FREQFACTOR
), (int)(8.00 * FREQFACTOR
),
620 1, (int)( 49.75 * FREQFACTOR
), (int)(9.50 * FREQFACTOR
),
626 * Australian broadcast channels
629 #define IF_FREQ 38.90
630 static const int australia
[] = {
631 83, (int)(IF_FREQ
* FREQFACTOR
), 0,
632 28, (int)(527.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
633 10, (int)(209.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
634 6, (int)(175.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
635 4, (int)( 95.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
636 3, (int)( 86.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
637 1, (int)( 57.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
),
644 * France broadcast channels
647 #define IF_FREQ 38.90
648 static const int france
[] = {
649 69, (int)(IF_FREQ
* FREQFACTOR
), 0,
650 21, (int)(471.25 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
), /* 21 -> 69 */
651 5, (int)(176.00 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
), /* 5 -> 10 */
652 4, (int)( 63.75 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
), /* 4 */
653 3, (int)( 60.50 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
), /* 3 */
654 1, (int)( 47.75 * FREQFACTOR
), (int)(OFFSET
* FREQFACTOR
), /* 1 2 */
660 static const struct {
662 const char name
[BT848_MAX_CHNLSET_NAME_LEN
];
666 {irccable
, "cableirc"},
667 {hrccable
, "cablehrc"},
668 {weurope
, "weurope"},
669 {jpnbcst
, "jpnbcst"},
670 {jpncable
, "jpncable"},
672 {australia
, "australia"},
677 #define TBL_CHNL freqTable[bktr->tuner.chnlset].ptr[x]
678 #define TBL_BASE_FREQ freqTable[bktr->tuner.chnlset].ptr[x + 1]
679 #define TBL_OFFSET freqTable[bktr->tuner.chnlset].ptr[x + 2]
681 frequency_lookup(bktr_ptr_t bktr
, int channel
)
685 /* check for "> MAX channel" */
687 if (channel
> TBL_CHNL
)
690 /* search the table for data */
691 for (x
= 3; TBL_CHNL
; x
+= 3) {
692 if (channel
>= TBL_CHNL
) {
693 return(TBL_BASE_FREQ
+
694 ((channel
- TBL_CHNL
) * TBL_OFFSET
));
698 /* not found, must be below the MIN channel */
706 #define TBL_IF freqTable[bktr->tuner.chnlset].ptr[1]
709 /* Initialise the tuner structures in the bktr_softc */
710 /* This is needed as the tuner details are no longer globally declared */
712 void select_tuner(bktr_ptr_t bktr
, int tuner_type
) {
713 if (tuner_type
< Bt848_MAX_TUNER
) {
714 bktr
->card
.tuner
= &tuners
[tuner_type
];
716 bktr
->card
.tuner
= NULL
;
722 * Programming the tuner properly is quite complicated.
723 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
724 * The tuner (front end) covers 45.75 MHz - 855.25 MHz and an FM band of
725 * 87.5 MHz to 108.0 MHz.
727 * RF and IF. RF = radio frequencies, it is the transmitted signal.
728 * IF is the Intermediate Frequency (the offset from the base
729 * signal where the video, color, audio and NICAM signals are.
731 * Eg, Picture at 38.9 MHz, Colour at 34.47 MHz, sound at 32.9 MHz
732 * NICAM at 32.348 MHz.
733 * Strangely enough, there is an IF (intermediate frequency) for
734 * FM Radio which is 10.7 MHz.
736 * The tuner also works in Bands. Philips bands are
737 * FM radio band 87.50 to 108.00 MHz
738 * Low band 45.75 to 170.00 MHz
739 * Mid band 170.00 to 450.00 MHz
740 * High band 450.00 to 855.25 MHz
743 * Now we need to set the PLL on the tuner to the required freuqncy.
744 * It has a programmable divisor.
746 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
749 * For RADIO we want a different equation.
750 * freq IF is 10.70 MHz (so the data sheet tells me)
751 * N = (freq RF + freq IF) / step size
752 * The step size must be set to 50 kHz (so the data sheet tells me)
753 * (note this is 50 kHz, the other things are in MHz)
754 * so we end up with N = 20x(freq RF + 10.7)
761 #define FM_RADIO_BAND 3
764 /* Check if these are correct for other than Philips PAL */
765 #define STATUSBIT_COLD 0x80
766 #define STATUSBIT_LOCK 0x40
767 #define STATUSBIT_TV 0x20
768 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
769 #define STATUSBIT_ADC 0x07
772 * set the frequency of the tuner
773 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
774 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
775 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
779 tv_freq(bktr_ptr_t bktr
, int frequency
, int type
)
781 const struct TUNER
* tuner
;
787 #if defined(TEST_TUNER_AFC)
788 int oldFrequency
, afcDelta
;
791 tuner
= bktr
->card
.tuner
;
795 if (type
== TV_FREQUENCY
) {
797 * select the band based on frequency
799 if (frequency
< (tuner
->bandLimits
[0] * FREQFACTOR
))
800 band_select
= LOW_BAND
;
801 else if (frequency
< (tuner
->bandLimits
[1] * FREQFACTOR
))
802 band_select
= MID_BAND
;
804 band_select
= HIGH_BAND
;
806 #if defined(TEST_TUNER_AFC)
811 * N = 16 * { fRF(pc) + fIF(pc) }
812 * or N = 16* fRF(pc) + 16*fIF(pc) }
814 * pc is picture carrier, fRF & fIF are in MHz
816 * fortunatly, frequency is passed in as MHz * 16
817 * and the TBL_IF frequency is also stored in MHz * 16
819 N
= frequency
+ TBL_IF
;
821 /* set the address of the PLL */
822 addr
= bktr
->card
.tuner_pllAddr
;
823 control
= tuner
->pllControl
[band_select
];
824 band
= tuner
->bandAddrs
[band_select
];
826 if(!(band
&& control
)) /* Don't try to set un- */
827 return(-1); /* supported modes. */
829 if (frequency
> bktr
->tuner
.frequency
) {
830 i2cWrite(bktr
, addr
, (N
>>8) & 0x7f, N
& 0xff);
831 i2cWrite(bktr
, addr
, control
, band
);
834 i2cWrite(bktr
, addr
, control
, band
);
835 i2cWrite(bktr
, addr
, (N
>>8) & 0x7f, N
& 0xff);
838 #if defined(TUNER_AFC)
839 if (bktr
->tuner
.afc
== TRUE
) {
840 #if defined(TEST_TUNER_AFC)
841 oldFrequency
= frequency
;
843 if ((N
= do_afc(bktr
, addr
, N
)) < 0) {
844 /* AFC failed, restore requested frequency */
845 N
= frequency
+ TBL_IF
;
846 #if defined(TEST_TUNER_AFC)
847 printf("%s: do_afc: failed to lock\n",
850 i2cWrite(bktr
, addr
, (N
>>8) & 0x7f, N
& 0xff);
853 frequency
= N
- TBL_IF
;
854 #if defined(TEST_TUNER_AFC)
855 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr
), frequency
, frequency
/ 16, frequency
% 16);
856 afcDelta
= frequency
- oldFrequency
;
857 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr
), afcDelta
, afcDelta
/ 16, afcDelta
% 16);
860 #endif /* TUNER_AFC */
862 bktr
->tuner
.frequency
= frequency
;
865 if (type
== FM_RADIO_FREQUENCY
) {
866 band_select
= FM_RADIO_BAND
;
869 * N = { fRF(pc) + fIF(pc) }/step_size
870 * The step size is 50kHz for FM radio.
871 * (eg after 102.35MHz comes 102.40 MHz)
872 * fIF is 10.7 MHz (as detailed in the specs)
874 * frequency is passed in as MHz * 100
876 * So, we have N = (frequency/100 + 10.70) /(50/1000)
878 N
= (frequency
+ 1070)/5;
880 /* set the address of the PLL */
881 addr
= bktr
->card
.tuner_pllAddr
;
882 control
= tuner
->pllControl
[band_select
];
883 band
= tuner
->bandAddrs
[band_select
];
885 if(!(band
&& control
)) /* Don't try to set un- */
886 return(-1); /* supported modes. */
888 band
|= bktr
->tuner
.radio_mode
; /* tuner.radio_mode is set in
889 * the ioctls RADIO_SETMODE
890 * and RADIO_GETMODE */
892 i2cWrite(bktr
, addr
, control
, band
);
893 i2cWrite(bktr
, addr
, (N
>>8) & 0x7f, N
& 0xff);
895 bktr
->tuner
.frequency
= (N
* 5) - 1070;
906 #if defined(TUNER_AFC)
911 do_afc(bktr_ptr_t bktr
, int addr
, int frequency
)
917 origFrequency
= frequency
;
919 /* wait for first setting to take effect */
920 tsleep(BKTR_SLEEP
, PZERO
, "tuning", hz
/8);
922 if ((status
= i2cRead(bktr
, addr
+ 1)) < 0)
925 #if defined(TEST_TUNER_AFC)
926 printf("%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr
), frequency
, status
);
928 for (step
= 0; step
< AFC_MAX_STEP
; ++step
) {
929 if ((status
= i2cRead(bktr
, addr
+ 1)) < 0)
931 if (!(status
& 0x40)) {
932 #if defined(TEST_TUNER_AFC)
933 printf("%s: no lock!\n", bktr_name(bktr
));
938 switch(status
& AFC_BITS
) {
939 case AFC_FREQ_CENTERED
:
940 #if defined(TEST_TUNER_AFC)
941 printf("%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr
), frequency
, status
);
945 case AFC_FREQ_MINUS_125
:
946 case AFC_FREQ_MINUS_62
:
947 #if defined(TEST_TUNER_AFC)
948 printf("%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr
), frequency
, status
);
953 case AFC_FREQ_PLUS_62
:
954 case AFC_FREQ_PLUS_125
:
955 #if defined(TEST_TUNER_AFC)
956 printf("%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr
), frequency
, status
);
963 (frequency
>>8) & 0x7f, frequency
& 0xff);
969 (origFrequency
>>8) & 0x7f, origFrequency
& 0xff);
973 #endif /* TUNER_AFC */
978 * Get the Tuner status and signal strength
980 int get_tuner_status(bktr_ptr_t bktr
) {
981 return i2cRead(bktr
, bktr
->card
.tuner_pllAddr
+ 1);
985 * set the channel of the tuner
988 tv_channel(bktr_ptr_t bktr
, int channel
)
992 /* calculate the frequency according to tuner type */
993 if ((frequency
= frequency_lookup(bktr
, channel
)) < 0)
996 /* set the new frequency */
997 if (tv_freq(bktr
, frequency
, TV_FREQUENCY
) < 0)
1000 /* OK to update records */
1001 return((bktr
->tuner
.channel
= channel
));
1005 * get channelset name
1008 tuner_getchnlset(struct bktr_chnlset
*chnlset
)
1010 if ((chnlset
->index
< CHNLSET_MIN
) ||
1011 (chnlset
->index
> CHNLSET_MAX
))
1014 memcpy(&chnlset
->name
, &freqTable
[chnlset
->index
].name
,
1015 BT848_MAX_CHNLSET_NAME_LEN
);
1017 chnlset
->max_channel
=freqTable
[chnlset
->index
].ptr
[0];