mt2063.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  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 selecting tuner mode
  96. */
  97. enum MT2063_RCVR_MODES {
  98. MT2063_CABLE_QAM = 0, /* Digital cable */
  99. MT2063_CABLE_ANALOG, /* Analog cable */
  100. MT2063_OFFAIR_COFDM, /* Digital offair */
  101. MT2063_OFFAIR_COFDM_SAWLESS, /* Digital offair without SAW */
  102. MT2063_OFFAIR_ANALOG, /* Analog offair */
  103. MT2063_OFFAIR_8VSB, /* Analog offair */
  104. MT2063_NUM_RCVR_MODES
  105. };
  106. /*
  107. * Possible values for MT2063_DNC_OUTPUT
  108. */
  109. enum MT2063_DNC_Output_Enable {
  110. MT2063_DNC_NONE = 0,
  111. MT2063_DNC_1,
  112. MT2063_DNC_2,
  113. MT2063_DNC_BOTH
  114. };
  115. /*
  116. ** Two-wire serial bus subaddresses of the tuner registers.
  117. ** Also known as the tuner's register addresses.
  118. */
  119. enum MT2063_Register_Offsets {
  120. MT2063_REG_PART_REV = 0, /* 0x00: Part/Rev Code */
  121. MT2063_REG_LO1CQ_1, /* 0x01: LO1C Queued Byte 1 */
  122. MT2063_REG_LO1CQ_2, /* 0x02: LO1C Queued Byte 2 */
  123. MT2063_REG_LO2CQ_1, /* 0x03: LO2C Queued Byte 1 */
  124. MT2063_REG_LO2CQ_2, /* 0x04: LO2C Queued Byte 2 */
  125. MT2063_REG_LO2CQ_3, /* 0x05: LO2C Queued Byte 3 */
  126. MT2063_REG_RSVD_06, /* 0x06: Reserved */
  127. MT2063_REG_LO_STATUS, /* 0x07: LO Status */
  128. MT2063_REG_FIFFC, /* 0x08: FIFF Center */
  129. MT2063_REG_CLEARTUNE, /* 0x09: ClearTune Filter */
  130. MT2063_REG_ADC_OUT, /* 0x0A: ADC_OUT */
  131. MT2063_REG_LO1C_1, /* 0x0B: LO1C Byte 1 */
  132. MT2063_REG_LO1C_2, /* 0x0C: LO1C Byte 2 */
  133. MT2063_REG_LO2C_1, /* 0x0D: LO2C Byte 1 */
  134. MT2063_REG_LO2C_2, /* 0x0E: LO2C Byte 2 */
  135. MT2063_REG_LO2C_3, /* 0x0F: LO2C Byte 3 */
  136. MT2063_REG_RSVD_10, /* 0x10: Reserved */
  137. MT2063_REG_PWR_1, /* 0x11: PWR Byte 1 */
  138. MT2063_REG_PWR_2, /* 0x12: PWR Byte 2 */
  139. MT2063_REG_TEMP_STATUS, /* 0x13: Temp Status */
  140. MT2063_REG_XO_STATUS, /* 0x14: Crystal Status */
  141. MT2063_REG_RF_STATUS, /* 0x15: RF Attn Status */
  142. MT2063_REG_FIF_STATUS, /* 0x16: FIF Attn Status */
  143. MT2063_REG_LNA_OV, /* 0x17: LNA Attn Override */
  144. MT2063_REG_RF_OV, /* 0x18: RF Attn Override */
  145. MT2063_REG_FIF_OV, /* 0x19: FIF Attn Override */
  146. MT2063_REG_LNA_TGT, /* 0x1A: Reserved */
  147. MT2063_REG_PD1_TGT, /* 0x1B: Pwr Det 1 Target */
  148. MT2063_REG_PD2_TGT, /* 0x1C: Pwr Det 2 Target */
  149. MT2063_REG_RSVD_1D, /* 0x1D: Reserved */
  150. MT2063_REG_RSVD_1E, /* 0x1E: Reserved */
  151. MT2063_REG_RSVD_1F, /* 0x1F: Reserved */
  152. MT2063_REG_RSVD_20, /* 0x20: Reserved */
  153. MT2063_REG_BYP_CTRL, /* 0x21: Bypass Control */
  154. MT2063_REG_RSVD_22, /* 0x22: Reserved */
  155. MT2063_REG_RSVD_23, /* 0x23: Reserved */
  156. MT2063_REG_RSVD_24, /* 0x24: Reserved */
  157. MT2063_REG_RSVD_25, /* 0x25: Reserved */
  158. MT2063_REG_RSVD_26, /* 0x26: Reserved */
  159. MT2063_REG_RSVD_27, /* 0x27: Reserved */
  160. MT2063_REG_FIFF_CTRL, /* 0x28: FIFF Control */
  161. MT2063_REG_FIFF_OFFSET, /* 0x29: FIFF Offset */
  162. MT2063_REG_CTUNE_CTRL, /* 0x2A: Reserved */
  163. MT2063_REG_CTUNE_OV, /* 0x2B: Reserved */
  164. MT2063_REG_CTRL_2C, /* 0x2C: Reserved */
  165. MT2063_REG_FIFF_CTRL2, /* 0x2D: Fiff Control */
  166. MT2063_REG_RSVD_2E, /* 0x2E: Reserved */
  167. MT2063_REG_DNC_GAIN, /* 0x2F: DNC Control */
  168. MT2063_REG_VGA_GAIN, /* 0x30: VGA Gain Ctrl */
  169. MT2063_REG_RSVD_31, /* 0x31: Reserved */
  170. MT2063_REG_TEMP_SEL, /* 0x32: Temperature Selection */
  171. MT2063_REG_RSVD_33, /* 0x33: Reserved */
  172. MT2063_REG_RSVD_34, /* 0x34: Reserved */
  173. MT2063_REG_RSVD_35, /* 0x35: Reserved */
  174. MT2063_REG_RSVD_36, /* 0x36: Reserved */
  175. MT2063_REG_RSVD_37, /* 0x37: Reserved */
  176. MT2063_REG_RSVD_38, /* 0x38: Reserved */
  177. MT2063_REG_RSVD_39, /* 0x39: Reserved */
  178. MT2063_REG_RSVD_3A, /* 0x3A: Reserved */
  179. MT2063_REG_RSVD_3B, /* 0x3B: Reserved */
  180. MT2063_REG_RSVD_3C, /* 0x3C: Reserved */
  181. MT2063_REG_END_REGS
  182. };
  183. enum MTTune_atv_standard {
  184. MTTUNEA_UNKNOWN = 0,
  185. MTTUNEA_PAL_B,
  186. MTTUNEA_PAL_G,
  187. MTTUNEA_PAL_I,
  188. MTTUNEA_PAL_L,
  189. MTTUNEA_PAL_MN,
  190. MTTUNEA_PAL_DK,
  191. MTTUNEA_DIGITAL,
  192. MTTUNEA_FMRADIO,
  193. MTTUNEA_DVBC,
  194. MTTUNEA_DVBT
  195. };
  196. struct mt2063_state {
  197. struct i2c_adapter *i2c;
  198. const struct mt2063_config *config;
  199. struct dvb_tuner_ops ops;
  200. struct dvb_frontend *frontend;
  201. struct tuner_state status;
  202. enum MTTune_atv_standard tv_type;
  203. u32 frequency;
  204. u32 srate;
  205. u32 bandwidth;
  206. u32 reference;
  207. u32 tuner_id;
  208. struct MT2063_AvoidSpursData_t AS_Data;
  209. u32 f_IF1_actual;
  210. u32 rcvr_mode;
  211. u32 ctfilt_sw;
  212. u32 CTFiltMax[31];
  213. u32 num_regs;
  214. u8 reg[MT2063_REG_END_REGS];
  215. };
  216. /*
  217. * mt2063_write - Write data into the I2C bus
  218. */
  219. static u32 mt2063_write(struct mt2063_state *state, u8 reg, u8 *data, u32 len)
  220. {
  221. struct dvb_frontend *fe = state->frontend;
  222. int ret;
  223. u8 buf[60];
  224. struct i2c_msg msg = {
  225. .addr = state->config->tuner_address,
  226. .flags = 0,
  227. .buf = buf,
  228. .len = len + 1
  229. };
  230. msg.buf[0] = reg;
  231. memcpy(msg.buf + 1, data, len);
  232. fe->ops.i2c_gate_ctrl(fe, 1);
  233. ret = i2c_transfer(state->i2c, &msg, 1);
  234. fe->ops.i2c_gate_ctrl(fe, 0);
  235. if (ret < 0)
  236. printk("mt2063_writeregs error ret=%d\n", ret);
  237. return ret;
  238. }
  239. /*
  240. * mt2063_write - Write register data into the I2C bus, caching the value
  241. */
  242. static u32 mt2063_setreg(struct mt2063_state *state, u8 reg, u8 val)
  243. {
  244. u32 status;
  245. if (reg >= MT2063_REG_END_REGS)
  246. return -ERANGE;
  247. status = mt2063_write(state, reg, &val, 1);
  248. if (status < 0)
  249. return status;
  250. state->reg[reg] = val;
  251. return 0;
  252. }
  253. /*
  254. * mt2063_read - Read data from the I2C bus
  255. */
  256. static u32 mt2063_read(struct mt2063_state *state,
  257. u8 subAddress, u8 *pData, u32 cnt)
  258. {
  259. u32 status = 0; /* Status to be returned */
  260. struct dvb_frontend *fe = state->frontend;
  261. u32 i = 0;
  262. fe->ops.i2c_gate_ctrl(fe, 1);
  263. for (i = 0; i < cnt; i++) {
  264. int ret;
  265. u8 b0[] = { subAddress + i };
  266. struct i2c_msg msg[] = {
  267. {
  268. .addr = state->config->tuner_address,
  269. .flags = I2C_M_RD,
  270. .buf = b0,
  271. .len = 1
  272. }, {
  273. .addr = state->config->tuner_address,
  274. .flags = I2C_M_RD,
  275. .buf = pData + 1,
  276. .len = 1
  277. }
  278. };
  279. ret = i2c_transfer(state->i2c, msg, 2);
  280. if (ret < 0)
  281. break;
  282. }
  283. fe->ops.i2c_gate_ctrl(fe, 0);
  284. return (status);
  285. }
  286. /*
  287. * FIXME: Is this really needed?
  288. */
  289. static int MT2063_Sleep(struct dvb_frontend *fe)
  290. {
  291. /*
  292. ** ToDo: Add code here to implement a OS blocking
  293. ** for a period of "nMinDelayTime" milliseconds.
  294. */
  295. msleep(10);
  296. return 0;
  297. }
  298. /*
  299. * Microtune spur avoidance
  300. */
  301. /* Implement ceiling, floor functions. */
  302. #define ceil(n, d) (((n) < 0) ? (-((-(n))/(d))) : (n)/(d) + ((n)%(d) != 0))
  303. #define floor(n, d) (((n) < 0) ? (-((-(n))/(d))) - ((n)%(d) != 0) : (n)/(d))
  304. struct MT2063_FIFZone_t {
  305. s32 min_;
  306. s32 max_;
  307. };
  308. static struct MT2063_ExclZone_t *InsertNode(struct MT2063_AvoidSpursData_t
  309. *pAS_Info,
  310. struct MT2063_ExclZone_t *pPrevNode)
  311. {
  312. struct MT2063_ExclZone_t *pNode;
  313. /* Check for a node in the free list */
  314. if (pAS_Info->freeZones != NULL) {
  315. /* Use one from the free list */
  316. pNode = pAS_Info->freeZones;
  317. pAS_Info->freeZones = pNode->next_;
  318. } else {
  319. /* Grab a node from the array */
  320. pNode = &pAS_Info->MT2063_ExclZones[pAS_Info->nZones];
  321. }
  322. if (pPrevNode != NULL) {
  323. pNode->next_ = pPrevNode->next_;
  324. pPrevNode->next_ = pNode;
  325. } else { /* insert at the beginning of the list */
  326. pNode->next_ = pAS_Info->usedZones;
  327. pAS_Info->usedZones = pNode;
  328. }
  329. pAS_Info->nZones++;
  330. return pNode;
  331. }
  332. static struct MT2063_ExclZone_t *RemoveNode(struct MT2063_AvoidSpursData_t
  333. *pAS_Info,
  334. struct MT2063_ExclZone_t *pPrevNode,
  335. struct MT2063_ExclZone_t
  336. *pNodeToRemove)
  337. {
  338. struct MT2063_ExclZone_t *pNext = pNodeToRemove->next_;
  339. /* Make previous node point to the subsequent node */
  340. if (pPrevNode != NULL)
  341. pPrevNode->next_ = pNext;
  342. /* Add pNodeToRemove to the beginning of the freeZones */
  343. pNodeToRemove->next_ = pAS_Info->freeZones;
  344. pAS_Info->freeZones = pNodeToRemove;
  345. /* Decrement node count */
  346. pAS_Info->nZones--;
  347. return pNext;
  348. }
  349. /*****************************************************************************
  350. **
  351. ** Name: MT_AddExclZone
  352. **
  353. ** Description: Add (and merge) an exclusion zone into the list.
  354. ** If the range (f_min, f_max) is totally outside the
  355. ** 1st IF BW, ignore the entry.
  356. ** If the range (f_min, f_max) is negative, ignore the entry.
  357. **
  358. ** Revision History:
  359. **
  360. ** SCR Date Author Description
  361. ** -------------------------------------------------------------------------
  362. ** 103 01-31-2005 DAD Ver 1.14: In MT_AddExclZone(), if the range
  363. ** (f_min, f_max) < 0, ignore the entry.
  364. **
  365. *****************************************************************************/
  366. static void MT2063_AddExclZone(struct MT2063_AvoidSpursData_t *pAS_Info,
  367. u32 f_min, u32 f_max)
  368. {
  369. struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
  370. struct MT2063_ExclZone_t *pPrev = NULL;
  371. struct MT2063_ExclZone_t *pNext = NULL;
  372. /* Check to see if this overlaps the 1st IF filter */
  373. if ((f_max > (pAS_Info->f_if1_Center - (pAS_Info->f_if1_bw / 2)))
  374. && (f_min < (pAS_Info->f_if1_Center + (pAS_Info->f_if1_bw / 2)))
  375. && (f_min < f_max)) {
  376. /*
  377. ** 1 2 3 4 5 6
  378. **
  379. ** New entry: |---| |--| |--| |-| |---| |--|
  380. ** or or or or or
  381. ** Existing: |--| |--| |--| |---| |-| |--|
  382. */
  383. /* Check for our place in the list */
  384. while ((pNode != NULL) && (pNode->max_ < f_min)) {
  385. pPrev = pNode;
  386. pNode = pNode->next_;
  387. }
  388. if ((pNode != NULL) && (pNode->min_ < f_max)) {
  389. /* Combine me with pNode */
  390. if (f_min < pNode->min_)
  391. pNode->min_ = f_min;
  392. if (f_max > pNode->max_)
  393. pNode->max_ = f_max;
  394. } else {
  395. pNode = InsertNode(pAS_Info, pPrev);
  396. pNode->min_ = f_min;
  397. pNode->max_ = f_max;
  398. }
  399. /* Look for merging possibilities */
  400. pNext = pNode->next_;
  401. while ((pNext != NULL) && (pNext->min_ < pNode->max_)) {
  402. if (pNext->max_ > pNode->max_)
  403. pNode->max_ = pNext->max_;
  404. pNext = RemoveNode(pAS_Info, pNode, pNext); /* Remove pNext, return ptr to pNext->next */
  405. }
  406. }
  407. }
  408. /*
  409. ** Reset all exclusion zones.
  410. ** Add zones to protect the PLL FracN regions near zero
  411. **
  412. ** N/A I 06-17-2008 RSK Ver 1.19: Refactoring avoidance of DECT
  413. ** frequencies into MT_ResetExclZones().
  414. */
  415. static void MT2063_ResetExclZones(struct MT2063_AvoidSpursData_t *pAS_Info)
  416. {
  417. u32 center;
  418. pAS_Info->nZones = 0; /* this clears the used list */
  419. pAS_Info->usedZones = NULL; /* reset ptr */
  420. pAS_Info->freeZones = NULL; /* reset ptr */
  421. center =
  422. pAS_Info->f_ref *
  423. ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 +
  424. pAS_Info->f_in) / pAS_Info->f_ref) - pAS_Info->f_in;
  425. while (center <
  426. pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
  427. pAS_Info->f_LO1_FracN_Avoid) {
  428. /* Exclude LO1 FracN */
  429. MT2063_AddExclZone(pAS_Info,
  430. center - pAS_Info->f_LO1_FracN_Avoid,
  431. center - 1);
  432. MT2063_AddExclZone(pAS_Info, center + 1,
  433. center + pAS_Info->f_LO1_FracN_Avoid);
  434. center += pAS_Info->f_ref;
  435. }
  436. center =
  437. pAS_Info->f_ref *
  438. ((pAS_Info->f_if1_Center - pAS_Info->f_if1_bw / 2 -
  439. pAS_Info->f_out) / pAS_Info->f_ref) + pAS_Info->f_out;
  440. while (center <
  441. pAS_Info->f_if1_Center + pAS_Info->f_if1_bw / 2 +
  442. pAS_Info->f_LO2_FracN_Avoid) {
  443. /* Exclude LO2 FracN */
  444. MT2063_AddExclZone(pAS_Info,
  445. center - pAS_Info->f_LO2_FracN_Avoid,
  446. center - 1);
  447. MT2063_AddExclZone(pAS_Info, center + 1,
  448. center + pAS_Info->f_LO2_FracN_Avoid);
  449. center += pAS_Info->f_ref;
  450. }
  451. if (MT2063_EXCLUDE_US_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
  452. /* Exclude LO1 values that conflict with DECT channels */
  453. MT2063_AddExclZone(pAS_Info, 1920836000 - pAS_Info->f_in, 1922236000 - pAS_Info->f_in); /* Ctr = 1921.536 */
  454. MT2063_AddExclZone(pAS_Info, 1922564000 - pAS_Info->f_in, 1923964000 - pAS_Info->f_in); /* Ctr = 1923.264 */
  455. MT2063_AddExclZone(pAS_Info, 1924292000 - pAS_Info->f_in, 1925692000 - pAS_Info->f_in); /* Ctr = 1924.992 */
  456. MT2063_AddExclZone(pAS_Info, 1926020000 - pAS_Info->f_in, 1927420000 - pAS_Info->f_in); /* Ctr = 1926.720 */
  457. MT2063_AddExclZone(pAS_Info, 1927748000 - pAS_Info->f_in, 1929148000 - pAS_Info->f_in); /* Ctr = 1928.448 */
  458. }
  459. if (MT2063_EXCLUDE_EURO_DECT_FREQUENCIES(pAS_Info->avoidDECT)) {
  460. MT2063_AddExclZone(pAS_Info, 1896644000 - pAS_Info->f_in, 1898044000 - pAS_Info->f_in); /* Ctr = 1897.344 */
  461. MT2063_AddExclZone(pAS_Info, 1894916000 - pAS_Info->f_in, 1896316000 - pAS_Info->f_in); /* Ctr = 1895.616 */
  462. MT2063_AddExclZone(pAS_Info, 1893188000 - pAS_Info->f_in, 1894588000 - pAS_Info->f_in); /* Ctr = 1893.888 */
  463. MT2063_AddExclZone(pAS_Info, 1891460000 - pAS_Info->f_in, 1892860000 - pAS_Info->f_in); /* Ctr = 1892.16 */
  464. MT2063_AddExclZone(pAS_Info, 1889732000 - pAS_Info->f_in, 1891132000 - pAS_Info->f_in); /* Ctr = 1890.432 */
  465. MT2063_AddExclZone(pAS_Info, 1888004000 - pAS_Info->f_in, 1889404000 - pAS_Info->f_in); /* Ctr = 1888.704 */
  466. MT2063_AddExclZone(pAS_Info, 1886276000 - pAS_Info->f_in, 1887676000 - pAS_Info->f_in); /* Ctr = 1886.976 */
  467. MT2063_AddExclZone(pAS_Info, 1884548000 - pAS_Info->f_in, 1885948000 - pAS_Info->f_in); /* Ctr = 1885.248 */
  468. MT2063_AddExclZone(pAS_Info, 1882820000 - pAS_Info->f_in, 1884220000 - pAS_Info->f_in); /* Ctr = 1883.52 */
  469. MT2063_AddExclZone(pAS_Info, 1881092000 - pAS_Info->f_in, 1882492000 - pAS_Info->f_in); /* Ctr = 1881.792 */
  470. }
  471. }
  472. /*****************************************************************************
  473. **
  474. ** Name: MT_ChooseFirstIF
  475. **
  476. ** Description: Choose the best available 1st IF
  477. ** If f_Desired is not excluded, choose that first.
  478. ** Otherwise, return the value closest to f_Center that is
  479. ** not excluded
  480. **
  481. ** Revision History:
  482. **
  483. ** SCR Date Author Description
  484. ** -------------------------------------------------------------------------
  485. ** 117 03-29-2007 RSK Ver 1.15: Re-wrote to match search order from
  486. ** tuner DLL.
  487. ** 147 07-27-2007 RSK Ver 1.17: Corrected calculation (-) to (+)
  488. ** Added logic to force f_Center within 1/2 f_Step.
  489. **
  490. *****************************************************************************/
  491. static u32 MT2063_ChooseFirstIF(struct MT2063_AvoidSpursData_t *pAS_Info)
  492. {
  493. /*
  494. ** Update "f_Desired" to be the nearest "combinational-multiple" of "f_LO1_Step".
  495. ** The resulting number, F_LO1 must be a multiple of f_LO1_Step. And F_LO1 is the arithmetic sum
  496. ** of f_in + f_Center. Neither f_in, nor f_Center must be a multiple of f_LO1_Step.
  497. ** However, the sum must be.
  498. */
  499. const u32 f_Desired =
  500. pAS_Info->f_LO1_Step *
  501. ((pAS_Info->f_if1_Request + pAS_Info->f_in +
  502. pAS_Info->f_LO1_Step / 2) / pAS_Info->f_LO1_Step) -
  503. pAS_Info->f_in;
  504. const u32 f_Step =
  505. (pAS_Info->f_LO1_Step >
  506. pAS_Info->f_LO2_Step) ? pAS_Info->f_LO1_Step : pAS_Info->
  507. f_LO2_Step;
  508. u32 f_Center;
  509. s32 i;
  510. s32 j = 0;
  511. u32 bDesiredExcluded = 0;
  512. u32 bZeroExcluded = 0;
  513. s32 tmpMin, tmpMax;
  514. s32 bestDiff;
  515. struct MT2063_ExclZone_t *pNode = pAS_Info->usedZones;
  516. struct MT2063_FIFZone_t zones[MT2063_MAX_ZONES];
  517. if (pAS_Info->nZones == 0)
  518. return f_Desired;
  519. /* f_Center needs to be an integer multiple of f_Step away from f_Desired */
  520. if (pAS_Info->f_if1_Center > f_Desired)
  521. f_Center =
  522. f_Desired +
  523. f_Step *
  524. ((pAS_Info->f_if1_Center - f_Desired +
  525. f_Step / 2) / f_Step);
  526. else
  527. f_Center =
  528. f_Desired -
  529. f_Step *
  530. ((f_Desired - pAS_Info->f_if1_Center +
  531. f_Step / 2) / f_Step);
  532. //assert;
  533. //if (!abs((s32) f_Center - (s32) pAS_Info->f_if1_Center) <= (s32) (f_Step/2))
  534. // return 0;
  535. /* Take MT_ExclZones, center around f_Center and change the resolution to f_Step */
  536. while (pNode != NULL) {
  537. /* floor function */
  538. tmpMin =
  539. floor((s32) (pNode->min_ - f_Center), (s32) f_Step);
  540. /* ceil function */
  541. tmpMax =
  542. ceil((s32) (pNode->max_ - f_Center), (s32) f_Step);
  543. if ((pNode->min_ < f_Desired) && (pNode->max_ > f_Desired))
  544. bDesiredExcluded = 1;
  545. if ((tmpMin < 0) && (tmpMax > 0))
  546. bZeroExcluded = 1;
  547. /* See if this zone overlaps the previous */
  548. if ((j > 0) && (tmpMin < zones[j - 1].max_))
  549. zones[j - 1].max_ = tmpMax;
  550. else {
  551. /* Add new zone */
  552. //assert(j<MT2063_MAX_ZONES);
  553. //if (j>=MT2063_MAX_ZONES)
  554. //break;
  555. zones[j].min_ = tmpMin;
  556. zones[j].max_ = tmpMax;
  557. j++;
  558. }
  559. pNode = pNode->next_;
  560. }
  561. /*
  562. ** If the desired is okay, return with it
  563. */
  564. if (bDesiredExcluded == 0)
  565. return f_Desired;
  566. /*
  567. ** If the desired is excluded and the center is okay, return with it
  568. */
  569. if (bZeroExcluded == 0)
  570. return f_Center;
  571. /* Find the value closest to 0 (f_Center) */
  572. bestDiff = zones[0].min_;
  573. for (i = 0; i < j; i++) {
  574. if (abs(zones[i].min_) < abs(bestDiff))
  575. bestDiff = zones[i].min_;
  576. if (abs(zones[i].max_) < abs(bestDiff))
  577. bestDiff = zones[i].max_;
  578. }
  579. if (bestDiff < 0)
  580. return f_Center - ((u32) (-bestDiff) * f_Step);
  581. return f_Center + (bestDiff * f_Step);
  582. }
  583. /****************************************************************************
  584. **
  585. ** Name: gcd
  586. **
  587. ** Description: Uses Euclid's algorithm
  588. **
  589. ** Parameters: u, v - unsigned values whose GCD is desired.
  590. **
  591. ** Global: None
  592. **
  593. ** Returns: greatest common divisor of u and v, if either value
  594. ** is 0, the other value is returned as the result.
  595. **
  596. ** Dependencies: None.
  597. **
  598. ** Revision History:
  599. **
  600. ** SCR Date Author Description
  601. ** -------------------------------------------------------------------------
  602. ** N/A 06-01-2004 JWS Original
  603. ** N/A 08-03-2004 DAD Changed to Euclid's since it can handle
  604. ** unsigned numbers.
  605. **
  606. ****************************************************************************/
  607. static u32 MT2063_gcd(u32 u, u32 v)
  608. {
  609. u32 r;
  610. while (v != 0) {
  611. r = u % v;
  612. u = v;
  613. v = r;
  614. }
  615. return u;
  616. }
  617. /****************************************************************************
  618. **
  619. ** Name: IsSpurInBand
  620. **
  621. ** Description: Checks to see if a spur will be present within the IF's
  622. ** bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW)
  623. **
  624. ** ma mb mc md
  625. ** <--+-+-+-------------------+-------------------+-+-+-->
  626. ** | ^ 0 ^ |
  627. ** ^ b=-fIFOut+fIFBW/2 -b=+fIFOut-fIFBW/2 ^
  628. ** a=-fIFOut-fIFBW/2 -a=+fIFOut+fIFBW/2
  629. **
  630. ** Note that some equations are doubled to prevent round-off
  631. ** problems when calculating fIFBW/2
  632. **
  633. ** Parameters: pAS_Info - Avoid Spurs information block
  634. ** fm - If spur, amount f_IF1 has to move negative
  635. ** fp - If spur, amount f_IF1 has to move positive
  636. **
  637. ** Global: None
  638. **
  639. ** Returns: 1 if an LO spur would be present, otherwise 0.
  640. **
  641. ** Dependencies: None.
  642. **
  643. ** Revision History:
  644. **
  645. ** SCR Date Author Description
  646. ** -------------------------------------------------------------------------
  647. ** N/A 11-28-2002 DAD Implemented algorithm from applied patent
  648. **
  649. ****************************************************************************/
  650. static u32 IsSpurInBand(struct MT2063_AvoidSpursData_t *pAS_Info,
  651. u32 *fm, u32 * fp)
  652. {
  653. /*
  654. ** Calculate LO frequency settings.
  655. */
  656. u32 n, n0;
  657. const u32 f_LO1 = pAS_Info->f_LO1;
  658. const u32 f_LO2 = pAS_Info->f_LO2;
  659. const u32 d = pAS_Info->f_out + pAS_Info->f_out_bw / 2;
  660. const u32 c = d - pAS_Info->f_out_bw;
  661. const u32 f = pAS_Info->f_zif_bw / 2;
  662. const u32 f_Scale = (f_LO1 / (UINT_MAX / 2 / pAS_Info->maxH1)) + 1;
  663. s32 f_nsLO1, f_nsLO2;
  664. s32 f_Spur;
  665. u32 ma, mb, mc, md, me, mf;
  666. u32 lo_gcd, gd_Scale, gc_Scale, gf_Scale, hgds, hgfs, hgcs;
  667. *fm = 0;
  668. /*
  669. ** For each edge (d, c & f), calculate a scale, based on the gcd
  670. ** of f_LO1, f_LO2 and the edge value. Use the larger of this
  671. ** gcd-based scale factor or f_Scale.
  672. */
  673. lo_gcd = MT2063_gcd(f_LO1, f_LO2);
  674. gd_Scale = max((u32) MT2063_gcd(lo_gcd, d), f_Scale);
  675. hgds = gd_Scale / 2;
  676. gc_Scale = max((u32) MT2063_gcd(lo_gcd, c), f_Scale);
  677. hgcs = gc_Scale / 2;
  678. gf_Scale = max((u32) MT2063_gcd(lo_gcd, f), f_Scale);
  679. hgfs = gf_Scale / 2;
  680. n0 = DIV_ROUND_UP(f_LO2 - d, f_LO1 - f_LO2);
  681. /* Check out all multiples of LO1 from n0 to m_maxLOSpurHarmonic */
  682. for (n = n0; n <= pAS_Info->maxH1; ++n) {
  683. md = (n * ((f_LO1 + hgds) / gd_Scale) -
  684. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  685. /* If # fLO2 harmonics > m_maxLOSpurHarmonic, then no spurs present */
  686. if (md >= pAS_Info->maxH1)
  687. break;
  688. ma = (n * ((f_LO1 + hgds) / gd_Scale) +
  689. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  690. /* If no spurs between +/- (f_out + f_IFBW/2), then try next harmonic */
  691. if (md == ma)
  692. continue;
  693. mc = (n * ((f_LO1 + hgcs) / gc_Scale) -
  694. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  695. if (mc != md) {
  696. f_nsLO1 = (s32) (n * (f_LO1 / gc_Scale));
  697. f_nsLO2 = (s32) (mc * (f_LO2 / gc_Scale));
  698. f_Spur =
  699. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  700. n * (f_LO1 % gc_Scale) - mc * (f_LO2 % gc_Scale);
  701. *fp = ((f_Spur - (s32) c) / (mc - n)) + 1;
  702. *fm = (((s32) d - f_Spur) / (mc - n)) + 1;
  703. return 1;
  704. }
  705. /* Location of Zero-IF-spur to be checked */
  706. me = (n * ((f_LO1 + hgfs) / gf_Scale) +
  707. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  708. mf = (n * ((f_LO1 + hgfs) / gf_Scale) -
  709. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  710. if (me != mf) {
  711. f_nsLO1 = n * (f_LO1 / gf_Scale);
  712. f_nsLO2 = me * (f_LO2 / gf_Scale);
  713. f_Spur =
  714. (gf_Scale * (f_nsLO1 - f_nsLO2)) +
  715. n * (f_LO1 % gf_Scale) - me * (f_LO2 % gf_Scale);
  716. *fp = ((f_Spur + (s32) f) / (me - n)) + 1;
  717. *fm = (((s32) f - f_Spur) / (me - n)) + 1;
  718. return 1;
  719. }
  720. mb = (n * ((f_LO1 + hgcs) / gc_Scale) +
  721. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  722. if (ma != mb) {
  723. f_nsLO1 = n * (f_LO1 / gc_Scale);
  724. f_nsLO2 = ma * (f_LO2 / gc_Scale);
  725. f_Spur =
  726. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  727. n * (f_LO1 % gc_Scale) - ma * (f_LO2 % gc_Scale);
  728. *fp = (((s32) d + f_Spur) / (ma - n)) + 1;
  729. *fm = (-(f_Spur + (s32) c) / (ma - n)) + 1;
  730. return 1;
  731. }
  732. }
  733. /* No spurs found */
  734. return 0;
  735. }
  736. /*****************************************************************************
  737. **
  738. ** Name: MT_AvoidSpurs
  739. **
  740. ** Description: Main entry point to avoid spurs.
  741. ** Checks for existing spurs in present LO1, LO2 freqs
  742. ** and if present, chooses spur-free LO1, LO2 combination
  743. ** that tunes the same input/output frequencies.
  744. **
  745. ** Revision History:
  746. **
  747. ** SCR Date Author Description
  748. ** -------------------------------------------------------------------------
  749. ** 096 04-06-2005 DAD Ver 1.11: Fix divide by 0 error if maxH==0.
  750. **
  751. *****************************************************************************/
  752. static u32 MT2063_AvoidSpurs(struct MT2063_AvoidSpursData_t *pAS_Info)
  753. {
  754. u32 status = 0;
  755. u32 fm, fp; /* restricted range on LO's */
  756. pAS_Info->bSpurAvoided = 0;
  757. pAS_Info->nSpursFound = 0;
  758. if (pAS_Info->maxH1 == 0)
  759. return 0;
  760. /*
  761. ** Avoid LO Generated Spurs
  762. **
  763. ** Make sure that have no LO-related spurs within the IF output
  764. ** bandwidth.
  765. **
  766. ** If there is an LO spur in this band, start at the current IF1 frequency
  767. ** and work out until we find a spur-free frequency or run up against the
  768. ** 1st IF SAW band edge. Use temporary copies of fLO1 and fLO2 so that they
  769. ** will be unchanged if a spur-free setting is not found.
  770. */
  771. pAS_Info->bSpurPresent = IsSpurInBand(pAS_Info, &fm, &fp);
  772. if (pAS_Info->bSpurPresent) {
  773. u32 zfIF1 = pAS_Info->f_LO1 - pAS_Info->f_in; /* current attempt at a 1st IF */
  774. u32 zfLO1 = pAS_Info->f_LO1; /* current attempt at an LO1 freq */
  775. u32 zfLO2 = pAS_Info->f_LO2; /* current attempt at an LO2 freq */
  776. u32 delta_IF1;
  777. u32 new_IF1;
  778. /*
  779. ** Spur was found, attempt to find a spur-free 1st IF
  780. */
  781. do {
  782. pAS_Info->nSpursFound++;
  783. /* Raise f_IF1_upper, if needed */
  784. MT2063_AddExclZone(pAS_Info, zfIF1 - fm, zfIF1 + fp);
  785. /* Choose next IF1 that is closest to f_IF1_CENTER */
  786. new_IF1 = MT2063_ChooseFirstIF(pAS_Info);
  787. if (new_IF1 > zfIF1) {
  788. pAS_Info->f_LO1 += (new_IF1 - zfIF1);
  789. pAS_Info->f_LO2 += (new_IF1 - zfIF1);
  790. } else {
  791. pAS_Info->f_LO1 -= (zfIF1 - new_IF1);
  792. pAS_Info->f_LO2 -= (zfIF1 - new_IF1);
  793. }
  794. zfIF1 = new_IF1;
  795. if (zfIF1 > pAS_Info->f_if1_Center)
  796. delta_IF1 = zfIF1 - pAS_Info->f_if1_Center;
  797. else
  798. delta_IF1 = pAS_Info->f_if1_Center - zfIF1;
  799. }
  800. /*
  801. ** Continue while the new 1st IF is still within the 1st IF bandwidth
  802. ** and there is a spur in the band (again)
  803. */
  804. while ((2 * delta_IF1 + pAS_Info->f_out_bw <=
  805. pAS_Info->f_if1_bw)
  806. && (pAS_Info->bSpurPresent =
  807. IsSpurInBand(pAS_Info, &fm, &fp)));
  808. /*
  809. ** Use the LO-spur free values found. If the search went all the way to
  810. ** the 1st IF band edge and always found spurs, just leave the original
  811. ** choice. It's as "good" as any other.
  812. */
  813. if (pAS_Info->bSpurPresent == 1) {
  814. status |= MT2063_SPUR_PRESENT_ERR;
  815. pAS_Info->f_LO1 = zfLO1;
  816. pAS_Info->f_LO2 = zfLO2;
  817. } else
  818. pAS_Info->bSpurAvoided = 1;
  819. }
  820. status |=
  821. ((pAS_Info->
  822. nSpursFound << MT2063_SPUR_SHIFT) & MT2063_SPUR_CNT_MASK);
  823. return (status);
  824. }
  825. /*
  826. ** The expected version of MT_AvoidSpursData_t
  827. ** If the version is different, an updated file is needed from Microtune
  828. */
  829. typedef enum {
  830. MT2063_SET_ATTEN,
  831. MT2063_INCR_ATTEN,
  832. MT2063_DECR_ATTEN
  833. } MT2063_ATTEN_CNTL_MODE;
  834. /*
  835. * Constants used by the tuning algorithm
  836. */
  837. #define MT2063_REF_FREQ (16000000UL) /* Reference oscillator Frequency (in Hz) */
  838. #define MT2063_IF1_BW (22000000UL) /* The IF1 filter bandwidth (in Hz) */
  839. #define MT2063_TUNE_STEP_SIZE (50000UL) /* Tune in steps of 50 kHz */
  840. #define MT2063_SPUR_STEP_HZ (250000UL) /* Step size (in Hz) to move IF1 when avoiding spurs */
  841. #define MT2063_ZIF_BW (2000000UL) /* Zero-IF spur-free bandwidth (in Hz) */
  842. #define MT2063_MAX_HARMONICS_1 (15UL) /* Highest intra-tuner LO Spur Harmonic to be avoided */
  843. #define MT2063_MAX_HARMONICS_2 (5UL) /* Highest inter-tuner LO Spur Harmonic to be avoided */
  844. #define MT2063_MIN_LO_SEP (1000000UL) /* Minimum inter-tuner LO frequency separation */
  845. #define MT2063_LO1_FRACN_AVOID (0UL) /* LO1 FracN numerator avoid region (in Hz) */
  846. #define MT2063_LO2_FRACN_AVOID (199999UL) /* LO2 FracN numerator avoid region (in Hz) */
  847. #define MT2063_MIN_FIN_FREQ (44000000UL) /* Minimum input frequency (in Hz) */
  848. #define MT2063_MAX_FIN_FREQ (1100000000UL) /* Maximum input frequency (in Hz) */
  849. #define MT2063_MIN_FOUT_FREQ (36000000UL) /* Minimum output frequency (in Hz) */
  850. #define MT2063_MAX_FOUT_FREQ (57000000UL) /* Maximum output frequency (in Hz) */
  851. #define MT2063_MIN_DNC_FREQ (1293000000UL) /* Minimum LO2 frequency (in Hz) */
  852. #define MT2063_MAX_DNC_FREQ (1614000000UL) /* Maximum LO2 frequency (in Hz) */
  853. #define MT2063_MIN_UPC_FREQ (1396000000UL) /* Minimum LO1 frequency (in Hz) */
  854. #define MT2063_MAX_UPC_FREQ (2750000000UL) /* Maximum LO1 frequency (in Hz) */
  855. /*
  856. ** Define the supported Part/Rev codes for the MT2063
  857. */
  858. #define MT2063_B0 (0x9B)
  859. #define MT2063_B1 (0x9C)
  860. #define MT2063_B2 (0x9D)
  861. #define MT2063_B3 (0x9E)
  862. /*
  863. ** Constants for setting receiver modes.
  864. ** (6 modes defined at this time, enumerated by MT2063_RCVR_MODES)
  865. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  866. ** DNC Output is selected, the other is always off)
  867. **
  868. ** If PAL-L or L' is received, set:
  869. ** MT2063_SetParam(hMT2063,MT2063_TAGC,1);
  870. **
  871. ** --------------+----------------------------------------------
  872. ** Mode 0 : | MT2063_CABLE_QAM
  873. ** Mode 1 : | MT2063_CABLE_ANALOG
  874. ** Mode 2 : | MT2063_OFFAIR_COFDM
  875. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  876. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  877. ** Mode 5 : | MT2063_OFFAIR_8VSB
  878. ** --------------+----+----+----+----+-----+-----+--------------
  879. ** Mode | 0 | 1 | 2 | 3 | 4 | 5 |
  880. ** --------------+----+----+----+----+-----+-----+
  881. **
  882. **
  883. */
  884. static const u8 RFAGCEN[] = { 0, 0, 0, 0, 0, 0 };
  885. static const u8 LNARIN[] = { 0, 0, 3, 3, 3, 3 };
  886. static const u8 FIFFQEN[] = { 1, 1, 1, 1, 1, 1 };
  887. static const u8 FIFFQ[] = { 0, 0, 0, 0, 0, 0 };
  888. static const u8 DNC1GC[] = { 0, 0, 0, 0, 0, 0 };
  889. static const u8 DNC2GC[] = { 0, 0, 0, 0, 0, 0 };
  890. static const u8 ACLNAMAX[] = { 31, 31, 31, 31, 31, 31 };
  891. static const u8 LNATGT[] = { 44, 43, 43, 43, 43, 43 };
  892. static const u8 RFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  893. static const u8 ACRFMAX[] = { 31, 31, 31, 31, 31, 31 };
  894. static const u8 PD1TGT[] = { 36, 36, 38, 38, 36, 38 };
  895. static const u8 FIFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  896. static const u8 ACFIFMAX[] = { 29, 29, 29, 29, 29, 29 };
  897. static const u8 PD2TGT[] = { 40, 33, 38, 42, 30, 38 };
  898. /**
  899. * mt2063_lockStatus - Checks to see if LO1 and LO2 are locked
  900. *
  901. * @state: struct mt2063_state pointer
  902. *
  903. * This function returns 0, if no lock, 1 if locked and a value < 1 if error
  904. */
  905. unsigned int mt2063_lockStatus(struct mt2063_state *state)
  906. {
  907. const u32 nMaxWait = 100; /* wait a maximum of 100 msec */
  908. const u32 nPollRate = 2; /* poll status bits every 2 ms */
  909. const u32 nMaxLoops = nMaxWait / nPollRate;
  910. const u8 LO1LK = 0x80;
  911. u8 LO2LK = 0x08;
  912. u32 status;
  913. u32 nDelays = 0;
  914. /* LO2 Lock bit was in a different place for B0 version */
  915. if (state->tuner_id == MT2063_B0)
  916. LO2LK = 0x40;
  917. do {
  918. status = mt2063_read(state, MT2063_REG_LO_STATUS,
  919. &state->reg[MT2063_REG_LO_STATUS], 1);
  920. if (status < 0)
  921. return status;
  922. if ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) ==
  923. (LO1LK | LO2LK)) {
  924. return TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO;
  925. }
  926. msleep(nPollRate); /* Wait between retries */
  927. }
  928. while (++nDelays < nMaxLoops);
  929. /*
  930. * Got no lock or partial lock
  931. */
  932. return 0;
  933. }
  934. EXPORT_SYMBOL_GPL(mt2063_lockStatus);
  935. /*
  936. * mt2063_set_dnc_output_enable()
  937. */
  938. static u32 mt2063_get_dnc_output_enable(struct mt2063_state *state,
  939. enum MT2063_DNC_Output_Enable *pValue)
  940. {
  941. if ((state->reg[MT2063_REG_DNC_GAIN] & 0x03) == 0x03) { /* if DNC1 is off */
  942. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  943. *pValue = MT2063_DNC_NONE;
  944. else
  945. *pValue = MT2063_DNC_2;
  946. } else { /* DNC1 is on */
  947. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  948. *pValue = MT2063_DNC_1;
  949. else
  950. *pValue = MT2063_DNC_BOTH;
  951. }
  952. return 0;
  953. }
  954. /*
  955. * mt2063_set_dnc_output_enable()
  956. */
  957. static u32 mt2063_set_dnc_output_enable(struct mt2063_state *state,
  958. enum MT2063_DNC_Output_Enable nValue)
  959. {
  960. u32 status = 0; /* Status to be returned */
  961. u8 val = 0;
  962. /* selects, which DNC output is used */
  963. switch (nValue) {
  964. case MT2063_DNC_NONE:
  965. {
  966. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  967. if (state->reg[MT2063_REG_DNC_GAIN] !=
  968. val)
  969. status |=
  970. mt2063_setreg(state,
  971. MT2063_REG_DNC_GAIN,
  972. val);
  973. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  974. if (state->reg[MT2063_REG_VGA_GAIN] !=
  975. val)
  976. status |=
  977. mt2063_setreg(state,
  978. MT2063_REG_VGA_GAIN,
  979. val);
  980. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  981. if (state->reg[MT2063_REG_RSVD_20] !=
  982. val)
  983. status |=
  984. mt2063_setreg(state,
  985. MT2063_REG_RSVD_20,
  986. val);
  987. break;
  988. }
  989. case MT2063_DNC_1:
  990. {
  991. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  992. if (state->reg[MT2063_REG_DNC_GAIN] !=
  993. val)
  994. status |=
  995. mt2063_setreg(state,
  996. MT2063_REG_DNC_GAIN,
  997. val);
  998. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  999. if (state->reg[MT2063_REG_VGA_GAIN] !=
  1000. val)
  1001. status |=
  1002. mt2063_setreg(state,
  1003. MT2063_REG_VGA_GAIN,
  1004. val);
  1005. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  1006. if (state->reg[MT2063_REG_RSVD_20] !=
  1007. val)
  1008. status |=
  1009. mt2063_setreg(state,
  1010. MT2063_REG_RSVD_20,
  1011. val);
  1012. break;
  1013. }
  1014. case MT2063_DNC_2:
  1015. {
  1016. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  1017. if (state->reg[MT2063_REG_DNC_GAIN] !=
  1018. val)
  1019. status |=
  1020. mt2063_setreg(state,
  1021. MT2063_REG_DNC_GAIN,
  1022. val);
  1023. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  1024. if (state->reg[MT2063_REG_VGA_GAIN] !=
  1025. val)
  1026. status |=
  1027. mt2063_setreg(state,
  1028. MT2063_REG_VGA_GAIN,
  1029. val);
  1030. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  1031. if (state->reg[MT2063_REG_RSVD_20] !=
  1032. val)
  1033. status |=
  1034. mt2063_setreg(state,
  1035. MT2063_REG_RSVD_20,
  1036. val);
  1037. break;
  1038. }
  1039. case MT2063_DNC_BOTH:
  1040. {
  1041. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  1042. if (state->reg[MT2063_REG_DNC_GAIN] !=
  1043. val)
  1044. status |=
  1045. mt2063_setreg(state,
  1046. MT2063_REG_DNC_GAIN,
  1047. val);
  1048. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  1049. if (state->reg[MT2063_REG_VGA_GAIN] !=
  1050. val)
  1051. status |=
  1052. mt2063_setreg(state,
  1053. MT2063_REG_VGA_GAIN,
  1054. val);
  1055. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  1056. if (state->reg[MT2063_REG_RSVD_20] !=
  1057. val)
  1058. status |=
  1059. mt2063_setreg(state,
  1060. MT2063_REG_RSVD_20,
  1061. val);
  1062. break;
  1063. }
  1064. default:
  1065. break;
  1066. }
  1067. return (status);
  1068. }
  1069. /******************************************************************************
  1070. **
  1071. ** Name: MT2063_SetReceiverMode
  1072. **
  1073. ** Description: Set the MT2063 receiver mode
  1074. **
  1075. ** --------------+----------------------------------------------
  1076. ** Mode 0 : | MT2063_CABLE_QAM
  1077. ** Mode 1 : | MT2063_CABLE_ANALOG
  1078. ** Mode 2 : | MT2063_OFFAIR_COFDM
  1079. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  1080. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  1081. ** Mode 5 : | MT2063_OFFAIR_8VSB
  1082. ** --------------+----+----+----+----+-----+--------------------
  1083. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  1084. ** DNC Output is selected, the other is always off)
  1085. **
  1086. ** |<---------- Mode -------------->|
  1087. ** Reg Field | 0 | 1 | 2 | 3 | 4 | 5 |
  1088. ** ------------+-----+-----+-----+-----+-----+-----+
  1089. ** RFAGCen | OFF | OFF | OFF | OFF | OFF | OFF
  1090. ** LNARin | 0 | 0 | 3 | 3 | 3 | 3
  1091. ** FIFFQen | 1 | 1 | 1 | 1 | 1 | 1
  1092. ** FIFFq | 0 | 0 | 0 | 0 | 0 | 0
  1093. ** DNC1gc | 0 | 0 | 0 | 0 | 0 | 0
  1094. ** DNC2gc | 0 | 0 | 0 | 0 | 0 | 0
  1095. ** GCU Auto | 1 | 1 | 1 | 1 | 1 | 1
  1096. ** LNA max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1097. ** LNA Target | 44 | 43 | 43 | 43 | 43 | 43
  1098. ** ign RF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1099. ** RF max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1100. ** PD1 Target | 36 | 36 | 38 | 38 | 36 | 38
  1101. ** ign FIF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1102. ** FIF max Atn | 5 | 5 | 5 | 5 | 5 | 5
  1103. ** PD2 Target | 40 | 33 | 42 | 42 | 33 | 42
  1104. **
  1105. **
  1106. ** Parameters: state - ptr to mt2063_state structure
  1107. ** Mode - desired reciever mode
  1108. **
  1109. ** Usage: status = MT2063_SetReceiverMode(hMT2063, Mode);
  1110. **
  1111. ** Returns: status:
  1112. ** MT_OK - No errors
  1113. ** MT_COMM_ERR - Serial bus communications error
  1114. **
  1115. ** Dependencies: mt2063_setreg - Write a byte of data to a HW register.
  1116. ** Assumes that the tuner cache is valid.
  1117. **
  1118. ** Revision History:
  1119. **
  1120. ** SCR Date Author Description
  1121. ** -------------------------------------------------------------------------
  1122. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1123. ** N/A 01-10-2007 PINZ Added additional GCU Settings, FIFF Calib will be triggered
  1124. ** 155 10-01-2007 DAD Ver 1.06: Add receiver mode for SECAM positive
  1125. ** modulation
  1126. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1127. ** N/A 10-22-2007 PINZ Ver 1.07: Changed some Registers at init to have
  1128. ** the same settings as with MT Launcher
  1129. ** N/A 10-30-2007 PINZ Add SetParam VGAGC & VGAOI
  1130. ** Add SetParam DNC_OUTPUT_ENABLE
  1131. ** Removed VGAGC from receiver mode,
  1132. ** default now 1
  1133. ** N/A 10-31-2007 PINZ Ver 1.08: Add SetParam TAGC, removed from rcvr-mode
  1134. ** Add SetParam AMPGC, removed from rcvr-mode
  1135. ** Corrected names of GCU values
  1136. ** reorganized receiver modes, removed,
  1137. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1138. ** Actualized Receiver-Mode values
  1139. ** N/A 11-12-2007 PINZ Ver 1.09: Actualized Receiver-Mode values
  1140. ** N/A 11-27-2007 PINZ Improved buffered writing
  1141. ** 01-03-2008 PINZ Ver 1.10: Added a trigger of BYPATNUP for
  1142. ** correct wakeup of the LNA after shutdown
  1143. ** Set AFCsd = 1 as default
  1144. ** Changed CAP1sel default
  1145. ** 01-14-2008 PINZ Ver 1.11: Updated gain settings
  1146. ** 04-18-2008 PINZ Ver 1.15: Add SetParam LNARIN & PDxTGT
  1147. ** Split SetParam up to ACLNA / ACLNA_MAX
  1148. ** removed ACLNA_INRC/DECR (+RF & FIF)
  1149. ** removed GCUAUTO / BYPATNDN/UP
  1150. **
  1151. ******************************************************************************/
  1152. static u32 MT2063_SetReceiverMode(struct mt2063_state *state,
  1153. enum MT2063_RCVR_MODES Mode)
  1154. {
  1155. u32 status = 0; /* Status to be returned */
  1156. u8 val;
  1157. u32 longval;
  1158. if (Mode >= MT2063_NUM_RCVR_MODES)
  1159. status = -ERANGE;
  1160. /* RFAGCen */
  1161. if (status >= 0) {
  1162. val =
  1163. (state->
  1164. reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x40) | (RFAGCEN[Mode]
  1165. ? 0x40 :
  1166. 0x00);
  1167. if (state->reg[MT2063_REG_PD1_TGT] != val) {
  1168. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1169. }
  1170. }
  1171. /* LNARin */
  1172. if (status >= 0) {
  1173. u8 val = (state-> reg[MT2063_REG_CTRL_2C] & (u8) ~ 0x03) |
  1174. (LNARIN[Mode] & 0x03);
  1175. if (state->reg[MT2063_REG_CTRL_2C] != val)
  1176. status |= mt2063_setreg(state, MT2063_REG_CTRL_2C,
  1177. val);
  1178. }
  1179. /* FIFFQEN and FIFFQ */
  1180. if (status >= 0) {
  1181. val =
  1182. (state->
  1183. reg[MT2063_REG_FIFF_CTRL2] & (u8) ~ 0xF0) |
  1184. (FIFFQEN[Mode] << 7) | (FIFFQ[Mode] << 4);
  1185. if (state->reg[MT2063_REG_FIFF_CTRL2] != val) {
  1186. status |=
  1187. mt2063_setreg(state, MT2063_REG_FIFF_CTRL2, val);
  1188. /* trigger FIFF calibration, needed after changing FIFFQ */
  1189. val =
  1190. (state->reg[MT2063_REG_FIFF_CTRL] | (u8) 0x01);
  1191. status |=
  1192. mt2063_setreg(state, MT2063_REG_FIFF_CTRL, val);
  1193. val =
  1194. (state->
  1195. reg[MT2063_REG_FIFF_CTRL] & (u8) ~ 0x01);
  1196. status |=
  1197. mt2063_setreg(state, MT2063_REG_FIFF_CTRL, val);
  1198. }
  1199. }
  1200. /* DNC1GC & DNC2GC */
  1201. status |= mt2063_get_dnc_output_enable(state, &longval);
  1202. status |= mt2063_set_dnc_output_enable(state, longval);
  1203. /* acLNAmax */
  1204. if (status >= 0) {
  1205. u8 val = (state-> reg[MT2063_REG_LNA_OV] & (u8) ~ 0x1F) |
  1206. (ACLNAMAX[Mode] & 0x1F);
  1207. if (state->reg[MT2063_REG_LNA_OV] != val)
  1208. status |= mt2063_setreg(state, MT2063_REG_LNA_OV, val);
  1209. }
  1210. /* LNATGT */
  1211. if (status >= 0) {
  1212. u8 val = (state-> reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x3F) |
  1213. (LNATGT[Mode] & 0x3F);
  1214. if (state->reg[MT2063_REG_LNA_TGT] != val)
  1215. status |= mt2063_setreg(state, MT2063_REG_LNA_TGT, val);
  1216. }
  1217. /* ACRF */
  1218. if (status >= 0) {
  1219. u8 val = (state-> reg[MT2063_REG_RF_OV] & (u8) ~ 0x1F) |
  1220. (ACRFMAX[Mode] & 0x1F);
  1221. if (state->reg[MT2063_REG_RF_OV] != val)
  1222. status |= mt2063_setreg(state, MT2063_REG_RF_OV, val);
  1223. }
  1224. /* PD1TGT */
  1225. if (status >= 0) {
  1226. u8 val = (state-> reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x3F) |
  1227. (PD1TGT[Mode] & 0x3F);
  1228. if (state->reg[MT2063_REG_PD1_TGT] != val)
  1229. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1230. }
  1231. /* FIFATN */
  1232. if (status >= 0) {
  1233. u8 val = ACFIFMAX[Mode];
  1234. if (state->reg[MT2063_REG_PART_REV] != MT2063_B3 && val > 5)
  1235. val = 5;
  1236. val = (state-> reg[MT2063_REG_FIF_OV] & (u8) ~ 0x1F) |
  1237. (val & 0x1F);
  1238. if (state->reg[MT2063_REG_FIF_OV] != val) {
  1239. status |= mt2063_setreg(state, MT2063_REG_FIF_OV, val);
  1240. }
  1241. }
  1242. /* PD2TGT */
  1243. if (status >= 0) {
  1244. u8 val = (state-> reg[MT2063_REG_PD2_TGT] & (u8) ~ 0x3F) |
  1245. (PD2TGT[Mode] & 0x3F);
  1246. if (state->reg[MT2063_REG_PD2_TGT] != val)
  1247. status |= mt2063_setreg(state, MT2063_REG_PD2_TGT, val);
  1248. }
  1249. /* Ignore ATN Overload */
  1250. if (status >= 0) {
  1251. val =
  1252. (state->
  1253. reg[MT2063_REG_LNA_TGT] & (u8) ~ 0x80) | (RFOVDIS[Mode]
  1254. ? 0x80 :
  1255. 0x00);
  1256. if (state->reg[MT2063_REG_LNA_TGT] != val) {
  1257. status |= mt2063_setreg(state, MT2063_REG_LNA_TGT, val);
  1258. }
  1259. }
  1260. /* Ignore FIF Overload */
  1261. if (status >= 0) {
  1262. val =
  1263. (state->
  1264. reg[MT2063_REG_PD1_TGT] & (u8) ~ 0x80) |
  1265. (FIFOVDIS[Mode] ? 0x80 : 0x00);
  1266. if (state->reg[MT2063_REG_PD1_TGT] != val) {
  1267. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1268. }
  1269. }
  1270. if (status >= 0)
  1271. state->rcvr_mode = Mode;
  1272. return (status);
  1273. }
  1274. /****************************************************************************
  1275. **
  1276. ** Name: MT2063_ClearPowerMaskBits
  1277. **
  1278. ** Description: Clears the power-down mask bits for various sections of
  1279. ** the MT2063
  1280. **
  1281. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1282. ** Bits - Mask bits to be cleared.
  1283. **
  1284. ** See definition of MT2063_Mask_Bits type for description
  1285. ** of each of the power bits.
  1286. **
  1287. ** Returns: status:
  1288. ** MT_OK - No errors
  1289. ** MT_INV_HANDLE - Invalid tuner handle
  1290. ** MT_COMM_ERR - Serial bus communications error
  1291. **
  1292. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1293. **
  1294. ** Revision History:
  1295. **
  1296. ** SCR Date Author Description
  1297. ** -------------------------------------------------------------------------
  1298. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1299. **
  1300. ****************************************************************************/
  1301. static u32 MT2063_ClearPowerMaskBits(struct mt2063_state *state,
  1302. enum MT2063_Mask_Bits Bits)
  1303. {
  1304. u32 status = 0; /* Status to be returned */
  1305. Bits = (enum MT2063_Mask_Bits)(Bits & MT2063_ALL_SD); /* Only valid bits for this tuner */
  1306. if ((Bits & 0xFF00) != 0) {
  1307. state->reg[MT2063_REG_PWR_2] &= ~(u8) (Bits >> 8);
  1308. status |=
  1309. mt2063_write(state,
  1310. MT2063_REG_PWR_2,
  1311. &state->reg[MT2063_REG_PWR_2], 1);
  1312. }
  1313. if ((Bits & 0xFF) != 0) {
  1314. state->reg[MT2063_REG_PWR_1] &= ~(u8) (Bits & 0xFF);
  1315. status |=
  1316. mt2063_write(state,
  1317. MT2063_REG_PWR_1,
  1318. &state->reg[MT2063_REG_PWR_1], 1);
  1319. }
  1320. return (status);
  1321. }
  1322. /****************************************************************************
  1323. **
  1324. ** Name: MT2063_SoftwareShutdown
  1325. **
  1326. ** Description: Enables or disables software shutdown function. When
  1327. ** Shutdown==1, any section whose power mask is set will be
  1328. ** shutdown.
  1329. **
  1330. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1331. ** Shutdown - 1 = shutdown the masked sections, otherwise
  1332. ** power all sections on
  1333. **
  1334. ** Returns: status:
  1335. ** MT_OK - No errors
  1336. ** MT_INV_HANDLE - Invalid tuner handle
  1337. ** MT_COMM_ERR - Serial bus communications error
  1338. **
  1339. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1340. **
  1341. ** Revision History:
  1342. **
  1343. ** SCR Date Author Description
  1344. ** -------------------------------------------------------------------------
  1345. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1346. ** 01-03-2008 PINZ Ver 1.xx: Added a trigger of BYPATNUP for
  1347. ** correct wakeup of the LNA
  1348. **
  1349. ****************************************************************************/
  1350. static u32 MT2063_SoftwareShutdown(struct mt2063_state *state, u8 Shutdown)
  1351. {
  1352. u32 status; /* Status to be returned */
  1353. if (Shutdown == 1)
  1354. state->reg[MT2063_REG_PWR_1] |= 0x04; /* Turn the bit on */
  1355. else
  1356. state->reg[MT2063_REG_PWR_1] &= ~0x04; /* Turn off the bit */
  1357. status = mt2063_write(state,
  1358. MT2063_REG_PWR_1,
  1359. &state->reg[MT2063_REG_PWR_1], 1);
  1360. if (Shutdown != 1) {
  1361. state->reg[MT2063_REG_BYP_CTRL] =
  1362. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F) | 0x40;
  1363. status |=
  1364. mt2063_write(state,
  1365. MT2063_REG_BYP_CTRL,
  1366. &state->reg[MT2063_REG_BYP_CTRL],
  1367. 1);
  1368. state->reg[MT2063_REG_BYP_CTRL] =
  1369. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F);
  1370. status |=
  1371. mt2063_write(state,
  1372. MT2063_REG_BYP_CTRL,
  1373. &state->reg[MT2063_REG_BYP_CTRL],
  1374. 1);
  1375. }
  1376. return status;
  1377. }
  1378. static u32 MT2063_Round_fLO(u32 f_LO, u32 f_LO_Step, u32 f_ref)
  1379. {
  1380. return f_ref * (f_LO / f_ref)
  1381. + f_LO_Step * (((f_LO % f_ref) + (f_LO_Step / 2)) / f_LO_Step);
  1382. }
  1383. /****************************************************************************
  1384. **
  1385. ** Name: fLO_FractionalTerm
  1386. **
  1387. ** Description: Calculates the portion contributed by FracN / denom.
  1388. **
  1389. ** This function preserves maximum precision without
  1390. ** risk of overflow. It accurately calculates
  1391. ** f_ref * num / denom to within 1 HZ with fixed math.
  1392. **
  1393. ** Parameters: num - Fractional portion of the multiplier
  1394. ** denom - denominator portion of the ratio
  1395. ** This routine successfully handles denom values
  1396. ** up to and including 2^18.
  1397. ** f_Ref - SRO frequency. This calculation handles
  1398. ** f_ref as two separate 14-bit fields.
  1399. ** Therefore, a maximum value of 2^28-1
  1400. ** may safely be used for f_ref. This is
  1401. ** the genesis of the magic number "14" and the
  1402. ** magic mask value of 0x03FFF.
  1403. **
  1404. ** Returns: f_ref * num / denom
  1405. **
  1406. ** Revision History:
  1407. **
  1408. ** SCR Date Author Description
  1409. ** -------------------------------------------------------------------------
  1410. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1411. **
  1412. ****************************************************************************/
  1413. static u32 MT2063_fLO_FractionalTerm(u32 f_ref, u32 num, u32 denom)
  1414. {
  1415. u32 t1 = (f_ref >> 14) * num;
  1416. u32 term1 = t1 / denom;
  1417. u32 loss = t1 % denom;
  1418. u32 term2 =
  1419. (((f_ref & 0x00003FFF) * num + (loss << 14)) + (denom / 2)) / denom;
  1420. return ((term1 << 14) + term2);
  1421. }
  1422. /****************************************************************************
  1423. **
  1424. ** Name: CalcLO1Mult
  1425. **
  1426. ** Description: Calculates Integer divider value and the numerator
  1427. ** value for a FracN PLL.
  1428. **
  1429. ** This function assumes that the f_LO and f_Ref are
  1430. ** evenly divisible by f_LO_Step.
  1431. **
  1432. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  1433. ** FracN - OUTPUT: Fractional portion of the multiplier
  1434. ** f_LO - desired LO frequency.
  1435. ** f_LO_Step - Minimum step size for the LO (in Hz).
  1436. ** f_Ref - SRO frequency.
  1437. ** f_Avoid - Range of PLL frequencies to avoid near
  1438. ** integer multiples of f_Ref (in Hz).
  1439. **
  1440. ** Returns: Recalculated LO frequency.
  1441. **
  1442. ** Revision History:
  1443. **
  1444. ** SCR Date Author Description
  1445. ** -------------------------------------------------------------------------
  1446. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1447. **
  1448. ****************************************************************************/
  1449. static u32 MT2063_CalcLO1Mult(u32 * Div,
  1450. u32 * FracN,
  1451. u32 f_LO,
  1452. u32 f_LO_Step, u32 f_Ref)
  1453. {
  1454. /* Calculate the whole number portion of the divider */
  1455. *Div = f_LO / f_Ref;
  1456. /* Calculate the numerator value (round to nearest f_LO_Step) */
  1457. *FracN =
  1458. (64 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  1459. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  1460. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN, 64);
  1461. }
  1462. /****************************************************************************
  1463. **
  1464. ** Name: CalcLO2Mult
  1465. **
  1466. ** Description: Calculates Integer divider value and the numerator
  1467. ** value for a FracN PLL.
  1468. **
  1469. ** This function assumes that the f_LO and f_Ref are
  1470. ** evenly divisible by f_LO_Step.
  1471. **
  1472. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  1473. ** FracN - OUTPUT: Fractional portion of the multiplier
  1474. ** f_LO - desired LO frequency.
  1475. ** f_LO_Step - Minimum step size for the LO (in Hz).
  1476. ** f_Ref - SRO frequency.
  1477. ** f_Avoid - Range of PLL frequencies to avoid near
  1478. ** integer multiples of f_Ref (in Hz).
  1479. **
  1480. ** Returns: Recalculated LO frequency.
  1481. **
  1482. ** Revision History:
  1483. **
  1484. ** SCR Date Author Description
  1485. ** -------------------------------------------------------------------------
  1486. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1487. **
  1488. ****************************************************************************/
  1489. static u32 MT2063_CalcLO2Mult(u32 * Div,
  1490. u32 * FracN,
  1491. u32 f_LO,
  1492. u32 f_LO_Step, u32 f_Ref)
  1493. {
  1494. /* Calculate the whole number portion of the divider */
  1495. *Div = f_LO / f_Ref;
  1496. /* Calculate the numerator value (round to nearest f_LO_Step) */
  1497. *FracN =
  1498. (8191 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  1499. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  1500. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN,
  1501. 8191);
  1502. }
  1503. /****************************************************************************
  1504. **
  1505. ** Name: FindClearTuneFilter
  1506. **
  1507. ** Description: Calculate the corrrect ClearTune filter to be used for
  1508. ** a given input frequency.
  1509. **
  1510. ** Parameters: state - ptr to tuner data structure
  1511. ** f_in - RF input center frequency (in Hz).
  1512. **
  1513. ** Returns: ClearTune filter number (0-31)
  1514. **
  1515. ** Dependencies: MUST CALL MT2064_Open BEFORE FindClearTuneFilter!
  1516. **
  1517. ** Revision History:
  1518. **
  1519. ** SCR Date Author Description
  1520. ** -------------------------------------------------------------------------
  1521. ** 04-10-2008 PINZ Ver 1.14: Use software-controlled ClearTune
  1522. ** cross-over frequency values.
  1523. **
  1524. ****************************************************************************/
  1525. static u32 FindClearTuneFilter(struct mt2063_state *state, u32 f_in)
  1526. {
  1527. u32 RFBand;
  1528. u32 idx; /* index loop */
  1529. /*
  1530. ** Find RF Band setting
  1531. */
  1532. RFBand = 31; /* def when f_in > all */
  1533. for (idx = 0; idx < 31; ++idx) {
  1534. if (state->CTFiltMax[idx] >= f_in) {
  1535. RFBand = idx;
  1536. break;
  1537. }
  1538. }
  1539. return RFBand;
  1540. }
  1541. /****************************************************************************
  1542. **
  1543. ** Name: MT2063_Tune
  1544. **
  1545. ** Description: Change the tuner's tuned frequency to RFin.
  1546. **
  1547. ** Parameters: h - Open handle to the tuner (from MT2063_Open).
  1548. ** f_in - RF input center frequency (in Hz).
  1549. **
  1550. ** Returns: status:
  1551. ** MT_OK - No errors
  1552. ** MT_INV_HANDLE - Invalid tuner handle
  1553. ** MT_UPC_UNLOCK - Upconverter PLL unlocked
  1554. ** MT_DNC_UNLOCK - Downconverter PLL unlocked
  1555. ** MT_COMM_ERR - Serial bus communications error
  1556. ** MT_SPUR_CNT_MASK - Count of avoided LO spurs
  1557. ** MT_SPUR_PRESENT - LO spur possible in output
  1558. ** MT_FIN_RANGE - Input freq out of range
  1559. ** MT_FOUT_RANGE - Output freq out of range
  1560. ** MT_UPC_RANGE - Upconverter freq out of range
  1561. ** MT_DNC_RANGE - Downconverter freq out of range
  1562. **
  1563. ** Dependencies: MUST CALL MT2063_Open BEFORE MT2063_Tune!
  1564. **
  1565. ** MT_ReadSub - Read data from the two-wire serial bus
  1566. ** MT_WriteSub - Write data to the two-wire serial bus
  1567. ** MT_Sleep - Delay execution for x milliseconds
  1568. ** MT2063_GetLocked - Checks to see if LO1 and LO2 are locked
  1569. **
  1570. ** Revision History:
  1571. **
  1572. ** SCR Date Author Description
  1573. ** -------------------------------------------------------------------------
  1574. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1575. ** 04-10-2008 PINZ Ver 1.05: Use software-controlled ClearTune
  1576. ** cross-over frequency values.
  1577. ** 175 I 16-06-2008 PINZ Ver 1.16: Add control to avoid US DECT freqs.
  1578. ** 175 I 06-19-2008 RSK Ver 1.17: Refactor DECT control to SpurAvoid.
  1579. ** 06-24-2008 PINZ Ver 1.18: Add Get/SetParam CTFILT_SW
  1580. **
  1581. ****************************************************************************/
  1582. static u32 MT2063_Tune(struct mt2063_state *state, u32 f_in)
  1583. { /* RF input center frequency */
  1584. u32 status = 0; /* status of operation */
  1585. u32 LO1; /* 1st LO register value */
  1586. u32 Num1; /* Numerator for LO1 reg. value */
  1587. u32 f_IF1; /* 1st IF requested */
  1588. u32 LO2; /* 2nd LO register value */
  1589. u32 Num2; /* Numerator for LO2 reg. value */
  1590. u32 ofLO1, ofLO2; /* last time's LO frequencies */
  1591. u32 ofin, ofout; /* last time's I/O frequencies */
  1592. u8 fiffc = 0x80; /* FIFF center freq from tuner */
  1593. u32 fiffof; /* Offset from FIFF center freq */
  1594. const u8 LO1LK = 0x80; /* Mask for LO1 Lock bit */
  1595. u8 LO2LK = 0x08; /* Mask for LO2 Lock bit */
  1596. u8 val;
  1597. u32 RFBand;
  1598. /* Check the input and output frequency ranges */
  1599. if ((f_in < MT2063_MIN_FIN_FREQ) || (f_in > MT2063_MAX_FIN_FREQ))
  1600. return -EINVAL;
  1601. if ((state->AS_Data.f_out < MT2063_MIN_FOUT_FREQ)
  1602. || (state->AS_Data.f_out > MT2063_MAX_FOUT_FREQ))
  1603. return -EINVAL;
  1604. /*
  1605. ** Save original LO1 and LO2 register values
  1606. */
  1607. ofLO1 = state->AS_Data.f_LO1;
  1608. ofLO2 = state->AS_Data.f_LO2;
  1609. ofin = state->AS_Data.f_in;
  1610. ofout = state->AS_Data.f_out;
  1611. /*
  1612. ** Find and set RF Band setting
  1613. */
  1614. if (state->ctfilt_sw == 1) {
  1615. val = (state->reg[MT2063_REG_CTUNE_CTRL] | 0x08);
  1616. if (state->reg[MT2063_REG_CTUNE_CTRL] != val) {
  1617. status |=
  1618. mt2063_setreg(state, MT2063_REG_CTUNE_CTRL, val);
  1619. }
  1620. val = state->reg[MT2063_REG_CTUNE_OV];
  1621. RFBand = FindClearTuneFilter(state, f_in);
  1622. state->reg[MT2063_REG_CTUNE_OV] =
  1623. (u8) ((state->reg[MT2063_REG_CTUNE_OV] & ~0x1F)
  1624. | RFBand);
  1625. if (state->reg[MT2063_REG_CTUNE_OV] != val) {
  1626. status |=
  1627. mt2063_setreg(state, MT2063_REG_CTUNE_OV, val);
  1628. }
  1629. }
  1630. /*
  1631. ** Read the FIFF Center Frequency from the tuner
  1632. */
  1633. if (status >= 0) {
  1634. status |=
  1635. mt2063_read(state,
  1636. MT2063_REG_FIFFC,
  1637. &state->reg[MT2063_REG_FIFFC], 1);
  1638. fiffc = state->reg[MT2063_REG_FIFFC];
  1639. }
  1640. /*
  1641. ** Assign in the requested values
  1642. */
  1643. state->AS_Data.f_in = f_in;
  1644. /* Request a 1st IF such that LO1 is on a step size */
  1645. state->AS_Data.f_if1_Request =
  1646. MT2063_Round_fLO(state->AS_Data.f_if1_Request + f_in,
  1647. state->AS_Data.f_LO1_Step,
  1648. state->AS_Data.f_ref) - f_in;
  1649. /*
  1650. ** Calculate frequency settings. f_IF1_FREQ + f_in is the
  1651. ** desired LO1 frequency
  1652. */
  1653. MT2063_ResetExclZones(&state->AS_Data);
  1654. f_IF1 = MT2063_ChooseFirstIF(&state->AS_Data);
  1655. state->AS_Data.f_LO1 =
  1656. MT2063_Round_fLO(f_IF1 + f_in, state->AS_Data.f_LO1_Step,
  1657. state->AS_Data.f_ref);
  1658. state->AS_Data.f_LO2 =
  1659. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  1660. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1661. /*
  1662. ** Check for any LO spurs in the output bandwidth and adjust
  1663. ** the LO settings to avoid them if needed
  1664. */
  1665. status |= MT2063_AvoidSpurs(&state->AS_Data);
  1666. /*
  1667. ** MT_AvoidSpurs spurs may have changed the LO1 & LO2 values.
  1668. ** Recalculate the LO frequencies and the values to be placed
  1669. ** in the tuning registers.
  1670. */
  1671. state->AS_Data.f_LO1 =
  1672. MT2063_CalcLO1Mult(&LO1, &Num1, state->AS_Data.f_LO1,
  1673. state->AS_Data.f_LO1_Step, state->AS_Data.f_ref);
  1674. state->AS_Data.f_LO2 =
  1675. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  1676. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1677. state->AS_Data.f_LO2 =
  1678. MT2063_CalcLO2Mult(&LO2, &Num2, state->AS_Data.f_LO2,
  1679. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1680. /*
  1681. ** Check the upconverter and downconverter frequency ranges
  1682. */
  1683. if ((state->AS_Data.f_LO1 < MT2063_MIN_UPC_FREQ)
  1684. || (state->AS_Data.f_LO1 > MT2063_MAX_UPC_FREQ))
  1685. status |= MT2063_UPC_RANGE;
  1686. if ((state->AS_Data.f_LO2 < MT2063_MIN_DNC_FREQ)
  1687. || (state->AS_Data.f_LO2 > MT2063_MAX_DNC_FREQ))
  1688. status |= MT2063_DNC_RANGE;
  1689. /* LO2 Lock bit was in a different place for B0 version */
  1690. if (state->tuner_id == MT2063_B0)
  1691. LO2LK = 0x40;
  1692. /*
  1693. ** If we have the same LO frequencies and we're already locked,
  1694. ** then skip re-programming the LO registers.
  1695. */
  1696. if ((ofLO1 != state->AS_Data.f_LO1)
  1697. || (ofLO2 != state->AS_Data.f_LO2)
  1698. || ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) !=
  1699. (LO1LK | LO2LK))) {
  1700. /*
  1701. ** Calculate the FIFFOF register value
  1702. **
  1703. ** IF1_Actual
  1704. ** FIFFOF = ------------ - 8 * FIFFC - 4992
  1705. ** f_ref/64
  1706. */
  1707. fiffof =
  1708. (state->AS_Data.f_LO1 -
  1709. f_in) / (state->AS_Data.f_ref / 64) - 8 * (u32) fiffc -
  1710. 4992;
  1711. if (fiffof > 0xFF)
  1712. fiffof = 0xFF;
  1713. /*
  1714. ** Place all of the calculated values into the local tuner
  1715. ** register fields.
  1716. */
  1717. if (status >= 0) {
  1718. state->reg[MT2063_REG_LO1CQ_1] = (u8) (LO1 & 0xFF); /* DIV1q */
  1719. state->reg[MT2063_REG_LO1CQ_2] = (u8) (Num1 & 0x3F); /* NUM1q */
  1720. state->reg[MT2063_REG_LO2CQ_1] = (u8) (((LO2 & 0x7F) << 1) /* DIV2q */
  1721. |(Num2 >> 12)); /* NUM2q (hi) */
  1722. state->reg[MT2063_REG_LO2CQ_2] = (u8) ((Num2 & 0x0FF0) >> 4); /* NUM2q (mid) */
  1723. state->reg[MT2063_REG_LO2CQ_3] = (u8) (0xE0 | (Num2 & 0x000F)); /* NUM2q (lo) */
  1724. /*
  1725. ** Now write out the computed register values
  1726. ** IMPORTANT: There is a required order for writing
  1727. ** (0x05 must follow all the others).
  1728. */
  1729. status |= mt2063_write(state, MT2063_REG_LO1CQ_1, &state->reg[MT2063_REG_LO1CQ_1], 5); /* 0x01 - 0x05 */
  1730. if (state->tuner_id == MT2063_B0) {
  1731. /* Re-write the one-shot bits to trigger the tune operation */
  1732. status |= mt2063_write(state, MT2063_REG_LO2CQ_3, &state->reg[MT2063_REG_LO2CQ_3], 1); /* 0x05 */
  1733. }
  1734. /* Write out the FIFF offset only if it's changing */
  1735. if (state->reg[MT2063_REG_FIFF_OFFSET] !=
  1736. (u8) fiffof) {
  1737. state->reg[MT2063_REG_FIFF_OFFSET] =
  1738. (u8) fiffof;
  1739. status |=
  1740. mt2063_write(state,
  1741. MT2063_REG_FIFF_OFFSET,
  1742. &state->
  1743. reg[MT2063_REG_FIFF_OFFSET],
  1744. 1);
  1745. }
  1746. }
  1747. /*
  1748. ** Check for LO's locking
  1749. */
  1750. if (status < 0)
  1751. return status;
  1752. status = mt2063_lockStatus(state);
  1753. if (status < 0)
  1754. return status;
  1755. if (!status)
  1756. return -EINVAL; /* Couldn't lock */
  1757. /*
  1758. * If we locked OK, assign calculated data to mt2063_state structure
  1759. */
  1760. state->f_IF1_actual = state->AS_Data.f_LO1 - f_in;
  1761. }
  1762. return status;
  1763. }
  1764. int mt2063_setTune(struct dvb_frontend *fe, u32 f_in, u32 bw_in,
  1765. enum MTTune_atv_standard tv_type)
  1766. {
  1767. struct mt2063_state *state = fe->tuner_priv;
  1768. u32 status = 0;
  1769. s32 pict_car = 0;
  1770. s32 pict2chanb_vsb = 0;
  1771. s32 pict2chanb_snd = 0;
  1772. s32 pict2snd1 = 0;
  1773. s32 pict2snd2 = 0;
  1774. s32 ch_bw = 0;
  1775. s32 if_mid = 0;
  1776. s32 rcvr_mode = 0;
  1777. switch (tv_type) {
  1778. case MTTUNEA_PAL_B:{
  1779. pict_car = 38900000;
  1780. ch_bw = 8000000;
  1781. pict2chanb_vsb = -1250000;
  1782. pict2snd1 = 5500000;
  1783. pict2snd2 = 5742000;
  1784. rcvr_mode = 1;
  1785. break;
  1786. }
  1787. case MTTUNEA_PAL_G:{
  1788. pict_car = 38900000;
  1789. ch_bw = 7000000;
  1790. pict2chanb_vsb = -1250000;
  1791. pict2snd1 = 5500000;
  1792. pict2snd2 = 0;
  1793. rcvr_mode = 1;
  1794. break;
  1795. }
  1796. case MTTUNEA_PAL_I:{
  1797. pict_car = 38900000;
  1798. ch_bw = 8000000;
  1799. pict2chanb_vsb = -1250000;
  1800. pict2snd1 = 6000000;
  1801. pict2snd2 = 0;
  1802. rcvr_mode = 1;
  1803. break;
  1804. }
  1805. case MTTUNEA_PAL_L:{
  1806. pict_car = 38900000;
  1807. ch_bw = 8000000;
  1808. pict2chanb_vsb = -1250000;
  1809. pict2snd1 = 6500000;
  1810. pict2snd2 = 0;
  1811. rcvr_mode = 1;
  1812. break;
  1813. }
  1814. case MTTUNEA_PAL_MN:{
  1815. pict_car = 38900000;
  1816. ch_bw = 6000000;
  1817. pict2chanb_vsb = -1250000;
  1818. pict2snd1 = 4500000;
  1819. pict2snd2 = 0;
  1820. rcvr_mode = 1;
  1821. break;
  1822. }
  1823. case MTTUNEA_PAL_DK:{
  1824. pict_car = 38900000;
  1825. ch_bw = 8000000;
  1826. pict2chanb_vsb = -1250000;
  1827. pict2snd1 = 6500000;
  1828. pict2snd2 = 0;
  1829. rcvr_mode = 1;
  1830. break;
  1831. }
  1832. case MTTUNEA_DIGITAL:{
  1833. pict_car = 36125000;
  1834. ch_bw = 8000000;
  1835. pict2chanb_vsb = -(ch_bw / 2);
  1836. pict2snd1 = 0;
  1837. pict2snd2 = 0;
  1838. rcvr_mode = 2;
  1839. break;
  1840. }
  1841. case MTTUNEA_FMRADIO:{
  1842. pict_car = 38900000;
  1843. ch_bw = 8000000;
  1844. pict2chanb_vsb = -(ch_bw / 2);
  1845. pict2snd1 = 0;
  1846. pict2snd2 = 0;
  1847. rcvr_mode = 4;
  1848. //f_in -= 2900000;
  1849. break;
  1850. }
  1851. case MTTUNEA_DVBC:{
  1852. pict_car = 36125000;
  1853. ch_bw = 8000000;
  1854. pict2chanb_vsb = -(ch_bw / 2);
  1855. pict2snd1 = 0;
  1856. pict2snd2 = 0;
  1857. rcvr_mode = MT2063_CABLE_QAM;
  1858. break;
  1859. }
  1860. case MTTUNEA_DVBT:{
  1861. pict_car = 36125000;
  1862. ch_bw = bw_in; //8000000
  1863. pict2chanb_vsb = -(ch_bw / 2);
  1864. pict2snd1 = 0;
  1865. pict2snd2 = 0;
  1866. rcvr_mode = MT2063_OFFAIR_COFDM;
  1867. break;
  1868. }
  1869. case MTTUNEA_UNKNOWN:
  1870. break;
  1871. default:
  1872. break;
  1873. }
  1874. pict2chanb_snd = pict2chanb_vsb - ch_bw;
  1875. if_mid = pict_car - (pict2chanb_vsb + (ch_bw / 2));
  1876. state->AS_Data.f_LO2_Step = 125000;
  1877. state->AS_Data.f_out = if_mid;
  1878. state->AS_Data.f_out_bw = ch_bw + 750000;
  1879. status = MT2063_SetReceiverMode(state, rcvr_mode);
  1880. if (status < 0)
  1881. return status;
  1882. status = MT2063_Tune(state, (f_in + (pict2chanb_vsb + (ch_bw / 2))));
  1883. return status;
  1884. }
  1885. static const u8 MT2063B0_defaults[] = {
  1886. /* Reg, Value */
  1887. 0x19, 0x05,
  1888. 0x1B, 0x1D,
  1889. 0x1C, 0x1F,
  1890. 0x1D, 0x0F,
  1891. 0x1E, 0x3F,
  1892. 0x1F, 0x0F,
  1893. 0x20, 0x3F,
  1894. 0x22, 0x21,
  1895. 0x23, 0x3F,
  1896. 0x24, 0x20,
  1897. 0x25, 0x3F,
  1898. 0x27, 0xEE,
  1899. 0x2C, 0x27, /* bit at 0x20 is cleared below */
  1900. 0x30, 0x03,
  1901. 0x2C, 0x07, /* bit at 0x20 is cleared here */
  1902. 0x2D, 0x87,
  1903. 0x2E, 0xAA,
  1904. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1905. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1906. 0x00
  1907. };
  1908. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  1909. static const u8 MT2063B1_defaults[] = {
  1910. /* Reg, Value */
  1911. 0x05, 0xF0,
  1912. 0x11, 0x10, /* New Enable AFCsd */
  1913. 0x19, 0x05,
  1914. 0x1A, 0x6C,
  1915. 0x1B, 0x24,
  1916. 0x1C, 0x28,
  1917. 0x1D, 0x8F,
  1918. 0x1E, 0x14,
  1919. 0x1F, 0x8F,
  1920. 0x20, 0x57,
  1921. 0x22, 0x21, /* New - ver 1.03 */
  1922. 0x23, 0x3C, /* New - ver 1.10 */
  1923. 0x24, 0x20, /* New - ver 1.03 */
  1924. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  1925. 0x2D, 0x87, /* FIFFQ=0 */
  1926. 0x2F, 0xF3,
  1927. 0x30, 0x0C, /* New - ver 1.11 */
  1928. 0x31, 0x1B, /* New - ver 1.11 */
  1929. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  1930. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1931. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1932. 0x00
  1933. };
  1934. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  1935. static const u8 MT2063B3_defaults[] = {
  1936. /* Reg, Value */
  1937. 0x05, 0xF0,
  1938. 0x19, 0x3D,
  1939. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  1940. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  1941. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1942. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1943. 0x00
  1944. };
  1945. static int mt2063_init(struct dvb_frontend *fe)
  1946. {
  1947. u32 status;
  1948. struct mt2063_state *state = fe->tuner_priv;
  1949. u8 all_resets = 0xF0; /* reset/load bits */
  1950. const u8 *def = NULL;
  1951. u32 FCRUN;
  1952. s32 maxReads;
  1953. u32 fcu_osc;
  1954. u32 i;
  1955. state->rcvr_mode = MT2063_CABLE_QAM;
  1956. /* Read the Part/Rev code from the tuner */
  1957. status = mt2063_read(state, MT2063_REG_PART_REV, state->reg, 1);
  1958. if (status < 0)
  1959. return status;
  1960. /* Check the part/rev code */
  1961. if (((state->reg[MT2063_REG_PART_REV] != MT2063_B0) /* MT2063 B0 */
  1962. &&(state->reg[MT2063_REG_PART_REV] != MT2063_B1) /* MT2063 B1 */
  1963. &&(state->reg[MT2063_REG_PART_REV] != MT2063_B3))) /* MT2063 B3 */
  1964. return -ENODEV; /* Wrong tuner Part/Rev code */
  1965. /* Check the 2nd byte of the Part/Rev code from the tuner */
  1966. status = mt2063_read(state, MT2063_REG_RSVD_3B,
  1967. &state->reg[MT2063_REG_RSVD_3B], 1);
  1968. /* b7 != 0 ==> NOT MT2063 */
  1969. if (status < 0 ||((state->reg[MT2063_REG_RSVD_3B] & 0x80) != 0x00))
  1970. return -ENODEV; /* Wrong tuner Part/Rev code */
  1971. /* Reset the tuner */
  1972. status = mt2063_write(state, MT2063_REG_LO2CQ_3, &all_resets, 1);
  1973. if (status < 0)
  1974. return status;
  1975. /* change all of the default values that vary from the HW reset values */
  1976. /* def = (state->reg[PART_REV] == MT2063_B0) ? MT2063B0_defaults : MT2063B1_defaults; */
  1977. switch (state->reg[MT2063_REG_PART_REV]) {
  1978. case MT2063_B3:
  1979. def = MT2063B3_defaults;
  1980. break;
  1981. case MT2063_B1:
  1982. def = MT2063B1_defaults;
  1983. break;
  1984. case MT2063_B0:
  1985. def = MT2063B0_defaults;
  1986. break;
  1987. default:
  1988. return -ENODEV;
  1989. break;
  1990. }
  1991. while (status >= 0 && *def) {
  1992. u8 reg = *def++;
  1993. u8 val = *def++;
  1994. status = mt2063_write(state, reg, &val, 1);
  1995. }
  1996. if (status < 0)
  1997. return status;
  1998. /* Wait for FIFF location to complete. */
  1999. FCRUN = 1;
  2000. maxReads = 10;
  2001. while (status >= 0 && (FCRUN != 0) && (maxReads-- > 0)) {
  2002. msleep(2);
  2003. status = mt2063_read(state,
  2004. MT2063_REG_XO_STATUS,
  2005. &state->
  2006. reg[MT2063_REG_XO_STATUS], 1);
  2007. FCRUN = (state->reg[MT2063_REG_XO_STATUS] & 0x40) >> 6;
  2008. }
  2009. if (FCRUN != 0 || status < 0)
  2010. return -ENODEV;
  2011. status = mt2063_read(state,
  2012. MT2063_REG_FIFFC,
  2013. &state->reg[MT2063_REG_FIFFC], 1);
  2014. if (status < 0)
  2015. return status;
  2016. /* Read back all the registers from the tuner */
  2017. status = mt2063_read(state,
  2018. MT2063_REG_PART_REV,
  2019. state->reg, MT2063_REG_END_REGS);
  2020. if (status < 0)
  2021. return status;
  2022. /* Initialize the tuner state. */
  2023. state->tuner_id = state->reg[MT2063_REG_PART_REV];
  2024. state->AS_Data.f_ref = MT2063_REF_FREQ;
  2025. state->AS_Data.f_if1_Center = (state->AS_Data.f_ref / 8) *
  2026. ((u32) state->reg[MT2063_REG_FIFFC] + 640);
  2027. state->AS_Data.f_if1_bw = MT2063_IF1_BW;
  2028. state->AS_Data.f_out = 43750000UL;
  2029. state->AS_Data.f_out_bw = 6750000UL;
  2030. state->AS_Data.f_zif_bw = MT2063_ZIF_BW;
  2031. state->AS_Data.f_LO1_Step = state->AS_Data.f_ref / 64;
  2032. state->AS_Data.f_LO2_Step = MT2063_TUNE_STEP_SIZE;
  2033. state->AS_Data.maxH1 = MT2063_MAX_HARMONICS_1;
  2034. state->AS_Data.maxH2 = MT2063_MAX_HARMONICS_2;
  2035. state->AS_Data.f_min_LO_Separation = MT2063_MIN_LO_SEP;
  2036. state->AS_Data.f_if1_Request = state->AS_Data.f_if1_Center;
  2037. state->AS_Data.f_LO1 = 2181000000UL;
  2038. state->AS_Data.f_LO2 = 1486249786UL;
  2039. state->f_IF1_actual = state->AS_Data.f_if1_Center;
  2040. state->AS_Data.f_in = state->AS_Data.f_LO1 - state->f_IF1_actual;
  2041. state->AS_Data.f_LO1_FracN_Avoid = MT2063_LO1_FRACN_AVOID;
  2042. state->AS_Data.f_LO2_FracN_Avoid = MT2063_LO2_FRACN_AVOID;
  2043. state->num_regs = MT2063_REG_END_REGS;
  2044. state->AS_Data.avoidDECT = MT2063_AVOID_BOTH;
  2045. state->ctfilt_sw = 0;
  2046. state->CTFiltMax[0] = 69230000;
  2047. state->CTFiltMax[1] = 105770000;
  2048. state->CTFiltMax[2] = 140350000;
  2049. state->CTFiltMax[3] = 177110000;
  2050. state->CTFiltMax[4] = 212860000;
  2051. state->CTFiltMax[5] = 241130000;
  2052. state->CTFiltMax[6] = 274370000;
  2053. state->CTFiltMax[7] = 309820000;
  2054. state->CTFiltMax[8] = 342450000;
  2055. state->CTFiltMax[9] = 378870000;
  2056. state->CTFiltMax[10] = 416210000;
  2057. state->CTFiltMax[11] = 456500000;
  2058. state->CTFiltMax[12] = 495790000;
  2059. state->CTFiltMax[13] = 534530000;
  2060. state->CTFiltMax[14] = 572610000;
  2061. state->CTFiltMax[15] = 598970000;
  2062. state->CTFiltMax[16] = 635910000;
  2063. state->CTFiltMax[17] = 672130000;
  2064. state->CTFiltMax[18] = 714840000;
  2065. state->CTFiltMax[19] = 739660000;
  2066. state->CTFiltMax[20] = 770410000;
  2067. state->CTFiltMax[21] = 814660000;
  2068. state->CTFiltMax[22] = 846950000;
  2069. state->CTFiltMax[23] = 867820000;
  2070. state->CTFiltMax[24] = 915980000;
  2071. state->CTFiltMax[25] = 947450000;
  2072. state->CTFiltMax[26] = 983110000;
  2073. state->CTFiltMax[27] = 1021630000;
  2074. state->CTFiltMax[28] = 1061870000;
  2075. state->CTFiltMax[29] = 1098330000;
  2076. state->CTFiltMax[30] = 1138990000;
  2077. /*
  2078. ** Fetch the FCU osc value and use it and the fRef value to
  2079. ** scale all of the Band Max values
  2080. */
  2081. state->reg[MT2063_REG_CTUNE_CTRL] = 0x0A;
  2082. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  2083. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  2084. if (status < 0)
  2085. return status;
  2086. /* Read the ClearTune filter calibration value */
  2087. status = mt2063_read(state, MT2063_REG_FIFFC,
  2088. &state->reg[MT2063_REG_FIFFC], 1);
  2089. if (status < 0)
  2090. return status;
  2091. fcu_osc = state->reg[MT2063_REG_FIFFC];
  2092. state->reg[MT2063_REG_CTUNE_CTRL] = 0x00;
  2093. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  2094. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  2095. if (status < 0)
  2096. return status;
  2097. /* Adjust each of the values in the ClearTune filter cross-over table */
  2098. for (i = 0; i < 31; i++)
  2099. state->CTFiltMax[i] =(state->CTFiltMax[i] / 768) * (fcu_osc + 640);
  2100. status = MT2063_SoftwareShutdown(state, 1);
  2101. if (status < 0)
  2102. return status;
  2103. status = MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
  2104. if (status < 0)
  2105. return status;
  2106. return 0;
  2107. }
  2108. static int mt2063_get_status(struct dvb_frontend *fe, u32 * status)
  2109. {
  2110. int rc = 0;
  2111. /* FIXME: add get tuner lock status */
  2112. return rc;
  2113. }
  2114. static int mt2063_get_state(struct dvb_frontend *fe,
  2115. enum tuner_param param, struct tuner_state *tunstate)
  2116. {
  2117. struct mt2063_state *state = fe->tuner_priv;
  2118. switch (param) {
  2119. case DVBFE_TUNER_FREQUENCY:
  2120. //get frequency
  2121. break;
  2122. case DVBFE_TUNER_TUNERSTEP:
  2123. break;
  2124. case DVBFE_TUNER_IFFREQ:
  2125. break;
  2126. case DVBFE_TUNER_BANDWIDTH:
  2127. //get bandwidth
  2128. break;
  2129. case DVBFE_TUNER_REFCLOCK:
  2130. tunstate->refclock = mt2063_lockStatus(state);
  2131. break;
  2132. default:
  2133. break;
  2134. }
  2135. return (int)tunstate->refclock;
  2136. }
  2137. static int mt2063_set_state(struct dvb_frontend *fe,
  2138. enum tuner_param param, struct tuner_state *tunstate)
  2139. {
  2140. struct mt2063_state *state = fe->tuner_priv;
  2141. u32 status = 0;
  2142. switch (param) {
  2143. case DVBFE_TUNER_FREQUENCY:
  2144. //set frequency
  2145. status =
  2146. mt2063_setTune(fe,
  2147. tunstate->frequency, tunstate->bandwidth,
  2148. state->tv_type);
  2149. state->frequency = tunstate->frequency;
  2150. break;
  2151. case DVBFE_TUNER_TUNERSTEP:
  2152. break;
  2153. case DVBFE_TUNER_IFFREQ:
  2154. break;
  2155. case DVBFE_TUNER_BANDWIDTH:
  2156. //set bandwidth
  2157. state->bandwidth = tunstate->bandwidth;
  2158. break;
  2159. case DVBFE_TUNER_REFCLOCK:
  2160. break;
  2161. default:
  2162. break;
  2163. }
  2164. return (int)status;
  2165. }
  2166. static int mt2063_release(struct dvb_frontend *fe)
  2167. {
  2168. struct mt2063_state *state = fe->tuner_priv;
  2169. fe->tuner_priv = NULL;
  2170. kfree(state);
  2171. return 0;
  2172. }
  2173. static struct dvb_tuner_ops mt2063_ops = {
  2174. .info = {
  2175. .name = "MT2063 Silicon Tuner",
  2176. .frequency_min = 45000000,
  2177. .frequency_max = 850000000,
  2178. .frequency_step = 0,
  2179. },
  2180. .init = mt2063_init,
  2181. .sleep = MT2063_Sleep,
  2182. .get_status = mt2063_get_status,
  2183. .get_state = mt2063_get_state,
  2184. .set_state = mt2063_set_state,
  2185. .release = mt2063_release
  2186. };
  2187. struct dvb_frontend *mt2063_attach(struct dvb_frontend *fe,
  2188. struct mt2063_config *config,
  2189. struct i2c_adapter *i2c)
  2190. {
  2191. struct mt2063_state *state = NULL;
  2192. state = kzalloc(sizeof(struct mt2063_state), GFP_KERNEL);
  2193. if (state == NULL)
  2194. goto error;
  2195. state->config = config;
  2196. state->i2c = i2c;
  2197. state->frontend = fe;
  2198. state->reference = config->refclock / 1000; /* kHz */
  2199. fe->tuner_priv = state;
  2200. fe->ops.tuner_ops = mt2063_ops;
  2201. printk("%s: Attaching MT2063 \n", __func__);
  2202. return fe;
  2203. error:
  2204. kfree(state);
  2205. return NULL;
  2206. }
  2207. EXPORT_SYMBOL_GPL(mt2063_attach);
  2208. /*
  2209. * Ancillary routines visible outside mt2063
  2210. * FIXME: Remove them in favor of using standard tuner callbacks
  2211. */
  2212. unsigned int tuner_MT2063_SoftwareShutdown(struct dvb_frontend *fe)
  2213. {
  2214. struct mt2063_state *state = fe->tuner_priv;
  2215. int err = 0;
  2216. err = MT2063_SoftwareShutdown(state, 1);
  2217. if (err < 0)
  2218. printk(KERN_ERR "%s: Couldn't shutdown\n", __func__);
  2219. return err;
  2220. }
  2221. EXPORT_SYMBOL_GPL(tuner_MT2063_SoftwareShutdown);
  2222. unsigned int tuner_MT2063_ClearPowerMaskBits(struct dvb_frontend *fe)
  2223. {
  2224. struct mt2063_state *state = fe->tuner_priv;
  2225. int err = 0;
  2226. err = MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
  2227. if (err < 0)
  2228. printk(KERN_ERR "%s: Invalid parameter\n", __func__);
  2229. return err;
  2230. }
  2231. EXPORT_SYMBOL_GPL(tuner_MT2063_ClearPowerMaskBits);
  2232. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  2233. MODULE_AUTHOR("Henry");
  2234. MODULE_DESCRIPTION("MT2063 Silicon tuner");
  2235. MODULE_LICENSE("GPL");