drxd_hard.c 71 KB

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