Path: blob/master/drivers/media/dvb/frontends/cx22700.c
15112 views
/*1Conexant cx22700 DVB OFDM demodulator driver23Copyright (C) 2001-2002 Convergence Integrated Media GmbH4Holger Waechtler <[email protected]>56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.1920*/2122#include <linux/kernel.h>23#include <linux/init.h>24#include <linux/module.h>25#include <linux/string.h>26#include <linux/slab.h>27#include "dvb_frontend.h"28#include "cx22700.h"293031struct cx22700_state {3233struct i2c_adapter* i2c;3435const struct cx22700_config* config;3637struct dvb_frontend frontend;38};394041static int debug;42#define dprintk(args...) \43do { \44if (debug) printk(KERN_DEBUG "cx22700: " args); \45} while (0)4647static u8 init_tab [] = {480x04, 0x10,490x05, 0x09,500x06, 0x00,510x08, 0x04,520x09, 0x00,530x0a, 0x01,540x15, 0x40,550x16, 0x10,560x17, 0x87,570x18, 0x17,580x1a, 0x10,590x25, 0x04,600x2e, 0x00,610x39, 0x00,620x3a, 0x04,630x45, 0x08,640x46, 0x02,650x47, 0x05,66};676869static int cx22700_writereg (struct cx22700_state* state, u8 reg, u8 data)70{71int ret;72u8 buf [] = { reg, data };73struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };7475dprintk ("%s\n", __func__);7677ret = i2c_transfer (state->i2c, &msg, 1);7879if (ret != 1)80printk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",81__func__, reg, data, ret);8283return (ret != 1) ? -1 : 0;84}8586static int cx22700_readreg (struct cx22700_state* state, u8 reg)87{88int ret;89u8 b0 [] = { reg };90u8 b1 [] = { 0 };91struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },92{ .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };9394dprintk ("%s\n", __func__);9596ret = i2c_transfer (state->i2c, msg, 2);9798if (ret != 2) return -EIO;99100return b1[0];101}102103static int cx22700_set_inversion (struct cx22700_state* state, int inversion)104{105u8 val;106107dprintk ("%s\n", __func__);108109switch (inversion) {110case INVERSION_AUTO:111return -EOPNOTSUPP;112case INVERSION_ON:113val = cx22700_readreg (state, 0x09);114return cx22700_writereg (state, 0x09, val | 0x01);115case INVERSION_OFF:116val = cx22700_readreg (state, 0x09);117return cx22700_writereg (state, 0x09, val & 0xfe);118default:119return -EINVAL;120}121}122123static int cx22700_set_tps (struct cx22700_state *state, struct dvb_ofdm_parameters *p)124{125static const u8 qam_tab [4] = { 0, 1, 0, 2 };126static const u8 fec_tab [6] = { 0, 1, 2, 0, 3, 4 };127u8 val;128129dprintk ("%s\n", __func__);130131if (p->code_rate_HP < FEC_1_2 || p->code_rate_HP > FEC_7_8)132return -EINVAL;133134if (p->code_rate_LP < FEC_1_2 || p->code_rate_LP > FEC_7_8)135return -EINVAL;136137if (p->code_rate_HP == FEC_4_5 || p->code_rate_LP == FEC_4_5)138return -EINVAL;139140if (p->guard_interval < GUARD_INTERVAL_1_32 ||141p->guard_interval > GUARD_INTERVAL_1_4)142return -EINVAL;143144if (p->transmission_mode != TRANSMISSION_MODE_2K &&145p->transmission_mode != TRANSMISSION_MODE_8K)146return -EINVAL;147148if (p->constellation != QPSK &&149p->constellation != QAM_16 &&150p->constellation != QAM_64)151return -EINVAL;152153if (p->hierarchy_information < HIERARCHY_NONE ||154p->hierarchy_information > HIERARCHY_4)155return -EINVAL;156157if (p->bandwidth < BANDWIDTH_8_MHZ || p->bandwidth > BANDWIDTH_6_MHZ)158return -EINVAL;159160if (p->bandwidth == BANDWIDTH_7_MHZ)161cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 | 0x10));162else163cx22700_writereg (state, 0x09, cx22700_readreg (state, 0x09 & ~0x10));164165val = qam_tab[p->constellation - QPSK];166val |= p->hierarchy_information - HIERARCHY_NONE;167168cx22700_writereg (state, 0x04, val);169170val = fec_tab[p->code_rate_HP - FEC_1_2] << 3;171val |= fec_tab[p->code_rate_LP - FEC_1_2];172173cx22700_writereg (state, 0x05, val);174175val = (p->guard_interval - GUARD_INTERVAL_1_32) << 2;176val |= p->transmission_mode - TRANSMISSION_MODE_2K;177178cx22700_writereg (state, 0x06, val);179180cx22700_writereg (state, 0x08, 0x04 | 0x02); /* use user tps parameters */181cx22700_writereg (state, 0x08, 0x04); /* restart acquisition */182183return 0;184}185186static int cx22700_get_tps (struct cx22700_state* state, struct dvb_ofdm_parameters *p)187{188static const fe_modulation_t qam_tab [3] = { QPSK, QAM_16, QAM_64 };189static const fe_code_rate_t fec_tab [5] = { FEC_1_2, FEC_2_3, FEC_3_4,190FEC_5_6, FEC_7_8 };191u8 val;192193dprintk ("%s\n", __func__);194195if (!(cx22700_readreg(state, 0x07) & 0x20)) /* tps valid? */196return -EAGAIN;197198val = cx22700_readreg (state, 0x01);199200if ((val & 0x7) > 4)201p->hierarchy_information = HIERARCHY_AUTO;202else203p->hierarchy_information = HIERARCHY_NONE + (val & 0x7);204205if (((val >> 3) & 0x3) > 2)206p->constellation = QAM_AUTO;207else208p->constellation = qam_tab[(val >> 3) & 0x3];209210val = cx22700_readreg (state, 0x02);211212if (((val >> 3) & 0x07) > 4)213p->code_rate_HP = FEC_AUTO;214else215p->code_rate_HP = fec_tab[(val >> 3) & 0x07];216217if ((val & 0x07) > 4)218p->code_rate_LP = FEC_AUTO;219else220p->code_rate_LP = fec_tab[val & 0x07];221222val = cx22700_readreg (state, 0x03);223224p->guard_interval = GUARD_INTERVAL_1_32 + ((val >> 6) & 0x3);225p->transmission_mode = TRANSMISSION_MODE_2K + ((val >> 5) & 0x1);226227return 0;228}229230static int cx22700_init (struct dvb_frontend* fe)231232{ struct cx22700_state* state = fe->demodulator_priv;233int i;234235dprintk("cx22700_init: init chip\n");236237cx22700_writereg (state, 0x00, 0x02); /* soft reset */238cx22700_writereg (state, 0x00, 0x00);239240msleep(10);241242for (i=0; i<sizeof(init_tab); i+=2)243cx22700_writereg (state, init_tab[i], init_tab[i+1]);244245cx22700_writereg (state, 0x00, 0x01);246247return 0;248}249250static int cx22700_read_status(struct dvb_frontend* fe, fe_status_t* status)251{252struct cx22700_state* state = fe->demodulator_priv;253254u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)255| (cx22700_readreg (state, 0x0e) << 1);256u8 sync = cx22700_readreg (state, 0x07);257258*status = 0;259260if (rs_ber < 0xff00)261*status |= FE_HAS_SIGNAL;262263if (sync & 0x20)264*status |= FE_HAS_CARRIER;265266if (sync & 0x10)267*status |= FE_HAS_VITERBI;268269if (sync & 0x10)270*status |= FE_HAS_SYNC;271272if (*status == 0x0f)273*status |= FE_HAS_LOCK;274275return 0;276}277278static int cx22700_read_ber(struct dvb_frontend* fe, u32* ber)279{280struct cx22700_state* state = fe->demodulator_priv;281282*ber = cx22700_readreg (state, 0x0c) & 0x7f;283cx22700_writereg (state, 0x0c, 0x00);284285return 0;286}287288static int cx22700_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)289{290struct cx22700_state* state = fe->demodulator_priv;291292u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)293| (cx22700_readreg (state, 0x0e) << 1);294*signal_strength = ~rs_ber;295296return 0;297}298299static int cx22700_read_snr(struct dvb_frontend* fe, u16* snr)300{301struct cx22700_state* state = fe->demodulator_priv;302303u16 rs_ber = (cx22700_readreg (state, 0x0d) << 9)304| (cx22700_readreg (state, 0x0e) << 1);305*snr = ~rs_ber;306307return 0;308}309310static int cx22700_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)311{312struct cx22700_state* state = fe->demodulator_priv;313314*ucblocks = cx22700_readreg (state, 0x0f);315cx22700_writereg (state, 0x0f, 0x00);316317return 0;318}319320static int cx22700_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)321{322struct cx22700_state* state = fe->demodulator_priv;323324cx22700_writereg (state, 0x00, 0x02); /* XXX CHECKME: soft reset*/325cx22700_writereg (state, 0x00, 0x00);326327if (fe->ops.tuner_ops.set_params) {328fe->ops.tuner_ops.set_params(fe, p);329if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);330}331332cx22700_set_inversion (state, p->inversion);333cx22700_set_tps (state, &p->u.ofdm);334cx22700_writereg (state, 0x37, 0x01); /* PAL loop filter off */335cx22700_writereg (state, 0x00, 0x01); /* restart acquire */336337return 0;338}339340static int cx22700_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)341{342struct cx22700_state* state = fe->demodulator_priv;343u8 reg09 = cx22700_readreg (state, 0x09);344345p->inversion = reg09 & 0x1 ? INVERSION_ON : INVERSION_OFF;346return cx22700_get_tps (state, &p->u.ofdm);347}348349static int cx22700_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)350{351struct cx22700_state* state = fe->demodulator_priv;352353if (enable) {354return cx22700_writereg(state, 0x0a, 0x00);355} else {356return cx22700_writereg(state, 0x0a, 0x01);357}358}359360static int cx22700_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)361{362fesettings->min_delay_ms = 150;363fesettings->step_size = 166667;364fesettings->max_drift = 166667*2;365return 0;366}367368static void cx22700_release(struct dvb_frontend* fe)369{370struct cx22700_state* state = fe->demodulator_priv;371kfree(state);372}373374static struct dvb_frontend_ops cx22700_ops;375376struct dvb_frontend* cx22700_attach(const struct cx22700_config* config,377struct i2c_adapter* i2c)378{379struct cx22700_state* state = NULL;380381/* allocate memory for the internal state */382state = kzalloc(sizeof(struct cx22700_state), GFP_KERNEL);383if (state == NULL) goto error;384385/* setup the state */386state->config = config;387state->i2c = i2c;388389/* check if the demod is there */390if (cx22700_readreg(state, 0x07) < 0) goto error;391392/* create dvb_frontend */393memcpy(&state->frontend.ops, &cx22700_ops, sizeof(struct dvb_frontend_ops));394state->frontend.demodulator_priv = state;395return &state->frontend;396397error:398kfree(state);399return NULL;400}401402static struct dvb_frontend_ops cx22700_ops = {403404.info = {405.name = "Conexant CX22700 DVB-T",406.type = FE_OFDM,407.frequency_min = 470000000,408.frequency_max = 860000000,409.frequency_stepsize = 166667,410.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |411FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |412FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |413FE_CAN_RECOVER414},415416.release = cx22700_release,417418.init = cx22700_init,419.i2c_gate_ctrl = cx22700_i2c_gate_ctrl,420421.set_frontend = cx22700_set_frontend,422.get_frontend = cx22700_get_frontend,423.get_tune_settings = cx22700_get_tune_settings,424425.read_status = cx22700_read_status,426.read_ber = cx22700_read_ber,427.read_signal_strength = cx22700_read_signal_strength,428.read_snr = cx22700_read_snr,429.read_ucblocks = cx22700_read_ucblocks,430};431432module_param(debug, int, 0644);433MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");434435MODULE_DESCRIPTION("Conexant CX22700 DVB-T Demodulator driver");436MODULE_AUTHOR("Holger Waechtler");437MODULE_LICENSE("GPL");438439EXPORT_SYMBOL(cx22700_attach);440441442