mt2063.c 65 KB

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