drxd_hard.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002
  1. /*
  2. * drxd_hard.c: DVB-T Demodulator Micronas DRX3975D-A2,DRX397xD-B1
  3. *
  4. * Copyright (C) 2003-2007 Micronas
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 only, as published by the Free Software Foundation.
  9. *
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301, USA
  21. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/firmware.h>
  29. #include <linux/i2c.h>
  30. #include <linux/version.h>
  31. #include <asm/div64.h>
  32. #include "dvb_frontend.h"
  33. #include "drxd.h"
  34. #include "drxd_firm.h"
  35. #define DRX_FW_FILENAME_A2 "drxd-a2-1.1.fw"
  36. #define DRX_FW_FILENAME_B1 "drxd-b1-1.1.fw"
  37. #define CHUNK_SIZE 48
  38. #define DRX_I2C_RMW 0x10
  39. #define DRX_I2C_BROADCAST 0x20
  40. #define DRX_I2C_CLEARCRC 0x80
  41. #define DRX_I2C_SINGLE_MASTER 0xC0
  42. #define DRX_I2C_MODEFLAGS 0xC0
  43. #define DRX_I2C_FLAGS 0xF0
  44. #ifndef SIZEOF_ARRAY
  45. #define SIZEOF_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
  46. #endif
  47. #define DEFAULT_LOCK_TIMEOUT 1100
  48. #define DRX_CHANNEL_AUTO 0
  49. #define DRX_CHANNEL_HIGH 1
  50. #define DRX_CHANNEL_LOW 2
  51. #define DRX_LOCK_MPEG 1
  52. #define DRX_LOCK_FEC 2
  53. #define DRX_LOCK_DEMOD 4
  54. /****************************************************************************/
  55. enum CSCDState {
  56. CSCD_INIT = 0,
  57. CSCD_SET,
  58. CSCD_SAVED
  59. };
  60. enum CDrxdState {
  61. DRXD_UNINITIALIZED = 0,
  62. DRXD_STOPPED,
  63. DRXD_STARTED
  64. };
  65. enum AGC_CTRL_MODE {
  66. AGC_CTRL_AUTO = 0,
  67. AGC_CTRL_USER,
  68. AGC_CTRL_OFF
  69. };
  70. enum OperationMode {
  71. OM_Default,
  72. OM_DVBT_Diversity_Front,
  73. OM_DVBT_Diversity_End
  74. };
  75. struct SCfgAgc {
  76. enum AGC_CTRL_MODE ctrlMode;
  77. u16 outputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  78. u16 settleLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  79. u16 minOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  80. u16 maxOutputLevel; /* range [0, ... , 1023], 1/n of fullscale range */
  81. u16 speed; /* range [0, ... , 1023], 1/n of fullscale range */
  82. u16 R1;
  83. u16 R2;
  84. u16 R3;
  85. };
  86. struct SNoiseCal {
  87. int cpOpt;
  88. u16 cpNexpOfs;
  89. u16 tdCal2k;
  90. u16 tdCal8k;
  91. };
  92. enum app_env {
  93. APPENV_STATIC = 0,
  94. APPENV_PORTABLE = 1,
  95. APPENV_MOBILE = 2
  96. };
  97. enum EIFFilter {
  98. IFFILTER_SAW = 0,
  99. IFFILTER_DISCRETE = 1
  100. };
  101. struct drxd_state {
  102. struct dvb_frontend frontend;
  103. struct dvb_frontend_ops ops;
  104. struct dvb_frontend_parameters param;
  105. const struct firmware *fw;
  106. struct device *dev;
  107. struct i2c_adapter *i2c;
  108. void *priv;
  109. struct drxd_config config;
  110. int i2c_access;
  111. int init_done;
  112. struct mutex mutex;
  113. u8 chip_adr;
  114. u16 hi_cfg_timing_div;
  115. u16 hi_cfg_bridge_delay;
  116. u16 hi_cfg_wakeup_key;
  117. u16 hi_cfg_ctrl;
  118. u16 intermediate_freq;
  119. u16 osc_clock_freq;
  120. enum CSCDState cscd_state;
  121. enum CDrxdState drxd_state;
  122. u16 sys_clock_freq;
  123. s16 osc_clock_deviation;
  124. u16 expected_sys_clock_freq;
  125. u16 insert_rs_byte;
  126. u16 enable_parallel;
  127. int operation_mode;
  128. struct SCfgAgc if_agc_cfg;
  129. struct SCfgAgc rf_agc_cfg;
  130. struct SNoiseCal noise_cal;
  131. u32 fe_fs_add_incr;
  132. u32 org_fe_fs_add_incr;
  133. u16 current_fe_if_incr;
  134. u16 m_FeAgRegAgPwd;
  135. u16 m_FeAgRegAgAgcSio;
  136. u16 m_EcOcRegOcModeLop;
  137. u16 m_EcOcRegSncSncLvl;
  138. u8 *m_InitAtomicRead;
  139. u8 *m_HiI2cPatch;
  140. u8 *m_ResetCEFR;
  141. u8 *m_InitFE_1;
  142. u8 *m_InitFE_2;
  143. u8 *m_InitCP;
  144. u8 *m_InitCE;
  145. u8 *m_InitEQ;
  146. u8 *m_InitSC;
  147. u8 *m_InitEC;
  148. u8 *m_ResetECRAM;
  149. u8 *m_InitDiversityFront;
  150. u8 *m_InitDiversityEnd;
  151. u8 *m_DisableDiversity;
  152. u8 *m_StartDiversityFront;
  153. u8 *m_StartDiversityEnd;
  154. u8 *m_DiversityDelay8MHZ;
  155. u8 *m_DiversityDelay6MHZ;
  156. u8 *microcode;
  157. u32 microcode_length;
  158. int type_A;
  159. int PGA;
  160. int diversity;
  161. int tuner_mirrors;
  162. enum app_env app_env_default;
  163. enum app_env app_env_diversity;
  164. };
  165. /****************************************************************************/
  166. /* I2C **********************************************************************/
  167. /****************************************************************************/
  168. static int i2c_write(struct i2c_adapter *adap, u8 adr, u8 * data, int len)
  169. {
  170. struct i2c_msg msg = {.addr = adr, .flags = 0, .buf = data, .len = len };
  171. if (i2c_transfer(adap, &msg, 1) != 1)
  172. return -1;
  173. return 0;
  174. }
  175. static int i2c_read(struct i2c_adapter *adap,
  176. u8 adr, u8 *msg, int len, u8 *answ, int alen)
  177. {
  178. struct i2c_msg msgs[2] = {
  179. {
  180. .addr = adr, .flags = 0,
  181. .buf = msg, .len = len
  182. }, {
  183. .addr = adr, .flags = I2C_M_RD,
  184. .buf = answ, .len = alen
  185. }
  186. };
  187. if (i2c_transfer(adap, msgs, 2) != 2)
  188. return -1;
  189. return 0;
  190. }
  191. inline u32 MulDiv32(u32 a, u32 b, u32 c)
  192. {
  193. u64 tmp64;
  194. tmp64 = (u64)a * (u64)b;
  195. do_div(tmp64, c);
  196. return (u32) tmp64;
  197. }
  198. static int Read16(struct drxd_state *state, u32 reg, u16 *data, u8 flags)
  199. {
  200. u8 adr = state->config.demod_address;
  201. u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
  202. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  203. };
  204. u8 mm2[2];
  205. if (i2c_read(state->i2c, adr, mm1, 4, mm2, 2) < 0)
  206. return -1;
  207. if (data)
  208. *data = mm2[0] | (mm2[1] << 8);
  209. return mm2[0] | (mm2[1] << 8);
  210. }
  211. static int Read32(struct drxd_state *state, u32 reg, u32 *data, u8 flags)
  212. {
  213. u8 adr = state->config.demod_address;
  214. u8 mm1[4] = { reg & 0xff, (reg >> 16) & 0xff,
  215. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  216. };
  217. u8 mm2[4];
  218. if (i2c_read(state->i2c, adr, mm1, 4, mm2, 4) < 0)
  219. return -1;
  220. if (data)
  221. *data =
  222. mm2[0] | (mm2[1] << 8) | (mm2[2] << 16) | (mm2[3] << 24);
  223. return 0;
  224. }
  225. static int Write16(struct drxd_state *state, u32 reg, u16 data, u8 flags)
  226. {
  227. u8 adr = state->config.demod_address;
  228. u8 mm[6] = { reg & 0xff, (reg >> 16) & 0xff,
  229. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
  230. data & 0xff, (data >> 8) & 0xff
  231. };
  232. if (i2c_write(state->i2c, adr, mm, 6) < 0)
  233. return -1;
  234. return 0;
  235. }
  236. static int Write32(struct drxd_state *state, u32 reg, u32 data, u8 flags)
  237. {
  238. u8 adr = state->config.demod_address;
  239. u8 mm[8] = { reg & 0xff, (reg >> 16) & 0xff,
  240. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff,
  241. data & 0xff, (data >> 8) & 0xff,
  242. (data >> 16) & 0xff, (data >> 24) & 0xff
  243. };
  244. if (i2c_write(state->i2c, adr, mm, 8) < 0)
  245. return -1;
  246. return 0;
  247. }
  248. static int write_chunk(struct drxd_state *state,
  249. u32 reg, u8 *data, u32 len, u8 flags)
  250. {
  251. u8 adr = state->config.demod_address;
  252. u8 mm[CHUNK_SIZE + 4] = { reg & 0xff, (reg >> 16) & 0xff,
  253. flags | ((reg >> 24) & 0xff), (reg >> 8) & 0xff
  254. };
  255. int i;
  256. for (i = 0; i < len; i++)
  257. mm[4 + i] = data[i];
  258. if (i2c_write(state->i2c, adr, mm, 4 + len) < 0) {
  259. printk(KERN_ERR "error in write_chunk\n");
  260. return -1;
  261. }
  262. return 0;
  263. }
  264. static int WriteBlock(struct drxd_state *state,
  265. u32 Address, u16 BlockSize, u8 *pBlock, u8 Flags)
  266. {
  267. while (BlockSize > 0) {
  268. u16 Chunk = BlockSize > CHUNK_SIZE ? CHUNK_SIZE : BlockSize;
  269. if (write_chunk(state, Address, pBlock, Chunk, Flags) < 0)
  270. return -1;
  271. pBlock += Chunk;
  272. Address += (Chunk >> 1);
  273. BlockSize -= Chunk;
  274. }
  275. return 0;
  276. }
  277. static int WriteTable(struct drxd_state *state, u8 * pTable)
  278. {
  279. int status = 0;
  280. if (pTable == NULL)
  281. return 0;
  282. while (!status) {
  283. u16 Length;
  284. u32 Address = pTable[0] | (pTable[1] << 8) |
  285. (pTable[2] << 16) | (pTable[3] << 24);
  286. if (Address == 0xFFFFFFFF)
  287. break;
  288. pTable += sizeof(u32);
  289. Length = pTable[0] | (pTable[1] << 8);
  290. pTable += sizeof(u16);
  291. if (!Length)
  292. break;
  293. status = WriteBlock(state, Address, Length * 2, pTable, 0);
  294. pTable += (Length * 2);
  295. }
  296. return status;
  297. }
  298. /****************************************************************************/
  299. /****************************************************************************/
  300. /****************************************************************************/
  301. static int ResetCEFR(struct drxd_state *state)
  302. {
  303. return WriteTable(state, state->m_ResetCEFR);
  304. }
  305. static int InitCP(struct drxd_state *state)
  306. {
  307. return WriteTable(state, state->m_InitCP);
  308. }
  309. static int InitCE(struct drxd_state *state)
  310. {
  311. int status;
  312. enum app_env AppEnv = state->app_env_default;
  313. do {
  314. status = WriteTable(state, state->m_InitCE);
  315. if (status < 0)
  316. break;
  317. if (state->operation_mode == OM_DVBT_Diversity_Front ||
  318. state->operation_mode == OM_DVBT_Diversity_End) {
  319. AppEnv = state->app_env_diversity;
  320. }
  321. if (AppEnv == APPENV_STATIC) {
  322. status = Write16(state, CE_REG_TAPSET__A, 0x0000, 0);
  323. if (status < 0)
  324. break;
  325. } else if (AppEnv == APPENV_PORTABLE) {
  326. status = Write16(state, CE_REG_TAPSET__A, 0x0001, 0);
  327. if (status < 0)
  328. break;
  329. } else if (AppEnv == APPENV_MOBILE && state->type_A) {
  330. status = Write16(state, CE_REG_TAPSET__A, 0x0002, 0);
  331. if (status < 0)
  332. break;
  333. } else if (AppEnv == APPENV_MOBILE && !state->type_A) {
  334. status = Write16(state, CE_REG_TAPSET__A, 0x0006, 0);
  335. if (status < 0)
  336. break;
  337. }
  338. /* start ce */
  339. status = Write16(state, B_CE_REG_COMM_EXEC__A, 0x0001, 0);
  340. if (status < 0)
  341. break;
  342. } while (0);
  343. return status;
  344. }
  345. static int StopOC(struct drxd_state *state)
  346. {
  347. int status = 0;
  348. u16 ocSyncLvl = 0;
  349. u16 ocModeLop = state->m_EcOcRegOcModeLop;
  350. u16 dtoIncLop = 0;
  351. u16 dtoIncHip = 0;
  352. do {
  353. /* Store output configuration */
  354. status = Read16(state, EC_OC_REG_SNC_ISC_LVL__A, &ocSyncLvl, 0);
  355. if (status < 0)
  356. break;
  357. /* CHK_ERROR(Read16(EC_OC_REG_OC_MODE_LOP__A, &ocModeLop)); */
  358. state->m_EcOcRegSncSncLvl = ocSyncLvl;
  359. /* m_EcOcRegOcModeLop = ocModeLop; */
  360. /* Flush FIFO (byte-boundary) at fixed rate */
  361. status = Read16(state, EC_OC_REG_RCN_MAP_LOP__A, &dtoIncLop, 0);
  362. if (status < 0)
  363. break;
  364. status = Read16(state, EC_OC_REG_RCN_MAP_HIP__A, &dtoIncHip, 0);
  365. if (status < 0)
  366. break;
  367. status = Write16(state, EC_OC_REG_DTO_INC_LOP__A, dtoIncLop, 0);
  368. if (status < 0)
  369. break;
  370. status = Write16(state, EC_OC_REG_DTO_INC_HIP__A, dtoIncHip, 0);
  371. if (status < 0)
  372. break;
  373. ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC__M);
  374. ocModeLop |= EC_OC_REG_OC_MODE_LOP_DTO_CTR_SRC_STATIC;
  375. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
  376. if (status < 0)
  377. break;
  378. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
  379. if (status < 0)
  380. break;
  381. msleep(1);
  382. /* Output pins to '0' */
  383. status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS__M, 0);
  384. if (status < 0)
  385. break;
  386. /* Force the OC out of sync */
  387. ocSyncLvl &= ~(EC_OC_REG_SNC_ISC_LVL_OSC__M);
  388. status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, ocSyncLvl, 0);
  389. if (status < 0)
  390. break;
  391. ocModeLop &= ~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M);
  392. ocModeLop |= EC_OC_REG_OC_MODE_LOP_PAR_ENA_ENABLE;
  393. ocModeLop |= 0x2; /* Magically-out-of-sync */
  394. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, ocModeLop, 0);
  395. if (status < 0)
  396. break;
  397. status = Write16(state, EC_OC_REG_COMM_INT_STA__A, 0x0, 0);
  398. if (status < 0)
  399. break;
  400. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
  401. if (status < 0)
  402. break;
  403. } while (0);
  404. return status;
  405. }
  406. static int StartOC(struct drxd_state *state)
  407. {
  408. int status = 0;
  409. do {
  410. /* Stop OC */
  411. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_HOLD, 0);
  412. if (status < 0)
  413. break;
  414. /* Restore output configuration */
  415. status = Write16(state, EC_OC_REG_SNC_ISC_LVL__A, state->m_EcOcRegSncSncLvl, 0);
  416. if (status < 0)
  417. break;
  418. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, state->m_EcOcRegOcModeLop, 0);
  419. if (status < 0)
  420. break;
  421. /* Output pins active again */
  422. status = Write16(state, EC_OC_REG_OCR_MPG_UOS__A, EC_OC_REG_OCR_MPG_UOS_INIT, 0);
  423. if (status < 0)
  424. break;
  425. /* Start OC */
  426. status = Write16(state, EC_OC_REG_COMM_EXEC__A, EC_OC_REG_COMM_EXEC_CTL_ACTIVE, 0);
  427. if (status < 0)
  428. break;
  429. } while (0);
  430. return status;
  431. }
  432. static int InitEQ(struct drxd_state *state)
  433. {
  434. return WriteTable(state, state->m_InitEQ);
  435. }
  436. static int InitEC(struct drxd_state *state)
  437. {
  438. return WriteTable(state, state->m_InitEC);
  439. }
  440. static int InitSC(struct drxd_state *state)
  441. {
  442. return WriteTable(state, state->m_InitSC);
  443. }
  444. static int InitAtomicRead(struct drxd_state *state)
  445. {
  446. return WriteTable(state, state->m_InitAtomicRead);
  447. }
  448. static int CorrectSysClockDeviation(struct drxd_state *state);
  449. static int DRX_GetLockStatus(struct drxd_state *state, u32 * pLockStatus)
  450. {
  451. u16 ScRaRamLock = 0;
  452. const u16 mpeg_lock_mask = (SC_RA_RAM_LOCK_MPEG__M |
  453. SC_RA_RAM_LOCK_FEC__M |
  454. SC_RA_RAM_LOCK_DEMOD__M);
  455. const u16 fec_lock_mask = (SC_RA_RAM_LOCK_FEC__M |
  456. SC_RA_RAM_LOCK_DEMOD__M);
  457. const u16 demod_lock_mask = SC_RA_RAM_LOCK_DEMOD__M;
  458. int status;
  459. *pLockStatus = 0;
  460. status = Read16(state, SC_RA_RAM_LOCK__A, &ScRaRamLock, 0x0000);
  461. if (status < 0) {
  462. printk(KERN_ERR "Can't read SC_RA_RAM_LOCK__A status = %08x\n", status);
  463. return status;
  464. }
  465. if (state->drxd_state != DRXD_STARTED)
  466. return 0;
  467. if ((ScRaRamLock & mpeg_lock_mask) == mpeg_lock_mask) {
  468. *pLockStatus |= DRX_LOCK_MPEG;
  469. CorrectSysClockDeviation(state);
  470. }
  471. if ((ScRaRamLock & fec_lock_mask) == fec_lock_mask)
  472. *pLockStatus |= DRX_LOCK_FEC;
  473. if ((ScRaRamLock & demod_lock_mask) == demod_lock_mask)
  474. *pLockStatus |= DRX_LOCK_DEMOD;
  475. return 0;
  476. }
  477. /****************************************************************************/
  478. static int SetCfgIfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
  479. {
  480. int status;
  481. if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
  482. return -1;
  483. if (cfg->ctrlMode == AGC_CTRL_USER) {
  484. do {
  485. u16 FeAgRegPm1AgcWri;
  486. u16 FeAgRegAgModeLop;
  487. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
  488. if (status < 0)
  489. break;
  490. FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
  491. FeAgRegAgModeLop |= FE_AG_REG_AG_MODE_LOP_MODE_4_STATIC;
  492. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
  493. if (status < 0)
  494. break;
  495. FeAgRegPm1AgcWri = (u16) (cfg->outputLevel &
  496. FE_AG_REG_PM1_AGC_WRI__M);
  497. status = Write16(state, FE_AG_REG_PM1_AGC_WRI__A, FeAgRegPm1AgcWri, 0);
  498. if (status < 0)
  499. break;
  500. } while (0);
  501. } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
  502. if (((cfg->maxOutputLevel) < (cfg->minOutputLevel)) ||
  503. ((cfg->maxOutputLevel) > DRXD_FE_CTRL_MAX) ||
  504. ((cfg->speed) > DRXD_FE_CTRL_MAX) ||
  505. ((cfg->settleLevel) > DRXD_FE_CTRL_MAX)
  506. )
  507. return -1;
  508. do {
  509. u16 FeAgRegAgModeLop;
  510. u16 FeAgRegEgcSetLvl;
  511. u16 slope, offset;
  512. /* == Mode == */
  513. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &FeAgRegAgModeLop, 0);
  514. if (status < 0)
  515. break;
  516. FeAgRegAgModeLop &= (~FE_AG_REG_AG_MODE_LOP_MODE_4__M);
  517. FeAgRegAgModeLop |=
  518. FE_AG_REG_AG_MODE_LOP_MODE_4_DYNAMIC;
  519. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, FeAgRegAgModeLop, 0);
  520. if (status < 0)
  521. break;
  522. /* == Settle level == */
  523. FeAgRegEgcSetLvl = (u16) ((cfg->settleLevel >> 1) &
  524. FE_AG_REG_EGC_SET_LVL__M);
  525. status = Write16(state, FE_AG_REG_EGC_SET_LVL__A, FeAgRegEgcSetLvl, 0);
  526. if (status < 0)
  527. break;
  528. /* == Min/Max == */
  529. slope = (u16) ((cfg->maxOutputLevel -
  530. cfg->minOutputLevel) / 2);
  531. offset = (u16) ((cfg->maxOutputLevel +
  532. cfg->minOutputLevel) / 2 - 511);
  533. status = Write16(state, FE_AG_REG_GC1_AGC_RIC__A, slope, 0);
  534. if (status < 0)
  535. break;
  536. status = Write16(state, FE_AG_REG_GC1_AGC_OFF__A, offset, 0);
  537. if (status < 0)
  538. break;
  539. /* == Speed == */
  540. {
  541. const u16 maxRur = 8;
  542. const u16 slowIncrDecLUT[] = { 3, 4, 4, 5, 6 };
  543. const u16 fastIncrDecLUT[] = { 14, 15, 15, 16,
  544. 17, 18, 18, 19,
  545. 20, 21, 22, 23,
  546. 24, 26, 27, 28,
  547. 29, 31
  548. };
  549. u16 fineSteps = (DRXD_FE_CTRL_MAX + 1) /
  550. (maxRur + 1);
  551. u16 fineSpeed = (u16) (cfg->speed -
  552. ((cfg->speed /
  553. fineSteps) *
  554. fineSteps));
  555. u16 invRurCount = (u16) (cfg->speed /
  556. fineSteps);
  557. u16 rurCount;
  558. if (invRurCount > maxRur) {
  559. rurCount = 0;
  560. fineSpeed += fineSteps;
  561. } else {
  562. rurCount = maxRur - invRurCount;
  563. }
  564. /*
  565. fastInc = default *
  566. (2^(fineSpeed/fineSteps))
  567. => range[default...2*default>
  568. slowInc = default *
  569. (2^(fineSpeed/fineSteps))
  570. */
  571. {
  572. u16 fastIncrDec =
  573. fastIncrDecLUT[fineSpeed /
  574. ((fineSteps /
  575. (14 + 1)) + 1)];
  576. u16 slowIncrDec =
  577. slowIncrDecLUT[fineSpeed /
  578. (fineSteps /
  579. (3 + 1))];
  580. status = Write16(state, FE_AG_REG_EGC_RUR_CNT__A, rurCount, 0);
  581. if (status < 0)
  582. break;
  583. status = Write16(state, FE_AG_REG_EGC_FAS_INC__A, fastIncrDec, 0);
  584. if (status < 0)
  585. break;
  586. status = Write16(state, FE_AG_REG_EGC_FAS_DEC__A, fastIncrDec, 0);
  587. if (status < 0)
  588. break;
  589. status = Write16(state, FE_AG_REG_EGC_SLO_INC__A, slowIncrDec, 0);
  590. if (status < 0)
  591. break;
  592. status = Write16(state, FE_AG_REG_EGC_SLO_DEC__A, slowIncrDec, 0);
  593. if (status < 0)
  594. break;
  595. }
  596. }
  597. } while (0);
  598. } else {
  599. /* No OFF mode for IF control */
  600. return -1;
  601. }
  602. return status;
  603. }
  604. static int SetCfgRfAgc(struct drxd_state *state, struct SCfgAgc *cfg)
  605. {
  606. int status = 0;
  607. if (cfg->outputLevel > DRXD_FE_CTRL_MAX)
  608. return -1;
  609. if (cfg->ctrlMode == AGC_CTRL_USER) {
  610. do {
  611. u16 AgModeLop = 0;
  612. u16 level = (cfg->outputLevel);
  613. if (level == DRXD_FE_CTRL_MAX)
  614. level++;
  615. status = Write16(state, FE_AG_REG_PM2_AGC_WRI__A, level, 0x0000);
  616. if (status < 0)
  617. break;
  618. /*==== Mode ====*/
  619. /* Powerdown PD2, WRI source */
  620. state->m_FeAgRegAgPwd &= ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  621. state->m_FeAgRegAgPwd |=
  622. FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
  623. status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
  624. if (status < 0)
  625. break;
  626. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  627. if (status < 0)
  628. break;
  629. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  630. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  631. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  632. FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
  633. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  634. if (status < 0)
  635. break;
  636. /* enable AGC2 pin */
  637. {
  638. u16 FeAgRegAgAgcSio = 0;
  639. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  640. if (status < 0)
  641. break;
  642. FeAgRegAgAgcSio &=
  643. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  644. FeAgRegAgAgcSio |=
  645. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
  646. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  647. if (status < 0)
  648. break;
  649. }
  650. } while (0);
  651. } else if (cfg->ctrlMode == AGC_CTRL_AUTO) {
  652. u16 AgModeLop = 0;
  653. do {
  654. u16 level;
  655. /* Automatic control */
  656. /* Powerup PD2, AGC2 as output, TGC source */
  657. (state->m_FeAgRegAgPwd) &=
  658. ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  659. (state->m_FeAgRegAgPwd) |=
  660. FE_AG_REG_AG_PWD_PWD_PD2_DISABLE;
  661. status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
  662. if (status < 0)
  663. break;
  664. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  665. if (status < 0)
  666. break;
  667. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  668. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  669. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  670. FE_AG_REG_AG_MODE_LOP_MODE_E_DYNAMIC);
  671. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  672. if (status < 0)
  673. break;
  674. /* Settle level */
  675. level = (((cfg->settleLevel) >> 4) &
  676. FE_AG_REG_TGC_SET_LVL__M);
  677. status = Write16(state, FE_AG_REG_TGC_SET_LVL__A, level, 0x0000);
  678. if (status < 0)
  679. break;
  680. /* Min/max: don't care */
  681. /* Speed: TODO */
  682. /* enable AGC2 pin */
  683. {
  684. u16 FeAgRegAgAgcSio = 0;
  685. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  686. if (status < 0)
  687. break;
  688. FeAgRegAgAgcSio &=
  689. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  690. FeAgRegAgAgcSio |=
  691. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_OUTPUT;
  692. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  693. if (status < 0)
  694. break;
  695. }
  696. } while (0);
  697. } else {
  698. u16 AgModeLop = 0;
  699. do {
  700. /* No RF AGC control */
  701. /* Powerdown PD2, AGC2 as output, WRI source */
  702. (state->m_FeAgRegAgPwd) &=
  703. ~(FE_AG_REG_AG_PWD_PWD_PD2__M);
  704. (state->m_FeAgRegAgPwd) |=
  705. FE_AG_REG_AG_PWD_PWD_PD2_ENABLE;
  706. status = Write16(state, FE_AG_REG_AG_PWD__A, (state->m_FeAgRegAgPwd), 0x0000);
  707. if (status < 0)
  708. break;
  709. status = Read16(state, FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  710. if (status < 0)
  711. break;
  712. AgModeLop &= (~(FE_AG_REG_AG_MODE_LOP_MODE_5__M |
  713. FE_AG_REG_AG_MODE_LOP_MODE_E__M));
  714. AgModeLop |= (FE_AG_REG_AG_MODE_LOP_MODE_5_STATIC |
  715. FE_AG_REG_AG_MODE_LOP_MODE_E_STATIC);
  716. status = Write16(state, FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  717. if (status < 0)
  718. break;
  719. /* set FeAgRegAgAgcSio AGC2 (RF) as input */
  720. {
  721. u16 FeAgRegAgAgcSio = 0;
  722. status = Read16(state, FE_AG_REG_AG_AGC_SIO__A, &FeAgRegAgAgcSio, 0x0000);
  723. if (status < 0)
  724. break;
  725. FeAgRegAgAgcSio &=
  726. ~(FE_AG_REG_AG_AGC_SIO_AGC_SIO_2__M);
  727. FeAgRegAgAgcSio |=
  728. FE_AG_REG_AG_AGC_SIO_AGC_SIO_2_INPUT;
  729. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, FeAgRegAgAgcSio, 0x0000);
  730. if (status < 0)
  731. break;
  732. }
  733. } while (0);
  734. }
  735. return status;
  736. }
  737. static int ReadIFAgc(struct drxd_state *state, u32 * pValue)
  738. {
  739. int status = 0;
  740. *pValue = 0;
  741. if (state->if_agc_cfg.ctrlMode != AGC_CTRL_OFF) {
  742. u16 Value;
  743. status = Read16(state, FE_AG_REG_GC1_AGC_DAT__A, &Value, 0);
  744. Value &= FE_AG_REG_GC1_AGC_DAT__M;
  745. if (status >= 0) {
  746. /* 3.3V
  747. |
  748. R1
  749. |
  750. Vin - R3 - * -- Vout
  751. |
  752. R2
  753. |
  754. GND
  755. */
  756. u32 R1 = state->if_agc_cfg.R1;
  757. u32 R2 = state->if_agc_cfg.R2;
  758. u32 R3 = state->if_agc_cfg.R3;
  759. u32 Vmax = (3300 * R2) / (R1 + R2);
  760. u32 Rpar = (R2 * R3) / (R3 + R2);
  761. u32 Vmin = (3300 * Rpar) / (R1 + Rpar);
  762. u32 Vout = Vmin + ((Vmax - Vmin) * Value) / 1024;
  763. *pValue = Vout;
  764. }
  765. }
  766. return status;
  767. }
  768. static int load_firmware(struct drxd_state *state, const char *fw_name)
  769. {
  770. const struct firmware *fw;
  771. if (request_firmware(&fw, fw_name, state->dev) < 0) {
  772. printk(KERN_ERR "drxd: firmware load failure [%s]\n", fw_name);
  773. return -EIO;
  774. }
  775. state->microcode = kzalloc(fw->size, GFP_KERNEL);
  776. if (state->microcode == NULL) {
  777. printk(KERN_ERR "drxd: firmware load failure: nomemory\n");
  778. return -ENOMEM;
  779. }
  780. memcpy(state->microcode, fw->data, fw->size);
  781. state->microcode_length = fw->size;
  782. return 0;
  783. }
  784. static int DownloadMicrocode(struct drxd_state *state,
  785. const u8 *pMCImage, u32 Length)
  786. {
  787. u8 *pSrc;
  788. u16 Flags;
  789. u32 Address;
  790. u16 nBlocks;
  791. u16 BlockSize;
  792. u16 BlockCRC;
  793. u32 offset = 0;
  794. int i, status = 0;
  795. pSrc = (u8 *) pMCImage;
  796. Flags = (pSrc[0] << 8) | pSrc[1];
  797. pSrc += sizeof(u16);
  798. offset += sizeof(u16);
  799. nBlocks = (pSrc[0] << 8) | pSrc[1];
  800. pSrc += sizeof(u16);
  801. offset += sizeof(u16);
  802. for (i = 0; i < nBlocks; i++) {
  803. Address = (pSrc[0] << 24) | (pSrc[1] << 16) |
  804. (pSrc[2] << 8) | pSrc[3];
  805. pSrc += sizeof(u32);
  806. offset += sizeof(u32);
  807. BlockSize = ((pSrc[0] << 8) | pSrc[1]) * sizeof(u16);
  808. pSrc += sizeof(u16);
  809. offset += sizeof(u16);
  810. Flags = (pSrc[0] << 8) | pSrc[1];
  811. pSrc += sizeof(u16);
  812. offset += sizeof(u16);
  813. BlockCRC = (pSrc[0] << 8) | pSrc[1];
  814. pSrc += sizeof(u16);
  815. offset += sizeof(u16);
  816. status = WriteBlock(state, Address, BlockSize,
  817. pSrc, DRX_I2C_CLEARCRC);
  818. if (status < 0)
  819. break;
  820. pSrc += BlockSize;
  821. offset += BlockSize;
  822. }
  823. return status;
  824. }
  825. static int HI_Command(struct drxd_state *state, u16 cmd, u16 * pResult)
  826. {
  827. u32 nrRetries = 0;
  828. u16 waitCmd;
  829. int status;
  830. status = Write16(state, HI_RA_RAM_SRV_CMD__A, cmd, 0);
  831. if (status < 0)
  832. return status;
  833. do {
  834. nrRetries += 1;
  835. if (nrRetries > DRXD_MAX_RETRIES) {
  836. status = -1;
  837. break;
  838. };
  839. status = Read16(state, HI_RA_RAM_SRV_CMD__A, &waitCmd, 0);
  840. } while (waitCmd != 0);
  841. if (status >= 0)
  842. status = Read16(state, HI_RA_RAM_SRV_RES__A, pResult, 0);
  843. return status;
  844. }
  845. static int HI_CfgCommand(struct drxd_state *state)
  846. {
  847. int status = 0;
  848. mutex_lock(&state->mutex);
  849. Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  850. Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, state->hi_cfg_timing_div, 0);
  851. Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, state->hi_cfg_bridge_delay, 0);
  852. Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, state->hi_cfg_wakeup_key, 0);
  853. Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, state->hi_cfg_ctrl, 0);
  854. Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  855. if ((state->hi_cfg_ctrl & HI_RA_RAM_SRV_CFG_ACT_PWD_EXE) ==
  856. HI_RA_RAM_SRV_CFG_ACT_PWD_EXE)
  857. status = Write16(state, HI_RA_RAM_SRV_CMD__A,
  858. HI_RA_RAM_SRV_CMD_CONFIG, 0);
  859. else
  860. status = HI_Command(state, HI_RA_RAM_SRV_CMD_CONFIG, 0);
  861. mutex_unlock(&state->mutex);
  862. return status;
  863. }
  864. static int InitHI(struct drxd_state *state)
  865. {
  866. state->hi_cfg_wakeup_key = (state->chip_adr);
  867. /* port/bridge/power down ctrl */
  868. state->hi_cfg_ctrl = HI_RA_RAM_SRV_CFG_ACT_SLV0_ON;
  869. return HI_CfgCommand(state);
  870. }
  871. static int HI_ResetCommand(struct drxd_state *state)
  872. {
  873. int status;
  874. mutex_lock(&state->mutex);
  875. status = Write16(state, HI_RA_RAM_SRV_RST_KEY__A,
  876. HI_RA_RAM_SRV_RST_KEY_ACT, 0);
  877. if (status == 0)
  878. status = HI_Command(state, HI_RA_RAM_SRV_CMD_RESET, 0);
  879. mutex_unlock(&state->mutex);
  880. msleep(1);
  881. return status;
  882. }
  883. static int DRX_ConfigureI2CBridge(struct drxd_state *state, int bEnableBridge)
  884. {
  885. state->hi_cfg_ctrl &= (~HI_RA_RAM_SRV_CFG_ACT_BRD__M);
  886. if (bEnableBridge)
  887. state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_ON;
  888. else
  889. state->hi_cfg_ctrl |= HI_RA_RAM_SRV_CFG_ACT_BRD_OFF;
  890. return HI_CfgCommand(state);
  891. }
  892. #define HI_TR_WRITE 0x9
  893. #define HI_TR_READ 0xA
  894. #define HI_TR_READ_WRITE 0xB
  895. #define HI_TR_BROADCAST 0x4
  896. #if 0
  897. static int AtomicReadBlock(struct drxd_state *state,
  898. u32 Addr, u16 DataSize, u8 *pData, u8 Flags)
  899. {
  900. int status;
  901. int i = 0;
  902. /* Parameter check */
  903. if ((!pData) || ((DataSize & 1) != 0))
  904. return -1;
  905. mutex_lock(&state->mutex);
  906. do {
  907. /* Instruct HI to read n bytes */
  908. /* TODO use proper names forthese egisters */
  909. status = Write16(state, HI_RA_RAM_SRV_CFG_KEY__A, (HI_TR_FUNC_ADDR & 0xFFFF), 0);
  910. if (status < 0)
  911. break;
  912. status = Write16(state, HI_RA_RAM_SRV_CFG_DIV__A, (u16) (Addr >> 16), 0);
  913. if (status < 0)
  914. break;
  915. status = Write16(state, HI_RA_RAM_SRV_CFG_BDL__A, (u16) (Addr & 0xFFFF), 0);
  916. if (status < 0)
  917. break;
  918. status = Write16(state, HI_RA_RAM_SRV_CFG_WUP__A, (u16) ((DataSize / 2) - 1), 0);
  919. if (status < 0)
  920. break;
  921. status = Write16(state, HI_RA_RAM_SRV_CFG_ACT__A, HI_TR_READ, 0);
  922. if (status < 0)
  923. break;
  924. status = HI_Command(state, HI_RA_RAM_SRV_CMD_EXECUTE, 0);
  925. if (status < 0)
  926. break;
  927. } while (0);
  928. if (status >= 0) {
  929. for (i = 0; i < (DataSize / 2); i += 1) {
  930. u16 word;
  931. status = Read16(state, (HI_RA_RAM_USR_BEGIN__A + i),
  932. &word, 0);
  933. if (status < 0)
  934. break;
  935. pData[2 * i] = (u8) (word & 0xFF);
  936. pData[(2 * i) + 1] = (u8) (word >> 8);
  937. }
  938. }
  939. mutex_unlock(&state->mutex);
  940. return status;
  941. }
  942. static int AtomicReadReg32(struct drxd_state *state,
  943. u32 Addr, u32 *pData, u8 Flags)
  944. {
  945. u8 buf[sizeof(u32)];
  946. int status;
  947. if (!pData)
  948. return -1;
  949. status = AtomicReadBlock(state, Addr, sizeof(u32), buf, Flags);
  950. *pData = (((u32) buf[0]) << 0) +
  951. (((u32) buf[1]) << 8) +
  952. (((u32) buf[2]) << 16) + (((u32) buf[3]) << 24);
  953. return status;
  954. }
  955. #endif
  956. static int StopAllProcessors(struct drxd_state *state)
  957. {
  958. return Write16(state, HI_COMM_EXEC__A,
  959. SC_COMM_EXEC_CTL_STOP, DRX_I2C_BROADCAST);
  960. }
  961. static int EnableAndResetMB(struct drxd_state *state)
  962. {
  963. if (state->type_A) {
  964. /* disable? monitor bus observe @ EC_OC */
  965. Write16(state, EC_OC_REG_OC_MON_SIO__A, 0x0000, 0x0000);
  966. }
  967. /* do inverse broadcast, followed by explicit write to HI */
  968. Write16(state, HI_COMM_MB__A, 0x0000, DRX_I2C_BROADCAST);
  969. Write16(state, HI_COMM_MB__A, 0x0000, 0x0000);
  970. return 0;
  971. }
  972. static int InitCC(struct drxd_state *state)
  973. {
  974. if (state->osc_clock_freq == 0 ||
  975. state->osc_clock_freq > 20000 ||
  976. (state->osc_clock_freq % 4000) != 0) {
  977. printk(KERN_ERR "invalid osc frequency %d\n", state->osc_clock_freq);
  978. return -1;
  979. }
  980. Write16(state, CC_REG_OSC_MODE__A, CC_REG_OSC_MODE_M20, 0);
  981. Write16(state, CC_REG_PLL_MODE__A, CC_REG_PLL_MODE_BYPASS_PLL |
  982. CC_REG_PLL_MODE_PUMP_CUR_12, 0);
  983. Write16(state, CC_REG_REF_DIVIDE__A, state->osc_clock_freq / 4000, 0);
  984. Write16(state, CC_REG_PWD_MODE__A, CC_REG_PWD_MODE_DOWN_PLL, 0);
  985. Write16(state, CC_REG_UPDATE__A, CC_REG_UPDATE_KEY, 0);
  986. return 0;
  987. }
  988. static int ResetECOD(struct drxd_state *state)
  989. {
  990. int status = 0;
  991. if (state->type_A)
  992. status = Write16(state, EC_OD_REG_SYNC__A, 0x0664, 0);
  993. else
  994. status = Write16(state, B_EC_OD_REG_SYNC__A, 0x0664, 0);
  995. if (!(status < 0))
  996. status = WriteTable(state, state->m_ResetECRAM);
  997. if (!(status < 0))
  998. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0001, 0);
  999. return status;
  1000. }
  1001. /* Configure PGA switch */
  1002. static int SetCfgPga(struct drxd_state *state, int pgaSwitch)
  1003. {
  1004. int status;
  1005. u16 AgModeLop = 0;
  1006. u16 AgModeHip = 0;
  1007. do {
  1008. if (pgaSwitch) {
  1009. /* PGA on */
  1010. /* fine gain */
  1011. status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  1012. if (status < 0)
  1013. break;
  1014. AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
  1015. AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_DYNAMIC;
  1016. status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  1017. if (status < 0)
  1018. break;
  1019. /* coarse gain */
  1020. status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
  1021. if (status < 0)
  1022. break;
  1023. AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
  1024. AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_DYNAMIC;
  1025. status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
  1026. if (status < 0)
  1027. break;
  1028. /* enable fine and coarse gain, enable AAF,
  1029. no ext resistor */
  1030. status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFY_PCY_AFY_REN, 0x0000);
  1031. if (status < 0)
  1032. break;
  1033. } else {
  1034. /* PGA off, bypass */
  1035. /* fine gain */
  1036. status = Read16(state, B_FE_AG_REG_AG_MODE_LOP__A, &AgModeLop, 0x0000);
  1037. if (status < 0)
  1038. break;
  1039. AgModeLop &= (~(B_FE_AG_REG_AG_MODE_LOP_MODE_C__M));
  1040. AgModeLop |= B_FE_AG_REG_AG_MODE_LOP_MODE_C_STATIC;
  1041. status = Write16(state, B_FE_AG_REG_AG_MODE_LOP__A, AgModeLop, 0x0000);
  1042. if (status < 0)
  1043. break;
  1044. /* coarse gain */
  1045. status = Read16(state, B_FE_AG_REG_AG_MODE_HIP__A, &AgModeHip, 0x0000);
  1046. if (status < 0)
  1047. break;
  1048. AgModeHip &= (~(B_FE_AG_REG_AG_MODE_HIP_MODE_J__M));
  1049. AgModeHip |= B_FE_AG_REG_AG_MODE_HIP_MODE_J_STATIC;
  1050. status = Write16(state, B_FE_AG_REG_AG_MODE_HIP__A, AgModeHip, 0x0000);
  1051. if (status < 0)
  1052. break;
  1053. /* disable fine and coarse gain, enable AAF,
  1054. no ext resistor */
  1055. status = Write16(state, B_FE_AG_REG_AG_PGA_MODE__A, B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN, 0x0000);
  1056. if (status < 0)
  1057. break;
  1058. }
  1059. } while (0);
  1060. return status;
  1061. }
  1062. static int InitFE(struct drxd_state *state)
  1063. {
  1064. int status;
  1065. do {
  1066. status = WriteTable(state, state->m_InitFE_1);
  1067. if (status < 0)
  1068. break;
  1069. if (state->type_A) {
  1070. status = Write16(state, FE_AG_REG_AG_PGA_MODE__A,
  1071. FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
  1072. 0);
  1073. } else {
  1074. if (state->PGA)
  1075. status = SetCfgPga(state, 0);
  1076. else
  1077. status =
  1078. Write16(state, B_FE_AG_REG_AG_PGA_MODE__A,
  1079. B_FE_AG_REG_AG_PGA_MODE_PFN_PCN_AFY_REN,
  1080. 0);
  1081. }
  1082. if (status < 0)
  1083. break;
  1084. status = Write16(state, FE_AG_REG_AG_AGC_SIO__A, state->m_FeAgRegAgAgcSio, 0x0000);
  1085. if (status < 0)
  1086. break;
  1087. status = Write16(state, FE_AG_REG_AG_PWD__A, state->m_FeAgRegAgPwd, 0x0000);
  1088. if (status < 0)
  1089. break;
  1090. status = WriteTable(state, state->m_InitFE_2);
  1091. if (status < 0)
  1092. break;
  1093. } while (0);
  1094. return status;
  1095. }
  1096. static int InitFT(struct drxd_state *state)
  1097. {
  1098. /*
  1099. norm OFFSET, MB says =2 voor 8K en =3 voor 2K waarschijnlijk
  1100. SC stuff
  1101. */
  1102. return Write16(state, FT_REG_COMM_EXEC__A, 0x0001, 0x0000);
  1103. }
  1104. static int SC_WaitForReady(struct drxd_state *state)
  1105. {
  1106. u16 curCmd;
  1107. int i;
  1108. for (i = 0; i < DRXD_MAX_RETRIES; i += 1) {
  1109. int status = Read16(state, SC_RA_RAM_CMD__A, &curCmd, 0);
  1110. if (status == 0 || curCmd == 0)
  1111. return status;
  1112. }
  1113. return -1;
  1114. }
  1115. static int SC_SendCommand(struct drxd_state *state, u16 cmd)
  1116. {
  1117. int status = 0;
  1118. u16 errCode;
  1119. Write16(state, SC_RA_RAM_CMD__A, cmd, 0);
  1120. SC_WaitForReady(state);
  1121. Read16(state, SC_RA_RAM_CMD_ADDR__A, &errCode, 0);
  1122. if (errCode == 0xFFFF) {
  1123. printk(KERN_ERR "Command Error\n");
  1124. status = -1;
  1125. }
  1126. return status;
  1127. }
  1128. static int SC_ProcStartCommand(struct drxd_state *state,
  1129. u16 subCmd, u16 param0, u16 param1)
  1130. {
  1131. int status = 0;
  1132. u16 scExec;
  1133. mutex_lock(&state->mutex);
  1134. do {
  1135. Read16(state, SC_COMM_EXEC__A, &scExec, 0);
  1136. if (scExec != 1) {
  1137. status = -1;
  1138. break;
  1139. }
  1140. SC_WaitForReady(state);
  1141. Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
  1142. Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
  1143. Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
  1144. SC_SendCommand(state, SC_RA_RAM_CMD_PROC_START);
  1145. } while (0);
  1146. mutex_unlock(&state->mutex);
  1147. return status;
  1148. }
  1149. static int SC_SetPrefParamCommand(struct drxd_state *state,
  1150. u16 subCmd, u16 param0, u16 param1)
  1151. {
  1152. int status;
  1153. mutex_lock(&state->mutex);
  1154. do {
  1155. status = SC_WaitForReady(state);
  1156. if (status < 0)
  1157. break;
  1158. status = Write16(state, SC_RA_RAM_CMD_ADDR__A, subCmd, 0);
  1159. if (status < 0)
  1160. break;
  1161. status = Write16(state, SC_RA_RAM_PARAM1__A, param1, 0);
  1162. if (status < 0)
  1163. break;
  1164. status = Write16(state, SC_RA_RAM_PARAM0__A, param0, 0);
  1165. if (status < 0)
  1166. break;
  1167. status = SC_SendCommand(state, SC_RA_RAM_CMD_SET_PREF_PARAM);
  1168. if (status < 0)
  1169. break;
  1170. } while (0);
  1171. mutex_unlock(&state->mutex);
  1172. return status;
  1173. }
  1174. #if 0
  1175. static int SC_GetOpParamCommand(struct drxd_state *state, u16 * result)
  1176. {
  1177. int status = 0;
  1178. mutex_lock(&state->mutex);
  1179. do {
  1180. status = SC_WaitForReady(state);
  1181. if (status < 0)
  1182. break;
  1183. status = SC_SendCommand(state, SC_RA_RAM_CMD_GET_OP_PARAM);
  1184. if (status < 0)
  1185. break;
  1186. status = Read16(state, SC_RA_RAM_PARAM0__A, result, 0);
  1187. if (status < 0)
  1188. break;
  1189. } while (0);
  1190. mutex_unlock(&state->mutex);
  1191. return status;
  1192. }
  1193. #endif
  1194. static int ConfigureMPEGOutput(struct drxd_state *state, int bEnableOutput)
  1195. {
  1196. int status;
  1197. do {
  1198. u16 EcOcRegIprInvMpg = 0;
  1199. u16 EcOcRegOcModeLop = 0;
  1200. u16 EcOcRegOcModeHip = 0;
  1201. u16 EcOcRegOcMpgSio = 0;
  1202. /*CHK_ERROR(Read16(state, EC_OC_REG_OC_MODE_LOP__A, &EcOcRegOcModeLop, 0)); */
  1203. if (state->operation_mode == OM_DVBT_Diversity_Front) {
  1204. if (bEnableOutput) {
  1205. EcOcRegOcModeHip |=
  1206. B_EC_OC_REG_OC_MODE_HIP_MPG_BUS_SRC_MONITOR;
  1207. } else
  1208. EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
  1209. EcOcRegOcModeLop |=
  1210. EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
  1211. } else {
  1212. EcOcRegOcModeLop = state->m_EcOcRegOcModeLop;
  1213. if (bEnableOutput)
  1214. EcOcRegOcMpgSio &= (~(EC_OC_REG_OC_MPG_SIO__M));
  1215. else
  1216. EcOcRegOcMpgSio |= EC_OC_REG_OC_MPG_SIO__M;
  1217. /* Don't Insert RS Byte */
  1218. if (state->insert_rs_byte) {
  1219. EcOcRegOcModeLop &=
  1220. (~(EC_OC_REG_OC_MODE_LOP_PAR_ENA__M));
  1221. EcOcRegOcModeHip &=
  1222. (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
  1223. EcOcRegOcModeHip |=
  1224. EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_ENABLE;
  1225. } else {
  1226. EcOcRegOcModeLop |=
  1227. EC_OC_REG_OC_MODE_LOP_PAR_ENA_DISABLE;
  1228. EcOcRegOcModeHip &=
  1229. (~EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL__M);
  1230. EcOcRegOcModeHip |=
  1231. EC_OC_REG_OC_MODE_HIP_MPG_PAR_VAL_DISABLE;
  1232. }
  1233. /* Mode = Parallel */
  1234. if (state->enable_parallel)
  1235. EcOcRegOcModeLop &=
  1236. (~(EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE__M));
  1237. else
  1238. EcOcRegOcModeLop |=
  1239. EC_OC_REG_OC_MODE_LOP_MPG_TRM_MDE_SERIAL;
  1240. }
  1241. /* Invert Data */
  1242. /* EcOcRegIprInvMpg |= 0x00FF; */
  1243. EcOcRegIprInvMpg &= (~(0x00FF));
  1244. /* Invert Error ( we don't use the pin ) */
  1245. /* EcOcRegIprInvMpg |= 0x0100; */
  1246. EcOcRegIprInvMpg &= (~(0x0100));
  1247. /* Invert Start ( we don't use the pin ) */
  1248. /* EcOcRegIprInvMpg |= 0x0200; */
  1249. EcOcRegIprInvMpg &= (~(0x0200));
  1250. /* Invert Valid ( we don't use the pin ) */
  1251. /* EcOcRegIprInvMpg |= 0x0400; */
  1252. EcOcRegIprInvMpg &= (~(0x0400));
  1253. /* Invert Clock */
  1254. /* EcOcRegIprInvMpg |= 0x0800; */
  1255. EcOcRegIprInvMpg &= (~(0x0800));
  1256. /* EcOcRegOcModeLop =0x05; */
  1257. status = Write16(state, EC_OC_REG_IPR_INV_MPG__A, EcOcRegIprInvMpg, 0);
  1258. if (status < 0)
  1259. break;
  1260. status = Write16(state, EC_OC_REG_OC_MODE_LOP__A, EcOcRegOcModeLop, 0);
  1261. if (status < 0)
  1262. break;
  1263. status = Write16(state, EC_OC_REG_OC_MODE_HIP__A, EcOcRegOcModeHip, 0x0000);
  1264. if (status < 0)
  1265. break;
  1266. status = Write16(state, EC_OC_REG_OC_MPG_SIO__A, EcOcRegOcMpgSio, 0);
  1267. if (status < 0)
  1268. break;
  1269. } while (0);
  1270. return status;
  1271. }
  1272. static int SetDeviceTypeId(struct drxd_state *state)
  1273. {
  1274. int status = 0;
  1275. u16 deviceId = 0;
  1276. do {
  1277. status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
  1278. if (status < 0)
  1279. break;
  1280. /* TODO: why twice? */
  1281. status = Read16(state, CC_REG_JTAGID_L__A, &deviceId, 0);
  1282. if (status < 0)
  1283. break;
  1284. printk(KERN_INFO "drxd: deviceId = %04x\n", deviceId);
  1285. state->type_A = 0;
  1286. state->PGA = 0;
  1287. state->diversity = 0;
  1288. if (deviceId == 0) { /* on A2 only 3975 available */
  1289. state->type_A = 1;
  1290. printk(KERN_INFO "DRX3975D-A2\n");
  1291. } else {
  1292. deviceId >>= 12;
  1293. printk(KERN_INFO "DRX397%dD-B1\n", deviceId);
  1294. switch (deviceId) {
  1295. case 4:
  1296. state->diversity = 1;
  1297. case 3:
  1298. case 7:
  1299. state->PGA = 1;
  1300. break;
  1301. case 6:
  1302. state->diversity = 1;
  1303. case 5:
  1304. case 8:
  1305. break;
  1306. default:
  1307. status = -1;
  1308. break;
  1309. }
  1310. }
  1311. } while (0);
  1312. if (status < 0)
  1313. return status;
  1314. /* Init Table selection */
  1315. state->m_InitAtomicRead = DRXD_InitAtomicRead;
  1316. state->m_InitSC = DRXD_InitSC;
  1317. state->m_ResetECRAM = DRXD_ResetECRAM;
  1318. if (state->type_A) {
  1319. state->m_ResetCEFR = DRXD_ResetCEFR;
  1320. state->m_InitFE_1 = DRXD_InitFEA2_1;
  1321. state->m_InitFE_2 = DRXD_InitFEA2_2;
  1322. state->m_InitCP = DRXD_InitCPA2;
  1323. state->m_InitCE = DRXD_InitCEA2;
  1324. state->m_InitEQ = DRXD_InitEQA2;
  1325. state->m_InitEC = DRXD_InitECA2;
  1326. if (load_firmware(state, DRX_FW_FILENAME_A2))
  1327. return -EIO;
  1328. } else {
  1329. state->m_ResetCEFR = NULL;
  1330. state->m_InitFE_1 = DRXD_InitFEB1_1;
  1331. state->m_InitFE_2 = DRXD_InitFEB1_2;
  1332. state->m_InitCP = DRXD_InitCPB1;
  1333. state->m_InitCE = DRXD_InitCEB1;
  1334. state->m_InitEQ = DRXD_InitEQB1;
  1335. state->m_InitEC = DRXD_InitECB1;
  1336. if (load_firmware(state, DRX_FW_FILENAME_B1))
  1337. return -EIO;
  1338. }
  1339. if (state->diversity) {
  1340. state->m_InitDiversityFront = DRXD_InitDiversityFront;
  1341. state->m_InitDiversityEnd = DRXD_InitDiversityEnd;
  1342. state->m_DisableDiversity = DRXD_DisableDiversity;
  1343. state->m_StartDiversityFront = DRXD_StartDiversityFront;
  1344. state->m_StartDiversityEnd = DRXD_StartDiversityEnd;
  1345. state->m_DiversityDelay8MHZ = DRXD_DiversityDelay8MHZ;
  1346. state->m_DiversityDelay6MHZ = DRXD_DiversityDelay6MHZ;
  1347. } else {
  1348. state->m_InitDiversityFront = NULL;
  1349. state->m_InitDiversityEnd = NULL;
  1350. state->m_DisableDiversity = NULL;
  1351. state->m_StartDiversityFront = NULL;
  1352. state->m_StartDiversityEnd = NULL;
  1353. state->m_DiversityDelay8MHZ = NULL;
  1354. state->m_DiversityDelay6MHZ = NULL;
  1355. }
  1356. return status;
  1357. }
  1358. static int CorrectSysClockDeviation(struct drxd_state *state)
  1359. {
  1360. int status;
  1361. s32 incr = 0;
  1362. s32 nomincr = 0;
  1363. u32 bandwidth = 0;
  1364. u32 sysClockInHz = 0;
  1365. u32 sysClockFreq = 0; /* in kHz */
  1366. s16 oscClockDeviation;
  1367. s16 Diff;
  1368. do {
  1369. /* Retrieve bandwidth and incr, sanity check */
  1370. /* These accesses should be AtomicReadReg32, but that
  1371. causes trouble (at least for diversity */
  1372. status = Read32(state, LC_RA_RAM_IFINCR_NOM_L__A, ((u32 *) &nomincr), 0);
  1373. if (status < 0)
  1374. break;
  1375. status = Read32(state, FE_IF_REG_INCR0__A, (u32 *) &incr, 0);
  1376. if (status < 0)
  1377. break;
  1378. if (state->type_A) {
  1379. if ((nomincr - incr < -500) || (nomincr - incr > 500))
  1380. break;
  1381. } else {
  1382. if ((nomincr - incr < -2000) || (nomincr - incr > 2000))
  1383. break;
  1384. }
  1385. switch (state->param.u.ofdm.bandwidth) {
  1386. case BANDWIDTH_8_MHZ:
  1387. bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
  1388. break;
  1389. case BANDWIDTH_7_MHZ:
  1390. bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
  1391. break;
  1392. case BANDWIDTH_6_MHZ:
  1393. bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
  1394. break;
  1395. default:
  1396. return -1;
  1397. break;
  1398. }
  1399. /* Compute new sysclock value
  1400. sysClockFreq = (((incr + 2^23)*bandwidth)/2^21)/1000 */
  1401. incr += (1 << 23);
  1402. sysClockInHz = MulDiv32(incr, bandwidth, 1 << 21);
  1403. sysClockFreq = (u32) (sysClockInHz / 1000);
  1404. /* rounding */
  1405. if ((sysClockInHz % 1000) > 500)
  1406. sysClockFreq++;
  1407. /* Compute clock deviation in ppm */
  1408. oscClockDeviation = (u16) ((((s32) (sysClockFreq) -
  1409. (s32)
  1410. (state->expected_sys_clock_freq)) *
  1411. 1000000L) /
  1412. (s32)
  1413. (state->expected_sys_clock_freq));
  1414. Diff = oscClockDeviation - state->osc_clock_deviation;
  1415. /*printk(KERN_INFO "sysclockdiff=%d\n", Diff); */
  1416. if (Diff >= -200 && Diff <= 200) {
  1417. state->sys_clock_freq = (u16) sysClockFreq;
  1418. if (oscClockDeviation != state->osc_clock_deviation) {
  1419. if (state->config.osc_deviation) {
  1420. state->config.osc_deviation(state->priv,
  1421. oscClockDeviation,
  1422. 1);
  1423. state->osc_clock_deviation =
  1424. oscClockDeviation;
  1425. }
  1426. }
  1427. /* switch OFF SRMM scan in SC */
  1428. status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DONT_SCAN, 0);
  1429. if (status < 0)
  1430. break;
  1431. /* overrule FE_IF internal value for
  1432. proper re-locking */
  1433. status = Write16(state, SC_RA_RAM_IF_SAVE__AX, state->current_fe_if_incr, 0);
  1434. if (status < 0)
  1435. break;
  1436. state->cscd_state = CSCD_SAVED;
  1437. }
  1438. } while (0);
  1439. return status;
  1440. }
  1441. static int DRX_Stop(struct drxd_state *state)
  1442. {
  1443. int status;
  1444. if (state->drxd_state != DRXD_STARTED)
  1445. return 0;
  1446. do {
  1447. if (state->cscd_state != CSCD_SAVED) {
  1448. u32 lock;
  1449. status = DRX_GetLockStatus(state, &lock);
  1450. if (status < 0)
  1451. break;
  1452. }
  1453. status = StopOC(state);
  1454. if (status < 0)
  1455. break;
  1456. state->drxd_state = DRXD_STOPPED;
  1457. status = ConfigureMPEGOutput(state, 0);
  1458. if (status < 0)
  1459. break;
  1460. if (state->type_A) {
  1461. /* Stop relevant processors off the device */
  1462. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0x0000);
  1463. if (status < 0)
  1464. break;
  1465. status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1466. if (status < 0)
  1467. break;
  1468. status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1469. if (status < 0)
  1470. break;
  1471. } else {
  1472. /* Stop all processors except HI & CC & FE */
  1473. status = Write16(state, B_SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1474. if (status < 0)
  1475. break;
  1476. status = Write16(state, B_LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1477. if (status < 0)
  1478. break;
  1479. status = Write16(state, B_FT_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1480. if (status < 0)
  1481. break;
  1482. status = Write16(state, B_CP_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1483. if (status < 0)
  1484. break;
  1485. status = Write16(state, B_CE_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1486. if (status < 0)
  1487. break;
  1488. status = Write16(state, B_EQ_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  1489. if (status < 0)
  1490. break;
  1491. status = Write16(state, EC_OD_REG_COMM_EXEC__A, 0x0000, 0);
  1492. if (status < 0)
  1493. break;
  1494. }
  1495. } while (0);
  1496. return status;
  1497. }
  1498. int SetOperationMode(struct drxd_state *state, int oMode)
  1499. {
  1500. int status;
  1501. do {
  1502. if (state->drxd_state != DRXD_STOPPED) {
  1503. status = -1;
  1504. break;
  1505. }
  1506. if (oMode == state->operation_mode) {
  1507. status = 0;
  1508. break;
  1509. }
  1510. if (oMode != OM_Default && !state->diversity) {
  1511. status = -1;
  1512. break;
  1513. }
  1514. switch (oMode) {
  1515. case OM_DVBT_Diversity_Front:
  1516. status = WriteTable(state, state->m_InitDiversityFront);
  1517. break;
  1518. case OM_DVBT_Diversity_End:
  1519. status = WriteTable(state, state->m_InitDiversityEnd);
  1520. break;
  1521. case OM_Default:
  1522. /* We need to check how to
  1523. get DRXD out of diversity */
  1524. default:
  1525. status = WriteTable(state, state->m_DisableDiversity);
  1526. break;
  1527. }
  1528. } while (0);
  1529. if (!status)
  1530. state->operation_mode = oMode;
  1531. return status;
  1532. }
  1533. static int StartDiversity(struct drxd_state *state)
  1534. {
  1535. int status = 0;
  1536. u16 rcControl;
  1537. do {
  1538. if (state->operation_mode == OM_DVBT_Diversity_Front) {
  1539. status = WriteTable(state, state->m_StartDiversityFront);
  1540. if (status < 0)
  1541. break;
  1542. } else if (state->operation_mode == OM_DVBT_Diversity_End) {
  1543. status = WriteTable(state, state->m_StartDiversityEnd);
  1544. if (status < 0)
  1545. break;
  1546. if (state->param.u.ofdm.bandwidth == BANDWIDTH_8_MHZ) {
  1547. status = WriteTable(state, state->m_DiversityDelay8MHZ);
  1548. if (status < 0)
  1549. break;
  1550. } else {
  1551. status = WriteTable(state, state->m_DiversityDelay6MHZ);
  1552. if (status < 0)
  1553. break;
  1554. }
  1555. status = Read16(state, B_EQ_REG_RC_SEL_CAR__A, &rcControl, 0);
  1556. if (status < 0)
  1557. break;
  1558. rcControl &= ~(B_EQ_REG_RC_SEL_CAR_FFTMODE__M);
  1559. rcControl |= B_EQ_REG_RC_SEL_CAR_DIV_ON |
  1560. /* combining enabled */
  1561. B_EQ_REG_RC_SEL_CAR_MEAS_A_CC |
  1562. B_EQ_REG_RC_SEL_CAR_PASS_A_CC |
  1563. B_EQ_REG_RC_SEL_CAR_LOCAL_A_CC;
  1564. status = Write16(state, B_EQ_REG_RC_SEL_CAR__A, rcControl, 0);
  1565. if (status < 0)
  1566. break;
  1567. }
  1568. } while (0);
  1569. return status;
  1570. }
  1571. static int SetFrequencyShift(struct drxd_state *state,
  1572. u32 offsetFreq, int channelMirrored)
  1573. {
  1574. int negativeShift = (state->tuner_mirrors == channelMirrored);
  1575. /* Handle all mirroring
  1576. *
  1577. * Note: ADC mirroring (aliasing) is implictly handled by limiting
  1578. * feFsRegAddInc to 28 bits below
  1579. * (if the result before masking is more than 28 bits, this means
  1580. * that the ADC is mirroring.
  1581. * The masking is in fact the aliasing of the ADC)
  1582. *
  1583. */
  1584. /* Compute register value, unsigned computation */
  1585. state->fe_fs_add_incr = MulDiv32(state->intermediate_freq +
  1586. offsetFreq,
  1587. 1 << 28, state->sys_clock_freq);
  1588. /* Remove integer part */
  1589. state->fe_fs_add_incr &= 0x0FFFFFFFL;
  1590. if (negativeShift)
  1591. state->fe_fs_add_incr = ((1 << 28) - state->fe_fs_add_incr);
  1592. /* Save the frequency shift without tunerOffset compensation
  1593. for CtrlGetChannel. */
  1594. state->org_fe_fs_add_incr = MulDiv32(state->intermediate_freq,
  1595. 1 << 28, state->sys_clock_freq);
  1596. /* Remove integer part */
  1597. state->org_fe_fs_add_incr &= 0x0FFFFFFFL;
  1598. if (negativeShift)
  1599. state->org_fe_fs_add_incr = ((1L << 28) -
  1600. state->org_fe_fs_add_incr);
  1601. return Write32(state, FE_FS_REG_ADD_INC_LOP__A,
  1602. state->fe_fs_add_incr, 0);
  1603. }
  1604. static int SetCfgNoiseCalibration(struct drxd_state *state,
  1605. struct SNoiseCal *noiseCal)
  1606. {
  1607. u16 beOptEna;
  1608. int status = 0;
  1609. do {
  1610. status = Read16(state, SC_RA_RAM_BE_OPT_ENA__A, &beOptEna, 0);
  1611. if (status < 0)
  1612. break;
  1613. if (noiseCal->cpOpt) {
  1614. beOptEna |= (1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
  1615. } else {
  1616. beOptEna &= ~(1 << SC_RA_RAM_BE_OPT_ENA_CP_OPT);
  1617. status = Write16(state, CP_REG_AC_NEXP_OFFS__A, noiseCal->cpNexpOfs, 0);
  1618. if (status < 0)
  1619. break;
  1620. }
  1621. status = Write16(state, SC_RA_RAM_BE_OPT_ENA__A, beOptEna, 0);
  1622. if (status < 0)
  1623. break;
  1624. if (!state->type_A) {
  1625. status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_2K__A, noiseCal->tdCal2k, 0);
  1626. if (status < 0)
  1627. break;
  1628. status = Write16(state, B_SC_RA_RAM_CO_TD_CAL_8K__A, noiseCal->tdCal8k, 0);
  1629. if (status < 0)
  1630. break;
  1631. }
  1632. } while (0);
  1633. return status;
  1634. }
  1635. static int DRX_Start(struct drxd_state *state, s32 off)
  1636. {
  1637. struct dvb_ofdm_parameters *p = &state->param.u.ofdm;
  1638. int status;
  1639. u16 transmissionParams = 0;
  1640. u16 operationMode = 0;
  1641. u16 qpskTdTpsPwr = 0;
  1642. u16 qam16TdTpsPwr = 0;
  1643. u16 qam64TdTpsPwr = 0;
  1644. u32 feIfIncr = 0;
  1645. u32 bandwidth = 0;
  1646. int mirrorFreqSpect;
  1647. u16 qpskSnCeGain = 0;
  1648. u16 qam16SnCeGain = 0;
  1649. u16 qam64SnCeGain = 0;
  1650. u16 qpskIsGainMan = 0;
  1651. u16 qam16IsGainMan = 0;
  1652. u16 qam64IsGainMan = 0;
  1653. u16 qpskIsGainExp = 0;
  1654. u16 qam16IsGainExp = 0;
  1655. u16 qam64IsGainExp = 0;
  1656. u16 bandwidthParam = 0;
  1657. if (off < 0)
  1658. off = (off - 500) / 1000;
  1659. else
  1660. off = (off + 500) / 1000;
  1661. do {
  1662. if (state->drxd_state != DRXD_STOPPED)
  1663. return -1;
  1664. status = ResetECOD(state);
  1665. if (status < 0)
  1666. break;
  1667. if (state->type_A) {
  1668. status = InitSC(state);
  1669. if (status < 0)
  1670. break;
  1671. } else {
  1672. status = InitFT(state);
  1673. if (status < 0)
  1674. break;
  1675. status = InitCP(state);
  1676. if (status < 0)
  1677. break;
  1678. status = InitCE(state);
  1679. if (status < 0)
  1680. break;
  1681. status = InitEQ(state);
  1682. if (status < 0)
  1683. break;
  1684. status = InitSC(state);
  1685. if (status < 0)
  1686. break;
  1687. }
  1688. /* Restore current IF & RF AGC settings */
  1689. status = SetCfgIfAgc(state, &state->if_agc_cfg);
  1690. if (status < 0)
  1691. break;
  1692. status = SetCfgRfAgc(state, &state->rf_agc_cfg);
  1693. if (status < 0)
  1694. break;
  1695. mirrorFreqSpect = (state->param.inversion == INVERSION_ON);
  1696. switch (p->transmission_mode) {
  1697. default: /* Not set, detect it automatically */
  1698. operationMode |= SC_RA_RAM_OP_AUTO_MODE__M;
  1699. /* fall through , try first guess DRX_FFTMODE_8K */
  1700. case TRANSMISSION_MODE_8K:
  1701. transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_8K;
  1702. if (state->type_A) {
  1703. status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_8K, 0x0000);
  1704. if (status < 0)
  1705. break;
  1706. qpskSnCeGain = 99;
  1707. qam16SnCeGain = 83;
  1708. qam64SnCeGain = 67;
  1709. }
  1710. break;
  1711. case TRANSMISSION_MODE_2K:
  1712. transmissionParams |= SC_RA_RAM_OP_PARAM_MODE_2K;
  1713. if (state->type_A) {
  1714. status = Write16(state, EC_SB_REG_TR_MODE__A, EC_SB_REG_TR_MODE_2K, 0x0000);
  1715. if (status < 0)
  1716. break;
  1717. qpskSnCeGain = 97;
  1718. qam16SnCeGain = 71;
  1719. qam64SnCeGain = 65;
  1720. }
  1721. break;
  1722. }
  1723. switch (p->guard_interval) {
  1724. case GUARD_INTERVAL_1_4:
  1725. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
  1726. break;
  1727. case GUARD_INTERVAL_1_8:
  1728. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_8;
  1729. break;
  1730. case GUARD_INTERVAL_1_16:
  1731. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_16;
  1732. break;
  1733. case GUARD_INTERVAL_1_32:
  1734. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_32;
  1735. break;
  1736. default: /* Not set, detect it automatically */
  1737. operationMode |= SC_RA_RAM_OP_AUTO_GUARD__M;
  1738. /* try first guess 1/4 */
  1739. transmissionParams |= SC_RA_RAM_OP_PARAM_GUARD_4;
  1740. break;
  1741. }
  1742. switch (p->hierarchy_information) {
  1743. case HIERARCHY_1:
  1744. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A1;
  1745. if (state->type_A) {
  1746. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0001, 0x0000);
  1747. if (status < 0)
  1748. break;
  1749. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0001, 0x0000);
  1750. if (status < 0)
  1751. break;
  1752. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1753. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA1;
  1754. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA1;
  1755. qpskIsGainMan =
  1756. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1757. qam16IsGainMan =
  1758. SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
  1759. qam64IsGainMan =
  1760. SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
  1761. qpskIsGainExp =
  1762. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1763. qam16IsGainExp =
  1764. SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
  1765. qam64IsGainExp =
  1766. SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
  1767. }
  1768. break;
  1769. case HIERARCHY_2:
  1770. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A2;
  1771. if (state->type_A) {
  1772. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0002, 0x0000);
  1773. if (status < 0)
  1774. break;
  1775. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0002, 0x0000);
  1776. if (status < 0)
  1777. break;
  1778. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1779. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA2;
  1780. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA2;
  1781. qpskIsGainMan =
  1782. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1783. qam16IsGainMan =
  1784. SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_MAN__PRE;
  1785. qam64IsGainMan =
  1786. SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_MAN__PRE;
  1787. qpskIsGainExp =
  1788. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1789. qam16IsGainExp =
  1790. SC_RA_RAM_EQ_IS_GAIN_16QAM_A2_EXP__PRE;
  1791. qam64IsGainExp =
  1792. SC_RA_RAM_EQ_IS_GAIN_64QAM_A2_EXP__PRE;
  1793. }
  1794. break;
  1795. case HIERARCHY_4:
  1796. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_A4;
  1797. if (state->type_A) {
  1798. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0003, 0x0000);
  1799. if (status < 0)
  1800. break;
  1801. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0003, 0x0000);
  1802. if (status < 0)
  1803. break;
  1804. qpskTdTpsPwr = EQ_TD_TPS_PWR_UNKNOWN;
  1805. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHA4;
  1806. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHA4;
  1807. qpskIsGainMan =
  1808. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_MAN__PRE;
  1809. qam16IsGainMan =
  1810. SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_MAN__PRE;
  1811. qam64IsGainMan =
  1812. SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_MAN__PRE;
  1813. qpskIsGainExp =
  1814. SC_RA_RAM_EQ_IS_GAIN_UNKNOWN_EXP__PRE;
  1815. qam16IsGainExp =
  1816. SC_RA_RAM_EQ_IS_GAIN_16QAM_A4_EXP__PRE;
  1817. qam64IsGainExp =
  1818. SC_RA_RAM_EQ_IS_GAIN_64QAM_A4_EXP__PRE;
  1819. }
  1820. break;
  1821. case HIERARCHY_AUTO:
  1822. default:
  1823. /* Not set, detect it automatically, start with none */
  1824. operationMode |= SC_RA_RAM_OP_AUTO_HIER__M;
  1825. transmissionParams |= SC_RA_RAM_OP_PARAM_HIER_NO;
  1826. if (state->type_A) {
  1827. status = Write16(state, EQ_REG_OT_ALPHA__A, 0x0000, 0x0000);
  1828. if (status < 0)
  1829. break;
  1830. status = Write16(state, EC_SB_REG_ALPHA__A, 0x0000, 0x0000);
  1831. if (status < 0)
  1832. break;
  1833. qpskTdTpsPwr = EQ_TD_TPS_PWR_QPSK;
  1834. qam16TdTpsPwr = EQ_TD_TPS_PWR_QAM16_ALPHAN;
  1835. qam64TdTpsPwr = EQ_TD_TPS_PWR_QAM64_ALPHAN;
  1836. qpskIsGainMan =
  1837. SC_RA_RAM_EQ_IS_GAIN_QPSK_MAN__PRE;
  1838. qam16IsGainMan =
  1839. SC_RA_RAM_EQ_IS_GAIN_16QAM_MAN__PRE;
  1840. qam64IsGainMan =
  1841. SC_RA_RAM_EQ_IS_GAIN_64QAM_MAN__PRE;
  1842. qpskIsGainExp =
  1843. SC_RA_RAM_EQ_IS_GAIN_QPSK_EXP__PRE;
  1844. qam16IsGainExp =
  1845. SC_RA_RAM_EQ_IS_GAIN_16QAM_EXP__PRE;
  1846. qam64IsGainExp =
  1847. SC_RA_RAM_EQ_IS_GAIN_64QAM_EXP__PRE;
  1848. }
  1849. break;
  1850. }
  1851. status = status;
  1852. if (status < 0)
  1853. break;
  1854. switch (p->constellation) {
  1855. default:
  1856. operationMode |= SC_RA_RAM_OP_AUTO_CONST__M;
  1857. /* fall through , try first guess
  1858. DRX_CONSTELLATION_QAM64 */
  1859. case QAM_64:
  1860. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM64;
  1861. if (state->type_A) {
  1862. status = Write16(state, EQ_REG_OT_CONST__A, 0x0002, 0x0000);
  1863. if (status < 0)
  1864. break;
  1865. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_64QAM, 0x0000);
  1866. if (status < 0)
  1867. break;
  1868. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0020, 0x0000);
  1869. if (status < 0)
  1870. break;
  1871. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0008, 0x0000);
  1872. if (status < 0)
  1873. break;
  1874. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0002, 0x0000);
  1875. if (status < 0)
  1876. break;
  1877. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam64TdTpsPwr, 0x0000);
  1878. if (status < 0)
  1879. break;
  1880. status = Write16(state, EQ_REG_SN_CEGAIN__A, qam64SnCeGain, 0x0000);
  1881. if (status < 0)
  1882. break;
  1883. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam64IsGainMan, 0x0000);
  1884. if (status < 0)
  1885. break;
  1886. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam64IsGainExp, 0x0000);
  1887. if (status < 0)
  1888. break;
  1889. }
  1890. break;
  1891. case QPSK:
  1892. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QPSK;
  1893. if (state->type_A) {
  1894. status = Write16(state, EQ_REG_OT_CONST__A, 0x0000, 0x0000);
  1895. if (status < 0)
  1896. break;
  1897. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_QPSK, 0x0000);
  1898. if (status < 0)
  1899. break;
  1900. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
  1901. if (status < 0)
  1902. break;
  1903. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0000, 0x0000);
  1904. if (status < 0)
  1905. break;
  1906. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
  1907. if (status < 0)
  1908. break;
  1909. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qpskTdTpsPwr, 0x0000);
  1910. if (status < 0)
  1911. break;
  1912. status = Write16(state, EQ_REG_SN_CEGAIN__A, qpskSnCeGain, 0x0000);
  1913. if (status < 0)
  1914. break;
  1915. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qpskIsGainMan, 0x0000);
  1916. if (status < 0)
  1917. break;
  1918. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qpskIsGainExp, 0x0000);
  1919. if (status < 0)
  1920. break;
  1921. }
  1922. break;
  1923. case QAM_16:
  1924. transmissionParams |= SC_RA_RAM_OP_PARAM_CONST_QAM16;
  1925. if (state->type_A) {
  1926. status = Write16(state, EQ_REG_OT_CONST__A, 0x0001, 0x0000);
  1927. if (status < 0)
  1928. break;
  1929. status = Write16(state, EC_SB_REG_CONST__A, EC_SB_REG_CONST_16QAM, 0x0000);
  1930. if (status < 0)
  1931. break;
  1932. status = Write16(state, EC_SB_REG_SCALE_MSB__A, 0x0010, 0x0000);
  1933. if (status < 0)
  1934. break;
  1935. status = Write16(state, EC_SB_REG_SCALE_BIT2__A, 0x0004, 0x0000);
  1936. if (status < 0)
  1937. break;
  1938. status = Write16(state, EC_SB_REG_SCALE_LSB__A, 0x0000, 0x0000);
  1939. if (status < 0)
  1940. break;
  1941. status = Write16(state, EQ_REG_TD_TPS_PWR_OFS__A, qam16TdTpsPwr, 0x0000);
  1942. if (status < 0)
  1943. break;
  1944. status = Write16(state, EQ_REG_SN_CEGAIN__A, qam16SnCeGain, 0x0000);
  1945. if (status < 0)
  1946. break;
  1947. status = Write16(state, EQ_REG_IS_GAIN_MAN__A, qam16IsGainMan, 0x0000);
  1948. if (status < 0)
  1949. break;
  1950. status = Write16(state, EQ_REG_IS_GAIN_EXP__A, qam16IsGainExp, 0x0000);
  1951. if (status < 0)
  1952. break;
  1953. }
  1954. break;
  1955. }
  1956. status = status;
  1957. if (status < 0)
  1958. break;
  1959. switch (DRX_CHANNEL_HIGH) {
  1960. default:
  1961. case DRX_CHANNEL_AUTO:
  1962. case DRX_CHANNEL_LOW:
  1963. transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_LO;
  1964. status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_LO, 0x0000);
  1965. if (status < 0)
  1966. break;
  1967. break;
  1968. case DRX_CHANNEL_HIGH:
  1969. transmissionParams |= SC_RA_RAM_OP_PARAM_PRIO_HI;
  1970. status = Write16(state, EC_SB_REG_PRIOR__A, EC_SB_REG_PRIOR_HI, 0x0000);
  1971. if (status < 0)
  1972. break;
  1973. break;
  1974. }
  1975. switch (p->code_rate_HP) {
  1976. case FEC_1_2:
  1977. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_1_2;
  1978. if (state->type_A) {
  1979. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C1_2, 0x0000);
  1980. if (status < 0)
  1981. break;
  1982. }
  1983. break;
  1984. default:
  1985. operationMode |= SC_RA_RAM_OP_AUTO_RATE__M;
  1986. case FEC_2_3:
  1987. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_2_3;
  1988. if (state->type_A) {
  1989. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C2_3, 0x0000);
  1990. if (status < 0)
  1991. break;
  1992. }
  1993. break;
  1994. case FEC_3_4:
  1995. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_3_4;
  1996. if (state->type_A) {
  1997. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C3_4, 0x0000);
  1998. if (status < 0)
  1999. break;
  2000. }
  2001. break;
  2002. case FEC_5_6:
  2003. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_5_6;
  2004. if (state->type_A) {
  2005. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C5_6, 0x0000);
  2006. if (status < 0)
  2007. break;
  2008. }
  2009. break;
  2010. case FEC_7_8:
  2011. transmissionParams |= SC_RA_RAM_OP_PARAM_RATE_7_8;
  2012. if (state->type_A) {
  2013. status = Write16(state, EC_VD_REG_SET_CODERATE__A, EC_VD_REG_SET_CODERATE_C7_8, 0x0000);
  2014. if (status < 0)
  2015. break;
  2016. }
  2017. break;
  2018. }
  2019. status = status;
  2020. if (status < 0)
  2021. break;
  2022. /* First determine real bandwidth (Hz) */
  2023. /* Also set delay for impulse noise cruncher (only A2) */
  2024. /* Also set parameters for EC_OC fix, note
  2025. EC_OC_REG_TMD_HIL_MAR is changed
  2026. by SC for fix for some 8K,1/8 guard but is restored by
  2027. InitEC and ResetEC
  2028. functions */
  2029. switch (p->bandwidth) {
  2030. case BANDWIDTH_AUTO:
  2031. case BANDWIDTH_8_MHZ:
  2032. /* (64/7)*(8/8)*1000000 */
  2033. bandwidth = DRXD_BANDWIDTH_8MHZ_IN_HZ;
  2034. bandwidthParam = 0;
  2035. status = Write16(state,
  2036. FE_AG_REG_IND_DEL__A, 50, 0x0000);
  2037. break;
  2038. case BANDWIDTH_7_MHZ:
  2039. /* (64/7)*(7/8)*1000000 */
  2040. bandwidth = DRXD_BANDWIDTH_7MHZ_IN_HZ;
  2041. bandwidthParam = 0x4807; /*binary:0100 1000 0000 0111 */
  2042. status = Write16(state,
  2043. FE_AG_REG_IND_DEL__A, 59, 0x0000);
  2044. break;
  2045. case BANDWIDTH_6_MHZ:
  2046. /* (64/7)*(6/8)*1000000 */
  2047. bandwidth = DRXD_BANDWIDTH_6MHZ_IN_HZ;
  2048. bandwidthParam = 0x0F07; /*binary: 0000 1111 0000 0111 */
  2049. status = Write16(state,
  2050. FE_AG_REG_IND_DEL__A, 71, 0x0000);
  2051. break;
  2052. default:
  2053. status = -EINVAL;
  2054. }
  2055. if (status < 0)
  2056. break;
  2057. status = Write16(state, SC_RA_RAM_BAND__A, bandwidthParam, 0x0000);
  2058. if (status < 0)
  2059. break;
  2060. {
  2061. u16 sc_config;
  2062. status = Read16(state, SC_RA_RAM_CONFIG__A, &sc_config, 0);
  2063. if (status < 0)
  2064. break;
  2065. /* enable SLAVE mode in 2k 1/32 to
  2066. prevent timing change glitches */
  2067. if ((p->transmission_mode == TRANSMISSION_MODE_2K) &&
  2068. (p->guard_interval == GUARD_INTERVAL_1_32)) {
  2069. /* enable slave */
  2070. sc_config |= SC_RA_RAM_CONFIG_SLAVE__M;
  2071. } else {
  2072. /* disable slave */
  2073. sc_config &= ~SC_RA_RAM_CONFIG_SLAVE__M;
  2074. }
  2075. status = Write16(state, SC_RA_RAM_CONFIG__A, sc_config, 0);
  2076. if (status < 0)
  2077. break;
  2078. }
  2079. status = SetCfgNoiseCalibration(state, &state->noise_cal);
  2080. if (status < 0)
  2081. break;
  2082. if (state->cscd_state == CSCD_INIT) {
  2083. /* switch on SRMM scan in SC */
  2084. status = Write16(state, SC_RA_RAM_SAMPLE_RATE_COUNT__A, DRXD_OSCDEV_DO_SCAN, 0x0000);
  2085. if (status < 0)
  2086. break;
  2087. /* CHK_ERROR(Write16(SC_RA_RAM_SAMPLE_RATE_STEP__A, DRXD_OSCDEV_STEP, 0x0000));*/
  2088. state->cscd_state = CSCD_SET;
  2089. }
  2090. /* Now compute FE_IF_REG_INCR */
  2091. /*((( SysFreq/BandWidth)/2)/2) -1) * 2^23) =>
  2092. ((SysFreq / BandWidth) * (2^21) ) - (2^23) */
  2093. feIfIncr = MulDiv32(state->sys_clock_freq * 1000,
  2094. (1ULL << 21), bandwidth) - (1 << 23);
  2095. status = Write16(state, FE_IF_REG_INCR0__A, (u16) (feIfIncr & FE_IF_REG_INCR0__M), 0x0000);
  2096. if (status < 0)
  2097. break;
  2098. status = Write16(state, FE_IF_REG_INCR1__A, (u16) ((feIfIncr >> FE_IF_REG_INCR0__W) & FE_IF_REG_INCR1__M), 0x0000);
  2099. if (status < 0)
  2100. break;
  2101. /* Bandwidth setting done */
  2102. /* Mirror & frequency offset */
  2103. SetFrequencyShift(state, off, mirrorFreqSpect);
  2104. /* Start SC, write channel settings to SC */
  2105. /* Enable SC after setting all other parameters */
  2106. status = Write16(state, SC_COMM_STATE__A, 0, 0x0000);
  2107. if (status < 0)
  2108. break;
  2109. status = Write16(state, SC_COMM_EXEC__A, 1, 0x0000);
  2110. if (status < 0)
  2111. break;
  2112. /* Write SC parameter registers, operation mode */
  2113. #if 1
  2114. operationMode = (SC_RA_RAM_OP_AUTO_MODE__M |
  2115. SC_RA_RAM_OP_AUTO_GUARD__M |
  2116. SC_RA_RAM_OP_AUTO_CONST__M |
  2117. SC_RA_RAM_OP_AUTO_HIER__M |
  2118. SC_RA_RAM_OP_AUTO_RATE__M);
  2119. #endif
  2120. status = SC_SetPrefParamCommand(state, 0x0000, transmissionParams, operationMode);
  2121. if (status < 0)
  2122. break;
  2123. /* Start correct processes to get in lock */
  2124. status = SC_ProcStartCommand(state, SC_RA_RAM_PROC_LOCKTRACK, SC_RA_RAM_SW_EVENT_RUN_NMASK__M, SC_RA_RAM_LOCKTRACK_MIN);
  2125. if (status < 0)
  2126. break;
  2127. status = StartOC(state);
  2128. if (status < 0)
  2129. break;
  2130. if (state->operation_mode != OM_Default) {
  2131. status = StartDiversity(state);
  2132. if (status < 0)
  2133. break;
  2134. }
  2135. state->drxd_state = DRXD_STARTED;
  2136. } while (0);
  2137. return status;
  2138. }
  2139. static int CDRXD(struct drxd_state *state, u32 IntermediateFrequency)
  2140. {
  2141. u32 ulRfAgcOutputLevel = 0xffffffff;
  2142. u32 ulRfAgcSettleLevel = 528; /* Optimum value for MT2060 */
  2143. u32 ulRfAgcMinLevel = 0; /* Currently unused */
  2144. u32 ulRfAgcMaxLevel = DRXD_FE_CTRL_MAX; /* Currently unused */
  2145. u32 ulRfAgcSpeed = 0; /* Currently unused */
  2146. u32 ulRfAgcMode = 0; /*2; Off */
  2147. u32 ulRfAgcR1 = 820;
  2148. u32 ulRfAgcR2 = 2200;
  2149. u32 ulRfAgcR3 = 150;
  2150. u32 ulIfAgcMode = 0; /* Auto */
  2151. u32 ulIfAgcOutputLevel = 0xffffffff;
  2152. u32 ulIfAgcSettleLevel = 0xffffffff;
  2153. u32 ulIfAgcMinLevel = 0xffffffff;
  2154. u32 ulIfAgcMaxLevel = 0xffffffff;
  2155. u32 ulIfAgcSpeed = 0xffffffff;
  2156. u32 ulIfAgcR1 = 820;
  2157. u32 ulIfAgcR2 = 2200;
  2158. u32 ulIfAgcR3 = 150;
  2159. u32 ulClock = state->config.clock;
  2160. u32 ulSerialMode = 0;
  2161. u32 ulEcOcRegOcModeLop = 4; /* Dynamic DTO source */
  2162. u32 ulHiI2cDelay = HI_I2C_DELAY;
  2163. u32 ulHiI2cBridgeDelay = HI_I2C_BRIDGE_DELAY;
  2164. u32 ulHiI2cPatch = 0;
  2165. u32 ulEnvironment = APPENV_PORTABLE;
  2166. u32 ulEnvironmentDiversity = APPENV_MOBILE;
  2167. u32 ulIFFilter = IFFILTER_SAW;
  2168. state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2169. state->if_agc_cfg.outputLevel = 0;
  2170. state->if_agc_cfg.settleLevel = 140;
  2171. state->if_agc_cfg.minOutputLevel = 0;
  2172. state->if_agc_cfg.maxOutputLevel = 1023;
  2173. state->if_agc_cfg.speed = 904;
  2174. if (ulIfAgcMode == 1 && ulIfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
  2175. state->if_agc_cfg.ctrlMode = AGC_CTRL_USER;
  2176. state->if_agc_cfg.outputLevel = (u16) (ulIfAgcOutputLevel);
  2177. }
  2178. if (ulIfAgcMode == 0 &&
  2179. ulIfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
  2180. ulIfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
  2181. ulIfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
  2182. ulIfAgcSpeed <= DRXD_FE_CTRL_MAX) {
  2183. state->if_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2184. state->if_agc_cfg.settleLevel = (u16) (ulIfAgcSettleLevel);
  2185. state->if_agc_cfg.minOutputLevel = (u16) (ulIfAgcMinLevel);
  2186. state->if_agc_cfg.maxOutputLevel = (u16) (ulIfAgcMaxLevel);
  2187. state->if_agc_cfg.speed = (u16) (ulIfAgcSpeed);
  2188. }
  2189. state->if_agc_cfg.R1 = (u16) (ulIfAgcR1);
  2190. state->if_agc_cfg.R2 = (u16) (ulIfAgcR2);
  2191. state->if_agc_cfg.R3 = (u16) (ulIfAgcR3);
  2192. state->rf_agc_cfg.R1 = (u16) (ulRfAgcR1);
  2193. state->rf_agc_cfg.R2 = (u16) (ulRfAgcR2);
  2194. state->rf_agc_cfg.R3 = (u16) (ulRfAgcR3);
  2195. state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2196. /* rest of the RFAgcCfg structure currently unused */
  2197. if (ulRfAgcMode == 1 && ulRfAgcOutputLevel <= DRXD_FE_CTRL_MAX) {
  2198. state->rf_agc_cfg.ctrlMode = AGC_CTRL_USER;
  2199. state->rf_agc_cfg.outputLevel = (u16) (ulRfAgcOutputLevel);
  2200. }
  2201. if (ulRfAgcMode == 0 &&
  2202. ulRfAgcSettleLevel <= DRXD_FE_CTRL_MAX &&
  2203. ulRfAgcMinLevel <= DRXD_FE_CTRL_MAX &&
  2204. ulRfAgcMaxLevel <= DRXD_FE_CTRL_MAX &&
  2205. ulRfAgcSpeed <= DRXD_FE_CTRL_MAX) {
  2206. state->rf_agc_cfg.ctrlMode = AGC_CTRL_AUTO;
  2207. state->rf_agc_cfg.settleLevel = (u16) (ulRfAgcSettleLevel);
  2208. state->rf_agc_cfg.minOutputLevel = (u16) (ulRfAgcMinLevel);
  2209. state->rf_agc_cfg.maxOutputLevel = (u16) (ulRfAgcMaxLevel);
  2210. state->rf_agc_cfg.speed = (u16) (ulRfAgcSpeed);
  2211. }
  2212. if (ulRfAgcMode == 2)
  2213. state->rf_agc_cfg.ctrlMode = AGC_CTRL_OFF;
  2214. if (ulEnvironment <= 2)
  2215. state->app_env_default = (enum app_env)
  2216. (ulEnvironment);
  2217. if (ulEnvironmentDiversity <= 2)
  2218. state->app_env_diversity = (enum app_env)
  2219. (ulEnvironmentDiversity);
  2220. if (ulIFFilter == IFFILTER_DISCRETE) {
  2221. /* discrete filter */
  2222. state->noise_cal.cpOpt = 0;
  2223. state->noise_cal.cpNexpOfs = 40;
  2224. state->noise_cal.tdCal2k = -40;
  2225. state->noise_cal.tdCal8k = -24;
  2226. } else {
  2227. /* SAW filter */
  2228. state->noise_cal.cpOpt = 1;
  2229. state->noise_cal.cpNexpOfs = 0;
  2230. state->noise_cal.tdCal2k = -21;
  2231. state->noise_cal.tdCal8k = -24;
  2232. }
  2233. state->m_EcOcRegOcModeLop = (u16) (ulEcOcRegOcModeLop);
  2234. state->chip_adr = (state->config.demod_address << 1) | 1;
  2235. switch (ulHiI2cPatch) {
  2236. case 1:
  2237. state->m_HiI2cPatch = DRXD_HiI2cPatch_1;
  2238. break;
  2239. case 3:
  2240. state->m_HiI2cPatch = DRXD_HiI2cPatch_3;
  2241. break;
  2242. default:
  2243. state->m_HiI2cPatch = NULL;
  2244. }
  2245. /* modify tuner and clock attributes */
  2246. state->intermediate_freq = (u16) (IntermediateFrequency / 1000);
  2247. /* expected system clock frequency in kHz */
  2248. state->expected_sys_clock_freq = 48000;
  2249. /* real system clock frequency in kHz */
  2250. state->sys_clock_freq = 48000;
  2251. state->osc_clock_freq = (u16) ulClock;
  2252. state->osc_clock_deviation = 0;
  2253. state->cscd_state = CSCD_INIT;
  2254. state->drxd_state = DRXD_UNINITIALIZED;
  2255. state->PGA = 0;
  2256. state->type_A = 0;
  2257. state->tuner_mirrors = 0;
  2258. /* modify MPEG output attributes */
  2259. state->insert_rs_byte = state->config.insert_rs_byte;
  2260. state->enable_parallel = (ulSerialMode != 1);
  2261. /* Timing div, 250ns/Psys */
  2262. /* Timing div, = ( delay (nano seconds) * sysclk (kHz) )/ 1000 */
  2263. state->hi_cfg_timing_div = (u16) ((state->sys_clock_freq / 1000) *
  2264. ulHiI2cDelay) / 1000;
  2265. /* Bridge delay, uses oscilator clock */
  2266. /* Delay = ( delay (nano seconds) * oscclk (kHz) )/ 1000 */
  2267. state->hi_cfg_bridge_delay = (u16) ((state->osc_clock_freq / 1000) *
  2268. ulHiI2cBridgeDelay) / 1000;
  2269. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
  2270. /* state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO; */
  2271. state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
  2272. return 0;
  2273. }
  2274. int DRXD_init(struct drxd_state *state, const u8 * fw, u32 fw_size)
  2275. {
  2276. int status = 0;
  2277. u32 driverVersion;
  2278. if (state->init_done)
  2279. return 0;
  2280. CDRXD(state, state->config.IF ? state->config.IF : 36000000);
  2281. do {
  2282. state->operation_mode = OM_Default;
  2283. status = SetDeviceTypeId(state);
  2284. if (status < 0)
  2285. break;
  2286. /* Apply I2c address patch to B1 */
  2287. if (!state->type_A && state->m_HiI2cPatch != NULL)
  2288. status = WriteTable(state, state->m_HiI2cPatch);
  2289. if (status < 0)
  2290. break;
  2291. if (state->type_A) {
  2292. /* HI firmware patch for UIO readout,
  2293. avoid clearing of result register */
  2294. status = Write16(state, 0x43012D, 0x047f, 0);
  2295. if (status < 0)
  2296. break;
  2297. }
  2298. status = HI_ResetCommand(state);
  2299. if (status < 0)
  2300. break;
  2301. status = StopAllProcessors(state);
  2302. if (status < 0)
  2303. break;
  2304. status = InitCC(state);
  2305. if (status < 0)
  2306. break;
  2307. state->osc_clock_deviation = 0;
  2308. if (state->config.osc_deviation)
  2309. state->osc_clock_deviation =
  2310. state->config.osc_deviation(state->priv, 0, 0);
  2311. {
  2312. /* Handle clock deviation */
  2313. s32 devB;
  2314. s32 devA = (s32) (state->osc_clock_deviation) *
  2315. (s32) (state->expected_sys_clock_freq);
  2316. /* deviation in kHz */
  2317. s32 deviation = (devA / (1000000L));
  2318. /* rounding, signed */
  2319. if (devA > 0)
  2320. devB = (2);
  2321. else
  2322. devB = (-2);
  2323. if ((devB * (devA % 1000000L) > 1000000L)) {
  2324. /* add +1 or -1 */
  2325. deviation += (devB / 2);
  2326. }
  2327. state->sys_clock_freq =
  2328. (u16) ((state->expected_sys_clock_freq) +
  2329. deviation);
  2330. }
  2331. status = InitHI(state);
  2332. if (status < 0)
  2333. break;
  2334. status = InitAtomicRead(state);
  2335. if (status < 0)
  2336. break;
  2337. status = EnableAndResetMB(state);
  2338. if (status < 0)
  2339. break;
  2340. if (state->type_A)
  2341. status = ResetCEFR(state);
  2342. if (status < 0)
  2343. break;
  2344. if (fw) {
  2345. status = DownloadMicrocode(state, fw, fw_size);
  2346. if (status < 0)
  2347. break;
  2348. } else {
  2349. status = DownloadMicrocode(state, state->microcode, state->microcode_length);
  2350. if (status < 0)
  2351. break;
  2352. }
  2353. if (state->PGA) {
  2354. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_PRO;
  2355. SetCfgPga(state, 0); /* PGA = 0 dB */
  2356. } else {
  2357. state->m_FeAgRegAgPwd = DRXD_DEF_AG_PWD_CONSUMER;
  2358. }
  2359. state->m_FeAgRegAgAgcSio = DRXD_DEF_AG_AGC_SIO;
  2360. status = InitFE(state);
  2361. if (status < 0)
  2362. break;
  2363. status = InitFT(state);
  2364. if (status < 0)
  2365. break;
  2366. status = InitCP(state);
  2367. if (status < 0)
  2368. break;
  2369. status = InitCE(state);
  2370. if (status < 0)
  2371. break;
  2372. status = InitEQ(state);
  2373. if (status < 0)
  2374. break;
  2375. status = InitEC(state);
  2376. if (status < 0)
  2377. break;
  2378. status = InitSC(state);
  2379. if (status < 0)
  2380. break;
  2381. status = SetCfgIfAgc(state, &state->if_agc_cfg);
  2382. if (status < 0)
  2383. break;
  2384. status = SetCfgRfAgc(state, &state->rf_agc_cfg);
  2385. if (status < 0)
  2386. break;
  2387. state->cscd_state = CSCD_INIT;
  2388. status = Write16(state, SC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  2389. if (status < 0)
  2390. break;
  2391. status = Write16(state, LC_COMM_EXEC__A, SC_COMM_EXEC_CTL_STOP, 0);
  2392. if (status < 0)
  2393. break;
  2394. driverVersion = (((VERSION_MAJOR / 10) << 4) +
  2395. (VERSION_MAJOR % 10)) << 24;
  2396. driverVersion += (((VERSION_MINOR / 10) << 4) +
  2397. (VERSION_MINOR % 10)) << 16;
  2398. driverVersion += ((VERSION_PATCH / 1000) << 12) +
  2399. ((VERSION_PATCH / 100) << 8) +
  2400. ((VERSION_PATCH / 10) << 4) + (VERSION_PATCH % 10);
  2401. status = Write32(state, SC_RA_RAM_DRIVER_VERSION__AX, driverVersion, 0);
  2402. if (status < 0)
  2403. break;
  2404. status = StopOC(state);
  2405. if (status < 0)
  2406. break;
  2407. state->drxd_state = DRXD_STOPPED;
  2408. state->init_done = 1;
  2409. status = 0;
  2410. } while (0);
  2411. return status;
  2412. }
  2413. int DRXD_status(struct drxd_state *state, u32 * pLockStatus)
  2414. {
  2415. DRX_GetLockStatus(state, pLockStatus);
  2416. /*if (*pLockStatus&DRX_LOCK_MPEG) */
  2417. if (*pLockStatus & DRX_LOCK_FEC) {
  2418. ConfigureMPEGOutput(state, 1);
  2419. /* Get status again, in case we have MPEG lock now */
  2420. /*DRX_GetLockStatus(state, pLockStatus); */
  2421. }
  2422. return 0;
  2423. }
  2424. /****************************************************************************/
  2425. /****************************************************************************/
  2426. /****************************************************************************/
  2427. static int drxd_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  2428. {
  2429. struct drxd_state *state = fe->demodulator_priv;
  2430. u32 value;
  2431. int res;
  2432. res = ReadIFAgc(state, &value);
  2433. if (res < 0)
  2434. *strength = 0;
  2435. else
  2436. *strength = 0xffff - (value << 4);
  2437. return 0;
  2438. }
  2439. static int drxd_read_status(struct dvb_frontend *fe, fe_status_t * status)
  2440. {
  2441. struct drxd_state *state = fe->demodulator_priv;
  2442. u32 lock;
  2443. DRXD_status(state, &lock);
  2444. *status = 0;
  2445. /* No MPEG lock in V255 firmware, bug ? */
  2446. #if 1
  2447. if (lock & DRX_LOCK_MPEG)
  2448. *status |= FE_HAS_LOCK;
  2449. #else
  2450. if (lock & DRX_LOCK_FEC)
  2451. *status |= FE_HAS_LOCK;
  2452. #endif
  2453. if (lock & DRX_LOCK_FEC)
  2454. *status |= FE_HAS_VITERBI | FE_HAS_SYNC;
  2455. if (lock & DRX_LOCK_DEMOD)
  2456. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  2457. return 0;
  2458. }
  2459. static int drxd_init(struct dvb_frontend *fe)
  2460. {
  2461. struct drxd_state *state = fe->demodulator_priv;
  2462. int err = 0;
  2463. /* if (request_firmware(&state->fw, "drxd.fw", state->dev)<0) */
  2464. return DRXD_init(state, 0, 0);
  2465. err = DRXD_init(state, state->fw->data, state->fw->size);
  2466. release_firmware(state->fw);
  2467. return err;
  2468. }
  2469. int drxd_config_i2c(struct dvb_frontend *fe, int onoff)
  2470. {
  2471. struct drxd_state *state = fe->demodulator_priv;
  2472. if (state->config.disable_i2c_gate_ctrl == 1)
  2473. return 0;
  2474. return DRX_ConfigureI2CBridge(state, onoff);
  2475. }
  2476. EXPORT_SYMBOL(drxd_config_i2c);
  2477. static int drxd_get_tune_settings(struct dvb_frontend *fe,
  2478. struct dvb_frontend_tune_settings *sets)
  2479. {
  2480. sets->min_delay_ms = 10000;
  2481. sets->max_drift = 0;
  2482. sets->step_size = 0;
  2483. return 0;
  2484. }
  2485. static int drxd_read_ber(struct dvb_frontend *fe, u32 * ber)
  2486. {
  2487. *ber = 0;
  2488. return 0;
  2489. }
  2490. static int drxd_read_snr(struct dvb_frontend *fe, u16 * snr)
  2491. {
  2492. *snr = 0;
  2493. return 0;
  2494. }
  2495. static int drxd_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
  2496. {
  2497. *ucblocks = 0;
  2498. return 0;
  2499. }
  2500. static int drxd_sleep(struct dvb_frontend *fe)
  2501. {
  2502. struct drxd_state *state = fe->demodulator_priv;
  2503. ConfigureMPEGOutput(state, 0);
  2504. return 0;
  2505. }
  2506. static int drxd_get_frontend(struct dvb_frontend *fe,
  2507. struct dvb_frontend_parameters *param)
  2508. {
  2509. return 0;
  2510. }
  2511. static int drxd_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  2512. {
  2513. return drxd_config_i2c(fe, enable);
  2514. }
  2515. static int drxd_set_frontend(struct dvb_frontend *fe,
  2516. struct dvb_frontend_parameters *param)
  2517. {
  2518. struct drxd_state *state = fe->demodulator_priv;
  2519. s32 off = 0;
  2520. state->param = *param;
  2521. DRX_Stop(state);
  2522. if (fe->ops.tuner_ops.set_params) {
  2523. fe->ops.tuner_ops.set_params(fe, param);
  2524. if (fe->ops.i2c_gate_ctrl)
  2525. fe->ops.i2c_gate_ctrl(fe, 0);
  2526. }
  2527. /* FIXME: move PLL drivers */
  2528. if (state->config.pll_set &&
  2529. state->config.pll_set(state->priv, param,
  2530. state->config.pll_address,
  2531. state->config.demoda_address, &off) < 0) {
  2532. printk(KERN_ERR "Error in pll_set\n");
  2533. return -1;
  2534. }
  2535. msleep(200);
  2536. return DRX_Start(state, off);
  2537. }
  2538. static void drxd_release(struct dvb_frontend *fe)
  2539. {
  2540. struct drxd_state *state = fe->demodulator_priv;
  2541. kfree(state);
  2542. }
  2543. static struct dvb_frontend_ops drxd_ops = {
  2544. .info = {
  2545. .name = "Micronas DRXD DVB-T",
  2546. .type = FE_OFDM,
  2547. .frequency_min = 47125000,
  2548. .frequency_max = 855250000,
  2549. .frequency_stepsize = 166667,
  2550. .frequency_tolerance = 0,
  2551. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  2552. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  2553. FE_CAN_FEC_AUTO |
  2554. FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  2555. FE_CAN_QAM_AUTO |
  2556. FE_CAN_TRANSMISSION_MODE_AUTO |
  2557. FE_CAN_GUARD_INTERVAL_AUTO |
  2558. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER | FE_CAN_MUTE_TS},
  2559. .release = drxd_release,
  2560. .init = drxd_init,
  2561. .sleep = drxd_sleep,
  2562. .i2c_gate_ctrl = drxd_i2c_gate_ctrl,
  2563. .set_frontend = drxd_set_frontend,
  2564. .get_frontend = drxd_get_frontend,
  2565. .get_tune_settings = drxd_get_tune_settings,
  2566. .read_status = drxd_read_status,
  2567. .read_ber = drxd_read_ber,
  2568. .read_signal_strength = drxd_read_signal_strength,
  2569. .read_snr = drxd_read_snr,
  2570. .read_ucblocks = drxd_read_ucblocks,
  2571. };
  2572. struct dvb_frontend *drxd_attach(const struct drxd_config *config,
  2573. void *priv, struct i2c_adapter *i2c,
  2574. struct device *dev)
  2575. {
  2576. struct drxd_state *state = NULL;
  2577. state = kmalloc(sizeof(struct drxd_state), GFP_KERNEL);
  2578. if (!state)
  2579. return NULL;
  2580. memset(state, 0, sizeof(*state));
  2581. memcpy(&state->ops, &drxd_ops, sizeof(struct dvb_frontend_ops));
  2582. state->dev = dev;
  2583. state->config = *config;
  2584. state->i2c = i2c;
  2585. state->priv = priv;
  2586. mutex_init(&state->mutex);
  2587. if (Read16(state, 0, 0, 0) < 0)
  2588. goto error;
  2589. memcpy(&state->frontend.ops, &drxd_ops,
  2590. sizeof(struct dvb_frontend_ops));
  2591. state->frontend.demodulator_priv = state;
  2592. ConfigureMPEGOutput(state, 0);
  2593. return &state->frontend;
  2594. error:
  2595. printk(KERN_ERR "drxd: not found\n");
  2596. kfree(state);
  2597. return NULL;
  2598. }
  2599. EXPORT_SYMBOL(drxd_attach);
  2600. MODULE_DESCRIPTION("DRXD driver");
  2601. MODULE_AUTHOR("Micronas");
  2602. MODULE_LICENSE("GPL");