mt2063.c 66 KB

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