Path: blob/master/drivers/media/common/tuners/mt2060.c
15112 views
/*1* Driver for Microtune MT2060 "Single chip dual conversion broadband tuner"2*3* Copyright (c) 2006 Olivier DANET <[email protected]>4*5* This program is free software; you can redistribute it and/or modify6* it under the terms of the GNU General Public License as published by7* the Free Software Foundation; either version 2 of the License, or8* (at your option) any later version.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13*14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=19*/2021/* In that file, frequencies are expressed in kiloHertz to avoid 32 bits overflows */2223#include <linux/module.h>24#include <linux/delay.h>25#include <linux/dvb/frontend.h>26#include <linux/i2c.h>27#include <linux/slab.h>2829#include "dvb_frontend.h"3031#include "mt2060.h"32#include "mt2060_priv.h"3334static int debug;35module_param(debug, int, 0644);36MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");3738#define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2060: " args); printk("\n"); }} while (0)3940// Reads a single register41static int mt2060_readreg(struct mt2060_priv *priv, u8 reg, u8 *val)42{43struct i2c_msg msg[2] = {44{ .addr = priv->cfg->i2c_address, .flags = 0, .buf = ®, .len = 1 },45{ .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val, .len = 1 },46};4748if (i2c_transfer(priv->i2c, msg, 2) != 2) {49printk(KERN_WARNING "mt2060 I2C read failed\n");50return -EREMOTEIO;51}52return 0;53}5455// Writes a single register56static int mt2060_writereg(struct mt2060_priv *priv, u8 reg, u8 val)57{58u8 buf[2] = { reg, val };59struct i2c_msg msg = {60.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 261};6263if (i2c_transfer(priv->i2c, &msg, 1) != 1) {64printk(KERN_WARNING "mt2060 I2C write failed\n");65return -EREMOTEIO;66}67return 0;68}6970// Writes a set of consecutive registers71static int mt2060_writeregs(struct mt2060_priv *priv,u8 *buf, u8 len)72{73struct i2c_msg msg = {74.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len75};76if (i2c_transfer(priv->i2c, &msg, 1) != 1) {77printk(KERN_WARNING "mt2060 I2C write failed (len=%i)\n",(int)len);78return -EREMOTEIO;79}80return 0;81}8283// Initialisation sequences84// LNABAND=3, NUM1=0x3C, DIV1=0x74, NUM2=0x1080, DIV2=0x4985static u8 mt2060_config1[] = {86REG_LO1C1,870x3F, 0x74, 0x00, 0x08, 0x9388};8990// FMCG=2, GP2=0, GP1=091static u8 mt2060_config2[] = {92REG_MISC_CTRL,930x20, 0x1E, 0x30, 0xff, 0x80, 0xff, 0x00, 0x2c, 0x4294};9596// VGAG=3, V1CSE=19798#ifdef MT2060_SPURCHECK99/* The function below calculates the frequency offset between the output frequency if2100and the closer cross modulation subcarrier between lo1 and lo2 up to the tenth harmonic */101static int mt2060_spurcalc(u32 lo1,u32 lo2,u32 if2)102{103int I,J;104int dia,diamin,diff;105diamin=1000000;106for (I = 1; I < 10; I++) {107J = ((2*I*lo1)/lo2+1)/2;108diff = I*(int)lo1-J*(int)lo2;109if (diff < 0) diff=-diff;110dia = (diff-(int)if2);111if (dia < 0) dia=-dia;112if (diamin > dia) diamin=dia;113}114return diamin;115}116117#define BANDWIDTH 4000 // kHz118119/* Calculates the frequency offset to add to avoid spurs. Returns 0 if no offset is needed */120static int mt2060_spurcheck(u32 lo1,u32 lo2,u32 if2)121{122u32 Spur,Sp1,Sp2;123int I,J;124I=0;125J=1000;126127Spur=mt2060_spurcalc(lo1,lo2,if2);128if (Spur < BANDWIDTH) {129/* Potential spurs detected */130dprintk("Spurs before : f_lo1: %d f_lo2: %d (kHz)",131(int)lo1,(int)lo2);132I=1000;133Sp1 = mt2060_spurcalc(lo1+I,lo2+I,if2);134Sp2 = mt2060_spurcalc(lo1-I,lo2-I,if2);135136if (Sp1 < Sp2) {137J=-J; I=-I; Spur=Sp2;138} else139Spur=Sp1;140141while (Spur < BANDWIDTH) {142I += J;143Spur = mt2060_spurcalc(lo1+I,lo2+I,if2);144}145dprintk("Spurs after : f_lo1: %d f_lo2: %d (kHz)",146(int)(lo1+I),(int)(lo2+I));147}148return I;149}150#endif151152#define IF2 36150 // IF2 frequency = 36.150 MHz153#define FREF 16000 // Quartz oscillator 16 MHz154155static int mt2060_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)156{157struct mt2060_priv *priv;158int ret=0;159int i=0;160u32 freq;161u8 lnaband;162u32 f_lo1,f_lo2;163u32 div1,num1,div2,num2;164u8 b[8];165u32 if1;166167priv = fe->tuner_priv;168169if1 = priv->if1_freq;170b[0] = REG_LO1B1;171b[1] = 0xFF;172173if (fe->ops.i2c_gate_ctrl)174fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */175176mt2060_writeregs(priv,b,2);177178freq = params->frequency / 1000; // Hz -> kHz179priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;180181f_lo1 = freq + if1 * 1000;182f_lo1 = (f_lo1 / 250) * 250;183f_lo2 = f_lo1 - freq - IF2;184// From the Comtech datasheet, the step used is 50kHz. The tuner chip could be more precise185f_lo2 = ((f_lo2 + 25) / 50) * 50;186priv->frequency = (f_lo1 - f_lo2 - IF2) * 1000,187188#ifdef MT2060_SPURCHECK189// LO-related spurs detection and correction190num1 = mt2060_spurcheck(f_lo1,f_lo2,IF2);191f_lo1 += num1;192f_lo2 += num1;193#endif194//Frequency LO1 = 16MHz * (DIV1 + NUM1/64 )195num1 = f_lo1 / (FREF / 64);196div1 = num1 / 64;197num1 &= 0x3f;198199// Frequency LO2 = 16MHz * (DIV2 + NUM2/8192 )200num2 = f_lo2 * 64 / (FREF / 128);201div2 = num2 / 8192;202num2 &= 0x1fff;203204if (freq <= 95000) lnaband = 0xB0; else205if (freq <= 180000) lnaband = 0xA0; else206if (freq <= 260000) lnaband = 0x90; else207if (freq <= 335000) lnaband = 0x80; else208if (freq <= 425000) lnaband = 0x70; else209if (freq <= 480000) lnaband = 0x60; else210if (freq <= 570000) lnaband = 0x50; else211if (freq <= 645000) lnaband = 0x40; else212if (freq <= 730000) lnaband = 0x30; else213if (freq <= 810000) lnaband = 0x20; else lnaband = 0x10;214215b[0] = REG_LO1C1;216b[1] = lnaband | ((num1 >>2) & 0x0F);217b[2] = div1;218b[3] = (num2 & 0x0F) | ((num1 & 3) << 4);219b[4] = num2 >> 4;220b[5] = ((num2 >>12) & 1) | (div2 << 1);221222dprintk("IF1: %dMHz",(int)if1);223dprintk("PLL freq=%dkHz f_lo1=%dkHz f_lo2=%dkHz",(int)freq,(int)f_lo1,(int)f_lo2);224dprintk("PLL div1=%d num1=%d div2=%d num2=%d",(int)div1,(int)num1,(int)div2,(int)num2);225dprintk("PLL [1..5]: %2x %2x %2x %2x %2x",(int)b[1],(int)b[2],(int)b[3],(int)b[4],(int)b[5]);226227mt2060_writeregs(priv,b,6);228229//Waits for pll lock or timeout230i = 0;231do {232mt2060_readreg(priv,REG_LO_STATUS,b);233if ((b[0] & 0x88)==0x88)234break;235msleep(4);236i++;237} while (i<10);238239if (fe->ops.i2c_gate_ctrl)240fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */241242return ret;243}244245static void mt2060_calibrate(struct mt2060_priv *priv)246{247u8 b = 0;248int i = 0;249250if (mt2060_writeregs(priv,mt2060_config1,sizeof(mt2060_config1)))251return;252if (mt2060_writeregs(priv,mt2060_config2,sizeof(mt2060_config2)))253return;254255/* initialize the clock output */256mt2060_writereg(priv, REG_VGAG, (priv->cfg->clock_out << 6) | 0x30);257258do {259b |= (1 << 6); // FM1SS;260mt2060_writereg(priv, REG_LO2C1,b);261msleep(20);262263if (i == 0) {264b |= (1 << 7); // FM1CA;265mt2060_writereg(priv, REG_LO2C1,b);266b &= ~(1 << 7); // FM1CA;267msleep(20);268}269270b &= ~(1 << 6); // FM1SS271mt2060_writereg(priv, REG_LO2C1,b);272273msleep(20);274i++;275} while (i < 9);276277i = 0;278while (i++ < 10 && mt2060_readreg(priv, REG_MISC_STAT, &b) == 0 && (b & (1 << 6)) == 0)279msleep(20);280281if (i <= 10) {282mt2060_readreg(priv, REG_FM_FREQ, &priv->fmfreq); // now find out, what is fmreq used for :)283dprintk("calibration was successful: %d", (int)priv->fmfreq);284} else285dprintk("FMCAL timed out");286}287288static int mt2060_get_frequency(struct dvb_frontend *fe, u32 *frequency)289{290struct mt2060_priv *priv = fe->tuner_priv;291*frequency = priv->frequency;292return 0;293}294295static int mt2060_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)296{297struct mt2060_priv *priv = fe->tuner_priv;298*bandwidth = priv->bandwidth;299return 0;300}301302static int mt2060_init(struct dvb_frontend *fe)303{304struct mt2060_priv *priv = fe->tuner_priv;305int ret;306307if (fe->ops.i2c_gate_ctrl)308fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */309310ret = mt2060_writereg(priv, REG_VGAG,311(priv->cfg->clock_out << 6) | 0x33);312313if (fe->ops.i2c_gate_ctrl)314fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */315316return ret;317}318319static int mt2060_sleep(struct dvb_frontend *fe)320{321struct mt2060_priv *priv = fe->tuner_priv;322int ret;323324if (fe->ops.i2c_gate_ctrl)325fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */326327ret = mt2060_writereg(priv, REG_VGAG,328(priv->cfg->clock_out << 6) | 0x30);329330if (fe->ops.i2c_gate_ctrl)331fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */332333return ret;334}335336static int mt2060_release(struct dvb_frontend *fe)337{338kfree(fe->tuner_priv);339fe->tuner_priv = NULL;340return 0;341}342343static const struct dvb_tuner_ops mt2060_tuner_ops = {344.info = {345.name = "Microtune MT2060",346.frequency_min = 48000000,347.frequency_max = 860000000,348.frequency_step = 50000,349},350351.release = mt2060_release,352353.init = mt2060_init,354.sleep = mt2060_sleep,355356.set_params = mt2060_set_params,357.get_frequency = mt2060_get_frequency,358.get_bandwidth = mt2060_get_bandwidth359};360361/* This functions tries to identify a MT2060 tuner by reading the PART/REV register. This is hasty. */362struct dvb_frontend * mt2060_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2060_config *cfg, u16 if1)363{364struct mt2060_priv *priv = NULL;365u8 id = 0;366367priv = kzalloc(sizeof(struct mt2060_priv), GFP_KERNEL);368if (priv == NULL)369return NULL;370371priv->cfg = cfg;372priv->i2c = i2c;373priv->if1_freq = if1;374375if (fe->ops.i2c_gate_ctrl)376fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */377378if (mt2060_readreg(priv,REG_PART_REV,&id) != 0) {379kfree(priv);380return NULL;381}382383if (id != PART_REV) {384kfree(priv);385return NULL;386}387printk(KERN_INFO "MT2060: successfully identified (IF1 = %d)\n", if1);388memcpy(&fe->ops.tuner_ops, &mt2060_tuner_ops, sizeof(struct dvb_tuner_ops));389390fe->tuner_priv = priv;391392mt2060_calibrate(priv);393394if (fe->ops.i2c_gate_ctrl)395fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */396397return fe;398}399EXPORT_SYMBOL(mt2060_attach);400401MODULE_AUTHOR("Olivier DANET");402MODULE_DESCRIPTION("Microtune MT2060 silicon tuner driver");403MODULE_LICENSE("GPL");404405406