Path: blob/master/drivers/media/dvb/frontends/cx24123.c
15112 views
/*1* Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver2*3* Copyright (C) 2005 Steven Toth <[email protected]>4*5* Support for KWorld DVB-S 100 by Vadim Catana <[email protected]>6*7* Support for CX24123/CX24113-NIM by Patrick Boettcher <[email protected]>8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License as11* published by the Free Software Foundation; either version 2 of12* the License, or (at your option) any later version.13*14* This program is distributed in the hope that it will be useful,15* but WITHOUT ANY WARRANTY; without even the implied warranty of16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17* General Public License for more details.18*19* You should have received a copy of the GNU General Public License20* along with this program; if not, write to the Free Software21* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.22*/2324#include <linux/slab.h>25#include <linux/kernel.h>26#include <linux/module.h>27#include <linux/init.h>2829#include "dvb_frontend.h"30#include "cx24123.h"3132#define XTAL 101110003334static int force_band;35module_param(force_band, int, 0644);36MODULE_PARM_DESC(force_band, "Force a specific band select "\37"(1-9, default:off).");3839static int debug;40module_param(debug, int, 0644);41MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");4243#define info(args...) do { printk(KERN_INFO "CX24123: " args); } while (0)44#define err(args...) do { printk(KERN_ERR "CX24123: " args); } while (0)4546#define dprintk(args...) \47do { \48if (debug) { \49printk(KERN_DEBUG "CX24123: %s: ", __func__); \50printk(args); \51} \52} while (0)5354struct cx24123_state {55struct i2c_adapter *i2c;56const struct cx24123_config *config;5758struct dvb_frontend frontend;5960/* Some PLL specifics for tuning */61u32 VCAarg;62u32 VGAarg;63u32 bandselectarg;64u32 pllarg;65u32 FILTune;6667struct i2c_adapter tuner_i2c_adapter;6869u8 demod_rev;7071/* The Demod/Tuner can't easily provide these, we cache them */72u32 currentfreq;73u32 currentsymbolrate;74};7576/* Various tuner defaults need to be established for a given symbol rate Sps */77static struct cx24123_AGC_val {78u32 symbolrate_low;79u32 symbolrate_high;80u32 VCAprogdata;81u32 VGAprogdata;82u32 FILTune;83} cx24123_AGC_vals[] =84{85{86.symbolrate_low = 1000000,87.symbolrate_high = 4999999,88/* the specs recommend other values for VGA offsets,89but tests show they are wrong */90.VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,91.VCAprogdata = (2 << 19) | (0x07 << 9) | 0x07,92.FILTune = 0x27f /* 0.41 V */93},94{95.symbolrate_low = 5000000,96.symbolrate_high = 14999999,97.VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,98.VCAprogdata = (2 << 19) | (0x07 << 9) | 0x1f,99.FILTune = 0x317 /* 0.90 V */100},101{102.symbolrate_low = 15000000,103.symbolrate_high = 45000000,104.VGAprogdata = (1 << 19) | (0x100 << 9) | 0x180,105.VCAprogdata = (2 << 19) | (0x07 << 9) | 0x3f,106.FILTune = 0x145 /* 2.70 V */107},108};109110/*111* Various tuner defaults need to be established for a given frequency kHz.112* fixme: The bounds on the bands do not match the doc in real life.113* fixme: Some of them have been moved, other might need adjustment.114*/115static struct cx24123_bandselect_val {116u32 freq_low;117u32 freq_high;118u32 VCOdivider;119u32 progdata;120} cx24123_bandselect_vals[] =121{122/* band 1 */123{124.freq_low = 950000,125.freq_high = 1074999,126.VCOdivider = 4,127.progdata = (0 << 19) | (0 << 9) | 0x40,128},129130/* band 2 */131{132.freq_low = 1075000,133.freq_high = 1177999,134.VCOdivider = 4,135.progdata = (0 << 19) | (0 << 9) | 0x80,136},137138/* band 3 */139{140.freq_low = 1178000,141.freq_high = 1295999,142.VCOdivider = 2,143.progdata = (0 << 19) | (1 << 9) | 0x01,144},145146/* band 4 */147{148.freq_low = 1296000,149.freq_high = 1431999,150.VCOdivider = 2,151.progdata = (0 << 19) | (1 << 9) | 0x02,152},153154/* band 5 */155{156.freq_low = 1432000,157.freq_high = 1575999,158.VCOdivider = 2,159.progdata = (0 << 19) | (1 << 9) | 0x04,160},161162/* band 6 */163{164.freq_low = 1576000,165.freq_high = 1717999,166.VCOdivider = 2,167.progdata = (0 << 19) | (1 << 9) | 0x08,168},169170/* band 7 */171{172.freq_low = 1718000,173.freq_high = 1855999,174.VCOdivider = 2,175.progdata = (0 << 19) | (1 << 9) | 0x10,176},177178/* band 8 */179{180.freq_low = 1856000,181.freq_high = 2035999,182.VCOdivider = 2,183.progdata = (0 << 19) | (1 << 9) | 0x20,184},185186/* band 9 */187{188.freq_low = 2036000,189.freq_high = 2150000,190.VCOdivider = 2,191.progdata = (0 << 19) | (1 << 9) | 0x40,192},193};194195static struct {196u8 reg;197u8 data;198} cx24123_regdata[] =199{200{0x00, 0x03}, /* Reset system */201{0x00, 0x00}, /* Clear reset */202{0x03, 0x07}, /* QPSK, DVB, Auto Acquisition (default) */203{0x04, 0x10}, /* MPEG */204{0x05, 0x04}, /* MPEG */205{0x06, 0x31}, /* MPEG (default) */206{0x0b, 0x00}, /* Freq search start point (default) */207{0x0c, 0x00}, /* Demodulator sample gain (default) */208{0x0d, 0x7f}, /* Force driver to shift until the maximum (+-10 MHz) */209{0x0e, 0x03}, /* Default non-inverted, FEC 3/4 (default) */210{0x0f, 0xfe}, /* FEC search mask (all supported codes) */211{0x10, 0x01}, /* Default search inversion, no repeat (default) */212{0x16, 0x00}, /* Enable reading of frequency */213{0x17, 0x01}, /* Enable EsNO Ready Counter */214{0x1c, 0x80}, /* Enable error counter */215{0x20, 0x00}, /* Tuner burst clock rate = 500KHz */216{0x21, 0x15}, /* Tuner burst mode, word length = 0x15 */217{0x28, 0x00}, /* Enable FILTERV with positive pol., DiSEqC 2.x off */218{0x29, 0x00}, /* DiSEqC LNB_DC off */219{0x2a, 0xb0}, /* DiSEqC Parameters (default) */220{0x2b, 0x73}, /* DiSEqC Tone Frequency (default) */221{0x2c, 0x00}, /* DiSEqC Message (0x2c - 0x31) */222{0x2d, 0x00},223{0x2e, 0x00},224{0x2f, 0x00},225{0x30, 0x00},226{0x31, 0x00},227{0x32, 0x8c}, /* DiSEqC Parameters (default) */228{0x33, 0x00}, /* Interrupts off (0x33 - 0x34) */229{0x34, 0x00},230{0x35, 0x03}, /* DiSEqC Tone Amplitude (default) */231{0x36, 0x02}, /* DiSEqC Parameters (default) */232{0x37, 0x3a}, /* DiSEqC Parameters (default) */233{0x3a, 0x00}, /* Enable AGC accumulator (for signal strength) */234{0x44, 0x00}, /* Constellation (default) */235{0x45, 0x00}, /* Symbol count (default) */236{0x46, 0x0d}, /* Symbol rate estimator on (default) */237{0x56, 0xc1}, /* Error Counter = Viterbi BER */238{0x57, 0xff}, /* Error Counter Window (default) */239{0x5c, 0x20}, /* Acquisition AFC Expiration window (default is 0x10) */240{0x67, 0x83}, /* Non-DCII symbol clock */241};242243static int cx24123_i2c_writereg(struct cx24123_state *state,244u8 i2c_addr, int reg, int data)245{246u8 buf[] = { reg, data };247struct i2c_msg msg = {248.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2249};250int err;251252/* printk(KERN_DEBUG "wr(%02x): %02x %02x\n", i2c_addr, reg, data); */253254err = i2c_transfer(state->i2c, &msg, 1);255if (err != 1) {256printk("%s: writereg error(err == %i, reg == 0x%02x,"257" data == 0x%02x)\n", __func__, err, reg, data);258return err;259}260261return 0;262}263264static int cx24123_i2c_readreg(struct cx24123_state *state, u8 i2c_addr, u8 reg)265{266int ret;267u8 b = 0;268struct i2c_msg msg[] = {269{ .addr = i2c_addr, .flags = 0, .buf = ®, .len = 1 },270{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &b, .len = 1 }271};272273ret = i2c_transfer(state->i2c, msg, 2);274275if (ret != 2) {276err("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);277return ret;278}279280/* printk(KERN_DEBUG "rd(%02x): %02x %02x\n", i2c_addr, reg, b); */281282return b;283}284285#define cx24123_readreg(state, reg) \286cx24123_i2c_readreg(state, state->config->demod_address, reg)287#define cx24123_writereg(state, reg, val) \288cx24123_i2c_writereg(state, state->config->demod_address, reg, val)289290static int cx24123_set_inversion(struct cx24123_state *state,291fe_spectral_inversion_t inversion)292{293u8 nom_reg = cx24123_readreg(state, 0x0e);294u8 auto_reg = cx24123_readreg(state, 0x10);295296switch (inversion) {297case INVERSION_OFF:298dprintk("inversion off\n");299cx24123_writereg(state, 0x0e, nom_reg & ~0x80);300cx24123_writereg(state, 0x10, auto_reg | 0x80);301break;302case INVERSION_ON:303dprintk("inversion on\n");304cx24123_writereg(state, 0x0e, nom_reg | 0x80);305cx24123_writereg(state, 0x10, auto_reg | 0x80);306break;307case INVERSION_AUTO:308dprintk("inversion auto\n");309cx24123_writereg(state, 0x10, auto_reg & ~0x80);310break;311default:312return -EINVAL;313}314315return 0;316}317318static int cx24123_get_inversion(struct cx24123_state *state,319fe_spectral_inversion_t *inversion)320{321u8 val;322323val = cx24123_readreg(state, 0x1b) >> 7;324325if (val == 0) {326dprintk("read inversion off\n");327*inversion = INVERSION_OFF;328} else {329dprintk("read inversion on\n");330*inversion = INVERSION_ON;331}332333return 0;334}335336static int cx24123_set_fec(struct cx24123_state *state, fe_code_rate_t fec)337{338u8 nom_reg = cx24123_readreg(state, 0x0e) & ~0x07;339340if ((fec < FEC_NONE) || (fec > FEC_AUTO))341fec = FEC_AUTO;342343/* Set the soft decision threshold */344if (fec == FEC_1_2)345cx24123_writereg(state, 0x43,346cx24123_readreg(state, 0x43) | 0x01);347else348cx24123_writereg(state, 0x43,349cx24123_readreg(state, 0x43) & ~0x01);350351switch (fec) {352case FEC_1_2:353dprintk("set FEC to 1/2\n");354cx24123_writereg(state, 0x0e, nom_reg | 0x01);355cx24123_writereg(state, 0x0f, 0x02);356break;357case FEC_2_3:358dprintk("set FEC to 2/3\n");359cx24123_writereg(state, 0x0e, nom_reg | 0x02);360cx24123_writereg(state, 0x0f, 0x04);361break;362case FEC_3_4:363dprintk("set FEC to 3/4\n");364cx24123_writereg(state, 0x0e, nom_reg | 0x03);365cx24123_writereg(state, 0x0f, 0x08);366break;367case FEC_4_5:368dprintk("set FEC to 4/5\n");369cx24123_writereg(state, 0x0e, nom_reg | 0x04);370cx24123_writereg(state, 0x0f, 0x10);371break;372case FEC_5_6:373dprintk("set FEC to 5/6\n");374cx24123_writereg(state, 0x0e, nom_reg | 0x05);375cx24123_writereg(state, 0x0f, 0x20);376break;377case FEC_6_7:378dprintk("set FEC to 6/7\n");379cx24123_writereg(state, 0x0e, nom_reg | 0x06);380cx24123_writereg(state, 0x0f, 0x40);381break;382case FEC_7_8:383dprintk("set FEC to 7/8\n");384cx24123_writereg(state, 0x0e, nom_reg | 0x07);385cx24123_writereg(state, 0x0f, 0x80);386break;387case FEC_AUTO:388dprintk("set FEC to auto\n");389cx24123_writereg(state, 0x0f, 0xfe);390break;391default:392return -EOPNOTSUPP;393}394395return 0;396}397398static int cx24123_get_fec(struct cx24123_state *state, fe_code_rate_t *fec)399{400int ret;401402ret = cx24123_readreg(state, 0x1b);403if (ret < 0)404return ret;405ret = ret & 0x07;406407switch (ret) {408case 1:409*fec = FEC_1_2;410break;411case 2:412*fec = FEC_2_3;413break;414case 3:415*fec = FEC_3_4;416break;417case 4:418*fec = FEC_4_5;419break;420case 5:421*fec = FEC_5_6;422break;423case 6:424*fec = FEC_6_7;425break;426case 7:427*fec = FEC_7_8;428break;429default:430/* this can happen when there's no lock */431*fec = FEC_NONE;432}433434return 0;435}436437/* Approximation of closest integer of log2(a/b). It actually gives the438lowest integer i such that 2^i >= round(a/b) */439static u32 cx24123_int_log2(u32 a, u32 b)440{441u32 exp, nearest = 0;442u32 div = a / b;443if (a % b >= b / 2)444++div;445if (div < (1 << 31)) {446for (exp = 1; div > exp; nearest++)447exp += exp;448}449return nearest;450}451452static int cx24123_set_symbolrate(struct cx24123_state *state, u32 srate)453{454u32 tmp, sample_rate, ratio, sample_gain;455u8 pll_mult;456457/* check if symbol rate is within limits */458if ((srate > state->frontend.ops.info.symbol_rate_max) ||459(srate < state->frontend.ops.info.symbol_rate_min))460return -EOPNOTSUPP;461462/* choose the sampling rate high enough for the required operation,463while optimizing the power consumed by the demodulator */464if (srate < (XTAL*2)/2)465pll_mult = 2;466else if (srate < (XTAL*3)/2)467pll_mult = 3;468else if (srate < (XTAL*4)/2)469pll_mult = 4;470else if (srate < (XTAL*5)/2)471pll_mult = 5;472else if (srate < (XTAL*6)/2)473pll_mult = 6;474else if (srate < (XTAL*7)/2)475pll_mult = 7;476else if (srate < (XTAL*8)/2)477pll_mult = 8;478else479pll_mult = 9;480481482sample_rate = pll_mult * XTAL;483484/*485SYSSymbolRate[21:0] = (srate << 23) / sample_rate486487We have to use 32 bit unsigned arithmetic without precision loss.488The maximum srate is 45000000 or 0x02AEA540. This number has489only 6 clear bits on top, hence we can shift it left only 6 bits490at a time. Borrowed from cx24110.c491*/492493tmp = srate << 6;494ratio = tmp / sample_rate;495496tmp = (tmp % sample_rate) << 6;497ratio = (ratio << 6) + (tmp / sample_rate);498499tmp = (tmp % sample_rate) << 6;500ratio = (ratio << 6) + (tmp / sample_rate);501502tmp = (tmp % sample_rate) << 5;503ratio = (ratio << 5) + (tmp / sample_rate);504505506cx24123_writereg(state, 0x01, pll_mult * 6);507508cx24123_writereg(state, 0x08, (ratio >> 16) & 0x3f);509cx24123_writereg(state, 0x09, (ratio >> 8) & 0xff);510cx24123_writereg(state, 0x0a, ratio & 0xff);511512/* also set the demodulator sample gain */513sample_gain = cx24123_int_log2(sample_rate, srate);514tmp = cx24123_readreg(state, 0x0c) & ~0xe0;515cx24123_writereg(state, 0x0c, tmp | sample_gain << 5);516517dprintk("srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n",518srate, ratio, sample_rate, sample_gain);519520return 0;521}522523/*524* Based on the required frequency and symbolrate, the tuner AGC has525* to be configured and the correct band selected.526* Calculate those values.527*/528static int cx24123_pll_calculate(struct dvb_frontend *fe,529struct dvb_frontend_parameters *p)530{531struct cx24123_state *state = fe->demodulator_priv;532u32 ndiv = 0, adiv = 0, vco_div = 0;533int i = 0;534int pump = 2;535int band = 0;536int num_bands = ARRAY_SIZE(cx24123_bandselect_vals);537struct cx24123_bandselect_val *bsv = NULL;538struct cx24123_AGC_val *agcv = NULL;539540/* Defaults for low freq, low rate */541state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;542state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;543state->bandselectarg = cx24123_bandselect_vals[0].progdata;544vco_div = cx24123_bandselect_vals[0].VCOdivider;545546/* For the given symbol rate, determine the VCA, VGA and547* FILTUNE programming bits */548for (i = 0; i < ARRAY_SIZE(cx24123_AGC_vals); i++) {549agcv = &cx24123_AGC_vals[i];550if ((agcv->symbolrate_low <= p->u.qpsk.symbol_rate) &&551(agcv->symbolrate_high >= p->u.qpsk.symbol_rate)) {552state->VCAarg = agcv->VCAprogdata;553state->VGAarg = agcv->VGAprogdata;554state->FILTune = agcv->FILTune;555}556}557558/* determine the band to use */559if (force_band < 1 || force_band > num_bands) {560for (i = 0; i < num_bands; i++) {561bsv = &cx24123_bandselect_vals[i];562if ((bsv->freq_low <= p->frequency) &&563(bsv->freq_high >= p->frequency))564band = i;565}566} else567band = force_band - 1;568569state->bandselectarg = cx24123_bandselect_vals[band].progdata;570vco_div = cx24123_bandselect_vals[band].VCOdivider;571572/* determine the charge pump current */573if (p->frequency < (cx24123_bandselect_vals[band].freq_low +574cx24123_bandselect_vals[band].freq_high) / 2)575pump = 0x01;576else577pump = 0x02;578579/* Determine the N/A dividers for the requested lband freq (in kHz). */580/* Note: the reference divider R=10, frequency is in KHz,581* XTAL is in Hz */582ndiv = (((p->frequency * vco_div * 10) /583(2 * XTAL / 1000)) / 32) & 0x1ff;584adiv = (((p->frequency * vco_div * 10) /585(2 * XTAL / 1000)) % 32) & 0x1f;586587if (adiv == 0 && ndiv > 0)588ndiv--;589590/* control bits 11, refdiv 11, charge pump polarity 1,591* charge pump current, ndiv, adiv */592state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) |593(pump << 14) | (ndiv << 5) | adiv;594595return 0;596}597598/*599* Tuner data is 21 bits long, must be left-aligned in data.600* Tuner cx24109 is written through a dedicated 3wire interface601* on the demod chip.602*/603static int cx24123_pll_writereg(struct dvb_frontend *fe,604struct dvb_frontend_parameters *p, u32 data)605{606struct cx24123_state *state = fe->demodulator_priv;607unsigned long timeout;608609dprintk("pll writereg called, data=0x%08x\n", data);610611/* align the 21 bytes into to bit23 boundary */612data = data << 3;613614/* Reset the demod pll word length to 0x15 bits */615cx24123_writereg(state, 0x21, 0x15);616617/* write the msb 8 bits, wait for the send to be completed */618timeout = jiffies + msecs_to_jiffies(40);619cx24123_writereg(state, 0x22, (data >> 16) & 0xff);620while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {621if (time_after(jiffies, timeout)) {622err("%s: demodulator is not responding, "\623"possibly hung, aborting.\n", __func__);624return -EREMOTEIO;625}626msleep(10);627}628629/* send another 8 bytes, wait for the send to be completed */630timeout = jiffies + msecs_to_jiffies(40);631cx24123_writereg(state, 0x22, (data >> 8) & 0xff);632while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {633if (time_after(jiffies, timeout)) {634err("%s: demodulator is not responding, "\635"possibly hung, aborting.\n", __func__);636return -EREMOTEIO;637}638msleep(10);639}640641/* send the lower 5 bits of this byte, padded with 3 LBB,642* wait for the send to be completed */643timeout = jiffies + msecs_to_jiffies(40);644cx24123_writereg(state, 0x22, (data) & 0xff);645while ((cx24123_readreg(state, 0x20) & 0x80)) {646if (time_after(jiffies, timeout)) {647err("%s: demodulator is not responding," \648"possibly hung, aborting.\n", __func__);649return -EREMOTEIO;650}651msleep(10);652}653654/* Trigger the demod to configure the tuner */655cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);656cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);657658return 0;659}660661static int cx24123_pll_tune(struct dvb_frontend *fe,662struct dvb_frontend_parameters *p)663{664struct cx24123_state *state = fe->demodulator_priv;665u8 val;666667dprintk("frequency=%i\n", p->frequency);668669if (cx24123_pll_calculate(fe, p) != 0) {670err("%s: cx24123_pll_calcutate failed\n", __func__);671return -EINVAL;672}673674/* Write the new VCO/VGA */675cx24123_pll_writereg(fe, p, state->VCAarg);676cx24123_pll_writereg(fe, p, state->VGAarg);677678/* Write the new bandselect and pll args */679cx24123_pll_writereg(fe, p, state->bandselectarg);680cx24123_pll_writereg(fe, p, state->pllarg);681682/* set the FILTUNE voltage */683val = cx24123_readreg(state, 0x28) & ~0x3;684cx24123_writereg(state, 0x27, state->FILTune >> 2);685cx24123_writereg(state, 0x28, val | (state->FILTune & 0x3));686687dprintk("pll tune VCA=%d, band=%d, pll=%d\n", state->VCAarg,688state->bandselectarg, state->pllarg);689690return 0;691}692693694/*695* 0x23:696* [7:7] = BTI enabled697* [6:6] = I2C repeater enabled698* [5:5] = I2C repeater start699* [0:0] = BTI start700*/701702/* mode == 1 -> i2c-repeater, 0 -> bti */703static int cx24123_repeater_mode(struct cx24123_state *state, u8 mode, u8 start)704{705u8 r = cx24123_readreg(state, 0x23) & 0x1e;706if (mode)707r |= (1 << 6) | (start << 5);708else709r |= (1 << 7) | (start);710return cx24123_writereg(state, 0x23, r);711}712713static int cx24123_initfe(struct dvb_frontend *fe)714{715struct cx24123_state *state = fe->demodulator_priv;716int i;717718dprintk("init frontend\n");719720/* Configure the demod to a good set of defaults */721for (i = 0; i < ARRAY_SIZE(cx24123_regdata); i++)722cx24123_writereg(state, cx24123_regdata[i].reg,723cx24123_regdata[i].data);724725/* Set the LNB polarity */726if (state->config->lnb_polarity)727cx24123_writereg(state, 0x32,728cx24123_readreg(state, 0x32) | 0x02);729730if (state->config->dont_use_pll)731cx24123_repeater_mode(state, 1, 0);732733return 0;734}735736static int cx24123_set_voltage(struct dvb_frontend *fe,737fe_sec_voltage_t voltage)738{739struct cx24123_state *state = fe->demodulator_priv;740u8 val;741742val = cx24123_readreg(state, 0x29) & ~0x40;743744switch (voltage) {745case SEC_VOLTAGE_13:746dprintk("setting voltage 13V\n");747return cx24123_writereg(state, 0x29, val & 0x7f);748case SEC_VOLTAGE_18:749dprintk("setting voltage 18V\n");750return cx24123_writereg(state, 0x29, val | 0x80);751case SEC_VOLTAGE_OFF:752/* already handled in cx88-dvb */753return 0;754default:755return -EINVAL;756};757758return 0;759}760761/* wait for diseqc queue to become ready (or timeout) */762static void cx24123_wait_for_diseqc(struct cx24123_state *state)763{764unsigned long timeout = jiffies + msecs_to_jiffies(200);765while (!(cx24123_readreg(state, 0x29) & 0x40)) {766if (time_after(jiffies, timeout)) {767err("%s: diseqc queue not ready, " \768"command may be lost.\n", __func__);769break;770}771msleep(10);772}773}774775static int cx24123_send_diseqc_msg(struct dvb_frontend *fe,776struct dvb_diseqc_master_cmd *cmd)777{778struct cx24123_state *state = fe->demodulator_priv;779int i, val, tone;780781dprintk("\n");782783/* stop continuous tone if enabled */784tone = cx24123_readreg(state, 0x29);785if (tone & 0x10)786cx24123_writereg(state, 0x29, tone & ~0x50);787788/* wait for diseqc queue ready */789cx24123_wait_for_diseqc(state);790791/* select tone mode */792cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);793794for (i = 0; i < cmd->msg_len; i++)795cx24123_writereg(state, 0x2C + i, cmd->msg[i]);796797val = cx24123_readreg(state, 0x29);798cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40) |799((cmd->msg_len-3) & 3));800801/* wait for diseqc message to finish sending */802cx24123_wait_for_diseqc(state);803804/* restart continuous tone if enabled */805if (tone & 0x10)806cx24123_writereg(state, 0x29, tone & ~0x40);807808return 0;809}810811static int cx24123_diseqc_send_burst(struct dvb_frontend *fe,812fe_sec_mini_cmd_t burst)813{814struct cx24123_state *state = fe->demodulator_priv;815int val, tone;816817dprintk("\n");818819/* stop continuous tone if enabled */820tone = cx24123_readreg(state, 0x29);821if (tone & 0x10)822cx24123_writereg(state, 0x29, tone & ~0x50);823824/* wait for diseqc queue ready */825cx24123_wait_for_diseqc(state);826827/* select tone mode */828cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) | 0x4);829msleep(30);830val = cx24123_readreg(state, 0x29);831if (burst == SEC_MINI_A)832cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x00));833else if (burst == SEC_MINI_B)834cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x08));835else836return -EINVAL;837838cx24123_wait_for_diseqc(state);839cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);840841/* restart continuous tone if enabled */842if (tone & 0x10)843cx24123_writereg(state, 0x29, tone & ~0x40);844845return 0;846}847848static int cx24123_read_status(struct dvb_frontend *fe, fe_status_t *status)849{850struct cx24123_state *state = fe->demodulator_priv;851int sync = cx24123_readreg(state, 0x14);852853*status = 0;854if (state->config->dont_use_pll) {855u32 tun_status = 0;856if (fe->ops.tuner_ops.get_status)857fe->ops.tuner_ops.get_status(fe, &tun_status);858if (tun_status & TUNER_STATUS_LOCKED)859*status |= FE_HAS_SIGNAL;860} else {861int lock = cx24123_readreg(state, 0x20);862if (lock & 0x01)863*status |= FE_HAS_SIGNAL;864}865866if (sync & 0x02)867*status |= FE_HAS_CARRIER; /* Phase locked */868if (sync & 0x04)869*status |= FE_HAS_VITERBI;870871/* Reed-Solomon Status */872if (sync & 0x08)873*status |= FE_HAS_SYNC;874if (sync & 0x80)875*status |= FE_HAS_LOCK; /*Full Sync */876877return 0;878}879880/*881* Configured to return the measurement of errors in blocks,882* because no UCBLOCKS value is available, so this value doubles up883* to satisfy both measurements.884*/885static int cx24123_read_ber(struct dvb_frontend *fe, u32 *ber)886{887struct cx24123_state *state = fe->demodulator_priv;888889/* The true bit error rate is this value divided by890the window size (set as 256 * 255) */891*ber = ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |892(cx24123_readreg(state, 0x1d) << 8 |893cx24123_readreg(state, 0x1e));894895dprintk("BER = %d\n", *ber);896897return 0;898}899900static int cx24123_read_signal_strength(struct dvb_frontend *fe,901u16 *signal_strength)902{903struct cx24123_state *state = fe->demodulator_priv;904905/* larger = better */906*signal_strength = cx24123_readreg(state, 0x3b) << 8;907908dprintk("Signal strength = %d\n", *signal_strength);909910return 0;911}912913static int cx24123_read_snr(struct dvb_frontend *fe, u16 *snr)914{915struct cx24123_state *state = fe->demodulator_priv;916917/* Inverted raw Es/N0 count, totally bogus but better than the918BER threshold. */919*snr = 65535 - (((u16)cx24123_readreg(state, 0x18) << 8) |920(u16)cx24123_readreg(state, 0x19));921922dprintk("read S/N index = %d\n", *snr);923924return 0;925}926927static int cx24123_set_frontend(struct dvb_frontend *fe,928struct dvb_frontend_parameters *p)929{930struct cx24123_state *state = fe->demodulator_priv;931932dprintk("\n");933934if (state->config->set_ts_params)935state->config->set_ts_params(fe, 0);936937state->currentfreq = p->frequency;938state->currentsymbolrate = p->u.qpsk.symbol_rate;939940cx24123_set_inversion(state, p->inversion);941cx24123_set_fec(state, p->u.qpsk.fec_inner);942cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);943944if (!state->config->dont_use_pll)945cx24123_pll_tune(fe, p);946else if (fe->ops.tuner_ops.set_params)947fe->ops.tuner_ops.set_params(fe, p);948else949err("it seems I don't have a tuner...");950951/* Enable automatic acquisition and reset cycle */952cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));953cx24123_writereg(state, 0x00, 0x10);954cx24123_writereg(state, 0x00, 0);955956if (state->config->agc_callback)957state->config->agc_callback(fe);958959return 0;960}961962static int cx24123_get_frontend(struct dvb_frontend *fe,963struct dvb_frontend_parameters *p)964{965struct cx24123_state *state = fe->demodulator_priv;966967dprintk("\n");968969if (cx24123_get_inversion(state, &p->inversion) != 0) {970err("%s: Failed to get inversion status\n", __func__);971return -EREMOTEIO;972}973if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {974err("%s: Failed to get fec status\n", __func__);975return -EREMOTEIO;976}977p->frequency = state->currentfreq;978p->u.qpsk.symbol_rate = state->currentsymbolrate;979980return 0;981}982983static int cx24123_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)984{985struct cx24123_state *state = fe->demodulator_priv;986u8 val;987988/* wait for diseqc queue ready */989cx24123_wait_for_diseqc(state);990991val = cx24123_readreg(state, 0x29) & ~0x40;992993switch (tone) {994case SEC_TONE_ON:995dprintk("setting tone on\n");996return cx24123_writereg(state, 0x29, val | 0x10);997case SEC_TONE_OFF:998dprintk("setting tone off\n");999return cx24123_writereg(state, 0x29, val & 0xef);1000default:1001err("CASE reached default with tone=%d\n", tone);1002return -EINVAL;1003}10041005return 0;1006}10071008static int cx24123_tune(struct dvb_frontend *fe,1009struct dvb_frontend_parameters *params,1010unsigned int mode_flags,1011unsigned int *delay,1012fe_status_t *status)1013{1014int retval = 0;10151016if (params != NULL)1017retval = cx24123_set_frontend(fe, params);10181019if (!(mode_flags & FE_TUNE_MODE_ONESHOT))1020cx24123_read_status(fe, status);1021*delay = HZ/10;10221023return retval;1024}10251026static int cx24123_get_algo(struct dvb_frontend *fe)1027{1028return 1; /* FE_ALGO_HW */1029}10301031static void cx24123_release(struct dvb_frontend *fe)1032{1033struct cx24123_state *state = fe->demodulator_priv;1034dprintk("\n");1035i2c_del_adapter(&state->tuner_i2c_adapter);1036kfree(state);1037}10381039static int cx24123_tuner_i2c_tuner_xfer(struct i2c_adapter *i2c_adap,1040struct i2c_msg msg[], int num)1041{1042struct cx24123_state *state = i2c_get_adapdata(i2c_adap);1043/* this repeater closes after the first stop */1044cx24123_repeater_mode(state, 1, 1);1045return i2c_transfer(state->i2c, msg, num);1046}10471048static u32 cx24123_tuner_i2c_func(struct i2c_adapter *adapter)1049{1050return I2C_FUNC_I2C;1051}10521053static struct i2c_algorithm cx24123_tuner_i2c_algo = {1054.master_xfer = cx24123_tuner_i2c_tuner_xfer,1055.functionality = cx24123_tuner_i2c_func,1056};10571058struct i2c_adapter *1059cx24123_get_tuner_i2c_adapter(struct dvb_frontend *fe)1060{1061struct cx24123_state *state = fe->demodulator_priv;1062return &state->tuner_i2c_adapter;1063}1064EXPORT_SYMBOL(cx24123_get_tuner_i2c_adapter);10651066static struct dvb_frontend_ops cx24123_ops;10671068struct dvb_frontend *cx24123_attach(const struct cx24123_config *config,1069struct i2c_adapter *i2c)1070{1071/* allocate memory for the internal state */1072struct cx24123_state *state =1073kzalloc(sizeof(struct cx24123_state), GFP_KERNEL);10741075dprintk("\n");1076if (state == NULL) {1077err("Unable to kzalloc\n");1078goto error;1079}10801081/* setup the state */1082state->config = config;1083state->i2c = i2c;10841085/* check if the demod is there */1086state->demod_rev = cx24123_readreg(state, 0x00);1087switch (state->demod_rev) {1088case 0xe1:1089info("detected CX24123C\n");1090break;1091case 0xd1:1092info("detected CX24123\n");1093break;1094default:1095err("wrong demod revision: %x\n", state->demod_rev);1096goto error;1097}10981099/* create dvb_frontend */1100memcpy(&state->frontend.ops, &cx24123_ops,1101sizeof(struct dvb_frontend_ops));1102state->frontend.demodulator_priv = state;11031104/* create tuner i2c adapter */1105if (config->dont_use_pll)1106cx24123_repeater_mode(state, 1, 0);11071108strlcpy(state->tuner_i2c_adapter.name, "CX24123 tuner I2C bus",1109sizeof(state->tuner_i2c_adapter.name));1110state->tuner_i2c_adapter.algo = &cx24123_tuner_i2c_algo;1111state->tuner_i2c_adapter.algo_data = NULL;1112i2c_set_adapdata(&state->tuner_i2c_adapter, state);1113if (i2c_add_adapter(&state->tuner_i2c_adapter) < 0) {1114err("tuner i2c bus could not be initialized\n");1115goto error;1116}11171118return &state->frontend;11191120error:1121kfree(state);11221123return NULL;1124}1125EXPORT_SYMBOL(cx24123_attach);11261127static struct dvb_frontend_ops cx24123_ops = {11281129.info = {1130.name = "Conexant CX24123/CX24109",1131.type = FE_QPSK,1132.frequency_min = 950000,1133.frequency_max = 2150000,1134.frequency_stepsize = 1011, /* kHz for QPSK frontends */1135.frequency_tolerance = 5000,1136.symbol_rate_min = 1000000,1137.symbol_rate_max = 45000000,1138.caps = FE_CAN_INVERSION_AUTO |1139FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |1140FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |1141FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |1142FE_CAN_QPSK | FE_CAN_RECOVER1143},11441145.release = cx24123_release,11461147.init = cx24123_initfe,1148.set_frontend = cx24123_set_frontend,1149.get_frontend = cx24123_get_frontend,1150.read_status = cx24123_read_status,1151.read_ber = cx24123_read_ber,1152.read_signal_strength = cx24123_read_signal_strength,1153.read_snr = cx24123_read_snr,1154.diseqc_send_master_cmd = cx24123_send_diseqc_msg,1155.diseqc_send_burst = cx24123_diseqc_send_burst,1156.set_tone = cx24123_set_tone,1157.set_voltage = cx24123_set_voltage,1158.tune = cx24123_tune,1159.get_frontend_algo = cx24123_get_algo,1160};11611162MODULE_DESCRIPTION("DVB Frontend module for Conexant " \1163"CX24123/CX24109/CX24113 hardware");1164MODULE_AUTHOR("Steven Toth");1165MODULE_LICENSE("GPL");1166116711681169