2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * programming the msp34* sound processor family
34 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
36 * what works and what doesn't:
39 * Support for Hauppauge cards added (decoding handled by tuner) added by
40 * Frederic Crozat <fcrozat@mail.dotcom.fr>
43 * should work. The stereo modes are backward compatible to FM-mono,
44 * therefore FM-Mono should be always available.
46 * FM-Stereo (B/G, used in germany)
47 * should work, with autodetect
49 * FM-Stereo (satellite)
50 * should work, no autodetect (i.e. default is mono, but you can
51 * switch to stereo -- untested)
53 * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
54 * should work, with autodetect. Support for NICAM was added by
55 * Pekka Pietikainen <pp@netppl.fi>
59 * - better SAT support
62 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
63 * using soundcore instead of OSS
66 * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
67 * are in the public domain. Please contact me (Alex) and not Gerd for
68 * any problems you encounter under FreeBSD.
71 * - mutex handling (currently not mp-safe)
72 * - the various options here as loader tunables or compile time or whatever
73 * - how does the new dolby flag work with the current dpl_* stuff?
74 * Maybe it's just enough to set the dolby flag to 1 and it works.
75 * As I don't have a dolby card myself, I can't test it, though.
78 #include "opt_bktr.h" /* Include any kernel config options */
79 #ifdef BKTR_NEW_MSP34XX_DRIVER /* file only needed for new driver */
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/vnode.h>
86 #include <sys/unistd.h>
87 #include <sys/kthread.h>
88 #include <sys/malloc.h>
90 #ifdef BKTR_USE_FREEBSD_SMBUS
91 #include <sys/bus.h> /* required by bktr_reg.h */
94 #include <machine/bus.h> /* required by bktr_reg.h */
96 #include <dev/bktr/ioctl_meteor.h>
97 #include <dev/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
98 #include <dev/bktr/bktr_reg.h>
99 #include <dev/bktr/bktr_tuner.h>
100 #include <dev/bktr/bktr_audio.h>
101 #include <dev/bktr/bktr_core.h>
103 #define VIDEO_MODE_PAL 0
104 #define VIDEO_MODE_NTSC 1
105 #define VIDEO_MODE_SECAM 2
106 #define VIDEO_MODE_AUTO 3
108 #define VIDEO_SOUND_MONO 1
109 #define VIDEO_SOUND_STEREO 2
110 #define VIDEO_SOUND_LANG1 4
111 #define VIDEO_SOUND_LANG2 8
113 #define DFP_COUNT 0x41
114 static const int bl_dfp[] = {
115 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
116 0x0b, 0x0d, 0x0e, 0x10
127 int main, second; /* sound carrier */
131 int left, right; /* volume */
134 /* shadow register set */
135 int dfp_regs[DFP_COUNT];
138 struct proc *kthread;
140 int active,restart,rmmod;
146 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
148 /* ---------------------------------------------------------------------- */
150 #define dprintk(...) do { \
152 printf("%s: ", bktr_name(client)); \
153 printf(__VA_ARGS__); \
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 /* Terrestrial 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",val & 1, (val & 0x1e) >> 1);
622 switch ((val & 0x1e) >> 1) {
625 newstereo = VIDEO_SOUND_STEREO;
629 newstereo = VIDEO_SOUND_MONO
634 newstereo = VIDEO_SOUND_MONO
639 newstereo = VIDEO_SOUND_MONO;
645 newstereo = VIDEO_SOUND_MONO;
649 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
650 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
652 (val & 0x0002) ? "no" : "yes",
653 (val & 0x0004) ? "no" : "yes",
654 (val & 0x0040) ? "stereo" : "mono",
655 (val & 0x0080) ? ", nicam 2nd mono" : "",
656 (val & 0x0100) ? ", bilingual/SAP" : "");
657 newstereo = VIDEO_SOUND_MONO;
658 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
659 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
662 if (newstereo != msp->stereo) {
664 dprintk("msp34xx: watch: stereo %d => %d\n",
665 msp->stereo,newstereo);
666 msp->stereo = newstereo;
668 if (newnicam != msp->nicam_on) {
670 dprintk("msp34xx: watch: nicam %d => %d\n",
671 msp->nicam_on,newnicam);
672 msp->nicam_on = newnicam;
678 * A kernel thread for msp3400 control -- we don't want to block the
679 * in the ioctl while doing the sound carrier & stereo detect
682 /* stereo/multilang monitoring */
683 static void watch_stereo(bktr_ptr_t client)
685 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
687 if (autodetect_stereo(client)) {
688 if (msp->stereo & VIDEO_SOUND_STEREO)
689 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
690 else if (msp->stereo & VIDEO_SOUND_LANG1)
691 msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
693 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
695 if (client->stereo_once)
696 msp->watch_stereo = 0;
700 static void msp3400c_thread(void *data)
702 bktr_ptr_t client = data;
703 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
705 struct CARRIER_DETECT *cd;
706 int count, max1,max2,val1,val2, val,this;
708 dprintk("msp3400: thread started\n");
714 tsleep(msp->kthread, PRIBIO, "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, PRIBIO, "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, PRIBIO, "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,cd[this].name);
775 /* carrier detect pass #2 -- second (stereo) carrier */
778 cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
781 cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
786 cd = NULL; count = 0;
790 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
791 /* autodetect doesn't work well with AM ... */
792 cd = NULL; count = 0; max2 = 0;
794 for (this = 0; this < count; this++) {
795 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
797 tsleep(msp->kthread, PRIBIO, "carrier detection", hz/100);
801 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
805 val2 = val, max2 = this;
806 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
809 /* programm the msp3400 according to the results */
810 msp->main = carrier_detect_main[max1].cdo;
815 msp->second = carrier_detect_55[max2].cdo;
816 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
818 /* XXX why mono? this probably can do stereo... - Alex*/
819 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
820 msp->watch_stereo = 1;
821 } else if (max2 == 1 && msp->nicam) {
823 msp->second = carrier_detect_55[max2].cdo;
824 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
826 msp3400c_setcarrier(client, msp->second, msp->main);
827 msp->watch_stereo = 1;
834 msp->second = MSP_CARRIER(6.552);
835 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
837 msp3400c_setcarrier(client, msp->second, msp->main);
838 msp->watch_stereo = 1;
841 if (max2 == 1 || max2 == 2) {
843 msp->second = carrier_detect_65[max2].cdo;
844 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
846 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
847 msp->watch_stereo = 1;
848 } else if (max2 == 0 &&
849 msp->norm == VIDEO_MODE_SECAM) {
850 /* L NICAM or AM-mono */
851 msp->second = carrier_detect_65[max2].cdo;
852 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
854 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
855 msp3400c_setcarrier(client, msp->second, msp->main);
856 /* volume prescale for SCART (AM mono input) */
857 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
858 msp->watch_stereo = 1;
859 } else if (max2 == 0 && msp->nicam) {
861 msp->second = carrier_detect_65[max2].cdo;
862 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
864 msp3400c_setcarrier(client, msp->second, msp->main);
865 msp->watch_stereo = 1;
873 msp->second = carrier_detect_main[max1].cdo;
874 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
876 msp3400c_setcarrier(client, msp->second, msp->main);
877 msp->stereo = VIDEO_SOUND_MONO;
878 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
882 if (msp->watch_stereo)
883 watch_stereo(client);
885 /* unmute + restore dfp registers */
886 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
887 msp3400c_restore_dfp(client);
890 msp3400c_print_mode(msp);
896 dprintk("msp3400: thread: exit\n");
900 wakeup(&msp->kthread);
906 /* ----------------------------------------------------------------------- */
907 /* this one uses the automatic sound standard detection of newer */
908 /* msp34xx chip versions */
910 static struct MODES {
915 { 0x0000, 0, 0, "ERROR" },
916 { 0x0001, 0, 0, "autodetect start" },
917 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
918 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
919 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
920 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
921 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
922 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
923 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
924 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
925 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
926 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
927 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
928 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
929 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
930 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
931 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
932 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
933 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
934 { -1, 0, 0, NULL }, /* EOF */
937 static void msp3410d_thread(void *data)
939 bktr_ptr_t client = data;
940 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
944 dprintk("msp3410: thread started\n");
950 if (!msp->watch_stereo)
954 tsleep(msp->kthread, PRIBIO, "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, PRIBIO, "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",std);
1023 /* programmed some specific mode */
1026 /* triggered autodetect */
1028 tsleep(msp->kthread, PRIBIO, "autodetection", hz/10);
1033 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1036 dprintk("msp3410: detection still in progress\n");
1039 for (i = 0; modelist[i].name != NULL; i++)
1040 if (modelist[i].retval == val)
1042 dprintk("msp3410: current mode: %s (0x%04x)\n",
1043 modelist[i].name ? modelist[i].name : "unknown",
1045 msp->main = modelist[i].main;
1046 msp->second = modelist[i].second;
1048 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1049 /* autodetection has failed, let backup */
1050 dprintk("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
1051 modelist[8].name ? modelist[8].name : "unknown",val);
1053 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1056 /* set various prescales */
1057 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1058 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1059 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1063 case 0x0008: /* B/G NICAM */
1064 case 0x000a: /* I NICAM */
1066 msp->mode = MSP_MODE_FM_NICAM1;
1068 msp->mode = MSP_MODE_FM_NICAM2;
1069 /* just turn on stereo */
1070 msp->stereo = VIDEO_SOUND_STEREO;
1072 msp->watch_stereo = 1;
1073 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1076 msp->mode = MSP_MODE_AM_NICAM;
1077 msp->stereo = VIDEO_SOUND_MONO;
1079 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1080 msp->watch_stereo = 1;
1082 case 0x0020: /* BTSC */
1083 /* just turn on stereo */
1084 msp->mode = MSP_MODE_BTSC;
1085 msp->stereo = VIDEO_SOUND_STEREO;
1087 msp->watch_stereo = 1;
1088 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1090 case 0x0040: /* FM radio */
1091 msp->mode = MSP_MODE_FM_RADIO;
1092 msp->stereo = VIDEO_SOUND_STEREO;
1094 msp->watch_stereo = 0;
1096 msp3400c_set_scart(client,SCART_IN2,0);
1097 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1098 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1099 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1102 msp->mode = MSP_MODE_FM_TERRA;
1103 msp->stereo = VIDEO_SOUND_STEREO;
1105 msp->watch_stereo = 1;
1106 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1110 if (msp->watch_stereo)
1111 watch_stereo(client);
1113 /* unmute + restore dfp registers */
1114 msp3400c_setbass(client, msp->bass);
1115 msp3400c_settreble(client, msp->treble);
1116 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1117 msp3400c_restore_dfp(client);
1123 dprintk("msp3410: thread: exit\n");
1126 msp->kthread = NULL;
1127 wakeup(&msp->kthread);
1133 int msp_attach(bktr_ptr_t bktr)
1135 struct msp3400c *msp;
1140 msp = (struct msp3400c *) malloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
1143 bktr->msp3400c_info = msp;
1145 memset(msp,0,sizeof(struct msp3400c));
1149 msp->treble = 32768;
1152 for (i = 0; i < DFP_COUNT; i++)
1153 msp->dfp_regs[i] = -1;
1155 msp3400c_reset(bktr);
1157 rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
1159 rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
1160 if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
1161 free(msp, M_DEVBUF);
1162 bktr->msp3400c_info = NULL;
1163 printf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
1168 /* this will turn on a 1kHz beep - might be useful for debugging... */
1169 msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1172 sprintf(buf,"MSP34%02d%c-%c%d",
1173 (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
1174 msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
1176 if (bktr->mspsimple == -1) {
1178 /* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
1179 msp->simple = ((rev1&0xff)+'@' > 'C');
1181 /* use kenv value */
1182 msp->simple = bktr->mspsimple;
1185 /* hello world :-) */
1187 printf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
1189 printf(", has NICAM support");
1193 /* startup control thread */
1194 err = kproc_create(msp->simple ? msp3410d_thread : msp3400c_thread,
1195 bktr, &msp->kthread, (RFFDG | RFPROC), 0,
1196 "%s_msp34xx_thread", bktr->bktr_xname);
1198 printf("%s: Error returned by kproc_create: %d", bktr_name(bktr), err);
1199 free(msp, M_DEVBUF);
1200 bktr->msp3400c_info = NULL;
1203 wakeup(msp->kthread);
1209 int msp_detach(bktr_ptr_t client)
1211 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1213 /* shutdown control thread */
1216 /* XXX mutex lock required */
1219 msp->watch_stereo = 0;
1220 wakeup(msp->kthread);
1222 while (msp->kthread)
1223 tsleep(&msp->kthread, PRIBIO, "wait for kthread", hz/10);
1227 if (client->msp3400c_info != NULL) {
1228 free(client->msp3400c_info, M_DEVBUF);
1229 client->msp3400c_info = NULL;
1232 msp3400c_reset(client);
1237 void msp_wake_thread(bktr_ptr_t client)
1239 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1241 msp3400c_setvolume(client,msp->muted,0,0);
1242 msp->watch_stereo=0;
1245 wakeup(msp->kthread);
1248 void msp_halt_thread(bktr_ptr_t client)
1250 struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
1252 msp3400c_setvolume(client,msp->muted,0,0);
1255 msp->halt_thread = 1;
1256 wakeup(msp->kthread);
1258 #endif /* BKTR_NEW_MSP34XX_DRIVER */