mt2063.c 104 KB

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