📄 tvaudio.c
字号:
/* * experimental driver for simple i2c audio chips. * * Copyright (c) 2000 Gerd Knorr * based on code by: * Eric Sandeen (eric_sandeen@bigfoot.com) * Steve VanDeBogart (vandebo@uclink.berkeley.edu) * Greg Alexander (galexand@acm.org) * * This code is placed under the terms of the GNU General Public License * * OPTIONS: * debug - set to 1 if you'd like to see debug messages * */#include <linux/config.h>#include <linux/module.h>#include <linux/moduleparam.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 "compat.h"#include <linux/videodev.h>#include <linux/i2c.h>#include <linux/i2c-algo-bit.h>#include <linux/init.h>#include <linux/smp_lock.h>#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)#include "i2c-compat.h"#else#endif#include <media/tvaudio.h>#include <media/v4l2-common.h>#include <media/i2c-addr.h>/* ---------------------------------------------------------------------- *//* insmod args */static int debug = 0; /* insmod parameter */module_param(debug, int, 0644);MODULE_DESCRIPTION("device driver for various i2c TV sound decoder / audiomux chips");MODULE_AUTHOR("Eric Sandeen, Steve VanDeBogart, Greg Alexander, Gerd Knorr");MODULE_LICENSE("GPL");#define UNSET (-1U)/* ---------------------------------------------------------------------- *//* our structs */#define MAXREGS 64struct CHIPSTATE;typedef int (*getvalue)(int);typedef int (*checkit)(struct CHIPSTATE*);typedef int (*initialize)(struct CHIPSTATE*);typedef int (*getmode)(struct CHIPSTATE*);typedef void (*setmode)(struct CHIPSTATE*, int mode);typedef void (*checkmode)(struct CHIPSTATE*);/* i2c command */typedef struct AUDIOCMD { int count; /* # of bytes to send */ unsigned char bytes[MAXREGS+1]; /* addr, data, data, ... */} audiocmd;/* chip description */struct CHIPDESC { char *name; /* chip name */ int id; /* ID */ int addr_lo, addr_hi; /* i2c address range */ int registers; /* # of registers */ int *insmodopt; checkit checkit; initialize initialize; int flags;#define CHIP_HAS_VOLUME 1#define CHIP_HAS_BASSTREBLE 2#define CHIP_HAS_INPUTSEL 4 /* various i2c command sequences */ audiocmd init; /* which register has which value */ int leftreg,rightreg,treblereg,bassreg; /* initialize with (defaults to 65535/65535/32768/32768 */ int leftinit,rightinit,trebleinit,bassinit; /* functions to convert the values (v4l -> chip) */ getvalue volfunc,treblefunc,bassfunc; /* get/set mode */ getmode getmode; setmode setmode; /* check / autoswitch audio after channel switches */ checkmode checkmode; /* input switch register + values for v4l inputs */ int inputreg; int inputmap[4]; int inputmute; int inputmask;};static struct CHIPDESC chiplist[];/* current state of the chip */struct CHIPSTATE { struct i2c_client c; /* index into CHIPDESC array */ int type; /* shadow register set */ audiocmd shadow; /* current settings */ __u16 left,right,treble,bass,muted,mode; int prevmode; int radio; int input; /* thread */ pid_t tpid; struct completion texit; wait_queue_head_t wq; struct timer_list wt; int done; int watch_stereo; int audmode;};/* ---------------------------------------------------------------------- *//* i2c addresses */static unsigned short normal_i2c[] = { I2C_ADDR_TDA8425 >> 1, I2C_ADDR_TEA6300 >> 1, I2C_ADDR_TEA6420 >> 1, I2C_ADDR_TDA9840 >> 1, I2C_ADDR_TDA985x_L >> 1, I2C_ADDR_TDA985x_H >> 1, I2C_ADDR_TDA9874 >> 1, I2C_ADDR_PIC16C54 >> 1, I2C_CLIENT_END };#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };#endifI2C_CLIENT_INSMOD;static struct i2c_driver driver;static struct i2c_client client_template;/* ---------------------------------------------------------------------- *//* i2c I/O functions */static int chip_write(struct CHIPSTATE *chip, int subaddr, int val){ unsigned char buffer[2]; if (-1 == subaddr) { v4l_dbg(1, debug, &chip->c, "%s: chip_write: 0x%x\n", chip->c.name, val); chip->shadow.bytes[1] = val; buffer[0] = val; if (1 != i2c_master_send(&chip->c,buffer,1)) { v4l_warn(&chip->c, "%s: I/O error (write 0x%x)\n", chip->c.name, val); return -1; } } else { v4l_dbg(1, debug, &chip->c, "%s: chip_write: reg%d=0x%x\n", chip->c.name, subaddr, val); chip->shadow.bytes[subaddr+1] = val; buffer[0] = subaddr; buffer[1] = val; if (2 != i2c_master_send(&chip->c,buffer,2)) { v4l_warn(&chip->c, "%s: I/O error (write reg%d=0x%x)\n", chip->c.name, subaddr, val); return -1; } } return 0;}static int chip_write_masked(struct CHIPSTATE *chip, int subaddr, int val, int mask){ if (mask != 0) { if (-1 == subaddr) { val = (chip->shadow.bytes[1] & ~mask) | (val & mask); } else { val = (chip->shadow.bytes[subaddr+1] & ~mask) | (val & mask); } } return chip_write(chip, subaddr, val);}static int chip_read(struct CHIPSTATE *chip){ unsigned char buffer; if (1 != i2c_master_recv(&chip->c,&buffer,1)) { v4l_warn(&chip->c, "%s: I/O error (read)\n", chip->c.name); return -1; } v4l_dbg(1, debug, &chip->c, "%s: chip_read: 0x%x\n",chip->c.name, buffer); return buffer;}static int chip_read2(struct CHIPSTATE *chip, int subaddr){ unsigned char write[1]; unsigned char read[1]; struct i2c_msg msgs[2] = { { chip->c.addr, 0, 1, write }, { chip->c.addr, I2C_M_RD, 1, read } }; write[0] = subaddr; if (2 != i2c_transfer(chip->c.adapter,msgs,2)) { v4l_warn(&chip->c, "%s: I/O error (read2)\n", chip->c.name); return -1; } v4l_dbg(1, debug, &chip->c, "%s: chip_read2: reg%d=0x%x\n", chip->c.name, subaddr,read[0]); return read[0];}static int chip_cmd(struct CHIPSTATE *chip, char *name, audiocmd *cmd){ int i; if (0 == cmd->count) return 0; /* update our shadow register set; print bytes if (debug > 0) */ v4l_dbg(1, debug, &chip->c, "%s: chip_cmd(%s): reg=%d, data:", chip->c.name, name,cmd->bytes[0]); for (i = 1; i < cmd->count; i++) { if (debug) printk(" 0x%x",cmd->bytes[i]); chip->shadow.bytes[i+cmd->bytes[0]] = cmd->bytes[i]; } if (debug) printk("\n"); /* send data to the chip */ if (cmd->count != i2c_master_send(&chip->c,cmd->bytes,cmd->count)) { v4l_warn(&chip->c, "%s: I/O error (%s)\n", chip->c.name, name); return -1; } return 0;}/* ---------------------------------------------------------------------- *//* kernel thread for doing i2c stuff asyncronly * right now it is used only to check the audio mode (mono/stereo/whatever) * some time after switching to another TV channel, then turn on stereo * if available, ... */static void chip_thread_wake(unsigned long data){ struct CHIPSTATE *chip = (struct CHIPSTATE*)data; wake_up_interruptible(&chip->wq);}static int chip_thread(void *data){ DECLARE_WAITQUEUE(wait, current); struct CHIPSTATE *chip = data; struct CHIPDESC *desc = chiplist + chip->type;#if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) daemonize("%s", chip->c.name);#else daemonize();#endif allow_signal(SIGTERM); v4l_dbg(1, debug, &chip->c, "%s: thread started\n", chip->c.name); for (;;) { add_wait_queue(&chip->wq, &wait); if (!chip->done) { set_current_state(TASK_INTERRUPTIBLE); schedule(); } remove_wait_queue(&chip->wq, &wait);#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,12) try_to_freeze();#endif if (chip->done || signal_pending(current)) break; v4l_dbg(1, debug, &chip->c, "%s: thread wakeup\n", chip->c.name); /* don't do anything for radio or if mode != auto */ if (chip->radio || chip->mode != 0) continue; /* have a look what's going on */ desc->checkmode(chip); /* schedule next check */ mod_timer(&chip->wt, jiffies+2*HZ); } v4l_dbg(1, debug, &chip->c, "%s: thread exiting\n", chip->c.name); complete_and_exit(&chip->texit, 0); return 0;}static void generic_checkmode(struct CHIPSTATE *chip){ struct CHIPDESC *desc = chiplist + chip->type; int mode = desc->getmode(chip); if (mode == chip->prevmode) return; v4l_dbg(1, debug, &chip->c, "%s: thread checkmode\n", chip->c.name); chip->prevmode = mode; if (mode & VIDEO_SOUND_STEREO) desc->setmode(chip,VIDEO_SOUND_STEREO); else if (mode & VIDEO_SOUND_LANG1) desc->setmode(chip,VIDEO_SOUND_LANG1); else if (mode & VIDEO_SOUND_LANG2) desc->setmode(chip,VIDEO_SOUND_LANG2); else desc->setmode(chip,VIDEO_SOUND_MONO);}/* ---------------------------------------------------------------------- *//* audio chip descriptions - defines+functions for tda9840 */#define TDA9840_SW 0x00#define TDA9840_LVADJ 0x02#define TDA9840_STADJ 0x03#define TDA9840_TEST 0x04#define TDA9840_MONO 0x10#define TDA9840_STEREO 0x2a#define TDA9840_DUALA 0x12#define TDA9840_DUALB 0x1e#define TDA9840_DUALAB 0x1a#define TDA9840_DUALBA 0x16#define TDA9840_EXTERNAL 0x7a#define TDA9840_DS_DUAL 0x20 /* Dual sound identified */#define TDA9840_ST_STEREO 0x40 /* Stereo sound identified */#define TDA9840_PONRES 0x80 /* Power-on reset detected if = 1 */#define TDA9840_TEST_INT1SN 0x1 /* Integration time 0.5s when set */#define TDA9840_TEST_INTFU 0x02 /* Disables integrator function */static int tda9840_getmode(struct CHIPSTATE *chip){ int val, mode; val = chip_read(chip); mode = VIDEO_SOUND_MONO; if (val & TDA9840_DS_DUAL) mode |= VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2; if (val & TDA9840_ST_STEREO) mode |= VIDEO_SOUND_STEREO; v4l_dbg(1, debug, &chip->c, "tda9840_getmode(): raw chip read: %d, return: %d\n", val, mode); return mode;}static void tda9840_setmode(struct CHIPSTATE *chip, int mode){ int update = 1; int t = chip->shadow.bytes[TDA9840_SW + 1] & ~0x7e; switch (mode) { case VIDEO_SOUND_MONO: t |= TDA9840_MONO; break; case VIDEO_SOUND_STEREO: t |= TDA9840_STEREO; break; case VIDEO_SOUND_LANG1: t |= TDA9840_DUALA; break; case VIDEO_SOUND_LANG2: t |= TDA9840_DUALB; break; default: update = 0; } if (update) chip_write(chip, TDA9840_SW, t);}static int tda9840_checkit(struct CHIPSTATE *chip){ int rc; rc = chip_read(chip); /* lower 5 bits should be 0 */ return ((rc & 0x1f) == 0) ? 1 : 0;}/* ---------------------------------------------------------------------- *//* audio chip descriptions - defines+functions for tda985x *//* subaddresses for TDA9855 */#define TDA9855_VR 0x00 /* Volume, right */#define TDA9855_VL 0x01 /* Volume, left */#define TDA9855_BA 0x02 /* Bass */#define TDA9855_TR 0x03 /* Treble */#define TDA9855_SW 0x04 /* Subwoofer - not connected on DTV2000 *//* subaddresses for TDA9850 */#define TDA9850_C4 0x04 /* Control 1 for TDA9850 *//* subaddesses for both chips */#define TDA985x_C5 0x05 /* Control 2 for TDA9850, Control 1 for TDA9855 */#define TDA985x_C6 0x06 /* Control 3 for TDA9850, Control 2 for TDA9855 */#define TDA985x_C7 0x07 /* Control 4 for TDA9850, Control 3 for TDA9855 */#define TDA985x_A1 0x08 /* Alignment 1 for both chips */#define TDA985x_A2 0x09 /* Alignment 2 for both chips */#define TDA985x_A3 0x0a /* Alignment 3 for both chips *//* Masks for bits in TDA9855 subaddresses *//* 0x00 - VR in TDA9855 *//* 0x01 - VL in TDA9855 *//* lower 7 bits control gain from -71dB (0x28) to 16dB (0x7f) * in 1dB steps - mute is 0x27 *//* 0x02 - BA in TDA9855 *//* lower 5 bits control bass gain from -12dB (0x06) to 16.5dB (0x19) * in .5dB steps - 0 is 0x0E *//* 0x03 - TR in TDA9855 *//* 4 bits << 1 control treble gain from -12dB (0x3) to 12dB (0xb) * in 3dB steps - 0 is 0x7 *//* Masks for bits in both chips' subaddresses *//* 0x04 - SW in TDA9855, C4/Control 1 in TDA9850 *//* Unique to TDA9855: *//* 4 bits << 2 control subwoofer/surround gain from -14db (0x1) to 14db (0xf) * in 3dB steps - mute is 0x0 *//* Unique to TDA9850: *//* lower 4 bits control stereo noise threshold, over which stereo turns off * set to values of 0x00 through 0x0f for Ster1 through Ster16 *//* 0x05 - C5 - Control 1 in TDA9855 , Control 2 in TDA9850*//* Unique to TDA9855: */#define TDA9855_MUTE 1<<7 /* GMU, Mute at outputs */#define TDA9855_AVL 1<<6 /* AVL, Automatic Volume Level */#define TDA9855_LOUD 1<<5 /* Loudness, 1==off */#define TDA9855_SUR 1<<3 /* Surround / Subwoofer 1==.5(L-R) 0==.5(L+R) */ /* Bits 0 to 3 select various combinations * of line in and line out, only the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -