mt2063.c 65 KB

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