⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stv0297_cs2.c

📁 V4l driver for DVB HD
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Driver for the DVB-C STV0297 demodulator * *  a special, temporary version for the CableStar2 which can hopefully be *  rewritten to be used with the other stv0297-based cards * *  Copyright (C) 2005 Patrick Boettcher <patrick.boettcher@desy.de> * *  Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net> * *  Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de> * *  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. * *  Changelog 2005-03-24 - revamp in order to get the CableStar2 working *  (thanks to John Jurrius, BBTI Inc.) */#include <linux/init.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/delay.h>#include "dvb_frontend.h"#include "stv0297_cs2.h"#include "stv0297_priv.h"struct stv0297_state {	struct i2c_adapter *i2c;	struct dvb_frontend_ops ops;	const struct stv0297_config *config;	struct dvb_frontend frontend;	int invert;	u32 lastber;	unsigned long base_freq;};static int debug;module_param(debug, int, 0644);MODULE_PARM_DESC(debug, "set debugging level (1=info,2=i2c,4=calc (|-able)).");#define dprintk(level,x...) if (level & debug) printk(x)#define dbufout(b,l) if (debug & 0x02) {\	int i; \	for (i = 0; i < l; i++) \		deb_i2c("%02x ",b[i]); \}#define deb_info(args...) dprintk(0x01,args)#define deb_i2c(args...)  dprintk(0x02,args)#define deb_calc(args...) dprintk(0x04,args)#define abs64(x) (x) < 0 ? -(x) : (x);static s64 div64(s64 dividend,s64 divisor){	s64 rdiv,remainder,result=0,i;	s64 divbits,valbits;	s64 neg = 0;	if (divisor == 0)		return 0;	if (dividend < 0)		neg = !neg;	if (divisor < 0)		neg = !neg;	dividend = abs64(dividend);	divisor  = abs64(divisor);	deb_calc("%Ld / %Ld\n",dividend,divisor);	for (i = 1,divbits = 0; i < divisor;  i *= 2, divbits++);	for (i = 1,valbits = 0; i < dividend; i *= 2, valbits++);	rdiv = divisor << (valbits-divbits);	for (i = valbits; i >= divbits; i--) {		result <<= 1;		if (dividend >= rdiv) {			result |= 1;			dividend -= rdiv;		}		rdiv >>= 1;	}	remainder = dividend;	if (neg)		return -result;	else		return result;}static int stv0297_writeregsI(struct stv0297_state *state, u8 reg1, u8 *b, u8 len){	int ret;	u8 buf[len+1];	struct i2c_msg msg = {		.addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len+1	};	deb_i2c("wr: %02x ",reg1);	dbufout(b,len);	deb_i2c("\n");	buf[0] = reg1;	memcpy(&buf[1],b,len);	if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1)		deb_info("%s: writereg error (reg == 0x%02x, datalen == %d, "			"ret == %i)\n", __FUNCTION__, reg1, len, ret);	return (ret != 1) ? -EREMOTEIO : 0;}static int stv0297_writeregI(struct stv0297_state *state, u8 reg, u8 data){	return stv0297_writeregsI(state,reg,&data,1);}static int stv0297_readregsI(struct stv0297_state *state, u8 reg1, u8 *b, u8 len){	int ret;	struct i2c_msg msg[] = {		{ .addr = state->config->demod_address, .flags = 0,        .buf = &reg1,.len = 1},		{ .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b,    .len = len}	};	/* the Nexus-CA needs a STOP between the register and data, but this breaks	 * the common i2c-routine, where you have 2 i2c-message in a package	 * when doing a read operation TODO	 */	if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {		deb_info("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);		return -EREMOTEIO;	}	deb_i2c("rd: %02x ",reg1);	dbufout(b,len);	deb_i2c("\n");	/*	if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {		deb_info("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);		return -1;	}*/	return 0;}static int stv0297_readregI(struct stv0297_state *state, u8 reg){	int ret;	u8 b[] = { 0 };	if ((ret = stv0297_readregsI(state,reg,b,1)) < 0)		return ret;	return b[0];}static int stv0297_writereg_maskI(struct stv0297_state *state, u8 reg, u8 mask, u8 data){	int val;	if ((val = stv0297_readregI(state, reg)) < 0)		return val;	val &= ~mask;	val |= (data & mask);	return stv0297_writeregI(state, reg, val);}static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate){	u64 tmp = div64(((u64) srate) << 32,state->config->fclk);	deb_info("symbolrate: %u, regval: %016Lx\n",srate,tmp );	stv0297_writeregI(state, ST_LOOP5, tmp & 0xFF);	stv0297_writeregI(state, ST_LOOP6, tmp >> 8);	stv0297_writeregI(state, ST_LOOP7, tmp >> 16);	stv0297_writeregI(state, ST_LOOP8, tmp >> 24);}static u32 stv0297_get_symbolrate(struct stv0297_state *state){	u64 tmp;	tmp  = stv0297_readregI(state, ST_LOOP5);	tmp |= stv0297_readregI(state, ST_LOOP6) << 8;	tmp |= stv0297_readregI(state, ST_LOOP7) << 16;	tmp |= stv0297_readregI(state, ST_LOOP8) << 24;	tmp *= state->config->fclk;	tmp >>= 32;	return (u32) tmp;}static void stv0297_set_crl(struct stv0297_state *state, struct dvb_frontend_parameters *p, s32 car_offset){	u8 crlbuf[10] = { 0 };	u32 sweeprate,iphase;	u64 tmp;	u16 sweepvalue;	     if (p->u.qam.symbol_rate < 2000000UL) sweeprate =   80000UL;	else if (p->u.qam.symbol_rate < 3500000UL) sweeprate =  200000UL;	else if (p->u.qam.symbol_rate < 4500000UL) sweeprate =  800000UL;	else                                       sweeprate = 1000000UL;	tmp = div64(((u64) sweeprate) << 29, p->u.qam.symbol_rate);	sweepvalue = div64(tmp,1000000UL);	if (sweepvalue & 1)		sweepvalue += 2; /* round up, adjust */	sweepvalue >>=1;	deb_calc("symbol_rate: %u, sweeprate: %u, sweepvalue: %u (%x)\n",p->u.qam.symbol_rate,sweeprate,sweepvalue,sweepvalue);	crlbuf[0] = sweepvalue & 0xff;	crlbuf[9] = (sweepvalue >> 4) & 0xf0;	if (p->u.qam.modulation == QAM_256)		crlbuf[1] = 0x58;	else		crlbuf[1] = 0x49;	crlbuf[2] = 0x0a;	crlbuf[3] = crlbuf[4] = crlbuf[5] = 0x00;	/* carrier offset */	if (p->u.qam.modulation == QAM_16)		iphase = 0;	else		iphase = div64(((u64) car_offset) << 28, p->u.qam.symbol_rate);	deb_calc("symbol_rate: %u, car_offset %d, iphase: %u (%x)\n",p->u.qam.symbol_rate,car_offset,iphase,iphase);	crlbuf[6]  =  iphase        & 0xff;	crlbuf[7]  = (iphase >>  8) & 0xff;	crlbuf[8]  = (iphase >> 16) & 0xff;	crlbuf[9] |= (iphase >> 24) & 0x0f;	stv0297_writeregsI(state,CRL_0,crlbuf,sizeof(crlbuf));}/*static long stv0297_get_carrieroffset(struct stv0297_state *state){	s64 tmp;	stv0297_writeregI(state, CRL_11, 0x00);	tmp = stv0297_readregI(state, CRL_6);	tmp |= (stv0297_readregI(state, CRL_7) << 8);	tmp |= (stv0297_readregI(state, CRL_8) << 16);	tmp |= (stv0297_readregI(state, CRL_9) & 0x0F) << 24;	tmp *= stv0297_get_symbolrate(state);	tmp >>= 28;	return (s32) tmp;}*/static int stv0297_set_modulation(struct stv0297_state *state, fe_modulation_t modulation){	int val = stv0297_readregI(state,EQU_0) & STV0297_CLEARQAM;	switch (modulation) {		case QAM_16:  val = STV0297_QAM16; break;		case QAM_32:  val = STV0297_QAM32; break;		case QAM_64:  val = STV0297_QAM64; break;		case QAM_128: val = STV0297_QAM128; break;		case QAM_256: val = STV0297_QAM256; break;		default: return -EINVAL;	}	stv0297_writeregI(state, EQU_0, val);	return 0;}static int stv0297_set_inversion(struct stv0297_state *state, fe_spectral_inversion_t inversion){	int val = 0;	switch (inversion) {		case INVERSION_OFF: val = 0; break;		case INVERSION_ON:  val = 1; break;		default: return -EINVAL;	}	stv0297_writereg_maskI(state, CTRL_3, 0x08, val << 3);	return 0;}int stv0297_cs2_enable_plli2c(struct dvb_frontend *fe){	struct stv0297_state *state = fe->demodulator_priv;	stv0297_writeregI(state, CTRL_7, 0x78);	stv0297_writeregI(state, CTRL_6, 0xc8);	return 0;}static int stv0297_init(struct dvb_frontend *fe){	struct stv0297_state *state = fe->demodulator_priv;	state->invert = 0;	return 0;}static int stv0297_reset(struct dvb_frontend *fe){	struct stv0297_state *state = fe->demodulator_priv;	/* soft reset */	stv0297_writeregI(state, CTRL_0, 0x01);	stv0297_writeregI(state, CTRL_0, 0x00);	/* reset deinterleaver */	stv0297_writeregI(state, CTRL_1, 0x01);	stv0297_writeregI(state, CTRL_1, 0x00);	/* device specific initialization here *//*	state->config_struct->demod_init(fe); */	/* this is the initialization for the CableStar, maybe it works for other too */	{		u8 equ0[] = { 0x48, 0x58 }; /* QAM64, for QAM256: { 0x3a, 0x8a } */		u8 equ3[] = { 0x00, 0x00 };		u8 equ7[] = { 0x00, 0x00 };		u8 delagc[] = { 0xff, /* gold, original: 0x6b or 0x54 */						 0x9d, 0xff, /* ALPS, MT203x: 0x54 (Gold) 0xb5 */						 0x00, /* gold, original: 0x44 or 0xff, 0xb0, 0x8b */						 0x29, /* original, or 0x00, 0x07, 0xb3 */						 0x55, /* gold */						 0x80, /* original */						 0x6e, 0x9c };		u8 wbagc0[] = { 0x1a, 0xfe, 0x33, 0x00 /* original */, 0xff, 0x00, 0x00 };		u8 wbagc9[] = { 0x04, 0x51, 0xf8 };#define GAIN_PATH0 0x01#define GAIN_PATH1 0x04#define INTEGRAL_GAIN_HI 0X0#define INTEGRAL_GAIN_LO 0X06#define DIRECT_GAIN_HI   0x0#define DIRECT_GAIN_LO   0x06#define STLOOP_ALGORITHM 0x00		u8 st_loop2[] = { (GAIN_PATH0 << 5) | (GAIN_PATH1 << 2) | INTEGRAL_GAIN_HI, DIRECT_GAIN_LO };		u8 st_loop9[] = { DIRECT_GAIN_HI | INTEGRAL_GAIN_LO, 0x5e /* original */, 0x04 | (STLOOP_ALGORITHM << 4) };		u8 pmfagc0[] = { 0xff, 0x04, 0x00, 0x00, 0x0c };		u8 ctrl[] = { 0x20, 0x00, 0x30,			stv0297_readregI(state,CTRL_3) & SPECTRAL_INVERSION_TOGGLE, /* Clear, except the Spectral Inversion Bit. */			0x04,0x22,0x08,0x1b,0x00 /* Gold */,0x00 };		u8 deint_sync[] = { 0x00, 0x04 };		u8 bert[] = { 0x86, 0x00, 0x00 }; /* bit4 of byte0 toggles byte-wise-counting */		u8 deint[] = { 0x91, 0x0b };		u8 outformat[] = { 0x5b, 0x10, 0x12 };		u8 rs_desc0[] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x00 };		u8 rs_desc14[] = { 0x00, 0x01 };		stv0297_writeregsI(state,EQU_0,equ0,sizeof(equ0));		stv0297_writeregsI(state,EQU_3,equ3,sizeof(equ3));		stv0297_writeregsI(state,EQU_7,equ7,sizeof(equ7));		stv0297_writeregsI(state,DELAGC_0,delagc,sizeof(delagc));		stv0297_writeregsI(state,WBAGC_0,wbagc0,sizeof(wbagc0));		stv0297_writeregsI(state,WBAGC_9,wbagc9,sizeof(wbagc9));		stv0297_writeregsI(state,ST_LOOP2,st_loop2,sizeof(st_loop2));		stv0297_writeregsI(state,ST_LOOP9,st_loop9,sizeof(st_loop9));		stv0297_writeregsI(state,PMFAGC_0,pmfagc0,sizeof(pmfagc0));		stv0297_writeregsI(state,CTRL_0,ctrl,sizeof(ctrl));		stv0297_writeregsI(state,DEINT_SYNC_0,deint_sync,sizeof(deint_sync));		stv0297_writeregsI(state,BERT_0,bert,sizeof(bert));		stv0297_writeregsI(state,DEINT_0,deint,sizeof(deint));		stv0297_writeregsI(state,OUTFORMAT_0,outformat,sizeof(outformat));		stv0297_writeregsI(state,RS_DESC_0,rs_desc0,sizeof(rs_desc0));		stv0297_writeregsI(state,RS_DESC_14,rs_desc14,sizeof(rs_desc14));	}

⌨️ 快捷键说明

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