2 * Copyright (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * programming the msp34* sound processor family
32 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
34 * what works and what doesn't:
37 * Support for Hauppauge cards added (decoding handled by tuner) added by
38 * Frederic Crozat <fcrozat@mail.dotcom.fr>
41 * should work. The stereo modes are backward compatible to FM-mono,
42 * therefore FM-Mono should be allways available.
44 * FM-Stereo (B/G, used in germany)
45 * should work, with autodetect
47 * FM-Stereo (satellite)
48 * should work, no autodetect (i.e. default is mono, but you can
49 * switch to stereo -- untested)
51 * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
52 * should work, with autodetect. Support for NICAM was added by
53 * Pekka Pietikainen <pp@netppl.fi>
57 * - better SAT support
60 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
61 * using soundcore instead of OSS
64 * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
65 * are in the public domain. Please contact me (Alex) and not Gerd for
66 * any problems you encounter under FreeBSD.
69 * - mutex handling (currently not mp-safe)
70 * - the various options here as loader tunables or compile time or whatever
71 * - how does the new dolby flag work with the current dpl_* stuff?
72 * Maybe it's just enough to set the dolby flag to 1 and it works.
73 * As I don't have a dolby card myself, I can't test it, though.
76 #include "opt_bktr.h" /* Include any kernel config options */
77 #ifdef BKTR_NEW_MSP34XX_DRIVER /* file only needed for new driver */
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
84 #include <sys/unistd.h>
85 #include <sys/kthread.h>
86 #include <sys/malloc.h>
88 #ifdef BKTR_USE_FREEBSD_SMBUS
89 #include <sys/bus.h> /* required by bktr_reg.h */
92 #include <machine/bus.h> /* required by bktr_reg.h */
94 #include <dev/bktr/ioctl_meteor.h>
95 #include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
96 #include <dev/bktr/bktr_reg.h>
97 #include <dev/bktr/bktr_tuner.h>
98 #include <dev/bktr/bktr_audio.h>
99 #include <dev/bktr/bktr_core.h>
101 #define VIDEO_MODE_PAL 0
102 #define VIDEO_MODE_NTSC 1
103 #define VIDEO_MODE_SECAM 2
104 #define VIDEO_MODE_AUTO 3
106 #define VIDEO_SOUND_MONO 1
107 #define VIDEO_SOUND_STEREO 2
108 #define VIDEO_SOUND_LANG1 4
109 #define VIDEO_SOUND_LANG2 8
111 #define DFP_COUNT 0x41
112 static const int bl_dfp[] = {
113 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
114 0x0b, 0x0d, 0x0e, 0x10
125 int main, second; /* sound carrier */
129 int left, right; /* volume */
132 /* shadow register set */
133 int dfp_regs[DFP_COUNT];
136 struct proc *kthread;
139 int active,restart,rmmod;
145 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
147 /* ---------------------------------------------------------------------- */
149 #define dprintk(...) do { \
151 printf("%s: ", bktr_name(client)); \
152 printf(__VA_ARGS__); \
156 /* ---------------------------------------------------------------------- */
158 #define I2C_MSP3400C 0x80
159 #define I2C_MSP3400C_DEM 0x10
160 #define I2C_MSP3400C_DFP 0x12
162 /* ----------------------------------------------------------------------- */
163 /* functions for talking to the MSP3400C Sound processor */
165 static int msp3400c_reset(bktr_ptr_t client)
167 /* use our own which handles config(8) options */
168 msp_dpl_reset(client, client->msp_addr);
174 msp3400c_read(bktr_ptr_t client, int dev, int addr)
177 return(msp_dpl_read(client, client->msp_addr, dev, addr));
181 msp3400c_write(bktr_ptr_t client, int dev, int addr, int val)
184 msp_dpl_write(client, client->msp_addr, dev, addr, val);
189 /* ------------------------------------------------------------------------ */
191 /* This macro is allowed for *constants* only, gcc must calculate it
192 at compile time. Remember -- no floats in kernel mode */
193 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
195 #define MSP_MODE_AM_DETECT 0
196 #define MSP_MODE_FM_RADIO 2
197 #define MSP_MODE_FM_TERRA 3
198 #define MSP_MODE_FM_SAT 4
199 #define MSP_MODE_FM_NICAM1 5
200 #define MSP_MODE_FM_NICAM2 6
201 #define MSP_MODE_AM_NICAM 7
202 #define MSP_MODE_BTSC 8
203 #define MSP_MODE_EXTERN 9
205 static struct MSP_INIT_DATA_DEM {
214 } msp_init_data[] = {
215 /* AM (for carrier detect / msp3400) */
216 { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
217 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
218 0x00d0, 0x0500, 0x0020, 0x3000},
220 /* AM (for carrier detect / msp3410) */
221 { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
222 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
223 0x00d0, 0x0100, 0x0020, 0x3000},
226 { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
227 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
228 0x00d0, 0x0480, 0x0020, 0x3000 },
230 /* Terrestial FM-mono + FM-stereo */
231 { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
232 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
233 0x00d0, 0x0480, 0x0030, 0x3000},
236 { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
237 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
238 0x00c6, 0x0480, 0x0000, 0x3000},
240 /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
241 { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
242 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
243 0x00d0, 0x0040, 0x0120, 0x3000},
245 /* NICAM/FM -- I (6.0/6.552) */
246 { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
247 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
248 0x00d0, 0x0040, 0x0120, 0x3000},
250 /* NICAM/AM -- L (6.5/5.85) */
251 { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
252 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
253 0x00c6, 0x0140, 0x0120, 0x7c03},
256 struct CARRIER_DETECT {
261 static struct CARRIER_DETECT carrier_detect_main[] = {
263 { MSP_CARRIER(4.5), "4.5 NTSC" },
264 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
265 { MSP_CARRIER(6.0), "6.0 PAL I" },
266 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
269 static struct CARRIER_DETECT carrier_detect_55[] = {
271 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
272 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
275 static struct CARRIER_DETECT carrier_detect_65[] = {
276 /* PAL SAT / SECAM */
277 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
278 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
279 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
280 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
281 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
282 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
285 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
287 /* ----------------------------------------------------------------------- */
292 #define SCART_IN1_DA 3
293 #define SCART_IN2_DA 4
299 static int scarts[3][9] = {
300 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
301 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
302 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
303 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
306 static char *scart_names[] = {
307 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
311 msp3400c_set_scart(bktr_ptr_t client, int in, int out)
313 struct msp3400c *msp = client->msp3400c_info;
315 if (-1 == scarts[out][in])
318 dprintk("msp34xx: scart switch: %s => %d\n",scart_names[in],out);
319 msp->acb &= ~scarts[out][SCART_MASK];
320 msp->acb |= scarts[out][in];
321 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
324 /* ------------------------------------------------------------------------ */
326 static void msp3400c_setcarrier(bktr_ptr_t client, int cdo1, int cdo2)
328 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
329 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
330 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
331 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
332 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
335 static void msp3400c_setvolume(bktr_ptr_t client,
336 int muted, int left, int right)
338 int vol = 0,val = 0,balance = 0;
341 vol = (left > right) ? left : right;
342 val = (vol * 0x73 / 65535) << 8;
345 balance = ((right-left) * 127) / vol;
348 dprintk("msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
349 muted ? "on" : "off", left, right, val>>8, balance);
350 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
351 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
352 /* scart - on/off only */
353 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
354 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
357 static void msp3400c_setbass(bktr_ptr_t client, int bass)
359 int val = ((bass-32768) * 0x60 / 65535) << 8;
361 dprintk("msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
362 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
365 static void msp3400c_settreble(bktr_ptr_t client, int treble)
367 int val = ((treble-32768) * 0x60 / 65535) << 8;
369 dprintk("msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
370 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
373 static void msp3400c_setmode(bktr_ptr_t client, int type)
375 struct msp3400c *msp = client->msp3400c_info;
378 dprintk("msp3400: setmode: %d\n",type);
380 msp->stereo = VIDEO_SOUND_MONO;
382 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
383 msp_init_data[type].ad_cv);
385 for (i = 5; i >= 0; i--) /* fir 1 */
386 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
387 msp_init_data[type].fir1[i]);
389 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
390 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
391 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
392 for (i = 5; i >= 0; i--)
393 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
394 msp_init_data[type].fir2[i]);
396 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
397 msp_init_data[type].mode_reg);
399 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
400 msp_init_data[type].cdo2);
402 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
405 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
407 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
409 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
410 msp_init_data[type].dfp_src);
412 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
413 msp_init_data[type].dfp_src);
414 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
415 msp_init_data[type].dfp_src);
416 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
417 msp_init_data[type].dfp_src);
419 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
420 msp_init_data[type].dfp_src);
421 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
422 msp_init_data[type].dfp_matrix);
426 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
430 /* turn on/off nicam + stereo */
431 static void msp3400c_setstereo(bktr_ptr_t client, int mode)
433 static char *strmode[] = { "0", "mono", "stereo", "3",
434 "lang1", "5", "6", "7", "lang2" };
435 struct msp3400c *msp = client->msp3400c_info;
436 int nicam=0; /* channel source: FM/AM or nicam */
439 /* switch demodulator */
441 case MSP_MODE_FM_TERRA:
442 dprintk("msp3400: FM setstereo: %s\n",strmode[mode]);
443 msp3400c_setcarrier(client,msp->second,msp->main);
445 case VIDEO_SOUND_STEREO:
446 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
448 case VIDEO_SOUND_MONO:
449 case VIDEO_SOUND_LANG1:
450 case VIDEO_SOUND_LANG2:
451 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
455 case MSP_MODE_FM_SAT:
456 dprintk("msp3400: SAT setstereo: %s\n",strmode[mode]);
458 case VIDEO_SOUND_MONO:
459 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
461 case VIDEO_SOUND_STEREO:
462 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
464 case VIDEO_SOUND_LANG1:
465 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
467 case VIDEO_SOUND_LANG2:
468 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
472 case MSP_MODE_FM_NICAM1:
473 case MSP_MODE_FM_NICAM2:
474 case MSP_MODE_AM_NICAM:
475 dprintk("msp3400: NICAM setstereo: %s\n",strmode[mode]);
476 msp3400c_setcarrier(client,msp->second,msp->main);
481 dprintk("msp3400: BTSC setstereo: %s\n",strmode[mode]);
484 case MSP_MODE_EXTERN:
485 dprintk("msp3400: extern setstereo: %s\n",strmode[mode]);
488 case MSP_MODE_FM_RADIO:
489 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
492 dprintk("msp3400: mono setstereo\n");
498 case VIDEO_SOUND_STEREO:
499 src = 0x0020 | nicam;
502 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
505 case VIDEO_SOUND_MONO:
506 if (msp->mode == MSP_MODE_AM_NICAM) {
507 dprintk("msp3400: switching to AM mono\n");
508 /* AM mono decoding is handled by tuner, not MSP chip */
509 /* SCART switching control register */
510 msp3400c_set_scart(client,SCART_MONO,0);
514 case VIDEO_SOUND_LANG1:
515 src = 0x0000 | nicam;
517 case VIDEO_SOUND_LANG2:
518 src = 0x0010 | nicam;
521 dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src);
524 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
525 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
526 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
527 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
529 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
530 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
531 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
532 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
537 msp3400c_print_mode(struct msp3400c *msp)
539 if (msp->main == msp->second) {
540 printf("bktr: msp3400: mono sound carrier: %d.%03d MHz\n",
541 msp->main/910000,(msp->main/910)%1000);
543 printf("bktr: msp3400: main sound carrier: %d.%03d MHz\n",
544 msp->main/910000,(msp->main/910)%1000);
546 if (msp->mode == MSP_MODE_FM_NICAM1 ||
547 msp->mode == MSP_MODE_FM_NICAM2)
548 printf("bktr: msp3400: NICAM/FM carrier : %d.%03d MHz\n",
549 msp->second/910000,(msp->second/910)%1000);
550 if (msp->mode == MSP_MODE_AM_NICAM)
551 printf("bktr: msp3400: NICAM/AM carrier : %d.%03d MHz\n",
552 msp->second/910000,(msp->second/910)%1000);
553 if (msp->mode == MSP_MODE_FM_TERRA &&
554 msp->main != msp->second) {
555 printf("bktr: msp3400: FM-stereo carrier : %d.%03d MHz\n",
556 msp->second/910000,(msp->second/910)%1000);
561 msp3400c_restore_dfp(bktr_ptr_t client)
563 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
566 for (i = 0; i < DFP_COUNT; i++) {
567 if (-1 == msp->dfp_regs[i])
569 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
573 /* ----------------------------------------------------------------------- */
575 struct REGISTER_DUMP {
580 struct REGISTER_DUMP d1[] = {
581 { 0x007e, "autodetect" },
582 { 0x0023, "C_AD_BITS " },
583 { 0x0038, "ADD_BITS " },
584 { 0x003e, "CIB_BITS " },
585 { 0x0057, "ERROR_RATE" },
589 autodetect_stereo(bktr_ptr_t client)
591 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
593 int newstereo = msp->stereo;
594 int newnicam = msp->nicam_on;
598 case MSP_MODE_FM_TERRA:
599 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
602 dprintk("msp34xx: stereo detect register: %d\n",val);
605 newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
606 } else if (val < -4096) {
607 newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
609 newstereo = VIDEO_SOUND_MONO;
613 case MSP_MODE_FM_NICAM1:
614 case MSP_MODE_FM_NICAM2:
615 case MSP_MODE_AM_NICAM:
616 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
617 dprintk("msp34xx: nicam sync=%d, mode=%d\n",val & 1, (val & 0x1e) >> 1);
621 switch ((val & 0x1e) >> 1) {
624 newstereo = VIDEO_SOUND_STEREO;
628 newstereo = VIDEO_SOUND_MONO
633 newstereo = VIDEO_SOUND_MONO
638 newstereo = VIDEO_SOUND_MONO;
644 newstereo = VIDEO_SOUND_MONO;
648 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
649 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
651 (val & 0x0002) ? "no" : "yes",
652 (val & 0x0004) ? "no" : "yes",
653 (val & 0x0040) ? "stereo" : "mono",
654 (val & 0x0080) ? ", nicam 2nd mono" : "",
655 (val & 0x0100) ? ", bilingual/SAP" : "");
656 newstereo = VIDEO_SOUND_MONO;
657 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
658 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
661 if (newstereo != msp->stereo) {
663 dprintk("msp34xx: watch: stereo %d => %d\n",
664 msp->stereo,newstereo);
665 msp->stereo = newstereo;
667 if (newnicam != msp->nicam_on) {
669 dprintk("msp34xx: watch: nicam %d => %d\n",
670 msp->nicam_on,newnicam);
671 msp->nicam_on = newnicam;
677 * A kernel thread for msp3400 control -- we don't want to block the
678 * in the ioctl while doing the sound carrier & stereo detect
681 /* stereo/multilang monitoring */
682 static void watch_stereo(bktr_ptr_t client)
684 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
686 if (autodetect_stereo(client)) {
687 if (msp->stereo & VIDEO_SOUND_STEREO)
688 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
689 else if (msp->stereo & VIDEO_SOUND_LANG1)
690 msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
692 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
694 if (client->stereo_once)
695 msp->watch_stereo = 0;
699 static void msp3400c_thread(void *data)
701 bktr_ptr_t client = data;
702 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
704 struct CARRIER_DETECT *cd;
705 int count, max1,max2,val1,val2, val,this;
707 dprintk("msp3400: thread started\n");
713 tsleep(msp->kthread, PRIBIO, "idle", 0);
716 if (msp->halt_thread) {
717 msp->watch_stereo = 0;
718 msp->halt_thread = 0;
722 if (VIDEO_MODE_RADIO == msp->norm ||
723 MSP_MODE_EXTERN == msp->mode)
724 continue; /* nothing to do */
728 if (msp->watch_stereo) {
729 watch_stereo(client);
734 /* some time for the tuner to sync */
735 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
738 if (VIDEO_MODE_RADIO == msp->norm ||
739 MSP_MODE_EXTERN == msp->mode)
740 continue; /* nothing to do */
742 msp3400c_setvolume(client, msp->muted, 0, 0);
743 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
746 msp->watch_stereo = 0;
748 /* carrier detect pass #1 -- main carrier */
749 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
751 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
752 /* autodetect doesn't work well with AM ... */
755 dprintk("msp3400: AM sound override\n");
758 for (this = 0; this < count; this++) {
759 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
761 tsleep(msp->kthread, PRIBIO, "carrier detect", hz/100);
766 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
770 val1 = val, max1 = this;
771 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
774 /* carrier detect pass #2 -- second (stereo) carrier */
777 cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
780 cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
785 cd = NULL; count = 0;
789 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
790 /* autodetect doesn't work well with AM ... */
791 cd = NULL; count = 0; max2 = 0;
793 for (this = 0; this < count; this++) {
794 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
796 tsleep(msp->kthread, PRIBIO, "carrier detection", hz/100);
800 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
804 val2 = val, max2 = this;
805 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
808 /* programm the msp3400 according to the results */
809 msp->main = carrier_detect_main[max1].cdo;
814 msp->second = carrier_detect_55[max2].cdo;
815 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
817 /* XXX why mono? this probably can do stereo... - Alex*/
818 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
819 msp->watch_stereo = 1;
820 } else if (max2 == 1 && msp->nicam) {
822 msp->second = carrier_detect_55[max2].cdo;
823 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
825 msp3400c_setcarrier(client, msp->second, msp->main);
826 msp->watch_stereo = 1;
833 msp->second = MSP_CARRIER(6.552);
834 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
836 msp3400c_setcarrier(client, msp->second, msp->main);
837 msp->watch_stereo = 1;
840 if (max2 == 1 || max2 == 2) {
842 msp->second = carrier_detect_65[max2].cdo;
843 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
845 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
846 msp->watch_stereo = 1;
847 } else if (max2 == 0 &&
848 msp->norm == VIDEO_MODE_SECAM) {
849 /* L NICAM or AM-mono */
850 msp->second = carrier_detect_65[max2].cdo;
851 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
853 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
854 msp3400c_setcarrier(client, msp->second, msp->main);
855 /* volume prescale for SCART (AM mono input) */
856 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
857 msp->watch_stereo = 1;
858 } else if (max2 == 0 && msp->nicam) {
860 msp->second = carrier_detect_65[max2].cdo;
861 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
863 msp3400c_setcarrier(client, msp->second, msp->main);
864 msp->watch_stereo = 1;
872 msp->second = carrier_detect_main[max1].cdo;
873 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
875 msp3400c_setcarrier(client, msp->second, msp->main);
876 msp->stereo = VIDEO_SOUND_MONO;
877 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
881 if (msp->watch_stereo)
882 watch_stereo(client);
884 /* unmute + restore dfp registers */
885 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
886 msp3400c_restore_dfp(client);
889 msp3400c_print_mode(msp);
895 dprintk("msp3400: thread: exit\n");
899 wakeup(&msp->kthread);
905 /* ----------------------------------------------------------------------- */
906 /* this one uses the automatic sound standard detection of newer */
907 /* msp34xx chip versions */
909 static struct MODES {
914 { 0x0000, 0, 0, "ERROR" },
915 { 0x0001, 0, 0, "autodetect start" },
916 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
917 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
918 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
919 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
920 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
921 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
922 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
923 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
924 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
925 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
926 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
927 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
928 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
929 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
930 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
931 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
932 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
933 { -1, 0, 0, NULL }, /* EOF */
936 static void msp3410d_thread(void *data)
938 bktr_ptr_t client = data;
939 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
943 dprintk("msp3410: thread started\n");
949 if (!msp->watch_stereo)
953 tsleep(msp->kthread, PRIBIO, "idle", timo);
956 if (msp->halt_thread) {
957 msp->watch_stereo = 0;
958 msp->halt_thread = 0;
959 dprintk("msp3410: thread halted\n");
963 if (msp->mode == MSP_MODE_EXTERN)
968 if (msp->watch_stereo) {
969 watch_stereo(client);
974 /* some time for the tuner to sync */
975 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
978 if (msp->mode == MSP_MODE_EXTERN)
981 msp->watch_stereo = 0;
983 /* put into sane state (and mute) */
984 msp3400c_reset(client);
986 /* start autodetect */
992 case VIDEO_MODE_NTSC: /* BTSC */
996 case VIDEO_MODE_SECAM:
1000 case VIDEO_MODE_RADIO:
1009 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1010 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1014 for (i = 0; modelist[i].name != NULL; i++)
1015 if (modelist[i].retval == std)
1017 dprintk("msp3410: setting mode: %s (0x%04x)\n",
1018 modelist[i].name ? modelist[i].name : "unknown",std);
1022 /* programmed some specific mode */
1025 /* triggered autodetect */
1027 tsleep(msp->kthread, PRIBIO, "autodetection", hz/10);
1032 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1035 dprintk("msp3410: detection still in progress\n");
1038 for (i = 0; modelist[i].name != NULL; i++)
1039 if (modelist[i].retval == val)
1041 dprintk("msp3410: current mode: %s (0x%04x)\n",
1042 modelist[i].name ? modelist[i].name : "unknown",
1044 msp->main = modelist[i].main;
1045 msp->second = modelist[i].second;
1047 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1048 /* autodetection has failed, let backup */
1049 dprintk("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
1050 modelist[8].name ? modelist[8].name : "unknown",val);
1052 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1055 /* set various prescales */
1056 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1057 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1058 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1062 case 0x0008: /* B/G NICAM */
1063 case 0x000a: /* I NICAM */
1065 msp->mode = MSP_MODE_FM_NICAM1;
1067 msp->mode = MSP_MODE_FM_NICAM2;
1068 /* just turn on stereo */
1069 msp->stereo = VIDEO_SOUND_STEREO;
1071 msp->watch_stereo = 1;
1072 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1075 msp->mode = MSP_MODE_AM_NICAM;
1076 msp->stereo = VIDEO_SOUND_MONO;
1078 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1079 msp->watch_stereo = 1;
1081 case 0x0020: /* BTSC */
1082 /* just turn on stereo */
1083 msp->mode = MSP_MODE_BTSC;
1084 msp->stereo = VIDEO_SOUND_STEREO;
1086 msp->watch_stereo = 1;
1087 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1089 case 0x0040: /* FM radio */
1090 msp->mode = MSP_MODE_FM_RADIO;
1091 msp->stereo = VIDEO_SOUND_STEREO;
1093 msp->watch_stereo = 0;
1095 msp3400c_set_scart(client,SCART_IN2,0);
1096 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1097 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1098 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1101 msp->mode = MSP_MODE_FM_TERRA;
1102 msp->stereo = VIDEO_SOUND_STEREO;
1104 msp->watch_stereo = 1;
1105 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1109 if (msp->watch_stereo)
1110 watch_stereo(client);
1112 /* unmute + restore dfp registers */
1113 msp3400c_setbass(client, msp->bass);
1114 msp3400c_settreble(client, msp->treble);
1115 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1116 msp3400c_restore_dfp(client);
1122 dprintk("msp3410: thread: exit\n");
1125 msp->kthread = NULL;
1126 wakeup(&msp->kthread);
1132 int msp_attach(bktr_ptr_t bktr)
1134 struct msp3400c *msp;
1139 msp = (struct msp3400c *) malloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
1142 bktr->msp3400c_info = msp;
1144 memset(msp,0,sizeof(struct msp3400c));
1148 msp->treble = 32768;
1150 msp->threaddesc = malloc(15 * sizeof(char), M_DEVBUF, M_NOWAIT);
1151 if (msp->threaddesc == NULL) {
1152 free(msp, M_DEVBUF);
1155 snprintf(msp->threaddesc, 14, "%s_msp34xx_thread", bktr->bktr_xname);
1157 for (i = 0; i < DFP_COUNT; i++)
1158 msp->dfp_regs[i] = -1;
1160 msp3400c_reset(bktr);
1162 rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
1164 rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
1165 if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
1166 free(msp->threaddesc, M_DEVBUF);
1167 free(msp, M_DEVBUF);
1168 bktr->msp3400c_info = NULL;
1169 printf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
1174 /* this will turn on a 1kHz beep - might be useful for debugging... */
1175 msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1178 sprintf(buf,"MSP34%02d%c-%c%d",
1179 (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
1180 msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
1182 if (bktr->mspsimple == -1) {
1184 /* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
1185 msp->simple = ((rev1&0xff)+'@' > 'C');
1187 /* use kenv value */
1188 msp->simple = bktr->mspsimple;
1191 /* hello world :-) */
1193 printf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
1195 printf(", has NICAM support");
1199 /* startup control thread */
1200 err = kproc_create(msp->simple ? msp3410d_thread : msp3400c_thread,
1201 bktr, &msp->kthread, (RFFDG | RFPROC), 0,
1204 printf("%s: Error returned by kproc_create: %d", bktr_name(bktr), err);
1205 free(msp->threaddesc, M_DEVBUF);
1206 free(msp, M_DEVBUF);
1207 bktr->msp3400c_info = NULL;
1210 wakeup(msp->kthread);
1216 int msp_detach(bktr_ptr_t client)
1218 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1220 /* shutdown control thread */
1223 /* XXX mutex lock required */
1226 msp->watch_stereo = 0;
1227 wakeup(msp->kthread);
1229 while (msp->kthread)
1230 tsleep(&msp->kthread, PRIBIO, "wait for kthread", hz/10);
1234 if (client->msp3400c_info != NULL) {
1235 free(client->msp3400c_info, M_DEVBUF);
1236 client->msp3400c_info = NULL;
1239 msp3400c_reset(client);
1244 void msp_wake_thread(bktr_ptr_t client)
1246 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1248 msp3400c_setvolume(client,msp->muted,0,0);
1249 msp->watch_stereo=0;
1252 wakeup(msp->kthread);
1255 void msp_halt_thread(bktr_ptr_t client)
1257 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1259 msp3400c_setvolume(client,msp->muted,0,0);
1262 msp->halt_thread = 1;
1263 wakeup(msp->kthread);
1265 #endif /* BKTR_NEW_MSP34XX_DRIVER */