mt2063.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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(KERN_ERR "%s error ret=%d\n", __func__, 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. /* Take MT_ExclZones, center around f_Center and change the resolution to f_Step */
  533. while (pNode != NULL) {
  534. /* floor function */
  535. tmpMin =
  536. floor((s32) (pNode->min_ - f_Center), (s32) f_Step);
  537. /* ceil function */
  538. tmpMax =
  539. ceil((s32) (pNode->max_ - f_Center), (s32) f_Step);
  540. if ((pNode->min_ < f_Desired) && (pNode->max_ > f_Desired))
  541. bDesiredExcluded = 1;
  542. if ((tmpMin < 0) && (tmpMax > 0))
  543. bZeroExcluded = 1;
  544. /* See if this zone overlaps the previous */
  545. if ((j > 0) && (tmpMin < zones[j - 1].max_))
  546. zones[j - 1].max_ = tmpMax;
  547. else {
  548. /* Add new zone */
  549. zones[j].min_ = tmpMin;
  550. zones[j].max_ = tmpMax;
  551. j++;
  552. }
  553. pNode = pNode->next_;
  554. }
  555. /*
  556. ** If the desired is okay, return with it
  557. */
  558. if (bDesiredExcluded == 0)
  559. return f_Desired;
  560. /*
  561. ** If the desired is excluded and the center is okay, return with it
  562. */
  563. if (bZeroExcluded == 0)
  564. return f_Center;
  565. /* Find the value closest to 0 (f_Center) */
  566. bestDiff = zones[0].min_;
  567. for (i = 0; i < j; i++) {
  568. if (abs(zones[i].min_) < abs(bestDiff))
  569. bestDiff = zones[i].min_;
  570. if (abs(zones[i].max_) < abs(bestDiff))
  571. bestDiff = zones[i].max_;
  572. }
  573. if (bestDiff < 0)
  574. return f_Center - ((u32) (-bestDiff) * f_Step);
  575. return f_Center + (bestDiff * f_Step);
  576. }
  577. /****************************************************************************
  578. **
  579. ** Name: gcd
  580. **
  581. ** Description: Uses Euclid's algorithm
  582. **
  583. ** Parameters: u, v - unsigned values whose GCD is desired.
  584. **
  585. ** Global: None
  586. **
  587. ** Returns: greatest common divisor of u and v, if either value
  588. ** is 0, the other value is returned as the result.
  589. **
  590. ** Dependencies: None.
  591. **
  592. ** Revision History:
  593. **
  594. ** SCR Date Author Description
  595. ** -------------------------------------------------------------------------
  596. ** N/A 06-01-2004 JWS Original
  597. ** N/A 08-03-2004 DAD Changed to Euclid's since it can handle
  598. ** unsigned numbers.
  599. **
  600. ****************************************************************************/
  601. static u32 MT2063_gcd(u32 u, u32 v)
  602. {
  603. u32 r;
  604. while (v != 0) {
  605. r = u % v;
  606. u = v;
  607. v = r;
  608. }
  609. return u;
  610. }
  611. /****************************************************************************
  612. **
  613. ** Name: IsSpurInBand
  614. **
  615. ** Description: Checks to see if a spur will be present within the IF's
  616. ** bandwidth. (fIFOut +/- fIFBW, -fIFOut +/- fIFBW)
  617. **
  618. ** ma mb mc md
  619. ** <--+-+-+-------------------+-------------------+-+-+-->
  620. ** | ^ 0 ^ |
  621. ** ^ b=-fIFOut+fIFBW/2 -b=+fIFOut-fIFBW/2 ^
  622. ** a=-fIFOut-fIFBW/2 -a=+fIFOut+fIFBW/2
  623. **
  624. ** Note that some equations are doubled to prevent round-off
  625. ** problems when calculating fIFBW/2
  626. **
  627. ** Parameters: pAS_Info - Avoid Spurs information block
  628. ** fm - If spur, amount f_IF1 has to move negative
  629. ** fp - If spur, amount f_IF1 has to move positive
  630. **
  631. ** Global: None
  632. **
  633. ** Returns: 1 if an LO spur would be present, otherwise 0.
  634. **
  635. ** Dependencies: None.
  636. **
  637. ** Revision History:
  638. **
  639. ** SCR Date Author Description
  640. ** -------------------------------------------------------------------------
  641. ** N/A 11-28-2002 DAD Implemented algorithm from applied patent
  642. **
  643. ****************************************************************************/
  644. static u32 IsSpurInBand(struct MT2063_AvoidSpursData_t *pAS_Info,
  645. u32 *fm, u32 * fp)
  646. {
  647. /*
  648. ** Calculate LO frequency settings.
  649. */
  650. u32 n, n0;
  651. const u32 f_LO1 = pAS_Info->f_LO1;
  652. const u32 f_LO2 = pAS_Info->f_LO2;
  653. const u32 d = pAS_Info->f_out + pAS_Info->f_out_bw / 2;
  654. const u32 c = d - pAS_Info->f_out_bw;
  655. const u32 f = pAS_Info->f_zif_bw / 2;
  656. const u32 f_Scale = (f_LO1 / (UINT_MAX / 2 / pAS_Info->maxH1)) + 1;
  657. s32 f_nsLO1, f_nsLO2;
  658. s32 f_Spur;
  659. u32 ma, mb, mc, md, me, mf;
  660. u32 lo_gcd, gd_Scale, gc_Scale, gf_Scale, hgds, hgfs, hgcs;
  661. *fm = 0;
  662. /*
  663. ** For each edge (d, c & f), calculate a scale, based on the gcd
  664. ** of f_LO1, f_LO2 and the edge value. Use the larger of this
  665. ** gcd-based scale factor or f_Scale.
  666. */
  667. lo_gcd = MT2063_gcd(f_LO1, f_LO2);
  668. gd_Scale = max((u32) MT2063_gcd(lo_gcd, d), f_Scale);
  669. hgds = gd_Scale / 2;
  670. gc_Scale = max((u32) MT2063_gcd(lo_gcd, c), f_Scale);
  671. hgcs = gc_Scale / 2;
  672. gf_Scale = max((u32) MT2063_gcd(lo_gcd, f), f_Scale);
  673. hgfs = gf_Scale / 2;
  674. n0 = DIV_ROUND_UP(f_LO2 - d, f_LO1 - f_LO2);
  675. /* Check out all multiples of LO1 from n0 to m_maxLOSpurHarmonic */
  676. for (n = n0; n <= pAS_Info->maxH1; ++n) {
  677. md = (n * ((f_LO1 + hgds) / gd_Scale) -
  678. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  679. /* If # fLO2 harmonics > m_maxLOSpurHarmonic, then no spurs present */
  680. if (md >= pAS_Info->maxH1)
  681. break;
  682. ma = (n * ((f_LO1 + hgds) / gd_Scale) +
  683. ((d + hgds) / gd_Scale)) / ((f_LO2 + hgds) / gd_Scale);
  684. /* If no spurs between +/- (f_out + f_IFBW/2), then try next harmonic */
  685. if (md == ma)
  686. continue;
  687. mc = (n * ((f_LO1 + hgcs) / gc_Scale) -
  688. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  689. if (mc != md) {
  690. f_nsLO1 = (s32) (n * (f_LO1 / gc_Scale));
  691. f_nsLO2 = (s32) (mc * (f_LO2 / gc_Scale));
  692. f_Spur =
  693. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  694. n * (f_LO1 % gc_Scale) - mc * (f_LO2 % gc_Scale);
  695. *fp = ((f_Spur - (s32) c) / (mc - n)) + 1;
  696. *fm = (((s32) d - f_Spur) / (mc - n)) + 1;
  697. return 1;
  698. }
  699. /* Location of Zero-IF-spur to be checked */
  700. me = (n * ((f_LO1 + hgfs) / gf_Scale) +
  701. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  702. mf = (n * ((f_LO1 + hgfs) / gf_Scale) -
  703. ((f + hgfs) / gf_Scale)) / ((f_LO2 + hgfs) / gf_Scale);
  704. if (me != mf) {
  705. f_nsLO1 = n * (f_LO1 / gf_Scale);
  706. f_nsLO2 = me * (f_LO2 / gf_Scale);
  707. f_Spur =
  708. (gf_Scale * (f_nsLO1 - f_nsLO2)) +
  709. n * (f_LO1 % gf_Scale) - me * (f_LO2 % gf_Scale);
  710. *fp = ((f_Spur + (s32) f) / (me - n)) + 1;
  711. *fm = (((s32) f - f_Spur) / (me - n)) + 1;
  712. return 1;
  713. }
  714. mb = (n * ((f_LO1 + hgcs) / gc_Scale) +
  715. ((c + hgcs) / gc_Scale)) / ((f_LO2 + hgcs) / gc_Scale);
  716. if (ma != mb) {
  717. f_nsLO1 = n * (f_LO1 / gc_Scale);
  718. f_nsLO2 = ma * (f_LO2 / gc_Scale);
  719. f_Spur =
  720. (gc_Scale * (f_nsLO1 - f_nsLO2)) +
  721. n * (f_LO1 % gc_Scale) - ma * (f_LO2 % gc_Scale);
  722. *fp = (((s32) d + f_Spur) / (ma - n)) + 1;
  723. *fm = (-(f_Spur + (s32) c) / (ma - n)) + 1;
  724. return 1;
  725. }
  726. }
  727. /* No spurs found */
  728. return 0;
  729. }
  730. /*****************************************************************************
  731. **
  732. ** Name: MT_AvoidSpurs
  733. **
  734. ** Description: Main entry point to avoid spurs.
  735. ** Checks for existing spurs in present LO1, LO2 freqs
  736. ** and if present, chooses spur-free LO1, LO2 combination
  737. ** that tunes the same input/output frequencies.
  738. **
  739. ** Revision History:
  740. **
  741. ** SCR Date Author Description
  742. ** -------------------------------------------------------------------------
  743. ** 096 04-06-2005 DAD Ver 1.11: Fix divide by 0 error if maxH==0.
  744. **
  745. *****************************************************************************/
  746. static u32 MT2063_AvoidSpurs(struct MT2063_AvoidSpursData_t *pAS_Info)
  747. {
  748. u32 status = 0;
  749. u32 fm, fp; /* restricted range on LO's */
  750. pAS_Info->bSpurAvoided = 0;
  751. pAS_Info->nSpursFound = 0;
  752. if (pAS_Info->maxH1 == 0)
  753. return 0;
  754. /*
  755. ** Avoid LO Generated Spurs
  756. **
  757. ** Make sure that have no LO-related spurs within the IF output
  758. ** bandwidth.
  759. **
  760. ** If there is an LO spur in this band, start at the current IF1 frequency
  761. ** and work out until we find a spur-free frequency or run up against the
  762. ** 1st IF SAW band edge. Use temporary copies of fLO1 and fLO2 so that they
  763. ** will be unchanged if a spur-free setting is not found.
  764. */
  765. pAS_Info->bSpurPresent = IsSpurInBand(pAS_Info, &fm, &fp);
  766. if (pAS_Info->bSpurPresent) {
  767. u32 zfIF1 = pAS_Info->f_LO1 - pAS_Info->f_in; /* current attempt at a 1st IF */
  768. u32 zfLO1 = pAS_Info->f_LO1; /* current attempt at an LO1 freq */
  769. u32 zfLO2 = pAS_Info->f_LO2; /* current attempt at an LO2 freq */
  770. u32 delta_IF1;
  771. u32 new_IF1;
  772. /*
  773. ** Spur was found, attempt to find a spur-free 1st IF
  774. */
  775. do {
  776. pAS_Info->nSpursFound++;
  777. /* Raise f_IF1_upper, if needed */
  778. MT2063_AddExclZone(pAS_Info, zfIF1 - fm, zfIF1 + fp);
  779. /* Choose next IF1 that is closest to f_IF1_CENTER */
  780. new_IF1 = MT2063_ChooseFirstIF(pAS_Info);
  781. if (new_IF1 > zfIF1) {
  782. pAS_Info->f_LO1 += (new_IF1 - zfIF1);
  783. pAS_Info->f_LO2 += (new_IF1 - zfIF1);
  784. } else {
  785. pAS_Info->f_LO1 -= (zfIF1 - new_IF1);
  786. pAS_Info->f_LO2 -= (zfIF1 - new_IF1);
  787. }
  788. zfIF1 = new_IF1;
  789. if (zfIF1 > pAS_Info->f_if1_Center)
  790. delta_IF1 = zfIF1 - pAS_Info->f_if1_Center;
  791. else
  792. delta_IF1 = pAS_Info->f_if1_Center - zfIF1;
  793. pAS_Info->bSpurPresent = IsSpurInBand(pAS_Info, &fm, &fp);
  794. /*
  795. ** Continue while the new 1st IF is still within the 1st IF bandwidth
  796. ** and there is a spur in the band (again)
  797. */
  798. } while ((2 * delta_IF1 + pAS_Info->f_out_bw <= pAS_Info->f_if1_bw) && pAS_Info->bSpurPresent);
  799. /*
  800. ** Use the LO-spur free values found. If the search went all the way to
  801. ** the 1st IF band edge and always found spurs, just leave the original
  802. ** choice. It's as "good" as any other.
  803. */
  804. if (pAS_Info->bSpurPresent == 1) {
  805. status |= MT2063_SPUR_PRESENT_ERR;
  806. pAS_Info->f_LO1 = zfLO1;
  807. pAS_Info->f_LO2 = zfLO2;
  808. } else
  809. pAS_Info->bSpurAvoided = 1;
  810. }
  811. status |=
  812. ((pAS_Info->
  813. nSpursFound << MT2063_SPUR_SHIFT) & MT2063_SPUR_CNT_MASK);
  814. return status;
  815. }
  816. /*
  817. * Constants used by the tuning algorithm
  818. */
  819. #define MT2063_REF_FREQ (16000000UL) /* Reference oscillator Frequency (in Hz) */
  820. #define MT2063_IF1_BW (22000000UL) /* The IF1 filter bandwidth (in Hz) */
  821. #define MT2063_TUNE_STEP_SIZE (50000UL) /* Tune in steps of 50 kHz */
  822. #define MT2063_SPUR_STEP_HZ (250000UL) /* Step size (in Hz) to move IF1 when avoiding spurs */
  823. #define MT2063_ZIF_BW (2000000UL) /* Zero-IF spur-free bandwidth (in Hz) */
  824. #define MT2063_MAX_HARMONICS_1 (15UL) /* Highest intra-tuner LO Spur Harmonic to be avoided */
  825. #define MT2063_MAX_HARMONICS_2 (5UL) /* Highest inter-tuner LO Spur Harmonic to be avoided */
  826. #define MT2063_MIN_LO_SEP (1000000UL) /* Minimum inter-tuner LO frequency separation */
  827. #define MT2063_LO1_FRACN_AVOID (0UL) /* LO1 FracN numerator avoid region (in Hz) */
  828. #define MT2063_LO2_FRACN_AVOID (199999UL) /* LO2 FracN numerator avoid region (in Hz) */
  829. #define MT2063_MIN_FIN_FREQ (44000000UL) /* Minimum input frequency (in Hz) */
  830. #define MT2063_MAX_FIN_FREQ (1100000000UL) /* Maximum input frequency (in Hz) */
  831. #define MT2063_MIN_FOUT_FREQ (36000000UL) /* Minimum output frequency (in Hz) */
  832. #define MT2063_MAX_FOUT_FREQ (57000000UL) /* Maximum output frequency (in Hz) */
  833. #define MT2063_MIN_DNC_FREQ (1293000000UL) /* Minimum LO2 frequency (in Hz) */
  834. #define MT2063_MAX_DNC_FREQ (1614000000UL) /* Maximum LO2 frequency (in Hz) */
  835. #define MT2063_MIN_UPC_FREQ (1396000000UL) /* Minimum LO1 frequency (in Hz) */
  836. #define MT2063_MAX_UPC_FREQ (2750000000UL) /* Maximum LO1 frequency (in Hz) */
  837. /*
  838. ** Define the supported Part/Rev codes for the MT2063
  839. */
  840. #define MT2063_B0 (0x9B)
  841. #define MT2063_B1 (0x9C)
  842. #define MT2063_B2 (0x9D)
  843. #define MT2063_B3 (0x9E)
  844. /*
  845. ** Constants for setting receiver modes.
  846. ** (6 modes defined at this time, enumerated by MT2063_RCVR_MODES)
  847. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  848. ** DNC Output is selected, the other is always off)
  849. **
  850. ** If PAL-L or L' is received, set:
  851. ** MT2063_SetParam(hMT2063,MT2063_TAGC,1);
  852. **
  853. ** --------------+----------------------------------------------
  854. ** Mode 0 : | MT2063_CABLE_QAM
  855. ** Mode 1 : | MT2063_CABLE_ANALOG
  856. ** Mode 2 : | MT2063_OFFAIR_COFDM
  857. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  858. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  859. ** Mode 5 : | MT2063_OFFAIR_8VSB
  860. ** --------------+----+----+----+----+-----+-----+--------------
  861. ** Mode | 0 | 1 | 2 | 3 | 4 | 5 |
  862. ** --------------+----+----+----+----+-----+-----+
  863. **
  864. **
  865. */
  866. static const u8 RFAGCEN[] = { 0, 0, 0, 0, 0, 0 };
  867. static const u8 LNARIN[] = { 0, 0, 3, 3, 3, 3 };
  868. static const u8 FIFFQEN[] = { 1, 1, 1, 1, 1, 1 };
  869. static const u8 FIFFQ[] = { 0, 0, 0, 0, 0, 0 };
  870. static const u8 DNC1GC[] = { 0, 0, 0, 0, 0, 0 };
  871. static const u8 DNC2GC[] = { 0, 0, 0, 0, 0, 0 };
  872. static const u8 ACLNAMAX[] = { 31, 31, 31, 31, 31, 31 };
  873. static const u8 LNATGT[] = { 44, 43, 43, 43, 43, 43 };
  874. static const u8 RFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  875. static const u8 ACRFMAX[] = { 31, 31, 31, 31, 31, 31 };
  876. static const u8 PD1TGT[] = { 36, 36, 38, 38, 36, 38 };
  877. static const u8 FIFOVDIS[] = { 0, 0, 0, 0, 0, 0 };
  878. static const u8 ACFIFMAX[] = { 29, 29, 29, 29, 29, 29 };
  879. static const u8 PD2TGT[] = { 40, 33, 38, 42, 30, 38 };
  880. /**
  881. * mt2063_lockStatus - Checks to see if LO1 and LO2 are locked
  882. *
  883. * @state: struct mt2063_state pointer
  884. *
  885. * This function returns 0, if no lock, 1 if locked and a value < 1 if error
  886. */
  887. unsigned int mt2063_lockStatus(struct mt2063_state *state)
  888. {
  889. const u32 nMaxWait = 100; /* wait a maximum of 100 msec */
  890. const u32 nPollRate = 2; /* poll status bits every 2 ms */
  891. const u32 nMaxLoops = nMaxWait / nPollRate;
  892. const u8 LO1LK = 0x80;
  893. u8 LO2LK = 0x08;
  894. u32 status;
  895. u32 nDelays = 0;
  896. /* LO2 Lock bit was in a different place for B0 version */
  897. if (state->tuner_id == MT2063_B0)
  898. LO2LK = 0x40;
  899. do {
  900. status = mt2063_read(state, MT2063_REG_LO_STATUS,
  901. &state->reg[MT2063_REG_LO_STATUS], 1);
  902. if (status < 0)
  903. return status;
  904. if ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) ==
  905. (LO1LK | LO2LK)) {
  906. return TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO;
  907. }
  908. msleep(nPollRate); /* Wait between retries */
  909. } while (++nDelays < nMaxLoops);
  910. /*
  911. * Got no lock or partial lock
  912. */
  913. return 0;
  914. }
  915. EXPORT_SYMBOL_GPL(mt2063_lockStatus);
  916. /*
  917. * mt2063_set_dnc_output_enable()
  918. */
  919. static u32 mt2063_get_dnc_output_enable(struct mt2063_state *state,
  920. enum MT2063_DNC_Output_Enable *pValue)
  921. {
  922. if ((state->reg[MT2063_REG_DNC_GAIN] & 0x03) == 0x03) { /* if DNC1 is off */
  923. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  924. *pValue = MT2063_DNC_NONE;
  925. else
  926. *pValue = MT2063_DNC_2;
  927. } else { /* DNC1 is on */
  928. if ((state->reg[MT2063_REG_VGA_GAIN] & 0x03) == 0x03) /* if DNC2 is off */
  929. *pValue = MT2063_DNC_1;
  930. else
  931. *pValue = MT2063_DNC_BOTH;
  932. }
  933. return 0;
  934. }
  935. /*
  936. * mt2063_set_dnc_output_enable()
  937. */
  938. static u32 mt2063_set_dnc_output_enable(struct mt2063_state *state,
  939. enum MT2063_DNC_Output_Enable nValue)
  940. {
  941. u32 status = 0; /* Status to be returned */
  942. u8 val = 0;
  943. /* selects, which DNC output is used */
  944. switch (nValue) {
  945. case MT2063_DNC_NONE:
  946. {
  947. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  948. if (state->reg[MT2063_REG_DNC_GAIN] !=
  949. val)
  950. status |=
  951. mt2063_setreg(state,
  952. MT2063_REG_DNC_GAIN,
  953. val);
  954. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  955. if (state->reg[MT2063_REG_VGA_GAIN] !=
  956. val)
  957. status |=
  958. mt2063_setreg(state,
  959. MT2063_REG_VGA_GAIN,
  960. val);
  961. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  962. if (state->reg[MT2063_REG_RSVD_20] !=
  963. val)
  964. status |=
  965. mt2063_setreg(state,
  966. MT2063_REG_RSVD_20,
  967. val);
  968. break;
  969. }
  970. case MT2063_DNC_1:
  971. {
  972. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  973. if (state->reg[MT2063_REG_DNC_GAIN] !=
  974. val)
  975. status |=
  976. mt2063_setreg(state,
  977. MT2063_REG_DNC_GAIN,
  978. val);
  979. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | 0x03; /* Set DNC2GC=3 */
  980. if (state->reg[MT2063_REG_VGA_GAIN] !=
  981. val)
  982. status |=
  983. mt2063_setreg(state,
  984. MT2063_REG_VGA_GAIN,
  985. val);
  986. val = (state->reg[MT2063_REG_RSVD_20] & ~0x40); /* Set PD2MUX=0 */
  987. if (state->reg[MT2063_REG_RSVD_20] !=
  988. val)
  989. status |=
  990. mt2063_setreg(state,
  991. MT2063_REG_RSVD_20,
  992. val);
  993. break;
  994. }
  995. case MT2063_DNC_2:
  996. {
  997. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | 0x03; /* Set DNC1GC=3 */
  998. if (state->reg[MT2063_REG_DNC_GAIN] !=
  999. val)
  1000. status |=
  1001. mt2063_setreg(state,
  1002. MT2063_REG_DNC_GAIN,
  1003. val);
  1004. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  1005. if (state->reg[MT2063_REG_VGA_GAIN] !=
  1006. val)
  1007. status |=
  1008. mt2063_setreg(state,
  1009. MT2063_REG_VGA_GAIN,
  1010. val);
  1011. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  1012. if (state->reg[MT2063_REG_RSVD_20] !=
  1013. val)
  1014. status |=
  1015. mt2063_setreg(state,
  1016. MT2063_REG_RSVD_20,
  1017. val);
  1018. break;
  1019. }
  1020. case MT2063_DNC_BOTH:
  1021. {
  1022. val = (state->reg[MT2063_REG_DNC_GAIN] & 0xFC) | (DNC1GC[state->rcvr_mode] & 0x03); /* Set DNC1GC=x */
  1023. if (state->reg[MT2063_REG_DNC_GAIN] !=
  1024. val)
  1025. status |=
  1026. mt2063_setreg(state,
  1027. MT2063_REG_DNC_GAIN,
  1028. val);
  1029. val = (state->reg[MT2063_REG_VGA_GAIN] & 0xFC) | (DNC2GC[state->rcvr_mode] & 0x03); /* Set DNC2GC=x */
  1030. if (state->reg[MT2063_REG_VGA_GAIN] !=
  1031. val)
  1032. status |=
  1033. mt2063_setreg(state,
  1034. MT2063_REG_VGA_GAIN,
  1035. val);
  1036. val = (state->reg[MT2063_REG_RSVD_20] | 0x40); /* Set PD2MUX=1 */
  1037. if (state->reg[MT2063_REG_RSVD_20] !=
  1038. val)
  1039. status |=
  1040. mt2063_setreg(state,
  1041. MT2063_REG_RSVD_20,
  1042. val);
  1043. break;
  1044. }
  1045. default:
  1046. break;
  1047. }
  1048. return status;
  1049. }
  1050. /******************************************************************************
  1051. **
  1052. ** Name: MT2063_SetReceiverMode
  1053. **
  1054. ** Description: Set the MT2063 receiver mode
  1055. **
  1056. ** --------------+----------------------------------------------
  1057. ** Mode 0 : | MT2063_CABLE_QAM
  1058. ** Mode 1 : | MT2063_CABLE_ANALOG
  1059. ** Mode 2 : | MT2063_OFFAIR_COFDM
  1060. ** Mode 3 : | MT2063_OFFAIR_COFDM_SAWLESS
  1061. ** Mode 4 : | MT2063_OFFAIR_ANALOG
  1062. ** Mode 5 : | MT2063_OFFAIR_8VSB
  1063. ** --------------+----+----+----+----+-----+--------------------
  1064. ** (DNC1GC & DNC2GC are the values, which are used, when the specific
  1065. ** DNC Output is selected, the other is always off)
  1066. **
  1067. ** |<---------- Mode -------------->|
  1068. ** Reg Field | 0 | 1 | 2 | 3 | 4 | 5 |
  1069. ** ------------+-----+-----+-----+-----+-----+-----+
  1070. ** RFAGCen | OFF | OFF | OFF | OFF | OFF | OFF
  1071. ** LNARin | 0 | 0 | 3 | 3 | 3 | 3
  1072. ** FIFFQen | 1 | 1 | 1 | 1 | 1 | 1
  1073. ** FIFFq | 0 | 0 | 0 | 0 | 0 | 0
  1074. ** DNC1gc | 0 | 0 | 0 | 0 | 0 | 0
  1075. ** DNC2gc | 0 | 0 | 0 | 0 | 0 | 0
  1076. ** GCU Auto | 1 | 1 | 1 | 1 | 1 | 1
  1077. ** LNA max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1078. ** LNA Target | 44 | 43 | 43 | 43 | 43 | 43
  1079. ** ign RF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1080. ** RF max Atn | 31 | 31 | 31 | 31 | 31 | 31
  1081. ** PD1 Target | 36 | 36 | 38 | 38 | 36 | 38
  1082. ** ign FIF Ovl | 0 | 0 | 0 | 0 | 0 | 0
  1083. ** FIF max Atn | 5 | 5 | 5 | 5 | 5 | 5
  1084. ** PD2 Target | 40 | 33 | 42 | 42 | 33 | 42
  1085. **
  1086. **
  1087. ** Parameters: state - ptr to mt2063_state structure
  1088. ** Mode - desired reciever mode
  1089. **
  1090. ** Usage: status = MT2063_SetReceiverMode(hMT2063, Mode);
  1091. **
  1092. ** Returns: status:
  1093. ** MT_OK - No errors
  1094. ** MT_COMM_ERR - Serial bus communications error
  1095. **
  1096. ** Dependencies: mt2063_setreg - Write a byte of data to a HW register.
  1097. ** Assumes that the tuner cache is valid.
  1098. **
  1099. ** Revision History:
  1100. **
  1101. ** SCR Date Author Description
  1102. ** -------------------------------------------------------------------------
  1103. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1104. ** N/A 01-10-2007 PINZ Added additional GCU Settings, FIFF Calib will be triggered
  1105. ** 155 10-01-2007 DAD Ver 1.06: Add receiver mode for SECAM positive
  1106. ** modulation
  1107. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1108. ** N/A 10-22-2007 PINZ Ver 1.07: Changed some Registers at init to have
  1109. ** the same settings as with MT Launcher
  1110. ** N/A 10-30-2007 PINZ Add SetParam VGAGC & VGAOI
  1111. ** Add SetParam DNC_OUTPUT_ENABLE
  1112. ** Removed VGAGC from receiver mode,
  1113. ** default now 1
  1114. ** N/A 10-31-2007 PINZ Ver 1.08: Add SetParam TAGC, removed from rcvr-mode
  1115. ** Add SetParam AMPGC, removed from rcvr-mode
  1116. ** Corrected names of GCU values
  1117. ** reorganized receiver modes, removed,
  1118. ** (MT2063_ANALOG_TV_POS_NO_RFAGC_MODE)
  1119. ** Actualized Receiver-Mode values
  1120. ** N/A 11-12-2007 PINZ Ver 1.09: Actualized Receiver-Mode values
  1121. ** N/A 11-27-2007 PINZ Improved buffered writing
  1122. ** 01-03-2008 PINZ Ver 1.10: Added a trigger of BYPATNUP for
  1123. ** correct wakeup of the LNA after shutdown
  1124. ** Set AFCsd = 1 as default
  1125. ** Changed CAP1sel default
  1126. ** 01-14-2008 PINZ Ver 1.11: Updated gain settings
  1127. ** 04-18-2008 PINZ Ver 1.15: Add SetParam LNARIN & PDxTGT
  1128. ** Split SetParam up to ACLNA / ACLNA_MAX
  1129. ** removed ACLNA_INRC/DECR (+RF & FIF)
  1130. ** removed GCUAUTO / BYPATNDN/UP
  1131. **
  1132. ******************************************************************************/
  1133. static u32 MT2063_SetReceiverMode(struct mt2063_state *state,
  1134. enum MT2063_RCVR_MODES Mode)
  1135. {
  1136. u32 status = 0; /* Status to be returned */
  1137. u8 val;
  1138. u32 longval;
  1139. if (Mode >= MT2063_NUM_RCVR_MODES)
  1140. status = -ERANGE;
  1141. /* RFAGCen */
  1142. if (status >= 0) {
  1143. val =
  1144. (state->
  1145. reg[MT2063_REG_PD1_TGT] & (u8) ~0x40) | (RFAGCEN[Mode]
  1146. ? 0x40 :
  1147. 0x00);
  1148. if (state->reg[MT2063_REG_PD1_TGT] != val)
  1149. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1150. }
  1151. /* LNARin */
  1152. if (status >= 0) {
  1153. u8 val = (state->reg[MT2063_REG_CTRL_2C] & (u8) ~0x03) |
  1154. (LNARIN[Mode] & 0x03);
  1155. if (state->reg[MT2063_REG_CTRL_2C] != val)
  1156. status |= mt2063_setreg(state, MT2063_REG_CTRL_2C, val);
  1157. }
  1158. /* FIFFQEN and FIFFQ */
  1159. if (status >= 0) {
  1160. val =
  1161. (state->
  1162. reg[MT2063_REG_FIFF_CTRL2] & (u8) ~0xF0) |
  1163. (FIFFQEN[Mode] << 7) | (FIFFQ[Mode] << 4);
  1164. if (state->reg[MT2063_REG_FIFF_CTRL2] != val) {
  1165. status |=
  1166. mt2063_setreg(state, MT2063_REG_FIFF_CTRL2, val);
  1167. /* trigger FIFF calibration, needed after changing FIFFQ */
  1168. val =
  1169. (state->reg[MT2063_REG_FIFF_CTRL] | (u8) 0x01);
  1170. status |=
  1171. mt2063_setreg(state, MT2063_REG_FIFF_CTRL, val);
  1172. val =
  1173. (state->
  1174. reg[MT2063_REG_FIFF_CTRL] & (u8) ~0x01);
  1175. status |=
  1176. mt2063_setreg(state, MT2063_REG_FIFF_CTRL, val);
  1177. }
  1178. }
  1179. /* DNC1GC & DNC2GC */
  1180. status |= mt2063_get_dnc_output_enable(state, &longval);
  1181. status |= mt2063_set_dnc_output_enable(state, longval);
  1182. /* acLNAmax */
  1183. if (status >= 0) {
  1184. u8 val = (state->reg[MT2063_REG_LNA_OV] & (u8) ~0x1F) |
  1185. (ACLNAMAX[Mode] & 0x1F);
  1186. if (state->reg[MT2063_REG_LNA_OV] != val)
  1187. status |= mt2063_setreg(state, MT2063_REG_LNA_OV, val);
  1188. }
  1189. /* LNATGT */
  1190. if (status >= 0) {
  1191. u8 val = (state->reg[MT2063_REG_LNA_TGT] & (u8) ~0x3F) |
  1192. (LNATGT[Mode] & 0x3F);
  1193. if (state->reg[MT2063_REG_LNA_TGT] != val)
  1194. status |= mt2063_setreg(state, MT2063_REG_LNA_TGT, val);
  1195. }
  1196. /* ACRF */
  1197. if (status >= 0) {
  1198. u8 val = (state->reg[MT2063_REG_RF_OV] & (u8) ~0x1F) |
  1199. (ACRFMAX[Mode] & 0x1F);
  1200. if (state->reg[MT2063_REG_RF_OV] != val)
  1201. status |= mt2063_setreg(state, MT2063_REG_RF_OV, val);
  1202. }
  1203. /* PD1TGT */
  1204. if (status >= 0) {
  1205. u8 val = (state->reg[MT2063_REG_PD1_TGT] & (u8) ~0x3F) |
  1206. (PD1TGT[Mode] & 0x3F);
  1207. if (state->reg[MT2063_REG_PD1_TGT] != val)
  1208. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1209. }
  1210. /* FIFATN */
  1211. if (status >= 0) {
  1212. u8 val = ACFIFMAX[Mode];
  1213. if (state->reg[MT2063_REG_PART_REV] != MT2063_B3 && val > 5)
  1214. val = 5;
  1215. val = (state->reg[MT2063_REG_FIF_OV] & (u8) ~0x1F) |
  1216. (val & 0x1F);
  1217. if (state->reg[MT2063_REG_FIF_OV] != val)
  1218. status |= mt2063_setreg(state, MT2063_REG_FIF_OV, val);
  1219. }
  1220. /* PD2TGT */
  1221. if (status >= 0) {
  1222. u8 val = (state->reg[MT2063_REG_PD2_TGT] & (u8) ~0x3F) |
  1223. (PD2TGT[Mode] & 0x3F);
  1224. if (state->reg[MT2063_REG_PD2_TGT] != val)
  1225. status |= mt2063_setreg(state, MT2063_REG_PD2_TGT, val);
  1226. }
  1227. /* Ignore ATN Overload */
  1228. if (status >= 0) {
  1229. val = (state->reg[MT2063_REG_LNA_TGT] & (u8) ~0x80) |
  1230. (RFOVDIS[Mode] ? 0x80 : 0x00);
  1231. if (state->reg[MT2063_REG_LNA_TGT] != val)
  1232. status |= mt2063_setreg(state, MT2063_REG_LNA_TGT, val);
  1233. }
  1234. /* Ignore FIF Overload */
  1235. if (status >= 0) {
  1236. val = (state->reg[MT2063_REG_PD1_TGT] & (u8) ~0x80) |
  1237. (FIFOVDIS[Mode] ? 0x80 : 0x00);
  1238. if (state->reg[MT2063_REG_PD1_TGT] != val)
  1239. status |= mt2063_setreg(state, MT2063_REG_PD1_TGT, val);
  1240. }
  1241. if (status >= 0)
  1242. state->rcvr_mode = Mode;
  1243. return status;
  1244. }
  1245. /****************************************************************************
  1246. **
  1247. ** Name: MT2063_ClearPowerMaskBits
  1248. **
  1249. ** Description: Clears the power-down mask bits for various sections of
  1250. ** the MT2063
  1251. **
  1252. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1253. ** Bits - Mask bits to be cleared.
  1254. **
  1255. ** See definition of MT2063_Mask_Bits type for description
  1256. ** of each of the power bits.
  1257. **
  1258. ** Returns: status:
  1259. ** MT_OK - No errors
  1260. ** MT_INV_HANDLE - Invalid tuner handle
  1261. ** MT_COMM_ERR - Serial bus communications error
  1262. **
  1263. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1264. **
  1265. ** Revision History:
  1266. **
  1267. ** SCR Date Author Description
  1268. ** -------------------------------------------------------------------------
  1269. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1270. **
  1271. ****************************************************************************/
  1272. static u32 MT2063_ClearPowerMaskBits(struct mt2063_state *state,
  1273. enum MT2063_Mask_Bits Bits)
  1274. {
  1275. u32 status = 0; /* Status to be returned */
  1276. Bits = (enum MT2063_Mask_Bits)(Bits & MT2063_ALL_SD); /* Only valid bits for this tuner */
  1277. if ((Bits & 0xFF00) != 0) {
  1278. state->reg[MT2063_REG_PWR_2] &= ~(u8) (Bits >> 8);
  1279. status |=
  1280. mt2063_write(state,
  1281. MT2063_REG_PWR_2,
  1282. &state->reg[MT2063_REG_PWR_2], 1);
  1283. }
  1284. if ((Bits & 0xFF) != 0) {
  1285. state->reg[MT2063_REG_PWR_1] &= ~(u8) (Bits & 0xFF);
  1286. status |=
  1287. mt2063_write(state,
  1288. MT2063_REG_PWR_1,
  1289. &state->reg[MT2063_REG_PWR_1], 1);
  1290. }
  1291. return status;
  1292. }
  1293. /****************************************************************************
  1294. **
  1295. ** Name: MT2063_SoftwareShutdown
  1296. **
  1297. ** Description: Enables or disables software shutdown function. When
  1298. ** Shutdown==1, any section whose power mask is set will be
  1299. ** shutdown.
  1300. **
  1301. ** Parameters: h - Tuner handle (returned by MT2063_Open)
  1302. ** Shutdown - 1 = shutdown the masked sections, otherwise
  1303. ** power all sections on
  1304. **
  1305. ** Returns: status:
  1306. ** MT_OK - No errors
  1307. ** MT_INV_HANDLE - Invalid tuner handle
  1308. ** MT_COMM_ERR - Serial bus communications error
  1309. **
  1310. ** Dependencies: USERS MUST CALL MT2063_Open() FIRST!
  1311. **
  1312. ** Revision History:
  1313. **
  1314. ** SCR Date Author Description
  1315. ** -------------------------------------------------------------------------
  1316. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1317. ** 01-03-2008 PINZ Ver 1.xx: Added a trigger of BYPATNUP for
  1318. ** correct wakeup of the LNA
  1319. **
  1320. ****************************************************************************/
  1321. static u32 MT2063_SoftwareShutdown(struct mt2063_state *state, u8 Shutdown)
  1322. {
  1323. u32 status; /* Status to be returned */
  1324. if (Shutdown == 1)
  1325. state->reg[MT2063_REG_PWR_1] |= 0x04; /* Turn the bit on */
  1326. else
  1327. state->reg[MT2063_REG_PWR_1] &= ~0x04; /* Turn off the bit */
  1328. status = mt2063_write(state,
  1329. MT2063_REG_PWR_1,
  1330. &state->reg[MT2063_REG_PWR_1], 1);
  1331. if (Shutdown != 1) {
  1332. state->reg[MT2063_REG_BYP_CTRL] =
  1333. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F) | 0x40;
  1334. status |=
  1335. mt2063_write(state,
  1336. MT2063_REG_BYP_CTRL,
  1337. &state->reg[MT2063_REG_BYP_CTRL],
  1338. 1);
  1339. state->reg[MT2063_REG_BYP_CTRL] =
  1340. (state->reg[MT2063_REG_BYP_CTRL] & 0x9F);
  1341. status |=
  1342. mt2063_write(state,
  1343. MT2063_REG_BYP_CTRL,
  1344. &state->reg[MT2063_REG_BYP_CTRL],
  1345. 1);
  1346. }
  1347. return status;
  1348. }
  1349. static u32 MT2063_Round_fLO(u32 f_LO, u32 f_LO_Step, u32 f_ref)
  1350. {
  1351. return f_ref * (f_LO / f_ref)
  1352. + f_LO_Step * (((f_LO % f_ref) + (f_LO_Step / 2)) / f_LO_Step);
  1353. }
  1354. /****************************************************************************
  1355. **
  1356. ** Name: fLO_FractionalTerm
  1357. **
  1358. ** Description: Calculates the portion contributed by FracN / denom.
  1359. **
  1360. ** This function preserves maximum precision without
  1361. ** risk of overflow. It accurately calculates
  1362. ** f_ref * num / denom to within 1 HZ with fixed math.
  1363. **
  1364. ** Parameters: num - Fractional portion of the multiplier
  1365. ** denom - denominator portion of the ratio
  1366. ** This routine successfully handles denom values
  1367. ** up to and including 2^18.
  1368. ** f_Ref - SRO frequency. This calculation handles
  1369. ** f_ref as two separate 14-bit fields.
  1370. ** Therefore, a maximum value of 2^28-1
  1371. ** may safely be used for f_ref. This is
  1372. ** the genesis of the magic number "14" and the
  1373. ** magic mask value of 0x03FFF.
  1374. **
  1375. ** Returns: f_ref * num / denom
  1376. **
  1377. ** Revision History:
  1378. **
  1379. ** SCR Date Author Description
  1380. ** -------------------------------------------------------------------------
  1381. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1382. **
  1383. ****************************************************************************/
  1384. static u32 MT2063_fLO_FractionalTerm(u32 f_ref, u32 num, u32 denom)
  1385. {
  1386. u32 t1 = (f_ref >> 14) * num;
  1387. u32 term1 = t1 / denom;
  1388. u32 loss = t1 % denom;
  1389. u32 term2 =
  1390. (((f_ref & 0x00003FFF) * num + (loss << 14)) + (denom / 2)) / denom;
  1391. return (term1 << 14) + term2;
  1392. }
  1393. /****************************************************************************
  1394. **
  1395. ** Name: CalcLO1Mult
  1396. **
  1397. ** Description: Calculates Integer divider value and the numerator
  1398. ** value for a FracN PLL.
  1399. **
  1400. ** This function assumes that the f_LO and f_Ref are
  1401. ** evenly divisible by f_LO_Step.
  1402. **
  1403. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  1404. ** FracN - OUTPUT: Fractional portion of the multiplier
  1405. ** f_LO - desired LO frequency.
  1406. ** f_LO_Step - Minimum step size for the LO (in Hz).
  1407. ** f_Ref - SRO frequency.
  1408. ** f_Avoid - Range of PLL frequencies to avoid near
  1409. ** integer multiples of f_Ref (in Hz).
  1410. **
  1411. ** Returns: Recalculated LO frequency.
  1412. **
  1413. ** Revision History:
  1414. **
  1415. ** SCR Date Author Description
  1416. ** -------------------------------------------------------------------------
  1417. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1418. **
  1419. ****************************************************************************/
  1420. static u32 MT2063_CalcLO1Mult(u32 *Div,
  1421. u32 *FracN,
  1422. u32 f_LO,
  1423. u32 f_LO_Step, u32 f_Ref)
  1424. {
  1425. /* Calculate the whole number portion of the divider */
  1426. *Div = f_LO / f_Ref;
  1427. /* Calculate the numerator value (round to nearest f_LO_Step) */
  1428. *FracN =
  1429. (64 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  1430. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  1431. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN, 64);
  1432. }
  1433. /****************************************************************************
  1434. **
  1435. ** Name: CalcLO2Mult
  1436. **
  1437. ** Description: Calculates Integer divider value and the numerator
  1438. ** value for a FracN PLL.
  1439. **
  1440. ** This function assumes that the f_LO and f_Ref are
  1441. ** evenly divisible by f_LO_Step.
  1442. **
  1443. ** Parameters: Div - OUTPUT: Whole number portion of the multiplier
  1444. ** FracN - OUTPUT: Fractional portion of the multiplier
  1445. ** f_LO - desired LO frequency.
  1446. ** f_LO_Step - Minimum step size for the LO (in Hz).
  1447. ** f_Ref - SRO frequency.
  1448. ** f_Avoid - Range of PLL frequencies to avoid near
  1449. ** integer multiples of f_Ref (in Hz).
  1450. **
  1451. ** Returns: Recalculated LO frequency.
  1452. **
  1453. ** Revision History:
  1454. **
  1455. ** SCR Date Author Description
  1456. ** -------------------------------------------------------------------------
  1457. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1458. **
  1459. ****************************************************************************/
  1460. static u32 MT2063_CalcLO2Mult(u32 *Div,
  1461. u32 *FracN,
  1462. u32 f_LO,
  1463. u32 f_LO_Step, u32 f_Ref)
  1464. {
  1465. /* Calculate the whole number portion of the divider */
  1466. *Div = f_LO / f_Ref;
  1467. /* Calculate the numerator value (round to nearest f_LO_Step) */
  1468. *FracN =
  1469. (8191 * (((f_LO % f_Ref) + (f_LO_Step / 2)) / f_LO_Step) +
  1470. (f_Ref / f_LO_Step / 2)) / (f_Ref / f_LO_Step);
  1471. return (f_Ref * (*Div)) + MT2063_fLO_FractionalTerm(f_Ref, *FracN,
  1472. 8191);
  1473. }
  1474. /****************************************************************************
  1475. **
  1476. ** Name: FindClearTuneFilter
  1477. **
  1478. ** Description: Calculate the corrrect ClearTune filter to be used for
  1479. ** a given input frequency.
  1480. **
  1481. ** Parameters: state - ptr to tuner data structure
  1482. ** f_in - RF input center frequency (in Hz).
  1483. **
  1484. ** Returns: ClearTune filter number (0-31)
  1485. **
  1486. ** Dependencies: MUST CALL MT2064_Open BEFORE FindClearTuneFilter!
  1487. **
  1488. ** Revision History:
  1489. **
  1490. ** SCR Date Author Description
  1491. ** -------------------------------------------------------------------------
  1492. ** 04-10-2008 PINZ Ver 1.14: Use software-controlled ClearTune
  1493. ** cross-over frequency values.
  1494. **
  1495. ****************************************************************************/
  1496. static u32 FindClearTuneFilter(struct mt2063_state *state, u32 f_in)
  1497. {
  1498. u32 RFBand;
  1499. u32 idx; /* index loop */
  1500. /*
  1501. ** Find RF Band setting
  1502. */
  1503. RFBand = 31; /* def when f_in > all */
  1504. for (idx = 0; idx < 31; ++idx) {
  1505. if (state->CTFiltMax[idx] >= f_in) {
  1506. RFBand = idx;
  1507. break;
  1508. }
  1509. }
  1510. return RFBand;
  1511. }
  1512. /****************************************************************************
  1513. **
  1514. ** Name: MT2063_Tune
  1515. **
  1516. ** Description: Change the tuner's tuned frequency to RFin.
  1517. **
  1518. ** Parameters: h - Open handle to the tuner (from MT2063_Open).
  1519. ** f_in - RF input center frequency (in Hz).
  1520. **
  1521. ** Returns: status:
  1522. ** MT_OK - No errors
  1523. ** MT_INV_HANDLE - Invalid tuner handle
  1524. ** MT_UPC_UNLOCK - Upconverter PLL unlocked
  1525. ** MT_DNC_UNLOCK - Downconverter PLL unlocked
  1526. ** MT_COMM_ERR - Serial bus communications error
  1527. ** MT_SPUR_CNT_MASK - Count of avoided LO spurs
  1528. ** MT_SPUR_PRESENT - LO spur possible in output
  1529. ** MT_FIN_RANGE - Input freq out of range
  1530. ** MT_FOUT_RANGE - Output freq out of range
  1531. ** MT_UPC_RANGE - Upconverter freq out of range
  1532. ** MT_DNC_RANGE - Downconverter freq out of range
  1533. **
  1534. ** Dependencies: MUST CALL MT2063_Open BEFORE MT2063_Tune!
  1535. **
  1536. ** MT_ReadSub - Read data from the two-wire serial bus
  1537. ** MT_WriteSub - Write data to the two-wire serial bus
  1538. ** MT_Sleep - Delay execution for x milliseconds
  1539. ** MT2063_GetLocked - Checks to see if LO1 and LO2 are locked
  1540. **
  1541. ** Revision History:
  1542. **
  1543. ** SCR Date Author Description
  1544. ** -------------------------------------------------------------------------
  1545. ** 138 06-19-2007 DAD Ver 1.00: Initial, derived from mt2067_b.
  1546. ** 04-10-2008 PINZ Ver 1.05: Use software-controlled ClearTune
  1547. ** cross-over frequency values.
  1548. ** 175 I 16-06-2008 PINZ Ver 1.16: Add control to avoid US DECT freqs.
  1549. ** 175 I 06-19-2008 RSK Ver 1.17: Refactor DECT control to SpurAvoid.
  1550. ** 06-24-2008 PINZ Ver 1.18: Add Get/SetParam CTFILT_SW
  1551. **
  1552. ****************************************************************************/
  1553. static u32 MT2063_Tune(struct mt2063_state *state, u32 f_in)
  1554. { /* RF input center frequency */
  1555. u32 status = 0; /* status of operation */
  1556. u32 LO1; /* 1st LO register value */
  1557. u32 Num1; /* Numerator for LO1 reg. value */
  1558. u32 f_IF1; /* 1st IF requested */
  1559. u32 LO2; /* 2nd LO register value */
  1560. u32 Num2; /* Numerator for LO2 reg. value */
  1561. u32 ofLO1, ofLO2; /* last time's LO frequencies */
  1562. u32 ofin, ofout; /* last time's I/O frequencies */
  1563. u8 fiffc = 0x80; /* FIFF center freq from tuner */
  1564. u32 fiffof; /* Offset from FIFF center freq */
  1565. const u8 LO1LK = 0x80; /* Mask for LO1 Lock bit */
  1566. u8 LO2LK = 0x08; /* Mask for LO2 Lock bit */
  1567. u8 val;
  1568. u32 RFBand;
  1569. /* Check the input and output frequency ranges */
  1570. if ((f_in < MT2063_MIN_FIN_FREQ) || (f_in > MT2063_MAX_FIN_FREQ))
  1571. return -EINVAL;
  1572. if ((state->AS_Data.f_out < MT2063_MIN_FOUT_FREQ)
  1573. || (state->AS_Data.f_out > MT2063_MAX_FOUT_FREQ))
  1574. return -EINVAL;
  1575. /*
  1576. ** Save original LO1 and LO2 register values
  1577. */
  1578. ofLO1 = state->AS_Data.f_LO1;
  1579. ofLO2 = state->AS_Data.f_LO2;
  1580. ofin = state->AS_Data.f_in;
  1581. ofout = state->AS_Data.f_out;
  1582. /*
  1583. ** Find and set RF Band setting
  1584. */
  1585. if (state->ctfilt_sw == 1) {
  1586. val = (state->reg[MT2063_REG_CTUNE_CTRL] | 0x08);
  1587. if (state->reg[MT2063_REG_CTUNE_CTRL] != val) {
  1588. status |=
  1589. mt2063_setreg(state, MT2063_REG_CTUNE_CTRL, val);
  1590. }
  1591. val = state->reg[MT2063_REG_CTUNE_OV];
  1592. RFBand = FindClearTuneFilter(state, f_in);
  1593. state->reg[MT2063_REG_CTUNE_OV] =
  1594. (u8) ((state->reg[MT2063_REG_CTUNE_OV] & ~0x1F)
  1595. | RFBand);
  1596. if (state->reg[MT2063_REG_CTUNE_OV] != val) {
  1597. status |=
  1598. mt2063_setreg(state, MT2063_REG_CTUNE_OV, val);
  1599. }
  1600. }
  1601. /*
  1602. ** Read the FIFF Center Frequency from the tuner
  1603. */
  1604. if (status >= 0) {
  1605. status |=
  1606. mt2063_read(state,
  1607. MT2063_REG_FIFFC,
  1608. &state->reg[MT2063_REG_FIFFC], 1);
  1609. fiffc = state->reg[MT2063_REG_FIFFC];
  1610. }
  1611. /*
  1612. ** Assign in the requested values
  1613. */
  1614. state->AS_Data.f_in = f_in;
  1615. /* Request a 1st IF such that LO1 is on a step size */
  1616. state->AS_Data.f_if1_Request =
  1617. MT2063_Round_fLO(state->AS_Data.f_if1_Request + f_in,
  1618. state->AS_Data.f_LO1_Step,
  1619. state->AS_Data.f_ref) - f_in;
  1620. /*
  1621. ** Calculate frequency settings. f_IF1_FREQ + f_in is the
  1622. ** desired LO1 frequency
  1623. */
  1624. MT2063_ResetExclZones(&state->AS_Data);
  1625. f_IF1 = MT2063_ChooseFirstIF(&state->AS_Data);
  1626. state->AS_Data.f_LO1 =
  1627. MT2063_Round_fLO(f_IF1 + f_in, state->AS_Data.f_LO1_Step,
  1628. state->AS_Data.f_ref);
  1629. state->AS_Data.f_LO2 =
  1630. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  1631. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1632. /*
  1633. ** Check for any LO spurs in the output bandwidth and adjust
  1634. ** the LO settings to avoid them if needed
  1635. */
  1636. status |= MT2063_AvoidSpurs(&state->AS_Data);
  1637. /*
  1638. ** MT_AvoidSpurs spurs may have changed the LO1 & LO2 values.
  1639. ** Recalculate the LO frequencies and the values to be placed
  1640. ** in the tuning registers.
  1641. */
  1642. state->AS_Data.f_LO1 =
  1643. MT2063_CalcLO1Mult(&LO1, &Num1, state->AS_Data.f_LO1,
  1644. state->AS_Data.f_LO1_Step, state->AS_Data.f_ref);
  1645. state->AS_Data.f_LO2 =
  1646. MT2063_Round_fLO(state->AS_Data.f_LO1 - state->AS_Data.f_out - f_in,
  1647. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1648. state->AS_Data.f_LO2 =
  1649. MT2063_CalcLO2Mult(&LO2, &Num2, state->AS_Data.f_LO2,
  1650. state->AS_Data.f_LO2_Step, state->AS_Data.f_ref);
  1651. /*
  1652. ** Check the upconverter and downconverter frequency ranges
  1653. */
  1654. if ((state->AS_Data.f_LO1 < MT2063_MIN_UPC_FREQ)
  1655. || (state->AS_Data.f_LO1 > MT2063_MAX_UPC_FREQ))
  1656. status |= MT2063_UPC_RANGE;
  1657. if ((state->AS_Data.f_LO2 < MT2063_MIN_DNC_FREQ)
  1658. || (state->AS_Data.f_LO2 > MT2063_MAX_DNC_FREQ))
  1659. status |= MT2063_DNC_RANGE;
  1660. /* LO2 Lock bit was in a different place for B0 version */
  1661. if (state->tuner_id == MT2063_B0)
  1662. LO2LK = 0x40;
  1663. /*
  1664. ** If we have the same LO frequencies and we're already locked,
  1665. ** then skip re-programming the LO registers.
  1666. */
  1667. if ((ofLO1 != state->AS_Data.f_LO1)
  1668. || (ofLO2 != state->AS_Data.f_LO2)
  1669. || ((state->reg[MT2063_REG_LO_STATUS] & (LO1LK | LO2LK)) !=
  1670. (LO1LK | LO2LK))) {
  1671. /*
  1672. ** Calculate the FIFFOF register value
  1673. **
  1674. ** IF1_Actual
  1675. ** FIFFOF = ------------ - 8 * FIFFC - 4992
  1676. ** f_ref/64
  1677. */
  1678. fiffof =
  1679. (state->AS_Data.f_LO1 -
  1680. f_in) / (state->AS_Data.f_ref / 64) - 8 * (u32) fiffc -
  1681. 4992;
  1682. if (fiffof > 0xFF)
  1683. fiffof = 0xFF;
  1684. /*
  1685. ** Place all of the calculated values into the local tuner
  1686. ** register fields.
  1687. */
  1688. if (status >= 0) {
  1689. state->reg[MT2063_REG_LO1CQ_1] = (u8) (LO1 & 0xFF); /* DIV1q */
  1690. state->reg[MT2063_REG_LO1CQ_2] = (u8) (Num1 & 0x3F); /* NUM1q */
  1691. state->reg[MT2063_REG_LO2CQ_1] = (u8) (((LO2 & 0x7F) << 1) /* DIV2q */
  1692. |(Num2 >> 12)); /* NUM2q (hi) */
  1693. state->reg[MT2063_REG_LO2CQ_2] = (u8) ((Num2 & 0x0FF0) >> 4); /* NUM2q (mid) */
  1694. state->reg[MT2063_REG_LO2CQ_3] = (u8) (0xE0 | (Num2 & 0x000F)); /* NUM2q (lo) */
  1695. /*
  1696. ** Now write out the computed register values
  1697. ** IMPORTANT: There is a required order for writing
  1698. ** (0x05 must follow all the others).
  1699. */
  1700. status |= mt2063_write(state, MT2063_REG_LO1CQ_1, &state->reg[MT2063_REG_LO1CQ_1], 5); /* 0x01 - 0x05 */
  1701. if (state->tuner_id == MT2063_B0) {
  1702. /* Re-write the one-shot bits to trigger the tune operation */
  1703. status |= mt2063_write(state, MT2063_REG_LO2CQ_3, &state->reg[MT2063_REG_LO2CQ_3], 1); /* 0x05 */
  1704. }
  1705. /* Write out the FIFF offset only if it's changing */
  1706. if (state->reg[MT2063_REG_FIFF_OFFSET] !=
  1707. (u8) fiffof) {
  1708. state->reg[MT2063_REG_FIFF_OFFSET] =
  1709. (u8) fiffof;
  1710. status |=
  1711. mt2063_write(state,
  1712. MT2063_REG_FIFF_OFFSET,
  1713. &state->
  1714. reg[MT2063_REG_FIFF_OFFSET],
  1715. 1);
  1716. }
  1717. }
  1718. /*
  1719. ** Check for LO's locking
  1720. */
  1721. if (status < 0)
  1722. return status;
  1723. status = mt2063_lockStatus(state);
  1724. if (status < 0)
  1725. return status;
  1726. if (!status)
  1727. return -EINVAL; /* Couldn't lock */
  1728. /*
  1729. * If we locked OK, assign calculated data to mt2063_state structure
  1730. */
  1731. state->f_IF1_actual = state->AS_Data.f_LO1 - f_in;
  1732. }
  1733. return status;
  1734. }
  1735. int mt2063_setTune(struct dvb_frontend *fe, u32 f_in, u32 bw_in,
  1736. enum MTTune_atv_standard tv_type)
  1737. {
  1738. struct mt2063_state *state = fe->tuner_priv;
  1739. u32 status = 0;
  1740. s32 pict_car = 0;
  1741. s32 pict2chanb_vsb = 0;
  1742. s32 pict2chanb_snd = 0;
  1743. s32 pict2snd1 = 0;
  1744. s32 pict2snd2 = 0;
  1745. s32 ch_bw = 0;
  1746. s32 if_mid = 0;
  1747. s32 rcvr_mode = 0;
  1748. switch (tv_type) {
  1749. case MTTUNEA_PAL_B:{
  1750. pict_car = 38900000;
  1751. ch_bw = 8000000;
  1752. pict2chanb_vsb = -1250000;
  1753. pict2snd1 = 5500000;
  1754. pict2snd2 = 5742000;
  1755. rcvr_mode = 1;
  1756. break;
  1757. }
  1758. case MTTUNEA_PAL_G:{
  1759. pict_car = 38900000;
  1760. ch_bw = 7000000;
  1761. pict2chanb_vsb = -1250000;
  1762. pict2snd1 = 5500000;
  1763. pict2snd2 = 0;
  1764. rcvr_mode = 1;
  1765. break;
  1766. }
  1767. case MTTUNEA_PAL_I:{
  1768. pict_car = 38900000;
  1769. ch_bw = 8000000;
  1770. pict2chanb_vsb = -1250000;
  1771. pict2snd1 = 6000000;
  1772. pict2snd2 = 0;
  1773. rcvr_mode = 1;
  1774. break;
  1775. }
  1776. case MTTUNEA_PAL_L:{
  1777. pict_car = 38900000;
  1778. ch_bw = 8000000;
  1779. pict2chanb_vsb = -1250000;
  1780. pict2snd1 = 6500000;
  1781. pict2snd2 = 0;
  1782. rcvr_mode = 1;
  1783. break;
  1784. }
  1785. case MTTUNEA_PAL_MN:{
  1786. pict_car = 38900000;
  1787. ch_bw = 6000000;
  1788. pict2chanb_vsb = -1250000;
  1789. pict2snd1 = 4500000;
  1790. pict2snd2 = 0;
  1791. rcvr_mode = 1;
  1792. break;
  1793. }
  1794. case MTTUNEA_PAL_DK:{
  1795. pict_car = 38900000;
  1796. ch_bw = 8000000;
  1797. pict2chanb_vsb = -1250000;
  1798. pict2snd1 = 6500000;
  1799. pict2snd2 = 0;
  1800. rcvr_mode = 1;
  1801. break;
  1802. }
  1803. case MTTUNEA_DIGITAL:{
  1804. pict_car = 36125000;
  1805. ch_bw = 8000000;
  1806. pict2chanb_vsb = -(ch_bw / 2);
  1807. pict2snd1 = 0;
  1808. pict2snd2 = 0;
  1809. rcvr_mode = 2;
  1810. break;
  1811. }
  1812. case MTTUNEA_FMRADIO:{
  1813. pict_car = 38900000;
  1814. ch_bw = 8000000;
  1815. pict2chanb_vsb = -(ch_bw / 2);
  1816. pict2snd1 = 0;
  1817. pict2snd2 = 0;
  1818. rcvr_mode = 4;
  1819. break;
  1820. }
  1821. case MTTUNEA_DVBC:{
  1822. pict_car = 36125000;
  1823. ch_bw = 8000000;
  1824. pict2chanb_vsb = -(ch_bw / 2);
  1825. pict2snd1 = 0;
  1826. pict2snd2 = 0;
  1827. rcvr_mode = MT2063_CABLE_QAM;
  1828. break;
  1829. }
  1830. case MTTUNEA_DVBT:{
  1831. pict_car = 36125000;
  1832. ch_bw = bw_in;
  1833. pict2chanb_vsb = -(ch_bw / 2);
  1834. pict2snd1 = 0;
  1835. pict2snd2 = 0;
  1836. rcvr_mode = MT2063_OFFAIR_COFDM;
  1837. break;
  1838. }
  1839. case MTTUNEA_UNKNOWN:
  1840. break;
  1841. default:
  1842. break;
  1843. }
  1844. pict2chanb_snd = pict2chanb_vsb - ch_bw;
  1845. if_mid = pict_car - (pict2chanb_vsb + (ch_bw / 2));
  1846. state->AS_Data.f_LO2_Step = 125000;
  1847. state->AS_Data.f_out = if_mid;
  1848. state->AS_Data.f_out_bw = ch_bw + 750000;
  1849. status = MT2063_SetReceiverMode(state, rcvr_mode);
  1850. if (status < 0)
  1851. return status;
  1852. status = MT2063_Tune(state, (f_in + (pict2chanb_vsb + (ch_bw / 2))));
  1853. return status;
  1854. }
  1855. static const u8 MT2063B0_defaults[] = {
  1856. /* Reg, Value */
  1857. 0x19, 0x05,
  1858. 0x1B, 0x1D,
  1859. 0x1C, 0x1F,
  1860. 0x1D, 0x0F,
  1861. 0x1E, 0x3F,
  1862. 0x1F, 0x0F,
  1863. 0x20, 0x3F,
  1864. 0x22, 0x21,
  1865. 0x23, 0x3F,
  1866. 0x24, 0x20,
  1867. 0x25, 0x3F,
  1868. 0x27, 0xEE,
  1869. 0x2C, 0x27, /* bit at 0x20 is cleared below */
  1870. 0x30, 0x03,
  1871. 0x2C, 0x07, /* bit at 0x20 is cleared here */
  1872. 0x2D, 0x87,
  1873. 0x2E, 0xAA,
  1874. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1875. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1876. 0x00
  1877. };
  1878. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  1879. static const u8 MT2063B1_defaults[] = {
  1880. /* Reg, Value */
  1881. 0x05, 0xF0,
  1882. 0x11, 0x10, /* New Enable AFCsd */
  1883. 0x19, 0x05,
  1884. 0x1A, 0x6C,
  1885. 0x1B, 0x24,
  1886. 0x1C, 0x28,
  1887. 0x1D, 0x8F,
  1888. 0x1E, 0x14,
  1889. 0x1F, 0x8F,
  1890. 0x20, 0x57,
  1891. 0x22, 0x21, /* New - ver 1.03 */
  1892. 0x23, 0x3C, /* New - ver 1.10 */
  1893. 0x24, 0x20, /* New - ver 1.03 */
  1894. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  1895. 0x2D, 0x87, /* FIFFQ=0 */
  1896. 0x2F, 0xF3,
  1897. 0x30, 0x0C, /* New - ver 1.11 */
  1898. 0x31, 0x1B, /* New - ver 1.11 */
  1899. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  1900. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1901. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1902. 0x00
  1903. };
  1904. /* writing 0x05 0xf0 sw-resets all registers, so we write only needed changes */
  1905. static const u8 MT2063B3_defaults[] = {
  1906. /* Reg, Value */
  1907. 0x05, 0xF0,
  1908. 0x19, 0x3D,
  1909. 0x2C, 0x24, /* bit at 0x20 is cleared below */
  1910. 0x2C, 0x04, /* bit at 0x20 is cleared here */
  1911. 0x28, 0xE1, /* Set the FIFCrst bit here */
  1912. 0x28, 0xE0, /* Clear the FIFCrst bit here */
  1913. 0x00
  1914. };
  1915. static int mt2063_init(struct dvb_frontend *fe)
  1916. {
  1917. u32 status;
  1918. struct mt2063_state *state = fe->tuner_priv;
  1919. u8 all_resets = 0xF0; /* reset/load bits */
  1920. const u8 *def = NULL;
  1921. u32 FCRUN;
  1922. s32 maxReads;
  1923. u32 fcu_osc;
  1924. u32 i;
  1925. state->rcvr_mode = MT2063_CABLE_QAM;
  1926. /* Read the Part/Rev code from the tuner */
  1927. status = mt2063_read(state, MT2063_REG_PART_REV, state->reg, 1);
  1928. if (status < 0)
  1929. return status;
  1930. /* Check the part/rev code */
  1931. if (((state->reg[MT2063_REG_PART_REV] != MT2063_B0) /* MT2063 B0 */
  1932. && (state->reg[MT2063_REG_PART_REV] != MT2063_B1) /* MT2063 B1 */
  1933. && (state->reg[MT2063_REG_PART_REV] != MT2063_B3))) /* MT2063 B3 */
  1934. return -ENODEV; /* Wrong tuner Part/Rev code */
  1935. /* Check the 2nd byte of the Part/Rev code from the tuner */
  1936. status = mt2063_read(state, MT2063_REG_RSVD_3B,
  1937. &state->reg[MT2063_REG_RSVD_3B], 1);
  1938. /* b7 != 0 ==> NOT MT2063 */
  1939. if (status < 0 || ((state->reg[MT2063_REG_RSVD_3B] & 0x80) != 0x00))
  1940. return -ENODEV; /* Wrong tuner Part/Rev code */
  1941. /* Reset the tuner */
  1942. status = mt2063_write(state, MT2063_REG_LO2CQ_3, &all_resets, 1);
  1943. if (status < 0)
  1944. return status;
  1945. /* change all of the default values that vary from the HW reset values */
  1946. /* def = (state->reg[PART_REV] == MT2063_B0) ? MT2063B0_defaults : MT2063B1_defaults; */
  1947. switch (state->reg[MT2063_REG_PART_REV]) {
  1948. case MT2063_B3:
  1949. def = MT2063B3_defaults;
  1950. break;
  1951. case MT2063_B1:
  1952. def = MT2063B1_defaults;
  1953. break;
  1954. case MT2063_B0:
  1955. def = MT2063B0_defaults;
  1956. break;
  1957. default:
  1958. return -ENODEV;
  1959. break;
  1960. }
  1961. while (status >= 0 && *def) {
  1962. u8 reg = *def++;
  1963. u8 val = *def++;
  1964. status = mt2063_write(state, reg, &val, 1);
  1965. }
  1966. if (status < 0)
  1967. return status;
  1968. /* Wait for FIFF location to complete. */
  1969. FCRUN = 1;
  1970. maxReads = 10;
  1971. while (status >= 0 && (FCRUN != 0) && (maxReads-- > 0)) {
  1972. msleep(2);
  1973. status = mt2063_read(state,
  1974. MT2063_REG_XO_STATUS,
  1975. &state->
  1976. reg[MT2063_REG_XO_STATUS], 1);
  1977. FCRUN = (state->reg[MT2063_REG_XO_STATUS] & 0x40) >> 6;
  1978. }
  1979. if (FCRUN != 0 || status < 0)
  1980. return -ENODEV;
  1981. status = mt2063_read(state,
  1982. MT2063_REG_FIFFC,
  1983. &state->reg[MT2063_REG_FIFFC], 1);
  1984. if (status < 0)
  1985. return status;
  1986. /* Read back all the registers from the tuner */
  1987. status = mt2063_read(state,
  1988. MT2063_REG_PART_REV,
  1989. state->reg, MT2063_REG_END_REGS);
  1990. if (status < 0)
  1991. return status;
  1992. /* Initialize the tuner state. */
  1993. state->tuner_id = state->reg[MT2063_REG_PART_REV];
  1994. state->AS_Data.f_ref = MT2063_REF_FREQ;
  1995. state->AS_Data.f_if1_Center = (state->AS_Data.f_ref / 8) *
  1996. ((u32) state->reg[MT2063_REG_FIFFC] + 640);
  1997. state->AS_Data.f_if1_bw = MT2063_IF1_BW;
  1998. state->AS_Data.f_out = 43750000UL;
  1999. state->AS_Data.f_out_bw = 6750000UL;
  2000. state->AS_Data.f_zif_bw = MT2063_ZIF_BW;
  2001. state->AS_Data.f_LO1_Step = state->AS_Data.f_ref / 64;
  2002. state->AS_Data.f_LO2_Step = MT2063_TUNE_STEP_SIZE;
  2003. state->AS_Data.maxH1 = MT2063_MAX_HARMONICS_1;
  2004. state->AS_Data.maxH2 = MT2063_MAX_HARMONICS_2;
  2005. state->AS_Data.f_min_LO_Separation = MT2063_MIN_LO_SEP;
  2006. state->AS_Data.f_if1_Request = state->AS_Data.f_if1_Center;
  2007. state->AS_Data.f_LO1 = 2181000000UL;
  2008. state->AS_Data.f_LO2 = 1486249786UL;
  2009. state->f_IF1_actual = state->AS_Data.f_if1_Center;
  2010. state->AS_Data.f_in = state->AS_Data.f_LO1 - state->f_IF1_actual;
  2011. state->AS_Data.f_LO1_FracN_Avoid = MT2063_LO1_FRACN_AVOID;
  2012. state->AS_Data.f_LO2_FracN_Avoid = MT2063_LO2_FRACN_AVOID;
  2013. state->num_regs = MT2063_REG_END_REGS;
  2014. state->AS_Data.avoidDECT = MT2063_AVOID_BOTH;
  2015. state->ctfilt_sw = 0;
  2016. state->CTFiltMax[0] = 69230000;
  2017. state->CTFiltMax[1] = 105770000;
  2018. state->CTFiltMax[2] = 140350000;
  2019. state->CTFiltMax[3] = 177110000;
  2020. state->CTFiltMax[4] = 212860000;
  2021. state->CTFiltMax[5] = 241130000;
  2022. state->CTFiltMax[6] = 274370000;
  2023. state->CTFiltMax[7] = 309820000;
  2024. state->CTFiltMax[8] = 342450000;
  2025. state->CTFiltMax[9] = 378870000;
  2026. state->CTFiltMax[10] = 416210000;
  2027. state->CTFiltMax[11] = 456500000;
  2028. state->CTFiltMax[12] = 495790000;
  2029. state->CTFiltMax[13] = 534530000;
  2030. state->CTFiltMax[14] = 572610000;
  2031. state->CTFiltMax[15] = 598970000;
  2032. state->CTFiltMax[16] = 635910000;
  2033. state->CTFiltMax[17] = 672130000;
  2034. state->CTFiltMax[18] = 714840000;
  2035. state->CTFiltMax[19] = 739660000;
  2036. state->CTFiltMax[20] = 770410000;
  2037. state->CTFiltMax[21] = 814660000;
  2038. state->CTFiltMax[22] = 846950000;
  2039. state->CTFiltMax[23] = 867820000;
  2040. state->CTFiltMax[24] = 915980000;
  2041. state->CTFiltMax[25] = 947450000;
  2042. state->CTFiltMax[26] = 983110000;
  2043. state->CTFiltMax[27] = 1021630000;
  2044. state->CTFiltMax[28] = 1061870000;
  2045. state->CTFiltMax[29] = 1098330000;
  2046. state->CTFiltMax[30] = 1138990000;
  2047. /*
  2048. ** Fetch the FCU osc value and use it and the fRef value to
  2049. ** scale all of the Band Max values
  2050. */
  2051. state->reg[MT2063_REG_CTUNE_CTRL] = 0x0A;
  2052. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  2053. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  2054. if (status < 0)
  2055. return status;
  2056. /* Read the ClearTune filter calibration value */
  2057. status = mt2063_read(state, MT2063_REG_FIFFC,
  2058. &state->reg[MT2063_REG_FIFFC], 1);
  2059. if (status < 0)
  2060. return status;
  2061. fcu_osc = state->reg[MT2063_REG_FIFFC];
  2062. state->reg[MT2063_REG_CTUNE_CTRL] = 0x00;
  2063. status = mt2063_write(state, MT2063_REG_CTUNE_CTRL,
  2064. &state->reg[MT2063_REG_CTUNE_CTRL], 1);
  2065. if (status < 0)
  2066. return status;
  2067. /* Adjust each of the values in the ClearTune filter cross-over table */
  2068. for (i = 0; i < 31; i++)
  2069. state->CTFiltMax[i] = (state->CTFiltMax[i] / 768) * (fcu_osc + 640);
  2070. status = MT2063_SoftwareShutdown(state, 1);
  2071. if (status < 0)
  2072. return status;
  2073. status = MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
  2074. if (status < 0)
  2075. return status;
  2076. return 0;
  2077. }
  2078. static int mt2063_get_status(struct dvb_frontend *fe, u32 * status)
  2079. {
  2080. int rc = 0;
  2081. /* FIXME: add get tuner lock status */
  2082. return rc;
  2083. }
  2084. static int mt2063_get_state(struct dvb_frontend *fe,
  2085. enum tuner_param param, struct tuner_state *tunstate)
  2086. {
  2087. struct mt2063_state *state = fe->tuner_priv;
  2088. switch (param) {
  2089. case DVBFE_TUNER_FREQUENCY:
  2090. /* get frequency */
  2091. break;
  2092. case DVBFE_TUNER_TUNERSTEP:
  2093. break;
  2094. case DVBFE_TUNER_IFFREQ:
  2095. break;
  2096. case DVBFE_TUNER_BANDWIDTH:
  2097. /* get bandwidth */
  2098. break;
  2099. case DVBFE_TUNER_REFCLOCK:
  2100. tunstate->refclock = mt2063_lockStatus(state);
  2101. break;
  2102. default:
  2103. break;
  2104. }
  2105. return (int)tunstate->refclock;
  2106. }
  2107. static int mt2063_set_state(struct dvb_frontend *fe,
  2108. enum tuner_param param, struct tuner_state *tunstate)
  2109. {
  2110. struct mt2063_state *state = fe->tuner_priv;
  2111. u32 status = 0;
  2112. switch (param) {
  2113. case DVBFE_TUNER_FREQUENCY:
  2114. /* set frequency */
  2115. status =
  2116. mt2063_setTune(fe,
  2117. tunstate->frequency, tunstate->bandwidth,
  2118. state->tv_type);
  2119. state->frequency = tunstate->frequency;
  2120. break;
  2121. case DVBFE_TUNER_TUNERSTEP:
  2122. break;
  2123. case DVBFE_TUNER_IFFREQ:
  2124. break;
  2125. case DVBFE_TUNER_BANDWIDTH:
  2126. /* set bandwidth */
  2127. state->bandwidth = tunstate->bandwidth;
  2128. break;
  2129. case DVBFE_TUNER_REFCLOCK:
  2130. break;
  2131. default:
  2132. break;
  2133. }
  2134. return (int)status;
  2135. }
  2136. static int mt2063_release(struct dvb_frontend *fe)
  2137. {
  2138. struct mt2063_state *state = fe->tuner_priv;
  2139. fe->tuner_priv = NULL;
  2140. kfree(state);
  2141. return 0;
  2142. }
  2143. static struct dvb_tuner_ops mt2063_ops = {
  2144. .info = {
  2145. .name = "MT2063 Silicon Tuner",
  2146. .frequency_min = 45000000,
  2147. .frequency_max = 850000000,
  2148. .frequency_step = 0,
  2149. },
  2150. .init = mt2063_init,
  2151. .sleep = MT2063_Sleep,
  2152. .get_status = mt2063_get_status,
  2153. .get_state = mt2063_get_state,
  2154. .set_state = mt2063_set_state,
  2155. .release = mt2063_release
  2156. };
  2157. struct dvb_frontend *mt2063_attach(struct dvb_frontend *fe,
  2158. struct mt2063_config *config,
  2159. struct i2c_adapter *i2c)
  2160. {
  2161. struct mt2063_state *state = NULL;
  2162. state = kzalloc(sizeof(struct mt2063_state), GFP_KERNEL);
  2163. if (state == NULL)
  2164. goto error;
  2165. state->config = config;
  2166. state->i2c = i2c;
  2167. state->frontend = fe;
  2168. state->reference = config->refclock / 1000; /* kHz */
  2169. fe->tuner_priv = state;
  2170. fe->ops.tuner_ops = mt2063_ops;
  2171. printk(KERN_INFO "%s: Attaching MT2063\n", __func__);
  2172. return fe;
  2173. error:
  2174. kfree(state);
  2175. return NULL;
  2176. }
  2177. EXPORT_SYMBOL_GPL(mt2063_attach);
  2178. /*
  2179. * Ancillary routines visible outside mt2063
  2180. * FIXME: Remove them in favor of using standard tuner callbacks
  2181. */
  2182. unsigned int tuner_MT2063_SoftwareShutdown(struct dvb_frontend *fe)
  2183. {
  2184. struct mt2063_state *state = fe->tuner_priv;
  2185. int err = 0;
  2186. err = MT2063_SoftwareShutdown(state, 1);
  2187. if (err < 0)
  2188. printk(KERN_ERR "%s: Couldn't shutdown\n", __func__);
  2189. return err;
  2190. }
  2191. EXPORT_SYMBOL_GPL(tuner_MT2063_SoftwareShutdown);
  2192. unsigned int tuner_MT2063_ClearPowerMaskBits(struct dvb_frontend *fe)
  2193. {
  2194. struct mt2063_state *state = fe->tuner_priv;
  2195. int err = 0;
  2196. err = MT2063_ClearPowerMaskBits(state, MT2063_ALL_SD);
  2197. if (err < 0)
  2198. printk(KERN_ERR "%s: Invalid parameter\n", __func__);
  2199. return err;
  2200. }
  2201. EXPORT_SYMBOL_GPL(tuner_MT2063_ClearPowerMaskBits);
  2202. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  2203. MODULE_AUTHOR("Henry");
  2204. MODULE_DESCRIPTION("MT2063 Silicon tuner");
  2205. MODULE_LICENSE("GPL");