📄 tda80xx.c
字号:
/* * tda80xx.c * * Philips TDA8044 / TDA8083 QPSK demodulator driver * * Copyright (C) 2001 Felix Domke <tmbinc@elitedvb.net> * Copyright (C) 2002-2004 Andreas Oberritter <obi@linuxtv.org> * * 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/config.h>#include <linux/delay.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/threads.h>#include <linux/interrupt.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/slab.h>#include <asm/irq.h>#include <asm/div64.h>#include "dvb_frontend.h"#include "tda80xx.h"enum { ID_TDA8044 = 0x04, ID_TDA8083 = 0x05,};struct tda80xx_state { struct i2c_adapter* i2c; struct dvb_frontend_ops ops; /* configuration settings */ const struct tda80xx_config* config; struct dvb_frontend frontend; u32 clk; int afc_loop; struct work_struct worklet; fe_code_rate_t code_rate; fe_spectral_inversion_t spectral_inversion; fe_status_t status; u8 id;};static int debug = 1;#define dprintk if (debug) printkstatic u8 tda8044_inittab_pre[] = { 0x02, 0x00, 0x6f, 0xb5, 0x86, 0x22, 0x00, 0xea, 0x30, 0x42, 0x98, 0x68, 0x70, 0x42, 0x99, 0x58, 0x95, 0x10, 0xf5, 0xe7, 0x93, 0x0b, 0x15, 0x68, 0x9a, 0x90, 0x61, 0x80, 0x00, 0xe0, 0x40, 0x00, 0x0f, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};static u8 tda8044_inittab_post[] = { 0x04, 0x00, 0x6f, 0xb5, 0x86, 0x22, 0x00, 0xea, 0x30, 0x42, 0x98, 0x68, 0x70, 0x42, 0x99, 0x50, 0x95, 0x10, 0xf5, 0xe7, 0x93, 0x0b, 0x15, 0x68, 0x9a, 0x90, 0x61, 0x80, 0x00, 0xe0, 0x40, 0x6c, 0x0f, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};static u8 tda8083_inittab[] = { 0x04, 0x00, 0x4a, 0x79, 0x04, 0x00, 0xff, 0xea, 0x48, 0x42, 0x79, 0x60, 0x70, 0x52, 0x9a, 0x10, 0x0e, 0x10, 0xf2, 0xa7, 0x93, 0x0b, 0x05, 0xc8, 0x9d, 0x00, 0x42, 0x80, 0x00, 0x60, 0x40, 0x00, 0x00, 0x75, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};static __inline__ u32 tda80xx_div(u32 a, u32 b){ return (a + (b / 2)) / b;}static __inline__ u32 tda80xx_gcd(u32 a, u32 b){ u32 r; while ((r = a % b)) { a = b; b = r; } return b;}static int tda80xx_read(struct tda80xx_state* state, u8 reg, u8 *buf, u8 len){ int ret; struct i2c_msg msg[] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®, .len = 1 }, { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } }; ret = i2c_transfer(state->i2c, msg, 2); if (ret != 2) dprintk("%s: readreg error (reg %02x, ret == %i)\n", __FUNCTION__, reg, ret); mdelay(10); return (ret == 2) ? 0 : -EREMOTEIO;}static int tda80xx_write(struct tda80xx_state* state, u8 reg, const u8 *buf, u8 len){ int ret; u8 wbuf[len + 1]; struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = wbuf, .len = len + 1 }; wbuf[0] = reg; memcpy(&wbuf[1], buf, len); ret = i2c_transfer(state->i2c, &msg, 1); if (ret != 1) dprintk("%s: i2c xfer error (ret == %i)\n", __FUNCTION__, ret); mdelay(10); return (ret == 1) ? 0 : -EREMOTEIO;}static __inline__ u8 tda80xx_readreg(struct tda80xx_state* state, u8 reg){ u8 val; tda80xx_read(state, reg, &val, 1); return val;}static __inline__ int tda80xx_writereg(struct tda80xx_state* state, u8 reg, u8 data){ return tda80xx_write(state, reg, &data, 1);}static int tda80xx_set_parameters(struct tda80xx_state* state, fe_spectral_inversion_t inversion, u32 symbol_rate, fe_code_rate_t fec_inner){ u8 buf[15]; u64 ratio; u32 clk; u32 k; u32 sr = symbol_rate; u32 gcd; u8 scd; if (symbol_rate > (state->clk * 3) / 16) scd = 0; else if (symbol_rate > (state->clk * 3) / 32) scd = 1; else if (symbol_rate > (state->clk * 3) / 64) scd = 2; else scd = 3; clk = scd ? (state->clk / (scd * 2)) : state->clk; /* * Viterbi decoder: * Differential decoding off * Spectral inversion unknown * QPSK modulation */ if (inversion == INVERSION_ON) buf[0] = 0x60; else if (inversion == INVERSION_OFF) buf[0] = 0x20; else buf[0] = 0x00; /* * CLK ratio: * system clock frequency is up to 64 or 96 MHz * * formula: * r = k * clk / symbol_rate * * k: 2^21 for caa 0..3, * 2^20 for caa 4..5, * 2^19 for caa 6..7 */ if (symbol_rate <= (clk * 3) / 32) k = (1 << 19); else if (symbol_rate <= (clk * 3) / 16) k = (1 << 20); else k = (1 << 21); gcd = tda80xx_gcd(clk, sr); clk /= gcd; sr /= gcd; gcd = tda80xx_gcd(k, sr); k /= gcd; sr /= gcd; ratio = (u64)k * (u64)clk; do_div(ratio, sr); buf[1] = ratio >> 16; buf[2] = ratio >> 8; buf[3] = ratio; /* nyquist filter roll-off factor 35% */ buf[4] = 0x20; clk = scd ? (state->clk / (scd * 2)) : state->clk; /* Anti Alias Filter */ if (symbol_rate < (clk * 3) / 64) printk("tda80xx: unsupported symbol rate: %u\n", symbol_rate); else if (symbol_rate <= clk / 16) buf[4] |= 0x07; else if (symbol_rate <= (clk * 3) / 32) buf[4] |= 0x06; else if (symbol_rate <= clk / 8) buf[4] |= 0x05; else if (symbol_rate <= (clk * 3) / 16) buf[4] |= 0x04; else if (symbol_rate <= clk / 4) buf[4] |= 0x03; else if (symbol_rate <= (clk * 3) / 8) buf[4] |= 0x02; else if (symbol_rate <= clk / 2) buf[4] |= 0x01; else buf[4] |= 0x00; /* Sigma Delta converter */ buf[5] = 0x00; /* FEC: Possible puncturing rates */ if (fec_inner == FEC_NONE) buf[6] = 0x00; else if ((fec_inner >= FEC_1_2) && (fec_inner <= FEC_8_9)) buf[6] = (1 << (8 - fec_inner)); else if (fec_inner == FEC_AUTO) buf[6] = 0xff; else return -EINVAL; /* carrier lock detector threshold value */ buf[7] = 0x30; /* AFC1: proportional part settings */ buf[8] = 0x42; /* AFC1: integral part settings */ buf[9] = 0x98; /* PD: Leaky integrator SCPC mode */ buf[10] = 0x28; /* AFC2, AFC1 controls */ buf[11] = 0x30; /* PD: proportional part settings */ buf[12] = 0x42; /* PD: integral part settings */ buf[13] = 0x99; /* AGC */ buf[14] = 0x50 | scd; printk("symbol_rate=%u clk=%u\n", symbol_rate, clk); return tda80xx_write(state, 0x01, buf, sizeof(buf));}static int tda80xx_set_clk(struct tda80xx_state* state){ u8 buf[2]; /* CLK proportional part */ buf[0] = (0x06 << 5) | 0x08; /* CMP[2:0], CSP[4:0] */ /* CLK integral part */ buf[1] = (0x04 << 5) | 0x1a; /* CMI[2:0], CSI[4:0] */ return tda80xx_write(state, 0x17, buf, sizeof(buf));}#if 0static int tda80xx_set_scpc_freq_offset(struct tda80xx_state* state){ /* a constant value is nonsense here imho */ return tda80xx_writereg(state, 0x22, 0xf9);}#endifstatic int tda80xx_close_loop(struct tda80xx_state* state){ u8 buf[2]; /* PD: Loop closed, LD: lock detect enable, SCPC: Sweep mode - AFC1 loop closed */ buf[0] = 0x68; /* AFC1: Loop closed, CAR Feedback: 8192 */ buf[1] = 0x70; return tda80xx_write(state, 0x0b, buf, sizeof(buf));}static irqreturn_t tda80xx_irq(int irq, void *priv, struct pt_regs *pt){ schedule_work(priv); return IRQ_HANDLED;}static void tda80xx_read_status_int(struct tda80xx_state* state){ u8 val; static const fe_spectral_inversion_t inv_tab[] = { INVERSION_OFF, INVERSION_ON }; static const fe_code_rate_t fec_tab[] = { FEC_8_9, FEC_1_2, FEC_2_3, FEC_3_4, FEC_4_5, FEC_5_6, FEC_6_7, FEC_7_8, }; val = tda80xx_readreg(state, 0x02); state->status = 0; if (val & 0x01) /* demodulator lock */ state->status |= FE_HAS_SIGNAL; if (val & 0x02) /* clock recovery lock */ state->status |= FE_HAS_CARRIER; if (val & 0x04) /* viterbi lock */ state->status |= FE_HAS_VITERBI; if (val & 0x08) /* deinterleaver lock (packet sync) */ state->status |= FE_HAS_SYNC; if (val & 0x10) /* derandomizer lock (frame sync) */ state->status |= FE_HAS_LOCK; if (val & 0x20) /* frontend can not lock */ state->status |= FE_TIMEDOUT; if ((state->status & (FE_HAS_CARRIER)) && (state->afc_loop)) { printk("tda80xx: closing loop\n"); tda80xx_close_loop(state); state->afc_loop = 0; } if (state->status & (FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK)) { val = tda80xx_readreg(state, 0x0e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -