Path: blob/master/drivers/media/common/tuners/mt20xx.c
15112 views
/*1* i2c tv tuner chip device driver2* controls microtune tuners, mt2032 + mt2050 at the moment.3*4* This "mt20xx" module was split apart from the original "tuner" module.5*/6#include <linux/delay.h>7#include <linux/i2c.h>8#include <linux/slab.h>9#include <linux/videodev2.h>10#include "tuner-i2c.h"11#include "mt20xx.h"1213static int debug;14module_param(debug, int, 0644);15MODULE_PARM_DESC(debug, "enable verbose debug messages");1617/* ---------------------------------------------------------------------- */1819static unsigned int optimize_vco = 1;20module_param(optimize_vco, int, 0644);2122static unsigned int tv_antenna = 1;23module_param(tv_antenna, int, 0644);2425static unsigned int radio_antenna;26module_param(radio_antenna, int, 0644);2728/* ---------------------------------------------------------------------- */2930#define MT2032 0x0431#define MT2030 0x0632#define MT2040 0x0733#define MT2050 0x423435static char *microtune_part[] = {36[ MT2030 ] = "MT2030",37[ MT2032 ] = "MT2032",38[ MT2040 ] = "MT2040",39[ MT2050 ] = "MT2050",40};4142struct microtune_priv {43struct tuner_i2c_props i2c_props;4445unsigned int xogc;46//unsigned int radio_if2;4748u32 frequency;49};5051static int microtune_release(struct dvb_frontend *fe)52{53kfree(fe->tuner_priv);54fe->tuner_priv = NULL;5556return 0;57}5859static int microtune_get_frequency(struct dvb_frontend *fe, u32 *frequency)60{61struct microtune_priv *priv = fe->tuner_priv;62*frequency = priv->frequency;63return 0;64}6566// IsSpurInBand()?67static int mt2032_spurcheck(struct dvb_frontend *fe,68int f1, int f2, int spectrum_from,int spectrum_to)69{70struct microtune_priv *priv = fe->tuner_priv;71int n1=1,n2,f;7273f1=f1/1000; //scale to kHz to avoid 32bit overflows74f2=f2/1000;75spectrum_from/=1000;76spectrum_to/=1000;7778tuner_dbg("spurcheck f1=%d f2=%d from=%d to=%d\n",79f1,f2,spectrum_from,spectrum_to);8081do {82n2=-n1;83f=n1*(f1-f2);84do {85n2--;86f=f-f2;87tuner_dbg("spurtest n1=%d n2=%d ftest=%d\n",n1,n2,f);8889if( (f>spectrum_from) && (f<spectrum_to))90tuner_dbg("mt2032 spurcheck triggered: %d\n",n1);91} while ( (f>(f2-spectrum_to)) || (n2>-5));92n1++;93} while (n1<5);9495return 1;96}9798static int mt2032_compute_freq(struct dvb_frontend *fe,99unsigned int rfin,100unsigned int if1, unsigned int if2,101unsigned int spectrum_from,102unsigned int spectrum_to,103unsigned char *buf,104int *ret_sel,105unsigned int xogc) //all in Hz106{107struct microtune_priv *priv = fe->tuner_priv;108unsigned int fref,lo1,lo1n,lo1a,s,sel,lo1freq, desired_lo1,109desired_lo2,lo2,lo2n,lo2a,lo2num,lo2freq;110111fref= 5250 *1000; //5.25MHz112desired_lo1=rfin+if1;113114lo1=(2*(desired_lo1/1000)+(fref/1000)) / (2*fref/1000);115lo1n=lo1/8;116lo1a=lo1-(lo1n*8);117118s=rfin/1000/1000+1090;119120if(optimize_vco) {121if(s>1890) sel=0;122else if(s>1720) sel=1;123else if(s>1530) sel=2;124else if(s>1370) sel=3;125else sel=4; // >1090126}127else {128if(s>1790) sel=0; // <1958129else if(s>1617) sel=1;130else if(s>1449) sel=2;131else if(s>1291) sel=3;132else sel=4; // >1090133}134*ret_sel=sel;135136lo1freq=(lo1a+8*lo1n)*fref;137138tuner_dbg("mt2032: rfin=%d lo1=%d lo1n=%d lo1a=%d sel=%d, lo1freq=%d\n",139rfin,lo1,lo1n,lo1a,sel,lo1freq);140141desired_lo2=lo1freq-rfin-if2;142lo2=(desired_lo2)/fref;143lo2n=lo2/8;144lo2a=lo2-(lo2n*8);145lo2num=((desired_lo2/1000)%(fref/1000))* 3780/(fref/1000); //scale to fit in 32bit arith146lo2freq=(lo2a+8*lo2n)*fref + lo2num*(fref/1000)/3780*1000;147148tuner_dbg("mt2032: rfin=%d lo2=%d lo2n=%d lo2a=%d num=%d lo2freq=%d\n",149rfin,lo2,lo2n,lo2a,lo2num,lo2freq);150151if (lo1a > 7 || lo1n < 17 || lo1n > 48 || lo2a > 7 || lo2n < 17 ||152lo2n > 30) {153tuner_info("mt2032: frequency parameters out of range: %d %d %d %d\n",154lo1a, lo1n, lo2a,lo2n);155return(-1);156}157158mt2032_spurcheck(fe, lo1freq, desired_lo2, spectrum_from, spectrum_to);159// should recalculate lo1 (one step up/down)160161// set up MT2032 register map for transfer over i2c162buf[0]=lo1n-1;163buf[1]=lo1a | (sel<<4);164buf[2]=0x86; // LOGC165buf[3]=0x0f; //reserved166buf[4]=0x1f;167buf[5]=(lo2n-1) | (lo2a<<5);168if(rfin >400*1000*1000)169buf[6]=0xe4;170else171buf[6]=0xf4; // set PKEN per rev 1.2172buf[7]=8+xogc;173buf[8]=0xc3; //reserved174buf[9]=0x4e; //reserved175buf[10]=0xec; //reserved176buf[11]=(lo2num&0xff);177buf[12]=(lo2num>>8) |0x80; // Lo2RST178179return 0;180}181182static int mt2032_check_lo_lock(struct dvb_frontend *fe)183{184struct microtune_priv *priv = fe->tuner_priv;185int try,lock=0;186unsigned char buf[2];187188for(try=0;try<10;try++) {189buf[0]=0x0e;190tuner_i2c_xfer_send(&priv->i2c_props,buf,1);191tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);192tuner_dbg("mt2032 Reg.E=0x%02x\n",buf[0]);193lock=buf[0] &0x06;194195if (lock==6)196break;197198tuner_dbg("mt2032: pll wait 1ms for lock (0x%2x)\n",buf[0]);199udelay(1000);200}201return lock;202}203204static int mt2032_optimize_vco(struct dvb_frontend *fe,int sel,int lock)205{206struct microtune_priv *priv = fe->tuner_priv;207unsigned char buf[2];208int tad1;209210buf[0]=0x0f;211tuner_i2c_xfer_send(&priv->i2c_props,buf,1);212tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);213tuner_dbg("mt2032 Reg.F=0x%02x\n",buf[0]);214tad1=buf[0]&0x07;215216if(tad1 ==0) return lock;217if(tad1 ==1) return lock;218219if(tad1==2) {220if(sel==0)221return lock;222else sel--;223}224else {225if(sel<4)226sel++;227else228return lock;229}230231tuner_dbg("mt2032 optimize_vco: sel=%d\n",sel);232233buf[0]=0x0f;234buf[1]=sel;235tuner_i2c_xfer_send(&priv->i2c_props,buf,2);236lock=mt2032_check_lo_lock(fe);237return lock;238}239240241static void mt2032_set_if_freq(struct dvb_frontend *fe, unsigned int rfin,242unsigned int if1, unsigned int if2,243unsigned int from, unsigned int to)244{245unsigned char buf[21];246int lint_try,ret,sel,lock=0;247struct microtune_priv *priv = fe->tuner_priv;248249tuner_dbg("mt2032_set_if_freq rfin=%d if1=%d if2=%d from=%d to=%d\n",250rfin,if1,if2,from,to);251252buf[0]=0;253ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);254tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);255256buf[0]=0;257ret=mt2032_compute_freq(fe,rfin,if1,if2,from,to,&buf[1],&sel,priv->xogc);258if (ret<0)259return;260261// send only the relevant registers per Rev. 1.2262buf[0]=0;263ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,4);264buf[5]=5;265ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,4);266buf[11]=11;267ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+11,3);268if(ret!=3)269tuner_warn("i2c i/o error: rc == %d (should be 3)\n",ret);270271// wait for PLLs to lock (per manual), retry LINT if not.272for(lint_try=0; lint_try<2; lint_try++) {273lock=mt2032_check_lo_lock(fe);274275if(optimize_vco)276lock=mt2032_optimize_vco(fe,sel,lock);277if(lock==6) break;278279tuner_dbg("mt2032: re-init PLLs by LINT\n");280buf[0]=7;281buf[1]=0x80 +8+priv->xogc; // set LINT to re-init PLLs282tuner_i2c_xfer_send(&priv->i2c_props,buf,2);283mdelay(10);284buf[1]=8+priv->xogc;285tuner_i2c_xfer_send(&priv->i2c_props,buf,2);286}287288if (lock!=6)289tuner_warn("MT2032 Fatal Error: PLLs didn't lock.\n");290291buf[0]=2;292buf[1]=0x20; // LOGC for optimal phase noise293ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);294if (ret!=2)295tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);296}297298299static int mt2032_set_tv_freq(struct dvb_frontend *fe,300struct analog_parameters *params)301{302int if2,from,to;303304// signal bandwidth and picture carrier305if (params->std & V4L2_STD_525_60) {306// NTSC307from = 40750*1000;308to = 46750*1000;309if2 = 45750*1000;310} else {311// PAL312from = 32900*1000;313to = 39900*1000;314if2 = 38900*1000;315}316317mt2032_set_if_freq(fe, params->frequency*62500,3181090*1000*1000, if2, from, to);319320return 0;321}322323static int mt2032_set_radio_freq(struct dvb_frontend *fe,324struct analog_parameters *params)325{326struct microtune_priv *priv = fe->tuner_priv;327int if2;328329if (params->std & V4L2_STD_525_60) {330tuner_dbg("pinnacle ntsc\n");331if2 = 41300 * 1000;332} else {333tuner_dbg("pinnacle pal\n");334if2 = 33300 * 1000;335}336337// per Manual for FM tuning: first if center freq. 1085 MHz338mt2032_set_if_freq(fe, params->frequency * 125 / 2,3391085*1000*1000,if2,if2,if2);340341return 0;342}343344static int mt2032_set_params(struct dvb_frontend *fe,345struct analog_parameters *params)346{347struct microtune_priv *priv = fe->tuner_priv;348int ret = -EINVAL;349350switch (params->mode) {351case V4L2_TUNER_RADIO:352ret = mt2032_set_radio_freq(fe, params);353priv->frequency = params->frequency * 125 / 2;354break;355case V4L2_TUNER_ANALOG_TV:356case V4L2_TUNER_DIGITAL_TV:357ret = mt2032_set_tv_freq(fe, params);358priv->frequency = params->frequency * 62500;359break;360}361362return ret;363}364365static struct dvb_tuner_ops mt2032_tuner_ops = {366.set_analog_params = mt2032_set_params,367.release = microtune_release,368.get_frequency = microtune_get_frequency,369};370371// Initialization as described in "MT203x Programming Procedures", Rev 1.2, Feb.2001372static int mt2032_init(struct dvb_frontend *fe)373{374struct microtune_priv *priv = fe->tuner_priv;375unsigned char buf[21];376int ret,xogc,xok=0;377378// Initialize Registers per spec.379buf[1]=2; // Index to register 2380buf[2]=0xff;381buf[3]=0x0f;382buf[4]=0x1f;383ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+1,4);384385buf[5]=6; // Index register 6386buf[6]=0xe4;387buf[7]=0x8f;388buf[8]=0xc3;389buf[9]=0x4e;390buf[10]=0xec;391ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+5,6);392393buf[12]=13; // Index register 13394buf[13]=0x32;395ret=tuner_i2c_xfer_send(&priv->i2c_props,buf+12,2);396397// Adjust XOGC (register 7), wait for XOK398xogc=7;399do {400tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);401mdelay(10);402buf[0]=0x0e;403tuner_i2c_xfer_send(&priv->i2c_props,buf,1);404tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);405xok=buf[0]&0x01;406tuner_dbg("mt2032: xok = 0x%02x\n",xok);407if (xok == 1) break;408409xogc--;410tuner_dbg("mt2032: xogc = 0x%02x\n",xogc&0x07);411if (xogc == 3) {412xogc=4; // min. 4 per spec413break;414}415buf[0]=0x07;416buf[1]=0x88 + xogc;417ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);418if (ret!=2)419tuner_warn("i2c i/o error: rc == %d (should be 2)\n",ret);420} while (xok != 1 );421priv->xogc=xogc;422423memcpy(&fe->ops.tuner_ops, &mt2032_tuner_ops, sizeof(struct dvb_tuner_ops));424425return(1);426}427428static void mt2050_set_antenna(struct dvb_frontend *fe, unsigned char antenna)429{430struct microtune_priv *priv = fe->tuner_priv;431unsigned char buf[2];432int ret;433434buf[0] = 6;435buf[1] = antenna ? 0x11 : 0x10;436ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2);437tuner_dbg("mt2050: enabled antenna connector %d\n", antenna);438}439440static void mt2050_set_if_freq(struct dvb_frontend *fe,unsigned int freq, unsigned int if2)441{442struct microtune_priv *priv = fe->tuner_priv;443unsigned int if1=1218*1000*1000;444unsigned int f_lo1,f_lo2,lo1,lo2,f_lo1_modulo,f_lo2_modulo,num1,num2,div1a,div1b,div2a,div2b;445int ret;446unsigned char buf[6];447448tuner_dbg("mt2050_set_if_freq freq=%d if1=%d if2=%d\n",449freq,if1,if2);450451f_lo1=freq+if1;452f_lo1=(f_lo1/1000000)*1000000;453454f_lo2=f_lo1-freq-if2;455f_lo2=(f_lo2/50000)*50000;456457lo1=f_lo1/4000000;458lo2=f_lo2/4000000;459460f_lo1_modulo= f_lo1-(lo1*4000000);461f_lo2_modulo= f_lo2-(lo2*4000000);462463num1=4*f_lo1_modulo/4000000;464num2=4096*(f_lo2_modulo/1000)/4000;465466// todo spurchecks467468div1a=(lo1/12)-1;469div1b=lo1-(div1a+1)*12;470471div2a=(lo2/8)-1;472div2b=lo2-(div2a+1)*8;473474if (debug > 1) {475tuner_dbg("lo1 lo2 = %d %d\n", lo1, lo2);476tuner_dbg("num1 num2 div1a div1b div2a div2b= %x %x %x %x %x %x\n",477num1,num2,div1a,div1b,div2a,div2b);478}479480buf[0]=1;481buf[1]= 4*div1b + num1;482if(freq<275*1000*1000) buf[1] = buf[1]|0x80;483484buf[2]=div1a;485buf[3]=32*div2b + num2/256;486buf[4]=num2-(num2/256)*256;487buf[5]=div2a;488if(num2!=0) buf[5]=buf[5]|0x40;489490if (debug > 1) {491int i;492tuner_dbg("bufs is: ");493for(i=0;i<6;i++)494printk("%x ",buf[i]);495printk("\n");496}497498ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,6);499if (ret!=6)500tuner_warn("i2c i/o error: rc == %d (should be 6)\n",ret);501}502503static int mt2050_set_tv_freq(struct dvb_frontend *fe,504struct analog_parameters *params)505{506unsigned int if2;507508if (params->std & V4L2_STD_525_60) {509// NTSC510if2 = 45750*1000;511} else {512// PAL513if2 = 38900*1000;514}515if (V4L2_TUNER_DIGITAL_TV == params->mode) {516// DVB (pinnacle 300i)517if2 = 36150*1000;518}519mt2050_set_if_freq(fe, params->frequency*62500, if2);520mt2050_set_antenna(fe, tv_antenna);521522return 0;523}524525static int mt2050_set_radio_freq(struct dvb_frontend *fe,526struct analog_parameters *params)527{528struct microtune_priv *priv = fe->tuner_priv;529int if2;530531if (params->std & V4L2_STD_525_60) {532tuner_dbg("pinnacle ntsc\n");533if2 = 41300 * 1000;534} else {535tuner_dbg("pinnacle pal\n");536if2 = 33300 * 1000;537}538539mt2050_set_if_freq(fe, params->frequency * 125 / 2, if2);540mt2050_set_antenna(fe, radio_antenna);541542return 0;543}544545static int mt2050_set_params(struct dvb_frontend *fe,546struct analog_parameters *params)547{548struct microtune_priv *priv = fe->tuner_priv;549int ret = -EINVAL;550551switch (params->mode) {552case V4L2_TUNER_RADIO:553ret = mt2050_set_radio_freq(fe, params);554priv->frequency = params->frequency * 125 / 2;555break;556case V4L2_TUNER_ANALOG_TV:557case V4L2_TUNER_DIGITAL_TV:558ret = mt2050_set_tv_freq(fe, params);559priv->frequency = params->frequency * 62500;560break;561}562563return ret;564}565566static struct dvb_tuner_ops mt2050_tuner_ops = {567.set_analog_params = mt2050_set_params,568.release = microtune_release,569.get_frequency = microtune_get_frequency,570};571572static int mt2050_init(struct dvb_frontend *fe)573{574struct microtune_priv *priv = fe->tuner_priv;575unsigned char buf[2];576int ret;577578buf[0]=6;579buf[1]=0x10;580ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // power581582buf[0]=0x0f;583buf[1]=0x0f;584ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,2); // m1lo585586buf[0]=0x0d;587ret=tuner_i2c_xfer_send(&priv->i2c_props,buf,1);588tuner_i2c_xfer_recv(&priv->i2c_props,buf,1);589590tuner_dbg("mt2050: sro is %x\n",buf[0]);591592memcpy(&fe->ops.tuner_ops, &mt2050_tuner_ops, sizeof(struct dvb_tuner_ops));593594return 0;595}596597struct dvb_frontend *microtune_attach(struct dvb_frontend *fe,598struct i2c_adapter* i2c_adap,599u8 i2c_addr)600{601struct microtune_priv *priv = NULL;602char *name;603unsigned char buf[21];604int company_code;605606priv = kzalloc(sizeof(struct microtune_priv), GFP_KERNEL);607if (priv == NULL)608return NULL;609fe->tuner_priv = priv;610611priv->i2c_props.addr = i2c_addr;612priv->i2c_props.adap = i2c_adap;613priv->i2c_props.name = "mt20xx";614615//priv->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */616617memset(buf,0,sizeof(buf));618619name = "unknown";620621tuner_i2c_xfer_send(&priv->i2c_props,buf,1);622tuner_i2c_xfer_recv(&priv->i2c_props,buf,21);623if (debug) {624int i;625tuner_dbg("MT20xx hexdump:");626for(i=0;i<21;i++) {627printk(" %02x",buf[i]);628if(((i+1)%8)==0) printk(" ");629}630printk("\n");631}632company_code = buf[0x11] << 8 | buf[0x12];633tuner_info("microtune: companycode=%04x part=%02x rev=%02x\n",634company_code,buf[0x13],buf[0x14]);635636637if (buf[0x13] < ARRAY_SIZE(microtune_part) &&638NULL != microtune_part[buf[0x13]])639name = microtune_part[buf[0x13]];640switch (buf[0x13]) {641case MT2032:642mt2032_init(fe);643break;644case MT2050:645mt2050_init(fe);646break;647default:648tuner_info("microtune %s found, not (yet?) supported, sorry :-/\n",649name);650return NULL;651}652653strlcpy(fe->ops.tuner_ops.info.name, name,654sizeof(fe->ops.tuner_ops.info.name));655tuner_info("microtune %s found, OK\n",name);656return fe;657}658659EXPORT_SYMBOL_GPL(microtune_attach);660661MODULE_DESCRIPTION("Microtune tuner driver");662MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");663MODULE_LICENSE("GPL");664665/*666* Overrides for Emacs so that we follow Linus's tabbing style.667* ---------------------------------------------------------------------------668* Local variables:669* c-basic-offset: 8670* End:671*/672673674