📄 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/malloc.h>#include <linux/videodev.h>#include <linux/i2c.h>#include <linux/i2c-algo-bit.h>#include <linux/init.h>#include <linux/smp_lock.h>#include "audiochip.h"#include "tvaudio.h"#include "id.h"/* ---------------------------------------------------------------------- *//* insmod args */MODULE_PARM(debug,"i");static int debug = 0; /* insmod parameter */#define dprintk if (debug) printk/* ---------------------------------------------------------------------- *//* our structs */#define MAXREGS 64struct CHIPSTATE;typedef int (*getvalue)(int);typedef int (*checkit)(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; 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;};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; /* thread */ struct task_struct *thread; struct semaphore *notify; wait_queue_head_t wq; int wake,done;};/* ---------------------------------------------------------------------- *//* i2c adresses */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_PIC16C54 >> 1, I2C_CLIENT_END };static unsigned short normal_i2c_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };static struct i2c_client_address_data addr_data = { normal_i2c, normal_i2c_range, probe, probe_range, ignore, ignore_range, force};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", chip->c.name, 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", chip->c.name, val); return -1; } } else { dprintk("%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)) { printk(KERN_WARNING "%s: I/O error (write reg%d=0x%x)\n", chip->c.name, subaddr, val); return -1; } } return 0;}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", chip->c.name); return -1; } dprintk("%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[1] = subaddr; if (2 != i2c_transfer(chip->c.adapter,msgs,2)) { printk(KERN_WARNING "%s: I/O error (read2)\n", chip->c.name); return -1; } dprintk("%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) */ dprintk("%s: chip_cmd(%s): reg=%d, data:", chip->c.name,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", 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 int chip_thread(void *data){ struct CHIPSTATE *chip = data; struct CHIPDESC *desc = chiplist + chip->type; #ifdef CONFIG_SMP lock_kernel();#endif daemonize(); sigfillset(¤t->blocked); strcpy(current->comm,chip->c.name); chip->thread = current;#ifdef CONFIG_SMP unlock_kernel();#endif dprintk("%s: thread started\n", chip->c.name); if(chip->notify != NULL) up(chip->notify); for (;;) { if (chip->done) break; if (!chip->wake) { interruptible_sleep_on(&chip->wq); dprintk("%s: thread wakeup\n", chip->c.name); if (chip->done || signal_pending(current)) break; } chip->wake = 0; /* wait some time -- let the audio hardware figure the current mode */ current->state = TASK_INTERRUPTIBLE; schedule_timeout(HZ); if (signal_pending(current)) break; if (chip->wake) continue; dprintk("%s: thread checkmode\n", chip->c.name); desc->checkmode(chip); } chip->thread = NULL; dprintk("%s: thread exiting\n", chip->c.name); if(chip->notify != NULL) up_and_exit(chip->notify,0); return 0;}/* ---------------------------------------------------------------------- *//* 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 0x7aint tda9840_getmode(struct CHIPSTATE *chip){ return VIDEO_SOUND_MONO | VIDEO_SOUND_STEREO | VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;}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 *//* 0x00 - VR in TDA9855 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -