Path: blob/master/drivers/media/dvb/frontends/cx24113.c
15112 views
/*1* Driver for Conexant CX24113/CX24128 Tuner (Satellite)2*3* Copyright (C) 2007-8 Patrick Boettcher <[email protected]>4*5* Developed for BBTI / Technisat6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15*16* GNU General Public License for more details.17*18* You should have received a copy of the GNU General Public License19* along with this program; if not, write to the Free Software20* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.21*/2223#include <linux/slab.h>24#include <linux/kernel.h>25#include <linux/module.h>26#include <linux/init.h>2728#include "dvb_frontend.h"29#include "cx24113.h"3031static int debug;3233#define info(args...) do { printk(KERN_INFO "CX24113: " args); } while (0)34#define err(args...) do { printk(KERN_ERR "CX24113: " args); } while (0)3536#define dprintk(args...) \37do { \38if (debug) { \39printk(KERN_DEBUG "CX24113: %s: ", __func__); \40printk(args); \41} \42} while (0)4344struct cx24113_state {45struct i2c_adapter *i2c;46const struct cx24113_config *config;4748#define REV_CX24113 0x2349u8 rev;50u8 ver;5152u8 icp_mode:1;5354#define ICP_LEVEL1 055#define ICP_LEVEL2 156#define ICP_LEVEL3 257#define ICP_LEVEL4 358u8 icp_man:2;59u8 icp_auto_low:2;60u8 icp_auto_mlow:2;61u8 icp_auto_mhi:2;62u8 icp_auto_hi:2;63u8 icp_dig;6465#define LNA_MIN_GAIN 066#define LNA_MID_GAIN 167#define LNA_MAX_GAIN 268u8 lna_gain:2;6970u8 acp_on:1;7172u8 vco_mode:2;73u8 vco_shift:1;74#define VCOBANDSEL_6 0x8075#define VCOBANDSEL_5 0x0176#define VCOBANDSEL_4 0x0277#define VCOBANDSEL_3 0x0478#define VCOBANDSEL_2 0x0879#define VCOBANDSEL_1 0x1080u8 vco_band;8182#define VCODIV4 483#define VCODIV2 284u8 vcodiv;8586u8 bs_delay:4;87u16 bs_freqcnt:13;88u16 bs_rdiv;89u8 prescaler_mode:1;9091u8 rfvga_bias_ctrl;9293s16 tuner_gain_thres;94u8 gain_level;9596u32 frequency;9798u8 refdiv;99100u8 Fwindow_enabled;101};102103static int cx24113_writereg(struct cx24113_state *state, int reg, int data)104{105u8 buf[] = { reg, data };106struct i2c_msg msg = { .addr = state->config->i2c_addr,107.flags = 0, .buf = buf, .len = 2 };108int err = i2c_transfer(state->i2c, &msg, 1);109if (err != 1) {110printk(KERN_DEBUG "%s: writereg error(err == %i, reg == 0x%02x,"111" data == 0x%02x)\n", __func__, err, reg, data);112return err;113}114115return 0;116}117118static int cx24113_readreg(struct cx24113_state *state, u8 reg)119{120int ret;121u8 b;122struct i2c_msg msg[] = {123{ .addr = state->config->i2c_addr,124.flags = 0, .buf = ®, .len = 1 },125{ .addr = state->config->i2c_addr,126.flags = I2C_M_RD, .buf = &b, .len = 1 }127};128129ret = i2c_transfer(state->i2c, msg, 2);130131if (ret != 2) {132printk(KERN_DEBUG "%s: reg=0x%x (error=%d)\n",133__func__, reg, ret);134return ret;135}136137return b;138}139140static void cx24113_set_parameters(struct cx24113_state *state)141{142u8 r;143144r = cx24113_readreg(state, 0x10) & 0x82;145r |= state->icp_mode;146r |= state->icp_man << 4;147r |= state->icp_dig << 2;148r |= state->prescaler_mode << 5;149cx24113_writereg(state, 0x10, r);150151r = (state->icp_auto_low << 0) | (state->icp_auto_mlow << 2)152| (state->icp_auto_mhi << 4) | (state->icp_auto_hi << 6);153cx24113_writereg(state, 0x11, r);154155if (state->rev == REV_CX24113) {156r = cx24113_readreg(state, 0x20) & 0xec;157r |= state->lna_gain;158r |= state->rfvga_bias_ctrl << 4;159cx24113_writereg(state, 0x20, r);160}161162r = cx24113_readreg(state, 0x12) & 0x03;163r |= state->acp_on << 2;164r |= state->bs_delay << 4;165cx24113_writereg(state, 0x12, r);166167r = cx24113_readreg(state, 0x18) & 0x40;168r |= state->vco_shift;169if (state->vco_band == VCOBANDSEL_6)170r |= (1 << 7);171else172r |= (state->vco_band << 1);173cx24113_writereg(state, 0x18, r);174175r = cx24113_readreg(state, 0x14) & 0x20;176r |= (state->vco_mode << 6) | ((state->bs_freqcnt >> 8) & 0x1f);177cx24113_writereg(state, 0x14, r);178cx24113_writereg(state, 0x15, (state->bs_freqcnt & 0xff));179180cx24113_writereg(state, 0x16, (state->bs_rdiv >> 4) & 0xff);181r = (cx24113_readreg(state, 0x17) & 0x0f) |182((state->bs_rdiv & 0x0f) << 4);183cx24113_writereg(state, 0x17, r);184}185186#define VGA_0 0x00187#define VGA_1 0x04188#define VGA_2 0x02189#define VGA_3 0x06190#define VGA_4 0x01191#define VGA_5 0x05192#define VGA_6 0x03193#define VGA_7 0x07194195#define RFVGA_0 0x00196#define RFVGA_1 0x01197#define RFVGA_2 0x02198#define RFVGA_3 0x03199200static int cx24113_set_gain_settings(struct cx24113_state *state,201s16 power_estimation)202{203u8 ampout = cx24113_readreg(state, 0x1d) & 0xf0,204vga = cx24113_readreg(state, 0x1f) & 0x3f,205rfvga = cx24113_readreg(state, 0x20) & 0xf3;206u8 gain_level = power_estimation >= state->tuner_gain_thres;207208dprintk("power estimation: %d, thres: %d, gain_level: %d/%d\n",209power_estimation, state->tuner_gain_thres,210state->gain_level, gain_level);211212if (gain_level == state->gain_level)213return 0; /* nothing to be done */214215ampout |= 0xf;216217if (gain_level) {218rfvga |= RFVGA_0 << 2;219vga |= (VGA_7 << 3) | VGA_7;220} else {221rfvga |= RFVGA_2 << 2;222vga |= (VGA_6 << 3) | VGA_2;223}224state->gain_level = gain_level;225226cx24113_writereg(state, 0x1d, ampout);227cx24113_writereg(state, 0x1f, vga);228cx24113_writereg(state, 0x20, rfvga);229230return 1; /* did something */231}232233static int cx24113_set_Fref(struct cx24113_state *state, u8 high)234{235u8 xtal = cx24113_readreg(state, 0x02);236if (state->rev == 0x43 && state->vcodiv == VCODIV4)237high = 1;238239xtal &= ~0x2;240if (high)241xtal |= high << 1;242return cx24113_writereg(state, 0x02, xtal);243}244245static int cx24113_enable(struct cx24113_state *state, u8 enable)246{247u8 r21 = (cx24113_readreg(state, 0x21) & 0xc0) | enable;248if (state->rev == REV_CX24113)249r21 |= (1 << 1);250return cx24113_writereg(state, 0x21, r21);251}252253static int cx24113_set_bandwidth(struct cx24113_state *state, u32 bandwidth_khz)254{255u8 r;256257if (bandwidth_khz <= 19000)258r = 0x03 << 6;259else if (bandwidth_khz <= 25000)260r = 0x02 << 6;261else262r = 0x01 << 6;263264dprintk("bandwidth to be set: %d\n", bandwidth_khz);265bandwidth_khz *= 10;266bandwidth_khz -= 10000;267bandwidth_khz /= 1000;268bandwidth_khz += 5;269bandwidth_khz /= 10;270271dprintk("bandwidth: %d %d\n", r >> 6, bandwidth_khz);272273r |= bandwidth_khz & 0x3f;274275return cx24113_writereg(state, 0x1e, r);276}277278static int cx24113_set_clk_inversion(struct cx24113_state *state, u8 on)279{280u8 r = (cx24113_readreg(state, 0x10) & 0x7f) | ((on & 0x1) << 7);281return cx24113_writereg(state, 0x10, r);282}283284static int cx24113_get_status(struct dvb_frontend *fe, u32 *status)285{286struct cx24113_state *state = fe->tuner_priv;287u8 r = (cx24113_readreg(state, 0x10) & 0x02) >> 1;288if (r)289*status |= TUNER_STATUS_LOCKED;290dprintk("PLL locked: %d\n", r);291return 0;292}293294static u8 cx24113_set_ref_div(struct cx24113_state *state, u8 refdiv)295{296if (state->rev == 0x43 && state->vcodiv == VCODIV4)297refdiv = 2;298return state->refdiv = refdiv;299}300301static void cx24113_calc_pll_nf(struct cx24113_state *state, u16 *n, s32 *f)302{303s32 N;304s64 F;305u64 dividend;306u8 R, r;307u8 vcodiv;308u8 factor;309s32 freq_hz = state->frequency * 1000;310311if (state->config->xtal_khz < 20000)312factor = 1;313else314factor = 2;315316if (state->rev == REV_CX24113) {317if (state->frequency >= 1100000)318vcodiv = VCODIV2;319else320vcodiv = VCODIV4;321} else {322if (state->frequency >= 1165000)323vcodiv = VCODIV2;324else325vcodiv = VCODIV4;326}327state->vcodiv = vcodiv;328329dprintk("calculating N/F for %dHz with vcodiv %d\n", freq_hz, vcodiv);330R = 0;331do {332R = cx24113_set_ref_div(state, R + 1);333334/* calculate tuner PLL settings: */335N = (freq_hz / 100 * vcodiv) * R;336N /= (state->config->xtal_khz) * factor * 2;337N += 5; /* For round up. */338N /= 10;339N -= 32;340} while (N < 6 && R < 3);341342if (N < 6) {343err("strange frequency: N < 6\n");344return;345}346F = freq_hz;347F *= (u64) (R * vcodiv * 262144);348dprintk("1 N: %d, F: %lld, R: %d\n", N, (long long)F, R);349/* do_div needs an u64 as first argument */350dividend = F;351do_div(dividend, state->config->xtal_khz * 1000 * factor * 2);352F = dividend;353dprintk("2 N: %d, F: %lld, R: %d\n", N, (long long)F, R);354F -= (N + 32) * 262144;355356dprintk("3 N: %d, F: %lld, R: %d\n", N, (long long)F, R);357358if (state->Fwindow_enabled) {359if (F > (262144 / 2 - 1638))360F = 262144 / 2 - 1638;361if (F < (-262144 / 2 + 1638))362F = -262144 / 2 + 1638;363if ((F < 3277 && F > 0) || (F > -3277 && F < 0)) {364F = 0;365r = cx24113_readreg(state, 0x10);366cx24113_writereg(state, 0x10, r | (1 << 6));367}368}369dprintk("4 N: %d, F: %lld, R: %d\n", N, (long long)F, R);370371*n = (u16) N;372*f = (s32) F;373}374375376static void cx24113_set_nfr(struct cx24113_state *state, u16 n, s32 f, u8 r)377{378u8 reg;379cx24113_writereg(state, 0x19, (n >> 1) & 0xff);380381reg = ((n & 0x1) << 7) | ((f >> 11) & 0x7f);382cx24113_writereg(state, 0x1a, reg);383384cx24113_writereg(state, 0x1b, (f >> 3) & 0xff);385386reg = cx24113_readreg(state, 0x1c) & 0x1f;387cx24113_writereg(state, 0x1c, reg | ((f & 0x7) << 5));388389cx24113_set_Fref(state, r - 1);390}391392static int cx24113_set_frequency(struct cx24113_state *state, u32 frequency)393{394u8 r = 1; /* or 2 */395u16 n = 6;396s32 f = 0;397398r = cx24113_readreg(state, 0x14);399cx24113_writereg(state, 0x14, r & 0x3f);400401r = cx24113_readreg(state, 0x10);402cx24113_writereg(state, 0x10, r & 0xbf);403404state->frequency = frequency;405406dprintk("tuning to frequency: %d\n", frequency);407408cx24113_calc_pll_nf(state, &n, &f);409cx24113_set_nfr(state, n, f, state->refdiv);410411r = cx24113_readreg(state, 0x18) & 0xbf;412if (state->vcodiv != VCODIV2)413r |= 1 << 6;414cx24113_writereg(state, 0x18, r);415416/* The need for this sleep is not clear. But helps in some cases */417msleep(5);418419r = cx24113_readreg(state, 0x1c) & 0xef;420cx24113_writereg(state, 0x1c, r | (1 << 4));421return 0;422}423424static int cx24113_init(struct dvb_frontend *fe)425{426struct cx24113_state *state = fe->tuner_priv;427int ret;428429state->tuner_gain_thres = -50;430state->gain_level = 255; /* to force a gain-setting initialization */431state->icp_mode = 0;432433if (state->config->xtal_khz < 11000) {434state->icp_auto_hi = ICP_LEVEL4;435state->icp_auto_mhi = ICP_LEVEL4;436state->icp_auto_mlow = ICP_LEVEL3;437state->icp_auto_low = ICP_LEVEL3;438} else {439state->icp_auto_hi = ICP_LEVEL4;440state->icp_auto_mhi = ICP_LEVEL4;441state->icp_auto_mlow = ICP_LEVEL3;442state->icp_auto_low = ICP_LEVEL2;443}444445state->icp_dig = ICP_LEVEL3;446state->icp_man = ICP_LEVEL1;447state->acp_on = 1;448state->vco_mode = 0;449state->vco_shift = 0;450state->vco_band = VCOBANDSEL_1;451state->bs_delay = 8;452state->bs_freqcnt = 0x0fff;453state->bs_rdiv = 0x0fff;454state->prescaler_mode = 0;455state->lna_gain = LNA_MAX_GAIN;456state->rfvga_bias_ctrl = 1;457state->Fwindow_enabled = 1;458459cx24113_set_Fref(state, 0);460cx24113_enable(state, 0x3d);461cx24113_set_parameters(state);462463cx24113_set_gain_settings(state, -30);464465cx24113_set_bandwidth(state, 18025);466cx24113_set_clk_inversion(state, 1);467468if (state->config->xtal_khz >= 40000)469ret = cx24113_writereg(state, 0x02,470(cx24113_readreg(state, 0x02) & 0xfb) | (1 << 2));471else472ret = cx24113_writereg(state, 0x02,473(cx24113_readreg(state, 0x02) & 0xfb) | (0 << 2));474475return ret;476}477478static int cx24113_set_params(struct dvb_frontend *fe,479struct dvb_frontend_parameters *p)480{481struct cx24113_state *state = fe->tuner_priv;482/* for a ROLL-OFF factor of 0.35, 0.2: 600, 0.25: 625 */483u32 roll_off = 675;484u32 bw;485486bw = ((p->u.qpsk.symbol_rate/100) * roll_off) / 1000;487bw += (10000000/100) + 5;488bw /= 10;489bw += 1000;490cx24113_set_bandwidth(state, bw);491492cx24113_set_frequency(state, p->frequency);493msleep(5);494return cx24113_get_status(fe, &bw);495}496497static s8 cx24113_agc_table[2][10] = {498{-54, -41, -35, -30, -25, -21, -16, -10, -6, -2},499{-39, -35, -30, -25, -19, -15, -11, -5, 1, 9},500};501502void cx24113_agc_callback(struct dvb_frontend *fe)503{504struct cx24113_state *state = fe->tuner_priv;505s16 s, i;506if (!fe->ops.read_signal_strength)507return;508509do {510/* this only works with the current CX24123 implementation */511fe->ops.read_signal_strength(fe, (u16 *) &s);512s >>= 8;513dprintk("signal strength: %d\n", s);514for (i = 0; i < sizeof(cx24113_agc_table[0]); i++)515if (cx24113_agc_table[state->gain_level][i] > s)516break;517s = -25 - i*5;518} while (cx24113_set_gain_settings(state, s));519}520EXPORT_SYMBOL(cx24113_agc_callback);521522static int cx24113_get_frequency(struct dvb_frontend *fe, u32 *frequency)523{524struct cx24113_state *state = fe->tuner_priv;525*frequency = state->frequency;526return 0;527}528529static int cx24113_release(struct dvb_frontend *fe)530{531struct cx24113_state *state = fe->tuner_priv;532dprintk("\n");533fe->tuner_priv = NULL;534kfree(state);535return 0;536}537538static const struct dvb_tuner_ops cx24113_tuner_ops = {539.info = {540.name = "Conexant CX24113",541.frequency_min = 950000,542.frequency_max = 2150000,543.frequency_step = 125,544},545546.release = cx24113_release,547548.init = cx24113_init,549.sleep = NULL,550551.set_params = cx24113_set_params,552.get_frequency = cx24113_get_frequency,553.get_bandwidth = NULL,554.get_status = cx24113_get_status,555};556557struct dvb_frontend *cx24113_attach(struct dvb_frontend *fe,558const struct cx24113_config *config, struct i2c_adapter *i2c)559{560/* allocate memory for the internal state */561struct cx24113_state *state =562kzalloc(sizeof(struct cx24113_state), GFP_KERNEL);563int rc;564if (state == NULL) {565err("Unable to kzalloc\n");566goto error;567}568569/* setup the state */570state->config = config;571state->i2c = i2c;572573info("trying to detect myself\n");574575/* making a dummy read, because of some expected troubles576* after power on */577cx24113_readreg(state, 0x00);578579rc = cx24113_readreg(state, 0x00);580if (rc < 0) {581info("CX24113 not found.\n");582goto error;583}584state->rev = rc;585586switch (rc) {587case 0x43:588info("detected CX24113 variant\n");589break;590case REV_CX24113:591info("successfully detected\n");592break;593default:594err("unsupported device id: %x\n", state->rev);595goto error;596}597state->ver = cx24113_readreg(state, 0x01);598info("version: %x\n", state->ver);599600/* create dvb_frontend */601memcpy(&fe->ops.tuner_ops, &cx24113_tuner_ops,602sizeof(struct dvb_tuner_ops));603fe->tuner_priv = state;604return fe;605606error:607kfree(state);608609return NULL;610}611EXPORT_SYMBOL(cx24113_attach);612613module_param(debug, int, 0644);614MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");615616MODULE_AUTHOR("Patrick Boettcher <[email protected]>");617MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24113/CX24128hardware");618MODULE_LICENSE("GPL");619620621622