mt2063.c 65 KB

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