mt2063.c 65 KB

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