saa7114.c

来自「trident tm5600的linux驱动」· C语言 代码 · 共 1,074 行 · 第 1/2 页

C
1,074
字号
/* * saa7114 - Philips SAA7114H video decoder driver version 0.0.1 * * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com> * * Based on saa7111 driver by Dave Perks * * Copyright (C) 1998 Dave Perks <dperks@ibm.net> * * Slight changes for video timing and attachment output by * Wolfgang Scherr <scherr@net4you.net> * * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net> *    - moved over to linux>=2.4.x i2c protocol (1/1/2003) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/module.h>#include <linux/types.h>#include <linux/ioctl.h>#include <asm/uaccess.h>#include <linux/i2c.h>#include <linux/i2c-id.h>#include <linux/videodev.h>#include <linux/video_decoder.h>#include <media/v4l2-common.h>#include <media/v4l2-i2c-drv-legacy.h>#include "compat.h"MODULE_DESCRIPTION("Philips SAA7114H video decoder driver");MODULE_AUTHOR("Maxim Yevtyushkin");MODULE_LICENSE("GPL");static int debug;module_param(debug, int, 0);MODULE_PARM_DESC(debug, "Debug level (0-1)");/* ----------------------------------------------------------------------- */struct saa7114 {	unsigned char reg[0xf0 * 2];	int norm;	int input;	int enable;	int bright;	int contrast;	int hue;	int sat;	int playback;};#define   I2C_DELAY   10//#define SAA_7114_NTSC_HSYNC_START       (-3)//#define SAA_7114_NTSC_HSYNC_STOP        (-18)#define SAA_7114_NTSC_HSYNC_START  (-17)#define SAA_7114_NTSC_HSYNC_STOP   (-32)//#define SAA_7114_NTSC_HOFFSET           (5)#define SAA_7114_NTSC_HOFFSET		(6)#define SAA_7114_NTSC_VOFFSET           (10)#define SAA_7114_NTSC_WIDTH             (720)#define SAA_7114_NTSC_HEIGHT            (250)#define SAA_7114_SECAM_HSYNC_START      (-17)#define SAA_7114_SECAM_HSYNC_STOP       (-32)#define SAA_7114_SECAM_HOFFSET          (2)#define SAA_7114_SECAM_VOFFSET          (10)#define SAA_7114_SECAM_WIDTH            (720)#define SAA_7114_SECAM_HEIGHT           (300)#define SAA_7114_PAL_HSYNC_START        (-17)#define SAA_7114_PAL_HSYNC_STOP         (-32)#define SAA_7114_PAL_HOFFSET            (2)#define SAA_7114_PAL_VOFFSET            (10)#define SAA_7114_PAL_WIDTH              (720)#define SAA_7114_PAL_HEIGHT             (300)#define SAA_7114_VERTICAL_CHROMA_OFFSET         0	//0x50504040#define SAA_7114_VERTICAL_LUMA_OFFSET           0#define REG_ADDR(x) (((x) << 1) + 1)#define LOBYTE(x) ((unsigned char)((x) & 0xff))#define HIBYTE(x) ((unsigned char)(((x) >> 8) & 0xff))#define LOWORD(x) ((unsigned short int)((x) & 0xffff))#define HIWORD(x) ((unsigned short int)(((x) >> 16) & 0xffff))/* ----------------------------------------------------------------------- */static inline int saa7114_write(struct i2c_client *client, u8 reg, u8 value){	return i2c_smbus_write_byte_data(client, reg, value);}static int saa7114_write_block(struct i2c_client *client, const u8 *data, unsigned int len){	int ret = -1;	u8 reg;	/* the saa7114 has an autoincrement function, use it if	 * the adapter understands raw I2C */	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {		/* do raw I2C, not smbus compatible */		u8 block_data[32];		int block_len;		while (len >= 2) {			block_len = 0;			block_data[block_len++] = reg = data[0];			do {				block_data[block_len++] = data[1];				reg++;				len -= 2;				data += 2;			} while (len >= 2 && data[0] == reg && block_len < 32);			ret = i2c_master_send(client, block_data, block_len);			if (ret < 0)				break;		}	} else {		/* do some slow I2C emulation kind of thing */		while (len >= 2) {			reg = *data++;			ret = saa7114_write(client, reg, *data++);			if (ret < 0)				break;			len -= 2;		}	}	return ret;}static inline int saa7114_read(struct i2c_client *client, u8 reg){	return i2c_smbus_read_byte_data(client, reg);}/* ----------------------------------------------------------------------- */// initially set NTSC, compositestatic const unsigned char init[] = {	0x00, 0x00,		/* 00 - ID byte , chip version,				 * read only */	0x01, 0x08,		/* 01 - X,X,X,X, IDEL3 to IDEL0 -				 * horizontal increment delay,				 * recommended position */	0x02, 0x00,		/* 02 - FUSE=3, GUDL=2, MODE=0 ;				 * input control */	0x03, 0x10,		/* 03 - HLNRS=0, VBSL=1, WPOFF=0,				 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */	0x04, 0x90,		/* 04 - GAI1=256 */	0x05, 0x90,		/* 05 - GAI2=256 */	0x06, SAA_7114_NTSC_HSYNC_START,	/* 06 - HSB: hsync start,				 * depends on the video standard */	0x07, SAA_7114_NTSC_HSYNC_STOP,	/* 07 - HSS: hsync stop, depends				 *on the video standard */	0x08, 0xb8,		/* 08 - AUFD=1, FSEL=1, EXFIL=0, VTRC=1,				 * HPLL: free running in playback, locked				 * in capture, VNOI=0 */	0x09, 0x80,		/* 09 - BYPS=0, PREF=0, BPSS=0, VBLB=0,				 * UPTCV=0, APER=1; depends from input */	0x0a, 0x80,		/* 0a - BRIG=128 */	0x0b, 0x44,		/* 0b - CONT=1.109 */	0x0c, 0x40,		/* 0c - SATN=1.0 */	0x0d, 0x00,		/* 0d - HUE=0 */	0x0e, 0x84,		/* 0e - CDTO, CSTD2 to 0, DCVF, FCTC,				 * CCOMB; depends from video standard */	0x0f, 0x24,		/* 0f - ACGC,CGAIN6 to CGAIN0; depends				 * from video standard */	0x10, 0x03,		/* 10 - OFFU1 to 0, OFFV1 to 0, CHBW,				 * LCBW2 to 0 */	0x11, 0x59,		/* 11 - COLO, RTP1, HEDL1 to 0, RTP0,				 * YDEL2 to 0 */	0x12, 0xc9,		/* 12 - RT signal control RTSE13 to 10				 * and 03 to 00 */	0x13, 0x80,		/* 13 - RT/X port output control  */	0x14, 0x00,		/* 14 - analog, ADC, compatibility control */	0x15, 0x00,		/* 15 - VGATE start FID change  */	0x16, 0xfe,		/* 16 - VGATE stop */	0x17, 0x00,		/* 17 - Misc., VGATE MSBs */	0x18, 0x40,		/* RAWG */	0x19, 0x80,		/* RAWO */	0x1a, 0x00,	0x1b, 0x00,	0x1c, 0x00,	0x1d, 0x00,	0x1e, 0x00,	0x1f, 0x00,		/* status byte, read only */	0x20, 0x00,		/* video decoder reserved part */	0x21, 0x00,	0x22, 0x00,	0x23, 0x00,	0x24, 0x00,	0x25, 0x00,	0x26, 0x00,	0x27, 0x00,	0x28, 0x00,	0x29, 0x00,	0x2a, 0x00,	0x2b, 0x00,	0x2c, 0x00,	0x2d, 0x00,	0x2e, 0x00,	0x2f, 0x00,	0x30, 0xbc,		/* audio clock generator */	0x31, 0xdf,	0x32, 0x02,	0x33, 0x00,	0x34, 0xcd,	0x35, 0xcc,	0x36, 0x3a,	0x37, 0x00,	0x38, 0x03,	0x39, 0x10,	0x3a, 0x00,	0x3b, 0x00,	0x3c, 0x00,	0x3d, 0x00,	0x3e, 0x00,	0x3f, 0x00,	0x40, 0x00,		/* VBI data slicer */	0x41, 0xff,	0x42, 0xff,	0x43, 0xff,	0x44, 0xff,	0x45, 0xff,	0x46, 0xff,	0x47, 0xff,	0x48, 0xff,	0x49, 0xff,	0x4a, 0xff,	0x4b, 0xff,	0x4c, 0xff,	0x4d, 0xff,	0x4e, 0xff,	0x4f, 0xff,	0x50, 0xff,	0x51, 0xff,	0x52, 0xff,	0x53, 0xff,	0x54, 0xff,	0x55, 0xff,	0x56, 0xff,	0x57, 0xff,	0x58, 0x40,		// framing code	0x59, 0x47,		// horizontal offset	0x5a, 0x06,		// vertical offset	0x5b, 0x83,		// field offset	0x5c, 0x00,		// reserved	0x5d, 0x3e,		// header and data	0x5e, 0x00,		// sliced data	0x5f, 0x00,		// reserved	0x60, 0x00,		/* video decoder reserved part */	0x61, 0x00,	0x62, 0x00,	0x63, 0x00,	0x64, 0x00,	0x65, 0x00,	0x66, 0x00,	0x67, 0x00,	0x68, 0x00,	0x69, 0x00,	0x6a, 0x00,	0x6b, 0x00,	0x6c, 0x00,	0x6d, 0x00,	0x6e, 0x00,	0x6f, 0x00,	0x70, 0x00,		/* video decoder reserved part */	0x71, 0x00,	0x72, 0x00,	0x73, 0x00,	0x74, 0x00,	0x75, 0x00,	0x76, 0x00,	0x77, 0x00,	0x78, 0x00,	0x79, 0x00,	0x7a, 0x00,	0x7b, 0x00,	0x7c, 0x00,	0x7d, 0x00,	0x7e, 0x00,	0x7f, 0x00,	0x80, 0x00,		/* X-port, I-port and scaler */	0x81, 0x00,	0x82, 0x00,	0x83, 0x00,	0x84, 0xc5,	0x85, 0x0d,		// hsync and vsync ?	0x86, 0x40,	0x87, 0x01,	0x88, 0x00,	0x89, 0x00,	0x8a, 0x00,	0x8b, 0x00,	0x8c, 0x00,	0x8d, 0x00,	0x8e, 0x00,	0x8f, 0x00,	0x90, 0x03,		/* Task A definition           */	0x91, 0x08,	0x92, 0x00,	0x93, 0x40,	0x94, 0x00,		// window settings	0x95, 0x00,	0x96, 0x00,	0x97, 0x00,	0x98, 0x00,	0x99, 0x00,	0x9a, 0x00,	0x9b, 0x00,	0x9c, 0x00,	0x9d, 0x00,	0x9e, 0x00,	0x9f, 0x00,	0xa0, 0x01,		/* horizontal integer prescaling ratio */	0xa1, 0x00,		/* horizontal prescaler accumulation				 * sequence length */	0xa2, 0x00,		/* UV FIR filter, Y FIR filter, prescaler				 * DC gain */	0xa3, 0x00,	0xa4, 0x80,		// luminance brightness	0xa5, 0x40,		// luminance gain	0xa6, 0x40,		// chrominance saturation	0xa7, 0x00,	0xa8, 0x00,		// horizontal luminance scaling increment	0xa9, 0x04,	0xaa, 0x00,		// horizontal luminance phase offset	0xab, 0x00,	0xac, 0x00,		// horizontal chrominance scaling increment	0xad, 0x02,	0xae, 0x00,		// horizontal chrominance phase offset	0xaf, 0x00,	0xb0, 0x00,		// vertical luminance scaling increment	0xb1, 0x04,	0xb2, 0x00,		// vertical chrominance scaling increment	0xb3, 0x04,	0xb4, 0x00,	0xb5, 0x00,	0xb6, 0x00,	0xb7, 0x00,	0xb8, 0x00,	0xb9, 0x00,	0xba, 0x00,	0xbb, 0x00,	0xbc, 0x00,	0xbd, 0x00,	0xbe, 0x00,	0xbf, 0x00,	0xc0, 0x02,		// Task B definition	0xc1, 0x08,	0xc2, 0x00,	0xc3, 0x40,	0xc4, 0x00,		// window settings	0xc5, 0x00,	0xc6, 0x00,	0xc7, 0x00,	0xc8, 0x00,	0xc9, 0x00,	0xca, 0x00,	0xcb, 0x00,	0xcc, 0x00,	0xcd, 0x00,	0xce, 0x00,	0xcf, 0x00,	0xd0, 0x01,		// horizontal integer prescaling ratio	0xd1, 0x00,		// horizontal prescaler accumulation sequence length	0xd2, 0x00,		// UV FIR filter, Y FIR filter, prescaler DC gain	0xd3, 0x00,	0xd4, 0x80,		// luminance brightness	0xd5, 0x40,		// luminance gain	0xd6, 0x40,		// chrominance saturation	0xd7, 0x00,	0xd8, 0x00,		// horizontal luminance scaling increment	0xd9, 0x04,	0xda, 0x00,		// horizontal luminance phase offset	0xdb, 0x00,	0xdc, 0x00,		// horizontal chrominance scaling increment	0xdd, 0x02,	0xde, 0x00,		// horizontal chrominance phase offset	0xdf, 0x00,	0xe0, 0x00,		// vertical luminance scaling increment	0xe1, 0x04,	0xe2, 0x00,		// vertical chrominance scaling increment	0xe3, 0x04,	0xe4, 0x00,	0xe5, 0x00,	0xe6, 0x00,	0xe7, 0x00,	0xe8, 0x00,	0xe9, 0x00,	0xea, 0x00,	0xeb, 0x00,	0xec, 0x00,	0xed, 0x00,	0xee, 0x00,	0xef, 0x00};static int saa7114_command(struct i2c_client *client, unsigned cmd, void *arg){	struct saa7114 *decoder = i2c_get_clientdata(client);	switch (cmd) {	case 0:		//dprintk(1, KERN_INFO "%s: writing init\n", I2C_NAME(client));		//saa7114_write_block(client, init, sizeof(init));		break;	case DECODER_DUMP:	{		int i;		if (!debug)			break;		v4l_info(client, "decoder dump\n");		for (i = 0; i < 32; i += 16) {			int j;			v4l_info(client, "%03x", i);			for (j = 0; j < 16; ++j) {				printk(KERN_CONT " %02x",				       saa7114_read(client, i + j));			}			printk(KERN_CONT "\n");		}		break;	}	case DECODER_GET_CAPABILITIES:	{		struct video_decoder_capability *cap = arg;		v4l_dbg(1, debug, client, "get capabilities\n");		cap->flags = VIDEO_DECODER_PAL |			     VIDEO_DECODER_NTSC |			     VIDEO_DECODER_AUTO |			     VIDEO_DECODER_CCIR;		cap->inputs = 8;		cap->outputs = 1;		break;	}	case DECODER_GET_STATUS:	{		int *iarg = arg;		int status;		int res;		status = saa7114_read(client, 0x1f);		v4l_dbg(1, debug, client, "status: 0x%02x\n", status);		res = 0;		if ((status & (1 << 6)) == 0) {			res |= DECODER_STATUS_GOOD;		}		switch (decoder->norm) {		case VIDEO_MODE_NTSC:			res |= DECODER_STATUS_NTSC;			break;		case VIDEO_MODE_PAL:			res |= DECODER_STATUS_PAL;			break;		case VIDEO_MODE_SECAM:			res |= DECODER_STATUS_SECAM;			break;		default:		case VIDEO_MODE_AUTO:			if ((status & (1 << 5)) != 0) {				res |= DECODER_STATUS_NTSC;			} else {				res |= DECODER_STATUS_PAL;			}			break;		}		if ((status & (1 << 0)) != 0) {			res |= DECODER_STATUS_COLOR;		}		*iarg = res;		break;	}	case DECODER_SET_NORM:	{		int *iarg = arg;		short int hoff = 0, voff = 0, w = 0, h = 0;		v4l_dbg(1, debug, client, "set norm\n");		switch (*iarg) {		case VIDEO_MODE_NTSC:			v4l_dbg(1, debug, client, "NTSC\n");			decoder->reg[REG_ADDR(0x06)] =			    SAA_7114_NTSC_HSYNC_START;			decoder->reg[REG_ADDR(0x07)] =			    SAA_7114_NTSC_HSYNC_STOP;			decoder->reg[REG_ADDR(0x08)] = decoder->playback ? 0x7c : 0xb8;	// PLL free when playback, PLL close when capture			decoder->reg[REG_ADDR(0x0e)] = 0x85;			decoder->reg[REG_ADDR(0x0f)] = 0x24;			hoff = SAA_7114_NTSC_HOFFSET;			voff = SAA_7114_NTSC_VOFFSET;			w = SAA_7114_NTSC_WIDTH;			h = SAA_7114_NTSC_HEIGHT;			break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?