mt2063.c 65 KB

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