📄 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/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/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 "audiochip.h"
#include "id.h"
#include "i2c-compat.h"
#else
#include <media/audiochip.h>
#include <media/id.h>
#endif
#include "tvaudio.h"
/* ---------------------------------------------------------------------- */
/* insmod args */
MODULE_PARM(debug,"i");
static int debug = 0; /* insmod parameter */
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)
#define dprintk if (debug) printk
/* ---------------------------------------------------------------------- */
/* our structs */
#define MAXREGS 64
struct 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[8];
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,mode;
int prevmode;
int norm;
/* thread */
struct task_struct *thread;
struct semaphore *notify;
wait_queue_head_t wq;
struct timer_list wt;
int done;
int watch_stereo;
};
#define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
/* ---------------------------------------------------------------------- */
/* i2c addresses */
static unsigned short normal_i2c[] = {
I2C_TDA8425 >> 1,
I2C_TEA6300 >> 1,
I2C_TEA6420 >> 1,
I2C_TDA9840 >> 1,
I2C_TDA985x_L >> 1,
I2C_TDA985x_H >> 1,
I2C_TDA9874 >> 1,
I2C_PIC16C54 >> 1,
I2C_CLIENT_END };
static unsigned short normal_i2c_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
I2C_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) {
dprintk("%s: chip_write: 0x%x\n",
i2c_clientname(&chip->c), val);
chip->shadow.bytes[1] = val;
buffer[0] = val;
if (1 != i2c_master_send(&chip->c,buffer,1)) {
printk(KERN_WARNING "%s: I/O error (write 0x%x)\n",
i2c_clientname(&chip->c), val);
return -1;
}
} else {
dprintk("%s: chip_write: reg%d=0x%x\n",
i2c_clientname(&chip->c), subaddr, val);
chip->shadow.bytes[subaddr+1] = val;
buffer[0] = subaddr;
buffer[1] = val;
if (2 != i2c_master_send(&chip->c,buffer,2)) {
printk(KERN_WARNING "%s: I/O error (write reg%d=0x%x)\n",
i2c_clientname(&chip->c), 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)) {
printk(KERN_WARNING "%s: I/O error (read)\n",
i2c_clientname(&chip->c));
return -1;
}
dprintk("%s: chip_read: 0x%x\n",i2c_clientname(&chip->c),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)) {
printk(KERN_WARNING "%s: I/O error (read2)\n",
i2c_clientname(&chip->c));
return -1;
}
dprintk("%s: chip_read2: reg%d=0x%x\n",
i2c_clientname(&chip->c),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) */
dprintk("%s: chip_cmd(%s): reg=%d, data:",
i2c_clientname(&chip->c),name,cmd->bytes[0]);
for (i = 1; i < cmd->count; i++) {
dprintk(" 0x%x",cmd->bytes[i]);
chip->shadow.bytes[i+cmd->bytes[0]] = cmd->bytes[i];
}
dprintk("\n");
/* send data to the chip */
if (cmd->count != i2c_master_send(&chip->c,cmd->bytes,cmd->count)) {
printk(KERN_WARNING "%s: I/O error (%s)\n", i2c_clientname(&chip->c), 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)
{
struct CHIPSTATE *chip = data;
struct CHIPDESC *desc = chiplist + chip->type;
lock_kernel();
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,61)
daemonize();
sigfillset(¤t->blocked);
strcpy(current->comm,i2c_clientname(&chip->c));
#else
daemonize("%s",i2c_clientname(&chip->c));
#endif
chip->thread = current;
unlock_kernel();
dprintk("%s: thread started\n", i2c_clientname(&chip->c));
if(chip->notify != NULL)
up(chip->notify);
for (;;) {
interruptible_sleep_on(&chip->wq);
dprintk("%s: thread wakeup\n", i2c_clientname(&chip->c));
if (chip->done || signal_pending(current))
break;
/* don't do anything for radio or if mode != auto */
if (chip->norm == VIDEO_MODE_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);
}
chip->thread = NULL;
dprintk("%s: thread exiting\n", i2c_clientname(&chip->c));
if(chip->notify != NULL)
up(chip->notify);
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;
dprintk("%s: thread checkmode\n", i2c_clientname(&chip->c));
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;
dprintk ("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);
}
/* ---------------------------------------------------------------------- */
/* 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -