mt2063.c 116 KB

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