mt2063.c 170 KB

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