cx24116.c
来自「trident tm5600的linux驱动」· C语言 代码 · 共 1,488 行 · 第 1/3 页
C
1,488 行
/* Conexant cx24116/cx24118 - DVBS/S2 Satellite demod/tuner driver Copyright (C) 2006-2008 Steven Toth <stoth@hauppauge.com> Copyright (C) 2006-2007 Georg Acher Copyright (C) 2007-2008 Darron Broad March 2007 Fixed some bugs. Added diseqc support. Added corrected signal strength support. August 2007 Sync with legacy version. Some clean ups. Copyright (C) 2008 Igor Liplianin September, 9th 2008 Fixed locking on high symbol rates (>30000). Implement MPEG initialization parameter. 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/slab.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/init.h>#include <linux/firmware.h>#include "dvb_frontend.h"#include "cx24116.h"static int debug;module_param(debug, int, 0644);MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");#define dprintk(args...) \ do { \ if (debug) \ printk(KERN_INFO "cx24116: " args); \ } while (0)#define CX24116_DEFAULT_FIRMWARE "dvb-fe-cx24116.fw"#define CX24116_SEARCH_RANGE_KHZ 5000/* known registers */#define CX24116_REG_COMMAND (0x00) /* command args 0x00..0x1e */#define CX24116_REG_EXECUTE (0x1f) /* execute command */#define CX24116_REG_MAILBOX (0x96) /* FW or multipurpose mailbox? */#define CX24116_REG_RESET (0x20) /* reset status > 0 */#define CX24116_REG_SIGNAL (0x9e) /* signal low */#define CX24116_REG_SSTATUS (0x9d) /* signal high / status */#define CX24116_REG_QUALITY8 (0xa3)#define CX24116_REG_QSTATUS (0xbc)#define CX24116_REG_QUALITY0 (0xd5)#define CX24116_REG_BER0 (0xc9)#define CX24116_REG_BER8 (0xc8)#define CX24116_REG_BER16 (0xc7)#define CX24116_REG_BER24 (0xc6)#define CX24116_REG_UCB0 (0xcb)#define CX24116_REG_UCB8 (0xca)#define CX24116_REG_CLKDIV (0xf3)#define CX24116_REG_RATEDIV (0xf9)/* configured fec (not tuned) or actual FEC (tuned) 1=1/2 2=2/3 etc */#define CX24116_REG_FECSTATUS (0x9c)/* FECSTATUS bits *//* mask to determine configured fec (not tuned) or actual fec (tuned) */#define CX24116_FEC_FECMASK (0x1f)/* Select DVB-S demodulator, else DVB-S2 */#define CX24116_FEC_DVBS (0x20)#define CX24116_FEC_UNKNOWN (0x40) /* Unknown/unused *//* Pilot mode requested when tuning else always reset when tuned */#define CX24116_FEC_PILOT (0x80)/* arg buffer size */#define CX24116_ARGLEN (0x1e)/* rolloff */#define CX24116_ROLLOFF_020 (0x00)#define CX24116_ROLLOFF_025 (0x01)#define CX24116_ROLLOFF_035 (0x02)/* pilot bit */#define CX24116_PILOT_OFF (0x00)#define CX24116_PILOT_ON (0x40)/* signal status */#define CX24116_HAS_SIGNAL (0x01)#define CX24116_HAS_CARRIER (0x02)#define CX24116_HAS_VITERBI (0x04)#define CX24116_HAS_SYNCLOCK (0x08)#define CX24116_HAS_UNKNOWN1 (0x10)#define CX24116_HAS_UNKNOWN2 (0x20)#define CX24116_STATUS_MASK (0x3f)#define CX24116_SIGNAL_MASK (0xc0)#define CX24116_DISEQC_TONEOFF (0) /* toneburst never sent */#define CX24116_DISEQC_TONECACHE (1) /* toneburst cached */#define CX24116_DISEQC_MESGCACHE (2) /* message cached *//* arg offset for DiSEqC */#define CX24116_DISEQC_BURST (1)#define CX24116_DISEQC_ARG2_2 (2) /* unknown value=2 */#define CX24116_DISEQC_ARG3_0 (3) /* unknown value=0 */#define CX24116_DISEQC_ARG4_0 (4) /* unknown value=0 */#define CX24116_DISEQC_MSGLEN (5)#define CX24116_DISEQC_MSGOFS (6)/* DiSEqC burst */#define CX24116_DISEQC_MINI_A (0)#define CX24116_DISEQC_MINI_B (1)/* DiSEqC tone burst */static int toneburst = 1;module_param(toneburst, int, 0644);MODULE_PARM_DESC(toneburst, "DiSEqC toneburst 0=OFF, 1=TONE CACHE, "\ "2=MESSAGE CACHE (default:1)");/* SNR measurements */static int esno_snr;module_param(esno_snr, int, 0644);MODULE_PARM_DESC(debug, "SNR return units, 0=PERCENTAGE 0-100, "\ "1=ESNO(db * 10) (default:0)");enum cmds { CMD_SET_VCO = 0x10, CMD_TUNEREQUEST = 0x11, CMD_MPEGCONFIG = 0x13, CMD_TUNERINIT = 0x14, CMD_BANDWIDTH = 0x15, CMD_GETAGC = 0x19, CMD_LNBCONFIG = 0x20, CMD_LNBSEND = 0x21, /* Formerly CMD_SEND_DISEQC */ CMD_SET_TONEPRE = 0x22, CMD_SET_TONE = 0x23, CMD_UPDFWVERS = 0x35, CMD_TUNERSLEEP = 0x36, CMD_AGCCONTROL = 0x3b, /* Unknown */};/* The Demod/Tuner can't easily provide these, we cache them */struct cx24116_tuning { u32 frequency; u32 symbol_rate; fe_spectral_inversion_t inversion; fe_code_rate_t fec; fe_modulation_t modulation; fe_pilot_t pilot; fe_rolloff_t rolloff; /* Demod values */ u8 fec_val; u8 fec_mask; u8 inversion_val; u8 pilot_val; u8 rolloff_val;};/* Basic commands that are sent to the firmware */struct cx24116_cmd { u8 len; u8 args[CX24116_ARGLEN];};struct cx24116_state { struct i2c_adapter *i2c; const struct cx24116_config *config; struct dvb_frontend frontend; struct cx24116_tuning dcur; struct cx24116_tuning dnxt; u8 skip_fw_load; u8 burst; struct cx24116_cmd dsec_cmd;};static int cx24116_writereg(struct cx24116_state *state, int reg, int data){ u8 buf[] = { reg, data }; struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 }; int err; if (debug > 1) printk("cx24116: %s: write reg 0x%02x, value 0x%02x\n", __func__, reg, data); err = i2c_transfer(state->i2c, &msg, 1); if (err != 1) { printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x," " value == 0x%02x)\n", __func__, err, reg, data); return -EREMOTEIO; } return 0;}/* Bulk byte writes to a single I2C address, for 32k firmware load */static int cx24116_writeregN(struct cx24116_state *state, int reg, const u8 *data, u16 len){ int ret = -EREMOTEIO; struct i2c_msg msg; u8 *buf; buf = kmalloc(len + 1, GFP_KERNEL); if (buf == NULL) { printk("Unable to kmalloc\n"); ret = -ENOMEM; goto error; } *(buf) = reg; memcpy(buf + 1, data, len); msg.addr = state->config->demod_address; msg.flags = 0; msg.buf = buf; msg.len = len + 1; if (debug > 1) printk(KERN_INFO "cx24116: %s: write regN 0x%02x, len = %d\n", __func__, reg, len); ret = i2c_transfer(state->i2c, &msg, 1); if (ret != 1) { printk(KERN_ERR "%s: writereg error(err == %i, reg == 0x%02x\n", __func__, ret, reg); ret = -EREMOTEIO; }error: kfree(buf); return ret;}static int cx24116_readreg(struct cx24116_state *state, u8 reg){ int ret; u8 b0[] = { reg }; u8 b1[] = { 0 }; struct i2c_msg msg[] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 }, { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } }; ret = i2c_transfer(state->i2c, msg, 2); if (ret != 2) { printk(KERN_ERR "%s: reg=0x%x (error=%d)\n", __func__, reg, ret); return ret; } if (debug > 1) printk(KERN_INFO "cx24116: read reg 0x%02x, value 0x%02x\n", reg, b1[0]); return b1[0];}static int cx24116_set_inversion(struct cx24116_state *state, fe_spectral_inversion_t inversion){ dprintk("%s(%d)\n", __func__, inversion); switch (inversion) { case INVERSION_OFF: state->dnxt.inversion_val = 0x00; break; case INVERSION_ON: state->dnxt.inversion_val = 0x04; break; case INVERSION_AUTO: state->dnxt.inversion_val = 0x0C; break; default: return -EINVAL; } state->dnxt.inversion = inversion; return 0;}/* * modfec (modulation and FEC) * =========================== * * MOD FEC mask/val standard * ---- -------- ----------- -------- * QPSK FEC_1_2 0x02 0x02+X DVB-S * QPSK FEC_2_3 0x04 0x02+X DVB-S * QPSK FEC_3_4 0x08 0x02+X DVB-S * QPSK FEC_4_5 0x10 0x02+X DVB-S (?) * QPSK FEC_5_6 0x20 0x02+X DVB-S * QPSK FEC_6_7 0x40 0x02+X DVB-S * QPSK FEC_7_8 0x80 0x02+X DVB-S * QPSK FEC_8_9 0x01 0x02+X DVB-S (?) (NOT SUPPORTED?) * QPSK AUTO 0xff 0x02+X DVB-S * * For DVB-S high byte probably represents FEC * and low byte selects the modulator. The high * byte is search range mask. Bit 5 may turn * on DVB-S and remaining bits represent some * kind of calibration (how/what i do not know). * * Eg.(2/3) szap "Zone Horror" * * mask/val = 0x04, 0x20 * status 1f | signal c3c0 | snr a333 | ber 00000098 | unc 0 | FE_HAS_LOCK * * mask/val = 0x04, 0x30 * status 1f | signal c3c0 | snr a333 | ber 00000000 | unc 0 | FE_HAS_LOCK * * After tuning FECSTATUS contains actual FEC * in use numbered 1 through to 8 for 1/2 .. 2/3 etc * * NBC=NOT/NON BACKWARD COMPATIBLE WITH DVB-S (DVB-S2 only) * * NBC-QPSK FEC_1_2 0x00, 0x04 DVB-S2 * NBC-QPSK FEC_3_5 0x00, 0x05 DVB-S2 * NBC-QPSK FEC_2_3 0x00, 0x06 DVB-S2 * NBC-QPSK FEC_3_4 0x00, 0x07 DVB-S2 * NBC-QPSK FEC_4_5 0x00, 0x08 DVB-S2 * NBC-QPSK FEC_5_6 0x00, 0x09 DVB-S2 * NBC-QPSK FEC_8_9 0x00, 0x0a DVB-S2 * NBC-QPSK FEC_9_10 0x00, 0x0b DVB-S2 * * NBC-8PSK FEC_3_5 0x00, 0x0c DVB-S2 * NBC-8PSK FEC_2_3 0x00, 0x0d DVB-S2 * NBC-8PSK FEC_3_4 0x00, 0x0e DVB-S2 * NBC-8PSK FEC_5_6 0x00, 0x0f DVB-S2 * NBC-8PSK FEC_8_9 0x00, 0x10 DVB-S2 * NBC-8PSK FEC_9_10 0x00, 0x11 DVB-S2 * * For DVB-S2 low bytes selects both modulator * and FEC. High byte is meaningless here. To * set pilot, bit 6 (0x40) is set. When inspecting * FECSTATUS bit 7 (0x80) represents the pilot * selection whilst not tuned. When tuned, actual FEC * in use is found in FECSTATUS as per above. Pilot * value is reset. *//* A table of modulation, fec and configuration bytes for the demod. * Not all S2 mmodulation schemes are support and not all rates with * a scheme are support. Especially, no auto detect when in S2 mode. */struct cx24116_modfec { fe_delivery_system_t delivery_system; fe_modulation_t modulation; fe_code_rate_t fec; u8 mask; /* In DVBS mode this is used to autodetect */ u8 val; /* Passed to the firmware to indicate mode selection */} CX24116_MODFEC_MODES[] = { /* QPSK. For unknown rates we set hardware to auto detect 0xfe 0x30 */ /*mod fec mask val */ { SYS_DVBS, QPSK, FEC_NONE, 0xfe, 0x30 }, { SYS_DVBS, QPSK, FEC_1_2, 0x02, 0x2e }, /* 00000010 00101110 */ { SYS_DVBS, QPSK, FEC_2_3, 0x04, 0x2f }, /* 00000100 00101111 */ { SYS_DVBS, QPSK, FEC_3_4, 0x08, 0x30 }, /* 00001000 00110000 */ { SYS_DVBS, QPSK, FEC_4_5, 0xfe, 0x30 }, /* 000?0000 ? */ { SYS_DVBS, QPSK, FEC_5_6, 0x20, 0x31 }, /* 00100000 00110001 */ { SYS_DVBS, QPSK, FEC_6_7, 0xfe, 0x30 }, /* 0?000000 ? */ { SYS_DVBS, QPSK, FEC_7_8, 0x80, 0x32 }, /* 10000000 00110010 */ { SYS_DVBS, QPSK, FEC_8_9, 0xfe, 0x30 }, /* 0000000? ? */ { SYS_DVBS, QPSK, FEC_AUTO, 0xfe, 0x30 }, /* NBC-QPSK */ { SYS_DVBS2, QPSK, FEC_1_2, 0x00, 0x04 }, { SYS_DVBS2, QPSK, FEC_3_5, 0x00, 0x05 }, { SYS_DVBS2, QPSK, FEC_2_3, 0x00, 0x06 }, { SYS_DVBS2, QPSK, FEC_3_4, 0x00, 0x07 }, { SYS_DVBS2, QPSK, FEC_4_5, 0x00, 0x08 }, { SYS_DVBS2, QPSK, FEC_5_6, 0x00, 0x09 }, { SYS_DVBS2, QPSK, FEC_8_9, 0x00, 0x0a }, { SYS_DVBS2, QPSK, FEC_9_10, 0x00, 0x0b }, /* 8PSK */ { SYS_DVBS2, PSK_8, FEC_3_5, 0x00, 0x0c }, { SYS_DVBS2, PSK_8, FEC_2_3, 0x00, 0x0d }, { SYS_DVBS2, PSK_8, FEC_3_4, 0x00, 0x0e }, { SYS_DVBS2, PSK_8, FEC_5_6, 0x00, 0x0f }, { SYS_DVBS2, PSK_8, FEC_8_9, 0x00, 0x10 }, { SYS_DVBS2, PSK_8, FEC_9_10, 0x00, 0x11 }, /* * `val' can be found in the FECSTATUS register when tuning. * FECSTATUS will give the actual FEC in use if tuning was successful. */};static int cx24116_lookup_fecmod(struct cx24116_state *state, fe_modulation_t m, fe_code_rate_t f){ int i, ret = -EOPNOTSUPP; dprintk("%s(0x%02x,0x%02x)\n", __func__, m, f); for (i = 0; i < ARRAY_SIZE(CX24116_MODFEC_MODES); i++) { if ((m == CX24116_MODFEC_MODES[i].modulation) && (f == CX24116_MODFEC_MODES[i].fec)) { ret = i; break; } } return ret;}static int cx24116_set_fec(struct cx24116_state *state, fe_modulation_t mod, fe_code_rate_t fec){ int ret = 0; dprintk("%s(0x%02x,0x%02x)\n", __func__, mod, fec); ret = cx24116_lookup_fecmod(state, mod, fec); if (ret < 0) return ret; state->dnxt.fec = fec; state->dnxt.fec_val = CX24116_MODFEC_MODES[ret].val; state->dnxt.fec_mask = CX24116_MODFEC_MODES[ret].mask; dprintk("%s() mask/val = 0x%02x/0x%02x\n", __func__, state->dnxt.fec_mask, state->dnxt.fec_val); return 0;}static int cx24116_set_symbolrate(struct cx24116_state *state, u32 rate){ dprintk("%s(%d)\n", __func__, rate); /* check if symbol rate is within limits */ if ((rate > state->frontend.ops.info.symbol_rate_max) || (rate < state->frontend.ops.info.symbol_rate_min)) { dprintk("%s() unsupported symbol_rate = %d\n", __func__, rate); return -EOPNOTSUPP; } state->dnxt.symbol_rate = rate; dprintk("%s() symbol_rate = %d\n", __func__, rate); return 0;}static int cx24116_load_firmware(struct dvb_frontend *fe, const struct firmware *fw);static int cx24116_firmware_ondemand(struct dvb_frontend *fe){ struct cx24116_state *state = fe->demodulator_priv; const struct firmware *fw; int ret = 0; dprintk("%s()\n", __func__); if (cx24116_readreg(state, 0x20) > 0) { if (state->skip_fw_load) return 0; /* Load firmware */ /* request the firmware, this will block until loaded */ printk(KERN_INFO "%s: Waiting for firmware upload (%s)...\n", __func__, CX24116_DEFAULT_FIRMWARE); ret = request_firmware(&fw, CX24116_DEFAULT_FIRMWARE, &state->i2c->dev); printk(KERN_INFO "%s: Waiting for firmware upload(2)...\n", __func__); if (ret) { printk(KERN_ERR "%s: No firmware uploaded " "(timeout or file not found?)\n", __func__); return ret;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?