drxd_hard.c 71 KB

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