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
26 * $FreeBSD: src/sys/dev/bktr/msp34xx.c,v 1.5 2004/12/16 23:19:57 julian Exp
27 * $DragonFly: src/sys/dev/video/bktr/msp34xx.c,v 1.4 2006/09/05 00:55:44 dillon Exp $
31 * programming the msp34* sound processor family
33 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
35 * what works and what doesn't:
38 * Support for Hauppauge cards added (decoding handled by tuner) added by
39 * Frederic Crozat <fcrozat@mail.dotcom.fr>
42 * should work. The stereo modes are backward compatible to FM-mono,
43 * therefore FM-Mono should be allways available.
45 * FM-Stereo (B/G, used in germany)
46 * should work, with autodetect
48 * FM-Stereo (satellite)
49 * should work, no autodetect (i.e. default is mono, but you can
50 * switch to stereo -- untested)
52 * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
53 * should work, with autodetect. Support for NICAM was added by
54 * Pekka Pietikainen <pp@netppl.fi>
58 * - better SAT support
61 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
62 * using soundcore instead of OSS
65 * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
66 * are in the public domain. Please contact me (Alex) and not Gerd for
67 * any problems you encounter under FreeBSD.
70 * - mutex handling (currently not mp-safe)
71 * - the various options here as loader tunables or compile time or whatever
72 * - how does the new dolby flag work with the current dpl_* stuff?
73 * Maybe it's just enough to set the dolby flag to 1 and it works.
74 * As I don't have a dolby card myself, I can't test it, though.
77 #include "opt_bktr.h" /* Include any kernel config options */
78 #ifdef BKTR_NEW_MSP34XX_DRIVER /* file only needed for new driver */
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/vnode.h>
85 #include <sys/unistd.h>
86 #include <sys/kthread.h>
87 #include <sys/malloc.h>
89 #ifdef BKTR_USE_FREEBSD_SMBUS
90 #include <sys/bus.h> /* required by bktr_reg.h */
93 #include <machine/bus.h> /* required by bktr_reg.h */
95 #include <dev/video/meteor/ioctl_meteor.h>
96 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
97 #include <dev/video/bktr/bktr_reg.h>
98 #include <dev/video/bktr/bktr_tuner.h>
99 #include <dev/video/bktr/bktr_audio.h>
100 #include <dev/video/bktr/bktr_core.h>
102 #define VIDEO_MODE_PAL 0
103 #define VIDEO_MODE_NTSC 1
104 #define VIDEO_MODE_SECAM 2
105 #define VIDEO_MODE_AUTO 3
107 #define VIDEO_SOUND_MONO 1
108 #define VIDEO_SOUND_STEREO 2
109 #define VIDEO_SOUND_LANG1 4
110 #define VIDEO_SOUND_LANG2 8
112 #define DFP_COUNT 0x41
113 static const int bl_dfp[] = {
114 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
115 0x0b, 0x0d, 0x0e, 0x10
126 int main, second; /* sound carrier */
130 int left, right; /* volume */
133 /* shadow register set */
134 int dfp_regs[DFP_COUNT];
137 struct thread *kthread;
140 int active,restart,rmmod;
146 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
148 /* ---------------------------------------------------------------------- */
150 #define dprintk(args) do { \
152 printf("%s: ", bktr_name(client)); \
157 /* ---------------------------------------------------------------------- */
159 #define I2C_MSP3400C 0x80
160 #define I2C_MSP3400C_DEM 0x10
161 #define I2C_MSP3400C_DFP 0x12
163 /* ----------------------------------------------------------------------- */
164 /* functions for talking to the MSP3400C Sound processor */
166 static int msp3400c_reset(bktr_ptr_t client)
168 /* use our own which handles config(8) options */
169 msp_dpl_reset(client, client->msp_addr);
175 msp3400c_read(bktr_ptr_t client, int dev, int addr)
178 return(msp_dpl_read(client, client->msp_addr, dev, addr));
182 msp3400c_write(bktr_ptr_t client, int dev, int addr, int val)
185 msp_dpl_write(client, client->msp_addr, dev, addr, val);
190 /* ------------------------------------------------------------------------ */
192 /* This macro is allowed for *constants* only, gcc must calculate it
193 at compile time. Remember -- no floats in kernel mode */
194 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
196 #define MSP_MODE_AM_DETECT 0
197 #define MSP_MODE_FM_RADIO 2
198 #define MSP_MODE_FM_TERRA 3
199 #define MSP_MODE_FM_SAT 4
200 #define MSP_MODE_FM_NICAM1 5
201 #define MSP_MODE_FM_NICAM2 6
202 #define MSP_MODE_AM_NICAM 7
203 #define MSP_MODE_BTSC 8
204 #define MSP_MODE_EXTERN 9
206 static struct MSP_INIT_DATA_DEM {
215 } msp_init_data[] = {
216 /* AM (for carrier detect / msp3400) */
217 { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
218 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
219 0x00d0, 0x0500, 0x0020, 0x3000},
221 /* AM (for carrier detect / msp3410) */
222 { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
223 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
224 0x00d0, 0x0100, 0x0020, 0x3000},
227 { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
228 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
229 0x00d0, 0x0480, 0x0020, 0x3000 },
231 /* Terrestial FM-mono + FM-stereo */
232 { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
233 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
234 0x00d0, 0x0480, 0x0030, 0x3000},
237 { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
238 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
239 0x00c6, 0x0480, 0x0000, 0x3000},
241 /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
242 { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
243 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
244 0x00d0, 0x0040, 0x0120, 0x3000},
246 /* NICAM/FM -- I (6.0/6.552) */
247 { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
248 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
249 0x00d0, 0x0040, 0x0120, 0x3000},
251 /* NICAM/AM -- L (6.5/5.85) */
252 { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
253 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
254 0x00c6, 0x0140, 0x0120, 0x7c03},
257 struct CARRIER_DETECT {
262 static struct CARRIER_DETECT carrier_detect_main[] = {
264 { MSP_CARRIER(4.5), "4.5 NTSC" },
265 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
266 { MSP_CARRIER(6.0), "6.0 PAL I" },
267 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
270 static struct CARRIER_DETECT carrier_detect_55[] = {
272 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
273 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
276 static struct CARRIER_DETECT carrier_detect_65[] = {
277 /* PAL SAT / SECAM */
278 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
279 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
280 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
281 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
282 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
283 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
286 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
288 /* ----------------------------------------------------------------------- */
293 #define SCART_IN1_DA 3
294 #define SCART_IN2_DA 4
300 static int scarts[3][9] = {
301 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
302 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
303 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
304 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
307 static char *scart_names[] = {
308 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
312 msp3400c_set_scart(bktr_ptr_t client, int in, int out)
314 struct msp3400c *msp = client->msp3400c_info;
316 if (-1 == scarts[out][in])
319 dprintk(("msp34xx: scart switch: %s => %d\n", scart_names[in], out));
320 msp->acb &= ~scarts[out][SCART_MASK];
321 msp->acb |= scarts[out][in];
322 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
325 /* ------------------------------------------------------------------------ */
327 static void msp3400c_setcarrier(bktr_ptr_t client, int cdo1, int cdo2)
329 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
330 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
331 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
332 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
333 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
336 static void msp3400c_setvolume(bktr_ptr_t client,
337 int muted, int left, int right)
339 int vol = 0,val = 0,balance = 0;
342 vol = (left > right) ? left : right;
343 val = (vol * 0x73 / 65535) << 8;
346 balance = ((right-left) * 127) / vol;
349 dprintk(("msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
350 muted ? "on" : "off", left, right, val>>8, balance));
351 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
352 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
353 /* scart - on/off only */
354 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
355 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
358 static void msp3400c_setbass(bktr_ptr_t client, int bass)
360 int val = ((bass-32768) * 0x60 / 65535) << 8;
362 dprintk(("msp34xx: setbass: %d 0x%02x\n",bass, val>>8));
363 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
366 static void msp3400c_settreble(bktr_ptr_t client, int treble)
368 int val = ((treble-32768) * 0x60 / 65535) << 8;
370 dprintk(("msp34xx: settreble: %d 0x%02x\n",treble, val>>8));
371 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
374 static void msp3400c_setmode(bktr_ptr_t client, int type)
376 struct msp3400c *msp = client->msp3400c_info;
379 dprintk(("msp3400: setmode: %d\n",type));
381 msp->stereo = VIDEO_SOUND_MONO;
383 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
384 msp_init_data[type].ad_cv);
386 for (i = 5; i >= 0; i--) /* fir 1 */
387 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
388 msp_init_data[type].fir1[i]);
390 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
391 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
392 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
393 for (i = 5; i >= 0; i--)
394 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
395 msp_init_data[type].fir2[i]);
397 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
398 msp_init_data[type].mode_reg);
400 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
401 msp_init_data[type].cdo2);
403 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
406 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
408 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
410 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
411 msp_init_data[type].dfp_src);
413 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
414 msp_init_data[type].dfp_src);
415 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
416 msp_init_data[type].dfp_src);
417 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
418 msp_init_data[type].dfp_src);
420 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
421 msp_init_data[type].dfp_src);
422 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
423 msp_init_data[type].dfp_matrix);
427 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
431 /* turn on/off nicam + stereo */
432 static void msp3400c_setstereo(bktr_ptr_t client, int mode)
434 static char *strmode[] = { "0", "mono", "stereo", "3",
435 "lang1", "5", "6", "7", "lang2" };
436 struct msp3400c *msp = client->msp3400c_info;
437 int nicam=0; /* channel source: FM/AM or nicam */
440 /* switch demodulator */
442 case MSP_MODE_FM_TERRA:
443 dprintk(("msp3400: FM setstereo: %s\n",strmode[mode]));
444 msp3400c_setcarrier(client,msp->second,msp->main);
446 case VIDEO_SOUND_STEREO:
447 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
449 case VIDEO_SOUND_MONO:
450 case VIDEO_SOUND_LANG1:
451 case VIDEO_SOUND_LANG2:
452 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
456 case MSP_MODE_FM_SAT:
457 dprintk(("msp3400: SAT setstereo: %s\n",strmode[mode]));
459 case VIDEO_SOUND_MONO:
460 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
462 case VIDEO_SOUND_STEREO:
463 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
465 case VIDEO_SOUND_LANG1:
466 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
468 case VIDEO_SOUND_LANG2:
469 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
473 case MSP_MODE_FM_NICAM1:
474 case MSP_MODE_FM_NICAM2:
475 case MSP_MODE_AM_NICAM:
476 dprintk(("msp3400: NICAM setstereo: %s\n",strmode[mode]));
477 msp3400c_setcarrier(client,msp->second,msp->main);
482 dprintk(("msp3400: BTSC setstereo: %s\n",strmode[mode]));
485 case MSP_MODE_EXTERN:
486 dprintk(("msp3400: extern setstereo: %s\n",strmode[mode]));
489 case MSP_MODE_FM_RADIO:
490 dprintk(("msp3400: FM-Radio setstereo: %s\n",strmode[mode]));
493 dprintk(("msp3400: mono setstereo\n"));
499 case VIDEO_SOUND_STEREO:
500 src = 0x0020 | nicam;
503 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
506 case VIDEO_SOUND_MONO:
507 if (msp->mode == MSP_MODE_AM_NICAM) {
508 dprintk(("msp3400: switching to AM mono\n"));
509 /* AM mono decoding is handled by tuner, not MSP chip */
510 /* SCART switching control register */
511 msp3400c_set_scart(client,SCART_MONO,0);
515 case VIDEO_SOUND_LANG1:
516 src = 0x0000 | nicam;
518 case VIDEO_SOUND_LANG2:
519 src = 0x0010 | nicam;
522 dprintk(("msp3400: setstereo final source/matrix = 0x%x\n", src));
525 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
526 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
527 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
528 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
530 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
531 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
532 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
533 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
538 msp3400c_print_mode(struct msp3400c *msp)
540 if (msp->main == msp->second) {
541 printf("bktr: msp3400: mono sound carrier: %d.%03d MHz\n",
542 msp->main/910000,(msp->main/910)%1000);
544 printf("bktr: msp3400: main sound carrier: %d.%03d MHz\n",
545 msp->main/910000,(msp->main/910)%1000);
547 if (msp->mode == MSP_MODE_FM_NICAM1 ||
548 msp->mode == MSP_MODE_FM_NICAM2)
549 printf("bktr: msp3400: NICAM/FM carrier : %d.%03d MHz\n",
550 msp->second/910000,(msp->second/910)%1000);
551 if (msp->mode == MSP_MODE_AM_NICAM)
552 printf("bktr: msp3400: NICAM/AM carrier : %d.%03d MHz\n",
553 msp->second/910000,(msp->second/910)%1000);
554 if (msp->mode == MSP_MODE_FM_TERRA &&
555 msp->main != msp->second) {
556 printf("bktr: msp3400: FM-stereo carrier : %d.%03d MHz\n",
557 msp->second/910000,(msp->second/910)%1000);
562 msp3400c_restore_dfp(bktr_ptr_t client)
564 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
567 for (i = 0; i < DFP_COUNT; i++) {
568 if (-1 == msp->dfp_regs[i])
570 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
574 /* ----------------------------------------------------------------------- */
576 struct REGISTER_DUMP {
581 struct REGISTER_DUMP d1[] = {
582 { 0x007e, "autodetect" },
583 { 0x0023, "C_AD_BITS " },
584 { 0x0038, "ADD_BITS " },
585 { 0x003e, "CIB_BITS " },
586 { 0x0057, "ERROR_RATE" },
590 autodetect_stereo(bktr_ptr_t client)
592 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
594 int newstereo = msp->stereo;
595 int newnicam = msp->nicam_on;
599 case MSP_MODE_FM_TERRA:
600 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
603 dprintk(("msp34xx: stereo detect register: %d\n",val));
606 newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
607 } else if (val < -4096) {
608 newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
610 newstereo = VIDEO_SOUND_MONO;
614 case MSP_MODE_FM_NICAM1:
615 case MSP_MODE_FM_NICAM2:
616 case MSP_MODE_AM_NICAM:
617 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
618 dprintk(("msp34xx: nicam sync=%d, mode=%d\n",
619 val & 1, (val & 0x1e) >> 1));
623 switch ((val & 0x1e) >> 1) {
626 newstereo = VIDEO_SOUND_STEREO;
630 newstereo = VIDEO_SOUND_MONO
635 newstereo = VIDEO_SOUND_MONO
640 newstereo = VIDEO_SOUND_MONO;
646 newstereo = VIDEO_SOUND_MONO;
650 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
651 dprintk(("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
653 (val & 0x0002) ? "no" : "yes",
654 (val & 0x0004) ? "no" : "yes",
655 (val & 0x0040) ? "stereo" : "mono",
656 (val & 0x0080) ? ", nicam 2nd mono" : "",
657 (val & 0x0100) ? ", bilingual/SAP" : ""));
658 newstereo = VIDEO_SOUND_MONO;
659 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
660 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
663 if (newstereo != msp->stereo) {
665 dprintk(("msp34xx: watch: stereo %d => %d\n",
666 msp->stereo,newstereo));
667 msp->stereo = newstereo;
669 if (newnicam != msp->nicam_on) {
671 dprintk(("msp34xx: watch: nicam %d => %d\n",
672 msp->nicam_on,newnicam));
673 msp->nicam_on = newnicam;
679 * A kernel thread for msp3400 control -- we don't want to block the
680 * in the ioctl while doing the sound carrier & stereo detect
683 /* stereo/multilang monitoring */
684 static void watch_stereo(bktr_ptr_t client)
686 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
688 if (autodetect_stereo(client)) {
689 if (msp->stereo & VIDEO_SOUND_STEREO)
690 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
691 else if (msp->stereo & VIDEO_SOUND_LANG1)
692 msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
694 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
696 if (client->stereo_once)
697 msp->watch_stereo = 0;
701 static void msp3400c_thread(void *data)
703 bktr_ptr_t client = data;
704 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
706 struct CARRIER_DETECT *cd;
707 int count, max1,max2,val1,val2, val,this;
709 dprintk(("msp3400: thread started\n"));
714 tsleep(msp->kthread, 0, "idle", 0);
717 if (msp->halt_thread) {
718 msp->watch_stereo = 0;
719 msp->halt_thread = 0;
723 if (VIDEO_MODE_RADIO == msp->norm ||
724 MSP_MODE_EXTERN == msp->mode)
725 continue; /* nothing to do */
729 if (msp->watch_stereo) {
730 watch_stereo(client);
735 /* some time for the tuner to sync */
736 tsleep(msp->kthread, 0, "tuner sync", hz/2);
739 if (VIDEO_MODE_RADIO == msp->norm ||
740 MSP_MODE_EXTERN == msp->mode)
741 continue; /* nothing to do */
743 msp3400c_setvolume(client, msp->muted, 0, 0);
744 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
747 msp->watch_stereo = 0;
749 /* carrier detect pass #1 -- main carrier */
750 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
752 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
753 /* autodetect doesn't work well with AM ... */
756 dprintk(("msp3400: AM sound override\n"));
759 for (this = 0; this < count; this++) {
760 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
762 tsleep(msp->kthread, 0, "carrier detect", hz/100);
767 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
771 val1 = val, max1 = this;
772 dprintk(("msp3400: carrier1 val: %5d / %s\n", val,
776 /* carrier detect pass #2 -- second (stereo) carrier */
779 cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
782 cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
787 cd = NULL; count = 0;
791 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
792 /* autodetect doesn't work well with AM ... */
793 cd = NULL; count = 0; max2 = 0;
795 for (this = 0; this < count; this++) {
796 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
798 tsleep(msp->kthread, 0, "carrier detection", hz/100);
802 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
806 val2 = val, max2 = this;
807 dprintk(("msp3400: carrier2 val: %5d / %s\n", val,
811 /* programm the msp3400 according to the results */
812 msp->main = carrier_detect_main[max1].cdo;
817 msp->second = carrier_detect_55[max2].cdo;
818 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
820 /* XXX why mono? this probably can do stereo... - Alex*/
821 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
822 msp->watch_stereo = 1;
823 } else if (max2 == 1 && msp->nicam) {
825 msp->second = carrier_detect_55[max2].cdo;
826 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
828 msp3400c_setcarrier(client, msp->second, msp->main);
829 msp->watch_stereo = 1;
836 msp->second = MSP_CARRIER(6.552);
837 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
839 msp3400c_setcarrier(client, msp->second, msp->main);
840 msp->watch_stereo = 1;
843 if (max2 == 1 || max2 == 2) {
845 msp->second = carrier_detect_65[max2].cdo;
846 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
848 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
849 msp->watch_stereo = 1;
850 } else if (max2 == 0 &&
851 msp->norm == VIDEO_MODE_SECAM) {
852 /* L NICAM or AM-mono */
853 msp->second = carrier_detect_65[max2].cdo;
854 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
856 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
857 msp3400c_setcarrier(client, msp->second, msp->main);
858 /* volume prescale for SCART (AM mono input) */
859 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
860 msp->watch_stereo = 1;
861 } else if (max2 == 0 && msp->nicam) {
863 msp->second = carrier_detect_65[max2].cdo;
864 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
866 msp3400c_setcarrier(client, msp->second, msp->main);
867 msp->watch_stereo = 1;
875 msp->second = carrier_detect_main[max1].cdo;
876 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
878 msp3400c_setcarrier(client, msp->second, msp->main);
879 msp->stereo = VIDEO_SOUND_MONO;
880 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
884 if (msp->watch_stereo)
885 watch_stereo(client);
887 /* unmute + restore dfp registers */
888 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
889 msp3400c_restore_dfp(client);
892 msp3400c_print_mode(msp);
898 dprintk(("msp3400: thread: exit\n"));
902 wakeup(&msp->kthread);
907 /* ----------------------------------------------------------------------- */
908 /* this one uses the automatic sound standard detection of newer */
909 /* msp34xx chip versions */
911 static struct MODES {
916 { 0x0000, 0, 0, "ERROR" },
917 { 0x0001, 0, 0, "autodetect start" },
918 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
919 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
920 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
921 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
922 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
923 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
924 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
925 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
926 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
927 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
928 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
929 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
930 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
931 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
932 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
933 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
934 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
935 { -1, 0, 0, NULL }, /* EOF */
938 static void msp3410d_thread(void *data)
940 bktr_ptr_t client = data;
941 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
945 dprintk(("msp3410: thread started\n"));
950 if (!msp->watch_stereo)
954 tsleep(msp->kthread, 0, "idle", timo);
957 if (msp->halt_thread) {
958 msp->watch_stereo = 0;
959 msp->halt_thread = 0;
960 dprintk(("msp3410: thread halted\n"));
964 if (msp->mode == MSP_MODE_EXTERN)
969 if (msp->watch_stereo) {
970 watch_stereo(client);
975 /* some time for the tuner to sync */
976 tsleep(msp->kthread, 0, "tuner sync", hz/2);
979 if (msp->mode == MSP_MODE_EXTERN)
982 msp->watch_stereo = 0;
984 /* put into sane state (and mute) */
985 msp3400c_reset(client);
987 /* start autodetect */
993 case VIDEO_MODE_NTSC: /* BTSC */
997 case VIDEO_MODE_SECAM:
1001 case VIDEO_MODE_RADIO:
1010 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1011 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1015 for (i = 0; modelist[i].name != NULL; i++)
1016 if (modelist[i].retval == std)
1018 dprintk(("msp3410: setting mode: %s (0x%04x)\n",
1019 modelist[i].name ? modelist[i].name : "unknown",
1024 /* programmed some specific mode */
1027 /* triggered autodetect */
1029 tsleep(msp->kthread, 0, "autodetection", hz/10);
1034 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1037 dprintk(("msp3410: detection still in progress\n"));
1040 for (i = 0; modelist[i].name != NULL; i++)
1041 if (modelist[i].retval == val)
1043 dprintk(("msp3410: current mode: %s (0x%04x)\n",
1044 modelist[i].name ? modelist[i].name : "unknown",
1046 msp->main = modelist[i].main;
1047 msp->second = modelist[i].second;
1049 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1050 /* autodetection has failed, let backup */
1051 dprintk(("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
1052 modelist[8].name ? modelist[8].name : "unknown",val));
1054 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1057 /* set various prescales */
1058 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1059 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1060 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1064 case 0x0008: /* B/G NICAM */
1065 case 0x000a: /* I NICAM */
1067 msp->mode = MSP_MODE_FM_NICAM1;
1069 msp->mode = MSP_MODE_FM_NICAM2;
1070 /* just turn on stereo */
1071 msp->stereo = VIDEO_SOUND_STEREO;
1073 msp->watch_stereo = 1;
1074 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1077 msp->mode = MSP_MODE_AM_NICAM;
1078 msp->stereo = VIDEO_SOUND_MONO;
1080 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1081 msp->watch_stereo = 1;
1083 case 0x0020: /* BTSC */
1084 /* just turn on stereo */
1085 msp->mode = MSP_MODE_BTSC;
1086 msp->stereo = VIDEO_SOUND_STEREO;
1088 msp->watch_stereo = 1;
1089 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1091 case 0x0040: /* FM radio */
1092 msp->mode = MSP_MODE_FM_RADIO;
1093 msp->stereo = VIDEO_SOUND_STEREO;
1095 msp->watch_stereo = 0;
1097 msp3400c_set_scart(client,SCART_IN2,0);
1098 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1099 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1100 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1103 msp->mode = MSP_MODE_FM_TERRA;
1104 msp->stereo = VIDEO_SOUND_STEREO;
1106 msp->watch_stereo = 1;
1107 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1111 if (msp->watch_stereo)
1112 watch_stereo(client);
1114 /* unmute + restore dfp registers */
1115 msp3400c_setbass(client, msp->bass);
1116 msp3400c_settreble(client, msp->treble);
1117 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1118 msp3400c_restore_dfp(client);
1124 dprintk(("msp3410: thread: exit\n"));
1127 msp->kthread = NULL;
1128 wakeup(&msp->kthread);
1133 int msp_attach(bktr_ptr_t bktr)
1135 struct msp3400c *msp;
1140 msp = (struct msp3400c *) kmalloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
1143 bktr->msp3400c_info = msp;
1145 memset(msp,0,sizeof(struct msp3400c));
1149 msp->treble = 32768;
1151 msp->threaddesc = kmalloc(15 * sizeof(char), M_DEVBUF, M_NOWAIT);
1152 if (msp->threaddesc == NULL) {
1153 kfree(msp, M_DEVBUF);
1156 snprintf(msp->threaddesc, 14, "%s_msp34xx_thread", bktr->bktr_xname);
1158 for (i = 0; i < DFP_COUNT; i++)
1159 msp->dfp_regs[i] = -1;
1161 msp3400c_reset(bktr);
1163 rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
1165 rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
1166 if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
1167 kfree(msp->threaddesc, M_DEVBUF);
1168 kfree(msp, M_DEVBUF);
1169 bktr->msp3400c_info = NULL;
1170 printf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
1175 /* this will turn on a 1kHz beep - might be useful for debugging... */
1176 msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1179 sprintf(buf,"MSP34%02d%c-%c%d",
1180 (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
1181 msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
1183 if (bktr->mspsimple == -1) {
1185 /* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
1186 msp->simple = ((rev1&0xff)+'@' > 'C');
1188 /* use kenv value */
1189 msp->simple = bktr->mspsimple;
1192 /* hello world :-) */
1194 printf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
1196 printf(", has NICAM support");
1200 /* startup control thread */
1201 err = kthread_create(msp->simple ? msp3410d_thread : msp3400c_thread,
1202 bktr, &msp->kthread, msp->threaddesc);
1204 printf("%s: Error returned by kthread_create: %d", bktr_name(bktr), err);
1205 kfree(msp->threaddesc, M_DEVBUF);
1206 kfree(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 */
1225 msp->watch_stereo = 0;
1226 wakeup(msp->kthread);
1228 while (msp->kthread)
1229 tsleep(&msp->kthread, 0, "wait for kthread", hz/10);
1232 if (client->msp3400c_info != NULL) {
1233 kfree(client->msp3400c_info, M_DEVBUF);
1234 client->msp3400c_info = NULL;
1237 msp3400c_reset(client);
1242 void msp_wake_thread(bktr_ptr_t client)
1244 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1246 msp3400c_setvolume(client,msp->muted,0,0);
1247 msp->watch_stereo=0;
1250 wakeup(msp->kthread);
1253 void msp_halt_thread(bktr_ptr_t client)
1255 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1257 msp3400c_setvolume(client,msp->muted,0,0);
1260 msp->halt_thread = 1;
1261 wakeup(msp->kthread);
1263 #endif /* BKTR_NEW_MSP34XX_DRIVER */