mt2063.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845
  1. #include <linux/init.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/string.h>
  5. #include "mt2063.h"
  6. static unsigned int verbose;
  7. module_param(verbose, int, 0644);
  8. /* Internal structures and types */
  9. /* FIXME: we probably don't need these new FE get/set property types for tuner */
  10. #define DVBFE_TUNER_SOFTWARE_SHUTDOWN 100
  11. #define DVBFE_TUNER_CLEAR_POWER_MASKBITS 101
  12. /* FIXME: Those two error codes need conversion*/
  13. /* Error: Upconverter PLL is not locked */
  14. #define MT2063_UPC_UNLOCK (0x80000002)
  15. /* Error: Downconverter PLL is not locked */
  16. #define MT2063_DNC_UNLOCK (0x80000004)
  17. /* Info: Unavoidable LO-related spur may be present in the output */
  18. #define MT2063_SPUR_PRESENT_ERR (0x00800000)
  19. /* Info: Mask of bits used for # of LO-related spurs that were avoided during tuning */
  20. #define MT2063_SPUR_CNT_MASK (0x001f0000)
  21. #define MT2063_SPUR_SHIFT (16)
  22. /* Info: Upconverter frequency is out of range (may be reason for MT_UPC_UNLOCK) */
  23. #define MT2063_UPC_RANGE (0x04000000)
  24. /* Info: Downconverter frequency is out of range (may be reason for MT_DPC_UNLOCK) */
  25. #define MT2063_DNC_RANGE (0x08000000)
  26. /*
  27. * Data Types
  28. */
  29. /*
  30. * Constant defining the version of the following structure
  31. * and therefore the API for this code.
  32. *
  33. * When compiling the tuner driver, the preprocessor will
  34. * check against this version number to make sure that
  35. * it matches the version that the tuner driver knows about.
  36. */
  37. /* DECT Frequency Avoidance */
  38. #define MT2063_DECT_AVOID_US_FREQS 0x00000001
  39. #define MT2063_DECT_AVOID_EURO_FREQS 0x00000002
  40. #define MT2063_EXCLUDE_US_DECT_FREQUENCIES(s) (((s) & MT2063_DECT_AVOID_US_FREQS) != 0)
  41. #define MT2063_EXCLUDE_EURO_DECT_FREQUENCIES(s) (((s) & MT2063_DECT_AVOID_EURO_FREQS) != 0)
  42. enum MT2063_DECT_Avoid_Type {
  43. MT2063_NO_DECT_AVOIDANCE = 0, /* Do not create DECT exclusion zones. */
  44. MT2063_AVOID_US_DECT = MT2063_DECT_AVOID_US_FREQS, /* Avoid US DECT frequencies. */
  45. MT2063_AVOID_EURO_DECT = MT2063_DECT_AVOID_EURO_FREQS, /* Avoid European DECT frequencies. */
  46. MT2063_AVOID_BOTH /* Avoid both regions. Not typically used. */
  47. };
  48. #define MT2063_MAX_ZONES 48
  49. struct MT2063_ExclZone_t {
  50. u32 min_;
  51. u32 max_;
  52. struct MT2063_ExclZone_t *next_;
  53. };
  54. /*
  55. * Structure of data needed for Spur Avoidance
  56. */
  57. struct MT2063_AvoidSpursData_t {
  58. u32 f_ref;
  59. u32 f_in;
  60. u32 f_LO1;
  61. u32 f_if1_Center;
  62. u32 f_if1_Request;
  63. u32 f_if1_bw;
  64. u32 f_LO2;
  65. u32 f_out;
  66. u32 f_out_bw;
  67. u32 f_LO1_Step;
  68. u32 f_LO2_Step;
  69. u32 f_LO1_FracN_Avoid;
  70. u32 f_LO2_FracN_Avoid;
  71. u32 f_zif_bw;
  72. u32 f_min_LO_Separation;
  73. u32 maxH1;
  74. u32 maxH2;
  75. enum MT2063_DECT_Avoid_Type avoidDECT;
  76. u32 bSpurPresent;
  77. u32 bSpurAvoided;
  78. u32 nSpursFound;
  79. u32 nZones;
  80. struct MT2063_ExclZone_t *freeZones;
  81. struct MT2063_ExclZone_t *usedZones;
  82. struct MT2063_ExclZone_t MT2063_ExclZones[MT2063_MAX_ZONES];
  83. };
  84. /*
  85. * Parameter for function MT2063_SetPowerMask that specifies the power down
  86. * of various sections of the MT2063.
  87. */
  88. enum MT2063_Mask_Bits {
  89. MT2063_REG_SD = 0x0040, /* Shutdown regulator */
  90. MT2063_SRO_SD = 0x0020, /* Shutdown SRO */
  91. MT2063_AFC_SD = 0x0010, /* Shutdown AFC A/D */
  92. MT2063_PD_SD = 0x0002, /* Enable power detector shutdown */
  93. MT2063_PDADC_SD = 0x0001, /* Enable power detector A/D shutdown */
  94. MT2063_VCO_SD = 0x8000, /* Enable VCO shutdown */
  95. MT2063_LTX_SD = 0x4000, /* Enable LTX shutdown */
  96. MT2063_LT1_SD = 0x2000, /* Enable LT1 shutdown */
  97. MT2063_LNA_SD = 0x1000, /* Enable LNA shutdown */
  98. MT2063_UPC_SD = 0x0800, /* Enable upconverter shutdown */
  99. MT2063_DNC_SD = 0x0400, /* Enable downconverter shutdown */
  100. MT2063_VGA_SD = 0x0200, /* Enable VGA shutdown */
  101. MT2063_AMP_SD = 0x0100, /* Enable AMP shutdown */
  102. MT2063_ALL_SD = 0xFF73, /* All shutdown bits for this tuner */
  103. MT2063_NONE_SD = 0x0000 /* No shutdown bits */
  104. };
  105. /*
  106. * Parameter for function MT2063_GetParam & MT2063_SetParam that
  107. * specifies the tuning algorithm parameter to be read/written.
  108. */
  109. enum MT2063_Param {
  110. /* tuner address set by MT2063_Open() */
  111. MT2063_IC_ADDR,
  112. /* max number of MT2063 tuners set by MT_TUNER_CNT in mt_userdef.h */
  113. MT2063_MAX_OPEN,
  114. /* current number of open MT2063 tuners set by MT2063_Open() */
  115. MT2063_NUM_OPEN,
  116. /* crystal frequency (default: 16000000 Hz) */
  117. MT2063_SRO_FREQ,
  118. /* min tuning step size (default: 50000 Hz) */
  119. MT2063_STEPSIZE,
  120. /* input center frequency set by MT2063_Tune() */
  121. MT2063_INPUT_FREQ,
  122. /* LO1 Frequency set by MT2063_Tune() */
  123. MT2063_LO1_FREQ,
  124. /* LO1 minimum step size (default: 250000 Hz) */
  125. MT2063_LO1_STEPSIZE,
  126. /* LO1 FracN keep-out region (default: 999999 Hz) */
  127. MT2063_LO1_FRACN_AVOID_PARAM,
  128. /* Current 1st IF in use set by MT2063_Tune() */
  129. MT2063_IF1_ACTUAL,
  130. /* Requested 1st IF set by MT2063_Tune() */
  131. MT2063_IF1_REQUEST,
  132. /* Center of 1st IF SAW filter (default: 1218000000 Hz) */
  133. MT2063_IF1_CENTER,
  134. /* Bandwidth of 1st IF SAW filter (default: 20000000 Hz) */
  135. MT2063_IF1_BW,
  136. /* zero-IF bandwidth (default: 2000000 Hz) */
  137. MT2063_ZIF_BW,
  138. /* LO2 Frequency set by MT2063_Tune() */
  139. MT2063_LO2_FREQ,
  140. /* LO2 minimum step size (default: 50000 Hz) */
  141. MT2063_LO2_STEPSIZE,
  142. /* LO2 FracN keep-out region (default: 374999 Hz) */
  143. MT2063_LO2_FRACN_AVOID,
  144. /* output center frequency set by MT2063_Tune() */
  145. MT2063_OUTPUT_FREQ,
  146. /* output bandwidth set by MT2063_Tune() */
  147. MT2063_OUTPUT_BW,
  148. /* min inter-tuner LO separation (default: 1000000 Hz) */
  149. MT2063_LO_SEPARATION,
  150. /* ID of avoid-spurs algorithm in use compile-time constant */
  151. MT2063_AS_ALG,
  152. /* max # of intra-tuner harmonics (default: 15) */
  153. MT2063_MAX_HARM1,
  154. /* max # of inter-tuner harmonics (default: 7) */
  155. MT2063_MAX_HARM2,
  156. /* # of 1st IF exclusion zones used set by MT2063_Tune() */
  157. MT2063_EXCL_ZONES,
  158. /* # of spurs found/avoided set by MT2063_Tune() */
  159. MT2063_NUM_SPURS,
  160. /* >0 spurs avoided set by MT2063_Tune() */
  161. MT2063_SPUR_AVOIDED,
  162. /* >0 spurs in output (mathematically) set by MT2063_Tune() */
  163. MT2063_SPUR_PRESENT,
  164. /* Receiver Mode for some parameters. 1 is DVB-T */
  165. MT2063_RCVR_MODE,
  166. /* directly set LNA attenuation, parameter is value to set */
  167. MT2063_ACLNA,
  168. /* maximum LNA attenuation, parameter is value to set */
  169. MT2063_ACLNA_MAX,
  170. /* directly set ATN attenuation. Paremeter is value to set. */
  171. MT2063_ACRF,
  172. /* maxium ATN attenuation. Paremeter is value to set. */
  173. MT2063_ACRF_MAX,
  174. /* directly set FIF attenuation. Paremeter is value to set. */
  175. MT2063_ACFIF,
  176. /* maxium FIF attenuation. Paremeter is value to set. */
  177. MT2063_ACFIF_MAX,
  178. /* LNA Rin */
  179. MT2063_LNA_RIN,
  180. /* Power Detector LNA level target */
  181. MT2063_LNA_TGT,
  182. /* Power Detector 1 level */
  183. MT2063_PD1,
  184. /* Power Detector 1 level target */
  185. MT2063_PD1_TGT,
  186. /* Power Detector 2 level */
  187. MT2063_PD2,
  188. /* Power Detector 2 level target */
  189. MT2063_PD2_TGT,
  190. /* Selects, which DNC is activ */
  191. MT2063_DNC_OUTPUT_ENABLE,
  192. /* VGA gain code */
  193. MT2063_VGAGC,
  194. /* VGA bias current */
  195. MT2063_VGAOI,
  196. /* TAGC, determins the speed of the AGC */
  197. MT2063_TAGC,
  198. /* AMP gain code */
  199. MT2063_AMPGC,
  200. /* Control setting to avoid DECT freqs (default: MT_AVOID_BOTH) */
  201. MT2063_AVOID_DECT,
  202. /* Cleartune filter selection: 0 - by IC (default), 1 - by software */
  203. MT2063_CTFILT_SW,
  204. MT2063_EOP /* last entry in enumerated list */
  205. };
  206. /*
  207. * Parameter for selecting tuner mode
  208. */
  209. enum MT2063_RCVR_MODES {
  210. MT2063_CABLE_QAM = 0, /* Digital cable */
  211. MT2063_CABLE_ANALOG, /* Analog cable */
  212. MT2063_OFFAIR_COFDM, /* Digital offair */
  213. MT2063_OFFAIR_COFDM_SAWLESS, /* Digital offair without SAW */
  214. MT2063_OFFAIR_ANALOG, /* Analog offair */
  215. MT2063_OFFAIR_8VSB, /* Analog offair */
  216. MT2063_NUM_RCVR_MODES
  217. };
  218. /*
  219. * Possible values for MT2063_DNC_OUTPUT
  220. */
  221. enum MT2063_DNC_Output_Enable {
  222. MT2063_DNC_NONE = 0,
  223. MT2063_DNC_1,
  224. MT2063_DNC_2,
  225. MT2063_DNC_BOTH
  226. };
  227. /*
  228. ** Two-wire serial bus subaddresses of the tuner registers.
  229. ** Also known as the tuner's register addresses.
  230. */
  231. enum MT2063_Register_Offsets {
  232. MT2063_REG_PART_REV = 0, /* 0x00: Part/Rev Code */
  233. MT2063_REG_LO1CQ_1, /* 0x01: LO1C Queued Byte 1 */
  234. MT2063_REG_LO1CQ_2, /* 0x02: LO1C Queued Byte 2 */
  235. MT2063_REG_LO2CQ_1, /* 0x03: LO2C Queued Byte 1 */
  236. MT2063_REG_LO2CQ_2, /* 0x04: LO2C Queued Byte 2 */
  237. MT2063_REG_LO2CQ_3, /* 0x05: LO2C Queued Byte 3 */
  238. MT2063_REG_RSVD_06, /* 0x06: Reserved */
  239. MT2063_REG_LO_STATUS, /* 0x07: LO Status */
  240. MT2063_REG_FIFFC, /* 0x08: FIFF Center */
  241. MT2063_REG_CLEARTUNE, /* 0x09: ClearTune Filter */
  242. MT2063_REG_ADC_OUT, /* 0x0A: ADC_OUT */
  243. MT2063_REG_LO1C_1, /* 0x0B: LO1C Byte 1 */
  244. MT2063_REG_LO1C_2, /* 0x0C: LO1C Byte 2 */
  245. MT2063_REG_LO2C_1, /* 0x0D: LO2C Byte 1 */
  246. MT2063_REG_LO2C_2, /* 0x0E: LO2C Byte 2 */
  247. MT2063_REG_LO2C_3, /* 0x0F: LO2C Byte 3 */
  248. MT2063_REG_RSVD_10, /* 0x10: Reserved */
  249. MT2063_REG_PWR_1, /* 0x11: PWR Byte 1 */
  250. MT2063_REG_PWR_2, /* 0x12: PWR Byte 2 */
  251. MT2063_REG_TEMP_STATUS, /* 0x13: Temp Status */
  252. MT2063_REG_XO_STATUS, /* 0x14: Crystal Status */
  253. MT2063_REG_RF_STATUS, /* 0x15: RF Attn Status */
  254. MT2063_REG_FIF_STATUS, /* 0x16: FIF Attn Status */
  255. MT2063_REG_LNA_OV, /* 0x17: LNA Attn Override */
  256. MT2063_REG_RF_OV, /* 0x18: RF Attn Override */
  257. MT2063_REG_FIF_OV, /* 0x19: FIF Attn Override */
  258. MT2063_REG_LNA_TGT, /* 0x1A: Reserved */
  259. MT2063_REG_PD1_TGT, /* 0x1B: Pwr Det 1 Target */
  260. MT2063_REG_PD2_TGT, /* 0x1C: Pwr Det 2 Target */
  261. MT2063_REG_RSVD_1D, /* 0x1D: Reserved */
  262. MT2063_REG_RSVD_1E, /* 0x1E: Reserved */
  263. MT2063_REG_RSVD_1F, /* 0x1F: Reserved */
  264. MT2063_REG_RSVD_20, /* 0x20: Reserved */
  265. MT2063_REG_BYP_CTRL, /* 0x21: Bypass Control */
  266. MT2063_REG_RSVD_22, /* 0x22: Reserved */
  267. MT2063_REG_RSVD_23, /* 0x23: Reserved */
  268. MT2063_REG_RSVD_24, /* 0x24: Reserved */
  269. MT2063_REG_RSVD_25, /* 0x25: Reserved */
  270. MT2063_REG_RSVD_26, /* 0x26: Reserved */
  271. MT2063_REG_RSVD_27, /* 0x27: Reserved */
  272. MT2063_REG_FIFF_CTRL, /* 0x28: FIFF Control */
  273. MT2063_REG_FIFF_OFFSET, /* 0x29: FIFF Offset */
  274. MT2063_REG_CTUNE_CTRL, /* 0x2A: Reserved */
  275. MT2063_REG_CTUNE_OV, /* 0x2B: Reserved */
  276. MT2063_REG_CTRL_2C, /* 0x2C: Reserved */
  277. MT2063_REG_FIFF_CTRL2, /* 0x2D: Fiff Control */
  278. MT2063_REG_RSVD_2E, /* 0x2E: Reserved */
  279. MT2063_REG_DNC_GAIN, /* 0x2F: DNC Control */
  280. MT2063_REG_VGA_GAIN, /* 0x30: VGA Gain Ctrl */
  281. MT2063_REG_RSVD_31, /* 0x31: Reserved */
  282. MT2063_REG_TEMP_SEL, /* 0x32: Temperature Selection */
  283. MT2063_REG_RSVD_33, /* 0x33: Reserved */
  284. MT2063_REG_RSVD_34, /* 0x34: Reserved */
  285. MT2063_REG_RSVD_35, /* 0x35: Reserved */
  286. MT2063_REG_RSVD_36, /* 0x36: Reserved */
  287. MT2063_REG_RSVD_37, /* 0x37: Reserved */
  288. MT2063_REG_RSVD_38, /* 0x38: Reserved */
  289. MT2063_REG_RSVD_39, /* 0x39: Reserved */
  290. MT2063_REG_RSVD_3A, /* 0x3A: Reserved */
  291. MT2063_REG_RSVD_3B, /* 0x3B: Reserved */
  292. MT2063_REG_RSVD_3C, /* 0x3C: Reserved */
  293. MT2063_REG_END_REGS
  294. };
  295. enum MTTune_atv_standard {
  296. MTTUNEA_UNKNOWN = 0,
  297. MTTUNEA_PAL_B,
  298. MTTUNEA_PAL_G,
  299. MTTUNEA_PAL_I,
  300. MTTUNEA_PAL_L,
  301. MTTUNEA_PAL_MN,
  302. MTTUNEA_PAL_DK,
  303. MTTUNEA_DIGITAL,
  304. MTTUNEA_FMRADIO,
  305. MTTUNEA_DVBC,
  306. MTTUNEA_DVBT
  307. };
  308. struct mt2063_state {
  309. struct i2c_adapter *i2c;
  310. const struct mt2063_config *config;
  311. struct dvb_tuner_ops ops;
  312. struct dvb_frontend *frontend;
  313. struct tuner_state status;
  314. enum MTTune_atv_standard tv_type;
  315. u32 frequency;
  316. u32 srate;
  317. u32 bandwidth;
  318. u32 reference;
  319. u32 tuner_id;
  320. struct MT2063_AvoidSpursData_t AS_Data;
  321. u32 f_IF1_actual;
  322. u32 rcvr_mode;
  323. u32 ctfilt_sw;
  324. u32 CTFiltMax[31];
  325. u32 num_regs;
  326. u8 reg[MT2063_REG_END_REGS];
  327. };
  328. /* Prototypes */
  329. static void MT2063_AddExclZone(struct MT2063_AvoidSpursData_t *pAS_Info,
  330. u32 f_min, u32 f_max);
  331. static u32 MT2063_GetReg(struct mt2063_state *state, u8 reg, u8 * val);
  332. static u32 MT2063_GetParam(struct mt2063_state *state, enum MT2063_Param param, u32 * pValue);
  333. static u32 MT2063_SetReg(struct mt2063_state *state, u8 reg, u8 val);
  334. static u32 MT2063_SetParam(struct mt2063_state *state, enum MT2063_Param param,
  335. enum MT2063_DNC_Output_Enable nValue);
  336. /*****************/
  337. /* From drivers/media/common/tuners/mt2063_cfg.h */
  338. unsigned int mt2063_setTune(struct dvb_frontend *fe, u32 f_in,
  339. u32 bw_in,
  340. enum MTTune_atv_standard tv_type)
  341. {
  342. struct dvb_frontend_ops *frontend_ops = NULL;
  343. struct dvb_tuner_ops *tuner_ops = NULL;
  344. struct tuner_state t_state;
  345. struct mt2063_state *state = fe->tuner_priv;
  346. int err = 0;
  347. t_state.frequency = f_in;
  348. t_state.bandwidth = bw_in;
  349. state->tv_type = tv_type;
  350. if (&fe->ops)
  351. frontend_ops = &fe->ops;
  352. if (&frontend_ops->tuner_ops)
  353. tuner_ops = &frontend_ops->tuner_ops;
  354. if (tuner_ops->set_state) {
  355. if ((err =
  356. tuner_ops->set_state(fe, DVBFE_TUNER_FREQUENCY,
  357. &t_state)) < 0) {
  358. printk("%s: Invalid parameter\n", __func__);
  359. return err;
  360. }
  361. }
  362. return err;
  363. }
  364. unsigned int mt2063_lockStatus(struct dvb_frontend *fe)
  365. {
  366. struct dvb_frontend_ops *frontend_ops = &fe->ops;
  367. struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
  368. struct tuner_state t_state;
  369. int err = 0;
  370. if (&fe->ops)
  371. frontend_ops = &fe->ops;
  372. if (&frontend_ops->tuner_ops)
  373. tuner_ops = &frontend_ops->tuner_ops;
  374. if (tuner_ops->get_state) {
  375. if ((err =
  376. tuner_ops->get_state(fe, DVBFE_TUNER_REFCLOCK,
  377. &t_state)) < 0) {
  378. printk("%s: Invalid parameter\n", __func__);
  379. return err;
  380. }
  381. }
  382. return err;
  383. }
  384. unsigned int tuner_MT2063_Open(struct dvb_frontend *fe)
  385. {
  386. struct dvb_frontend_ops *frontend_ops = &fe->ops;
  387. struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
  388. struct tuner_state t_state;
  389. int err = 0;
  390. if (&fe->ops)
  391. frontend_ops = &fe->ops;
  392. if (&frontend_ops->tuner_ops)
  393. tuner_ops = &frontend_ops->tuner_ops;
  394. if (tuner_ops->set_state) {
  395. if ((err =
  396. tuner_ops->set_state(fe, DVBFE_TUNER_OPEN,
  397. &t_state)) < 0) {
  398. printk("%s: Invalid parameter\n", __func__);
  399. return err;
  400. }
  401. }
  402. return err;
  403. }
  404. unsigned int tuner_MT2063_SoftwareShutdown(struct dvb_frontend *fe)
  405. {
  406. struct dvb_frontend_ops *frontend_ops = &fe->ops;
  407. struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
  408. struct tuner_state t_state;
  409. int err = 0;
  410. if (&fe->ops)
  411. frontend_ops = &fe->ops;
  412. if (&frontend_ops->tuner_ops)
  413. tuner_ops = &frontend_ops->tuner_ops;
  414. if (tuner_ops->set_state) {
  415. if ((err =
  416. tuner_ops->set_state(fe, DVBFE_TUNER_SOFTWARE_SHUTDOWN,
  417. &t_state)) < 0) {
  418. printk("%s: Invalid parameter\n", __func__);
  419. return err;
  420. }
  421. }
  422. return err;
  423. }
  424. unsigned int tuner_MT2063_ClearPowerMaskBits(struct dvb_frontend *fe)
  425. {
  426. struct dvb_frontend_ops *frontend_ops = &fe->ops;
  427. struct dvb_tuner_ops *tuner_ops = &frontend_ops->tuner_ops;
  428. struct tuner_state t_state;
  429. int err = 0;
  430. if (&fe->ops)
  431. frontend_ops = &fe->ops;
  432. if (&frontend_ops->tuner_ops)
  433. tuner_ops = &frontend_ops->tuner_ops;
  434. if (tuner_ops->set_state) {
  435. if ((err =
  436. tuner_ops->set_state(fe, DVBFE_TUNER_CLEAR_POWER_MASKBITS,
  437. &t_state)) < 0) {
  438. printk("%s: Invalid parameter\n", __func__);
  439. return err;
  440. }
  441. }
  442. return err;
  443. }
  444. /*
  445. * mt2063_write - Write data into the I2C bus
  446. */
  447. static u32 mt2063_write(struct mt2063_state *state,
  448. u8 reg, u8 *data, u32 len)
  449. {
  450. struct dvb_frontend *fe = state->frontend;
  451. int ret;
  452. u8 buf[60];
  453. struct i2c_msg msg = {
  454. .addr = state->config->tuner_address,
  455. .flags = 0,
  456. .buf = buf,
  457. .len = len + 1
  458. };
  459. msg.buf[0] = reg;
  460. memcpy(msg.buf + 1, data, len);
  461. fe->ops.i2c_gate_ctrl(fe, 1);
  462. ret = i2c_transfer(state->i2c, &msg, 1);
  463. fe->ops.i2c_gate_ctrl(fe, 0);
  464. if (ret < 0)
  465. printk("mt2063_writeregs error ret=%d\n", ret);
  466. return ret;
  467. }
  468. /*
  469. * mt2063_read - Read data from the I2C bus
  470. */
  471. static u32 mt2063_read(struct mt2063_state *state,
  472. u8 subAddress, u8 *pData, u32 cnt)
  473. {
  474. u32 status = 0; /* Status to be returned */
  475. struct dvb_frontend *fe = state->frontend;
  476. u32 i = 0;
  477. fe->ops.i2c_gate_ctrl(fe, 1);
  478. for (i = 0; i < cnt; i++) {
  479. int ret;
  480. u8 b0[] = { subAddress + i };
  481. struct i2c_msg msg[] = {
  482. {
  483. .addr = state->config->tuner_address,
  484. .flags = I2C_M_RD,
  485. .buf = b0,
  486. .len = 1
  487. }, {
  488. .addr = state->config->tuner_address,
  489. .flags = I2C_M_RD,
  490. .buf = pData + 1,
  491. .len = 1
  492. }
  493. };
  494. ret = i2c_transfer(state->i2c, msg, 2);
  495. if (ret < 0)
  496. break;
  497. }
  498. fe->ops.i2c_gate_ctrl(fe, 0);
  499. return (status);
  500. }
  501. /*
  502. * FIXME: Is this really needed?
  503. */
  504. static int MT2063_Sleep(struct dvb_frontend *fe)
  505. {
  506. /*
  507. ** ToDo: Add code here to implement a OS blocking
  508. ** for a period of "nMinDelayTime" milliseconds.
  509. */
  510. msleep(10);
  511. return 0;
  512. }
  513. /*
  514. * Microtune spur avoidance
  515. */
  516. /* Implement ceiling, floor functions. */
  517. #define ceil(n, d) (((n) < 0) ? (-((-(n))/(d))) : (n)/(d) + ((n)%(d) != 0))
  518. #define floor(n, d) (((n) < 0) ? (-((-(n))/(d))) - ((n)%(d) != 0) : (n)/(d))
  519. struct MT2063_FIFZone_t {
  520. s32 min_;
  521. s32 max_;
  522. };
  523. /*
  524. ** Reset all exclusion zones.
  525. ** Add zones to protect the PLL FracN regions near zero
  526. **
  527. ** N/A I 06-17-2008 RSK Ver 1.19: Refactoring avoidance of DECT
  528. ** frequencies into MT_ResetExclZones().
  529. */
  530. static void MT2063_ResetExclZones(struct MT2063_AvoidSpursData_t *pAS_Info)
  531. {
  532. u32 center;
  533. pAS_Info->nZones = 0; /* this clears the used list */
  534. pAS_Info->usedZones = NULL; /* reset ptr */
  535. pAS_Info->freeZones = NULL; /* reset ptr */
  536. center =
  537. pAS_Info->f_ref *
  538. ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 +
  539. pAS_Info->f_in) / pAS_Info->f_ref) - pAS_Info->f_in;
  540. while (center <
  541. pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
  542. pAS_Info->f_LO1_FracN_Avoid) {
  543. /* Exclude LO1 FracN */
  544. MT2063_AddExclZone(pAS_Info,
  545. center - pAS_Info->f_LO1_FracN_Avoid,
  546. center - 1);
  547. MT2063_AddExclZone(pAS_Info, center + 1,
  548. center + pAS_Info->f_LO1_FracN_Avoid);
  549. center += pAS_Info->f_ref;
  550. }
  551. center =
  552. pAS_Info->f_ref *
  553. ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 -
  554. pAS_Info->f_out) / pAS_Info->f_ref) + pAS_Info->f_out;
  555. while (center <
  556. pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
  557. pAS_Info->f_LO2_FracN_Avoid) {
  558. /* Exclude LO2 FracN */
  559. MT2063_AddExclZone(pAS_Info,
  560. center - pAS_Info->f_LO2_FracN_Avoid,
  561. center - 1);
  562. MT2063_AddExclZone(pAS_Info, center + 1,
  563. center + pAS_Info->f_LO2_FracN_Avoid);
  564. center += pAS_Info->f_ref;
  565. }
  566. if (MT2063_EXCLUDE_US_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
  567. /* Exclude LO1 values that conflict with DECT channels */
  568. MT2063_AddExclZone(pAS_Info, 1920836000 - pAS_Info->f_in, 1922236000 - pAS_Info->f_in); /* Ctr = 1921.536 */
  569. MT2063_AddExclZone(pAS_Info, 1922564000 - pAS_Info->f_in, 1923964000 - pAS_Info->f_in); /* Ctr = 1923.264 */
  570. MT2063_AddExclZone(pAS_Info, 1924292000 - pAS_Info->f_in, 1925692000 - pAS_Info->f_in); /* Ctr = 1924.992 */
  571. MT2063_AddExclZone(pAS_Info, 1926020000 - pAS_Info->f_in, 1927420000 - pAS_Info->f_in); /* Ctr = 1926.720 */
  572. MT2063_AddExclZone(pAS_Info, 1927748000 - pAS_Info->f_in, 1929148000 - pAS_Info->f_in); /* Ctr = 1928.448 */
  573. }
  574. if (MT2063_EXCLUDE_EURO_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
  575. MT2063_AddExclZone(pAS_Info, 1896644000 - pAS_Info->f_in, 1898044000 - pAS_Info->f_in); /* Ctr = 1897.344 */
  576. MT2063_AddExclZone(pAS_Info, 1894916000 - pAS_Info->f_in, 1896316000 - pAS_Info->f_in); /* Ctr = 1895.616 */
  577. MT2063_AddExclZone(pAS_Info, 1893188000 - pAS_Info->f_in, 1894588000 - pAS_Info->f_in); /* Ctr = 1893.888 */
  578. MT2063_AddExclZone(pAS_Info, 1891460000 - pAS_Info->f_in, 1892860000 - pAS_Info->f_in); /* Ctr = 1892.16 */
  579. MT2063_AddExclZone(pAS_Info, 1889732000 - pAS_Info->f_in, 1891132000 - pAS_Info->f_in); /* Ctr = 1890.432 */
  580. MT2063_AddExclZone(pAS_Info, 1888004000 - pAS_Info->f_in, 1889404000 - pAS_Info->f_in); /* Ctr = 1888.704 */
  581. MT2063_AddExclZone(pAS_Info, 1886276000 - pAS_Info->f_in, 1887676000 - pAS_Info->f_in); /* Ctr = 1886.976 */
  582. MT2063_AddExclZone(pAS_Info, 1884548000 - pAS_Info->f_in, 1885948000 - pAS_Info->f_in); /* Ctr = 1885.248 */
  583. MT2063_AddExclZone(pAS_Info, 1882820000 - pAS_Info->f_in, 1884220000 - pAS_Info->f_in); /* Ctr = 1883.52 */
  584. MT2063_AddExclZone(pAS_Info, 1881092000 - pAS_Info->f_in, 1882492000 - pAS_Info->f_in); /* Ctr = 1881.792 */
  585. }
  586. }
  587. static struct MT2063_ExclZone_t *InsertNode(struct MT2063_AvoidSpursData_t
  588. *pAS_Info,
  589. struct MT2063_ExclZone_t *pPrevNode)
  590. {
  591. struct MT2063_ExclZone_t *pNode;
  592. /* Check for a node in the free list */
  593. if (pAS_Info->freeZones != NULL) {
  594. /* Use one from the free list */
  595. pNode = pAS_Info->freeZones;
  596. pAS_Info->freeZones = pNode->next_;
  597. } else {
  598. /* Grab a node from the array */
  599. pNode = &pAS_Info->MT2063_ExclZones[pAS_Info->nZones];
  600. }
  601. if (pPrevNode != NULL) {
  602. pNode->next_ = pPrevNode->next_;
  603. pPrevNode->next_ = pNode;
  604. } else { /* insert at the beginning of the list */
  605. pNode->next_ = pAS_Info->usedZones;
  606. pAS_Info->usedZones = pNode;
  607. }
  608. pAS_Info->nZones++;
  609. return pNode;
  610. }
  611. static struct MT2063_ExclZone_t *RemoveNode(struct MT2063_AvoidSpursData_t
  612. *pAS_Info,
  613. struct MT2063_ExclZone_t *pPrevNode,
  614. struct MT2063_ExclZone_t
  615. *pNodeToRemove)
  616. {
  617. struct MT2063_ExclZone_t *pNext = pNodeToRemove->next_;
  618. /* Make previous node point to the subsequent node */
  619. if (pPrevNode != NULL)
  620. pPrevNode->next_ = pNext;
  621. /* Add pNodeToRemove to the beginning of the freeZones */
  622. pNodeToRemove->next_ = pAS_Info->freeZones;
  623. pAS_Info->freeZones = pNodeToRemove;
  624. /* Decrement node count */
  625. pAS_Info->nZones--;
  626. return pNext;
  627. }
  628. /*****************************************************************************
  629. **
  630. ** Name: MT_AddExclZone
  631. **
  632. ** Description: Add (and merge) an exclusion zone into the list.
  633. ** If the range (f_min, f_max) is totally outside the
  634. ** 1st IF BW, ignore the entry.
  635. ** If the range (f_min, f_max) is negative, ignore the entry.
  636. **
  637. ** Revision History:
  638. **
  639. ** SCR Date Author Description
  640. ** -------------------------------------------------------------------------
  641. ** 103 01-31-2005 DAD Ver 1.14: In MT_AddExclZone(), if the range
  642. ** (f_min, f_max) < 0, ignore the entry.
  643. **
  644. *****************************************************************************/
  645. static void MT2063_AddExclZone(struct MT2063_AvoidSpursData_t *pAS_Info,
  646. u32 f_min, u32 f_max)
  647. {
  648. struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
  649. struct MT2063_ExclZone_t *pPrev = NULL;
  650. struct MT2063_ExclZone_t *pNext = NULL;
  651. /* Check to see if this overlaps the 1st IF filter */
  652. if ((f_max > (pAS_Info->f_if1_Center - (pAS_Info->f_if1_bw / 2)))
  653. && (f_min < (pAS_Info->f_if1_Center + (pAS_Info->f_if1_bw / 2)))
  654. && (f_min < f_max)) {
  655. /*
  656. ** 1 2 3 4 5 6
  657. **
  658. ** New entry: |---| |--| |--| |-| |---| |--|
  659. ** or or or or or
  660. ** Existing: |--| |--| |--| |---| |-| |--|
  661. */
  662. /* Check for our place in the list */
  663. while ((pNode != NULL) && (pNode->max_ < f_min)) {
  664. pPrev = pNode;
  665. pNode = pNode->next_;
  666. }
  667. if ((pNode != NULL) && (pNode->min_ < f_max)) {
  668. /* Combine me with pNode */
  669. if (f_min < pNode->min_)
  670. pNode->min_ = f_min;
  671. if (f_max > pNode->max_)
  672. pNode->max_ = f_max;
  673. } else {
  674. pNode = InsertNode(pAS_Info, pPrev);
  675. pNode->min_ = f_min;
  676. pNode->max_ = f_max;
  677. }
  678. /* Look for merging possibilities */
  679. pNext = pNode->next_;
  680. while ((pNext != NULL) && (pNext->min_ < pNode->max_)) {
  681. if (pNext->max_ > pNode->max_)
  682. pNode->max_ = pNext->max_;
  683. pNext = RemoveNode(pAS_Info, pNode, pNext); /* Remove pNext, return ptr to pNext->next */
  684. }
  685. }
  686. }
  687. /*****************************************************************************
  688. **
  689. ** Name: MT_ChooseFirstIF
  690. **
  691. ** Description: Choose the best available 1st IF
  692. ** If f_Desired is not excluded, choose that first.
  693. ** Otherwise, return the value closest to f_Center that is
  694. ** not excluded
  695. **
  696. ** Revision History:
  697. **
  698. ** SCR Date Author Description
  699. ** -------------------------------------------------------------------------
  700. ** 117 03-29-2007 RSK Ver 1.15: Re-wrote to match search order from
  701. ** tuner DLL.
  702. ** 147 07-27-2007 RSK Ver 1.17: Corrected calculation (-) to (+)
  703. ** Added logic to force f_Center within 1/2 f_Step.
  704. **
  705. *****************************************************************************/
  706. static u32 MT2063_ChooseFirstIF(struct MT2063_AvoidSpursData_t *pAS_Info)
  707. {
  708. /*
  709. ** Update "f_Desired" to be the nearest "combinational-multiple" of "f_LO1_Step".
  710. ** The resulting number, F_LO1 must be a multiple of f_LO1_Step. And F_LO1 is the arithmetic sum
  711. ** of f_in + f_Center. Neither f_in, nor f_Center must be a multiple of f_LO1_Step.
  712. ** However, the sum must be.
  713. */
  714. const u32 f_Desired =
  715. pAS_Info->f_LO1_Step *
  716. ((pAS_Info->f_if1_Request + pAS_Info->f_in +
  717. pAS_Info->f_LO1_Step / 2) / pAS_Info->f_LO1_Step) -
  718. pAS_Info->f_in;
  719. const u32 f_Step =
  720. (pAS_Info->f_LO1_Step >
  721. pAS_Info->f_LO2_Step) ? pAS_Info->f_LO1_Step : pAS_Info->
  722. f_LO2_Step;
  723. u32 f_Center;
  724. s32 i;
  725. s32 j = 0;
  726. u32 bDesiredExcluded = 0;
  727. u32 bZeroExcluded = 0;
  728. s32 tmpMin, tmpMax;
  729. s32 bestDiff;
  730. struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
  731. struct MT2063_FIFZone_t zones[MT2063_MAX_ZONES];
  732. if (pAS_Info->nZones == 0)
  733. return f_Desired;
  734. /* f_Center needs to be an integer multiple of f_Step away from f_Desired */
  735. if (pAS_Info->f_if1_Center > f_Desired)
  736. f_Center =
  737. f_Desired +
  738. f_Step *
  739. ((pAS_Info->f_if1_Center - f_Desired +
  740. f_Step / 2) / f_Step);
  741. else
  742. f_Center =
  743. f_Desired -
  744. f_Step *
  745. ((f_Desired - pAS_Info->f_if1_Center +
  746. f_Step / 2) / f_Step);
  747. //assert;
  748. //if (!abs((s32) f_Center - (s32) pAS_Info->f_if1_Center) <= (s32) (f_Step/2))
  749. // return 0;
  750. /* Take MT_ExclZones, center around f_Center and change the resolution to f_Step */
  751. while (pNode != NULL) {
  752. /* floor function */
  753. tmpMin =
  754. floor((s32) (pNode->min_ - f_Center), (s32) f_Step);
  755. /* ceil function */
  756. tmpMax =
  757. ceil((s32) (pNode->max_ - f_Center), (s32) f_Step);
  758. if ((pNode->min_ < f_Desired) && (pNode->max_ > f_Desired))
  759. bDesiredExcluded = 1;
  760. if ((tmpMin < 0) && (tmpMax > 0))
  761. bZeroExcluded = 1;
  762. /* See if this zone overlaps the previous */
  763. if ((j > 0) && (tmpMin < zones[j - 1].max_))
  764. zones[j - 1].max_ = tmpMax;
  765. else {
  766. /* Add new zone */
  767. //assert(j<MT2063_MAX_ZONES);
  768. //if (j>=MT2063_MAX_ZONES)
  769. //break;
  770. zones[j].min_ = tmpMin;
  771. zones[j].max_ = tmpMax;
  772. j++;
  773. }
  774. pNode = pNode->next_;
  775. }
  776. /*
  777. ** If the desired is okay, return with it
  778. */
  779. if (bDesiredExcluded == 0)
  780. return f_Desired;
  781. /*
  782. ** If the desired is excluded and the center is okay, return with it
  783. */
  784. if (bZeroExcluded == 0)
  785. return f_Center;
  786. /* Find the value closest to 0 (f_Center) */
  787. bestDiff = zones[0].min_;
  788. for (i = 0; i < j; i++) {
  789. if (abs(zones[i].min_) < abs(bestDiff))
  790. bestDiff = zones[i].min_;
  791. if (abs(zones[i].max_) < abs(bestDiff))
  792. bestDiff = zones[i].max_;
  793. }
  794. if (bestDiff < 0)
  795. return f_Center - ((u32) (-bestDiff) * f_Step);
  796. return f_Center + (bestDiff * f_Step);
  797. }
  798. /****************************************************************************
  799. **
  800. ** Name: gcd
  801. **
  802. ** Description: Uses Euclid's algorithm
  803. **
  804. ** Parameters: u, v - unsigned values whose GCD is desired.
  805. **
  806. ** Global: None
  807. **
  808. ** Returns: greatest common divisor of u and v, if either value
  809. ** is 0, the other value is returned as the result.
  810. **
  811. ** Dependencies: None.
  812. **
  813. ** Revision History:
  814. **
  815. ** SCR Date Author Description
  816. ** -------------------------------------------------------------------------
  817. ** N/A 06-01-2004 JWS Original
  818. ** N/A 08-03-2004 DAD Changed to Euclid's since it can handle
  819. ** unsigned numbers.
  820. **
  821. ****************************************************************************/
  822. static u32 MT2063_gcd(u32 u, u32 v)
  823. {
  824. u32 r;
  825. while (v != 0) {
  826. r = u % v;
  827. u = v;
  828. v = r;
  829. }
  830. return u;
  831. }
  832. /****************************************************************************
  833. **
  834. ** Name: umax
  835. **
  836. ** Description: Implements a simple maximum function for unsigned numbers.
  837. ** Implemented as a function rather than a macro to avoid
  838. ** multiple evaluation of the calling parameters.
  839. **
  840. ** Parameters: a, b - Values to be compared
  841. **
  842. ** Global: None
  843. **
  844. ** Returns: larger of the input values.
  845. **
  846. ** Dependencies: None.
  847. **
  848. ** Revision History:
  849. **
  850. ** SCR Date Author Description
  851. ** -------------------------------------------------------------------------
  852. ** N/A 06-02-2004 JWS Original
  853. **
  854. ****************************************************************************/
  855. static u32 MT2063_umax(u32 a, u32 b)
  856. {
  857. return (a >= b) ? a : b;
  858. }
  859. /****************************************************************************
  860. **
  861. ** Name: IsSpurInBand
  862. **
  863. ** Description: Checks to see if a spur will be present within the IF's
  864. ** bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW)
  865. **
  866. ** ma mb mc md
  867. ** <--+-+-+-------------------+-------------------+-+-+-->
  868. ** | ^ 0 ^ |
  869. ** ^ b=-fIFOut+fIFBW/2 -b=+fIFOut-fIFBW/2 ^
  870. ** a=-fIFOut-fIFBW/2 -a=+fIFOut+fIFBW/2
  871. **
  872. ** Note that some equations are doubled to prevent round-off
  873. ** problems when calculating fIFBW/2
  874. **
  875. ** Parameters: pAS_Info - Avoid Spurs information block
  876. ** fm - If spur, amount f_IF1 has to move negative
  877. ** fp - If spur, amount f_IF1 has to move positive
  878. **
  879. ** Global: None
  880. **
  881. ** Returns: 1 if an LO spur would be present, otherwise 0.
  882. **
  883. ** Dependencies: None.
  884. **
  885. ** Revision History:
  886. **
  887. ** SCR Date Author Description
  888. ** -------------------------------------------------------------------------
  889. ** N/A 11-28-2002 DAD Implemented algorithm from applied patent
  890. **
  891. ****************************************************************************/
  892. static u32 IsSpurInBand(struct MT2063_AvoidSpursData_t *pAS_Info,
  893. u32 * fm, u32 * fp)
  894. {
  895. /*
  896. ** Calculate LO frequency settings.
  897. */
  898. u32 n, n0;
  899. const u32 f_LO1 = pAS_Info->f_LO1;
  900. const u32 f_LO2 = pAS_Info->f_LO2;
  901. const u32 d = pAS_Info->f_out + pAS_Info->f_out_bw / 2;
  902. const u32 c = d - pAS_Info->f_out_bw;
  903. const u32 f = pAS_Info->f_zif_bw / 2;
  904. const u32 f_Scale = (f_LO1 / (UINT_MAX / 2 / pAS_Info->maxH1)) + 1;
  905. s32 f_nsLO1, f_nsLO2;
  906. s32 f_Spur;
  907. u32 ma, mb, mc, md, me, mf;
  908. u32 lo_gcd, gd_Scale, gc_Scale, gf_Scale, hgds, hgfs, hgcs;
  909. *fm = 0;
  910. /*
  911. ** For each edge (d, c & f), calculate a scale, based on the gcd
  912. ** of f_LO1, f_LO2 and the edge value. Use the larger of this
  913. ** gcd-based scale factor or f_Scale.
  914. */
  915. lo_gcd = MT2063_gcd(f_LO1, f_LO2);
  916. gd_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, d), f_Scale);
  917. hgds = gd_Scale / 2;
  918. gc_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, c), f_Scale);
  919. hgcs = gc_Scale / 2;
  920. gf_Scale = MT2063_umax((u32) MT2063_gcd(lo_gcd, f), f_Scale);
  921. hgfs = gf_Scale / 2;
  922. n0 = DIV_ROUND_UP(f_LO2 - d, f_LO1 - f_LO2);
  923. /* Check out all multiples of LO1 from n0 to m_maxLOSpurHarmonic */
  924. for (n = n0; n <= pAS_Info->maxH1; ++n) {
  925. md = (n * ((f_LO1 + hgds) / gd_Scale) -
  926. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  927. /* If # fLO2 harmonics > m_maxLOSpurHarmonic, then no spurs present */
  928. if (md >= pAS_Info->maxH1)
  929. break;
  930. ma = (n * ((f_LO1 + hgds) / gd_Scale) +
  931. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  932. /* If no spurs between +/- (f_out + f_IFBW/2), then try next harmonic */
  933. if (md == ma)
  934. continue;
  935. mc = (n * ((f_LO1 + hgcs) / gc_Scale) -
  936. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  937. if (mc != md) {
  938. f_nsLO1 = (s32) (n * (f_LO1 / gc_Scale));
  939. f_nsLO2 = (s32) (mc * (f_LO2 / gc_Scale));
  940. f_Spur =
  941. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  942. n * (f_LO1 % gc_Scale) - mc * (f_LO2 % gc_Scale);
  943. *fp = ((f_Spur - (s32) c) / (mc - n)) + 1;
  944. *fm = (((s32) d - f_Spur) / (mc - n)) + 1;
  945. return 1;
  946. }
  947. /* Location of Zero-IF-spur to be checked */
  948. me = (n * ((f_LO1 + hgfs) / gf_Scale) +
  949. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  950. mf = (n * ((f_LO1 + hgfs) / gf_Scale) -
  951. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  952. if (me != mf) {
  953. f_nsLO1 = n * (f_LO1 / gf_Scale);
  954. f_nsLO2 = me * (f_LO2 / gf_Scale);
  955. f_Spur =
  956. (gf_Scale * (f_nsLO1 - f_nsLO2)) +
  957. n * (f_LO1 % gf_Scale) - me * (f_LO2 % gf_Scale);
  958. *fp = ((f_Spur + (s32) f) / (me - n)) + 1;
  959. *fm = (((s32) f - f_Spur) / (me - n)) + 1;
  960. return 1;
  961. }
  962. mb = (n * ((f_LO1 + hgcs) / gc_Scale) +
  963. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  964. if (ma != mb) {
  965. f_nsLO1 = n * (f_LO1 / gc_Scale);
  966. f_nsLO2 = ma * (f_LO2 / gc_Scale);
  967. f_Spur =
  968. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  969. n * (f_LO1 % gc_Scale) - ma * (f_LO2 % gc_Scale);
  970. *fp = (((s32) d + f_Spur) / (ma - n)) + 1;
  971. *fm = (-(f_Spur + (s32) c) / (ma - n)) + 1;
  972. return 1;
  973. }
  974. }
  975. /* No spurs found */
  976. return 0;
  977. }
  978. /*****************************************************************************
  979. **
  980. ** Name: MT_AvoidSpurs
  981. **
  982. ** Description: Main entry point to avoid spurs.
  983. ** Checks for existing spurs in present LO1, LO2 freqs
  984. ** and if present, chooses spur-free LO1, LO2 combination
  985. ** that tunes the same input/output frequencies.
  986. **
  987. ** Revision History:
  988. **
  989. ** SCR Date Author Description
  990. ** -------------------------------------------------------------------------
  991. ** 096 04-06-2005 DAD Ver 1.11: Fix divide by 0 error if maxH==0.
  992. **
  993. *****************************************************************************/
  994. static u32 MT2063_AvoidSpurs(void *h, struct MT2063_AvoidSpursData_t * pAS_Info)
  995. {
  996. u32 status = 0;
  997. u32 fm, fp; /* restricted range on LO's */
  998. pAS_Info->bSpurAvoided = 0;
  999. pAS_Info->nSpursFound = 0;
  1000. if (pAS_Info->maxH1 == 0)
  1001. return 0;
  1002. /*
  1003. ** Avoid LO Generated Spurs
  1004. **
  1005. ** Make sure that have no LO-related spurs within the IF output
  1006. ** bandwidth.
  1007. **
  1008. ** If there is an LO spur in this band, start at the current IF1 frequency
  1009. ** and work out until we find a spur-free frequency or run up against the
  1010. ** 1st IF SAW band edge. Use temporary copies of fLO1 and fLO2 so that they
  1011. ** will be unchanged if a spur-free setting is not found.
  1012. */
  1013. pAS_Info->bSpurPresent = IsSpurInBand(pAS_Info, &fm, &fp);
  1014. if (pAS_Info->bSpurPresent) {
  1015. u32 zfIF1 = pAS_Info->f_LO1 - pAS_Info->f_in; /* current attempt at a 1st IF */
  1016. u32 zfLO1 = pAS_Info->f_LO1; /* current attempt at an LO1 freq */
  1017. u32 zfLO2 = pAS_Info->f_LO2; /* current attempt at an LO2 freq */
  1018. u32 delta_IF1;
  1019. u32 new_IF1;
  1020. /*
  1021. ** Spur was found, attempt to find a spur-free 1st IF
  1022. */
  1023. do {
  1024. pAS_Info->nSpursFound++;
  1025. /* Raise f_IF1_upper, if needed */
  1026. MT2063_AddExclZone(pAS_Info, zfIF1 - fm, zfIF1 + fp);
  1027. /* Choose next IF1 that is closest to f_IF1_CENTER */
  1028. new_IF1 = MT2063_ChooseFirstIF(pAS_Info);
  1029. if (new_IF1 > zfIF1) {
  1030. pAS_Info->f_LO1 += (new_IF1 - zfIF1);
  1031. pAS_Info->f_LO2 += (new_IF1 - zfIF1);
  1032. } else {
  1033. pAS_Info->f_LO1 -= (zfIF1 - new_IF1);
  1034. pAS_Info->f_LO2 -= (zfIF1 - new_IF1);
  1035. }
  1036. zfIF1 = new_IF1;
  1037. if (zfIF1 > pAS_Info->f_if1_Center)
  1038. delta_IF1 = zfIF1 - pAS_Info->f_if1_Center;
  1039. else
  1040. delta_IF1 = pAS_Info->f_if1_Center - zfIF1;
  1041. }
  1042. /*
  1043. ** Continue while the new 1st IF is still within the 1st IF bandwidth
  1044. ** and there is a spur in the band (again)
  1045. */
  1046. while ((2 * delta_IF1 + pAS_Info->f_out_bw <=
  1047. pAS_Info->f_if1_bw)
  1048. && (pAS_Info->bSpurPresent =
  1049. IsSpurInBand(pAS_Info, &fm, &fp)));
  1050. /*
  1051. ** Use the LO-spur free values found. If the search went all the way to
  1052. ** the 1st IF band edge and always found spurs, just leave the original
  1053. ** choice. It's as "good" as any other.
  1054. */
  1055. if (pAS_Info->bSpurPresent == 1) {
  1056. status |= MT2063_SPUR_PRESENT_ERR;
  1057. pAS_Info->f_LO1 = zfLO1;
  1058. pAS_Info->f_LO2 = zfLO2;
  1059. } else
  1060. pAS_Info->bSpurAvoided = 1;
  1061. }
  1062. status |=
  1063. ((pAS_Info->
  1064. nSpursFound << MT2063_SPUR_SHIFT) & MT2063_SPUR_CNT_MASK);
  1065. return (status);
  1066. }
  1067. //end of mt2063_spuravoid.c
  1068. //=================================================================
  1069. //#################################################################
  1070. //=================================================================
  1071. /*
  1072. ** The expected version of MT_AvoidSpursData_t
  1073. ** If the version is different, an updated file is needed from Microtune
  1074. */
  1075. /* Expecting version 1.21 of the Spur Avoidance API */
  1076. typedef enum {
  1077. MT2063_SET_ATTEN,
  1078. MT2063_INCR_ATTEN,
  1079. MT2063_DECR_ATTEN
  1080. } MT2063_ATTEN_CNTL_MODE;
  1081. //#define TUNER_MT2063_OPTIMIZATION
  1082. /*
  1083. ** Constants used by the tuning algorithm
  1084. */
  1085. #define MT2063_REF_FREQ (16000000UL) /* Reference oscillator Frequency (in Hz) */
  1086. #define MT2063_IF1_BW (22000000UL) /* The IF1 filter bandwidth (in Hz) */
  1087. #define MT2063_TUNE_STEP_SIZE (50000UL) /* Tune in steps of 50 kHz */
  1088. #define MT2063_SPUR_STEP_HZ (250000UL) /* Step size (in Hz) to move IF1 when avoiding spurs */
  1089. #define MT2063_ZIF_BW (2000000UL) /* Zero-IF spur-free bandwidth (in Hz) */
  1090. #define MT2063_MAX_HARMONICS_1 (15UL) /* Highest intra-tuner LO Spur Harmonic to be avoided */
  1091. #define MT2063_MAX_HARMONICS_2 (5UL) /* Highest inter-tuner LO Spur Harmonic to be avoided */
  1092. #define MT2063_MIN_LO_SEP (1000000UL) /* Minimum inter-tuner LO frequency separation */
  1093. #define MT2063_LO1_FRACN_AVOID (0UL) /* LO1 FracN numerator avoid region (in Hz) */
  1094. #define MT2063_LO2_FRACN_AVOID (199999UL) /* LO2 FracN numerator avoid region (in Hz) */
  1095. #define MT2063_MIN_FIN_FREQ (44000000UL) /* Minimum input frequency (in Hz) */
  1096. #define MT2063_MAX_FIN_FREQ (1100000000UL) /* Maximum input frequency (in Hz) */
  1097. #define MT2063_MIN_FOUT_FREQ (36000000UL) /* Minimum output frequency (in Hz) */
  1098. #define MT2063_MAX_FOUT_FREQ (57000000UL) /* Maximum output frequency (in Hz) */
  1099. #define MT2063_MIN_DNC_FREQ (1293000000UL) /* Minimum LO2 frequency (in Hz) */
  1100. #define MT2063_MAX_DNC_FREQ (1614000000UL) /* Maximum LO2 frequency (in Hz) */
  1101. #define MT2063_MIN_UPC_FREQ (1396000000UL) /* Minimum LO1 frequency (in Hz) */
  1102. #define MT2063_MAX_UPC_FREQ (2750000000UL) /* Maximum LO1 frequency (in Hz) */
  1103. /*
  1104. ** Define the supported Part/Rev codes for the MT2063
  1105. */
  1106. #define MT2063_B0 (0x9B)
  1107. #define MT2063_B1 (0x9C)
  1108. #define MT2063_B2 (0x9D)
  1109. #define MT2063_B3 (0x9E)
  1110. /*
  1111. ** The number of Tuner Registers
  1112. */
  1113. static const u32 MT2063_Num_Registers = MT2063_REG_END_REGS;
  1114. #define USE_GLOBAL_TUNER 0
  1115. static u32 nMT2063MaxTuners = 1;
  1116. static u32 nMT2063OpenTuners = 0;
  1117. /*
  1118. ** Constants for setting receiver modes.
  1119. ** (6 modes defined at this time, enumerated by MT2063_RCVR_MODES)
  1120. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  1121. ** DNC Output is selected, the other is always off)
  1122. **
  1123. ** If PAL-L or L' is received, set:
  1124. ** MT2063_SetParam(hMT2063,MT2063_TAGC,1);
  1125. **
  1126. ** --------------+----------------------------------------------
  1127. ** Mode 0 : | MT2063_CABLE_QAM
  1128. ** Mode 1 : | MT2063_CABLE_ANALOG
  1129. ** Mode 2 : | MT2063_OFFAIR_COFDM
  1130. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  1131. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  1132. ** Mode 5 : | MT2063_OFFAIR_8VSB
  1133. ** --------------+----+----+----+----+-----+-----+--------------
  1134. ** Mode | 0 | 1 | 2 | 3 | 4 | 5 |
  1135. ** --------------+----+----+----+----+-----+-----+
  1136. **
  1137. **
  1138. */
  1139. static const u8 RFAGCEN[] = { 0, 0, 0, 0, 0, 0 };
  1140. static const u8 LNARIN[] = { 0, 0, 3, 3, 3, 3 };
  1141. static const u8 FIFFQEN[] = { 1, 1, 1, 1, 1, 1 };
  1142. static const u8 FIFFQ[] = { 0, 0, 0, 0, 0, 0 };
  1143. static const u8 DNC1GC[] = { 0, 0, 0, 0, 0, 0 };
  1144. static const u8 DNC2GC[] = { 0, 0, 0, 0, 0, 0 };
  1145. static const u8 ACLNAMAX[] = { 31, 31, 31, 31, 31, 31 };
  1146. static const u8 LNATGT[] = { 44, 43, 43, 43, 43, 43 };
  1147. static const u8 RFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  1148. static const u8 ACRFMAX[] = { 31, 31, 31, 31, 31, 31 };
  1149. static const u8 PD1TGT[] = { 36, 36, 38, 38, 36, 38 };
  1150. static const u8 FIFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  1151. static const u8 ACFIFMAX[] = { 29, 29, 29, 29, 29, 29 };
  1152. static const u8 PD2TGT[] = { 40, 33, 38, 42, 30, 38 };
  1153. /*
  1154. ** Local Function Prototypes - not available for external access.
  1155. */
  1156. /* Forward declaration(s): */
  1157. static u32 MT2063_CalcLO1Mult(u32 * Div, u32 * FracN, u32 f_LO,
  1158. u32 f_LO_Step, u32 f_Ref);
  1159. static u32 MT2063_CalcLO2Mult(u32 * Div, u32 * FracN, u32 f_LO,
  1160. u32 f_LO_Step, u32 f_Ref);
  1161. static u32 MT2063_fLO_FractionalTerm(u32 f_ref, u32 num,
  1162. u32 denom);
  1163. /****************************************************************************
  1164. **
  1165. ** Name: MT2063_GetLocked
  1166. **
  1167. ** Description: Checks to see if LO1 and LO2 are locked.
  1168. **
  1169. ** Parameters: h - Open handle to the tuner (from MT2063_Open).
  1170. **
  1171. ** Returns: status:
  1172. ** MT_OK - No errors
  1173. ** MT_UPC_UNLOCK - Upconverter PLL unlocked
  1174. ** MT_DNC_UNLOCK - Downconverter PLL unlocked
  1175. ** MT_COMM_ERR - Serial bus communications error
  1176. ** MT_INV_HANDLE - Invalid tuner handle
  1177. **
  1178. ** Dependencies: MT_ReadSub - Read byte(s) of data from the serial bus
  1179. ** MT_Sleep - Delay execution for x milliseconds
  1180. **
  1181. ** Revision History:
  1182. **
  1183. ** SCR Date Author Description
  1184. ** -------------------------------------------------------------------------
  1185. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1186. **
  1187. ****************************************************************************/
  1188. static u32 MT2063_GetLocked(struct mt2063_state *state)
  1189. {
  1190. const u32 nMaxWait = 100; /* wait a maximum of 100 msec */
  1191. const u32 nPollRate = 2; /* poll status bits every 2 ms */
  1192. const u32 nMaxLoops = nMaxWait / nPollRate;
  1193. const u8 LO1LK = 0x80;
  1194. u8 LO2LK = 0x08;
  1195. u32 status = 0; /* Status to be returned */
  1196. u32 nDelays = 0;
  1197. /* LO2 Lock bit was in a different place for B0 version */
  1198. if (state->tuner_id == MT2063_B0)
  1199. LO2LK = 0x40;
  1200. do {
  1201. status |=
  1202. mt2063_read(state,
  1203. MT2063_REG_LO_STATUS,
  1204. &state->reg[MT2063_REG_LO_STATUS], 1);
  1205. if (status < 0)
  1206. return (status);
  1207. if ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) ==
  1208. (LO1LK | LO2LK)) {
  1209. return (status);
  1210. }
  1211. msleep(nPollRate); /* Wait between retries */
  1212. }
  1213. while (++nDelays < nMaxLoops);
  1214. if ((state->reg[MT2063_REG_LO_STATUS] & LO1LK) == 0x00)
  1215. status |= MT2063_UPC_UNLOCK;
  1216. if ((state->reg[MT2063_REG_LO_STATUS] & LO2LK) == 0x00)
  1217. status |= MT2063_DNC_UNLOCK;
  1218. return (status);
  1219. }
  1220. /****************************************************************************
  1221. **
  1222. ** Name: MT2063_GetParam
  1223. **
  1224. ** Description: Gets a tuning algorithm parameter.
  1225. **
  1226. ** This function provides access to the internals of the
  1227. ** tuning algorithm - mostly for testing purposes.
  1228. **
  1229. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1230. ** param - Tuning algorithm parameter
  1231. ** (see enum MT2063_Param)
  1232. ** pValue - ptr to returned value
  1233. **
  1234. ** param Description
  1235. ** ---------------------- --------------------------------
  1236. ** MT2063_IC_ADDR Serial Bus address of this tuner
  1237. ** MT2063_MAX_OPEN Max # of MT2063's allowed open
  1238. ** MT2063_NUM_OPEN # of MT2063's open
  1239. ** MT2063_SRO_FREQ crystal frequency
  1240. ** MT2063_STEPSIZE minimum tuning step size
  1241. ** MT2063_INPUT_FREQ input center frequency
  1242. ** MT2063_LO1_FREQ LO1 Frequency
  1243. ** MT2063_LO1_STEPSIZE LO1 minimum step size
  1244. ** MT2063_LO1_FRACN_AVOID LO1 FracN keep-out region
  1245. ** MT2063_IF1_ACTUAL Current 1st IF in use
  1246. ** MT2063_IF1_REQUEST Requested 1st IF
  1247. ** MT2063_IF1_CENTER Center of 1st IF SAW filter
  1248. ** MT2063_IF1_BW Bandwidth of 1st IF SAW filter
  1249. ** MT2063_ZIF_BW zero-IF bandwidth
  1250. ** MT2063_LO2_FREQ LO2 Frequency
  1251. ** MT2063_LO2_STEPSIZE LO2 minimum step size
  1252. ** MT2063_LO2_FRACN_AVOID LO2 FracN keep-out region
  1253. ** MT2063_OUTPUT_FREQ output center frequency
  1254. ** MT2063_OUTPUT_BW output bandwidth
  1255. ** MT2063_LO_SEPARATION min inter-tuner LO separation
  1256. ** MT2063_AS_ALG ID of avoid-spurs algorithm in use
  1257. ** MT2063_MAX_HARM1 max # of intra-tuner harmonics
  1258. ** MT2063_MAX_HARM2 max # of inter-tuner harmonics
  1259. ** MT2063_EXCL_ZONES # of 1st IF exclusion zones
  1260. ** MT2063_NUM_SPURS # of spurs found/avoided
  1261. ** MT2063_SPUR_AVOIDED >0 spurs avoided
  1262. ** MT2063_SPUR_PRESENT >0 spurs in output (mathematically)
  1263. ** MT2063_RCVR_MODE Predefined modes.
  1264. ** MT2063_ACLNA LNA attenuator gain code
  1265. ** MT2063_ACRF RF attenuator gain code
  1266. ** MT2063_ACFIF FIF attenuator gain code
  1267. ** MT2063_ACLNA_MAX LNA attenuator limit
  1268. ** MT2063_ACRF_MAX RF attenuator limit
  1269. ** MT2063_ACFIF_MAX FIF attenuator limit
  1270. ** MT2063_PD1 Actual value of PD1
  1271. ** MT2063_PD2 Actual value of PD2
  1272. ** MT2063_DNC_OUTPUT_ENABLE DNC output selection
  1273. ** MT2063_VGAGC VGA gain code
  1274. ** MT2063_VGAOI VGA output current
  1275. ** MT2063_TAGC TAGC setting
  1276. ** MT2063_AMPGC AMP gain code
  1277. ** MT2063_AVOID_DECT Avoid DECT Frequencies
  1278. ** MT2063_CTFILT_SW Cleartune filter selection
  1279. **
  1280. ** Usage: status |= MT2063_GetParam(hMT2063,
  1281. ** MT2063_IF1_ACTUAL,
  1282. ** &f_IF1_Actual);
  1283. **
  1284. ** Returns: status:
  1285. ** MT_OK - No errors
  1286. ** MT_INV_HANDLE - Invalid tuner handle
  1287. ** MT_ARG_NULL - Null pointer argument passed
  1288. ** MT_ARG_RANGE - Invalid parameter requested
  1289. **
  1290. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1291. **
  1292. ** See Also: MT2063_SetParam, MT2063_Open
  1293. **
  1294. ** Revision History:
  1295. **
  1296. ** SCR Date Author Description
  1297. ** -------------------------------------------------------------------------
  1298. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1299. ** 154 09-13-2007 RSK Ver 1.05: Get/SetParam changes for LOx_FREQ
  1300. ** 10-31-2007 PINZ Ver 1.08: Get/SetParam add VGAGC, VGAOI, AMPGC, TAGC
  1301. ** 173 M 01-23-2008 RSK Ver 1.12: Read LO1C and LO2C registers from HW
  1302. ** in GetParam.
  1303. ** 04-18-2008 PINZ Ver 1.15: Add SetParam LNARIN & PDxTGT
  1304. ** Split SetParam up to ACLNA / ACLNA_MAX
  1305. ** removed ACLNA_INRC/DECR (+RF & FIF)
  1306. ** removed GCUAUTO / BYPATNDN/UP
  1307. ** 175 I 16-06-2008 PINZ Ver 1.16: Add control to avoid US DECT freqs.
  1308. ** 175 I 06-19-2008 RSK Ver 1.17: Refactor DECT control to SpurAvoid.
  1309. ** 06-24-2008 PINZ Ver 1.18: Add Get/SetParam CTFILT_SW
  1310. **
  1311. ****************************************************************************/
  1312. static u32 MT2063_GetParam(struct mt2063_state *state, enum MT2063_Param param, u32 *pValue)
  1313. {
  1314. u32 status = 0; /* Status to be returned */
  1315. u32 Div;
  1316. u32 Num;
  1317. if (pValue == NULL)
  1318. return -EINVAL;
  1319. switch (param) {
  1320. /* Serial Bus address of this tuner */
  1321. case MT2063_IC_ADDR:
  1322. *pValue = state->config->tuner_address;
  1323. break;
  1324. /* Max # of MT2063's allowed to be open */
  1325. case MT2063_MAX_OPEN:
  1326. *pValue = nMT2063MaxTuners;
  1327. break;
  1328. /* # of MT2063's open */
  1329. case MT2063_NUM_OPEN:
  1330. *pValue = nMT2063OpenTuners;
  1331. break;
  1332. /* crystal frequency */
  1333. case MT2063_SRO_FREQ:
  1334. *pValue = state->AS_Data.f_ref;
  1335. break;
  1336. /* minimum tuning step size */
  1337. case MT2063_STEPSIZE:
  1338. *pValue = state->AS_Data.f_LO2_Step;
  1339. break;
  1340. /* input center frequency */
  1341. case MT2063_INPUT_FREQ:
  1342. *pValue = state->AS_Data.f_in;
  1343. break;
  1344. /* LO1 Frequency */
  1345. case MT2063_LO1_FREQ:
  1346. {
  1347. /* read the actual tuner register values for LO1C_1 and LO1C_2 */
  1348. status |=
  1349. mt2063_read(state,
  1350. MT2063_REG_LO1C_1,
  1351. &state->
  1352. reg[MT2063_REG_LO1C_1], 2);
  1353. Div = state->reg[MT2063_REG_LO1C_1];
  1354. Num = state->reg[MT2063_REG_LO1C_2] & 0x3F;
  1355. state->AS_Data.f_LO1 =
  1356. (state->AS_Data.f_ref * Div) +
  1357. MT2063_fLO_FractionalTerm(state->AS_Data.
  1358. f_ref, Num, 64);
  1359. }
  1360. *pValue = state->AS_Data.f_LO1;
  1361. break;
  1362. /* LO1 minimum step size */
  1363. case MT2063_LO1_STEPSIZE:
  1364. *pValue = state->AS_Data.f_LO1_Step;
  1365. break;
  1366. /* LO1 FracN keep-out region */
  1367. case MT2063_LO1_FRACN_AVOID_PARAM:
  1368. *pValue = state->AS_Data.f_LO1_FracN_Avoid;
  1369. break;
  1370. /* Current 1st IF in use */
  1371. case MT2063_IF1_ACTUAL:
  1372. *pValue = state->f_IF1_actual;
  1373. break;
  1374. /* Requested 1st IF */
  1375. case MT2063_IF1_REQUEST:
  1376. *pValue = state->AS_Data.f_if1_Request;
  1377. break;
  1378. /* Center of 1st IF SAW filter */
  1379. case MT2063_IF1_CENTER:
  1380. *pValue = state->AS_Data.f_if1_Center;
  1381. break;
  1382. /* Bandwidth of 1st IF SAW filter */
  1383. case MT2063_IF1_BW:
  1384. *pValue = state->AS_Data.f_if1_bw;
  1385. break;
  1386. /* zero-IF bandwidth */
  1387. case MT2063_ZIF_BW:
  1388. *pValue = state->AS_Data.f_zif_bw;
  1389. break;
  1390. /* LO2 Frequency */
  1391. case MT2063_LO2_FREQ:
  1392. {
  1393. /* Read the actual tuner register values for LO2C_1, LO2C_2 and LO2C_3 */
  1394. status |=
  1395. mt2063_read(state,
  1396. MT2063_REG_LO2C_1,
  1397. &state->
  1398. reg[MT2063_REG_LO2C_1], 3);
  1399. Div =
  1400. (state->reg[MT2063_REG_LO2C_1] & 0xFE) >> 1;
  1401. Num =
  1402. ((state->
  1403. reg[MT2063_REG_LO2C_1] & 0x01) << 12) |
  1404. (state->
  1405. reg[MT2063_REG_LO2C_2] << 4) | (state->
  1406. reg
  1407. [MT2063_REG_LO2C_3]
  1408. & 0x00F);
  1409. state->AS_Data.f_LO2 =
  1410. (state->AS_Data.f_ref * Div) +
  1411. MT2063_fLO_FractionalTerm(state->AS_Data.
  1412. f_ref, Num, 8191);
  1413. }
  1414. *pValue = state->AS_Data.f_LO2;
  1415. break;
  1416. /* LO2 minimum step size */
  1417. case MT2063_LO2_STEPSIZE:
  1418. *pValue = state->AS_Data.f_LO2_Step;
  1419. break;
  1420. /* LO2 FracN keep-out region */
  1421. case MT2063_LO2_FRACN_AVOID:
  1422. *pValue = state->AS_Data.f_LO2_FracN_Avoid;
  1423. break;
  1424. /* output center frequency */
  1425. case MT2063_OUTPUT_FREQ:
  1426. *pValue = state->AS_Data.f_out;
  1427. break;
  1428. /* output bandwidth */
  1429. case MT2063_OUTPUT_BW:
  1430. *pValue = state->AS_Data.f_out_bw - 750000;
  1431. break;
  1432. /* min inter-tuner LO separation */
  1433. case MT2063_LO_SEPARATION:
  1434. *pValue = state->AS_Data.f_min_LO_Separation;
  1435. break;
  1436. /* max # of intra-tuner harmonics */
  1437. case MT2063_MAX_HARM1:
  1438. *pValue = state->AS_Data.maxH1;
  1439. break;
  1440. /* max # of inter-tuner harmonics */
  1441. case MT2063_MAX_HARM2:
  1442. *pValue = state->AS_Data.maxH2;
  1443. break;
  1444. /* # of 1st IF exclusion zones */
  1445. case MT2063_EXCL_ZONES:
  1446. *pValue = state->AS_Data.nZones;
  1447. break;
  1448. /* # of spurs found/avoided */
  1449. case MT2063_NUM_SPURS:
  1450. *pValue = state->AS_Data.nSpursFound;
  1451. break;
  1452. /* >0 spurs avoided */
  1453. case MT2063_SPUR_AVOIDED:
  1454. *pValue = state->AS_Data.bSpurAvoided;
  1455. break;
  1456. /* >0 spurs in output (mathematically) */
  1457. case MT2063_SPUR_PRESENT:
  1458. *pValue = state->AS_Data.bSpurPresent;
  1459. break;
  1460. /* Predefined receiver setup combination */
  1461. case MT2063_RCVR_MODE:
  1462. *pValue = state->rcvr_mode;
  1463. break;
  1464. case MT2063_PD1:
  1465. case MT2063_PD2: {
  1466. u8 mask = (param == MT2063_PD1 ? 0x01 : 0x03); /* PD1 vs PD2 */
  1467. u8 orig = (state->reg[MT2063_REG_BYP_CTRL]);
  1468. u8 reg = (orig & 0xF1) | mask; /* Only set 3 bits (not 5) */
  1469. int i;
  1470. *pValue = 0;
  1471. /* Initiate ADC output to reg 0x0A */
  1472. if (reg != orig)
  1473. status |=
  1474. mt2063_write(state,
  1475. MT2063_REG_BYP_CTRL,
  1476. &reg, 1);
  1477. if (status < 0)
  1478. return (status);
  1479. for (i = 0; i < 8; i++) {
  1480. status |=
  1481. mt2063_read(state,
  1482. MT2063_REG_ADC_OUT,
  1483. &state->
  1484. reg
  1485. [MT2063_REG_ADC_OUT],
  1486. 1);
  1487. if (status >= 0)
  1488. *pValue +=
  1489. state->
  1490. reg[MT2063_REG_ADC_OUT];
  1491. else {
  1492. if (i)
  1493. *pValue /= i;
  1494. return (status);
  1495. }
  1496. }
  1497. *pValue /= 8; /* divide by number of reads */
  1498. *pValue >>= 2; /* only want 6 MSB's out of 8 */
  1499. /* Restore value of Register BYP_CTRL */
  1500. if (reg != orig)
  1501. status |=
  1502. mt2063_write(state,
  1503. MT2063_REG_BYP_CTRL,
  1504. &orig, 1);
  1505. }
  1506. break;
  1507. /* Get LNA attenuator code */
  1508. case MT2063_ACLNA:
  1509. {
  1510. u8 val;
  1511. status |=
  1512. MT2063_GetReg(state, MT2063_REG_XO_STATUS,
  1513. &val);
  1514. *pValue = val & 0x1f;
  1515. }
  1516. break;
  1517. /* Get RF attenuator code */
  1518. case MT2063_ACRF:
  1519. {
  1520. u8 val;
  1521. status |=
  1522. MT2063_GetReg(state, MT2063_REG_RF_STATUS,
  1523. &val);
  1524. *pValue = val & 0x1f;
  1525. }
  1526. break;
  1527. /* Get FIF attenuator code */
  1528. case MT2063_ACFIF:
  1529. {
  1530. u8 val;
  1531. status |=
  1532. MT2063_GetReg(state, MT2063_REG_FIF_STATUS,
  1533. &val);
  1534. *pValue = val & 0x1f;
  1535. }
  1536. break;
  1537. /* Get LNA attenuator limit */
  1538. case MT2063_ACLNA_MAX:
  1539. {
  1540. u8 val;
  1541. status |=
  1542. MT2063_GetReg(state, MT2063_REG_LNA_OV,
  1543. &val);
  1544. *pValue = val & 0x1f;
  1545. }
  1546. break;
  1547. /* Get RF attenuator limit */
  1548. case MT2063_ACRF_MAX:
  1549. {
  1550. u8 val;
  1551. status |=
  1552. MT2063_GetReg(state, MT2063_REG_RF_OV,
  1553. &val);
  1554. *pValue = val & 0x1f;
  1555. }
  1556. break;
  1557. /* Get FIF attenuator limit */
  1558. case MT2063_ACFIF_MAX:
  1559. {
  1560. u8 val;
  1561. status |=
  1562. MT2063_GetReg(state, MT2063_REG_FIF_OV,
  1563. &val);
  1564. *pValue = val & 0x1f;
  1565. }
  1566. break;
  1567. /* Get current used DNC output */
  1568. case MT2063_DNC_OUTPUT_ENABLE:
  1569. {
  1570. if ((state->reg[MT2063_REG_DNC_GAIN] & 0x03) == 0x03) { /* if DNC1 is off */
  1571. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  1572. *pValue =
  1573. (u32) MT2063_DNC_NONE;
  1574. else
  1575. *pValue =
  1576. (u32) MT2063_DNC_2;
  1577. } else { /* DNC1 is on */
  1578. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  1579. *pValue =
  1580. (u32) MT2063_DNC_1;
  1581. else
  1582. *pValue =
  1583. (u32) MT2063_DNC_BOTH;
  1584. }
  1585. }
  1586. break;
  1587. /* Get VGA Gain Code */
  1588. case MT2063_VGAGC:
  1589. *pValue = ((state->reg[MT2063_REG_VGA_GAIN] & 0x0C) >> 2);
  1590. break;
  1591. /* Get VGA bias current */
  1592. case MT2063_VGAOI:
  1593. *pValue = (state->reg[MT2063_REG_RSVD_31] & 0x07);
  1594. break;
  1595. /* Get TAGC setting */
  1596. case MT2063_TAGC:
  1597. *pValue = (state->reg[MT2063_REG_RSVD_1E] & 0x03);
  1598. break;
  1599. /* Get AMP Gain Code */
  1600. case MT2063_AMPGC:
  1601. *pValue = (state->reg[MT2063_REG_TEMP_SEL] & 0x03);
  1602. break;
  1603. /* Avoid DECT Frequencies */
  1604. case MT2063_AVOID_DECT:
  1605. *pValue = state->AS_Data.avoidDECT;
  1606. break;
  1607. /* Cleartune filter selection: 0 - by IC (default), 1 - by software */
  1608. case MT2063_CTFILT_SW:
  1609. *pValue = state->ctfilt_sw;
  1610. break;
  1611. case MT2063_EOP:
  1612. default:
  1613. status |= -ERANGE;
  1614. }
  1615. return (status);
  1616. }
  1617. /****************************************************************************
  1618. **
  1619. ** Name: MT2063_GetReg
  1620. **
  1621. ** Description: Gets an MT2063 register.
  1622. **
  1623. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1624. ** reg - MT2063 register/subaddress location
  1625. ** *val - MT2063 register/subaddress value
  1626. **
  1627. ** Returns: status:
  1628. ** MT_OK - No errors
  1629. ** MT_COMM_ERR - Serial bus communications error
  1630. ** MT_INV_HANDLE - Invalid tuner handle
  1631. ** MT_ARG_NULL - Null pointer argument passed
  1632. ** MT_ARG_RANGE - Argument out of range
  1633. **
  1634. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1635. **
  1636. ** Use this function if you need to read a register from
  1637. ** the MT2063.
  1638. **
  1639. ** Revision History:
  1640. **
  1641. ** SCR Date Author Description
  1642. ** -------------------------------------------------------------------------
  1643. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1644. **
  1645. ****************************************************************************/
  1646. static u32 MT2063_GetReg(struct mt2063_state *state, u8 reg, u8 * val)
  1647. {
  1648. u32 status = 0; /* Status to be returned */
  1649. if (val == NULL)
  1650. return -EINVAL;
  1651. if (reg >= MT2063_REG_END_REGS)
  1652. return -ERANGE;
  1653. status = mt2063_read(state, reg, &state->reg[reg], 1);
  1654. return (status);
  1655. }
  1656. /******************************************************************************
  1657. **
  1658. ** Name: MT2063_SetReceiverMode
  1659. **
  1660. ** Description: Set the MT2063 receiver mode
  1661. **
  1662. ** --------------+----------------------------------------------
  1663. ** Mode 0 : | MT2063_CABLE_QAM
  1664. ** Mode 1 : | MT2063_CABLE_ANALOG
  1665. ** Mode 2 : | MT2063_OFFAIR_COFDM
  1666. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  1667. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  1668. ** Mode 5 : | MT2063_OFFAIR_8VSB
  1669. ** --------------+----+----+----+----+-----+--------------------
  1670. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  1671. ** DNC Output is selected, the other is always off)
  1672. **
  1673. ** |<---------- Mode -------------->|
  1674. ** Reg Field | 0 | 1 | 2 | 3 | 4 | 5 |
  1675. ** ------------+-----+-----+-----+-----+-----+-----+
  1676. ** RFAGCen | OFF | OFF | OFF | OFF | OFF | OFF
  1677. ** LNARin | 0 | 0 | 3 | 3 | 3 | 3
  1678. ** FIFFQen | 1 | 1 | 1 | 1 | 1 | 1
  1679. ** FIFFq | 0 | 0 | 0 | 0 | 0 | 0
  1680. ** DNC1gc | 0 | 0 | 0 | 0 | 0 | 0
  1681. ** DNC2gc | 0 | 0 | 0 | 0 | 0 | 0
  1682. ** GCU Auto | 1 | 1 | 1 | 1 | 1 | 1
  1683. ** LNA max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1684. ** LNA Target | 44 | 43 | 43 | 43 | 43 | 43
  1685. ** ign RF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1686. ** RF max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1687. ** PD1 Target | 36 | 36 | 38 | 38 | 36 | 38
  1688. ** ign FIF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1689. ** FIF max Atn | 5 | 5 | 5 | 5 | 5 | 5
  1690. ** PD2 Target | 40 | 33 | 42 | 42 | 33 | 42
  1691. **
  1692. **
  1693. ** Parameters: state - ptr to mt2063_state structure
  1694. ** Mode - desired reciever mode
  1695. **
  1696. ** Usage: status = MT2063_SetReceiverMode(hMT2063, Mode);
  1697. **
  1698. ** Returns: status:
  1699. ** MT_OK - No errors
  1700. ** MT_COMM_ERR - Serial bus communications error
  1701. **
  1702. ** Dependencies: MT2063_SetReg - Write a byte of data to a HW register.
  1703. ** Assumes that the tuner cache is valid.
  1704. **
  1705. ** Revision History:
  1706. **
  1707. ** SCR Date Author Description
  1708. ** -------------------------------------------------------------------------
  1709. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1710. ** N/A 01-10-2007 PINZ Added additional GCU Settings, FIFF Calib will be triggered
  1711. ** 155 10-01-2007 DAD Ver 1.06: Add receiver mode for SECAM positive
  1712. ** modulation
  1713. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1714. ** N/A 10-22-2007 PINZ Ver 1.07: Changed some Registers at init to have
  1715. ** the same settings as with MT Launcher
  1716. ** N/A 10-30-2007 PINZ Add SetParam VGAGC & VGAOI
  1717. ** Add SetParam DNC_OUTPUT_ENABLE
  1718. ** Removed VGAGC from receiver mode,
  1719. ** default now 1
  1720. ** N/A 10-31-2007 PINZ Ver 1.08: Add SetParam TAGC, removed from rcvr-mode
  1721. ** Add SetParam AMPGC, removed from rcvr-mode
  1722. ** Corrected names of GCU values
  1723. ** reorganized receiver modes, removed,
  1724. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1725. ** Actualized Receiver-Mode values
  1726. ** N/A 11-12-2007 PINZ Ver 1.09: Actualized Receiver-Mode values
  1727. ** N/A 11-27-2007 PINZ Improved buffered writing
  1728. ** 01-03-2008 PINZ Ver 1.10: Added a trigger of BYPATNUP for
  1729. ** correct wakeup of the LNA after shutdown
  1730. ** Set AFCsd = 1 as default
  1731. ** Changed CAP1sel default
  1732. ** 01-14-2008 PINZ Ver 1.11: Updated gain settings
  1733. ** 04-18-2008 PINZ Ver 1.15: Add SetParam LNARIN & PDxTGT
  1734. ** Split SetParam up to ACLNA / ACLNA_MAX
  1735. ** removed ACLNA_INRC/DECR (+RF & FIF)
  1736. ** removed GCUAUTO / BYPATNDN/UP
  1737. **
  1738. ******************************************************************************/
  1739. static u32 MT2063_SetReceiverMode(struct mt2063_state *state,
  1740. enum MT2063_RCVR_MODES Mode)
  1741. {
  1742. u32 status = 0; /* Status to be returned */
  1743. u8 val;
  1744. u32 longval;
  1745. if (Mode >= MT2063_NUM_RCVR_MODES)
  1746. status = -ERANGE;
  1747. /* RFAGCen */
  1748. if (status >= 0) {
  1749. val =
  1750. (state->
  1751. reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x40) | (RFAGCEN[Mode]
  1752. ? 0x40 :
  1753. 0x00);
  1754. if (state->reg[MT2063_REG_PD1_TGT] != val) {
  1755. status |= MT2063_SetReg(state, MT2063_REG_PD1_TGT, val);
  1756. }
  1757. }
  1758. /* LNARin */
  1759. if (status >= 0) {
  1760. status |= MT2063_SetParam(state, MT2063_LNA_RIN, LNARIN[Mode]);
  1761. }
  1762. /* FIFFQEN and FIFFQ */
  1763. if (status >= 0) {
  1764. val =
  1765. (state->
  1766. reg[MT2063_REG_FIFF_CTRL2] & (u8) ~ 0xF0) |
  1767. (FIFFQEN[Mode] << 7) | (FIFFQ[Mode] << 4);
  1768. if (state->reg[MT2063_REG_FIFF_CTRL2] != val) {
  1769. status |=
  1770. MT2063_SetReg(state, MT2063_REG_FIFF_CTRL2, val);
  1771. /* trigger FIFF calibration, needed after changing FIFFQ */
  1772. val =
  1773. (state->reg[MT2063_REG_FIFF_CTRL] | (u8) 0x01);
  1774. status |=
  1775. MT2063_SetReg(state, MT2063_REG_FIFF_CTRL, val);
  1776. val =
  1777. (state->
  1778. reg[MT2063_REG_FIFF_CTRL] & (u8) ~ 0x01);
  1779. status |=
  1780. MT2063_SetReg(state, MT2063_REG_FIFF_CTRL, val);
  1781. }
  1782. }
  1783. /* DNC1GC & DNC2GC */
  1784. status |= MT2063_GetParam(state, MT2063_DNC_OUTPUT_ENABLE, &longval);
  1785. status |= MT2063_SetParam(state, MT2063_DNC_OUTPUT_ENABLE, longval);
  1786. /* acLNAmax */
  1787. if (status >= 0) {
  1788. status |=
  1789. MT2063_SetParam(state, MT2063_ACLNA_MAX, ACLNAMAX[Mode]);
  1790. }
  1791. /* LNATGT */
  1792. if (status >= 0) {
  1793. status |= MT2063_SetParam(state, MT2063_LNA_TGT, LNATGT[Mode]);
  1794. }
  1795. /* ACRF */
  1796. if (status >= 0) {
  1797. status |=
  1798. MT2063_SetParam(state, MT2063_ACRF_MAX, ACRFMAX[Mode]);
  1799. }
  1800. /* PD1TGT */
  1801. if (status >= 0) {
  1802. status |= MT2063_SetParam(state, MT2063_PD1_TGT, PD1TGT[Mode]);
  1803. }
  1804. /* FIFATN */
  1805. if (status >= 0) {
  1806. status |=
  1807. MT2063_SetParam(state, MT2063_ACFIF_MAX, ACFIFMAX[Mode]);
  1808. }
  1809. /* PD2TGT */
  1810. if (status >= 0) {
  1811. status |= MT2063_SetParam(state, MT2063_PD2_TGT, PD2TGT[Mode]);
  1812. }
  1813. /* Ignore ATN Overload */
  1814. if (status >= 0) {
  1815. val =
  1816. (state->
  1817. reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x80) | (RFOVDIS[Mode]
  1818. ? 0x80 :
  1819. 0x00);
  1820. if (state->reg[MT2063_REG_LNA_TGT] != val) {
  1821. status |= MT2063_SetReg(state, MT2063_REG_LNA_TGT, val);
  1822. }
  1823. }
  1824. /* Ignore FIF Overload */
  1825. if (status >= 0) {
  1826. val =
  1827. (state->
  1828. reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x80) |
  1829. (FIFOVDIS[Mode] ? 0x80 : 0x00);
  1830. if (state->reg[MT2063_REG_PD1_TGT] != val) {
  1831. status |= MT2063_SetReg(state, MT2063_REG_PD1_TGT, val);
  1832. }
  1833. }
  1834. if (status >= 0)
  1835. state->rcvr_mode = Mode;
  1836. return (status);
  1837. }
  1838. /****************************************************************************
  1839. **
  1840. ** Name: MT2063_SetParam
  1841. **
  1842. ** Description: Sets a tuning algorithm parameter.
  1843. **
  1844. ** This function provides access to the internals of the
  1845. ** tuning algorithm. You can override many of the tuning
  1846. ** algorithm defaults using this function.
  1847. **
  1848. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1849. ** param - Tuning algorithm parameter
  1850. ** (see enum MT2063_Param)
  1851. ** nValue - value to be set
  1852. **
  1853. ** param Description
  1854. ** ---------------------- --------------------------------
  1855. ** MT2063_SRO_FREQ crystal frequency
  1856. ** MT2063_STEPSIZE minimum tuning step size
  1857. ** MT2063_LO1_FREQ LO1 frequency
  1858. ** MT2063_LO1_STEPSIZE LO1 minimum step size
  1859. ** MT2063_LO1_FRACN_AVOID LO1 FracN keep-out region
  1860. ** MT2063_IF1_REQUEST Requested 1st IF
  1861. ** MT2063_ZIF_BW zero-IF bandwidth
  1862. ** MT2063_LO2_FREQ LO2 frequency
  1863. ** MT2063_LO2_STEPSIZE LO2 minimum step size
  1864. ** MT2063_LO2_FRACN_AVOID LO2 FracN keep-out region
  1865. ** MT2063_OUTPUT_FREQ output center frequency
  1866. ** MT2063_OUTPUT_BW output bandwidth
  1867. ** MT2063_LO_SEPARATION min inter-tuner LO separation
  1868. ** MT2063_MAX_HARM1 max # of intra-tuner harmonics
  1869. ** MT2063_MAX_HARM2 max # of inter-tuner harmonics
  1870. ** MT2063_RCVR_MODE Predefined modes
  1871. ** MT2063_LNA_RIN Set LNA Rin (*)
  1872. ** MT2063_LNA_TGT Set target power level at LNA (*)
  1873. ** MT2063_PD1_TGT Set target power level at PD1 (*)
  1874. ** MT2063_PD2_TGT Set target power level at PD2 (*)
  1875. ** MT2063_ACLNA_MAX LNA attenuator limit (*)
  1876. ** MT2063_ACRF_MAX RF attenuator limit (*)
  1877. ** MT2063_ACFIF_MAX FIF attenuator limit (*)
  1878. ** MT2063_DNC_OUTPUT_ENABLE DNC output selection
  1879. ** MT2063_VGAGC VGA gain code
  1880. ** MT2063_VGAOI VGA output current
  1881. ** MT2063_TAGC TAGC setting
  1882. ** MT2063_AMPGC AMP gain code
  1883. ** MT2063_AVOID_DECT Avoid DECT Frequencies
  1884. ** MT2063_CTFILT_SW Cleartune filter selection
  1885. **
  1886. ** (*) This parameter is set by MT2063_RCVR_MODE, do not call
  1887. ** additionally.
  1888. **
  1889. ** Usage: status |= MT2063_SetParam(hMT2063,
  1890. ** MT2063_STEPSIZE,
  1891. ** 50000);
  1892. **
  1893. ** Returns: status:
  1894. ** MT_OK - No errors
  1895. ** MT_INV_HANDLE - Invalid tuner handle
  1896. ** MT_ARG_NULL - Null pointer argument passed
  1897. ** MT_ARG_RANGE - Invalid parameter requested
  1898. ** or set value out of range
  1899. ** or non-writable parameter
  1900. **
  1901. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1902. **
  1903. ** See Also: MT2063_GetParam, MT2063_Open
  1904. **
  1905. ** Revision History:
  1906. **
  1907. ** SCR Date Author Description
  1908. ** -------------------------------------------------------------------------
  1909. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1910. ** 154 09-13-2007 RSK Ver 1.05: Get/SetParam changes for LOx_FREQ
  1911. ** 10-31-2007 PINZ Ver 1.08: Get/SetParam add VGAGC, VGAOI, AMPGC, TAGC
  1912. ** 04-18-2008 PINZ Ver 1.15: Add SetParam LNARIN & PDxTGT
  1913. ** Split SetParam up to ACLNA / ACLNA_MAX
  1914. ** removed ACLNA_INRC/DECR (+RF & FIF)
  1915. ** removed GCUAUTO / BYPATNDN/UP
  1916. ** 175 I 06-06-2008 PINZ Ver 1.16: Add control to avoid US DECT freqs.
  1917. ** 175 I 06-19-2008 RSK Ver 1.17: Refactor DECT control to SpurAvoid.
  1918. ** 06-24-2008 PINZ Ver 1.18: Add Get/SetParam CTFILT_SW
  1919. **
  1920. ****************************************************************************/
  1921. static u32 MT2063_SetParam(struct mt2063_state *state,
  1922. enum MT2063_Param param,
  1923. enum MT2063_DNC_Output_Enable nValue)
  1924. {
  1925. u32 status = 0; /* Status to be returned */
  1926. u8 val = 0;
  1927. switch (param) {
  1928. /* crystal frequency */
  1929. case MT2063_SRO_FREQ:
  1930. state->AS_Data.f_ref = nValue;
  1931. state->AS_Data.f_LO1_FracN_Avoid = 0;
  1932. state->AS_Data.f_LO2_FracN_Avoid = nValue / 80 - 1;
  1933. state->AS_Data.f_LO1_Step = nValue / 64;
  1934. state->AS_Data.f_if1_Center =
  1935. (state->AS_Data.f_ref / 8) *
  1936. (state->reg[MT2063_REG_FIFFC] + 640);
  1937. break;
  1938. /* minimum tuning step size */
  1939. case MT2063_STEPSIZE:
  1940. state->AS_Data.f_LO2_Step = nValue;
  1941. break;
  1942. /* LO1 frequency */
  1943. case MT2063_LO1_FREQ:
  1944. {
  1945. /* Note: LO1 and LO2 are BOTH written at toggle of LDLOos */
  1946. /* Capture the Divider and Numerator portions of other LO */
  1947. u8 tempLO2CQ[3];
  1948. u8 tempLO2C[3];
  1949. u8 tmpOneShot;
  1950. u32 Div, FracN;
  1951. u8 restore = 0;
  1952. /* Buffer the queue for restoration later and get actual LO2 values. */
  1953. status |=
  1954. mt2063_read(state,
  1955. MT2063_REG_LO2CQ_1,
  1956. &(tempLO2CQ[0]), 3);
  1957. status |=
  1958. mt2063_read(state,
  1959. MT2063_REG_LO2C_1,
  1960. &(tempLO2C[0]), 3);
  1961. /* clear the one-shot bits */
  1962. tempLO2CQ[2] = tempLO2CQ[2] & 0x0F;
  1963. tempLO2C[2] = tempLO2C[2] & 0x0F;
  1964. /* only write the queue values if they are different from the actual. */
  1965. if ((tempLO2CQ[0] != tempLO2C[0]) ||
  1966. (tempLO2CQ[1] != tempLO2C[1]) ||
  1967. (tempLO2CQ[2] != tempLO2C[2])) {
  1968. /* put actual LO2 value into queue (with 0 in one-shot bits) */
  1969. status |=
  1970. mt2063_write(state,
  1971. MT2063_REG_LO2CQ_1,
  1972. &(tempLO2C[0]), 3);
  1973. if (status == 0) {
  1974. /* cache the bytes just written. */
  1975. state->reg[MT2063_REG_LO2CQ_1] =
  1976. tempLO2C[0];
  1977. state->reg[MT2063_REG_LO2CQ_2] =
  1978. tempLO2C[1];
  1979. state->reg[MT2063_REG_LO2CQ_3] =
  1980. tempLO2C[2];
  1981. }
  1982. restore = 1;
  1983. }
  1984. /* Calculate the Divider and Numberator components of LO1 */
  1985. status =
  1986. MT2063_CalcLO1Mult(&Div, &FracN, nValue,
  1987. state->AS_Data.f_ref /
  1988. 64,
  1989. state->AS_Data.f_ref);
  1990. state->reg[MT2063_REG_LO1CQ_1] =
  1991. (u8) (Div & 0x00FF);
  1992. state->reg[MT2063_REG_LO1CQ_2] =
  1993. (u8) (FracN);
  1994. status |=
  1995. mt2063_write(state,
  1996. MT2063_REG_LO1CQ_1,
  1997. &state->
  1998. reg[MT2063_REG_LO1CQ_1], 2);
  1999. /* set the one-shot bit to load the pair of LO values */
  2000. tmpOneShot = tempLO2CQ[2] | 0xE0;
  2001. status |=
  2002. mt2063_write(state,
  2003. MT2063_REG_LO2CQ_3,
  2004. &tmpOneShot, 1);
  2005. /* only restore the queue values if they were different from the actual. */
  2006. if (restore) {
  2007. /* put actual LO2 value into queue (0 in one-shot bits) */
  2008. status |=
  2009. mt2063_write(state,
  2010. MT2063_REG_LO2CQ_1,
  2011. &(tempLO2CQ[0]), 3);
  2012. /* cache the bytes just written. */
  2013. state->reg[MT2063_REG_LO2CQ_1] =
  2014. tempLO2CQ[0];
  2015. state->reg[MT2063_REG_LO2CQ_2] =
  2016. tempLO2CQ[1];
  2017. state->reg[MT2063_REG_LO2CQ_3] =
  2018. tempLO2CQ[2];
  2019. }
  2020. MT2063_GetParam(state,
  2021. MT2063_LO1_FREQ,
  2022. &state->AS_Data.f_LO1);
  2023. }
  2024. break;
  2025. /* LO1 minimum step size */
  2026. case MT2063_LO1_STEPSIZE:
  2027. state->AS_Data.f_LO1_Step = nValue;
  2028. break;
  2029. /* LO1 FracN keep-out region */
  2030. case MT2063_LO1_FRACN_AVOID_PARAM:
  2031. state->AS_Data.f_LO1_FracN_Avoid = nValue;
  2032. break;
  2033. /* Requested 1st IF */
  2034. case MT2063_IF1_REQUEST:
  2035. state->AS_Data.f_if1_Request = nValue;
  2036. break;
  2037. /* zero-IF bandwidth */
  2038. case MT2063_ZIF_BW:
  2039. state->AS_Data.f_zif_bw = nValue;
  2040. break;
  2041. /* LO2 frequency */
  2042. case MT2063_LO2_FREQ:
  2043. {
  2044. /* Note: LO1 and LO2 are BOTH written at toggle of LDLOos */
  2045. /* Capture the Divider and Numerator portions of other LO */
  2046. u8 tempLO1CQ[2];
  2047. u8 tempLO1C[2];
  2048. u32 Div2;
  2049. u32 FracN2;
  2050. u8 tmpOneShot;
  2051. u8 restore = 0;
  2052. /* Buffer the queue for restoration later and get actual LO2 values. */
  2053. status |=
  2054. mt2063_read(state,
  2055. MT2063_REG_LO1CQ_1,
  2056. &(tempLO1CQ[0]), 2);
  2057. status |=
  2058. mt2063_read(state,
  2059. MT2063_REG_LO1C_1,
  2060. &(tempLO1C[0]), 2);
  2061. /* only write the queue values if they are different from the actual. */
  2062. if ((tempLO1CQ[0] != tempLO1C[0])
  2063. || (tempLO1CQ[1] != tempLO1C[1])) {
  2064. /* put actual LO1 value into queue */
  2065. status |=
  2066. mt2063_write(state,
  2067. MT2063_REG_LO1CQ_1,
  2068. &(tempLO1C[0]), 2);
  2069. /* cache the bytes just written. */
  2070. state->reg[MT2063_REG_LO1CQ_1] =
  2071. tempLO1C[0];
  2072. state->reg[MT2063_REG_LO1CQ_2] =
  2073. tempLO1C[1];
  2074. restore = 1;
  2075. }
  2076. /* Calculate the Divider and Numberator components of LO2 */
  2077. status =
  2078. MT2063_CalcLO2Mult(&Div2, &FracN2, nValue,
  2079. state->AS_Data.f_ref /
  2080. 8191,
  2081. state->AS_Data.f_ref);
  2082. state->reg[MT2063_REG_LO2CQ_1] =
  2083. (u8) ((Div2 << 1) |
  2084. ((FracN2 >> 12) & 0x01)) & 0xFF;
  2085. state->reg[MT2063_REG_LO2CQ_2] =
  2086. (u8) ((FracN2 >> 4) & 0xFF);
  2087. state->reg[MT2063_REG_LO2CQ_3] =
  2088. (u8) ((FracN2 & 0x0F));
  2089. status |=
  2090. mt2063_write(state,
  2091. MT2063_REG_LO1CQ_1,
  2092. &state->
  2093. reg[MT2063_REG_LO1CQ_1], 3);
  2094. /* set the one-shot bit to load the LO values */
  2095. tmpOneShot =
  2096. state->reg[MT2063_REG_LO2CQ_3] | 0xE0;
  2097. status |=
  2098. mt2063_write(state,
  2099. MT2063_REG_LO2CQ_3,
  2100. &tmpOneShot, 1);
  2101. /* only restore LO1 queue value if they were different from the actual. */
  2102. if (restore) {
  2103. /* put previous LO1 queue value back into queue */
  2104. status |=
  2105. mt2063_write(state,
  2106. MT2063_REG_LO1CQ_1,
  2107. &(tempLO1CQ[0]), 2);
  2108. /* cache the bytes just written. */
  2109. state->reg[MT2063_REG_LO1CQ_1] =
  2110. tempLO1CQ[0];
  2111. state->reg[MT2063_REG_LO1CQ_2] =
  2112. tempLO1CQ[1];
  2113. }
  2114. MT2063_GetParam(state,
  2115. MT2063_LO2_FREQ,
  2116. &state->AS_Data.f_LO2);
  2117. }
  2118. break;
  2119. /* LO2 minimum step size */
  2120. case MT2063_LO2_STEPSIZE:
  2121. state->AS_Data.f_LO2_Step = nValue;
  2122. break;
  2123. /* LO2 FracN keep-out region */
  2124. case MT2063_LO2_FRACN_AVOID:
  2125. state->AS_Data.f_LO2_FracN_Avoid = nValue;
  2126. break;
  2127. /* output center frequency */
  2128. case MT2063_OUTPUT_FREQ:
  2129. state->AS_Data.f_out = nValue;
  2130. break;
  2131. /* output bandwidth */
  2132. case MT2063_OUTPUT_BW:
  2133. state->AS_Data.f_out_bw = nValue + 750000;
  2134. break;
  2135. /* min inter-tuner LO separation */
  2136. case MT2063_LO_SEPARATION:
  2137. state->AS_Data.f_min_LO_Separation = nValue;
  2138. break;
  2139. /* max # of intra-tuner harmonics */
  2140. case MT2063_MAX_HARM1:
  2141. state->AS_Data.maxH1 = nValue;
  2142. break;
  2143. /* max # of inter-tuner harmonics */
  2144. case MT2063_MAX_HARM2:
  2145. state->AS_Data.maxH2 = nValue;
  2146. break;
  2147. case MT2063_RCVR_MODE:
  2148. status |=
  2149. MT2063_SetReceiverMode(state,
  2150. (enum MT2063_RCVR_MODES)
  2151. nValue);
  2152. break;
  2153. /* Set LNA Rin -- nValue is desired value */
  2154. case MT2063_LNA_RIN:
  2155. val =
  2156. (state->
  2157. reg[MT2063_REG_CTRL_2C] & (u8) ~ 0x03) |
  2158. (nValue & 0x03);
  2159. if (state->reg[MT2063_REG_CTRL_2C] != val) {
  2160. status |=
  2161. MT2063_SetReg(state, MT2063_REG_CTRL_2C,
  2162. val);
  2163. }
  2164. break;
  2165. /* Set target power level at LNA -- nValue is desired value */
  2166. case MT2063_LNA_TGT:
  2167. val =
  2168. (state->
  2169. reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x3F) |
  2170. (nValue & 0x3F);
  2171. if (state->reg[MT2063_REG_LNA_TGT] != val) {
  2172. status |=
  2173. MT2063_SetReg(state, MT2063_REG_LNA_TGT,
  2174. val);
  2175. }
  2176. break;
  2177. /* Set target power level at PD1 -- nValue is desired value */
  2178. case MT2063_PD1_TGT:
  2179. val =
  2180. (state->
  2181. reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x3F) |
  2182. (nValue & 0x3F);
  2183. if (state->reg[MT2063_REG_PD1_TGT] != val) {
  2184. status |=
  2185. MT2063_SetReg(state, MT2063_REG_PD1_TGT,
  2186. val);
  2187. }
  2188. break;
  2189. /* Set target power level at PD2 -- nValue is desired value */
  2190. case MT2063_PD2_TGT:
  2191. val =
  2192. (state->
  2193. reg[MT2063_REG_PD2_TGT] & (u8) ~ 0x3F) |
  2194. (nValue & 0x3F);
  2195. if (state->reg[MT2063_REG_PD2_TGT] != val) {
  2196. status |=
  2197. MT2063_SetReg(state, MT2063_REG_PD2_TGT,
  2198. val);
  2199. }
  2200. break;
  2201. /* Set LNA atten limit -- nValue is desired value */
  2202. case MT2063_ACLNA_MAX:
  2203. val =
  2204. (state->
  2205. reg[MT2063_REG_LNA_OV] & (u8) ~ 0x1F) | (nValue
  2206. &
  2207. 0x1F);
  2208. if (state->reg[MT2063_REG_LNA_OV] != val) {
  2209. status |=
  2210. MT2063_SetReg(state, MT2063_REG_LNA_OV,
  2211. val);
  2212. }
  2213. break;
  2214. /* Set RF atten limit -- nValue is desired value */
  2215. case MT2063_ACRF_MAX:
  2216. val =
  2217. (state->
  2218. reg[MT2063_REG_RF_OV] & (u8) ~ 0x1F) | (nValue
  2219. &
  2220. 0x1F);
  2221. if (state->reg[MT2063_REG_RF_OV] != val) {
  2222. status |=
  2223. MT2063_SetReg(state, MT2063_REG_RF_OV, val);
  2224. }
  2225. break;
  2226. /* Set FIF atten limit -- nValue is desired value, max. 5 if no B3 */
  2227. case MT2063_ACFIF_MAX:
  2228. if (state->reg[MT2063_REG_PART_REV] != MT2063_B3
  2229. && nValue > 5)
  2230. nValue = 5;
  2231. val =
  2232. (state->
  2233. reg[MT2063_REG_FIF_OV] & (u8) ~ 0x1F) | (nValue
  2234. &
  2235. 0x1F);
  2236. if (state->reg[MT2063_REG_FIF_OV] != val) {
  2237. status |=
  2238. MT2063_SetReg(state, MT2063_REG_FIF_OV,
  2239. val);
  2240. }
  2241. break;
  2242. case MT2063_DNC_OUTPUT_ENABLE:
  2243. /* selects, which DNC output is used */
  2244. switch (nValue) {
  2245. case MT2063_DNC_NONE:
  2246. {
  2247. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  2248. if (state->reg[MT2063_REG_DNC_GAIN] !=
  2249. val)
  2250. status |=
  2251. MT2063_SetReg(state,
  2252. MT2063_REG_DNC_GAIN,
  2253. val);
  2254. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  2255. if (state->reg[MT2063_REG_VGA_GAIN] !=
  2256. val)
  2257. status |=
  2258. MT2063_SetReg(state,
  2259. MT2063_REG_VGA_GAIN,
  2260. val);
  2261. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  2262. if (state->reg[MT2063_REG_RSVD_20] !=
  2263. val)
  2264. status |=
  2265. MT2063_SetReg(state,
  2266. MT2063_REG_RSVD_20,
  2267. val);
  2268. break;
  2269. }
  2270. case MT2063_DNC_1:
  2271. {
  2272. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  2273. if (state->reg[MT2063_REG_DNC_GAIN] !=
  2274. val)
  2275. status |=
  2276. MT2063_SetReg(state,
  2277. MT2063_REG_DNC_GAIN,
  2278. val);
  2279. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  2280. if (state->reg[MT2063_REG_VGA_GAIN] !=
  2281. val)
  2282. status |=
  2283. MT2063_SetReg(state,
  2284. MT2063_REG_VGA_GAIN,
  2285. val);
  2286. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  2287. if (state->reg[MT2063_REG_RSVD_20] !=
  2288. val)
  2289. status |=
  2290. MT2063_SetReg(state,
  2291. MT2063_REG_RSVD_20,
  2292. val);
  2293. break;
  2294. }
  2295. case MT2063_DNC_2:
  2296. {
  2297. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  2298. if (state->reg[MT2063_REG_DNC_GAIN] !=
  2299. val)
  2300. status |=
  2301. MT2063_SetReg(state,
  2302. MT2063_REG_DNC_GAIN,
  2303. val);
  2304. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  2305. if (state->reg[MT2063_REG_VGA_GAIN] !=
  2306. val)
  2307. status |=
  2308. MT2063_SetReg(state,
  2309. MT2063_REG_VGA_GAIN,
  2310. val);
  2311. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  2312. if (state->reg[MT2063_REG_RSVD_20] !=
  2313. val)
  2314. status |=
  2315. MT2063_SetReg(state,
  2316. MT2063_REG_RSVD_20,
  2317. val);
  2318. break;
  2319. }
  2320. case MT2063_DNC_BOTH:
  2321. {
  2322. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  2323. if (state->reg[MT2063_REG_DNC_GAIN] !=
  2324. val)
  2325. status |=
  2326. MT2063_SetReg(state,
  2327. MT2063_REG_DNC_GAIN,
  2328. val);
  2329. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  2330. if (state->reg[MT2063_REG_VGA_GAIN] !=
  2331. val)
  2332. status |=
  2333. MT2063_SetReg(state,
  2334. MT2063_REG_VGA_GAIN,
  2335. val);
  2336. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  2337. if (state->reg[MT2063_REG_RSVD_20] !=
  2338. val)
  2339. status |=
  2340. MT2063_SetReg(state,
  2341. MT2063_REG_RSVD_20,
  2342. val);
  2343. break;
  2344. }
  2345. default:
  2346. break;
  2347. }
  2348. break;
  2349. case MT2063_VGAGC:
  2350. /* Set VGA gain code */
  2351. val =
  2352. (state->
  2353. reg[MT2063_REG_VGA_GAIN] & (u8) ~ 0x0C) |
  2354. ((nValue & 0x03) << 2);
  2355. if (state->reg[MT2063_REG_VGA_GAIN] != val) {
  2356. status |=
  2357. MT2063_SetReg(state, MT2063_REG_VGA_GAIN,
  2358. val);
  2359. }
  2360. break;
  2361. case MT2063_VGAOI:
  2362. /* Set VGA bias current */
  2363. val =
  2364. (state->
  2365. reg[MT2063_REG_RSVD_31] & (u8) ~ 0x07) |
  2366. (nValue & 0x07);
  2367. if (state->reg[MT2063_REG_RSVD_31] != val) {
  2368. status |=
  2369. MT2063_SetReg(state, MT2063_REG_RSVD_31,
  2370. val);
  2371. }
  2372. break;
  2373. case MT2063_TAGC:
  2374. /* Set TAGC */
  2375. val =
  2376. (state->
  2377. reg[MT2063_REG_RSVD_1E] & (u8) ~ 0x03) |
  2378. (nValue & 0x03);
  2379. if (state->reg[MT2063_REG_RSVD_1E] != val) {
  2380. status |=
  2381. MT2063_SetReg(state, MT2063_REG_RSVD_1E,
  2382. val);
  2383. }
  2384. break;
  2385. case MT2063_AMPGC:
  2386. /* Set Amp gain code */
  2387. val =
  2388. (state->
  2389. reg[MT2063_REG_TEMP_SEL] & (u8) ~ 0x03) |
  2390. (nValue & 0x03);
  2391. if (state->reg[MT2063_REG_TEMP_SEL] != val) {
  2392. status |=
  2393. MT2063_SetReg(state, MT2063_REG_TEMP_SEL,
  2394. val);
  2395. }
  2396. break;
  2397. /* Avoid DECT Frequencies */
  2398. case MT2063_AVOID_DECT:
  2399. {
  2400. enum MT2063_DECT_Avoid_Type newAvoidSetting =
  2401. (enum MT2063_DECT_Avoid_Type)nValue;
  2402. if ((newAvoidSetting >=
  2403. MT2063_NO_DECT_AVOIDANCE)
  2404. && (newAvoidSetting <= MT2063_AVOID_BOTH)) {
  2405. state->AS_Data.avoidDECT =
  2406. newAvoidSetting;
  2407. }
  2408. }
  2409. break;
  2410. /* Cleartune filter selection: 0 - by IC (default), 1 - by software */
  2411. case MT2063_CTFILT_SW:
  2412. state->ctfilt_sw = (nValue & 0x01);
  2413. break;
  2414. /* These parameters are read-only */
  2415. case MT2063_IC_ADDR:
  2416. case MT2063_MAX_OPEN:
  2417. case MT2063_NUM_OPEN:
  2418. case MT2063_INPUT_FREQ:
  2419. case MT2063_IF1_ACTUAL:
  2420. case MT2063_IF1_CENTER:
  2421. case MT2063_IF1_BW:
  2422. case MT2063_AS_ALG:
  2423. case MT2063_EXCL_ZONES:
  2424. case MT2063_SPUR_AVOIDED:
  2425. case MT2063_NUM_SPURS:
  2426. case MT2063_SPUR_PRESENT:
  2427. case MT2063_ACLNA:
  2428. case MT2063_ACRF:
  2429. case MT2063_ACFIF:
  2430. case MT2063_EOP:
  2431. default:
  2432. status |= -ERANGE;
  2433. }
  2434. return (status);
  2435. }
  2436. /****************************************************************************
  2437. **
  2438. ** Name: MT2063_ClearPowerMaskBits
  2439. **
  2440. ** Description: Clears the power-down mask bits for various sections of
  2441. ** the MT2063
  2442. **
  2443. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  2444. ** Bits - Mask bits to be cleared.
  2445. **
  2446. ** See definition of MT2063_Mask_Bits type for description
  2447. ** of each of the power bits.
  2448. **
  2449. ** Returns: status:
  2450. ** MT_OK - No errors
  2451. ** MT_INV_HANDLE - Invalid tuner handle
  2452. ** MT_COMM_ERR - Serial bus communications error
  2453. **
  2454. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  2455. **
  2456. ** Revision History:
  2457. **
  2458. ** SCR Date Author Description
  2459. ** -------------------------------------------------------------------------
  2460. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2461. **
  2462. ****************************************************************************/
  2463. static u32 MT2063_ClearPowerMaskBits(struct mt2063_state *state, enum MT2063_Mask_Bits Bits)
  2464. {
  2465. u32 status = 0; /* Status to be returned */
  2466. Bits = (enum MT2063_Mask_Bits)(Bits & MT2063_ALL_SD); /* Only valid bits for this tuner */
  2467. if ((Bits & 0xFF00) != 0) {
  2468. state->reg[MT2063_REG_PWR_2] &= ~(u8) (Bits >> 8);
  2469. status |=
  2470. mt2063_write(state,
  2471. MT2063_REG_PWR_2,
  2472. &state->reg[MT2063_REG_PWR_2], 1);
  2473. }
  2474. if ((Bits & 0xFF) != 0) {
  2475. state->reg[MT2063_REG_PWR_1] &= ~(u8) (Bits & 0xFF);
  2476. status |=
  2477. mt2063_write(state,
  2478. MT2063_REG_PWR_1,
  2479. &state->reg[MT2063_REG_PWR_1], 1);
  2480. }
  2481. return (status);
  2482. }
  2483. /****************************************************************************
  2484. **
  2485. ** Name: MT2063_SoftwareShutdown
  2486. **
  2487. ** Description: Enables or disables software shutdown function. When
  2488. ** Shutdown==1, any section whose power mask is set will be
  2489. ** shutdown.
  2490. **
  2491. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  2492. ** Shutdown - 1 = shutdown the masked sections, otherwise
  2493. ** power all sections on
  2494. **
  2495. ** Returns: status:
  2496. ** MT_OK - No errors
  2497. ** MT_INV_HANDLE - Invalid tuner handle
  2498. ** MT_COMM_ERR - Serial bus communications error
  2499. **
  2500. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  2501. **
  2502. ** Revision History:
  2503. **
  2504. ** SCR Date Author Description
  2505. ** -------------------------------------------------------------------------
  2506. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2507. ** 01-03-2008 PINZ Ver 1.xx: Added a trigger of BYPATNUP for
  2508. ** correct wakeup of the LNA
  2509. **
  2510. ****************************************************************************/
  2511. static u32 MT2063_SoftwareShutdown(struct mt2063_state *state, u8 Shutdown)
  2512. {
  2513. u32 status = 0; /* Status to be returned */
  2514. if (Shutdown == 1)
  2515. state->reg[MT2063_REG_PWR_1] |= 0x04; /* Turn the bit on */
  2516. else
  2517. state->reg[MT2063_REG_PWR_1] &= ~0x04; /* Turn off the bit */
  2518. status |=
  2519. mt2063_write(state,
  2520. MT2063_REG_PWR_1,
  2521. &state->reg[MT2063_REG_PWR_1], 1);
  2522. if (Shutdown != 1) {
  2523. state->reg[MT2063_REG_BYP_CTRL] =
  2524. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F) | 0x40;
  2525. status |=
  2526. mt2063_write(state,
  2527. MT2063_REG_BYP_CTRL,
  2528. &state->reg[MT2063_REG_BYP_CTRL],
  2529. 1);
  2530. state->reg[MT2063_REG_BYP_CTRL] =
  2531. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F);
  2532. status |=
  2533. mt2063_write(state,
  2534. MT2063_REG_BYP_CTRL,
  2535. &state->reg[MT2063_REG_BYP_CTRL],
  2536. 1);
  2537. }
  2538. return (status);
  2539. }
  2540. /****************************************************************************
  2541. **
  2542. ** Name: MT2063_SetReg
  2543. **
  2544. ** Description: Sets an MT2063 register.
  2545. **
  2546. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  2547. ** reg - MT2063 register/subaddress location
  2548. ** val - MT2063 register/subaddress value
  2549. **
  2550. ** Returns: status:
  2551. ** MT_OK - No errors
  2552. ** MT_COMM_ERR - Serial bus communications error
  2553. ** MT_INV_HANDLE - Invalid tuner handle
  2554. ** MT_ARG_RANGE - Argument out of range
  2555. **
  2556. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  2557. **
  2558. ** Use this function if you need to override a default
  2559. ** register value
  2560. **
  2561. ** Revision History:
  2562. **
  2563. ** SCR Date Author Description
  2564. ** -------------------------------------------------------------------------
  2565. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2566. **
  2567. ****************************************************************************/
  2568. static u32 MT2063_SetReg(struct mt2063_state *state, u8 reg, u8 val)
  2569. {
  2570. u32 status = 0; /* Status to be returned */
  2571. if (reg >= MT2063_REG_END_REGS)
  2572. status |= -ERANGE;
  2573. status = mt2063_write(state, reg, &val,
  2574. 1);
  2575. if (status >= 0)
  2576. state->reg[reg] = val;
  2577. return (status);
  2578. }
  2579. static u32 MT2063_Round_fLO(u32 f_LO, u32 f_LO_Step, u32 f_ref)
  2580. {
  2581. return f_ref * (f_LO / f_ref)
  2582. + f_LO_Step * (((f_LO % f_ref) + (f_LO_Step / 2)) / f_LO_Step);
  2583. }
  2584. /****************************************************************************
  2585. **
  2586. ** Name: fLO_FractionalTerm
  2587. **
  2588. ** Description: Calculates the portion contributed by FracN / denom.
  2589. **
  2590. ** This function preserves maximum precision without
  2591. ** risk of overflow. It accurately calculates
  2592. ** f_ref * num / denom to within 1 HZ with fixed math.
  2593. **
  2594. ** Parameters: num - Fractional portion of the multiplier
  2595. ** denom - denominator portion of the ratio
  2596. ** This routine successfully handles denom values
  2597. ** up to and including 2^18.
  2598. ** f_Ref - SRO frequency. This calculation handles
  2599. ** f_ref as two separate 14-bit fields.
  2600. ** Therefore, a maximum value of 2^28-1
  2601. ** may safely be used for f_ref. This is
  2602. ** the genesis of the magic number "14" and the
  2603. ** magic mask value of 0x03FFF.
  2604. **
  2605. ** Returns: f_ref * num / denom
  2606. **
  2607. ** Revision History:
  2608. **
  2609. ** SCR Date Author Description
  2610. ** -------------------------------------------------------------------------
  2611. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2612. **
  2613. ****************************************************************************/
  2614. static u32 MT2063_fLO_FractionalTerm(u32 f_ref,
  2615. u32 num, u32 denom)
  2616. {
  2617. u32 t1 = (f_ref >> 14) * num;
  2618. u32 term1 = t1 / denom;
  2619. u32 loss = t1 % denom;
  2620. u32 term2 =
  2621. (((f_ref & 0x00003FFF) * num + (loss << 14)) + (denom / 2)) / denom;
  2622. return ((term1 << 14) + term2);
  2623. }
  2624. /****************************************************************************
  2625. **
  2626. ** Name: CalcLO1Mult
  2627. **
  2628. ** Description: Calculates Integer divider value and the numerator
  2629. ** value for a FracN PLL.
  2630. **
  2631. ** This function assumes that the f_LO and f_Ref are
  2632. ** evenly divisible by f_LO_Step.
  2633. **
  2634. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  2635. ** FracN - OUTPUT: Fractional portion of the multiplier
  2636. ** f_LO - desired LO frequency.
  2637. ** f_LO_Step - Minimum step size for the LO (in Hz).
  2638. ** f_Ref - SRO frequency.
  2639. ** f_Avoid - Range of PLL frequencies to avoid near
  2640. ** integer multiples of f_Ref (in Hz).
  2641. **
  2642. ** Returns: Recalculated LO frequency.
  2643. **
  2644. ** Revision History:
  2645. **
  2646. ** SCR Date Author Description
  2647. ** -------------------------------------------------------------------------
  2648. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2649. **
  2650. ****************************************************************************/
  2651. static u32 MT2063_CalcLO1Mult(u32 * Div,
  2652. u32 * FracN,
  2653. u32 f_LO,
  2654. u32 f_LO_Step, u32 f_Ref)
  2655. {
  2656. /* Calculate the whole number portion of the divider */
  2657. *Div = f_LO / f_Ref;
  2658. /* Calculate the numerator value (round to nearest f_LO_Step) */
  2659. *FracN =
  2660. (64 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  2661. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  2662. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN, 64);
  2663. }
  2664. /****************************************************************************
  2665. **
  2666. ** Name: CalcLO2Mult
  2667. **
  2668. ** Description: Calculates Integer divider value and the numerator
  2669. ** value for a FracN PLL.
  2670. **
  2671. ** This function assumes that the f_LO and f_Ref are
  2672. ** evenly divisible by f_LO_Step.
  2673. **
  2674. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  2675. ** FracN - OUTPUT: Fractional portion of the multiplier
  2676. ** f_LO - desired LO frequency.
  2677. ** f_LO_Step - Minimum step size for the LO (in Hz).
  2678. ** f_Ref - SRO frequency.
  2679. ** f_Avoid - Range of PLL frequencies to avoid near
  2680. ** integer multiples of f_Ref (in Hz).
  2681. **
  2682. ** Returns: Recalculated LO frequency.
  2683. **
  2684. ** Revision History:
  2685. **
  2686. ** SCR Date Author Description
  2687. ** -------------------------------------------------------------------------
  2688. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2689. **
  2690. ****************************************************************************/
  2691. static u32 MT2063_CalcLO2Mult(u32 * Div,
  2692. u32 * FracN,
  2693. u32 f_LO,
  2694. u32 f_LO_Step, u32 f_Ref)
  2695. {
  2696. /* Calculate the whole number portion of the divider */
  2697. *Div = f_LO / f_Ref;
  2698. /* Calculate the numerator value (round to nearest f_LO_Step) */
  2699. *FracN =
  2700. (8191 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  2701. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  2702. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN,
  2703. 8191);
  2704. }
  2705. /****************************************************************************
  2706. **
  2707. ** Name: FindClearTuneFilter
  2708. **
  2709. ** Description: Calculate the corrrect ClearTune filter to be used for
  2710. ** a given input frequency.
  2711. **
  2712. ** Parameters: state - ptr to tuner data structure
  2713. ** f_in - RF input center frequency (in Hz).
  2714. **
  2715. ** Returns: ClearTune filter number (0-31)
  2716. **
  2717. ** Dependencies: MUST CALL MT2064_Open BEFORE FindClearTuneFilter!
  2718. **
  2719. ** Revision History:
  2720. **
  2721. ** SCR Date Author Description
  2722. ** -------------------------------------------------------------------------
  2723. ** 04-10-2008 PINZ Ver 1.14: Use software-controlled ClearTune
  2724. ** cross-over frequency values.
  2725. **
  2726. ****************************************************************************/
  2727. static u32 FindClearTuneFilter(struct mt2063_state *state, u32 f_in)
  2728. {
  2729. u32 RFBand;
  2730. u32 idx; /* index loop */
  2731. /*
  2732. ** Find RF Band setting
  2733. */
  2734. RFBand = 31; /* def when f_in > all */
  2735. for (idx = 0; idx < 31; ++idx) {
  2736. if (state->CTFiltMax[idx] >= f_in) {
  2737. RFBand = idx;
  2738. break;
  2739. }
  2740. }
  2741. return (RFBand);
  2742. }
  2743. /****************************************************************************
  2744. **
  2745. ** Name: MT2063_Tune
  2746. **
  2747. ** Description: Change the tuner's tuned frequency to RFin.
  2748. **
  2749. ** Parameters: h - Open handle to the tuner (from MT2063_Open).
  2750. ** f_in - RF input center frequency (in Hz).
  2751. **
  2752. ** Returns: status:
  2753. ** MT_OK - No errors
  2754. ** MT_INV_HANDLE - Invalid tuner handle
  2755. ** MT_UPC_UNLOCK - Upconverter PLL unlocked
  2756. ** MT_DNC_UNLOCK - Downconverter PLL unlocked
  2757. ** MT_COMM_ERR - Serial bus communications error
  2758. ** MT_SPUR_CNT_MASK - Count of avoided LO spurs
  2759. ** MT_SPUR_PRESENT - LO spur possible in output
  2760. ** MT_FIN_RANGE - Input freq out of range
  2761. ** MT_FOUT_RANGE - Output freq out of range
  2762. ** MT_UPC_RANGE - Upconverter freq out of range
  2763. ** MT_DNC_RANGE - Downconverter freq out of range
  2764. **
  2765. ** Dependencies: MUST CALL MT2063_Open BEFORE MT2063_Tune!
  2766. **
  2767. ** MT_ReadSub - Read data from the two-wire serial bus
  2768. ** MT_WriteSub - Write data to the two-wire serial bus
  2769. ** MT_Sleep - Delay execution for x milliseconds
  2770. ** MT2063_GetLocked - Checks to see if LO1 and LO2 are locked
  2771. **
  2772. ** Revision History:
  2773. **
  2774. ** SCR Date Author Description
  2775. ** -------------------------------------------------------------------------
  2776. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  2777. ** 04-10-2008 PINZ Ver 1.05: Use software-controlled ClearTune
  2778. ** cross-over frequency values.
  2779. ** 175 I 16-06-2008 PINZ Ver 1.16: Add control to avoid US DECT freqs.
  2780. ** 175 I 06-19-2008 RSK Ver 1.17: Refactor DECT control to SpurAvoid.
  2781. ** 06-24-2008 PINZ Ver 1.18: Add Get/SetParam CTFILT_SW
  2782. **
  2783. ****************************************************************************/
  2784. static u32 MT2063_Tune(struct mt2063_state *state, u32 f_in)
  2785. { /* RF input center frequency */
  2786. u32 status = 0; /* status of operation */
  2787. u32 LO1; /* 1st LO register value */
  2788. u32 Num1; /* Numerator for LO1 reg. value */
  2789. u32 f_IF1; /* 1st IF requested */
  2790. u32 LO2; /* 2nd LO register value */
  2791. u32 Num2; /* Numerator for LO2 reg. value */
  2792. u32 ofLO1, ofLO2; /* last time's LO frequencies */
  2793. u32 ofin, ofout; /* last time's I/O frequencies */
  2794. u8 fiffc = 0x80; /* FIFF center freq from tuner */
  2795. u32 fiffof; /* Offset from FIFF center freq */
  2796. const u8 LO1LK = 0x80; /* Mask for LO1 Lock bit */
  2797. u8 LO2LK = 0x08; /* Mask for LO2 Lock bit */
  2798. u8 val;
  2799. u32 RFBand;
  2800. /* Check the input and output frequency ranges */
  2801. if ((f_in < MT2063_MIN_FIN_FREQ) || (f_in > MT2063_MAX_FIN_FREQ))
  2802. return -EINVAL;
  2803. if ((state->AS_Data.f_out < MT2063_MIN_FOUT_FREQ)
  2804. || (state->AS_Data.f_out > MT2063_MAX_FOUT_FREQ))
  2805. return -EINVAL;
  2806. /*
  2807. ** Save original LO1 and LO2 register values
  2808. */
  2809. ofLO1 = state->AS_Data.f_LO1;
  2810. ofLO2 = state->AS_Data.f_LO2;
  2811. ofin = state->AS_Data.f_in;
  2812. ofout = state->AS_Data.f_out;
  2813. /*
  2814. ** Find and set RF Band setting
  2815. */
  2816. if (state->ctfilt_sw == 1) {
  2817. val = (state->reg[MT2063_REG_CTUNE_CTRL] | 0x08);
  2818. if (state->reg[MT2063_REG_CTUNE_CTRL] != val) {
  2819. status |=
  2820. MT2063_SetReg(state, MT2063_REG_CTUNE_CTRL, val);
  2821. }
  2822. val = state->reg[MT2063_REG_CTUNE_OV];
  2823. RFBand = FindClearTuneFilter(state, f_in);
  2824. state->reg[MT2063_REG_CTUNE_OV] =
  2825. (u8) ((state->reg[MT2063_REG_CTUNE_OV] & ~0x1F)
  2826. | RFBand);
  2827. if (state->reg[MT2063_REG_CTUNE_OV] != val) {
  2828. status |=
  2829. MT2063_SetReg(state, MT2063_REG_CTUNE_OV, val);
  2830. }
  2831. }
  2832. /*
  2833. ** Read the FIFF Center Frequency from the tuner
  2834. */
  2835. if (status >= 0) {
  2836. status |=
  2837. mt2063_read(state,
  2838. MT2063_REG_FIFFC,
  2839. &state->reg[MT2063_REG_FIFFC], 1);
  2840. fiffc = state->reg[MT2063_REG_FIFFC];
  2841. }
  2842. /*
  2843. ** Assign in the requested values
  2844. */
  2845. state->AS_Data.f_in = f_in;
  2846. /* Request a 1st IF such that LO1 is on a step size */
  2847. state->AS_Data.f_if1_Request =
  2848. MT2063_Round_fLO(state->AS_Data.f_if1_Request + f_in,
  2849. state->AS_Data.f_LO1_Step,
  2850. state->AS_Data.f_ref) - f_in;
  2851. /*
  2852. ** Calculate frequency settings. f_IF1_FREQ + f_in is the
  2853. ** desired LO1 frequency
  2854. */
  2855. MT2063_ResetExclZones(&state->AS_Data);
  2856. f_IF1 = MT2063_ChooseFirstIF(&state->AS_Data);
  2857. state->AS_Data.f_LO1 =
  2858. MT2063_Round_fLO(f_IF1 + f_in, state->AS_Data.f_LO1_Step,
  2859. state->AS_Data.f_ref);
  2860. state->AS_Data.f_LO2 =
  2861. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  2862. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  2863. /*
  2864. ** Check for any LO spurs in the output bandwidth and adjust
  2865. ** the LO settings to avoid them if needed
  2866. */
  2867. status |= MT2063_AvoidSpurs(state, &state->AS_Data);
  2868. /*
  2869. ** MT_AvoidSpurs spurs may have changed the LO1 & LO2 values.
  2870. ** Recalculate the LO frequencies and the values to be placed
  2871. ** in the tuning registers.
  2872. */
  2873. state->AS_Data.f_LO1 =
  2874. MT2063_CalcLO1Mult(&LO1, &Num1, state->AS_Data.f_LO1,
  2875. state->AS_Data.f_LO1_Step, state->AS_Data.f_ref);
  2876. state->AS_Data.f_LO2 =
  2877. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  2878. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  2879. state->AS_Data.f_LO2 =
  2880. MT2063_CalcLO2Mult(&LO2, &Num2, state->AS_Data.f_LO2,
  2881. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  2882. /*
  2883. ** Check the upconverter and downconverter frequency ranges
  2884. */
  2885. if ((state->AS_Data.f_LO1 < MT2063_MIN_UPC_FREQ)
  2886. || (state->AS_Data.f_LO1 > MT2063_MAX_UPC_FREQ))
  2887. status |= MT2063_UPC_RANGE;
  2888. if ((state->AS_Data.f_LO2 < MT2063_MIN_DNC_FREQ)
  2889. || (state->AS_Data.f_LO2 > MT2063_MAX_DNC_FREQ))
  2890. status |= MT2063_DNC_RANGE;
  2891. /* LO2 Lock bit was in a different place for B0 version */
  2892. if (state->tuner_id == MT2063_B0)
  2893. LO2LK = 0x40;
  2894. /*
  2895. ** If we have the same LO frequencies and we're already locked,
  2896. ** then skip re-programming the LO registers.
  2897. */
  2898. if ((ofLO1 != state->AS_Data.f_LO1)
  2899. || (ofLO2 != state->AS_Data.f_LO2)
  2900. || ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) !=
  2901. (LO1LK | LO2LK))) {
  2902. /*
  2903. ** Calculate the FIFFOF register value
  2904. **
  2905. ** IF1_Actual
  2906. ** FIFFOF = ------------ - 8 * FIFFC - 4992
  2907. ** f_ref/64
  2908. */
  2909. fiffof =
  2910. (state->AS_Data.f_LO1 -
  2911. f_in) / (state->AS_Data.f_ref / 64) - 8 * (u32) fiffc -
  2912. 4992;
  2913. if (fiffof > 0xFF)
  2914. fiffof = 0xFF;
  2915. /*
  2916. ** Place all of the calculated values into the local tuner
  2917. ** register fields.
  2918. */
  2919. if (status >= 0) {
  2920. state->reg[MT2063_REG_LO1CQ_1] = (u8) (LO1 & 0xFF); /* DIV1q */
  2921. state->reg[MT2063_REG_LO1CQ_2] = (u8) (Num1 & 0x3F); /* NUM1q */
  2922. state->reg[MT2063_REG_LO2CQ_1] = (u8) (((LO2 & 0x7F) << 1) /* DIV2q */
  2923. |(Num2 >> 12)); /* NUM2q (hi) */
  2924. state->reg[MT2063_REG_LO2CQ_2] = (u8) ((Num2 & 0x0FF0) >> 4); /* NUM2q (mid) */
  2925. state->reg[MT2063_REG_LO2CQ_3] = (u8) (0xE0 | (Num2 & 0x000F)); /* NUM2q (lo) */
  2926. /*
  2927. ** Now write out the computed register values
  2928. ** IMPORTANT: There is a required order for writing
  2929. ** (0x05 must follow all the others).
  2930. */
  2931. status |= mt2063_write(state, MT2063_REG_LO1CQ_1, &state->reg[MT2063_REG_LO1CQ_1], 5); /* 0x01 - 0x05 */
  2932. if (state->tuner_id == MT2063_B0) {
  2933. /* Re-write the one-shot bits to trigger the tune operation */
  2934. status |= mt2063_write(state, MT2063_REG_LO2CQ_3, &state->reg[MT2063_REG_LO2CQ_3], 1); /* 0x05 */
  2935. }
  2936. /* Write out the FIFF offset only if it's changing */
  2937. if (state->reg[MT2063_REG_FIFF_OFFSET] !=
  2938. (u8) fiffof) {
  2939. state->reg[MT2063_REG_FIFF_OFFSET] =
  2940. (u8) fiffof;
  2941. status |=
  2942. mt2063_write(state,
  2943. MT2063_REG_FIFF_OFFSET,
  2944. &state->
  2945. reg[MT2063_REG_FIFF_OFFSET],
  2946. 1);
  2947. }
  2948. }
  2949. /*
  2950. ** Check for LO's locking
  2951. */
  2952. if (status >= 0) {
  2953. status |= MT2063_GetLocked(state);
  2954. }
  2955. /*
  2956. ** If we locked OK, assign calculated data to mt2063_state structure
  2957. */
  2958. if (status >= 0) {
  2959. state->f_IF1_actual = state->AS_Data.f_LO1 - f_in;
  2960. }
  2961. }
  2962. return (status);
  2963. }
  2964. static u32 MT_Tune_atv(void *h, u32 f_in, u32 bw_in,
  2965. enum MTTune_atv_standard tv_type)
  2966. {
  2967. u32 status = 0;
  2968. s32 pict_car = 0;
  2969. s32 pict2chanb_vsb = 0;
  2970. s32 pict2chanb_snd = 0;
  2971. s32 pict2snd1 = 0;
  2972. s32 pict2snd2 = 0;
  2973. s32 ch_bw = 0;
  2974. s32 if_mid = 0;
  2975. s32 rcvr_mode = 0;
  2976. u32 mode_get = 0;
  2977. switch (tv_type) {
  2978. case MTTUNEA_PAL_B:{
  2979. pict_car = 38900000;
  2980. ch_bw = 8000000;
  2981. pict2chanb_vsb = -1250000;
  2982. pict2snd1 = 5500000;
  2983. pict2snd2 = 5742000;
  2984. rcvr_mode = 1;
  2985. break;
  2986. }
  2987. case MTTUNEA_PAL_G:{
  2988. pict_car = 38900000;
  2989. ch_bw = 7000000;
  2990. pict2chanb_vsb = -1250000;
  2991. pict2snd1 = 5500000;
  2992. pict2snd2 = 0;
  2993. rcvr_mode = 1;
  2994. break;
  2995. }
  2996. case MTTUNEA_PAL_I:{
  2997. pict_car = 38900000;
  2998. ch_bw = 8000000;
  2999. pict2chanb_vsb = -1250000;
  3000. pict2snd1 = 6000000;
  3001. pict2snd2 = 0;
  3002. rcvr_mode = 1;
  3003. break;
  3004. }
  3005. case MTTUNEA_PAL_L:{
  3006. pict_car = 38900000;
  3007. ch_bw = 8000000;
  3008. pict2chanb_vsb = -1250000;
  3009. pict2snd1 = 6500000;
  3010. pict2snd2 = 0;
  3011. rcvr_mode = 1;
  3012. break;
  3013. }
  3014. case MTTUNEA_PAL_MN:{
  3015. pict_car = 38900000;
  3016. ch_bw = 6000000;
  3017. pict2chanb_vsb = -1250000;
  3018. pict2snd1 = 4500000;
  3019. pict2snd2 = 0;
  3020. rcvr_mode = 1;
  3021. break;
  3022. }
  3023. case MTTUNEA_PAL_DK:{
  3024. pict_car = 38900000;
  3025. ch_bw = 8000000;
  3026. pict2chanb_vsb = -1250000;
  3027. pict2snd1 = 6500000;
  3028. pict2snd2 = 0;
  3029. rcvr_mode = 1;
  3030. break;
  3031. }
  3032. case MTTUNEA_DIGITAL:{
  3033. pict_car = 36125000;
  3034. ch_bw = 8000000;
  3035. pict2chanb_vsb = -(ch_bw / 2);
  3036. pict2snd1 = 0;
  3037. pict2snd2 = 0;
  3038. rcvr_mode = 2;
  3039. break;
  3040. }
  3041. case MTTUNEA_FMRADIO:{
  3042. pict_car = 38900000;
  3043. ch_bw = 8000000;
  3044. pict2chanb_vsb = -(ch_bw / 2);
  3045. pict2snd1 = 0;
  3046. pict2snd2 = 0;
  3047. rcvr_mode = 4;
  3048. //f_in -= 2900000;
  3049. break;
  3050. }
  3051. case MTTUNEA_DVBC:{
  3052. pict_car = 36125000;
  3053. ch_bw = 8000000;
  3054. pict2chanb_vsb = -(ch_bw / 2);
  3055. pict2snd1 = 0;
  3056. pict2snd2 = 0;
  3057. rcvr_mode = MT2063_CABLE_QAM;
  3058. break;
  3059. }
  3060. case MTTUNEA_DVBT:{
  3061. pict_car = 36125000;
  3062. ch_bw = bw_in; //8000000
  3063. pict2chanb_vsb = -(ch_bw / 2);
  3064. pict2snd1 = 0;
  3065. pict2snd2 = 0;
  3066. rcvr_mode = MT2063_OFFAIR_COFDM;
  3067. break;
  3068. }
  3069. case MTTUNEA_UNKNOWN:
  3070. break;
  3071. default:
  3072. break;
  3073. }
  3074. pict2chanb_snd = pict2chanb_vsb - ch_bw;
  3075. if_mid = pict_car - (pict2chanb_vsb + (ch_bw / 2));
  3076. status |= MT2063_SetParam(h, MT2063_STEPSIZE, 125000);
  3077. status |= MT2063_SetParam(h, MT2063_OUTPUT_FREQ, if_mid);
  3078. status |= MT2063_SetParam(h, MT2063_OUTPUT_BW, ch_bw);
  3079. status |= MT2063_GetParam(h, MT2063_RCVR_MODE, &mode_get);
  3080. status |= MT2063_SetParam(h, MT2063_RCVR_MODE, rcvr_mode);
  3081. status |= MT2063_Tune(h, (f_in + (pict2chanb_vsb + (ch_bw / 2))));
  3082. status |= MT2063_GetParam(h, MT2063_RCVR_MODE, &mode_get);
  3083. return (u32) status;
  3084. }
  3085. static const u8 MT2063B0_defaults[] = {
  3086. /* Reg, Value */
  3087. 0x19, 0x05,
  3088. 0x1B, 0x1D,
  3089. 0x1C, 0x1F,
  3090. 0x1D, 0x0F,
  3091. 0x1E, 0x3F,
  3092. 0x1F, 0x0F,
  3093. 0x20, 0x3F,
  3094. 0x22, 0x21,
  3095. 0x23, 0x3F,
  3096. 0x24, 0x20,
  3097. 0x25, 0x3F,
  3098. 0x27, 0xEE,
  3099. 0x2C, 0x27, /* bit at 0x20 is cleared below */
  3100. 0x30, 0x03,
  3101. 0x2C, 0x07, /* bit at 0x20 is cleared here */
  3102. 0x2D, 0x87,
  3103. 0x2E, 0xAA,
  3104. 0x28, 0xE1, /* Set the FIFCrst bit here */
  3105. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  3106. 0x00
  3107. };
  3108. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  3109. static const u8 MT2063B1_defaults[] = {
  3110. /* Reg, Value */
  3111. 0x05, 0xF0,
  3112. 0x11, 0x10, /* New Enable AFCsd */
  3113. 0x19, 0x05,
  3114. 0x1A, 0x6C,
  3115. 0x1B, 0x24,
  3116. 0x1C, 0x28,
  3117. 0x1D, 0x8F,
  3118. 0x1E, 0x14,
  3119. 0x1F, 0x8F,
  3120. 0x20, 0x57,
  3121. 0x22, 0x21, /* New - ver 1.03 */
  3122. 0x23, 0x3C, /* New - ver 1.10 */
  3123. 0x24, 0x20, /* New - ver 1.03 */
  3124. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  3125. 0x2D, 0x87, /* FIFFQ=0 */
  3126. 0x2F, 0xF3,
  3127. 0x30, 0x0C, /* New - ver 1.11 */
  3128. 0x31, 0x1B, /* New - ver 1.11 */
  3129. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  3130. 0x28, 0xE1, /* Set the FIFCrst bit here */
  3131. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  3132. 0x00
  3133. };
  3134. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  3135. static const u8 MT2063B3_defaults[] = {
  3136. /* Reg, Value */
  3137. 0x05, 0xF0,
  3138. 0x19, 0x3D,
  3139. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  3140. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  3141. 0x28, 0xE1, /* Set the FIFCrst bit here */
  3142. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  3143. 0x00
  3144. };
  3145. static int mt2063_init(struct dvb_frontend *fe)
  3146. {
  3147. u32 status;
  3148. struct mt2063_state *state = fe->tuner_priv;
  3149. u8 all_resets = 0xF0; /* reset/load bits */
  3150. const u8 *def = NULL;
  3151. u32 FCRUN;
  3152. s32 maxReads;
  3153. u32 fcu_osc;
  3154. u32 i;
  3155. state->rcvr_mode = MT2063_CABLE_QAM;
  3156. /* Read the Part/Rev code from the tuner */
  3157. status = mt2063_read(state, MT2063_REG_PART_REV, state->reg, 1);
  3158. if (status < 0)
  3159. return status;
  3160. /* Check the part/rev code */
  3161. if (((state->reg[MT2063_REG_PART_REV] != MT2063_B0) /* MT2063 B0 */
  3162. &&(state->reg[MT2063_REG_PART_REV] != MT2063_B1) /* MT2063 B1 */
  3163. &&(state->reg[MT2063_REG_PART_REV] != MT2063_B3))) /* MT2063 B3 */
  3164. return -ENODEV; /* Wrong tuner Part/Rev code */
  3165. /* Check the 2nd byte of the Part/Rev code from the tuner */
  3166. status = mt2063_read(state, MT2063_REG_RSVD_3B,
  3167. &state->reg[MT2063_REG_RSVD_3B], 1);
  3168. /* b7 != 0 ==> NOT MT2063 */
  3169. if (status < 0 ||((state->reg[MT2063_REG_RSVD_3B] & 0x80) != 0x00))
  3170. return -ENODEV; /* Wrong tuner Part/Rev code */
  3171. /* Reset the tuner */
  3172. status = mt2063_write(state, MT2063_REG_LO2CQ_3, &all_resets, 1);
  3173. if (status < 0)
  3174. return status;
  3175. /* change all of the default values that vary from the HW reset values */
  3176. /* def = (state->reg[PART_REV] == MT2063_B0) ? MT2063B0_defaults : MT2063B1_defaults; */
  3177. switch (state->reg[MT2063_REG_PART_REV]) {
  3178. case MT2063_B3:
  3179. def = MT2063B3_defaults;
  3180. break;
  3181. case MT2063_B1:
  3182. def = MT2063B1_defaults;
  3183. break;
  3184. case MT2063_B0:
  3185. def = MT2063B0_defaults;
  3186. break;
  3187. default:
  3188. return -ENODEV;
  3189. break;
  3190. }
  3191. while (status >= 0 && *def) {
  3192. u8 reg = *def++;
  3193. u8 val = *def++;
  3194. status = mt2063_write(state, reg, &val, 1);
  3195. }
  3196. if (status < 0)
  3197. return status;
  3198. /* Wait for FIFF location to complete. */
  3199. FCRUN = 1;
  3200. maxReads = 10;
  3201. while (status >= 0 && (FCRUN != 0) && (maxReads-- > 0)) {
  3202. msleep(2);
  3203. status = mt2063_read(state,
  3204. MT2063_REG_XO_STATUS,
  3205. &state->
  3206. reg[MT2063_REG_XO_STATUS], 1);
  3207. FCRUN = (state->reg[MT2063_REG_XO_STATUS] & 0x40) >> 6;
  3208. }
  3209. if (FCRUN != 0 || status < 0)
  3210. return -ENODEV;
  3211. status = mt2063_read(state,
  3212. MT2063_REG_FIFFC,
  3213. &state->reg[MT2063_REG_FIFFC], 1);
  3214. if (status < 0)
  3215. return status;
  3216. /* Read back all the registers from the tuner */
  3217. status = mt2063_read(state,
  3218. MT2063_REG_PART_REV,
  3219. state->reg, MT2063_REG_END_REGS);
  3220. if (status < 0)
  3221. return status;
  3222. /* Initialize the tuner state. */
  3223. state->tuner_id = state->reg[MT2063_REG_PART_REV];
  3224. state->AS_Data.f_ref = MT2063_REF_FREQ;
  3225. state->AS_Data.f_if1_Center = (state->AS_Data.f_ref / 8) *
  3226. ((u32) state->reg[MT2063_REG_FIFFC] + 640);
  3227. state->AS_Data.f_if1_bw = MT2063_IF1_BW;
  3228. state->AS_Data.f_out = 43750000UL;
  3229. state->AS_Data.f_out_bw = 6750000UL;
  3230. state->AS_Data.f_zif_bw = MT2063_ZIF_BW;
  3231. state->AS_Data.f_LO1_Step = state->AS_Data.f_ref / 64;
  3232. state->AS_Data.f_LO2_Step = MT2063_TUNE_STEP_SIZE;
  3233. state->AS_Data.maxH1 = MT2063_MAX_HARMONICS_1;
  3234. state->AS_Data.maxH2 = MT2063_MAX_HARMONICS_2;
  3235. state->AS_Data.f_min_LO_Separation = MT2063_MIN_LO_SEP;
  3236. state->AS_Data.f_if1_Request = state->AS_Data.f_if1_Center;
  3237. state->AS_Data.f_LO1 = 2181000000UL;
  3238. state->AS_Data.f_LO2 = 1486249786UL;
  3239. state->f_IF1_actual = state->AS_Data.f_if1_Center;
  3240. state->AS_Data.f_in = state->AS_Data.f_LO1 - state->f_IF1_actual;
  3241. state->AS_Data.f_LO1_FracN_Avoid = MT2063_LO1_FRACN_AVOID;
  3242. state->AS_Data.f_LO2_FracN_Avoid = MT2063_LO2_FRACN_AVOID;
  3243. state->num_regs = MT2063_REG_END_REGS;
  3244. state->AS_Data.avoidDECT = MT2063_AVOID_BOTH;
  3245. state->ctfilt_sw = 0;
  3246. state->CTFiltMax[0] = 69230000;
  3247. state->CTFiltMax[1] = 105770000;
  3248. state->CTFiltMax[2] = 140350000;
  3249. state->CTFiltMax[3] = 177110000;
  3250. state->CTFiltMax[4] = 212860000;
  3251. state->CTFiltMax[5] = 241130000;
  3252. state->CTFiltMax[6] = 274370000;
  3253. state->CTFiltMax[7] = 309820000;
  3254. state->CTFiltMax[8] = 342450000;
  3255. state->CTFiltMax[9] = 378870000;
  3256. state->CTFiltMax[10] = 416210000;
  3257. state->CTFiltMax[11] = 456500000;
  3258. state->CTFiltMax[12] = 495790000;
  3259. state->CTFiltMax[13] = 534530000;
  3260. state->CTFiltMax[14] = 572610000;
  3261. state->CTFiltMax[15] = 598970000;
  3262. state->CTFiltMax[16] = 635910000;
  3263. state->CTFiltMax[17] = 672130000;
  3264. state->CTFiltMax[18] = 714840000;
  3265. state->CTFiltMax[19] = 739660000;
  3266. state->CTFiltMax[20] = 770410000;
  3267. state->CTFiltMax[21] = 814660000;
  3268. state->CTFiltMax[22] = 846950000;
  3269. state->CTFiltMax[23] = 867820000;
  3270. state->CTFiltMax[24] = 915980000;
  3271. state->CTFiltMax[25] = 947450000;
  3272. state->CTFiltMax[26] = 983110000;
  3273. state->CTFiltMax[27] = 1021630000;
  3274. state->CTFiltMax[28] = 1061870000;
  3275. state->CTFiltMax[29] = 1098330000;
  3276. state->CTFiltMax[30] = 1138990000;
  3277. /*
  3278. ** Fetch the FCU osc value and use it and the fRef value to
  3279. ** scale all of the Band Max values
  3280. */
  3281. state->reg[MT2063_REG_CTUNE_CTRL] = 0x0A;
  3282. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  3283. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  3284. if (status < 0)
  3285. return status;
  3286. /* Read the ClearTune filter calibration value */
  3287. status = mt2063_read(state, MT2063_REG_FIFFC,
  3288. &state->reg[MT2063_REG_FIFFC], 1);
  3289. if (status < 0)
  3290. return status;
  3291. fcu_osc = state->reg[MT2063_REG_FIFFC];
  3292. state->reg[MT2063_REG_CTUNE_CTRL] = 0x00;
  3293. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  3294. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  3295. if (status < 0)
  3296. return status;
  3297. /* Adjust each of the values in the ClearTune filter cross-over table */
  3298. for (i = 0; i < 31; i++)
  3299. state->CTFiltMax[i] =(state->CTFiltMax[i] / 768) * (fcu_osc + 640);
  3300. status = MT2063_SoftwareShutdown(state, 1);
  3301. if (status < 0)
  3302. return status;
  3303. status = MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
  3304. if (status < 0)
  3305. return status;
  3306. return 0;
  3307. }
  3308. static int mt2063_get_status(struct dvb_frontend *fe, u32 * status)
  3309. {
  3310. int rc = 0;
  3311. //get tuner lock status
  3312. return rc;
  3313. }
  3314. static int mt2063_get_state(struct dvb_frontend *fe,
  3315. enum tuner_param param, struct tuner_state *tunstate)
  3316. {
  3317. struct mt2063_state *state = fe->tuner_priv;
  3318. switch (param) {
  3319. case DVBFE_TUNER_FREQUENCY:
  3320. //get frequency
  3321. break;
  3322. case DVBFE_TUNER_TUNERSTEP:
  3323. break;
  3324. case DVBFE_TUNER_IFFREQ:
  3325. break;
  3326. case DVBFE_TUNER_BANDWIDTH:
  3327. //get bandwidth
  3328. break;
  3329. case DVBFE_TUNER_REFCLOCK:
  3330. tunstate->refclock = (u32) MT2063_GetLocked(state);
  3331. break;
  3332. default:
  3333. break;
  3334. }
  3335. return (int)tunstate->refclock;
  3336. }
  3337. static int mt2063_set_state(struct dvb_frontend *fe,
  3338. enum tuner_param param, struct tuner_state *tunstate)
  3339. {
  3340. struct mt2063_state *state = fe->tuner_priv;
  3341. u32 status = 0;
  3342. switch (param) {
  3343. case DVBFE_TUNER_FREQUENCY:
  3344. //set frequency
  3345. status =
  3346. MT_Tune_atv(state,
  3347. tunstate->frequency, tunstate->bandwidth,
  3348. state->tv_type);
  3349. state->frequency = tunstate->frequency;
  3350. break;
  3351. case DVBFE_TUNER_TUNERSTEP:
  3352. break;
  3353. case DVBFE_TUNER_IFFREQ:
  3354. break;
  3355. case DVBFE_TUNER_BANDWIDTH:
  3356. //set bandwidth
  3357. state->bandwidth = tunstate->bandwidth;
  3358. break;
  3359. case DVBFE_TUNER_REFCLOCK:
  3360. break;
  3361. case DVBFE_TUNER_SOFTWARE_SHUTDOWN:
  3362. status = MT2063_SoftwareShutdown(state, 1);
  3363. break;
  3364. case DVBFE_TUNER_CLEAR_POWER_MASKBITS:
  3365. status =
  3366. MT2063_ClearPowerMaskBits(state,
  3367. MT2063_ALL_SD);
  3368. break;
  3369. default:
  3370. break;
  3371. }
  3372. return (int)status;
  3373. }
  3374. static int mt2063_release(struct dvb_frontend *fe)
  3375. {
  3376. struct mt2063_state *state = fe->tuner_priv;
  3377. fe->tuner_priv = NULL;
  3378. kfree(state);
  3379. return 0;
  3380. }
  3381. static struct dvb_tuner_ops mt2063_ops = {
  3382. .info = {
  3383. .name = "MT2063 Silicon Tuner",
  3384. .frequency_min = 45000000,
  3385. .frequency_max = 850000000,
  3386. .frequency_step = 0,
  3387. },
  3388. .init = mt2063_init,
  3389. .sleep = MT2063_Sleep,
  3390. .get_status = mt2063_get_status,
  3391. .get_state = mt2063_get_state,
  3392. .set_state = mt2063_set_state,
  3393. .release = mt2063_release
  3394. };
  3395. struct dvb_frontend *mt2063_attach(struct dvb_frontend *fe,
  3396. struct mt2063_config *config,
  3397. struct i2c_adapter *i2c)
  3398. {
  3399. struct mt2063_state *state = NULL;
  3400. state = kzalloc(sizeof(struct mt2063_state), GFP_KERNEL);
  3401. if (state == NULL)
  3402. goto error;
  3403. state->config = config;
  3404. state->i2c = i2c;
  3405. state->frontend = fe;
  3406. state->reference = config->refclock / 1000; /* kHz */
  3407. fe->tuner_priv = state;
  3408. fe->ops.tuner_ops = mt2063_ops;
  3409. printk("%s: Attaching MT2063 \n", __func__);
  3410. return fe;
  3411. error:
  3412. kfree(state);
  3413. return NULL;
  3414. }
  3415. EXPORT_SYMBOL(mt2063_attach);
  3416. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  3417. MODULE_AUTHOR("Henry");
  3418. MODULE_DESCRIPTION("MT2063 Silicon tuner");
  3419. MODULE_LICENSE("GPL");