📄 msp3400.c
字号:
/* * programming the msp34* sound processor family * * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org> * * what works and what doesn't: * * AM-Mono * Support for Hauppauge cards added (decoding handled by tuner) added by * Frederic Crozat <fcrozat@mail.dotcom.fr> * * FM-Mono * should work. The stereo modes are backward compatible to FM-mono, * therefore FM-Mono should be allways available. * * FM-Stereo (B/G, used in germany) * should work, with autodetect * * FM-Stereo (satellite) * should work, no autodetect (i.e. default is mono, but you can * switch to stereo -- untested) * * NICAM (B/G, L , used in UK, Scandinavia, Spain and France) * should work, with autodetect. Support for NICAM was added by * Pekka Pietikainen <pp@netppl.fi> * * * TODO: * - better SAT support * * * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch) * using soundcore instead of OSS * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/delay.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/i2c.h>#include <linux/videodev.h>#include <linux/init.h>#include <linux/smp_lock.h>#include <asm/semaphore.h>#include <asm/pgtable.h>#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)#include "audiochip.h"#include "i2c-compat.h"#else#include <media/audiochip.h>#endif#include "msp3400.h"/* insmod parameters */static int debug = 0; /* debug output */static int once = 0; /* no continous stereo monitoring */static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france), the autoscan seems work well only with FM... */static int simple = -1; /* use short programming (>= msp3410 only) */static int dolby = 0;#define DFP_COUNT 0x41static const int bl_dfp[] = { 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10};struct msp3400c { int rev1,rev2; int simple; int mode; int norm; int stereo; int nicam_on; int acb; int main, second; /* sound carrier */ int input; int muted; int left, right; /* volume */ int bass, treble; /* shadow register set */ int dfp_regs[DFP_COUNT]; /* thread */ pid_t tpid; struct completion texit; wait_queue_head_t wq; int active:1; int restart:1; int rmmod:1; int watch_stereo; struct timer_list wake_stereo;};#define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)#define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')#define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')#define VIDEO_MODE_RADIO 16 /* norm magic for radio mode *//* ---------------------------------------------------------------------- */#define dprintk if (debug >= 1) printk#define d2printk if (debug >= 2) printkMODULE_PARM(once,"i");MODULE_PARM(debug,"i");MODULE_PARM(simple,"i");MODULE_PARM(amsound,"i");MODULE_PARM(dolby,"i");MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");MODULE_AUTHOR("Gerd Knorr");MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too *//* ---------------------------------------------------------------------- */#define I2C_MSP3400C 0x80#define I2C_MSP3400C_ALT 0x88#define I2C_MSP3400C_DEM 0x10#define I2C_MSP3400C_DFP 0x12/* Addresses to scan */static unsigned short normal_i2c[] = { I2C_MSP3400C >> 1, I2C_MSP3400C_ALT >> 1, I2C_CLIENT_END};static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};I2C_CLIENT_INSMOD;/* ----------------------------------------------------------------------- *//* functions for talking to the MSP3400C Sound processor */#ifndef I2C_M_IGNORE_NAK# define I2C_M_IGNORE_NAK 0x1000#endifstatic int msp3400c_reset(struct i2c_client *client){ /* reset and read revision code */ static char reset_off[3] = { 0x00, 0x80, 0x00 }; static char reset_on[3] = { 0x00, 0x00, 0x00 }; static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e }; char read[2]; struct i2c_msg reset[2] = { { client->addr, I2C_M_IGNORE_NAK, 3, reset_off }, { client->addr, I2C_M_IGNORE_NAK, 3, reset_on }, }; struct i2c_msg test[2] = { { client->addr, 0, 3, write }, { client->addr, I2C_M_RD, 2, read }, }; if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) || (1 != i2c_transfer(client->adapter,&reset[1],1)) || (2 != i2c_transfer(client->adapter,test,2)) ) { printk(KERN_ERR "msp3400: chip reset failed\n"); return -1; } return 0; }static intmsp3400c_read(struct i2c_client *client, int dev, int addr){ int err; unsigned char write[3]; unsigned char read[2]; struct i2c_msg msgs[2] = { { client->addr, 0, 3, write }, { client->addr, I2C_M_RD, 2, read } }; write[0] = dev+1; write[1] = addr >> 8; write[2] = addr & 0xff; for (err = 0; err < 3;) { if (2 == i2c_transfer(client->adapter,msgs,2)) break; err++; printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n", err, dev, addr); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ/10); } if (3 == err) { printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n"); msp3400c_reset(client); return -1; } return read[0] << 8 | read[1];}static intmsp3400c_write(struct i2c_client *client, int dev, int addr, int val){ int err; unsigned char buffer[5]; buffer[0] = dev; buffer[1] = addr >> 8; buffer[2] = addr & 0xff; buffer[3] = val >> 8; buffer[4] = val & 0xff; for (err = 0; err < 3;) { if (5 == i2c_master_send(client, buffer, 5)) break; err++; printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n", err, dev, addr); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ/10); } if (3 == err) { printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n"); msp3400c_reset(client); return -1; } return 0;}/* ------------------------------------------------------------------------ *//* This macro is allowed for *constants* only, gcc must calculate it at compile time. Remember -- no floats in kernel mode */#define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))#define MSP_MODE_AM_DETECT 0#define MSP_MODE_FM_RADIO 2#define MSP_MODE_FM_TERRA 3#define MSP_MODE_FM_SAT 4#define MSP_MODE_FM_NICAM1 5#define MSP_MODE_FM_NICAM2 6#define MSP_MODE_AM_NICAM 7#define MSP_MODE_BTSC 8#define MSP_MODE_EXTERN 9static struct MSP_INIT_DATA_DEM { int fir1[6]; int fir2[6]; int cdo1; int cdo2; int ad_cv; int mode_reg; int dfp_src; int dfp_matrix;} msp_init_data[] = { /* AM (for carrier detect / msp3400) */ { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 }, MSP_CARRIER(5.5), MSP_CARRIER(5.5), 0x00d0, 0x0500, 0x0020, 0x3000}, /* AM (for carrier detect / msp3410) */ { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 }, MSP_CARRIER(5.5), MSP_CARRIER(5.5), 0x00d0, 0x0100, 0x0020, 0x3000}, /* FM Radio */ { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 }, MSP_CARRIER(10.7), MSP_CARRIER(10.7), 0x00d0, 0x0480, 0x0020, 0x3000 }, /* Terrestial FM-mono + FM-stereo */ { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 }, MSP_CARRIER(5.5), MSP_CARRIER(5.5), 0x00d0, 0x0480, 0x0030, 0x3000}, /* Sat FM-mono */ { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 }, MSP_CARRIER(6.5), MSP_CARRIER(6.5), 0x00c6, 0x0480, 0x0000, 0x3000}, /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */ { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 }, MSP_CARRIER(5.5), MSP_CARRIER(5.5), 0x00d0, 0x0040, 0x0120, 0x3000}, /* NICAM/FM -- I (6.0/6.552) */ { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 }, MSP_CARRIER(6.0), MSP_CARRIER(6.0), 0x00d0, 0x0040, 0x0120, 0x3000}, /* NICAM/AM -- L (6.5/5.85) */ { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 }, MSP_CARRIER(6.5), MSP_CARRIER(6.5), 0x00c6, 0x0140, 0x0120, 0x7c03},};struct CARRIER_DETECT { int cdo; char *name;};static struct CARRIER_DETECT carrier_detect_main[] = { /* main carrier */ { MSP_CARRIER(4.5), "4.5 NTSC" }, { MSP_CARRIER(5.5), "5.5 PAL B/G" }, { MSP_CARRIER(6.0), "6.0 PAL I" }, { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }};static struct CARRIER_DETECT carrier_detect_55[] = { /* PAL B/G */ { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" }, { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }};static struct CARRIER_DETECT carrier_detect_65[] = { /* PAL SAT / SECAM */ { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" }, { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" }, { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" }, { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" }, { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" }, { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },};#define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))/* ----------------------------------------------------------------------- */#define SCART_MASK 0#define SCART_IN1 1#define SCART_IN2 2#define SCART_IN1_DA 3#define SCART_IN2_DA 4#define SCART_IN3 5#define SCART_IN4 6#define SCART_MONO 7#define SCART_MUTE 8static int scarts[3][9] = { /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */ { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 }, { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 }, { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },};static char *scart_names[] = { "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"};static voidmsp3400c_set_scart(struct i2c_client *client, int in, int out){ struct msp3400c *msp = i2c_get_clientdata(client); if (-1 == scarts[out][in]) return; dprintk(KERN_DEBUG "msp34xx: scart switch: %s => %d\n",scart_names[in],out); msp->acb &= ~scarts[out][SCART_MASK]; msp->acb |= scarts[out][in]; msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);}/* ------------------------------------------------------------------------ */static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2){ msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff); msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12); msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff); msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12); msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/}static void msp3400c_setvolume(struct i2c_client *client, int muted, int left, int right){ int vol = 0,val = 0,balance = 0; if (!muted) { vol = (left > right) ? left : right; val = (vol * 0x73 / 65535) << 8; } if (vol > 0) { balance = ((right-left) * 127) / vol; } dprintk(KERN_DEBUG "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n", muted ? "on" : "off", left, right, val>>8, balance); msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */ msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */ /* scart - on/off only */ msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0); msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);}static void msp3400c_setbass(struct i2c_client *client, int bass){ int val = ((bass-32768) * 0x60 / 65535) << 8; dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8); msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */}static void msp3400c_settreble(struct i2c_client *client, int treble){ int val = ((treble-32768) * 0x60 / 65535) << 8; dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8); msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */}static void msp3400c_setmode(struct i2c_client *client, int type){ struct msp3400c *msp = i2c_get_clientdata(client); int i; dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type); msp->mode = type; msp->stereo = VIDEO_SOUND_MONO; msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */ msp_init_data[type].ad_cv); for (i = 5; i >= 0; i--) /* fir 1 */ msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001, msp_init_data[type].fir1[i]); msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */ msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040); msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000); for (i = 5; i >= 0; i--) msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, msp_init_data[type].fir2[i]); msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */ msp_init_data[type].mode_reg); msp3400c_setcarrier(client, msp_init_data[type].cdo1, msp_init_data[type].cdo2); msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/ if (dolby) { msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, 0x0520); /* I2S1 */ msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, 0x0620); /* I2S2 */ msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, msp_init_data[type].dfp_src); } else { msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008, msp_init_data[type].dfp_src); msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009, msp_init_data[type].dfp_src); msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b, msp_init_data[type].dfp_src); } msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a, msp_init_data[type].dfp_src); msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, msp_init_data[type].dfp_matrix); if (HAVE_NICAM(msp)) { /* nicam prescale */ msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */ }}/* turn on/off nicam + stereo */static void msp3400c_setstereo(struct i2c_client *client, int mode){ static char *strmode[16] = {#if __GNUC__ >= 3 [ 0 ... 15 ] = "invalid",#endif [ VIDEO_SOUND_MONO ] = "mono", [ VIDEO_SOUND_STEREO ] = "stereo", [ VIDEO_SOUND_LANG1 ] = "lang1", [ VIDEO_SOUND_LANG2 ] = "lang2", }; struct msp3400c *msp = i2c_get_clientdata(client); int nicam=0; /* channel source: FM/AM or nicam */ int src=0; /* switch demodulator */ switch (msp->mode) { case MSP_MODE_FM_TERRA: dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",strmode[mode]); msp3400c_setcarrier(client,msp->second,msp->main); switch (mode) { case VIDEO_SOUND_STEREO: msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001); break; case VIDEO_SOUND_MONO: case VIDEO_SOUND_LANG1: case VIDEO_SOUND_LANG2: msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000); break; } break; case MSP_MODE_FM_SAT: dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",strmode[mode]); switch (mode) { case VIDEO_SOUND_MONO: msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5)); break; case VIDEO_SOUND_STEREO: msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02)); break; case VIDEO_SOUND_LANG1: msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); break; case VIDEO_SOUND_LANG2: msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); break; } break; case MSP_MODE_FM_NICAM1: case MSP_MODE_FM_NICAM2: case MSP_MODE_AM_NICAM: dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",strmode[mode]); msp3400c_setcarrier(client,msp->second,msp->main); if (msp->nicam_on) nicam=0x0100; break; case MSP_MODE_BTSC: dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",strmode[mode]); nicam=0x0300;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -