2 * SPDX-License-Identifier: BSD-4-Clause
4 * 1. Redistributions of source code must retain the
5 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Amancio Hasty and
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * This is part of the Driver for Video Capture Cards (Frame grabbers)
41 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
43 * Copyright Roger Hardiman and Amancio Hasty.
45 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
56 #if (__FreeBSD_version < 500000)
57 #include <machine/clock.h> /* for DELAY */
58 #include <pci/pcivar.h>
61 #include <sys/mutex.h>
62 #include <sys/selinfo.h>
63 #include <dev/pci/pcivar.h>
66 #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 <dev/bktr/ioctl_meteor.h>
78 #include <dev/bktr/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 void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
143 static const struct TUNER tuners[] = {
144 /* XXX FIXME: fill in the band-switch crosspoints */
146 { "<no>", /* the 'name' */
147 TTYPE_XXX, /* input type */
148 { 0x00, /* control byte for Tuner PLL */
152 { 0x00, 0x00 }, /* band-switch crosspoints */
153 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
156 { "Temic NTSC", /* the 'name' */
157 TTYPE_NTSC, /* input type */
158 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
162 { 0x00, 0x00}, /* band-switch crosspoints */
163 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
166 { "Temic PAL", /* the 'name' */
167 TTYPE_PAL, /* input type */
168 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
172 { 0x00, 0x00 }, /* band-switch crosspoints */
173 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
176 { "Temic SECAM", /* the 'name' */
177 TTYPE_SECAM, /* input type */
178 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
182 { 0x00, 0x00 }, /* band-switch crosspoints */
183 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
186 { "Philips NTSC", /* the 'name' */
187 TTYPE_NTSC, /* input type */
188 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
192 { 0x00, 0x00 }, /* band-switch crosspoints */
193 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
196 { "Philips PAL", /* the 'name' */
197 TTYPE_PAL, /* input type */
198 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
202 { 0x00, 0x00 }, /* band-switch crosspoints */
203 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
206 { "Philips SECAM", /* the 'name' */
207 TTYPE_SECAM, /* input type */
208 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
212 { 0x00, 0x00 }, /* band-switch crosspoints */
213 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
216 { "Temic PAL I", /* the 'name' */
217 TTYPE_PAL, /* input type */
218 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
222 { 0x00, 0x00 }, /* band-switch crosspoints */
223 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
226 { "Philips PAL I", /* the 'name' */
227 TTYPE_PAL, /* input type */
228 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
232 { 0x00, 0x00 }, /* band-switch crosspoints */
233 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
235 /* PHILIPS_FR1236_NTSC */
236 { "Philips FR1236 NTSC FM", /* the 'name' */
237 TTYPE_NTSC, /* input type */
238 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
242 { 0x00, 0x00 }, /* band-switch crosspoints */
243 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
245 /* PHILIPS_FR1216_PAL */
246 { "Philips FR1216 PAL FM" , /* the 'name' */
247 TTYPE_PAL, /* input type */
248 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
252 { 0x00, 0x00 }, /* band-switch crosspoints */
253 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
255 /* PHILIPS_FR1236_SECAM */
256 { "Philips FR1236 SECAM FM", /* the 'name' */
257 TTYPE_SECAM, /* input type */
258 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
262 { 0x00, 0x00 }, /* band-switch crosspoints */
263 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
265 /* ALPS TSCH5 NTSC */
266 { "ALPS TSCH5 NTSC FM", /* the 'name' */
267 TTYPE_NTSC, /* input type */
268 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
272 { 0x00, 0x00 }, /* band-switch crosspoints */
273 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
275 /* ALPS TSBH1 NTSC */
276 { "ALPS TSBH1 NTSC", /* the 'name' */
277 TTYPE_NTSC, /* input type */
278 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
282 { 0x00, 0x00 }, /* band-switch crosspoints */
283 { 0x01, 0x02, 0x08, 0x00 } }, /* the band-switch values */
285 /* MT2032 Microtune */
286 { "MT2032", /* the 'name' */
287 TTYPE_PAL, /* input type */
288 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
292 { 0x00, 0x00 }, /* band-switch crosspoints */
293 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
295 /* LG TPI8PSB12P PAL */
296 { "LG TPI8PSB12P PAL", /* the 'name' */
297 TTYPE_PAL, /* input type */
298 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
302 { 0x00, 0x00 }, /* band-switch crosspoints */
303 { 0xa0, 0x90, 0x30, 0x8e } }, /* the band-switch values */
307 /* scaling factor for frequencies expressed as ints */
308 #define FREQFACTOR 16
312 * entry 0: MAX legal channel
313 * entry 1: IF frequency
314 * expressed as fi{mHz} * 16,
315 * eg 45.75mHz == 45.75 * 16 = 732
316 * entry 2: [place holder/future]
317 * entry 3: base of channel record 0
318 * entry 3 + (x*3): base of channel record 'x'
319 * entry LAST: NULL channel entry marking end of records
322 * int 0: base channel
323 * int 1: frequency of base channel,
324 * expressed as fb{mHz} * 16,
325 * int 2: offset frequency between channels,
326 * expressed as fo{mHz} * 16,
330 * North American Broadcast Channels:
332 * 2: 55.25 mHz - 4: 67.25 mHz
333 * 5: 77.25 mHz - 6: 83.25 mHz
334 * 7: 175.25 mHz - 13: 211.25 mHz
335 * 14: 471.25 mHz - 83: 885.25 mHz
340 static int nabcst[] = {
341 83, (int)( 45.75 * FREQFACTOR), 0,
342 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
343 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
344 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
345 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
351 * North American Cable Channels, IRC:
353 * 2: 55.25 mHz - 4: 67.25 mHz
354 * 5: 77.25 mHz - 6: 83.25 mHz
355 * 7: 175.25 mHz - 13: 211.25 mHz
356 * 14: 121.25 mHz - 22: 169.25 mHz
357 * 23: 217.25 mHz - 94: 643.25 mHz
358 * 95: 91.25 mHz - 99: 115.25 mHz
363 static int irccable[] = {
364 116, (int)( 45.75 * FREQFACTOR), 0,
365 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
366 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
367 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
368 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
369 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
370 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
371 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
377 * North American Cable Channels, HRC:
379 * 2: 54 mHz - 4: 66 mHz
380 * 5: 78 mHz - 6: 84 mHz
381 * 7: 174 mHz - 13: 210 mHz
382 * 14: 120 mHz - 22: 168 mHz
383 * 23: 216 mHz - 94: 642 mHz
384 * 95: 90 mHz - 99: 114 mHz
389 static int hrccable[] = {
390 116, (int)( 45.75 * FREQFACTOR), 0,
391 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
392 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
393 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
394 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
395 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
396 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
397 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
403 * Western European broadcast channels:
405 * (there are others that appear to vary between countries - rmt)
407 * here's the table Philips provides:
408 * caution, some of the offsets don't compute...
517 * Channels S21 - S41 are taken from
518 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
542 * 121 3890 000 IFFREQ
545 static int weurope[] = {
546 121, (int)( 38.90 * FREQFACTOR), 0,
547 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
548 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
549 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
550 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
551 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
552 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
553 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
554 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
555 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
556 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
557 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
562 * Japanese Broadcast Channels:
564 * 1: 91.25MHz - 3: 103.25MHz
565 * 4: 171.25MHz - 7: 189.25MHz
566 * 8: 193.25MHz - 12: 217.25MHz (VHF)
567 * 13: 471.25MHz - 62: 765.25MHz (UHF)
574 #define IF_FREQ 45.75
575 static int jpnbcst[] = {
576 62, (int)(IF_FREQ * FREQFACTOR), 0,
577 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
578 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
579 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
580 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
587 * Japanese Cable Channels:
589 * 1: 91.25MHz - 3: 103.25MHz
590 * 4: 171.25MHz - 7: 189.25MHz
591 * 8: 193.25MHz - 12: 217.25MHz
592 * 13: 109.25MHz - 21: 157.25MHz
594 * 23: 223.25MHz - 63: 463.25MHz
599 #define IF_FREQ 45.75
600 static int jpncable[] = {
601 63, (int)(IF_FREQ * FREQFACTOR), 0,
602 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
603 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
604 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
605 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
606 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
607 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
614 * xUSSR Broadcast Channels:
616 * 1: 49.75MHz - 2: 59.25MHz
617 * 3: 77.25MHz - 5: 93.25MHz
618 * 6: 175.25MHz - 12: 223.25MHz
620 * 21: 471.25MHz - 34: 575.25MHz
621 * 35: 583.25MHz - 69: 855.25MHz
625 * 70: 111.25MHz - 77: 167.25MHz
626 * 78: 231.25MHz -107: 463.25MHz
630 #define IF_FREQ 38.90
631 static int xussr[] = {
632 107, (int)(IF_FREQ * FREQFACTOR), 0,
633 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
634 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
635 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
636 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
637 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
638 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
639 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
645 * Australian broadcast channels
648 #define IF_FREQ 38.90
649 static int australia[] = {
650 83, (int)(IF_FREQ * FREQFACTOR), 0,
651 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
652 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
653 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
654 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
655 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
656 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
663 * France broadcast channels
666 #define IF_FREQ 38.90
667 static int france[] = {
668 69, (int)(IF_FREQ * FREQFACTOR), 0,
669 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
670 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
671 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
672 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
673 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
681 char name[BT848_MAX_CHNLSET_NAME_LEN];
685 {irccable, "cableirc"},
686 {hrccable, "cablehrc"},
687 {weurope, "weurope"},
688 {jpnbcst, "jpnbcst"},
689 {jpncable, "jpncable"},
691 {australia, "australia"},
696 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
697 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
698 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
700 frequency_lookup( bktr_ptr_t bktr, int channel )
704 /* check for "> MAX channel" */
706 if ( channel > TBL_CHNL )
709 /* search the table for data */
710 for ( x = 3; TBL_CHNL; x += 3 ) {
711 if ( channel >= TBL_CHNL ) {
712 return( TBL_BASE_FREQ +
713 ((channel - TBL_CHNL) * TBL_OFFSET) );
717 /* not found, must be below the MIN channel */
725 #define TBL_IF (bktr->format_params == BT848_IFORM_F_NTSCJ || \
726 bktr->format_params == BT848_IFORM_F_NTSCM ? \
727 nabcst[1] : weurope[1])
730 /* Initialise the tuner structures in the bktr_softc */
731 /* This is needed as the tuner details are no longer globally declared */
733 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
734 if (tuner_type < Bt848_MAX_TUNER) {
735 bktr->card.tuner = &tuners[ tuner_type ];
737 bktr->card.tuner = NULL;
743 * Programming the tuner properly is quite complicated.
744 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
745 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
746 * 87.5 Mhz to 108.0 Mhz.
748 * RF and IF. RF = radio frequencies, it is the transmitted signal.
749 * IF is the Intermediate Frequency (the offset from the base
750 * signal where the video, color, audio and NICAM signals are.
752 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
753 * NICAM at 32.348 Mhz.
754 * Strangely enough, there is an IF (intermediate frequency) for
755 * FM Radio which is 10.7 Mhz.
757 * The tuner also works in Bands. Philips bands are
758 * FM radio band 87.50 to 108.00 MHz
759 * Low band 45.75 to 170.00 MHz
760 * Mid band 170.00 to 450.00 MHz
761 * High band 450.00 to 855.25 MHz
764 * Now we need to set the PLL on the tuner to the required freuqncy.
765 * It has a programmable divisor.
767 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
770 * For RADIO we want a different equation.
771 * freq IF is 10.70 MHz (so the data sheet tells me)
772 * N = (freq RF + freq IF) / step size
773 * The step size must be set to 50 khz (so the data sheet tells me)
774 * (note this is 50 kHz, the other things are in MHz)
775 * so we end up with N = 20x(freq RF + 10.7)
782 #define FM_RADIO_BAND 3
785 /* Check if these are correct for other than Philips PAL */
786 #define STATUSBIT_COLD 0x80
787 #define STATUSBIT_LOCK 0x40
788 #define STATUSBIT_TV 0x20
789 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
790 #define STATUSBIT_ADC 0x07
793 * set the frequency of the tuner
794 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
795 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
796 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
800 tv_freq( bktr_ptr_t bktr, int frequency, int type )
802 const struct TUNER* tuner;
808 #if defined( TEST_TUNER_AFC )
809 int oldFrequency, afcDelta;
812 tuner = bktr->card.tuner;
816 if (tuner == &tuners[TUNER_MT2032]) {
817 mt2032_set_tv_freq(bktr, frequency);
820 if (type == TV_FREQUENCY) {
822 * select the band based on frequency
823 * XXX FIXME: get the cross-over points from the tuner struct
825 if ( frequency < (160 * FREQFACTOR ) )
826 band_select = LOW_BAND;
827 else if ( frequency < (454 * FREQFACTOR ) )
828 band_select = MID_BAND;
830 band_select = HIGH_BAND;
832 #if defined( TEST_TUNER_AFC )
833 if ( bktr->tuner.afc )
837 * N = 16 * { fRF(pc) + fIF(pc) }
838 * or N = 16* fRF(pc) + 16*fIF(pc) }
840 * pc is picture carrier, fRF & fIF are in MHz
842 * fortunatly, frequency is passed in as MHz * 16
843 * and the TBL_IF frequency is also stored in MHz * 16
845 N = frequency + TBL_IF;
847 /* set the address of the PLL */
848 addr = bktr->card.tuner_pllAddr;
849 control = tuner->pllControl[ band_select ];
850 band = tuner->bandAddrs[ band_select ];
852 if(!(band && control)) /* Don't try to set un- */
853 return(-1); /* supported modes. */
855 if ( frequency > bktr->tuner.frequency ) {
856 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
857 i2cWrite( bktr, addr, control, band );
860 i2cWrite( bktr, addr, control, band );
861 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
864 #if defined( TUNER_AFC )
865 if ( bktr->tuner.afc == TRUE ) {
866 #if defined( TEST_TUNER_AFC )
867 oldFrequency = frequency;
869 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
870 /* AFC failed, restore requested frequency */
871 N = frequency + TBL_IF;
872 #if defined( TEST_TUNER_AFC )
873 printf("%s: do_afc: failed to lock\n",
876 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
879 frequency = N - TBL_IF;
880 #if defined( TEST_TUNER_AFC )
881 printf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
882 afcDelta = frequency - oldFrequency;
883 printf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
886 #endif /* TUNER_AFC */
888 bktr->tuner.frequency = frequency;
891 if ( type == FM_RADIO_FREQUENCY ) {
892 band_select = FM_RADIO_BAND;
895 * N = { fRF(pc) + fIF(pc) }/step_size
896 * The step size is 50kHz for FM radio.
897 * (eg after 102.35MHz comes 102.40 MHz)
898 * fIF is 10.7 MHz (as detailed in the specs)
900 * frequency is passed in as MHz * 100
902 * So, we have N = (frequency/100 + 10.70) /(50/1000)
904 N = (frequency + 1070)/5;
906 /* set the address of the PLL */
907 addr = bktr->card.tuner_pllAddr;
908 control = tuner->pllControl[ band_select ];
909 band = tuner->bandAddrs[ band_select ];
911 if(!(band && control)) /* Don't try to set un- */
912 return(-1); /* supported modes. */
914 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
915 * the ioctls RADIO_SETMODE
916 * and RADIO_GETMODE */
918 i2cWrite( bktr, addr, control, band );
919 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
921 bktr->tuner.frequency = (N * 5) - 1070;
932 #if defined( TUNER_AFC )
937 do_afc( bktr_ptr_t bktr, int addr, int frequency )
943 origFrequency = frequency;
945 /* wait for first setting to take effect */
946 tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
948 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
951 #if defined( TEST_TUNER_AFC )
952 printf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
954 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
955 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
957 if ( !(status & 0x40) ) {
958 #if defined( TEST_TUNER_AFC )
959 printf( "%s: no lock!\n", bktr_name(bktr) );
964 switch( status & AFC_BITS ) {
965 case AFC_FREQ_CENTERED:
966 #if defined( TEST_TUNER_AFC )
967 printf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
971 case AFC_FREQ_MINUS_125:
972 case AFC_FREQ_MINUS_62:
973 #if defined( TEST_TUNER_AFC )
974 printf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
979 case AFC_FREQ_PLUS_62:
980 case AFC_FREQ_PLUS_125:
981 #if defined( TEST_TUNER_AFC )
982 printf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
988 i2cWrite( bktr, addr,
989 (frequency>>8) & 0x7f, frequency & 0xff );
994 i2cWrite( bktr, addr,
995 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
999 #endif /* TUNER_AFC */
1004 * Get the Tuner status and signal strength
1006 int get_tuner_status( bktr_ptr_t bktr ) {
1007 if (bktr->card.tuner == &tuners[TUNER_MT2032])
1009 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
1013 * set the channel of the tuner
1016 tv_channel( bktr_ptr_t bktr, int channel )
1020 /* calculate the frequency according to tuner type */
1021 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
1024 /* set the new frequency */
1025 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
1028 /* OK to update records */
1029 return( (bktr->tuner.channel = channel) );
1033 * get channelset name
1036 tuner_getchnlset(struct bktr_chnlset *chnlset)
1038 if (( chnlset->index < CHNLSET_MIN ) ||
1039 ( chnlset->index > CHNLSET_MAX ))
1042 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1043 BT848_MAX_CHNLSET_NAME_LEN);
1045 chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1052 #define TDA9887_ADDR 0x86
1055 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1057 u_char addr = TDA9887_ADDR;
1059 i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1060 i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1062 /* PAL BG: 0x09 PAL I: 0x0a NTSC: 0x04 */
1064 i2cWrite(bktr, addr, 2, 0x04);
1066 i2cWrite(bktr, addr, 2, 0x09);
1073 #define MT2032_OPTIMIZE_VCO 1
1075 /* holds the value of XOGC register after init */
1076 static int MT2032_XOGC = 4;
1078 /* card.tuner_pllAddr not set during init */
1079 #define MT2032_ADDR 0xc0
1082 #define MT2032_ADDR (bktr->card.tuner_pllAddr)
1086 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1090 if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1092 printf("%s: MT2032 write failed (i2c addr %#x)\n",
1093 bktr_name(bktr), MT2032_ADDR);
1096 if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1098 printf("%s: MT2032 get register %d failed\n",
1099 bktr_name(bktr), regNum);
1106 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1108 i2cWrite(bktr, MT2032_ADDR, regNum, data);
1111 #define MT2032_GetRegister(r) _MT2032_GetRegister(bktr,r)
1112 #define MT2032_SetRegister(r,d) _MT2032_SetRegister(bktr,r,d)
1116 mt2032_init(bktr_ptr_t bktr)
1123 TDA9887_init(bktr, 0);
1125 for (i = 0; i < 21; i++) {
1126 if ((x = MT2032_GetRegister(i)) == -1)
1133 printf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1135 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1136 if (rdbuf[0x13] != 4) {
1137 printf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1141 /* Initialize Registers per spec. */
1142 MT2032_SetRegister(2, 0xff);
1143 MT2032_SetRegister(3, 0x0f);
1144 MT2032_SetRegister(4, 0x1f);
1145 MT2032_SetRegister(6, 0xe4);
1146 MT2032_SetRegister(7, 0x8f);
1147 MT2032_SetRegister(8, 0xc3);
1148 MT2032_SetRegister(9, 0x4e);
1149 MT2032_SetRegister(10, 0xec);
1150 MT2032_SetRegister(13, 0x32);
1152 /* Adjust XOGC (register 7), wait for XOK */
1156 xok = MT2032_GetRegister(0x0e) & 0x01;
1162 xogc = 4; /* min. 4 per spec */
1165 MT2032_SetRegister(7, 0x88 + xogc);
1168 TDA9887_init(bktr, 1);
1176 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1180 f1 = f1 / 1000; /* scale to kHz to avoid 32bit overflows */
1182 spectrum_from /= 1000;
1183 spectrum_to /= 1000;
1191 if ((f > spectrum_from) && (f < spectrum_to)) {
1194 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1213 int fref, lo1, lo1n, lo1a, s, sel;
1214 int lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1218 fref = 5250 * 1000; /* 5.25MHz */
1220 /* per spec 2.3.1 */
1221 desired_lo1 = rfin + if1;
1222 lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1223 lo1freq = lo1 * fref;
1224 desired_lo2 = lo1freq - rfin - if2;
1226 /* per spec 2.3.2 */
1227 for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1228 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1231 if (lo1freq < desired_lo1) {
1237 lo1freq = lo1 * fref;
1238 desired_lo2 = lo1freq - rfin - if2;
1241 /* per spec 2.3.3 */
1242 s = lo1freq / 1000 / 1000;
1244 if (MT2032_OPTIMIZE_VCO) {
1247 } else if (s > 1720) {
1249 } else if (s > 1530) {
1251 } else if (s > 1370) {
1259 } else if (s > 1617) {
1261 } else if (s > 1449) {
1263 } else if (s > 1291) {
1272 /* per spec 2.3.4 */
1274 lo1a = lo1 - (lo1n * 8);
1275 lo2 = desired_lo2 / fref;
1277 lo2a = lo2 - (lo2n * 8);
1278 /* scale to fit in 32bit arith */
1279 lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1280 lo2freq = (lo2a + 8 * lo2n) * fref + lo2num * (fref / 1000) / 3780 * 1000;
1282 if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1283 lo2a > 7 || lo2n < 17 || lo2n > 30) {
1284 printf("MT2032: parameter out of range\n");
1287 /* set up MT2032 register map for transfer over i2c */
1289 buf[1] = lo1a | (sel << 4);
1290 buf[2] = 0x86; /* LOGC */
1291 buf[3] = 0x0f; /* reserved */
1293 buf[5] = (lo2n - 1) | (lo2a << 5);
1294 if (rfin < 400 * 1000 * 1000) {
1297 buf[6] = 0xf4; /* set PKEN per rev 1.2 */
1301 buf[8] = 0xc3; /* reserved */
1302 buf[9] = 0x4e; /* reserved */
1303 buf[10] = 0xec; /* reserved */
1304 buf[11] = (lo2num & 0xff);
1305 buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1311 MT2032_CheckLOLock(bktr_ptr_t bktr)
1314 for (t = 0; t < 10; t++) {
1315 lock = MT2032_GetRegister(0x0e) & 0x06;
1325 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1329 tad1 = MT2032_GetRegister(0x0f) & 0x07;
1350 lo1a = MT2032_GetRegister(0x01) & 0x07;
1351 MT2032_SetRegister(0x01, lo1a | (sel << 4));
1352 lock = MT2032_CheckLOLock(bktr);
1357 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1360 int lint_try, sel, lock = 0;
1362 if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1365 TDA9887_init(bktr, 0);
1367 /* send only the relevant registers per Rev. 1.2 */
1368 MT2032_SetRegister(0, buf[0x00]);
1369 MT2032_SetRegister(1, buf[0x01]);
1370 MT2032_SetRegister(2, buf[0x02]);
1372 MT2032_SetRegister(5, buf[0x05]);
1373 MT2032_SetRegister(6, buf[0x06]);
1374 MT2032_SetRegister(7, buf[0x07]);
1376 MT2032_SetRegister(11, buf[0x0B]);
1377 MT2032_SetRegister(12, buf[0x0C]);
1379 /* wait for PLLs to lock (per manual), retry LINT if not. */
1380 for (lint_try = 0; lint_try < 2; lint_try++) {
1381 lock = MT2032_CheckLOLock(bktr);
1383 if (MT2032_OPTIMIZE_VCO) {
1384 lock = MT2032_OptimizeVCO(bktr, sel, lock);
1389 /* set LINT to re-init PLLs */
1390 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1392 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1395 printf("%s: PLL didn't lock\n", bktr_name(bktr));
1397 MT2032_SetRegister(2, 0x20);
1399 TDA9887_init(bktr, 1);
1404 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1419 if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1420 1090*1000*1000, if2, from, to) == 0) {
1421 bktr->tuner.frequency = freq;
1422 stat = MT2032_GetRegister(0x0e);
1423 tad = MT2032_GetRegister(0x0f);
1425 printf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1426 bktr_name(bktr), freq*62500, stat, tad);