Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly/vkernel-mp.git] / sys / dev / video / bktr / bktr_tuner.c
blob8f8c3bf420b4d1d1a4aa17c722da572170755469
1 /*
2 * 1. Redistributions of source code must retain the
3 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Amancio Hasty and
17 * Roger Hardiman
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * $FreeBSD: src/sys/dev/bktr/bktr_tuner.c,v 1.18 2005/01/23 07:13:09 julian Exp
34 * $DragonFly: src/sys/dev/video/bktr/bktr_tuner.c,v 1.9 2006/12/23 00:26:26 swildner Exp $
39 * This is part of the Driver for Video Capture Cards (Frame grabbers)
40 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
41 * chipset.
42 * Copyright Roger Hardiman and Amancio Hasty.
44 * bktr_tuner : This deals with controlling the tuner fitted to TV cards.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/bus.h>
53 #include <bus/pci/pcivar.h>
55 #include <dev/video/meteor/ioctl_meteor.h>
56 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
57 #include <dev/video/bktr/bktr_reg.h>
58 #include <dev/video/bktr/bktr_tuner.h>
59 #include <dev/video/bktr/bktr_card.h>
60 #include <dev/video/bktr/bktr_core.h>
64 #if defined( TUNER_AFC )
65 #define AFC_DELAY 10000 /* 10 millisend delay */
66 #define AFC_BITS 0x07
67 #define AFC_FREQ_MINUS_125 0x00
68 #define AFC_FREQ_MINUS_62 0x01
69 #define AFC_FREQ_CENTERED 0x02
70 #define AFC_FREQ_PLUS_62 0x03
71 #define AFC_FREQ_PLUS_125 0x04
72 #define AFC_MAX_STEP (5 * FREQFACTOR) /* no more than 5 MHz */
73 #endif /* TUNER_AFC */
76 #define TTYPE_XXX 0
77 #define TTYPE_NTSC 1
78 #define TTYPE_NTSC_J 2
79 #define TTYPE_PAL 3
80 #define TTYPE_PAL_M 4
81 #define TTYPE_PAL_N 5
82 #define TTYPE_SECAM 6
84 #define TSA552x_CB_MSB (0x80)
85 #define TSA552x_CB_CP (1<<6) /* set this for fast tuning */
86 #define TSA552x_CB_T2 (1<<5) /* test mode - Normally set to 0 */
87 #define TSA552x_CB_T1 (1<<4) /* test mode - Normally set to 0 */
88 #define TSA552x_CB_T0 (1<<3) /* test mode - Normally set to 1 */
89 #define TSA552x_CB_RSA (1<<2) /* 0 for 31.25 khz, 1 for 62.5 kHz */
90 #define TSA552x_CB_RSB (1<<1) /* 0 for FM 50kHz steps, 1 = Use RSA*/
91 #define TSA552x_CB_OS (1<<0) /* Set to 0 for normal operation */
93 #define TSA552x_RADIO (TSA552x_CB_MSB | \
94 TSA552x_CB_T0)
96 /* raise the charge pump voltage for fast tuning */
97 #define TSA552x_FCONTROL (TSA552x_CB_MSB | \
98 TSA552x_CB_CP | \
99 TSA552x_CB_T0 | \
100 TSA552x_CB_RSA | \
101 TSA552x_CB_RSB)
103 /* lower the charge pump voltage for better residual oscillator FM */
104 #define TSA552x_SCONTROL (TSA552x_CB_MSB | \
105 TSA552x_CB_T0 | \
106 TSA552x_CB_RSA | \
107 TSA552x_CB_RSB)
109 /* The control value for the ALPS TSCH5 Tuner */
110 #define TSCH5_FCONTROL 0x82
111 #define TSCH5_RADIO 0x86
113 /* The control value for the ALPS TSBH1 Tuner */
114 #define TSBH1_FCONTROL 0xce
117 static void mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq);
120 static const struct TUNER tuners[] = {
121 /* XXX FIXME: fill in the band-switch crosspoints */
122 /* NO_TUNER */
123 { "<no>", /* the 'name' */
124 TTYPE_XXX, /* input type */
125 { 0x00, /* control byte for Tuner PLL */
126 0x00,
127 0x00,
128 0x00 },
129 { 0x00, 0x00 }, /* band-switch crosspoints */
130 { 0x00, 0x00, 0x00,0x00} }, /* the band-switch values */
132 /* TEMIC_NTSC */
133 { "Temic NTSC", /* the 'name' */
134 TTYPE_NTSC, /* input type */
135 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
136 TSA552x_SCONTROL,
137 TSA552x_SCONTROL,
138 0x00 },
139 { 0x00, 0x00}, /* band-switch crosspoints */
140 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
142 /* TEMIC_PAL */
143 { "Temic PAL", /* the 'name' */
144 TTYPE_PAL, /* input type */
145 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
146 TSA552x_SCONTROL,
147 TSA552x_SCONTROL,
148 0x00 },
149 { 0x00, 0x00 }, /* band-switch crosspoints */
150 { 0x02, 0x04, 0x01, 0x00 } }, /* the band-switch values */
152 /* TEMIC_SECAM */
153 { "Temic SECAM", /* the 'name' */
154 TTYPE_SECAM, /* input type */
155 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
156 TSA552x_SCONTROL,
157 TSA552x_SCONTROL,
158 0x00 },
159 { 0x00, 0x00 }, /* band-switch crosspoints */
160 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
162 /* PHILIPS_NTSC */
163 { "Philips NTSC", /* the 'name' */
164 TTYPE_NTSC, /* input type */
165 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
166 TSA552x_SCONTROL,
167 TSA552x_SCONTROL,
168 0x00 },
169 { 0x00, 0x00 }, /* band-switch crosspoints */
170 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
172 /* PHILIPS_PAL */
173 { "Philips PAL", /* the 'name' */
174 TTYPE_PAL, /* input type */
175 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
176 TSA552x_SCONTROL,
177 TSA552x_SCONTROL,
178 0x00 },
179 { 0x00, 0x00 }, /* band-switch crosspoints */
180 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
182 /* PHILIPS_SECAM */
183 { "Philips SECAM", /* the 'name' */
184 TTYPE_SECAM, /* input type */
185 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
186 TSA552x_SCONTROL,
187 TSA552x_SCONTROL,
188 0x00 },
189 { 0x00, 0x00 }, /* band-switch crosspoints */
190 { 0xa7, 0x97, 0x37, 0x00 } }, /* the band-switch values */
192 /* TEMIC_PAL I */
193 { "Temic PAL I", /* the 'name' */
194 TTYPE_PAL, /* input type */
195 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
196 TSA552x_SCONTROL,
197 TSA552x_SCONTROL,
198 0x00 },
199 { 0x00, 0x00 }, /* band-switch crosspoints */
200 { 0x02, 0x04, 0x01,0x00 } }, /* the band-switch values */
202 /* PHILIPS_PALI */
203 { "Philips PAL I", /* the 'name' */
204 TTYPE_PAL, /* input type */
205 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
206 TSA552x_SCONTROL,
207 TSA552x_SCONTROL,
208 0x00 },
209 { 0x00, 0x00 }, /* band-switch crosspoints */
210 { 0xa0, 0x90, 0x30,0x00 } }, /* the band-switch values */
212 /* PHILIPS_FR1236_NTSC */
213 { "Philips FR1236 NTSC FM", /* the 'name' */
214 TTYPE_NTSC, /* input type */
215 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
216 TSA552x_FCONTROL,
217 TSA552x_FCONTROL,
218 TSA552x_RADIO },
219 { 0x00, 0x00 }, /* band-switch crosspoints */
220 { 0xa0, 0x90, 0x30,0xa4 } }, /* the band-switch values */
222 /* PHILIPS_FR1216_PAL */
223 { "Philips FR1216 PAL FM" , /* the 'name' */
224 TTYPE_PAL, /* input type */
225 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
226 TSA552x_FCONTROL,
227 TSA552x_FCONTROL,
228 TSA552x_RADIO },
229 { 0x00, 0x00 }, /* band-switch crosspoints */
230 { 0xa0, 0x90, 0x30, 0xa4 } }, /* the band-switch values */
232 /* PHILIPS_FR1236_SECAM */
233 { "Philips FR1236 SECAM FM", /* the 'name' */
234 TTYPE_SECAM, /* input type */
235 { TSA552x_FCONTROL, /* control byte for Tuner PLL */
236 TSA552x_FCONTROL,
237 TSA552x_FCONTROL,
238 TSA552x_RADIO },
239 { 0x00, 0x00 }, /* band-switch crosspoints */
240 { 0xa7, 0x97, 0x37, 0xa4 } }, /* the band-switch values */
242 /* ALPS TSCH5 NTSC */
243 { "ALPS TSCH5 NTSC FM", /* the 'name' */
244 TTYPE_NTSC, /* input type */
245 { TSCH5_FCONTROL, /* control byte for Tuner PLL */
246 TSCH5_FCONTROL,
247 TSCH5_FCONTROL,
248 TSCH5_RADIO },
249 { 0x00, 0x00 }, /* band-switch crosspoints */
250 { 0x14, 0x12, 0x11, 0x04 } }, /* the band-switch values */
252 /* ALPS TSBH1 NTSC */
253 { "ALPS TSBH1 NTSC", /* the 'name' */
254 TTYPE_NTSC, /* input type */
255 { TSBH1_FCONTROL, /* control byte for Tuner PLL */
256 TSBH1_FCONTROL,
257 TSBH1_FCONTROL,
258 0x00 },
259 { 0x00, 0x00 }, /* band-switch crosspoints */
260 { 0x01, 0x02, 0x08, 0x00 } }, /* the band-switch values */
262 /* MT2032 Microtune */
263 { "MT2032", /* the 'name' */
264 TTYPE_PAL, /* input type */
265 { TSA552x_SCONTROL, /* control byte for Tuner PLL */
266 TSA552x_SCONTROL,
267 TSA552x_SCONTROL,
268 0x00 },
269 { 0x00, 0x00 }, /* band-switch crosspoints */
270 { 0xa0, 0x90, 0x30, 0x00 } }, /* the band-switch values */
274 /* scaling factor for frequencies expressed as ints */
275 #define FREQFACTOR 16
278 * Format:
279 * entry 0: MAX legal channel
280 * entry 1: IF frequency
281 * expressed as fi{mHz} * 16,
282 * eg 45.75mHz == 45.75 * 16 = 732
283 * entry 2: [place holder/future]
284 * entry 3: base of channel record 0
285 * entry 3 + (x*3): base of channel record 'x'
286 * entry LAST: NULL channel entry marking end of records
288 * Record:
289 * int 0: base channel
290 * int 1: frequency of base channel,
291 * expressed as fb{mHz} * 16,
292 * int 2: offset frequency between channels,
293 * expressed as fo{mHz} * 16,
297 * North American Broadcast Channels:
299 * 2: 55.25 mHz - 4: 67.25 mHz
300 * 5: 77.25 mHz - 6: 83.25 mHz
301 * 7: 175.25 mHz - 13: 211.25 mHz
302 * 14: 471.25 mHz - 83: 885.25 mHz
304 * IF freq: 45.75 mHz
306 #define OFFSET 6.00
307 static int nabcst[] = {
308 83, (int)( 45.75 * FREQFACTOR), 0,
309 14, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
310 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
311 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
312 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
315 #undef OFFSET
318 * North American Cable Channels, IRC:
320 * 2: 55.25 mHz - 4: 67.25 mHz
321 * 5: 77.25 mHz - 6: 83.25 mHz
322 * 7: 175.25 mHz - 13: 211.25 mHz
323 * 14: 121.25 mHz - 22: 169.25 mHz
324 * 23: 217.25 mHz - 94: 643.25 mHz
325 * 95: 91.25 mHz - 99: 115.25 mHz
327 * IF freq: 45.75 mHz
329 #define OFFSET 6.00
330 static int irccable[] = {
331 116, (int)( 45.75 * FREQFACTOR), 0,
332 100, (int)(649.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
333 95, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
334 23, (int)(217.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
335 14, (int)(121.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
336 7, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
337 5, (int)( 77.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
338 2, (int)( 55.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
341 #undef OFFSET
344 * North American Cable Channels, HRC:
346 * 2: 54 mHz - 4: 66 mHz
347 * 5: 78 mHz - 6: 84 mHz
348 * 7: 174 mHz - 13: 210 mHz
349 * 14: 120 mHz - 22: 168 mHz
350 * 23: 216 mHz - 94: 642 mHz
351 * 95: 90 mHz - 99: 114 mHz
353 * IF freq: 45.75 mHz
355 #define OFFSET 6.00
356 static int hrccable[] = {
357 116, (int)( 45.75 * FREQFACTOR), 0,
358 100, (int)(648.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
359 95, (int)( 90.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
360 23, (int)(216.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
361 14, (int)(120.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
362 7, (int)(174.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
363 5, (int)( 78.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
364 2, (int)( 54.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
367 #undef OFFSET
370 * Western European broadcast channels:
372 * (there are others that appear to vary between countries - rmt)
374 * here's the table Philips provides:
375 * caution, some of the offsets don't compute...
377 * 1 4525 700 N21
379 * 2 4825 700 E2
380 * 3 5525 700 E3
381 * 4 6225 700 E4
383 * 5 17525 700 E5
384 * 6 18225 700 E6
385 * 7 18925 700 E7
386 * 8 19625 700 E8
387 * 9 20325 700 E9
388 * 10 21025 700 E10
389 * 11 21725 700 E11
390 * 12 22425 700 E12
392 * 13 5375 700 ITA
393 * 14 6225 700 ITB
395 * 15 8225 700 ITC
397 * 16 17525 700 ITD
398 * 17 18325 700 ITE
400 * 18 19225 700 ITF
401 * 19 20125 700 ITG
402 * 20 21025 700 ITH
404 * 21 47125 800 E21
405 * 22 47925 800 E22
406 * 23 48725 800 E23
407 * 24 49525 800 E24
408 * 25 50325 800 E25
409 * 26 51125 800 E26
410 * 27 51925 800 E27
411 * 28 52725 800 E28
412 * 29 53525 800 E29
413 * 30 54325 800 E30
414 * 31 55125 800 E31
415 * 32 55925 800 E32
416 * 33 56725 800 E33
417 * 34 57525 800 E34
418 * 35 58325 800 E35
419 * 36 59125 800 E36
420 * 37 59925 800 E37
421 * 38 60725 800 E38
422 * 39 61525 800 E39
423 * 40 62325 800 E40
424 * 41 63125 800 E41
425 * 42 63925 800 E42
426 * 43 64725 800 E43
427 * 44 65525 800 E44
428 * 45 66325 800 E45
429 * 46 67125 800 E46
430 * 47 67925 800 E47
431 * 48 68725 800 E48
432 * 49 69525 800 E49
433 * 50 70325 800 E50
434 * 51 71125 800 E51
435 * 52 71925 800 E52
436 * 53 72725 800 E53
437 * 54 73525 800 E54
438 * 55 74325 800 E55
439 * 56 75125 800 E56
440 * 57 75925 800 E57
441 * 58 76725 800 E58
442 * 59 77525 800 E59
443 * 60 78325 800 E60
444 * 61 79125 800 E61
445 * 62 79925 800 E62
446 * 63 80725 800 E63
447 * 64 81525 800 E64
448 * 65 82325 800 E65
449 * 66 83125 800 E66
450 * 67 83925 800 E67
451 * 68 84725 800 E68
452 * 69 85525 800 E69
454 * 70 4575 800 IA
455 * 71 5375 800 IB
456 * 72 6175 800 IC
458 * 74 6925 700 S01
459 * 75 7625 700 S02
460 * 76 8325 700 S03
462 * 80 10525 700 S1
463 * 81 11225 700 S2
464 * 82 11925 700 S3
465 * 83 12625 700 S4
466 * 84 13325 700 S5
467 * 85 14025 700 S6
468 * 86 14725 700 S7
469 * 87 15425 700 S8
470 * 88 16125 700 S9
471 * 89 16825 700 S10
472 * 90 23125 700 S11
473 * 91 23825 700 S12
474 * 92 24525 700 S13
475 * 93 25225 700 S14
476 * 94 25925 700 S15
477 * 95 26625 700 S16
478 * 96 27325 700 S17
479 * 97 28025 700 S18
480 * 98 28725 700 S19
481 * 99 29425 700 S20
484 * Channels S21 - S41 are taken from
485 * http://gemma.apple.com:80/dev/technotes/tn/tn1012.html
487 * 100 30325 800 S21
488 * 101 31125 800 S22
489 * 102 31925 800 S23
490 * 103 32725 800 S24
491 * 104 33525 800 S25
492 * 105 34325 800 S26
493 * 106 35125 800 S27
494 * 107 35925 800 S28
495 * 108 36725 800 S29
496 * 109 37525 800 S30
497 * 110 38325 800 S31
498 * 111 39125 800 S32
499 * 112 39925 800 S33
500 * 113 40725 800 S34
501 * 114 41525 800 S35
502 * 115 42325 800 S36
503 * 116 43125 800 S37
504 * 117 43925 800 S38
505 * 118 44725 800 S39
506 * 119 45525 800 S40
507 * 120 46325 800 S41
509 * 121 3890 000 IFFREQ
512 static int weurope[] = {
513 121, (int)( 38.90 * FREQFACTOR), 0,
514 100, (int)(303.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
515 90, (int)(231.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
516 80, (int)(105.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
517 74, (int)( 69.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
518 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
519 17, (int)(183.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
520 16, (int)(175.25 * FREQFACTOR), (int)(9.00 * FREQFACTOR),
521 15, (int)(82.25 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
522 13, (int)(53.75 * FREQFACTOR), (int)(8.50 * FREQFACTOR),
523 5, (int)(175.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
524 2, (int)(48.25 * FREQFACTOR), (int)(7.00 * FREQFACTOR),
529 * Japanese Broadcast Channels:
531 * 1: 91.25MHz - 3: 103.25MHz
532 * 4: 171.25MHz - 7: 189.25MHz
533 * 8: 193.25MHz - 12: 217.25MHz (VHF)
534 * 13: 471.25MHz - 62: 765.25MHz (UHF)
536 * IF freq: 45.75 mHz
537 * OR
538 * IF freq: 58.75 mHz
540 #define OFFSET 6.00
541 #define IF_FREQ 45.75
542 static int jpnbcst[] = {
543 62, (int)(IF_FREQ * FREQFACTOR), 0,
544 13, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
545 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
546 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
547 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
550 #undef IF_FREQ
551 #undef OFFSET
554 * Japanese Cable Channels:
556 * 1: 91.25MHz - 3: 103.25MHz
557 * 4: 171.25MHz - 7: 189.25MHz
558 * 8: 193.25MHz - 12: 217.25MHz
559 * 13: 109.25MHz - 21: 157.25MHz
560 * 22: 165.25MHz
561 * 23: 223.25MHz - 63: 463.25MHz
563 * IF freq: 45.75 mHz
565 #define OFFSET 6.00
566 #define IF_FREQ 45.75
567 static int jpncable[] = {
568 63, (int)(IF_FREQ * FREQFACTOR), 0,
569 23, (int)(223.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
570 22, (int)(165.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
571 13, (int)(109.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
572 8, (int)(193.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
573 4, (int)(171.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
574 1, (int)( 91.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
577 #undef IF_FREQ
578 #undef OFFSET
581 * xUSSR Broadcast Channels:
583 * 1: 49.75MHz - 2: 59.25MHz
584 * 3: 77.25MHz - 5: 93.25MHz
585 * 6: 175.25MHz - 12: 223.25MHz
586 * 13-20 - not exist
587 * 21: 471.25MHz - 34: 575.25MHz
588 * 35: 583.25MHz - 69: 855.25MHz
590 * Cable channels
592 * 70: 111.25MHz - 77: 167.25MHz
593 * 78: 231.25MHz -107: 463.25MHz
595 * IF freq: 38.90 MHz
597 #define IF_FREQ 38.90
598 static int xussr[] = {
599 107, (int)(IF_FREQ * FREQFACTOR), 0,
600 78, (int)(231.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
601 70, (int)(111.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
602 35, (int)(583.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
603 21, (int)(471.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
604 6, (int)(175.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
605 3, (int)( 77.25 * FREQFACTOR), (int)(8.00 * FREQFACTOR),
606 1, (int)( 49.75 * FREQFACTOR), (int)(9.50 * FREQFACTOR),
609 #undef IF_FREQ
612 * Australian broadcast channels
614 #define OFFSET 7.00
615 #define IF_FREQ 38.90
616 static int australia[] = {
617 83, (int)(IF_FREQ * FREQFACTOR), 0,
618 28, (int)(527.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
619 10, (int)(209.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
620 6, (int)(175.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
621 4, (int)( 95.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
622 3, (int)( 86.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
623 1, (int)( 57.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR),
626 #undef OFFSET
627 #undef IF_FREQ
630 * France broadcast channels
632 #define OFFSET 8.00
633 #define IF_FREQ 38.90
634 static int france[] = {
635 69, (int)(IF_FREQ * FREQFACTOR), 0,
636 21, (int)(471.25 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 21 -> 69 */
637 5, (int)(176.00 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 5 -> 10 */
638 4, (int)( 63.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 4 */
639 3, (int)( 60.50 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 3 */
640 1, (int)( 47.75 * FREQFACTOR), (int)(OFFSET * FREQFACTOR), /* 1 2 */
643 #undef OFFSET
644 #undef IF_FREQ
646 static struct {
647 int *ptr;
648 char name[BT848_MAX_CHNLSET_NAME_LEN];
649 } freqTable[] = {
650 {NULL, ""},
651 {nabcst, "nabcst"},
652 {irccable, "cableirc"},
653 {hrccable, "cablehrc"},
654 {weurope, "weurope"},
655 {jpnbcst, "jpnbcst"},
656 {jpncable, "jpncable"},
657 {xussr, "xussr"},
658 {australia, "australia"},
659 {france, "france"},
663 #define TBL_CHNL freqTable[ bktr->tuner.chnlset ].ptr[ x ]
664 #define TBL_BASE_FREQ freqTable[ bktr->tuner.chnlset ].ptr[ x + 1 ]
665 #define TBL_OFFSET freqTable[ bktr->tuner.chnlset ].ptr[ x + 2 ]
666 static int
667 frequency_lookup( bktr_ptr_t bktr, int channel )
669 int x;
671 /* check for "> MAX channel" */
672 x = 0;
673 if ( channel > TBL_CHNL )
674 return( -1 );
676 /* search the table for data */
677 for ( x = 3; TBL_CHNL; x += 3 ) {
678 if ( channel >= TBL_CHNL ) {
679 return( TBL_BASE_FREQ +
680 ((channel - TBL_CHNL) * TBL_OFFSET) );
684 /* not found, must be below the MIN channel */
685 return( -1 );
687 #undef TBL_OFFSET
688 #undef TBL_BASE_FREQ
689 #undef TBL_CHNL
692 #define TBL_IF freqTable[ bktr->tuner.chnlset ].ptr[ 1 ]
695 /* Initialise the tuner structures in the bktr_softc */
696 /* This is needed as the tuner details are no longer globally declared */
698 void select_tuner( bktr_ptr_t bktr, int tuner_type ) {
699 if (tuner_type < Bt848_MAX_TUNER) {
700 bktr->card.tuner = &tuners[ tuner_type ];
701 } else {
702 bktr->card.tuner = NULL;
707 * Tuner Notes:
708 * Programming the tuner properly is quite complicated.
709 * Here are some notes, based on a FM1246 data sheet for a PAL-I tuner.
710 * The tuner (front end) covers 45.75 Mhz - 855.25 Mhz and an FM band of
711 * 87.5 Mhz to 108.0 Mhz.
713 * RF and IF. RF = radio frequencies, it is the transmitted signal.
714 * IF is the Intermediate Frequency (the offset from the base
715 * signal where the video, color, audio and NICAM signals are.
717 * Eg, Picture at 38.9 Mhz, Colour at 34.47 MHz, sound at 32.9 MHz
718 * NICAM at 32.348 Mhz.
719 * Strangely enough, there is an IF (intermediate frequency) for
720 * FM Radio which is 10.7 Mhz.
722 * The tuner also works in Bands. Philips bands are
723 * FM radio band 87.50 to 108.00 MHz
724 * Low band 45.75 to 170.00 MHz
725 * Mid band 170.00 to 450.00 MHz
726 * High band 450.00 to 855.25 MHz
729 * Now we need to set the PLL on the tuner to the required freuqncy.
730 * It has a programmable divisor.
731 * For TV we want
732 * N = 16 (freq RF(pc) + freq IF(pc)) pc is picture carrier and RF and IF
733 * are in MHz.
735 * For RADIO we want a different equation.
736 * freq IF is 10.70 MHz (so the data sheet tells me)
737 * N = (freq RF + freq IF) / step size
738 * The step size must be set to 50 khz (so the data sheet tells me)
739 * (note this is 50 kHz, the other things are in MHz)
740 * so we end up with N = 20x(freq RF + 10.7)
744 #define LOW_BAND 0
745 #define MID_BAND 1
746 #define HIGH_BAND 2
747 #define FM_RADIO_BAND 3
750 /* Check if these are correct for other than Philips PAL */
751 #define STATUSBIT_COLD 0x80
752 #define STATUSBIT_LOCK 0x40
753 #define STATUSBIT_TV 0x20
754 #define STATUSBIT_STEREO 0x10 /* valid if FM (aka not TV) */
755 #define STATUSBIT_ADC 0x07
758 * set the frequency of the tuner
759 * If 'type' is TV_FREQUENCY, the frequency is freq MHz*16
760 * If 'type' is FM_RADIO_FREQUENCY, the frequency is freq MHz * 100
761 * (note *16 gives is 4 bits of fraction, eg steps of nnn.0625)
765 tv_freq( bktr_ptr_t bktr, int frequency, int type )
767 const struct TUNER* tuner;
768 u_char addr;
769 u_char control;
770 u_char band;
771 int N;
772 int band_select = 0;
773 #if defined( TEST_TUNER_AFC )
774 int oldFrequency, afcDelta;
775 #endif
777 tuner = bktr->card.tuner;
778 if ( tuner == NULL )
779 return( -1 );
781 if (tuner == &tuners[TUNER_MT2032]) {
782 mt2032_set_tv_freq(bktr, frequency);
783 return 0;
785 if (type == TV_FREQUENCY) {
787 * select the band based on frequency
788 * XXX FIXME: get the cross-over points from the tuner struct
790 if ( frequency < (160 * FREQFACTOR ) )
791 band_select = LOW_BAND;
792 else if ( frequency < (454 * FREQFACTOR ) )
793 band_select = MID_BAND;
794 else
795 band_select = HIGH_BAND;
797 #if defined( TEST_TUNER_AFC )
798 if ( bktr->tuner.afc )
799 frequency -= 4;
800 #endif
802 * N = 16 * { fRF(pc) + fIF(pc) }
803 * or N = 16* fRF(pc) + 16*fIF(pc) }
804 * where:
805 * pc is picture carrier, fRF & fIF are in MHz
807 * fortunatly, frequency is passed in as MHz * 16
808 * and the TBL_IF frequency is also stored in MHz * 16
810 N = frequency + TBL_IF;
812 /* set the address of the PLL */
813 addr = bktr->card.tuner_pllAddr;
814 control = tuner->pllControl[ band_select ];
815 band = tuner->bandAddrs[ band_select ];
817 if(!(band && control)) /* Don't try to set un- */
818 return(-1); /* supported modes. */
820 if ( frequency > bktr->tuner.frequency ) {
821 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
822 i2cWrite( bktr, addr, control, band );
824 else {
825 i2cWrite( bktr, addr, control, band );
826 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
829 #if defined( TUNER_AFC )
830 if ( bktr->tuner.afc == TRUE ) {
831 #if defined( TEST_TUNER_AFC )
832 oldFrequency = frequency;
833 #endif
834 if ( (N = do_afc( bktr, addr, N )) < 0 ) {
835 /* AFC failed, restore requested frequency */
836 N = frequency + TBL_IF;
837 #if defined( TEST_TUNER_AFC )
838 kprintf("%s: do_afc: failed to lock\n",
839 bktr_name(bktr));
840 #endif
841 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
843 else
844 frequency = N - TBL_IF;
845 #if defined( TEST_TUNER_AFC )
846 kprintf("%s: do_afc: returned freq %d (%d %% %d)\n", bktr_name(bktr), frequency, frequency / 16, frequency % 16);
847 afcDelta = frequency - oldFrequency;
848 kprintf("%s: changed by: %d clicks (%d mod %d)\n", bktr_name(bktr), afcDelta, afcDelta / 16, afcDelta % 16);
849 #endif
851 #endif /* TUNER_AFC */
853 bktr->tuner.frequency = frequency;
856 if ( type == FM_RADIO_FREQUENCY ) {
857 band_select = FM_RADIO_BAND;
860 * N = { fRF(pc) + fIF(pc) }/step_size
861 * The step size is 50kHz for FM radio.
862 * (eg after 102.35MHz comes 102.40 MHz)
863 * fIF is 10.7 MHz (as detailed in the specs)
865 * frequency is passed in as MHz * 100
867 * So, we have N = (frequency/100 + 10.70) /(50/1000)
869 N = (frequency + 1070)/5;
871 /* set the address of the PLL */
872 addr = bktr->card.tuner_pllAddr;
873 control = tuner->pllControl[ band_select ];
874 band = tuner->bandAddrs[ band_select ];
876 if(!(band && control)) /* Don't try to set un- */
877 return(-1); /* supported modes. */
879 band |= bktr->tuner.radio_mode; /* tuner.radio_mode is set in
880 * the ioctls RADIO_SETMODE
881 * and RADIO_GETMODE */
883 i2cWrite( bktr, addr, control, band );
884 i2cWrite( bktr, addr, (N>>8) & 0x7f, N & 0xff );
886 bktr->tuner.frequency = (N * 5) - 1070;
892 return( 0 );
897 #if defined( TUNER_AFC )
902 do_afc( bktr_ptr_t bktr, int addr, int frequency )
904 int step;
905 int status;
906 int origFrequency;
908 origFrequency = frequency;
910 /* wait for first setting to take effect */
911 tsleep( BKTR_SLEEP, 0, "tuning", hz/8 );
913 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
914 return( -1 );
916 #if defined( TEST_TUNER_AFC )
917 kprintf( "%s: Original freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
918 #endif
919 for ( step = 0; step < AFC_MAX_STEP; ++step ) {
920 if ( (status = i2cRead( bktr, addr + 1 )) < 0 )
921 goto fubar;
922 if ( !(status & 0x40) ) {
923 #if defined( TEST_TUNER_AFC )
924 kprintf( "%s: no lock!\n", bktr_name(bktr) );
925 #endif
926 goto fubar;
929 switch( status & AFC_BITS ) {
930 case AFC_FREQ_CENTERED:
931 #if defined( TEST_TUNER_AFC )
932 kprintf( "%s: Centered, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
933 #endif
934 return( frequency );
936 case AFC_FREQ_MINUS_125:
937 case AFC_FREQ_MINUS_62:
938 #if defined( TEST_TUNER_AFC )
939 kprintf( "%s: Low, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
940 #endif
941 --frequency;
942 break;
944 case AFC_FREQ_PLUS_62:
945 case AFC_FREQ_PLUS_125:
946 #if defined( TEST_TUNER_AFC )
947 kprintf( "%s: Hi, freq: %d, status: 0x%02x\n", bktr_name(bktr), frequency, status );
948 #endif
949 ++frequency;
950 break;
953 i2cWrite( bktr, addr,
954 (frequency>>8) & 0x7f, frequency & 0xff );
955 DELAY( AFC_DELAY );
958 fubar:
959 i2cWrite( bktr, addr,
960 (origFrequency>>8) & 0x7f, origFrequency & 0xff );
962 return( -1 );
964 #endif /* TUNER_AFC */
965 #undef TBL_IF
969 * Get the Tuner status and signal strength
971 int get_tuner_status( bktr_ptr_t bktr ) {
972 if (bktr->card.tuner == &tuners[TUNER_MT2032])
973 return 0;
974 return i2cRead( bktr, bktr->card.tuner_pllAddr + 1 );
978 * set the channel of the tuner
981 tv_channel( bktr_ptr_t bktr, int channel )
983 int frequency;
985 /* calculate the frequency according to tuner type */
986 if ( (frequency = frequency_lookup( bktr, channel )) < 0 )
987 return( -1 );
989 /* set the new frequency */
990 if ( tv_freq( bktr, frequency, TV_FREQUENCY ) < 0 )
991 return( -1 );
993 /* OK to update records */
994 return( (bktr->tuner.channel = channel) );
998 * get channelset name
1001 tuner_getchnlset(struct bktr_chnlset *chnlset)
1003 if (( chnlset->index < CHNLSET_MIN ) ||
1004 ( chnlset->index > CHNLSET_MAX ))
1005 return( EINVAL );
1007 memcpy(&chnlset->name, &freqTable[chnlset->index].name,
1008 BT848_MAX_CHNLSET_NAME_LEN);
1010 chnlset->max_channel=freqTable[chnlset->index].ptr[0];
1011 return( 0 );
1017 #define TDA9887_ADDR 0x86
1019 static int
1020 TDA9887_init(bktr_ptr_t bktr, int output2_enable)
1022 u_char addr = TDA9887_ADDR;
1024 i2cWrite(bktr, addr, 0, output2_enable ? 0x50 : 0xd0);
1025 i2cWrite(bktr, addr, 1, 0x6e); /* takeover point / de-emphasis */
1027 /* PAL BG: 0x09 PAL I: 0x0a NTSC: 0x04 */
1028 #ifdef MT2032_NTSC
1029 i2cWrite(bktr, addr, 2, 0x04);
1030 #else
1031 i2cWrite(bktr, addr, 2, 0x09);
1032 #endif
1033 return 0;
1038 #define MT2032_OPTIMIZE_VCO 1
1040 /* holds the value of XOGC register after init */
1041 static int MT2032_XOGC = 4;
1043 /* card.tuner_pllAddr not set during init */
1044 #define MT2032_ADDR 0xc0
1046 #ifndef MT2032_ADDR
1047 #define MT2032_ADDR (bktr->card.tuner_pllAddr)
1048 #endif
1050 static int
1051 _MT2032_GetRegister(bktr_ptr_t bktr, u_char regNum)
1053 int ch;
1055 if (i2cWrite(bktr, MT2032_ADDR, regNum, -1) == -1) {
1056 if (bootverbose)
1057 kprintf("%s: MT2032 write failed (i2c addr %#x)\n",
1058 bktr_name(bktr), MT2032_ADDR);
1059 return -1;
1061 if ((ch = i2cRead(bktr, MT2032_ADDR + 1)) == -1) {
1062 if (bootverbose)
1063 kprintf("%s: MT2032 get register %d failed\n",
1064 bktr_name(bktr), regNum);
1065 return -1;
1067 return ch;
1070 static void
1071 _MT2032_SetRegister(bktr_ptr_t bktr, u_char regNum, u_char data)
1073 i2cWrite(bktr, MT2032_ADDR, regNum, data);
1076 #define MT2032_GetRegister(r) _MT2032_GetRegister(bktr,r)
1077 #define MT2032_SetRegister(r,d) _MT2032_SetRegister(bktr,r,d)
1080 int
1081 mt2032_init(bktr_ptr_t bktr)
1083 u_char rdbuf[22];
1084 int xogc, xok = 0;
1085 int i;
1086 int x;
1088 TDA9887_init(bktr, 0);
1090 for (i = 0; i < 21; i++) {
1091 if ((x = MT2032_GetRegister(i)) == -1)
1092 break;
1093 rdbuf[i] = x;
1095 if (i < 21)
1096 return -1;
1098 kprintf("%s: MT2032: Companycode=%02x%02x Part=%02x Revision=%02x\n",
1099 bktr_name(bktr),
1100 rdbuf[0x11], rdbuf[0x12], rdbuf[0x13], rdbuf[0x14]);
1101 if (rdbuf[0x13] != 4) {
1102 kprintf("%s: MT2032 not found or unknown type\n", bktr_name(bktr));
1103 return -1;
1106 /* Initialize Registers per spec. */
1107 MT2032_SetRegister(2, 0xff);
1108 MT2032_SetRegister(3, 0x0f);
1109 MT2032_SetRegister(4, 0x1f);
1110 MT2032_SetRegister(6, 0xe4);
1111 MT2032_SetRegister(7, 0x8f);
1112 MT2032_SetRegister(8, 0xc3);
1113 MT2032_SetRegister(9, 0x4e);
1114 MT2032_SetRegister(10, 0xec);
1115 MT2032_SetRegister(13, 0x32);
1117 /* Adjust XOGC (register 7), wait for XOK */
1118 xogc = 7;
1119 do {
1120 DELAY(10000);
1121 xok = MT2032_GetRegister(0x0e) & 0x01;
1122 if (xok == 1) {
1123 break;
1125 xogc--;
1126 if (xogc == 3) {
1127 xogc = 4; /* min. 4 per spec */
1128 break;
1130 MT2032_SetRegister(7, 0x88 + xogc);
1131 } while (xok != 1);
1133 TDA9887_init(bktr, 1);
1135 MT2032_XOGC = xogc;
1137 return 0;
1140 static int
1141 MT2032_SpurCheck(int f1, int f2, int spectrum_from, int spectrum_to)
1143 int n1 = 1, n2, f;
1145 f1 = f1 / 1000; /* scale to kHz to avoid 32bit overflows */
1146 f2 = f2 / 1000;
1147 spectrum_from /= 1000;
1148 spectrum_to /= 1000;
1150 do {
1151 n2 = -n1;
1152 f = n1 * (f1 - f2);
1153 do {
1154 n2--;
1155 f = f - f2;
1156 if ((f > spectrum_from) && (f < spectrum_to)) {
1157 return 1;
1159 } while ((f > (f2 - spectrum_to)) || (n2 > -5));
1160 n1++;
1161 } while (n1 < 5);
1163 return 0;
1166 static int
1167 MT2032_ComputeFreq(
1168 int rfin,
1169 int if1,
1170 int if2,
1171 int spectrum_from,
1172 int spectrum_to,
1173 unsigned char *buf,
1174 int *ret_sel,
1175 int xogc
1177 { /* all in Hz */
1178 int fref, lo1, lo1n, lo1a, s, sel;
1179 int lo1freq, desired_lo1, desired_lo2, lo2, lo2n, lo2a,
1180 lo2num, lo2freq;
1181 int nLO1adjust;
1183 fref = 5250 * 1000; /* 5.25MHz */
1185 /* per spec 2.3.1 */
1186 desired_lo1 = rfin + if1;
1187 lo1 = (2 * (desired_lo1 / 1000) + (fref / 1000)) / (2 * fref / 1000);
1188 lo1freq = lo1 * fref;
1189 desired_lo2 = lo1freq - rfin - if2;
1191 /* per spec 2.3.2 */
1192 for (nLO1adjust = 1; nLO1adjust < 3; nLO1adjust++) {
1193 if (!MT2032_SpurCheck(lo1freq, desired_lo2, spectrum_from, spectrum_to)) {
1194 break;
1196 if (lo1freq < desired_lo1) {
1197 lo1 += nLO1adjust;
1198 } else {
1199 lo1 -= nLO1adjust;
1202 lo1freq = lo1 * fref;
1203 desired_lo2 = lo1freq - rfin - if2;
1206 /* per spec 2.3.3 */
1207 s = lo1freq / 1000 / 1000;
1209 if (MT2032_OPTIMIZE_VCO) {
1210 if (s > 1890) {
1211 sel = 0;
1212 } else if (s > 1720) {
1213 sel = 1;
1214 } else if (s > 1530) {
1215 sel = 2;
1216 } else if (s > 1370) {
1217 sel = 3;
1218 } else {
1219 sel = 4;/* >1090 */
1221 } else {
1222 if (s > 1790) {
1223 sel = 0;/* <1958 */
1224 } else if (s > 1617) {
1225 sel = 1;
1226 } else if (s > 1449) {
1227 sel = 2;
1228 } else if (s > 1291) {
1229 sel = 3;
1230 } else {
1231 sel = 4;/* >1090 */
1235 *ret_sel = sel;
1237 /* per spec 2.3.4 */
1238 lo1n = lo1 / 8;
1239 lo1a = lo1 - (lo1n * 8);
1240 lo2 = desired_lo2 / fref;
1241 lo2n = lo2 / 8;
1242 lo2a = lo2 - (lo2n * 8);
1243 /* scale to fit in 32bit arith */
1244 lo2num = ((desired_lo2 / 1000) % (fref / 1000)) * 3780 / (fref / 1000);
1245 lo2freq = (lo2a + 8 * lo2n) * fref + lo2num * (fref / 1000) / 3780 * 1000;
1247 if (lo1a < 0 || lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a < 0 ||
1248 lo2a > 7 || lo2n < 17 || lo2n > 30) {
1249 kprintf("MT2032: parameter out of range\n");
1250 return -1;
1252 /* set up MT2032 register map for transfer over i2c */
1253 buf[0] = lo1n - 1;
1254 buf[1] = lo1a | (sel << 4);
1255 buf[2] = 0x86; /* LOGC */
1256 buf[3] = 0x0f; /* reserved */
1257 buf[4] = 0x1f;
1258 buf[5] = (lo2n - 1) | (lo2a << 5);
1259 if (rfin < 400 * 1000 * 1000) {
1260 buf[6] = 0xe4;
1261 } else {
1262 buf[6] = 0xf4; /* set PKEN per rev 1.2 */
1265 buf[7] = 8 + xogc;
1266 buf[8] = 0xc3; /* reserved */
1267 buf[9] = 0x4e; /* reserved */
1268 buf[10] = 0xec; /* reserved */
1269 buf[11] = (lo2num & 0xff);
1270 buf[12] = (lo2num >> 8) | 0x80; /* Lo2RST */
1272 return 0;
1275 static int
1276 MT2032_CheckLOLock(bktr_ptr_t bktr)
1278 int t, lock = 0;
1279 for (t = 0; t < 10; t++) {
1280 lock = MT2032_GetRegister(0x0e) & 0x06;
1281 if (lock == 6) {
1282 break;
1284 DELAY(1000);
1286 return lock;
1289 static int
1290 MT2032_OptimizeVCO(bktr_ptr_t bktr, int sel, int lock)
1292 int tad1, lo1a;
1294 tad1 = MT2032_GetRegister(0x0f) & 0x07;
1296 if (tad1 == 0) {
1297 return lock;
1299 if (tad1 == 1) {
1300 return lock;
1302 if (tad1 == 2) {
1303 if (sel == 0) {
1304 return lock;
1305 } else {
1306 sel--;
1308 } else {
1309 if (sel < 4) {
1310 sel++;
1311 } else {
1312 return lock;
1315 lo1a = MT2032_GetRegister(0x01) & 0x07;
1316 MT2032_SetRegister(0x01, lo1a | (sel << 4));
1317 lock = MT2032_CheckLOLock(bktr);
1318 return lock;
1321 static int
1322 MT2032_SetIFFreq(bktr_ptr_t bktr, int rfin, int if1, int if2, int from, int to)
1324 u_char buf[21];
1325 int lint_try, sel, lock = 0;
1327 if (MT2032_ComputeFreq(rfin, if1, if2, from, to, &buf[0], &sel, MT2032_XOGC) == -1)
1328 return -1;
1330 TDA9887_init(bktr, 0);
1332 /* send only the relevant registers per Rev. 1.2 */
1333 MT2032_SetRegister(0, buf[0x00]);
1334 MT2032_SetRegister(1, buf[0x01]);
1335 MT2032_SetRegister(2, buf[0x02]);
1337 MT2032_SetRegister(5, buf[0x05]);
1338 MT2032_SetRegister(6, buf[0x06]);
1339 MT2032_SetRegister(7, buf[0x07]);
1341 MT2032_SetRegister(11, buf[0x0B]);
1342 MT2032_SetRegister(12, buf[0x0C]);
1344 /* wait for PLLs to lock (per manual), retry LINT if not. */
1345 for (lint_try = 0; lint_try < 2; lint_try++) {
1346 lock = MT2032_CheckLOLock(bktr);
1348 if (MT2032_OPTIMIZE_VCO) {
1349 lock = MT2032_OptimizeVCO(bktr, sel, lock);
1351 if (lock == 6) {
1352 break;
1354 /* set LINT to re-init PLLs */
1355 MT2032_SetRegister(7, 0x80 + 8 + MT2032_XOGC);
1356 DELAY(10000);
1357 MT2032_SetRegister(7, 8 + MT2032_XOGC);
1359 if (lock != 6)
1360 kprintf("%s: PLL didn't lock\n", bktr_name(bktr));
1362 MT2032_SetRegister(2, 0x20);
1364 TDA9887_init(bktr, 1);
1365 return 0;
1368 static void
1369 mt2032_set_tv_freq(bktr_ptr_t bktr, unsigned int freq)
1371 int if2,from,to;
1372 int stat, tad;
1374 #ifdef MT2032_NTSC
1375 from=40750*1000;
1376 to=46750*1000;
1377 if2=45750*1000;
1378 #else
1379 from=32900*1000;
1380 to=39900*1000;
1381 if2=38900*1000;
1382 #endif
1384 if (MT2032_SetIFFreq(bktr, freq*62500 /* freq*1000*1000/16 */,
1385 1090*1000*1000, if2, from, to) == 0) {
1386 bktr->tuner.frequency = freq;
1387 stat = MT2032_GetRegister(0x0e);
1388 tad = MT2032_GetRegister(0x0f);
1389 if (bootverbose)
1390 kprintf("%s: frequency set to %d, st = %#x, tad = %#x\n",
1391 bktr_name(bktr), freq*62500, stat, tad);