dib8000.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB8000 chip (ISDB-T).
  3. *
  4. * Copyright (C) 2009 DiBcom (http://www.dibcom.fr/)
  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 as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mutex.h>
  14. #include <asm/div64.h>
  15. #include "dvb_math.h"
  16. #include "dvb_frontend.h"
  17. #include "dib8000.h"
  18. #define LAYER_ALL -1
  19. #define LAYER_A 1
  20. #define LAYER_B 2
  21. #define LAYER_C 3
  22. #define MAX_NUMBER_OF_FRONTENDS 6
  23. /* #define DIB8000_AGC_FREEZE */
  24. static int debug;
  25. module_param(debug, int, 0644);
  26. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  27. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB8000: "); printk(args); printk("\n"); } } while (0)
  28. struct i2c_device {
  29. struct i2c_adapter *adap;
  30. u8 addr;
  31. u8 *i2c_write_buffer;
  32. u8 *i2c_read_buffer;
  33. struct mutex *i2c_buffer_lock;
  34. };
  35. enum param_loop_step {
  36. LOOP_TUNE_1,
  37. LOOP_TUNE_2
  38. };
  39. enum dib8000_autosearch_step {
  40. AS_START = 0,
  41. AS_SEARCHING_FFT,
  42. AS_SEARCHING_GUARD,
  43. AS_DONE = 100,
  44. };
  45. enum timeout_mode {
  46. SYMBOL_DEPENDENT_OFF = 0,
  47. SYMBOL_DEPENDENT_ON,
  48. };
  49. struct dib8000_state {
  50. struct dib8000_config cfg;
  51. struct i2c_device i2c;
  52. struct dibx000_i2c_master i2c_master;
  53. u16 wbd_ref;
  54. u8 current_band;
  55. u32 current_bandwidth;
  56. struct dibx000_agc_config *current_agc;
  57. u32 timf;
  58. u32 timf_default;
  59. u8 div_force_off:1;
  60. u8 div_state:1;
  61. u16 div_sync_wait;
  62. u8 agc_state;
  63. u8 differential_constellation;
  64. u8 diversity_onoff;
  65. s16 ber_monitored_layer;
  66. u16 gpio_dir;
  67. u16 gpio_val;
  68. u16 revision;
  69. u8 isdbt_cfg_loaded;
  70. enum frontend_tune_state tune_state;
  71. s32 status;
  72. struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
  73. /* for the I2C transfer */
  74. struct i2c_msg msg[2];
  75. u8 i2c_write_buffer[4];
  76. u8 i2c_read_buffer[2];
  77. struct mutex i2c_buffer_lock;
  78. u8 input_mode_mpeg;
  79. u16 tuner_enable;
  80. struct i2c_adapter dib8096p_tuner_adap;
  81. u16 current_demod_bw;
  82. u16 seg_mask;
  83. u16 seg_diff_mask;
  84. u16 mode;
  85. u8 layer_b_nb_seg;
  86. u8 layer_c_nb_seg;
  87. u8 channel_parameters_set;
  88. u16 autosearch_state;
  89. u16 found_nfft;
  90. u16 found_guard;
  91. u8 subchannel;
  92. u8 symbol_duration;
  93. unsigned long timeout;
  94. u8 longest_intlv_layer;
  95. u16 output_mode;
  96. /* for DVBv5 stats */
  97. s64 init_ucb;
  98. unsigned long per_jiffies_stats;
  99. unsigned long ber_jiffies_stats;
  100. unsigned long ber_jiffies_stats_layer[3];
  101. #ifdef DIB8000_AGC_FREEZE
  102. u16 agc1_max;
  103. u16 agc1_min;
  104. u16 agc2_max;
  105. u16 agc2_min;
  106. #endif
  107. };
  108. enum dib8000_power_mode {
  109. DIB8000_POWER_ALL = 0,
  110. DIB8000_POWER_INTERFACE_ONLY,
  111. };
  112. static u16 dib8000_i2c_read16(struct i2c_device *i2c, u16 reg)
  113. {
  114. u16 ret;
  115. struct i2c_msg msg[2] = {
  116. {.addr = i2c->addr >> 1, .flags = 0, .len = 2},
  117. {.addr = i2c->addr >> 1, .flags = I2C_M_RD, .len = 2},
  118. };
  119. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  120. dprintk("could not acquire lock");
  121. return 0;
  122. }
  123. msg[0].buf = i2c->i2c_write_buffer;
  124. msg[0].buf[0] = reg >> 8;
  125. msg[0].buf[1] = reg & 0xff;
  126. msg[1].buf = i2c->i2c_read_buffer;
  127. if (i2c_transfer(i2c->adap, msg, 2) != 2)
  128. dprintk("i2c read error on %d", reg);
  129. ret = (msg[1].buf[0] << 8) | msg[1].buf[1];
  130. mutex_unlock(i2c->i2c_buffer_lock);
  131. return ret;
  132. }
  133. static u16 __dib8000_read_word(struct dib8000_state *state, u16 reg)
  134. {
  135. u16 ret;
  136. state->i2c_write_buffer[0] = reg >> 8;
  137. state->i2c_write_buffer[1] = reg & 0xff;
  138. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  139. state->msg[0].addr = state->i2c.addr >> 1;
  140. state->msg[0].flags = 0;
  141. state->msg[0].buf = state->i2c_write_buffer;
  142. state->msg[0].len = 2;
  143. state->msg[1].addr = state->i2c.addr >> 1;
  144. state->msg[1].flags = I2C_M_RD;
  145. state->msg[1].buf = state->i2c_read_buffer;
  146. state->msg[1].len = 2;
  147. if (i2c_transfer(state->i2c.adap, state->msg, 2) != 2)
  148. dprintk("i2c read error on %d", reg);
  149. ret = (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  150. return ret;
  151. }
  152. static u16 dib8000_read_word(struct dib8000_state *state, u16 reg)
  153. {
  154. u16 ret;
  155. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  156. dprintk("could not acquire lock");
  157. return 0;
  158. }
  159. ret = __dib8000_read_word(state, reg);
  160. mutex_unlock(&state->i2c_buffer_lock);
  161. return ret;
  162. }
  163. static u32 dib8000_read32(struct dib8000_state *state, u16 reg)
  164. {
  165. u16 rw[2];
  166. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  167. dprintk("could not acquire lock");
  168. return 0;
  169. }
  170. rw[0] = __dib8000_read_word(state, reg + 0);
  171. rw[1] = __dib8000_read_word(state, reg + 1);
  172. mutex_unlock(&state->i2c_buffer_lock);
  173. return ((rw[0] << 16) | (rw[1]));
  174. }
  175. static int dib8000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  176. {
  177. struct i2c_msg msg = {.addr = i2c->addr >> 1, .flags = 0, .len = 4};
  178. int ret = 0;
  179. if (mutex_lock_interruptible(i2c->i2c_buffer_lock) < 0) {
  180. dprintk("could not acquire lock");
  181. return -EINVAL;
  182. }
  183. msg.buf = i2c->i2c_write_buffer;
  184. msg.buf[0] = (reg >> 8) & 0xff;
  185. msg.buf[1] = reg & 0xff;
  186. msg.buf[2] = (val >> 8) & 0xff;
  187. msg.buf[3] = val & 0xff;
  188. ret = i2c_transfer(i2c->adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  189. mutex_unlock(i2c->i2c_buffer_lock);
  190. return ret;
  191. }
  192. static int dib8000_write_word(struct dib8000_state *state, u16 reg, u16 val)
  193. {
  194. int ret;
  195. if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
  196. dprintk("could not acquire lock");
  197. return -EINVAL;
  198. }
  199. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  200. state->i2c_write_buffer[1] = reg & 0xff;
  201. state->i2c_write_buffer[2] = (val >> 8) & 0xff;
  202. state->i2c_write_buffer[3] = val & 0xff;
  203. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  204. state->msg[0].addr = state->i2c.addr >> 1;
  205. state->msg[0].flags = 0;
  206. state->msg[0].buf = state->i2c_write_buffer;
  207. state->msg[0].len = 4;
  208. ret = (i2c_transfer(state->i2c.adap, state->msg, 1) != 1 ?
  209. -EREMOTEIO : 0);
  210. mutex_unlock(&state->i2c_buffer_lock);
  211. return ret;
  212. }
  213. static const s16 coeff_2k_sb_1seg_dqpsk[8] = {
  214. (769 << 5) | 0x0a, (745 << 5) | 0x03, (595 << 5) | 0x0d, (769 << 5) | 0x0a, (920 << 5) | 0x09, (784 << 5) | 0x02, (519 << 5) | 0x0c,
  215. (920 << 5) | 0x09
  216. };
  217. static const s16 coeff_2k_sb_1seg[8] = {
  218. (692 << 5) | 0x0b, (683 << 5) | 0x01, (519 << 5) | 0x09, (692 << 5) | 0x0b, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f, 0 | 0x1f
  219. };
  220. static const s16 coeff_2k_sb_3seg_0dqpsk_1dqpsk[8] = {
  221. (832 << 5) | 0x10, (912 << 5) | 0x05, (900 << 5) | 0x12, (832 << 5) | 0x10, (-931 << 5) | 0x0f, (912 << 5) | 0x04, (807 << 5) | 0x11,
  222. (-931 << 5) | 0x0f
  223. };
  224. static const s16 coeff_2k_sb_3seg_0dqpsk[8] = {
  225. (622 << 5) | 0x0c, (941 << 5) | 0x04, (796 << 5) | 0x10, (622 << 5) | 0x0c, (982 << 5) | 0x0c, (519 << 5) | 0x02, (572 << 5) | 0x0e,
  226. (982 << 5) | 0x0c
  227. };
  228. static const s16 coeff_2k_sb_3seg_1dqpsk[8] = {
  229. (699 << 5) | 0x14, (607 << 5) | 0x04, (944 << 5) | 0x13, (699 << 5) | 0x14, (-720 << 5) | 0x0d, (640 << 5) | 0x03, (866 << 5) | 0x12,
  230. (-720 << 5) | 0x0d
  231. };
  232. static const s16 coeff_2k_sb_3seg[8] = {
  233. (664 << 5) | 0x0c, (925 << 5) | 0x03, (937 << 5) | 0x10, (664 << 5) | 0x0c, (-610 << 5) | 0x0a, (697 << 5) | 0x01, (836 << 5) | 0x0e,
  234. (-610 << 5) | 0x0a
  235. };
  236. static const s16 coeff_4k_sb_1seg_dqpsk[8] = {
  237. (-955 << 5) | 0x0e, (687 << 5) | 0x04, (818 << 5) | 0x10, (-955 << 5) | 0x0e, (-922 << 5) | 0x0d, (750 << 5) | 0x03, (665 << 5) | 0x0f,
  238. (-922 << 5) | 0x0d
  239. };
  240. static const s16 coeff_4k_sb_1seg[8] = {
  241. (638 << 5) | 0x0d, (683 << 5) | 0x02, (638 << 5) | 0x0d, (638 << 5) | 0x0d, (-655 << 5) | 0x0a, (517 << 5) | 0x00, (698 << 5) | 0x0d,
  242. (-655 << 5) | 0x0a
  243. };
  244. static const s16 coeff_4k_sb_3seg_0dqpsk_1dqpsk[8] = {
  245. (-707 << 5) | 0x14, (910 << 5) | 0x06, (889 << 5) | 0x16, (-707 << 5) | 0x14, (-958 << 5) | 0x13, (993 << 5) | 0x05, (523 << 5) | 0x14,
  246. (-958 << 5) | 0x13
  247. };
  248. static const s16 coeff_4k_sb_3seg_0dqpsk[8] = {
  249. (-723 << 5) | 0x13, (910 << 5) | 0x05, (777 << 5) | 0x14, (-723 << 5) | 0x13, (-568 << 5) | 0x0f, (547 << 5) | 0x03, (696 << 5) | 0x12,
  250. (-568 << 5) | 0x0f
  251. };
  252. static const s16 coeff_4k_sb_3seg_1dqpsk[8] = {
  253. (-940 << 5) | 0x15, (607 << 5) | 0x05, (915 << 5) | 0x16, (-940 << 5) | 0x15, (-848 << 5) | 0x13, (683 << 5) | 0x04, (543 << 5) | 0x14,
  254. (-848 << 5) | 0x13
  255. };
  256. static const s16 coeff_4k_sb_3seg[8] = {
  257. (612 << 5) | 0x12, (910 << 5) | 0x04, (864 << 5) | 0x14, (612 << 5) | 0x12, (-869 << 5) | 0x13, (683 << 5) | 0x02, (869 << 5) | 0x12,
  258. (-869 << 5) | 0x13
  259. };
  260. static const s16 coeff_8k_sb_1seg_dqpsk[8] = {
  261. (-835 << 5) | 0x12, (684 << 5) | 0x05, (735 << 5) | 0x14, (-835 << 5) | 0x12, (-598 << 5) | 0x10, (781 << 5) | 0x04, (739 << 5) | 0x13,
  262. (-598 << 5) | 0x10
  263. };
  264. static const s16 coeff_8k_sb_1seg[8] = {
  265. (673 << 5) | 0x0f, (683 << 5) | 0x03, (808 << 5) | 0x12, (673 << 5) | 0x0f, (585 << 5) | 0x0f, (512 << 5) | 0x01, (780 << 5) | 0x0f,
  266. (585 << 5) | 0x0f
  267. };
  268. static const s16 coeff_8k_sb_3seg_0dqpsk_1dqpsk[8] = {
  269. (863 << 5) | 0x17, (930 << 5) | 0x07, (878 << 5) | 0x19, (863 << 5) | 0x17, (0 << 5) | 0x14, (521 << 5) | 0x05, (980 << 5) | 0x18,
  270. (0 << 5) | 0x14
  271. };
  272. static const s16 coeff_8k_sb_3seg_0dqpsk[8] = {
  273. (-924 << 5) | 0x17, (910 << 5) | 0x06, (774 << 5) | 0x17, (-924 << 5) | 0x17, (-877 << 5) | 0x15, (565 << 5) | 0x04, (553 << 5) | 0x15,
  274. (-877 << 5) | 0x15
  275. };
  276. static const s16 coeff_8k_sb_3seg_1dqpsk[8] = {
  277. (-921 << 5) | 0x19, (607 << 5) | 0x06, (881 << 5) | 0x19, (-921 << 5) | 0x19, (-921 << 5) | 0x14, (713 << 5) | 0x05, (1018 << 5) | 0x18,
  278. (-921 << 5) | 0x14
  279. };
  280. static const s16 coeff_8k_sb_3seg[8] = {
  281. (514 << 5) | 0x14, (910 << 5) | 0x05, (861 << 5) | 0x17, (514 << 5) | 0x14, (690 << 5) | 0x14, (683 << 5) | 0x03, (662 << 5) | 0x15,
  282. (690 << 5) | 0x14
  283. };
  284. static const s16 ana_fe_coeff_3seg[24] = {
  285. 81, 80, 78, 74, 68, 61, 54, 45, 37, 28, 19, 11, 4, 1022, 1017, 1013, 1010, 1008, 1008, 1008, 1008, 1010, 1014, 1017
  286. };
  287. static const s16 ana_fe_coeff_1seg[24] = {
  288. 249, 226, 164, 82, 5, 981, 970, 988, 1018, 20, 31, 26, 8, 1012, 1000, 1018, 1012, 8, 15, 14, 9, 3, 1017, 1003
  289. };
  290. static const s16 ana_fe_coeff_13seg[24] = {
  291. 396, 305, 105, -51, -77, -12, 41, 31, -11, -30, -11, 14, 15, -2, -13, -7, 5, 8, 1, -6, -7, -3, 0, 1
  292. };
  293. static u16 fft_to_mode(struct dib8000_state *state)
  294. {
  295. u16 mode;
  296. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  297. case TRANSMISSION_MODE_2K:
  298. mode = 1;
  299. break;
  300. case TRANSMISSION_MODE_4K:
  301. mode = 2;
  302. break;
  303. default:
  304. case TRANSMISSION_MODE_AUTO:
  305. case TRANSMISSION_MODE_8K:
  306. mode = 3;
  307. break;
  308. }
  309. return mode;
  310. }
  311. static void dib8000_set_acquisition_mode(struct dib8000_state *state)
  312. {
  313. u16 nud = dib8000_read_word(state, 298);
  314. nud |= (1 << 3) | (1 << 0);
  315. dprintk("acquisition mode activated");
  316. dib8000_write_word(state, 298, nud);
  317. }
  318. static int dib8000_set_output_mode(struct dvb_frontend *fe, int mode)
  319. {
  320. struct dib8000_state *state = fe->demodulator_priv;
  321. u16 outreg, fifo_threshold, smo_mode, sram = 0x0205; /* by default SDRAM deintlv is enabled */
  322. state->output_mode = mode;
  323. outreg = 0;
  324. fifo_threshold = 1792;
  325. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  326. dprintk("-I- Setting output mode for demod %p to %d",
  327. &state->fe[0], mode);
  328. switch (mode) {
  329. case OUTMODE_MPEG2_PAR_GATED_CLK: // STBs with parallel gated clock
  330. outreg = (1 << 10); /* 0x0400 */
  331. break;
  332. case OUTMODE_MPEG2_PAR_CONT_CLK: // STBs with parallel continues clock
  333. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  334. break;
  335. case OUTMODE_MPEG2_SERIAL: // STBs with serial input
  336. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  337. break;
  338. case OUTMODE_DIVERSITY:
  339. if (state->cfg.hostbus_diversity) {
  340. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  341. sram &= 0xfdff;
  342. } else
  343. sram |= 0x0c00;
  344. break;
  345. case OUTMODE_MPEG2_FIFO: // e.g. USB feeding
  346. smo_mode |= (3 << 1);
  347. fifo_threshold = 512;
  348. outreg = (1 << 10) | (5 << 6);
  349. break;
  350. case OUTMODE_HIGH_Z: // disable
  351. outreg = 0;
  352. break;
  353. case OUTMODE_ANALOG_ADC:
  354. outreg = (1 << 10) | (3 << 6);
  355. dib8000_set_acquisition_mode(state);
  356. break;
  357. default:
  358. dprintk("Unhandled output_mode passed to be set for demod %p",
  359. &state->fe[0]);
  360. return -EINVAL;
  361. }
  362. if (state->cfg.output_mpeg2_in_188_bytes)
  363. smo_mode |= (1 << 5);
  364. dib8000_write_word(state, 299, smo_mode);
  365. dib8000_write_word(state, 300, fifo_threshold); /* synchronous fread */
  366. dib8000_write_word(state, 1286, outreg);
  367. dib8000_write_word(state, 1291, sram);
  368. return 0;
  369. }
  370. static int dib8000_set_diversity_in(struct dvb_frontend *fe, int onoff)
  371. {
  372. struct dib8000_state *state = fe->demodulator_priv;
  373. u16 tmp, sync_wait = dib8000_read_word(state, 273) & 0xfff0;
  374. dprintk("set diversity input to %i", onoff);
  375. if (!state->differential_constellation) {
  376. dib8000_write_word(state, 272, 1 << 9); //dvsy_off_lmod4 = 1
  377. dib8000_write_word(state, 273, sync_wait | (1 << 2) | 2); // sync_enable = 1; comb_mode = 2
  378. } else {
  379. dib8000_write_word(state, 272, 0); //dvsy_off_lmod4 = 0
  380. dib8000_write_word(state, 273, sync_wait); // sync_enable = 0; comb_mode = 0
  381. }
  382. state->diversity_onoff = onoff;
  383. switch (onoff) {
  384. case 0: /* only use the internal way - not the diversity input */
  385. dib8000_write_word(state, 270, 1);
  386. dib8000_write_word(state, 271, 0);
  387. break;
  388. case 1: /* both ways */
  389. dib8000_write_word(state, 270, 6);
  390. dib8000_write_word(state, 271, 6);
  391. break;
  392. case 2: /* only the diversity input */
  393. dib8000_write_word(state, 270, 0);
  394. dib8000_write_word(state, 271, 1);
  395. break;
  396. }
  397. if (state->revision == 0x8002) {
  398. tmp = dib8000_read_word(state, 903);
  399. dib8000_write_word(state, 903, tmp & ~(1 << 3));
  400. msleep(30);
  401. dib8000_write_word(state, 903, tmp | (1 << 3));
  402. }
  403. return 0;
  404. }
  405. static void dib8000_set_power_mode(struct dib8000_state *state, enum dib8000_power_mode mode)
  406. {
  407. /* by default everything is going to be powered off */
  408. u16 reg_774 = 0x3fff, reg_775 = 0xffff, reg_776 = 0xffff,
  409. reg_900 = (dib8000_read_word(state, 900) & 0xfffc) | 0x3,
  410. reg_1280;
  411. if (state->revision != 0x8090)
  412. reg_1280 = (dib8000_read_word(state, 1280) & 0x00ff) | 0xff00;
  413. else
  414. reg_1280 = (dib8000_read_word(state, 1280) & 0x707f) | 0x8f80;
  415. /* now, depending on the requested mode, we power on */
  416. switch (mode) {
  417. /* power up everything in the demod */
  418. case DIB8000_POWER_ALL:
  419. reg_774 = 0x0000;
  420. reg_775 = 0x0000;
  421. reg_776 = 0x0000;
  422. reg_900 &= 0xfffc;
  423. if (state->revision != 0x8090)
  424. reg_1280 &= 0x00ff;
  425. else
  426. reg_1280 &= 0x707f;
  427. break;
  428. case DIB8000_POWER_INTERFACE_ONLY:
  429. if (state->revision != 0x8090)
  430. reg_1280 &= 0x00ff;
  431. else
  432. reg_1280 &= 0xfa7b;
  433. break;
  434. }
  435. dprintk("powermode : 774 : %x ; 775 : %x; 776 : %x ; 900 : %x; 1280 : %x", reg_774, reg_775, reg_776, reg_900, reg_1280);
  436. dib8000_write_word(state, 774, reg_774);
  437. dib8000_write_word(state, 775, reg_775);
  438. dib8000_write_word(state, 776, reg_776);
  439. dib8000_write_word(state, 900, reg_900);
  440. dib8000_write_word(state, 1280, reg_1280);
  441. }
  442. static int dib8000_set_adc_state(struct dib8000_state *state, enum dibx000_adc_states no)
  443. {
  444. int ret = 0;
  445. u16 reg, reg_907 = dib8000_read_word(state, 907);
  446. u16 reg_908 = dib8000_read_word(state, 908);
  447. switch (no) {
  448. case DIBX000_SLOW_ADC_ON:
  449. if (state->revision != 0x8090) {
  450. reg_908 |= (1 << 1) | (1 << 0);
  451. ret |= dib8000_write_word(state, 908, reg_908);
  452. reg_908 &= ~(1 << 1);
  453. } else {
  454. reg = dib8000_read_word(state, 1925);
  455. /* en_slowAdc = 1 & reset_sladc = 1 */
  456. dib8000_write_word(state, 1925, reg |
  457. (1<<4) | (1<<2));
  458. /* read acces to make it works... strange ... */
  459. reg = dib8000_read_word(state, 1925);
  460. msleep(20);
  461. /* en_slowAdc = 1 & reset_sladc = 0 */
  462. dib8000_write_word(state, 1925, reg & ~(1<<4));
  463. reg = dib8000_read_word(state, 921) & ~((0x3 << 14)
  464. | (0x3 << 12));
  465. /* ref = Vin1 => Vbg ; sel = Vin0 or Vin3 ;
  466. (Vin2 = Vcm) */
  467. dib8000_write_word(state, 921, reg | (1 << 14)
  468. | (3 << 12));
  469. }
  470. break;
  471. case DIBX000_SLOW_ADC_OFF:
  472. if (state->revision == 0x8090) {
  473. reg = dib8000_read_word(state, 1925);
  474. /* reset_sladc = 1 en_slowAdc = 0 */
  475. dib8000_write_word(state, 1925,
  476. (reg & ~(1<<2)) | (1<<4));
  477. }
  478. reg_908 |= (1 << 1) | (1 << 0);
  479. break;
  480. case DIBX000_ADC_ON:
  481. reg_907 &= 0x0fff;
  482. reg_908 &= 0x0003;
  483. break;
  484. case DIBX000_ADC_OFF: // leave the VBG voltage on
  485. reg_907 = (1 << 13) | (1 << 12);
  486. reg_908 = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 1);
  487. break;
  488. case DIBX000_VBG_ENABLE:
  489. reg_907 &= ~(1 << 15);
  490. break;
  491. case DIBX000_VBG_DISABLE:
  492. reg_907 |= (1 << 15);
  493. break;
  494. default:
  495. break;
  496. }
  497. ret |= dib8000_write_word(state, 907, reg_907);
  498. ret |= dib8000_write_word(state, 908, reg_908);
  499. return ret;
  500. }
  501. static int dib8000_set_bandwidth(struct dvb_frontend *fe, u32 bw)
  502. {
  503. struct dib8000_state *state = fe->demodulator_priv;
  504. u32 timf;
  505. if (bw == 0)
  506. bw = 6000;
  507. if (state->timf == 0) {
  508. dprintk("using default timf");
  509. timf = state->timf_default;
  510. } else {
  511. dprintk("using updated timf");
  512. timf = state->timf;
  513. }
  514. dib8000_write_word(state, 29, (u16) ((timf >> 16) & 0xffff));
  515. dib8000_write_word(state, 30, (u16) ((timf) & 0xffff));
  516. return 0;
  517. }
  518. static int dib8000_sad_calib(struct dib8000_state *state)
  519. {
  520. u8 sad_sel = 3;
  521. if (state->revision == 0x8090) {
  522. dib8000_write_word(state, 922, (sad_sel << 2));
  523. dib8000_write_word(state, 923, 2048);
  524. dib8000_write_word(state, 922, (sad_sel << 2) | 0x1);
  525. dib8000_write_word(state, 922, (sad_sel << 2));
  526. } else {
  527. /* internal */
  528. dib8000_write_word(state, 923, (0 << 1) | (0 << 0));
  529. dib8000_write_word(state, 924, 776);
  530. /* do the calibration */
  531. dib8000_write_word(state, 923, (1 << 0));
  532. dib8000_write_word(state, 923, (0 << 0));
  533. }
  534. msleep(1);
  535. return 0;
  536. }
  537. static int dib8000_set_wbd_ref(struct dvb_frontend *fe, u16 value)
  538. {
  539. struct dib8000_state *state = fe->demodulator_priv;
  540. if (value > 4095)
  541. value = 4095;
  542. state->wbd_ref = value;
  543. return dib8000_write_word(state, 106, value);
  544. }
  545. static void dib8000_reset_pll_common(struct dib8000_state *state, const struct dibx000_bandwidth_config *bw)
  546. {
  547. dprintk("ifreq: %d %x, inversion: %d", bw->ifreq, bw->ifreq, bw->ifreq >> 25);
  548. if (state->revision != 0x8090) {
  549. dib8000_write_word(state, 23,
  550. (u16) (((bw->internal * 1000) >> 16) & 0xffff));
  551. dib8000_write_word(state, 24,
  552. (u16) ((bw->internal * 1000) & 0xffff));
  553. } else {
  554. dib8000_write_word(state, 23, (u16) (((bw->internal / 2 * 1000) >> 16) & 0xffff));
  555. dib8000_write_word(state, 24,
  556. (u16) ((bw->internal / 2 * 1000) & 0xffff));
  557. }
  558. dib8000_write_word(state, 27, (u16) ((bw->ifreq >> 16) & 0x01ff));
  559. dib8000_write_word(state, 28, (u16) (bw->ifreq & 0xffff));
  560. dib8000_write_word(state, 26, (u16) ((bw->ifreq >> 25) & 0x0003));
  561. if (state->revision != 0x8090)
  562. dib8000_write_word(state, 922, bw->sad_cfg);
  563. }
  564. static void dib8000_reset_pll(struct dib8000_state *state)
  565. {
  566. const struct dibx000_bandwidth_config *pll = state->cfg.pll;
  567. u16 clk_cfg1, reg;
  568. if (state->revision != 0x8090) {
  569. dib8000_write_word(state, 901,
  570. (pll->pll_prediv << 8) | (pll->pll_ratio << 0));
  571. clk_cfg1 = (1 << 10) | (0 << 9) | (pll->IO_CLK_en_core << 8) |
  572. (pll->bypclk_div << 5) | (pll->enable_refdiv << 4) |
  573. (1 << 3) | (pll->pll_range << 1) |
  574. (pll->pll_reset << 0);
  575. dib8000_write_word(state, 902, clk_cfg1);
  576. clk_cfg1 = (clk_cfg1 & 0xfff7) | (pll->pll_bypass << 3);
  577. dib8000_write_word(state, 902, clk_cfg1);
  578. dprintk("clk_cfg1: 0x%04x", clk_cfg1);
  579. /* smpl_cfg: P_refclksel=2, P_ensmplsel=1 nodivsmpl=1 */
  580. if (state->cfg.pll->ADClkSrc == 0)
  581. dib8000_write_word(state, 904,
  582. (0 << 15) | (0 << 12) | (0 << 10) |
  583. (pll->modulo << 8) |
  584. (pll->ADClkSrc << 7) | (0 << 1));
  585. else if (state->cfg.refclksel != 0)
  586. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) |
  587. ((state->cfg.refclksel & 0x3) << 10) |
  588. (pll->modulo << 8) |
  589. (pll->ADClkSrc << 7) | (0 << 1));
  590. else
  591. dib8000_write_word(state, 904, (0 << 15) | (1 << 12) |
  592. (3 << 10) | (pll->modulo << 8) |
  593. (pll->ADClkSrc << 7) | (0 << 1));
  594. } else {
  595. dib8000_write_word(state, 1856, (!pll->pll_reset<<13) |
  596. (pll->pll_range<<12) | (pll->pll_ratio<<6) |
  597. (pll->pll_prediv));
  598. reg = dib8000_read_word(state, 1857);
  599. dib8000_write_word(state, 1857, reg|(!pll->pll_bypass<<15));
  600. reg = dib8000_read_word(state, 1858); /* Force clk out pll /2 */
  601. dib8000_write_word(state, 1858, reg | 1);
  602. dib8000_write_word(state, 904, (pll->modulo << 8));
  603. }
  604. dib8000_reset_pll_common(state, pll);
  605. }
  606. static int dib8000_update_pll(struct dvb_frontend *fe,
  607. struct dibx000_bandwidth_config *pll, u32 bw, u8 ratio)
  608. {
  609. struct dib8000_state *state = fe->demodulator_priv;
  610. u16 reg_1857, reg_1856 = dib8000_read_word(state, 1856);
  611. u8 loopdiv, prediv, oldprediv = state->cfg.pll->pll_prediv ;
  612. u32 internal, xtal;
  613. /* get back old values */
  614. prediv = reg_1856 & 0x3f;
  615. loopdiv = (reg_1856 >> 6) & 0x3f;
  616. if ((pll == NULL) || (pll->pll_prediv == prediv &&
  617. pll->pll_ratio == loopdiv))
  618. return -EINVAL;
  619. dprintk("Updating pll (prediv: old = %d new = %d ; loopdiv : old = %d new = %d)", prediv, pll->pll_prediv, loopdiv, pll->pll_ratio);
  620. if (state->revision == 0x8090) {
  621. reg_1856 &= 0xf000;
  622. reg_1857 = dib8000_read_word(state, 1857);
  623. /* disable PLL */
  624. dib8000_write_word(state, 1857, reg_1857 & ~(1 << 15));
  625. dib8000_write_word(state, 1856, reg_1856 |
  626. ((pll->pll_ratio & 0x3f) << 6) |
  627. (pll->pll_prediv & 0x3f));
  628. /* write new system clk into P_sec_len */
  629. internal = dib8000_read32(state, 23) / 1000;
  630. dprintk("Old Internal = %d", internal);
  631. xtal = 2 * (internal / loopdiv) * prediv;
  632. internal = 1000 * (xtal/pll->pll_prediv) * pll->pll_ratio;
  633. dprintk("Xtal = %d , New Fmem = %d New Fdemod = %d, New Fsampling = %d", xtal, internal/1000, internal/2000, internal/8000);
  634. dprintk("New Internal = %d", internal);
  635. dib8000_write_word(state, 23,
  636. (u16) (((internal / 2) >> 16) & 0xffff));
  637. dib8000_write_word(state, 24, (u16) ((internal / 2) & 0xffff));
  638. /* enable PLL */
  639. dib8000_write_word(state, 1857, reg_1857 | (1 << 15));
  640. while (((dib8000_read_word(state, 1856)>>15)&0x1) != 1)
  641. dprintk("Waiting for PLL to lock");
  642. /* verify */
  643. reg_1856 = dib8000_read_word(state, 1856);
  644. dprintk("PLL Updated with prediv = %d and loopdiv = %d",
  645. reg_1856&0x3f, (reg_1856>>6)&0x3f);
  646. } else {
  647. if (bw != state->current_demod_bw) {
  648. /** Bandwidth change => force PLL update **/
  649. dprintk("PLL: Bandwidth Change %d MHz -> %d MHz (prediv: %d->%d)", state->current_demod_bw / 1000, bw / 1000, oldprediv, state->cfg.pll->pll_prediv);
  650. if (state->cfg.pll->pll_prediv != oldprediv) {
  651. /** Full PLL change only if prediv is changed **/
  652. /** full update => bypass and reconfigure **/
  653. dprintk("PLL: New Setting for %d MHz Bandwidth (prediv: %d, ratio: %d)", bw/1000, state->cfg.pll->pll_prediv, state->cfg.pll->pll_ratio);
  654. dib8000_write_word(state, 902, dib8000_read_word(state, 902) | (1<<3)); /* bypass PLL */
  655. dib8000_reset_pll(state);
  656. dib8000_write_word(state, 898, 0x0004); /* sad */
  657. } else
  658. ratio = state->cfg.pll->pll_ratio;
  659. state->current_demod_bw = bw;
  660. }
  661. if (ratio != 0) {
  662. /** ratio update => only change ratio **/
  663. dprintk("PLL: Update ratio (prediv: %d, ratio: %d)", state->cfg.pll->pll_prediv, ratio);
  664. dib8000_write_word(state, 901, (state->cfg.pll->pll_prediv << 8) | (ratio << 0)); /* only the PLL ratio is updated. */
  665. }
  666. }
  667. return 0;
  668. }
  669. static int dib8000_reset_gpio(struct dib8000_state *st)
  670. {
  671. /* reset the GPIOs */
  672. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  673. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  674. /* TODO 782 is P_gpio_od */
  675. dib8000_write_word(st, 1032, st->cfg.gpio_pwm_pos);
  676. dib8000_write_word(st, 1037, st->cfg.pwm_freq_div);
  677. return 0;
  678. }
  679. static int dib8000_cfg_gpio(struct dib8000_state *st, u8 num, u8 dir, u8 val)
  680. {
  681. st->cfg.gpio_dir = dib8000_read_word(st, 1029);
  682. st->cfg.gpio_dir &= ~(1 << num); /* reset the direction bit */
  683. st->cfg.gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  684. dib8000_write_word(st, 1029, st->cfg.gpio_dir);
  685. st->cfg.gpio_val = dib8000_read_word(st, 1030);
  686. st->cfg.gpio_val &= ~(1 << num); /* reset the direction bit */
  687. st->cfg.gpio_val |= (val & 0x01) << num; /* set the new value */
  688. dib8000_write_word(st, 1030, st->cfg.gpio_val);
  689. dprintk("gpio dir: %x: gpio val: %x", st->cfg.gpio_dir, st->cfg.gpio_val);
  690. return 0;
  691. }
  692. static int dib8000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  693. {
  694. struct dib8000_state *state = fe->demodulator_priv;
  695. return dib8000_cfg_gpio(state, num, dir, val);
  696. }
  697. static const u16 dib8000_defaults[] = {
  698. /* auto search configuration - lock0 by default waiting
  699. * for cpil_lock; lock1 cpil_lock; lock2 tmcc_sync_lock */
  700. 3, 7,
  701. 0x0004,
  702. 0x0400,
  703. 0x0814,
  704. 12, 11,
  705. 0x001b,
  706. 0x7740,
  707. 0x005b,
  708. 0x8d80,
  709. 0x01c9,
  710. 0xc380,
  711. 0x0000,
  712. 0x0080,
  713. 0x0000,
  714. 0x0090,
  715. 0x0001,
  716. 0xd4c0,
  717. /*1, 32,
  718. 0x6680 // P_corm_thres Lock algorithms configuration */
  719. 11, 80, /* set ADC level to -16 */
  720. (1 << 13) - 825 - 117,
  721. (1 << 13) - 837 - 117,
  722. (1 << 13) - 811 - 117,
  723. (1 << 13) - 766 - 117,
  724. (1 << 13) - 737 - 117,
  725. (1 << 13) - 693 - 117,
  726. (1 << 13) - 648 - 117,
  727. (1 << 13) - 619 - 117,
  728. (1 << 13) - 575 - 117,
  729. (1 << 13) - 531 - 117,
  730. (1 << 13) - 501 - 117,
  731. 4, 108,
  732. 0,
  733. 0,
  734. 0,
  735. 0,
  736. 1, 175,
  737. 0x0410,
  738. 1, 179,
  739. 8192, // P_fft_nb_to_cut
  740. 6, 181,
  741. 0x2800, // P_coff_corthres_ ( 2k 4k 8k ) 0x2800
  742. 0x2800,
  743. 0x2800,
  744. 0x2800, // P_coff_cpilthres_ ( 2k 4k 8k ) 0x2800
  745. 0x2800,
  746. 0x2800,
  747. 2, 193,
  748. 0x0666, // P_pha3_thres
  749. 0x0000, // P_cti_use_cpe, P_cti_use_prog
  750. 2, 205,
  751. 0x200f, // P_cspu_regul, P_cspu_win_cut
  752. 0x000f, // P_des_shift_work
  753. 5, 215,
  754. 0x023d, // P_adp_regul_cnt
  755. 0x00a4, // P_adp_noise_cnt
  756. 0x00a4, // P_adp_regul_ext
  757. 0x7ff0, // P_adp_noise_ext
  758. 0x3ccc, // P_adp_fil
  759. 1, 230,
  760. 0x0000, // P_2d_byp_ti_num
  761. 1, 263,
  762. 0x800, //P_equal_thres_wgn
  763. 1, 268,
  764. (2 << 9) | 39, // P_equal_ctrl_synchro, P_equal_speedmode
  765. 1, 270,
  766. 0x0001, // P_div_lock0_wait
  767. 1, 285,
  768. 0x0020, //p_fec_
  769. 1, 299,
  770. 0x0062, /* P_smo_mode, P_smo_rs_discard, P_smo_fifo_flush, P_smo_pid_parse, P_smo_error_discard */
  771. 1, 338,
  772. (1 << 12) | // P_ctrl_corm_thres4pre_freq_inh=1
  773. (1 << 10) |
  774. (0 << 9) | /* P_ctrl_pre_freq_inh=0 */
  775. (3 << 5) | /* P_ctrl_pre_freq_step=3 */
  776. (1 << 0), /* P_pre_freq_win_len=1 */
  777. 0,
  778. };
  779. static u16 dib8000_identify(struct i2c_device *client)
  780. {
  781. u16 value;
  782. //because of glitches sometimes
  783. value = dib8000_i2c_read16(client, 896);
  784. if ((value = dib8000_i2c_read16(client, 896)) != 0x01b3) {
  785. dprintk("wrong Vendor ID (read=0x%x)", value);
  786. return 0;
  787. }
  788. value = dib8000_i2c_read16(client, 897);
  789. if (value != 0x8000 && value != 0x8001 &&
  790. value != 0x8002 && value != 0x8090) {
  791. dprintk("wrong Device ID (%x)", value);
  792. return 0;
  793. }
  794. switch (value) {
  795. case 0x8000:
  796. dprintk("found DiB8000A");
  797. break;
  798. case 0x8001:
  799. dprintk("found DiB8000B");
  800. break;
  801. case 0x8002:
  802. dprintk("found DiB8000C");
  803. break;
  804. case 0x8090:
  805. dprintk("found DiB8096P");
  806. break;
  807. }
  808. return value;
  809. }
  810. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 *unc);
  811. static void dib8000_reset_stats(struct dvb_frontend *fe)
  812. {
  813. struct dib8000_state *state = fe->demodulator_priv;
  814. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  815. u32 ucb;
  816. memset(&c->strength, 0, sizeof(c->strength));
  817. memset(&c->cnr, 0, sizeof(c->cnr));
  818. memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
  819. memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
  820. memset(&c->block_error, 0, sizeof(c->block_error));
  821. c->strength.len = 1;
  822. c->cnr.len = 1;
  823. c->block_error.len = 1;
  824. c->block_count.len = 1;
  825. c->post_bit_error.len = 1;
  826. c->post_bit_count.len = 1;
  827. c->strength.stat[0].scale = FE_SCALE_DECIBEL;
  828. c->strength.stat[0].uvalue = 0;
  829. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  830. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  831. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  832. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  833. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  834. dib8000_read_unc_blocks(fe, &ucb);
  835. state->init_ucb = -ucb;
  836. state->ber_jiffies_stats = 0;
  837. state->per_jiffies_stats = 0;
  838. memset(&state->ber_jiffies_stats_layer, 0,
  839. sizeof(state->ber_jiffies_stats_layer));
  840. }
  841. static int dib8000_reset(struct dvb_frontend *fe)
  842. {
  843. struct dib8000_state *state = fe->demodulator_priv;
  844. if ((state->revision = dib8000_identify(&state->i2c)) == 0)
  845. return -EINVAL;
  846. /* sram lead in, rdy */
  847. if (state->revision != 0x8090)
  848. dib8000_write_word(state, 1287, 0x0003);
  849. if (state->revision == 0x8000)
  850. dprintk("error : dib8000 MA not supported");
  851. dibx000_reset_i2c_master(&state->i2c_master);
  852. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  853. /* always leave the VBG voltage on - it consumes almost nothing but takes a long time to start */
  854. dib8000_set_adc_state(state, DIBX000_ADC_OFF);
  855. /* restart all parts */
  856. dib8000_write_word(state, 770, 0xffff);
  857. dib8000_write_word(state, 771, 0xffff);
  858. dib8000_write_word(state, 772, 0xfffc);
  859. dib8000_write_word(state, 898, 0x000c); /* restart sad */
  860. if (state->revision == 0x8090)
  861. dib8000_write_word(state, 1280, 0x0045);
  862. else
  863. dib8000_write_word(state, 1280, 0x004d);
  864. dib8000_write_word(state, 1281, 0x000c);
  865. dib8000_write_word(state, 770, 0x0000);
  866. dib8000_write_word(state, 771, 0x0000);
  867. dib8000_write_word(state, 772, 0x0000);
  868. dib8000_write_word(state, 898, 0x0004); // sad
  869. dib8000_write_word(state, 1280, 0x0000);
  870. dib8000_write_word(state, 1281, 0x0000);
  871. /* drives */
  872. if (state->revision != 0x8090) {
  873. if (state->cfg.drives)
  874. dib8000_write_word(state, 906, state->cfg.drives);
  875. else {
  876. dprintk("using standard PAD-drive-settings, please adjust settings in config-struct to be optimal.");
  877. /* min drive SDRAM - not optimal - adjust */
  878. dib8000_write_word(state, 906, 0x2d98);
  879. }
  880. }
  881. dib8000_reset_pll(state);
  882. if (state->revision != 0x8090)
  883. dib8000_write_word(state, 898, 0x0004);
  884. if (dib8000_reset_gpio(state) != 0)
  885. dprintk("GPIO reset was not successful.");
  886. if ((state->revision != 0x8090) &&
  887. (dib8000_set_output_mode(fe, OUTMODE_HIGH_Z) != 0))
  888. dprintk("OUTPUT_MODE could not be resetted.");
  889. state->current_agc = NULL;
  890. // P_iqc_alpha_pha, P_iqc_alpha_amp, P_iqc_dcc_alpha, ...
  891. /* P_iqc_ca2 = 0; P_iqc_impnc_on = 0; P_iqc_mode = 0; */
  892. if (state->cfg.pll->ifreq == 0)
  893. dib8000_write_word(state, 40, 0x0755); /* P_iqc_corr_inh = 0 enable IQcorr block */
  894. else
  895. dib8000_write_word(state, 40, 0x1f55); /* P_iqc_corr_inh = 1 disable IQcorr block */
  896. {
  897. u16 l = 0, r;
  898. const u16 *n;
  899. n = dib8000_defaults;
  900. l = *n++;
  901. while (l) {
  902. r = *n++;
  903. do {
  904. dib8000_write_word(state, r, *n++);
  905. r++;
  906. } while (--l);
  907. l = *n++;
  908. }
  909. }
  910. state->isdbt_cfg_loaded = 0;
  911. //div_cfg override for special configs
  912. if ((state->revision != 8090) && (state->cfg.div_cfg != 0))
  913. dib8000_write_word(state, 903, state->cfg.div_cfg);
  914. /* unforce divstr regardless whether i2c enumeration was done or not */
  915. dib8000_write_word(state, 1285, dib8000_read_word(state, 1285) & ~(1 << 1));
  916. dib8000_set_bandwidth(fe, 6000);
  917. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON);
  918. dib8000_sad_calib(state);
  919. if (state->revision != 0x8090)
  920. dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF);
  921. /* ber_rs_len = 3 */
  922. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5));
  923. dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
  924. dib8000_reset_stats(fe);
  925. return 0;
  926. }
  927. static void dib8000_restart_agc(struct dib8000_state *state)
  928. {
  929. // P_restart_iqc & P_restart_agc
  930. dib8000_write_word(state, 770, 0x0a00);
  931. dib8000_write_word(state, 770, 0x0000);
  932. }
  933. static int dib8000_update_lna(struct dib8000_state *state)
  934. {
  935. u16 dyn_gain;
  936. if (state->cfg.update_lna) {
  937. // read dyn_gain here (because it is demod-dependent and not tuner)
  938. dyn_gain = dib8000_read_word(state, 390);
  939. if (state->cfg.update_lna(state->fe[0], dyn_gain)) {
  940. dib8000_restart_agc(state);
  941. return 1;
  942. }
  943. }
  944. return 0;
  945. }
  946. static int dib8000_set_agc_config(struct dib8000_state *state, u8 band)
  947. {
  948. struct dibx000_agc_config *agc = NULL;
  949. int i;
  950. u16 reg;
  951. if (state->current_band == band && state->current_agc != NULL)
  952. return 0;
  953. state->current_band = band;
  954. for (i = 0; i < state->cfg.agc_config_count; i++)
  955. if (state->cfg.agc[i].band_caps & band) {
  956. agc = &state->cfg.agc[i];
  957. break;
  958. }
  959. if (agc == NULL) {
  960. dprintk("no valid AGC configuration found for band 0x%02x", band);
  961. return -EINVAL;
  962. }
  963. state->current_agc = agc;
  964. /* AGC */
  965. dib8000_write_word(state, 76, agc->setup);
  966. dib8000_write_word(state, 77, agc->inv_gain);
  967. dib8000_write_word(state, 78, agc->time_stabiliz);
  968. dib8000_write_word(state, 101, (agc->alpha_level << 12) | agc->thlock);
  969. // Demod AGC loop configuration
  970. dib8000_write_word(state, 102, (agc->alpha_mant << 5) | agc->alpha_exp);
  971. dib8000_write_word(state, 103, (agc->beta_mant << 6) | agc->beta_exp);
  972. dprintk("WBD: ref: %d, sel: %d, active: %d, alpha: %d",
  973. state->wbd_ref != 0 ? state->wbd_ref : agc->wbd_ref, agc->wbd_sel, !agc->perform_agc_softsplit, agc->wbd_sel);
  974. /* AGC continued */
  975. if (state->wbd_ref != 0)
  976. dib8000_write_word(state, 106, state->wbd_ref);
  977. else // use default
  978. dib8000_write_word(state, 106, agc->wbd_ref);
  979. if (state->revision == 0x8090) {
  980. reg = dib8000_read_word(state, 922) & (0x3 << 2);
  981. dib8000_write_word(state, 922, reg | (agc->wbd_sel << 2));
  982. }
  983. dib8000_write_word(state, 107, (agc->wbd_alpha << 9) | (agc->perform_agc_softsplit << 8));
  984. dib8000_write_word(state, 108, agc->agc1_max);
  985. dib8000_write_word(state, 109, agc->agc1_min);
  986. dib8000_write_word(state, 110, agc->agc2_max);
  987. dib8000_write_word(state, 111, agc->agc2_min);
  988. dib8000_write_word(state, 112, (agc->agc1_pt1 << 8) | agc->agc1_pt2);
  989. dib8000_write_word(state, 113, (agc->agc1_slope1 << 8) | agc->agc1_slope2);
  990. dib8000_write_word(state, 114, (agc->agc2_pt1 << 8) | agc->agc2_pt2);
  991. dib8000_write_word(state, 115, (agc->agc2_slope1 << 8) | agc->agc2_slope2);
  992. dib8000_write_word(state, 75, agc->agc1_pt3);
  993. if (state->revision != 0x8090)
  994. dib8000_write_word(state, 923,
  995. (dib8000_read_word(state, 923) & 0xffe3) |
  996. (agc->wbd_inv << 4) | (agc->wbd_sel << 2));
  997. return 0;
  998. }
  999. static void dib8000_pwm_agc_reset(struct dvb_frontend *fe)
  1000. {
  1001. struct dib8000_state *state = fe->demodulator_priv;
  1002. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  1003. dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000)));
  1004. }
  1005. static int dib8000_agc_soft_split(struct dib8000_state *state)
  1006. {
  1007. u16 agc, split_offset;
  1008. if (!state->current_agc || !state->current_agc->perform_agc_softsplit || state->current_agc->split.max == 0)
  1009. return 0;
  1010. // n_agc_global
  1011. agc = dib8000_read_word(state, 390);
  1012. if (agc > state->current_agc->split.min_thres)
  1013. split_offset = state->current_agc->split.min;
  1014. else if (agc < state->current_agc->split.max_thres)
  1015. split_offset = state->current_agc->split.max;
  1016. else
  1017. split_offset = state->current_agc->split.max *
  1018. (agc - state->current_agc->split.min_thres) /
  1019. (state->current_agc->split.max_thres - state->current_agc->split.min_thres);
  1020. dprintk("AGC split_offset: %d", split_offset);
  1021. // P_agc_force_split and P_agc_split_offset
  1022. dib8000_write_word(state, 107, (dib8000_read_word(state, 107) & 0xff00) | split_offset);
  1023. return 5000;
  1024. }
  1025. static int dib8000_agc_startup(struct dvb_frontend *fe)
  1026. {
  1027. struct dib8000_state *state = fe->demodulator_priv;
  1028. enum frontend_tune_state *tune_state = &state->tune_state;
  1029. int ret = 0;
  1030. u16 reg;
  1031. u32 upd_demod_gain_period = 0x8000;
  1032. switch (*tune_state) {
  1033. case CT_AGC_START:
  1034. // set power-up level: interf+analog+AGC
  1035. if (state->revision != 0x8090)
  1036. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  1037. else {
  1038. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  1039. reg = dib8000_read_word(state, 1947)&0xff00;
  1040. dib8000_write_word(state, 1946,
  1041. upd_demod_gain_period & 0xFFFF);
  1042. /* bit 14 = enDemodGain */
  1043. dib8000_write_word(state, 1947, reg | (1<<14) |
  1044. ((upd_demod_gain_period >> 16) & 0xFF));
  1045. /* enable adc i & q */
  1046. reg = dib8000_read_word(state, 1920);
  1047. dib8000_write_word(state, 1920, (reg | 0x3) &
  1048. (~(1 << 7)));
  1049. }
  1050. if (dib8000_set_agc_config(state, (unsigned char)(BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency / 1000))) != 0) {
  1051. *tune_state = CT_AGC_STOP;
  1052. state->status = FE_STATUS_TUNE_FAILED;
  1053. break;
  1054. }
  1055. ret = 70;
  1056. *tune_state = CT_AGC_STEP_0;
  1057. break;
  1058. case CT_AGC_STEP_0:
  1059. //AGC initialization
  1060. if (state->cfg.agc_control)
  1061. state->cfg.agc_control(fe, 1);
  1062. dib8000_restart_agc(state);
  1063. // wait AGC rough lock time
  1064. ret = 50;
  1065. *tune_state = CT_AGC_STEP_1;
  1066. break;
  1067. case CT_AGC_STEP_1:
  1068. // wait AGC accurate lock time
  1069. ret = 70;
  1070. if (dib8000_update_lna(state))
  1071. // wait only AGC rough lock time
  1072. ret = 50;
  1073. else
  1074. *tune_state = CT_AGC_STEP_2;
  1075. break;
  1076. case CT_AGC_STEP_2:
  1077. dib8000_agc_soft_split(state);
  1078. if (state->cfg.agc_control)
  1079. state->cfg.agc_control(fe, 0);
  1080. *tune_state = CT_AGC_STOP;
  1081. break;
  1082. default:
  1083. ret = dib8000_agc_soft_split(state);
  1084. break;
  1085. }
  1086. return ret;
  1087. }
  1088. static void dib8096p_host_bus_drive(struct dib8000_state *state, u8 drive)
  1089. {
  1090. u16 reg;
  1091. drive &= 0x7;
  1092. /* drive host bus 2, 3, 4 */
  1093. reg = dib8000_read_word(state, 1798) &
  1094. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  1095. reg |= (drive<<12) | (drive<<6) | drive;
  1096. dib8000_write_word(state, 1798, reg);
  1097. /* drive host bus 5,6 */
  1098. reg = dib8000_read_word(state, 1799) & ~((0x7 << 2) | (0x7 << 8));
  1099. reg |= (drive<<8) | (drive<<2);
  1100. dib8000_write_word(state, 1799, reg);
  1101. /* drive host bus 7, 8, 9 */
  1102. reg = dib8000_read_word(state, 1800) &
  1103. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  1104. reg |= (drive<<12) | (drive<<6) | drive;
  1105. dib8000_write_word(state, 1800, reg);
  1106. /* drive host bus 10, 11 */
  1107. reg = dib8000_read_word(state, 1801) & ~((0x7 << 2) | (0x7 << 8));
  1108. reg |= (drive<<8) | (drive<<2);
  1109. dib8000_write_word(state, 1801, reg);
  1110. /* drive host bus 12, 13, 14 */
  1111. reg = dib8000_read_word(state, 1802) &
  1112. ~(0x7 | (0x7 << 6) | (0x7 << 12));
  1113. reg |= (drive<<12) | (drive<<6) | drive;
  1114. dib8000_write_word(state, 1802, reg);
  1115. }
  1116. static u32 dib8096p_calcSyncFreq(u32 P_Kin, u32 P_Kout,
  1117. u32 insertExtSynchro, u32 syncSize)
  1118. {
  1119. u32 quantif = 3;
  1120. u32 nom = (insertExtSynchro * P_Kin+syncSize);
  1121. u32 denom = P_Kout;
  1122. u32 syncFreq = ((nom << quantif) / denom);
  1123. if ((syncFreq & ((1 << quantif) - 1)) != 0)
  1124. syncFreq = (syncFreq >> quantif) + 1;
  1125. else
  1126. syncFreq = (syncFreq >> quantif);
  1127. if (syncFreq != 0)
  1128. syncFreq = syncFreq - 1;
  1129. return syncFreq;
  1130. }
  1131. static void dib8096p_cfg_DibTx(struct dib8000_state *state, u32 P_Kin,
  1132. u32 P_Kout, u32 insertExtSynchro, u32 synchroMode,
  1133. u32 syncWord, u32 syncSize)
  1134. {
  1135. dprintk("Configure DibStream Tx");
  1136. dib8000_write_word(state, 1615, 1);
  1137. dib8000_write_word(state, 1603, P_Kin);
  1138. dib8000_write_word(state, 1605, P_Kout);
  1139. dib8000_write_word(state, 1606, insertExtSynchro);
  1140. dib8000_write_word(state, 1608, synchroMode);
  1141. dib8000_write_word(state, 1609, (syncWord >> 16) & 0xffff);
  1142. dib8000_write_word(state, 1610, syncWord & 0xffff);
  1143. dib8000_write_word(state, 1612, syncSize);
  1144. dib8000_write_word(state, 1615, 0);
  1145. }
  1146. static void dib8096p_cfg_DibRx(struct dib8000_state *state, u32 P_Kin,
  1147. u32 P_Kout, u32 synchroMode, u32 insertExtSynchro,
  1148. u32 syncWord, u32 syncSize, u32 dataOutRate)
  1149. {
  1150. u32 syncFreq;
  1151. dprintk("Configure DibStream Rx synchroMode = %d", synchroMode);
  1152. if ((P_Kin != 0) && (P_Kout != 0)) {
  1153. syncFreq = dib8096p_calcSyncFreq(P_Kin, P_Kout,
  1154. insertExtSynchro, syncSize);
  1155. dib8000_write_word(state, 1542, syncFreq);
  1156. }
  1157. dib8000_write_word(state, 1554, 1);
  1158. dib8000_write_word(state, 1536, P_Kin);
  1159. dib8000_write_word(state, 1537, P_Kout);
  1160. dib8000_write_word(state, 1539, synchroMode);
  1161. dib8000_write_word(state, 1540, (syncWord >> 16) & 0xffff);
  1162. dib8000_write_word(state, 1541, syncWord & 0xffff);
  1163. dib8000_write_word(state, 1543, syncSize);
  1164. dib8000_write_word(state, 1544, dataOutRate);
  1165. dib8000_write_word(state, 1554, 0);
  1166. }
  1167. static void dib8096p_enMpegMux(struct dib8000_state *state, int onoff)
  1168. {
  1169. u16 reg_1287;
  1170. reg_1287 = dib8000_read_word(state, 1287);
  1171. switch (onoff) {
  1172. case 1:
  1173. reg_1287 &= ~(1 << 8);
  1174. break;
  1175. case 0:
  1176. reg_1287 |= (1 << 8);
  1177. break;
  1178. }
  1179. dib8000_write_word(state, 1287, reg_1287);
  1180. }
  1181. static void dib8096p_configMpegMux(struct dib8000_state *state,
  1182. u16 pulseWidth, u16 enSerialMode, u16 enSerialClkDiv2)
  1183. {
  1184. u16 reg_1287;
  1185. dprintk("Enable Mpeg mux");
  1186. dib8096p_enMpegMux(state, 0);
  1187. /* If the input mode is MPEG do not divide the serial clock */
  1188. if ((enSerialMode == 1) && (state->input_mode_mpeg == 1))
  1189. enSerialClkDiv2 = 0;
  1190. reg_1287 = ((pulseWidth & 0x1f) << 3) |
  1191. ((enSerialMode & 0x1) << 2) | (enSerialClkDiv2 & 0x1);
  1192. dib8000_write_word(state, 1287, reg_1287);
  1193. dib8096p_enMpegMux(state, 1);
  1194. }
  1195. static void dib8096p_setDibTxMux(struct dib8000_state *state, int mode)
  1196. {
  1197. u16 reg_1288 = dib8000_read_word(state, 1288) & ~(0x7 << 7);
  1198. switch (mode) {
  1199. case MPEG_ON_DIBTX:
  1200. dprintk("SET MPEG ON DIBSTREAM TX");
  1201. dib8096p_cfg_DibTx(state, 8, 5, 0, 0, 0, 0);
  1202. reg_1288 |= (1 << 9); break;
  1203. case DIV_ON_DIBTX:
  1204. dprintk("SET DIV_OUT ON DIBSTREAM TX");
  1205. dib8096p_cfg_DibTx(state, 5, 5, 0, 0, 0, 0);
  1206. reg_1288 |= (1 << 8); break;
  1207. case ADC_ON_DIBTX:
  1208. dprintk("SET ADC_OUT ON DIBSTREAM TX");
  1209. dib8096p_cfg_DibTx(state, 20, 5, 10, 0, 0, 0);
  1210. reg_1288 |= (1 << 7); break;
  1211. default:
  1212. break;
  1213. }
  1214. dib8000_write_word(state, 1288, reg_1288);
  1215. }
  1216. static void dib8096p_setHostBusMux(struct dib8000_state *state, int mode)
  1217. {
  1218. u16 reg_1288 = dib8000_read_word(state, 1288) & ~(0x7 << 4);
  1219. switch (mode) {
  1220. case DEMOUT_ON_HOSTBUS:
  1221. dprintk("SET DEM OUT OLD INTERF ON HOST BUS");
  1222. dib8096p_enMpegMux(state, 0);
  1223. reg_1288 |= (1 << 6);
  1224. break;
  1225. case DIBTX_ON_HOSTBUS:
  1226. dprintk("SET DIBSTREAM TX ON HOST BUS");
  1227. dib8096p_enMpegMux(state, 0);
  1228. reg_1288 |= (1 << 5);
  1229. break;
  1230. case MPEG_ON_HOSTBUS:
  1231. dprintk("SET MPEG MUX ON HOST BUS");
  1232. reg_1288 |= (1 << 4);
  1233. break;
  1234. default:
  1235. break;
  1236. }
  1237. dib8000_write_word(state, 1288, reg_1288);
  1238. }
  1239. static int dib8096p_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1240. {
  1241. struct dib8000_state *state = fe->demodulator_priv;
  1242. u16 reg_1287;
  1243. switch (onoff) {
  1244. case 0: /* only use the internal way - not the diversity input */
  1245. dprintk("%s mode OFF : by default Enable Mpeg INPUT",
  1246. __func__);
  1247. /* outputRate = 8 */
  1248. dib8096p_cfg_DibRx(state, 8, 5, 0, 0, 0, 8, 0);
  1249. /* Do not divide the serial clock of MPEG MUX in
  1250. SERIAL MODE in case input mode MPEG is used */
  1251. reg_1287 = dib8000_read_word(state, 1287);
  1252. /* enSerialClkDiv2 == 1 ? */
  1253. if ((reg_1287 & 0x1) == 1) {
  1254. /* force enSerialClkDiv2 = 0 */
  1255. reg_1287 &= ~0x1;
  1256. dib8000_write_word(state, 1287, reg_1287);
  1257. }
  1258. state->input_mode_mpeg = 1;
  1259. break;
  1260. case 1: /* both ways */
  1261. case 2: /* only the diversity input */
  1262. dprintk("%s ON : Enable diversity INPUT", __func__);
  1263. dib8096p_cfg_DibRx(state, 5, 5, 0, 0, 0, 0, 0);
  1264. state->input_mode_mpeg = 0;
  1265. break;
  1266. }
  1267. dib8000_set_diversity_in(state->fe[0], onoff);
  1268. return 0;
  1269. }
  1270. static int dib8096p_set_output_mode(struct dvb_frontend *fe, int mode)
  1271. {
  1272. struct dib8000_state *state = fe->demodulator_priv;
  1273. u16 outreg, smo_mode, fifo_threshold;
  1274. u8 prefer_mpeg_mux_use = 1;
  1275. int ret = 0;
  1276. state->output_mode = mode;
  1277. dib8096p_host_bus_drive(state, 1);
  1278. fifo_threshold = 1792;
  1279. smo_mode = (dib8000_read_word(state, 299) & 0x0050) | (1 << 1);
  1280. outreg = dib8000_read_word(state, 1286) &
  1281. ~((1 << 10) | (0x7 << 6) | (1 << 1));
  1282. switch (mode) {
  1283. case OUTMODE_HIGH_Z:
  1284. outreg = 0;
  1285. break;
  1286. case OUTMODE_MPEG2_SERIAL:
  1287. if (prefer_mpeg_mux_use) {
  1288. dprintk("dib8096P setting output mode TS_SERIAL using Mpeg Mux");
  1289. dib8096p_configMpegMux(state, 3, 1, 1);
  1290. dib8096p_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1291. } else {/* Use Smooth block */
  1292. dprintk("dib8096P setting output mode TS_SERIAL using Smooth bloc");
  1293. dib8096p_setHostBusMux(state,
  1294. DEMOUT_ON_HOSTBUS);
  1295. outreg |= (2 << 6) | (0 << 1);
  1296. }
  1297. break;
  1298. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1299. if (prefer_mpeg_mux_use) {
  1300. dprintk("dib8096P setting output mode TS_PARALLEL_GATED using Mpeg Mux");
  1301. dib8096p_configMpegMux(state, 2, 0, 0);
  1302. dib8096p_setHostBusMux(state, MPEG_ON_HOSTBUS);
  1303. } else { /* Use Smooth block */
  1304. dprintk("dib8096P setting output mode TS_PARALLEL_GATED using Smooth block");
  1305. dib8096p_setHostBusMux(state,
  1306. DEMOUT_ON_HOSTBUS);
  1307. outreg |= (0 << 6);
  1308. }
  1309. break;
  1310. case OUTMODE_MPEG2_PAR_CONT_CLK: /* Using Smooth block only */
  1311. dprintk("dib8096P setting output mode TS_PARALLEL_CONT using Smooth block");
  1312. dib8096p_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1313. outreg |= (1 << 6);
  1314. break;
  1315. case OUTMODE_MPEG2_FIFO:
  1316. /* Using Smooth block because not supported
  1317. by new Mpeg Mux bloc */
  1318. dprintk("dib8096P setting output mode TS_FIFO using Smooth block");
  1319. dib8096p_setHostBusMux(state, DEMOUT_ON_HOSTBUS);
  1320. outreg |= (5 << 6);
  1321. smo_mode |= (3 << 1);
  1322. fifo_threshold = 512;
  1323. break;
  1324. case OUTMODE_DIVERSITY:
  1325. dprintk("dib8096P setting output mode MODE_DIVERSITY");
  1326. dib8096p_setDibTxMux(state, DIV_ON_DIBTX);
  1327. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1328. break;
  1329. case OUTMODE_ANALOG_ADC:
  1330. dprintk("dib8096P setting output mode MODE_ANALOG_ADC");
  1331. dib8096p_setDibTxMux(state, ADC_ON_DIBTX);
  1332. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  1333. break;
  1334. }
  1335. if (mode != OUTMODE_HIGH_Z)
  1336. outreg |= (1<<10);
  1337. dprintk("output_mpeg2_in_188_bytes = %d",
  1338. state->cfg.output_mpeg2_in_188_bytes);
  1339. if (state->cfg.output_mpeg2_in_188_bytes)
  1340. smo_mode |= (1 << 5);
  1341. ret |= dib8000_write_word(state, 299, smo_mode);
  1342. /* synchronous fread */
  1343. ret |= dib8000_write_word(state, 299 + 1, fifo_threshold);
  1344. ret |= dib8000_write_word(state, 1286, outreg);
  1345. return ret;
  1346. }
  1347. static int map_addr_to_serpar_number(struct i2c_msg *msg)
  1348. {
  1349. if (msg->buf[0] <= 15)
  1350. msg->buf[0] -= 1;
  1351. else if (msg->buf[0] == 17)
  1352. msg->buf[0] = 15;
  1353. else if (msg->buf[0] == 16)
  1354. msg->buf[0] = 17;
  1355. else if (msg->buf[0] == 19)
  1356. msg->buf[0] = 16;
  1357. else if (msg->buf[0] >= 21 && msg->buf[0] <= 25)
  1358. msg->buf[0] -= 3;
  1359. else if (msg->buf[0] == 28)
  1360. msg->buf[0] = 23;
  1361. else if (msg->buf[0] == 99)
  1362. msg->buf[0] = 99;
  1363. else
  1364. return -EINVAL;
  1365. return 0;
  1366. }
  1367. static int dib8096p_tuner_write_serpar(struct i2c_adapter *i2c_adap,
  1368. struct i2c_msg msg[], int num)
  1369. {
  1370. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1371. u8 n_overflow = 1;
  1372. u16 i = 1000;
  1373. u16 serpar_num = msg[0].buf[0];
  1374. while (n_overflow == 1 && i) {
  1375. n_overflow = (dib8000_read_word(state, 1984) >> 1) & 0x1;
  1376. i--;
  1377. if (i == 0)
  1378. dprintk("Tuner ITF: write busy (overflow)");
  1379. }
  1380. dib8000_write_word(state, 1985, (1 << 6) | (serpar_num & 0x3f));
  1381. dib8000_write_word(state, 1986, (msg[0].buf[1] << 8) | msg[0].buf[2]);
  1382. return num;
  1383. }
  1384. static int dib8096p_tuner_read_serpar(struct i2c_adapter *i2c_adap,
  1385. struct i2c_msg msg[], int num)
  1386. {
  1387. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1388. u8 n_overflow = 1, n_empty = 1;
  1389. u16 i = 1000;
  1390. u16 serpar_num = msg[0].buf[0];
  1391. u16 read_word;
  1392. while (n_overflow == 1 && i) {
  1393. n_overflow = (dib8000_read_word(state, 1984) >> 1) & 0x1;
  1394. i--;
  1395. if (i == 0)
  1396. dprintk("TunerITF: read busy (overflow)");
  1397. }
  1398. dib8000_write_word(state, 1985, (0<<6) | (serpar_num&0x3f));
  1399. i = 1000;
  1400. while (n_empty == 1 && i) {
  1401. n_empty = dib8000_read_word(state, 1984)&0x1;
  1402. i--;
  1403. if (i == 0)
  1404. dprintk("TunerITF: read busy (empty)");
  1405. }
  1406. read_word = dib8000_read_word(state, 1987);
  1407. msg[1].buf[0] = (read_word >> 8) & 0xff;
  1408. msg[1].buf[1] = (read_word) & 0xff;
  1409. return num;
  1410. }
  1411. static int dib8096p_tuner_rw_serpar(struct i2c_adapter *i2c_adap,
  1412. struct i2c_msg msg[], int num)
  1413. {
  1414. if (map_addr_to_serpar_number(&msg[0]) == 0) {
  1415. if (num == 1) /* write */
  1416. return dib8096p_tuner_write_serpar(i2c_adap, msg, 1);
  1417. else /* read */
  1418. return dib8096p_tuner_read_serpar(i2c_adap, msg, 2);
  1419. }
  1420. return num;
  1421. }
  1422. static int dib8096p_rw_on_apb(struct i2c_adapter *i2c_adap,
  1423. struct i2c_msg msg[], int num, u16 apb_address)
  1424. {
  1425. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1426. u16 word;
  1427. if (num == 1) { /* write */
  1428. dib8000_write_word(state, apb_address,
  1429. ((msg[0].buf[1] << 8) | (msg[0].buf[2])));
  1430. } else {
  1431. word = dib8000_read_word(state, apb_address);
  1432. msg[1].buf[0] = (word >> 8) & 0xff;
  1433. msg[1].buf[1] = (word) & 0xff;
  1434. }
  1435. return num;
  1436. }
  1437. static int dib8096p_tuner_xfer(struct i2c_adapter *i2c_adap,
  1438. struct i2c_msg msg[], int num)
  1439. {
  1440. struct dib8000_state *state = i2c_get_adapdata(i2c_adap);
  1441. u16 apb_address = 0, word;
  1442. int i = 0;
  1443. switch (msg[0].buf[0]) {
  1444. case 0x12:
  1445. apb_address = 1920;
  1446. break;
  1447. case 0x14:
  1448. apb_address = 1921;
  1449. break;
  1450. case 0x24:
  1451. apb_address = 1922;
  1452. break;
  1453. case 0x1a:
  1454. apb_address = 1923;
  1455. break;
  1456. case 0x22:
  1457. apb_address = 1924;
  1458. break;
  1459. case 0x33:
  1460. apb_address = 1926;
  1461. break;
  1462. case 0x34:
  1463. apb_address = 1927;
  1464. break;
  1465. case 0x35:
  1466. apb_address = 1928;
  1467. break;
  1468. case 0x36:
  1469. apb_address = 1929;
  1470. break;
  1471. case 0x37:
  1472. apb_address = 1930;
  1473. break;
  1474. case 0x38:
  1475. apb_address = 1931;
  1476. break;
  1477. case 0x39:
  1478. apb_address = 1932;
  1479. break;
  1480. case 0x2a:
  1481. apb_address = 1935;
  1482. break;
  1483. case 0x2b:
  1484. apb_address = 1936;
  1485. break;
  1486. case 0x2c:
  1487. apb_address = 1937;
  1488. break;
  1489. case 0x2d:
  1490. apb_address = 1938;
  1491. break;
  1492. case 0x2e:
  1493. apb_address = 1939;
  1494. break;
  1495. case 0x2f:
  1496. apb_address = 1940;
  1497. break;
  1498. case 0x30:
  1499. apb_address = 1941;
  1500. break;
  1501. case 0x31:
  1502. apb_address = 1942;
  1503. break;
  1504. case 0x32:
  1505. apb_address = 1943;
  1506. break;
  1507. case 0x3e:
  1508. apb_address = 1944;
  1509. break;
  1510. case 0x3f:
  1511. apb_address = 1945;
  1512. break;
  1513. case 0x40:
  1514. apb_address = 1948;
  1515. break;
  1516. case 0x25:
  1517. apb_address = 936;
  1518. break;
  1519. case 0x26:
  1520. apb_address = 937;
  1521. break;
  1522. case 0x27:
  1523. apb_address = 938;
  1524. break;
  1525. case 0x28:
  1526. apb_address = 939;
  1527. break;
  1528. case 0x1d:
  1529. /* get sad sel request */
  1530. i = ((dib8000_read_word(state, 921) >> 12)&0x3);
  1531. word = dib8000_read_word(state, 924+i);
  1532. msg[1].buf[0] = (word >> 8) & 0xff;
  1533. msg[1].buf[1] = (word) & 0xff;
  1534. return num;
  1535. case 0x1f:
  1536. if (num == 1) { /* write */
  1537. word = (u16) ((msg[0].buf[1] << 8) |
  1538. msg[0].buf[2]);
  1539. /* in the VGAMODE Sel are located on bit 0/1 */
  1540. word &= 0x3;
  1541. word = (dib8000_read_word(state, 921) &
  1542. ~(3<<12)) | (word<<12);
  1543. /* Set the proper input */
  1544. dib8000_write_word(state, 921, word);
  1545. return num;
  1546. }
  1547. }
  1548. if (apb_address != 0) /* R/W acces via APB */
  1549. return dib8096p_rw_on_apb(i2c_adap, msg, num, apb_address);
  1550. else /* R/W access via SERPAR */
  1551. return dib8096p_tuner_rw_serpar(i2c_adap, msg, num);
  1552. return 0;
  1553. }
  1554. static u32 dib8096p_i2c_func(struct i2c_adapter *adapter)
  1555. {
  1556. return I2C_FUNC_I2C;
  1557. }
  1558. static struct i2c_algorithm dib8096p_tuner_xfer_algo = {
  1559. .master_xfer = dib8096p_tuner_xfer,
  1560. .functionality = dib8096p_i2c_func,
  1561. };
  1562. static struct i2c_adapter *dib8096p_get_i2c_tuner(struct dvb_frontend *fe)
  1563. {
  1564. struct dib8000_state *st = fe->demodulator_priv;
  1565. return &st->dib8096p_tuner_adap;
  1566. }
  1567. static int dib8096p_tuner_sleep(struct dvb_frontend *fe, int onoff)
  1568. {
  1569. struct dib8000_state *state = fe->demodulator_priv;
  1570. u16 en_cur_state;
  1571. dprintk("sleep dib8096p: %d", onoff);
  1572. en_cur_state = dib8000_read_word(state, 1922);
  1573. /* LNAs and MIX are ON and therefore it is a valid configuration */
  1574. if (en_cur_state > 0xff)
  1575. state->tuner_enable = en_cur_state ;
  1576. if (onoff)
  1577. en_cur_state &= 0x00ff;
  1578. else {
  1579. if (state->tuner_enable != 0)
  1580. en_cur_state = state->tuner_enable;
  1581. }
  1582. dib8000_write_word(state, 1922, en_cur_state);
  1583. return 0;
  1584. }
  1585. static const s32 lut_1000ln_mant[] =
  1586. {
  1587. 908, 7003, 7090, 7170, 7244, 7313, 7377, 7438, 7495, 7549, 7600
  1588. };
  1589. static s32 dib8000_get_adc_power(struct dvb_frontend *fe, u8 mode)
  1590. {
  1591. struct dib8000_state *state = fe->demodulator_priv;
  1592. u32 ix = 0, tmp_val = 0, exp = 0, mant = 0;
  1593. s32 val;
  1594. val = dib8000_read32(state, 384);
  1595. if (mode) {
  1596. tmp_val = val;
  1597. while (tmp_val >>= 1)
  1598. exp++;
  1599. mant = (val * 1000 / (1<<exp));
  1600. ix = (u8)((mant-1000)/100); /* index of the LUT */
  1601. val = (lut_1000ln_mant[ix] + 693*(exp-20) - 6908);
  1602. val = (val*256)/1000;
  1603. }
  1604. return val;
  1605. }
  1606. static int dib8090p_get_dc_power(struct dvb_frontend *fe, u8 IQ)
  1607. {
  1608. struct dib8000_state *state = fe->demodulator_priv;
  1609. int val = 0;
  1610. switch (IQ) {
  1611. case 1:
  1612. val = dib8000_read_word(state, 403);
  1613. break;
  1614. case 0:
  1615. val = dib8000_read_word(state, 404);
  1616. break;
  1617. }
  1618. if (val & 0x200)
  1619. val -= 1024;
  1620. return val;
  1621. }
  1622. static void dib8000_update_timf(struct dib8000_state *state)
  1623. {
  1624. u32 timf = state->timf = dib8000_read32(state, 435);
  1625. dib8000_write_word(state, 29, (u16) (timf >> 16));
  1626. dib8000_write_word(state, 30, (u16) (timf & 0xffff));
  1627. dprintk("Updated timing frequency: %d (default: %d)", state->timf, state->timf_default);
  1628. }
  1629. static u32 dib8000_ctrl_timf(struct dvb_frontend *fe, uint8_t op, uint32_t timf)
  1630. {
  1631. struct dib8000_state *state = fe->demodulator_priv;
  1632. switch (op) {
  1633. case DEMOD_TIMF_SET:
  1634. state->timf = timf;
  1635. break;
  1636. case DEMOD_TIMF_UPDATE:
  1637. dib8000_update_timf(state);
  1638. break;
  1639. case DEMOD_TIMF_GET:
  1640. break;
  1641. }
  1642. dib8000_set_bandwidth(state->fe[0], 6000);
  1643. return state->timf;
  1644. }
  1645. static const u16 adc_target_16dB[11] = {
  1646. 7250, 7238, 7264, 7309, 7338, 7382, 7427, 7456, 7500, 7544, 7574
  1647. };
  1648. static const u8 permu_seg[] = { 6, 5, 7, 4, 8, 3, 9, 2, 10, 1, 11, 0, 12 };
  1649. static u16 dib8000_set_layer(struct dib8000_state *state, u8 layer_index, u16 max_constellation)
  1650. {
  1651. u8 cr, constellation, time_intlv;
  1652. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  1653. switch (c->layer[layer_index].modulation) {
  1654. case DQPSK:
  1655. constellation = 0;
  1656. break;
  1657. case QPSK:
  1658. constellation = 1;
  1659. break;
  1660. case QAM_16:
  1661. constellation = 2;
  1662. break;
  1663. case QAM_64:
  1664. default:
  1665. constellation = 3;
  1666. break;
  1667. }
  1668. switch (c->layer[layer_index].fec) {
  1669. case FEC_1_2:
  1670. cr = 1;
  1671. break;
  1672. case FEC_2_3:
  1673. cr = 2;
  1674. break;
  1675. case FEC_3_4:
  1676. cr = 3;
  1677. break;
  1678. case FEC_5_6:
  1679. cr = 5;
  1680. break;
  1681. case FEC_7_8:
  1682. default:
  1683. cr = 7;
  1684. break;
  1685. }
  1686. time_intlv = fls(c->layer[layer_index].interleaving);
  1687. if (time_intlv > 3 && !(time_intlv == 4 && c->isdbt_sb_mode == 1))
  1688. time_intlv = 0;
  1689. dib8000_write_word(state, 2 + layer_index, (constellation << 10) | ((c->layer[layer_index].segment_count & 0xf) << 6) | (cr << 3) | time_intlv);
  1690. if (c->layer[layer_index].segment_count > 0) {
  1691. switch (max_constellation) {
  1692. case DQPSK:
  1693. case QPSK:
  1694. if (c->layer[layer_index].modulation == QAM_16 || c->layer[layer_index].modulation == QAM_64)
  1695. max_constellation = c->layer[layer_index].modulation;
  1696. break;
  1697. case QAM_16:
  1698. if (c->layer[layer_index].modulation == QAM_64)
  1699. max_constellation = c->layer[layer_index].modulation;
  1700. break;
  1701. }
  1702. }
  1703. return max_constellation;
  1704. }
  1705. static const u16 adp_Q64[4] = {0x0148, 0xfff0, 0x00a4, 0xfff8}; /* P_adp_regul_cnt 0.04, P_adp_noise_cnt -0.002, P_adp_regul_ext 0.02, P_adp_noise_ext -0.001 */
  1706. static const u16 adp_Q16[4] = {0x023d, 0xffdf, 0x00a4, 0xfff0}; /* P_adp_regul_cnt 0.07, P_adp_noise_cnt -0.004, P_adp_regul_ext 0.02, P_adp_noise_ext -0.002 */
  1707. static const u16 adp_Qdefault[4] = {0x099a, 0xffae, 0x0333, 0xfff8}; /* P_adp_regul_cnt 0.3, P_adp_noise_cnt -0.01, P_adp_regul_ext 0.1, P_adp_noise_ext -0.002 */
  1708. static u16 dib8000_adp_fine_tune(struct dib8000_state *state, u16 max_constellation)
  1709. {
  1710. u16 i, ana_gain = 0;
  1711. const u16 *adp;
  1712. /* channel estimation fine configuration */
  1713. switch (max_constellation) {
  1714. case QAM_64:
  1715. ana_gain = 0x7;
  1716. adp = &adp_Q64[0];
  1717. break;
  1718. case QAM_16:
  1719. ana_gain = 0x7;
  1720. adp = &adp_Q16[0];
  1721. break;
  1722. default:
  1723. ana_gain = 0;
  1724. adp = &adp_Qdefault[0];
  1725. break;
  1726. }
  1727. for (i = 0; i < 4; i++)
  1728. dib8000_write_word(state, 215 + i, adp[i]);
  1729. return ana_gain;
  1730. }
  1731. static void dib8000_update_ana_gain(struct dib8000_state *state, u16 ana_gain)
  1732. {
  1733. u16 i;
  1734. dib8000_write_word(state, 116, ana_gain);
  1735. /* update ADC target depending on ana_gain */
  1736. if (ana_gain) { /* set -16dB ADC target for ana_gain=-1 */
  1737. for (i = 0; i < 10; i++)
  1738. dib8000_write_word(state, 80 + i, adc_target_16dB[i]);
  1739. } else { /* set -22dB ADC target for ana_gain=0 */
  1740. for (i = 0; i < 10; i++)
  1741. dib8000_write_word(state, 80 + i, adc_target_16dB[i] - 355);
  1742. }
  1743. }
  1744. static void dib8000_load_ana_fe_coefs(struct dib8000_state *state, const s16 *ana_fe)
  1745. {
  1746. u16 mode = 0;
  1747. if (state->isdbt_cfg_loaded == 0)
  1748. for (mode = 0; mode < 24; mode++)
  1749. dib8000_write_word(state, 117 + mode, ana_fe[mode]);
  1750. }
  1751. static const u16 lut_prbs_2k[14] = {
  1752. 0, 0x423, 0x009, 0x5C7, 0x7A6, 0x3D8, 0x527, 0x7FF, 0x79B, 0x3D6, 0x3A2, 0x53B, 0x2F4, 0x213
  1753. };
  1754. static const u16 lut_prbs_4k[14] = {
  1755. 0, 0x208, 0x0C3, 0x7B9, 0x423, 0x5C7, 0x3D8, 0x7FF, 0x3D6, 0x53B, 0x213, 0x029, 0x0D0, 0x48E
  1756. };
  1757. static const u16 lut_prbs_8k[14] = {
  1758. 0, 0x740, 0x069, 0x7DD, 0x208, 0x7B9, 0x5C7, 0x7FF, 0x53B, 0x029, 0x48E, 0x4C4, 0x367, 0x684
  1759. };
  1760. static u16 dib8000_get_init_prbs(struct dib8000_state *state, u16 subchannel)
  1761. {
  1762. int sub_channel_prbs_group = 0;
  1763. sub_channel_prbs_group = (subchannel / 3) + 1;
  1764. dprintk("sub_channel_prbs_group = %d , subchannel =%d prbs = 0x%04x", sub_channel_prbs_group, subchannel, lut_prbs_8k[sub_channel_prbs_group]);
  1765. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1766. case TRANSMISSION_MODE_2K:
  1767. return lut_prbs_2k[sub_channel_prbs_group];
  1768. case TRANSMISSION_MODE_4K:
  1769. return lut_prbs_4k[sub_channel_prbs_group];
  1770. default:
  1771. case TRANSMISSION_MODE_8K:
  1772. return lut_prbs_8k[sub_channel_prbs_group];
  1773. }
  1774. }
  1775. static void dib8000_set_13seg_channel(struct dib8000_state *state)
  1776. {
  1777. u16 i;
  1778. u16 coff_pow = 0x2800;
  1779. state->seg_mask = 0x1fff; /* All 13 segments enabled */
  1780. /* ---- COFF ---- Carloff, the most robust --- */
  1781. if (state->isdbt_cfg_loaded == 0) { /* if not Sound Broadcasting mode : put default values for 13 segments */
  1782. dib8000_write_word(state, 180, (16 << 6) | 9);
  1783. dib8000_write_word(state, 187, (4 << 12) | (8 << 5) | 0x2);
  1784. coff_pow = 0x2800;
  1785. for (i = 0; i < 6; i++)
  1786. dib8000_write_word(state, 181+i, coff_pow);
  1787. /* P_ctrl_corm_thres4pre_freq_inh=1, P_ctrl_pre_freq_mode_sat=1 */
  1788. /* P_ctrl_pre_freq_mode_sat=1, P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 3, P_pre_freq_win_len=1 */
  1789. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (3 << 5) | 1);
  1790. /* P_ctrl_pre_freq_win_len=8, P_ctrl_pre_freq_thres_lockin=6 */
  1791. dib8000_write_word(state, 340, (8 << 6) | (6 << 0));
  1792. /* P_ctrl_pre_freq_thres_lockout=4, P_small_use_tmcc/ac/cp=1 */
  1793. dib8000_write_word(state, 341, (4 << 3) | (1 << 2) | (1 << 1) | (1 << 0));
  1794. dib8000_write_word(state, 228, 0); /* default value */
  1795. dib8000_write_word(state, 265, 31); /* default value */
  1796. dib8000_write_word(state, 205, 0x200f); /* init value */
  1797. }
  1798. /*
  1799. * make the cpil_coff_lock more robust but slower p_coff_winlen
  1800. * 6bits; p_coff_thres_lock 6bits (for coff lock if needed)
  1801. */
  1802. if (state->cfg.pll->ifreq == 0)
  1803. dib8000_write_word(state, 266, ~state->seg_mask | state->seg_diff_mask | 0x40); /* P_equal_noise_seg_inh */
  1804. dib8000_load_ana_fe_coefs(state, ana_fe_coeff_13seg);
  1805. }
  1806. static void dib8000_set_subchannel_prbs(struct dib8000_state *state, u16 init_prbs)
  1807. {
  1808. u16 reg_1;
  1809. reg_1 = dib8000_read_word(state, 1);
  1810. dib8000_write_word(state, 1, (init_prbs << 2) | (reg_1 & 0x3)); /* ADDR 1 */
  1811. }
  1812. static void dib8000_small_fine_tune(struct dib8000_state *state)
  1813. {
  1814. u16 i;
  1815. const s16 *ncoeff;
  1816. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  1817. dib8000_write_word(state, 352, state->seg_diff_mask);
  1818. dib8000_write_word(state, 353, state->seg_mask);
  1819. /* P_small_coef_ext_enable=ISDB-Tsb, P_small_narrow_band=ISDB-Tsb, P_small_last_seg=13, P_small_offset_num_car=5 */
  1820. dib8000_write_word(state, 351, (c->isdbt_sb_mode << 9) | (c->isdbt_sb_mode << 8) | (13 << 4) | 5);
  1821. if (c->isdbt_sb_mode) {
  1822. /* ---- SMALL ---- */
  1823. switch (c->transmission_mode) {
  1824. case TRANSMISSION_MODE_2K:
  1825. if (c->isdbt_partial_reception == 0) { /* 1-seg */
  1826. if (c->layer[0].modulation == DQPSK) /* DQPSK */
  1827. ncoeff = coeff_2k_sb_1seg_dqpsk;
  1828. else /* QPSK or QAM */
  1829. ncoeff = coeff_2k_sb_1seg;
  1830. } else { /* 3-segments */
  1831. if (c->layer[0].modulation == DQPSK) { /* DQPSK on central segment */
  1832. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1833. ncoeff = coeff_2k_sb_3seg_0dqpsk_1dqpsk;
  1834. else /* QPSK or QAM on external segments */
  1835. ncoeff = coeff_2k_sb_3seg_0dqpsk;
  1836. } else { /* QPSK or QAM on central segment */
  1837. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1838. ncoeff = coeff_2k_sb_3seg_1dqpsk;
  1839. else /* QPSK or QAM on external segments */
  1840. ncoeff = coeff_2k_sb_3seg;
  1841. }
  1842. }
  1843. break;
  1844. case TRANSMISSION_MODE_4K:
  1845. if (c->isdbt_partial_reception == 0) { /* 1-seg */
  1846. if (c->layer[0].modulation == DQPSK) /* DQPSK */
  1847. ncoeff = coeff_4k_sb_1seg_dqpsk;
  1848. else /* QPSK or QAM */
  1849. ncoeff = coeff_4k_sb_1seg;
  1850. } else { /* 3-segments */
  1851. if (c->layer[0].modulation == DQPSK) { /* DQPSK on central segment */
  1852. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1853. ncoeff = coeff_4k_sb_3seg_0dqpsk_1dqpsk;
  1854. else /* QPSK or QAM on external segments */
  1855. ncoeff = coeff_4k_sb_3seg_0dqpsk;
  1856. } else { /* QPSK or QAM on central segment */
  1857. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1858. ncoeff = coeff_4k_sb_3seg_1dqpsk;
  1859. else /* QPSK or QAM on external segments */
  1860. ncoeff = coeff_4k_sb_3seg;
  1861. }
  1862. }
  1863. break;
  1864. case TRANSMISSION_MODE_AUTO:
  1865. case TRANSMISSION_MODE_8K:
  1866. default:
  1867. if (c->isdbt_partial_reception == 0) { /* 1-seg */
  1868. if (c->layer[0].modulation == DQPSK) /* DQPSK */
  1869. ncoeff = coeff_8k_sb_1seg_dqpsk;
  1870. else /* QPSK or QAM */
  1871. ncoeff = coeff_8k_sb_1seg;
  1872. } else { /* 3-segments */
  1873. if (c->layer[0].modulation == DQPSK) { /* DQPSK on central segment */
  1874. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1875. ncoeff = coeff_8k_sb_3seg_0dqpsk_1dqpsk;
  1876. else /* QPSK or QAM on external segments */
  1877. ncoeff = coeff_8k_sb_3seg_0dqpsk;
  1878. } else { /* QPSK or QAM on central segment */
  1879. if (c->layer[1].modulation == DQPSK) /* DQPSK on external segments */
  1880. ncoeff = coeff_8k_sb_3seg_1dqpsk;
  1881. else /* QPSK or QAM on external segments */
  1882. ncoeff = coeff_8k_sb_3seg;
  1883. }
  1884. }
  1885. break;
  1886. }
  1887. for (i = 0; i < 8; i++)
  1888. dib8000_write_word(state, 343 + i, ncoeff[i]);
  1889. }
  1890. }
  1891. static const u16 coff_thres_1seg[3] = {300, 150, 80};
  1892. static const u16 coff_thres_3seg[3] = {350, 300, 250};
  1893. static void dib8000_set_sb_channel(struct dib8000_state *state)
  1894. {
  1895. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  1896. const u16 *coff;
  1897. u16 i;
  1898. if (c->transmission_mode == TRANSMISSION_MODE_2K || c->transmission_mode == TRANSMISSION_MODE_4K) {
  1899. dib8000_write_word(state, 219, dib8000_read_word(state, 219) | 0x1); /* adp_pass =1 */
  1900. dib8000_write_word(state, 190, dib8000_read_word(state, 190) | (0x1 << 14)); /* pha3_force_pha_shift = 1 */
  1901. } else {
  1902. dib8000_write_word(state, 219, dib8000_read_word(state, 219) & 0xfffe); /* adp_pass =0 */
  1903. dib8000_write_word(state, 190, dib8000_read_word(state, 190) & 0xbfff); /* pha3_force_pha_shift = 0 */
  1904. }
  1905. if (c->isdbt_partial_reception == 1) /* 3-segments */
  1906. state->seg_mask = 0x00E0;
  1907. else /* 1-segment */
  1908. state->seg_mask = 0x0040;
  1909. dib8000_write_word(state, 268, (dib8000_read_word(state, 268) & 0xF9FF) | 0x0200);
  1910. /* ---- COFF ---- Carloff, the most robust --- */
  1911. /* P_coff_cpil_alpha=4, P_coff_inh=0, P_coff_cpil_winlen=64, P_coff_narrow_band=1, P_coff_square_val=1, P_coff_one_seg=~partial_rcpt, P_coff_use_tmcc=1, P_coff_use_ac=1 */
  1912. dib8000_write_word(state, 187, (4 << 12) | (0 << 11) | (63 << 5) | (0x3 << 3) | ((~c->isdbt_partial_reception & 1) << 2) | 0x3);
  1913. dib8000_write_word(state, 340, (16 << 6) | (8 << 0)); /* P_ctrl_pre_freq_win_len=16, P_ctrl_pre_freq_thres_lockin=8 */
  1914. dib8000_write_word(state, 341, (6 << 3) | (1 << 2) | (1 << 1) | (1 << 0));/* P_ctrl_pre_freq_thres_lockout=6, P_small_use_tmcc/ac/cp=1 */
  1915. /* Sound Broadcasting mode 1 seg */
  1916. if (c->isdbt_partial_reception == 0) {
  1917. /* P_coff_winlen=63, P_coff_thres_lock=15, P_coff_one_seg_width = (P_mode == 3) , P_coff_one_seg_sym = (P_mode-1) */
  1918. if (state->mode == 3)
  1919. dib8000_write_word(state, 180, 0x1fcf | ((state->mode - 1) << 14));
  1920. else
  1921. dib8000_write_word(state, 180, 0x0fcf | ((state->mode - 1) << 14));
  1922. /* P_ctrl_corm_thres4pre_freq_inh=1,P_ctrl_pre_freq_mode_sat=1, P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 5, P_pre_freq_win_len=4 */
  1923. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (5 << 5) | 4);
  1924. coff = &coff_thres_1seg[0];
  1925. } else { /* Sound Broadcasting mode 3 seg */
  1926. dib8000_write_word(state, 180, 0x1fcf | (1 << 14));
  1927. /* P_ctrl_corm_thres4pre_freq_inh = 1, P_ctrl_pre_freq_mode_sat=1, P_ctrl_pre_freq_inh=0, P_ctrl_pre_freq_step = 4, P_pre_freq_win_len=4 */
  1928. dib8000_write_word(state, 338, (1 << 12) | (1 << 10) | (0 << 9) | (4 << 5) | 4);
  1929. coff = &coff_thres_3seg[0];
  1930. }
  1931. dib8000_write_word(state, 228, 1); /* P_2d_mode_byp=1 */
  1932. dib8000_write_word(state, 205, dib8000_read_word(state, 205) & 0xfff0); /* P_cspu_win_cut = 0 */
  1933. if (c->isdbt_partial_reception == 0 && c->transmission_mode == TRANSMISSION_MODE_2K)
  1934. dib8000_write_word(state, 265, 15); /* P_equal_noise_sel = 15 */
  1935. /* Write COFF thres */
  1936. for (i = 0 ; i < 3; i++) {
  1937. dib8000_write_word(state, 181+i, coff[i]);
  1938. dib8000_write_word(state, 184+i, coff[i]);
  1939. }
  1940. /*
  1941. * make the cpil_coff_lock more robust but slower p_coff_winlen
  1942. * 6bits; p_coff_thres_lock 6bits (for coff lock if needed)
  1943. */
  1944. dib8000_write_word(state, 266, ~state->seg_mask | state->seg_diff_mask); /* P_equal_noise_seg_inh */
  1945. if (c->isdbt_partial_reception == 0)
  1946. dib8000_write_word(state, 178, 64); /* P_fft_powrange = 64 */
  1947. else
  1948. dib8000_write_word(state, 178, 32); /* P_fft_powrange = 32 */
  1949. }
  1950. static void dib8000_set_isdbt_common_channel(struct dib8000_state *state, u8 seq, u8 autosearching)
  1951. {
  1952. u16 p_cfr_left_edge = 0, p_cfr_right_edge = 0;
  1953. u16 tmcc_pow = 0, ana_gain = 0, tmp = 0, i = 0, nbseg_diff = 0 ;
  1954. u16 max_constellation = DQPSK;
  1955. int init_prbs;
  1956. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  1957. if (autosearching)
  1958. c->isdbt_partial_reception = 1;
  1959. /* P_mode */
  1960. dib8000_write_word(state, 10, (seq << 4));
  1961. /* init mode */
  1962. state->mode = fft_to_mode(state);
  1963. /* set guard */
  1964. tmp = dib8000_read_word(state, 1);
  1965. dib8000_write_word(state, 1, (tmp&0xfffc) | (c->guard_interval & 0x3));
  1966. dib8000_write_word(state, 274, (dib8000_read_word(state, 274) & 0xffcf) | ((c->isdbt_partial_reception & 1) << 5) | ((c->isdbt_sb_mode & 1) << 4));
  1967. /* signal optimization parameter */
  1968. if (c->isdbt_partial_reception) {
  1969. state->seg_diff_mask = (c->layer[0].modulation == DQPSK) << permu_seg[0];
  1970. for (i = 1; i < 3; i++)
  1971. nbseg_diff += (c->layer[i].modulation == DQPSK) * c->layer[i].segment_count;
  1972. for (i = 0; i < nbseg_diff; i++)
  1973. state->seg_diff_mask |= 1 << permu_seg[i+1];
  1974. } else {
  1975. for (i = 0; i < 3; i++)
  1976. nbseg_diff += (c->layer[i].modulation == DQPSK) * c->layer[i].segment_count;
  1977. for (i = 0; i < nbseg_diff; i++)
  1978. state->seg_diff_mask |= 1 << permu_seg[i];
  1979. }
  1980. if (state->seg_diff_mask)
  1981. dib8000_write_word(state, 268, (dib8000_read_word(state, 268) & 0xF9FF) | 0x0200);
  1982. else
  1983. dib8000_write_word(state, 268, (2 << 9) | 39); /*init value */
  1984. for (i = 0; i < 3; i++)
  1985. max_constellation = dib8000_set_layer(state, i, max_constellation);
  1986. if (autosearching == 0) {
  1987. state->layer_b_nb_seg = c->layer[1].segment_count;
  1988. state->layer_c_nb_seg = c->layer[2].segment_count;
  1989. }
  1990. /* WRITE: Mode & Diff mask */
  1991. dib8000_write_word(state, 0, (state->mode << 13) | state->seg_diff_mask);
  1992. state->differential_constellation = (state->seg_diff_mask != 0);
  1993. /* channel estimation fine configuration */
  1994. ana_gain = dib8000_adp_fine_tune(state, max_constellation);
  1995. /* update ana_gain depending on max constellation */
  1996. dib8000_update_ana_gain(state, ana_gain);
  1997. /* ---- ANA_FE ---- */
  1998. if (c->isdbt_partial_reception) /* 3-segments */
  1999. dib8000_load_ana_fe_coefs(state, ana_fe_coeff_3seg);
  2000. else
  2001. dib8000_load_ana_fe_coefs(state, ana_fe_coeff_1seg); /* 1-segment */
  2002. /* TSB or ISDBT ? apply it now */
  2003. if (c->isdbt_sb_mode) {
  2004. dib8000_set_sb_channel(state);
  2005. if (c->isdbt_sb_subchannel < 14)
  2006. init_prbs = dib8000_get_init_prbs(state, c->isdbt_sb_subchannel);
  2007. else
  2008. init_prbs = 0;
  2009. } else {
  2010. dib8000_set_13seg_channel(state);
  2011. init_prbs = 0xfff;
  2012. }
  2013. /* SMALL */
  2014. dib8000_small_fine_tune(state);
  2015. dib8000_set_subchannel_prbs(state, init_prbs);
  2016. /* ---- CHAN_BLK ---- */
  2017. for (i = 0; i < 13; i++) {
  2018. if ((((~state->seg_diff_mask) >> i) & 1) == 1) {
  2019. p_cfr_left_edge += (1 << i) * ((i == 0) || ((((state->seg_mask & (~state->seg_diff_mask)) >> (i - 1)) & 1) == 0));
  2020. p_cfr_right_edge += (1 << i) * ((i == 12) || ((((state->seg_mask & (~state->seg_diff_mask)) >> (i + 1)) & 1) == 0));
  2021. }
  2022. }
  2023. dib8000_write_word(state, 222, p_cfr_left_edge); /* p_cfr_left_edge */
  2024. dib8000_write_word(state, 223, p_cfr_right_edge); /* p_cfr_right_edge */
  2025. /* "P_cspu_left_edge" & "P_cspu_right_edge" not used => do not care */
  2026. dib8000_write_word(state, 189, ~state->seg_mask | state->seg_diff_mask); /* P_lmod4_seg_inh */
  2027. dib8000_write_word(state, 192, ~state->seg_mask | state->seg_diff_mask); /* P_pha3_seg_inh */
  2028. dib8000_write_word(state, 225, ~state->seg_mask | state->seg_diff_mask); /* P_tac_seg_inh */
  2029. if (!autosearching)
  2030. dib8000_write_word(state, 288, (~state->seg_mask | state->seg_diff_mask) & 0x1fff); /* P_tmcc_seg_eq_inh */
  2031. else
  2032. dib8000_write_word(state, 288, 0x1fff); /*disable equalisation of the tmcc when autosearch to be able to find the DQPSK channels. */
  2033. dib8000_write_word(state, 211, state->seg_mask & (~state->seg_diff_mask)); /* P_des_seg_enabled */
  2034. dib8000_write_word(state, 287, ~state->seg_mask | 0x1000); /* P_tmcc_seg_inh */
  2035. dib8000_write_word(state, 178, 32); /* P_fft_powrange = 32 */
  2036. /* ---- TMCC ---- */
  2037. for (i = 0; i < 3; i++)
  2038. tmcc_pow += (((c->layer[i].modulation == DQPSK) * 4 + 1) * c->layer[i].segment_count) ;
  2039. /* Quantif of "P_tmcc_dec_thres_?k" is (0, 5+mode, 9); */
  2040. /* Threshold is set at 1/4 of max power. */
  2041. tmcc_pow *= (1 << (9-2));
  2042. dib8000_write_word(state, 290, tmcc_pow); /* P_tmcc_dec_thres_2k */
  2043. dib8000_write_word(state, 291, tmcc_pow); /* P_tmcc_dec_thres_4k */
  2044. dib8000_write_word(state, 292, tmcc_pow); /* P_tmcc_dec_thres_8k */
  2045. /*dib8000_write_word(state, 287, (1 << 13) | 0x1000 ); */
  2046. /* ---- PHA3 ---- */
  2047. if (state->isdbt_cfg_loaded == 0)
  2048. dib8000_write_word(state, 250, 3285); /* p_2d_hspeed_thr0 */
  2049. state->isdbt_cfg_loaded = 0;
  2050. }
  2051. static u32 dib8000_wait_lock(struct dib8000_state *state, u32 internal,
  2052. u32 wait0_ms, u32 wait1_ms, u32 wait2_ms)
  2053. {
  2054. u32 value = 0; /* P_search_end0 wait time */
  2055. u16 reg = 11; /* P_search_end0 start addr */
  2056. for (reg = 11; reg < 16; reg += 2) {
  2057. if (reg == 11) {
  2058. if (state->revision == 0x8090)
  2059. value = internal * wait1_ms;
  2060. else
  2061. value = internal * wait0_ms;
  2062. } else if (reg == 13)
  2063. value = internal * wait1_ms;
  2064. else if (reg == 15)
  2065. value = internal * wait2_ms;
  2066. dib8000_write_word(state, reg, (u16)((value >> 16) & 0xffff));
  2067. dib8000_write_word(state, (reg + 1), (u16)(value & 0xffff));
  2068. }
  2069. return value;
  2070. }
  2071. static int dib8000_autosearch_start(struct dvb_frontend *fe)
  2072. {
  2073. struct dib8000_state *state = fe->demodulator_priv;
  2074. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2075. u8 slist = 0;
  2076. u32 value, internal = state->cfg.pll->internal;
  2077. if (state->revision == 0x8090)
  2078. internal = dib8000_read32(state, 23) / 1000;
  2079. if ((state->revision >= 0x8002) &&
  2080. (state->autosearch_state == AS_SEARCHING_FFT)) {
  2081. dib8000_write_word(state, 37, 0x0065); /* P_ctrl_pha_off_max default values */
  2082. dib8000_write_word(state, 116, 0x0000); /* P_ana_gain to 0 */
  2083. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x1fff) | (0 << 13) | (1 << 15)); /* P_mode = 0, P_restart_search=1 */
  2084. dib8000_write_word(state, 1, (dib8000_read_word(state, 1) & 0xfffc) | 0); /* P_guard = 0 */
  2085. dib8000_write_word(state, 6, 0); /* P_lock0_mask = 0 */
  2086. dib8000_write_word(state, 7, 0); /* P_lock1_mask = 0 */
  2087. dib8000_write_word(state, 8, 0); /* P_lock2_mask = 0 */
  2088. dib8000_write_word(state, 10, (dib8000_read_word(state, 10) & 0x200) | (16 << 4) | (0 << 0)); /* P_search_list=16, P_search_maxtrial=0 */
  2089. if (state->revision == 0x8090)
  2090. value = dib8000_wait_lock(state, internal, 10, 10, 10); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2091. else
  2092. value = dib8000_wait_lock(state, internal, 20, 20, 20); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2093. dib8000_write_word(state, 17, 0);
  2094. dib8000_write_word(state, 18, 200); /* P_search_rstst = 200 */
  2095. dib8000_write_word(state, 19, 0);
  2096. dib8000_write_word(state, 20, 400); /* P_search_rstend = 400 */
  2097. dib8000_write_word(state, 21, (value >> 16) & 0xffff); /* P_search_checkst */
  2098. dib8000_write_word(state, 22, value & 0xffff);
  2099. if (state->revision == 0x8090)
  2100. dib8000_write_word(state, 32, (dib8000_read_word(state, 32) & 0xf0ff) | (0 << 8)); /* P_corm_alpha = 0 */
  2101. else
  2102. dib8000_write_word(state, 32, (dib8000_read_word(state, 32) & 0xf0ff) | (9 << 8)); /* P_corm_alpha = 3 */
  2103. dib8000_write_word(state, 355, 2); /* P_search_param_max = 2 */
  2104. /* P_search_param_select = (1 | 1<<4 | 1 << 8) */
  2105. dib8000_write_word(state, 356, 0);
  2106. dib8000_write_word(state, 357, 0x111);
  2107. dib8000_write_word(state, 770, (dib8000_read_word(state, 770) & 0xdfff) | (1 << 13)); /* P_restart_ccg = 1 */
  2108. dib8000_write_word(state, 770, (dib8000_read_word(state, 770) & 0xdfff) | (0 << 13)); /* P_restart_ccg = 0 */
  2109. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x7ff) | (0 << 15) | (1 << 13)); /* P_restart_search = 0; */
  2110. } else if ((state->revision >= 0x8002) &&
  2111. (state->autosearch_state == AS_SEARCHING_GUARD)) {
  2112. c->transmission_mode = TRANSMISSION_MODE_8K;
  2113. c->guard_interval = GUARD_INTERVAL_1_8;
  2114. c->inversion = 0;
  2115. c->layer[0].modulation = QAM_64;
  2116. c->layer[0].fec = FEC_2_3;
  2117. c->layer[0].interleaving = 0;
  2118. c->layer[0].segment_count = 13;
  2119. slist = 16;
  2120. c->transmission_mode = state->found_nfft;
  2121. dib8000_set_isdbt_common_channel(state, slist, 1);
  2122. /* set lock_mask values */
  2123. dib8000_write_word(state, 6, 0x4);
  2124. if (state->revision == 0x8090)
  2125. dib8000_write_word(state, 7, ((1 << 12) | (1 << 11) | (1 << 10)));/* tmcc_dec_lock, tmcc_sync_lock, tmcc_data_lock, tmcc_bch_uncor */
  2126. else
  2127. dib8000_write_word(state, 7, 0x8);
  2128. dib8000_write_word(state, 8, 0x1000);
  2129. /* set lock_mask wait time values */
  2130. if (state->revision == 0x8090)
  2131. dib8000_wait_lock(state, internal, 50, 100, 1000); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2132. else
  2133. dib8000_wait_lock(state, internal, 50, 200, 1000); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2134. dib8000_write_word(state, 355, 3); /* P_search_param_max = 3 */
  2135. /* P_search_param_select = 0xf; look for the 4 different guard intervals */
  2136. dib8000_write_word(state, 356, 0);
  2137. dib8000_write_word(state, 357, 0xf);
  2138. value = dib8000_read_word(state, 0);
  2139. dib8000_write_word(state, 0, (u16)((1 << 15) | value));
  2140. dib8000_read_word(state, 1284); /* reset the INT. n_irq_pending */
  2141. dib8000_write_word(state, 0, (u16)value);
  2142. } else {
  2143. c->inversion = 0;
  2144. c->layer[0].modulation = QAM_64;
  2145. c->layer[0].fec = FEC_2_3;
  2146. c->layer[0].interleaving = 0;
  2147. c->layer[0].segment_count = 13;
  2148. if (!c->isdbt_sb_mode)
  2149. c->layer[0].segment_count = 13;
  2150. /* choose the right list, in sb, always do everything */
  2151. if (c->isdbt_sb_mode) {
  2152. slist = 7;
  2153. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13));
  2154. } else {
  2155. if (c->guard_interval == GUARD_INTERVAL_AUTO) {
  2156. if (c->transmission_mode == TRANSMISSION_MODE_AUTO) {
  2157. c->transmission_mode = TRANSMISSION_MODE_8K;
  2158. c->guard_interval = GUARD_INTERVAL_1_8;
  2159. slist = 7;
  2160. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); /* P_mode = 1 to have autosearch start ok with mode2 */
  2161. } else {
  2162. c->guard_interval = GUARD_INTERVAL_1_8;
  2163. slist = 3;
  2164. }
  2165. } else {
  2166. if (c->transmission_mode == TRANSMISSION_MODE_AUTO) {
  2167. c->transmission_mode = TRANSMISSION_MODE_8K;
  2168. slist = 2;
  2169. dib8000_write_word(state, 0, (dib8000_read_word(state, 0) & 0x9fff) | (1 << 13)); /* P_mode = 1 */
  2170. } else
  2171. slist = 0;
  2172. }
  2173. }
  2174. dprintk("Using list for autosearch : %d", slist);
  2175. dib8000_set_isdbt_common_channel(state, slist, 1);
  2176. /* set lock_mask values */
  2177. dib8000_write_word(state, 6, 0x4);
  2178. if (state->revision == 0x8090)
  2179. dib8000_write_word(state, 7, (1 << 12) | (1 << 11) | (1 << 10));
  2180. else
  2181. dib8000_write_word(state, 7, 0x8);
  2182. dib8000_write_word(state, 8, 0x1000);
  2183. /* set lock_mask wait time values */
  2184. if (state->revision == 0x8090)
  2185. dib8000_wait_lock(state, internal, 50, 200, 1000); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2186. else
  2187. dib8000_wait_lock(state, internal, 50, 100, 1000); /* time in ms configure P_search_end0 P_search_end1 P_search_end2 */
  2188. value = dib8000_read_word(state, 0);
  2189. dib8000_write_word(state, 0, (u16)((1 << 15) | value));
  2190. dib8000_read_word(state, 1284); /* reset the INT. n_irq_pending */
  2191. dib8000_write_word(state, 0, (u16)value);
  2192. }
  2193. return 0;
  2194. }
  2195. static int dib8000_autosearch_irq(struct dvb_frontend *fe)
  2196. {
  2197. struct dib8000_state *state = fe->demodulator_priv;
  2198. u16 irq_pending = dib8000_read_word(state, 1284);
  2199. if ((state->revision >= 0x8002) &&
  2200. (state->autosearch_state == AS_SEARCHING_FFT)) {
  2201. if (irq_pending & 0x1) {
  2202. dprintk("dib8000_autosearch_irq: max correlation result available");
  2203. return 3;
  2204. }
  2205. } else {
  2206. if (irq_pending & 0x1) { /* failed */
  2207. dprintk("dib8000_autosearch_irq failed");
  2208. return 1;
  2209. }
  2210. if (irq_pending & 0x2) { /* succeeded */
  2211. dprintk("dib8000_autosearch_irq succeeded");
  2212. return 2;
  2213. }
  2214. }
  2215. return 0; // still pending
  2216. }
  2217. static void dib8000_viterbi_state(struct dib8000_state *state, u8 onoff)
  2218. {
  2219. u16 tmp;
  2220. tmp = dib8000_read_word(state, 771);
  2221. if (onoff) /* start P_restart_chd : channel_decoder */
  2222. dib8000_write_word(state, 771, tmp & 0xfffd);
  2223. else /* stop P_restart_chd : channel_decoder */
  2224. dib8000_write_word(state, 771, tmp | (1<<1));
  2225. }
  2226. static void dib8000_set_dds(struct dib8000_state *state, s32 offset_khz)
  2227. {
  2228. s16 unit_khz_dds_val;
  2229. u32 abs_offset_khz = ABS(offset_khz);
  2230. u32 dds = state->cfg.pll->ifreq & 0x1ffffff;
  2231. u8 invert = !!(state->cfg.pll->ifreq & (1 << 25));
  2232. u8 ratio;
  2233. if (state->revision == 0x8090) {
  2234. ratio = 4;
  2235. unit_khz_dds_val = (1<<26) / (dib8000_read32(state, 23) / 1000);
  2236. if (offset_khz < 0)
  2237. dds = (1 << 26) - (abs_offset_khz * unit_khz_dds_val);
  2238. else
  2239. dds = (abs_offset_khz * unit_khz_dds_val);
  2240. if (invert)
  2241. dds = (1<<26) - dds;
  2242. } else {
  2243. ratio = 2;
  2244. unit_khz_dds_val = (u16) (67108864 / state->cfg.pll->internal);
  2245. if (offset_khz < 0)
  2246. unit_khz_dds_val *= -1;
  2247. /* IF tuner */
  2248. if (invert)
  2249. dds -= abs_offset_khz * unit_khz_dds_val;
  2250. else
  2251. dds += abs_offset_khz * unit_khz_dds_val;
  2252. }
  2253. dprintk("setting a DDS frequency offset of %c%dkHz", invert ? '-' : ' ', dds / unit_khz_dds_val);
  2254. if (abs_offset_khz <= (state->cfg.pll->internal / ratio)) {
  2255. /* Max dds offset is the half of the demod freq */
  2256. dib8000_write_word(state, 26, invert);
  2257. dib8000_write_word(state, 27, (u16)(dds >> 16) & 0x1ff);
  2258. dib8000_write_word(state, 28, (u16)(dds & 0xffff));
  2259. }
  2260. }
  2261. static void dib8000_set_frequency_offset(struct dib8000_state *state)
  2262. {
  2263. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2264. int i;
  2265. u32 current_rf;
  2266. int total_dds_offset_khz;
  2267. if (state->fe[0]->ops.tuner_ops.get_frequency)
  2268. state->fe[0]->ops.tuner_ops.get_frequency(state->fe[0], &current_rf);
  2269. else
  2270. current_rf = c->frequency;
  2271. current_rf /= 1000;
  2272. total_dds_offset_khz = (int)current_rf - (int)c->frequency / 1000;
  2273. if (c->isdbt_sb_mode) {
  2274. state->subchannel = c->isdbt_sb_subchannel;
  2275. i = dib8000_read_word(state, 26) & 1; /* P_dds_invspec */
  2276. dib8000_write_word(state, 26, c->inversion ^ i);
  2277. if (state->cfg.pll->ifreq == 0) { /* low if tuner */
  2278. if ((c->inversion ^ i) == 0)
  2279. dib8000_write_word(state, 26, dib8000_read_word(state, 26) | 1);
  2280. } else {
  2281. if ((c->inversion ^ i) == 0)
  2282. total_dds_offset_khz *= -1;
  2283. }
  2284. }
  2285. dprintk("%dkhz tuner offset (frequency = %dHz & current_rf = %dHz) total_dds_offset_hz = %d", c->frequency - current_rf, c->frequency, current_rf, total_dds_offset_khz);
  2286. /* apply dds offset now */
  2287. dib8000_set_dds(state, total_dds_offset_khz);
  2288. }
  2289. static u16 LUT_isdbt_symbol_duration[4] = { 26, 101, 63 };
  2290. static u32 dib8000_get_symbol_duration(struct dib8000_state *state)
  2291. {
  2292. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2293. u16 i;
  2294. switch (c->transmission_mode) {
  2295. case TRANSMISSION_MODE_2K:
  2296. i = 0;
  2297. break;
  2298. case TRANSMISSION_MODE_4K:
  2299. i = 2;
  2300. break;
  2301. default:
  2302. case TRANSMISSION_MODE_AUTO:
  2303. case TRANSMISSION_MODE_8K:
  2304. i = 1;
  2305. break;
  2306. }
  2307. return (LUT_isdbt_symbol_duration[i] / (c->bandwidth_hz / 1000)) + 1;
  2308. }
  2309. static void dib8000_set_isdbt_loop_params(struct dib8000_state *state, enum param_loop_step loop_step)
  2310. {
  2311. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2312. u16 reg_32 = 0, reg_37 = 0;
  2313. switch (loop_step) {
  2314. case LOOP_TUNE_1:
  2315. if (c->isdbt_sb_mode) {
  2316. if (c->isdbt_partial_reception == 0) {
  2317. reg_32 = ((11 - state->mode) << 12) | (6 << 8) | 0x40; /* P_timf_alpha = (11-P_mode), P_corm_alpha=6, P_corm_thres=0x40 */
  2318. reg_37 = (3 << 5) | (0 << 4) | (10 - state->mode); /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (10-P_mode) */
  2319. } else { /* Sound Broadcasting mode 3 seg */
  2320. reg_32 = ((10 - state->mode) << 12) | (6 << 8) | 0x60; /* P_timf_alpha = (10-P_mode), P_corm_alpha=6, P_corm_thres=0x60 */
  2321. reg_37 = (3 << 5) | (0 << 4) | (9 - state->mode); /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = (9-P_mode) */
  2322. }
  2323. } else { /* 13-seg start conf offset loop parameters */
  2324. reg_32 = ((9 - state->mode) << 12) | (6 << 8) | 0x80; /* P_timf_alpha = (9-P_mode, P_corm_alpha=6, P_corm_thres=0x80 */
  2325. reg_37 = (3 << 5) | (0 << 4) | (8 - state->mode); /* P_ctrl_pha_off_max=3 P_ctrl_sfreq_inh =0 P_ctrl_sfreq_step = 9 */
  2326. }
  2327. break;
  2328. case LOOP_TUNE_2:
  2329. if (c->isdbt_sb_mode) {
  2330. if (c->isdbt_partial_reception == 0) { /* Sound Broadcasting mode 1 seg */
  2331. reg_32 = ((13-state->mode) << 12) | (6 << 8) | 0x40; /* P_timf_alpha = (13-P_mode) , P_corm_alpha=6, P_corm_thres=0x40*/
  2332. reg_37 = (12-state->mode) | ((5 + state->mode) << 5);
  2333. } else { /* Sound Broadcasting mode 3 seg */
  2334. reg_32 = ((12-state->mode) << 12) | (6 << 8) | 0x60; /* P_timf_alpha = (12-P_mode) , P_corm_alpha=6, P_corm_thres=0x60 */
  2335. reg_37 = (11-state->mode) | ((5 + state->mode) << 5);
  2336. }
  2337. } else { /* 13 seg */
  2338. reg_32 = ((11-state->mode) << 12) | (6 << 8) | 0x80; /* P_timf_alpha = 8 , P_corm_alpha=6, P_corm_thres=0x80 */
  2339. reg_37 = ((5+state->mode) << 5) | (10 - state->mode);
  2340. }
  2341. break;
  2342. }
  2343. dib8000_write_word(state, 32, reg_32);
  2344. dib8000_write_word(state, 37, reg_37);
  2345. }
  2346. static void dib8000_demod_restart(struct dib8000_state *state)
  2347. {
  2348. dib8000_write_word(state, 770, 0x4000);
  2349. dib8000_write_word(state, 770, 0x0000);
  2350. return;
  2351. }
  2352. static void dib8000_set_sync_wait(struct dib8000_state *state)
  2353. {
  2354. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2355. u16 sync_wait = 64;
  2356. /* P_dvsy_sync_wait - reuse mode */
  2357. switch (c->transmission_mode) {
  2358. case TRANSMISSION_MODE_8K:
  2359. sync_wait = 256;
  2360. break;
  2361. case TRANSMISSION_MODE_4K:
  2362. sync_wait = 128;
  2363. break;
  2364. default:
  2365. case TRANSMISSION_MODE_2K:
  2366. sync_wait = 64;
  2367. break;
  2368. }
  2369. if (state->cfg.diversity_delay == 0)
  2370. sync_wait = (sync_wait * (1 << (c->guard_interval)) * 3) / 2 + 48; /* add 50% SFN margin + compensate for one DVSY-fifo */
  2371. else
  2372. sync_wait = (sync_wait * (1 << (c->guard_interval)) * 3) / 2 + state->cfg.diversity_delay; /* add 50% SFN margin + compensate for DVSY-fifo */
  2373. dib8000_write_word(state, 273, (dib8000_read_word(state, 273) & 0x000f) | (sync_wait << 4));
  2374. }
  2375. static unsigned long dib8000_get_timeout(struct dib8000_state *state, u32 delay, enum timeout_mode mode)
  2376. {
  2377. if (mode == SYMBOL_DEPENDENT_ON)
  2378. delay *= state->symbol_duration;
  2379. return jiffies + usecs_to_jiffies(delay * 100);
  2380. }
  2381. static s32 dib8000_get_status(struct dvb_frontend *fe)
  2382. {
  2383. struct dib8000_state *state = fe->demodulator_priv;
  2384. return state->status;
  2385. }
  2386. static enum frontend_tune_state dib8000_get_tune_state(struct dvb_frontend *fe)
  2387. {
  2388. struct dib8000_state *state = fe->demodulator_priv;
  2389. return state->tune_state;
  2390. }
  2391. static int dib8000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  2392. {
  2393. struct dib8000_state *state = fe->demodulator_priv;
  2394. state->tune_state = tune_state;
  2395. return 0;
  2396. }
  2397. static int dib8000_tune_restart_from_demod(struct dvb_frontend *fe)
  2398. {
  2399. struct dib8000_state *state = fe->demodulator_priv;
  2400. state->status = FE_STATUS_TUNE_PENDING;
  2401. state->tune_state = CT_DEMOD_START;
  2402. return 0;
  2403. }
  2404. static u16 dib8000_read_lock(struct dvb_frontend *fe)
  2405. {
  2406. struct dib8000_state *state = fe->demodulator_priv;
  2407. if (state->revision == 0x8090)
  2408. return dib8000_read_word(state, 570);
  2409. return dib8000_read_word(state, 568);
  2410. }
  2411. static int dib8090p_init_sdram(struct dib8000_state *state)
  2412. {
  2413. u16 reg = 0;
  2414. dprintk("init sdram");
  2415. reg = dib8000_read_word(state, 274) & 0xfff0;
  2416. dib8000_write_word(state, 274, reg | 0x7); /* P_dintlv_delay_ram = 7 because of MobileSdram */
  2417. dib8000_write_word(state, 1803, (7 << 2));
  2418. reg = dib8000_read_word(state, 1280);
  2419. dib8000_write_word(state, 1280, reg | (1 << 2)); /* force restart P_restart_sdram */
  2420. dib8000_write_word(state, 1280, reg); /* release restart P_restart_sdram */
  2421. return 0;
  2422. }
  2423. /**
  2424. * is_manual_mode - Check if TMCC should be used for parameters settings
  2425. * @c: struct dvb_frontend_properties
  2426. *
  2427. * By default, TMCC table should be used for parameter settings on most
  2428. * usercases. However, sometimes it is desirable to lock the demod to
  2429. * use the manual parameters.
  2430. *
  2431. * On manual mode, the current dib8000_tune state machine is very restrict:
  2432. * It requires that both per-layer and per-transponder parameters to be
  2433. * properly specified, otherwise the device won't lock.
  2434. *
  2435. * Check if all those conditions are properly satisfied before allowing
  2436. * the device to use the manual frequency lock mode.
  2437. */
  2438. static int is_manual_mode(struct dtv_frontend_properties *c)
  2439. {
  2440. int i, n_segs = 0;
  2441. /* Use auto mode on DVB-T compat mode */
  2442. if (c->delivery_system != SYS_ISDBT)
  2443. return 0;
  2444. /*
  2445. * Transmission mode is only detected on auto mode, currently
  2446. */
  2447. if (c->transmission_mode == TRANSMISSION_MODE_AUTO) {
  2448. dprintk("transmission mode auto");
  2449. return 0;
  2450. }
  2451. /*
  2452. * Guard interval is only detected on auto mode, currently
  2453. */
  2454. if (c->guard_interval == GUARD_INTERVAL_AUTO) {
  2455. dprintk("guard interval auto");
  2456. return 0;
  2457. }
  2458. /*
  2459. * If no layer is enabled, assume auto mode, as at least one
  2460. * layer should be enabled
  2461. */
  2462. if (!c->isdbt_layer_enabled) {
  2463. dprintk("no layer modulation specified");
  2464. return 0;
  2465. }
  2466. /*
  2467. * Check if the per-layer parameters aren't auto and
  2468. * disable a layer if segment count is 0 or invalid.
  2469. */
  2470. for (i = 0; i < 3; i++) {
  2471. if (!(c->isdbt_layer_enabled & 1 << i))
  2472. continue;
  2473. if ((c->layer[i].segment_count > 13) ||
  2474. (c->layer[i].segment_count == 0)) {
  2475. c->isdbt_layer_enabled &= ~(1 << i);
  2476. continue;
  2477. }
  2478. n_segs += c->layer[i].segment_count;
  2479. if ((c->layer[i].modulation == QAM_AUTO) ||
  2480. (c->layer[i].fec == FEC_AUTO)) {
  2481. dprintk("layer %c has either modulation or FEC auto",
  2482. 'A' + i);
  2483. return 0;
  2484. }
  2485. }
  2486. /*
  2487. * Userspace specified a wrong number of segments.
  2488. * fallback to auto mode.
  2489. */
  2490. if (n_segs == 0 || n_segs > 13) {
  2491. dprintk("number of segments is invalid");
  2492. return 0;
  2493. }
  2494. /* Everything looks ok for manual mode */
  2495. return 1;
  2496. }
  2497. static int dib8000_tune(struct dvb_frontend *fe)
  2498. {
  2499. struct dib8000_state *state = fe->demodulator_priv;
  2500. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  2501. enum frontend_tune_state *tune_state = &state->tune_state;
  2502. u16 locks, deeper_interleaver = 0, i;
  2503. int ret = 1; /* 1 symbol duration (in 100us unit) delay most of the time */
  2504. unsigned long *timeout = &state->timeout;
  2505. unsigned long now = jiffies;
  2506. #ifdef DIB8000_AGC_FREEZE
  2507. u16 agc1, agc2;
  2508. #endif
  2509. u32 corm[4] = {0, 0, 0, 0};
  2510. u8 find_index, max_value;
  2511. #if 0
  2512. if (*tune_state < CT_DEMOD_STOP)
  2513. dprintk("IN: context status = %d, TUNE_STATE %d autosearch step = %u jiffies = %lu",
  2514. state->channel_parameters_set, *tune_state, state->autosearch_state, now);
  2515. #endif
  2516. switch (*tune_state) {
  2517. case CT_DEMOD_START: /* 30 */
  2518. dib8000_reset_stats(fe);
  2519. if (state->revision == 0x8090)
  2520. dib8090p_init_sdram(state);
  2521. state->status = FE_STATUS_TUNE_PENDING;
  2522. state->channel_parameters_set = is_manual_mode(c);
  2523. dprintk("Tuning channel on %s search mode",
  2524. state->channel_parameters_set ? "manual" : "auto");
  2525. dib8000_viterbi_state(state, 0); /* force chan dec in restart */
  2526. /* Layer monitor */
  2527. dib8000_write_word(state, 285, dib8000_read_word(state, 285) & 0x60);
  2528. dib8000_set_frequency_offset(state);
  2529. dib8000_set_bandwidth(fe, c->bandwidth_hz / 1000);
  2530. if (state->channel_parameters_set == 0) { /* The channel struct is unknown, search it ! */
  2531. #ifdef DIB8000_AGC_FREEZE
  2532. if (state->revision != 0x8090) {
  2533. state->agc1_max = dib8000_read_word(state, 108);
  2534. state->agc1_min = dib8000_read_word(state, 109);
  2535. state->agc2_max = dib8000_read_word(state, 110);
  2536. state->agc2_min = dib8000_read_word(state, 111);
  2537. agc1 = dib8000_read_word(state, 388);
  2538. agc2 = dib8000_read_word(state, 389);
  2539. dib8000_write_word(state, 108, agc1);
  2540. dib8000_write_word(state, 109, agc1);
  2541. dib8000_write_word(state, 110, agc2);
  2542. dib8000_write_word(state, 111, agc2);
  2543. }
  2544. #endif
  2545. state->autosearch_state = AS_SEARCHING_FFT;
  2546. state->found_nfft = TRANSMISSION_MODE_AUTO;
  2547. state->found_guard = GUARD_INTERVAL_AUTO;
  2548. *tune_state = CT_DEMOD_SEARCH_NEXT;
  2549. } else { /* we already know the channel struct so TUNE only ! */
  2550. state->autosearch_state = AS_DONE;
  2551. *tune_state = CT_DEMOD_STEP_3;
  2552. }
  2553. state->symbol_duration = dib8000_get_symbol_duration(state);
  2554. break;
  2555. case CT_DEMOD_SEARCH_NEXT: /* 51 */
  2556. dib8000_autosearch_start(fe);
  2557. if (state->revision == 0x8090)
  2558. ret = 50;
  2559. else
  2560. ret = 15;
  2561. *tune_state = CT_DEMOD_STEP_1;
  2562. break;
  2563. case CT_DEMOD_STEP_1: /* 31 */
  2564. switch (dib8000_autosearch_irq(fe)) {
  2565. case 1: /* fail */
  2566. state->status = FE_STATUS_TUNE_FAILED;
  2567. state->autosearch_state = AS_DONE;
  2568. *tune_state = CT_DEMOD_STOP; /* else we are done here */
  2569. break;
  2570. case 2: /* Succes */
  2571. state->status = FE_STATUS_FFT_SUCCESS; /* signal to the upper layer, that there was a channel found and the parameters can be read */
  2572. *tune_state = CT_DEMOD_STEP_3;
  2573. if (state->autosearch_state == AS_SEARCHING_GUARD)
  2574. *tune_state = CT_DEMOD_STEP_2;
  2575. else
  2576. state->autosearch_state = AS_DONE;
  2577. break;
  2578. case 3: /* Autosearch FFT max correlation endded */
  2579. *tune_state = CT_DEMOD_STEP_2;
  2580. break;
  2581. }
  2582. break;
  2583. case CT_DEMOD_STEP_2:
  2584. switch (state->autosearch_state) {
  2585. case AS_SEARCHING_FFT:
  2586. /* searching for the correct FFT */
  2587. if (state->revision == 0x8090) {
  2588. corm[2] = (dib8000_read_word(state, 596) << 16) | (dib8000_read_word(state, 597));
  2589. corm[1] = (dib8000_read_word(state, 598) << 16) | (dib8000_read_word(state, 599));
  2590. corm[0] = (dib8000_read_word(state, 600) << 16) | (dib8000_read_word(state, 601));
  2591. } else {
  2592. corm[2] = (dib8000_read_word(state, 594) << 16) | (dib8000_read_word(state, 595));
  2593. corm[1] = (dib8000_read_word(state, 596) << 16) | (dib8000_read_word(state, 597));
  2594. corm[0] = (dib8000_read_word(state, 598) << 16) | (dib8000_read_word(state, 599));
  2595. }
  2596. /* dprintk("corm fft: %u %u %u", corm[0], corm[1], corm[2]); */
  2597. max_value = 0;
  2598. for (find_index = 1 ; find_index < 3 ; find_index++) {
  2599. if (corm[max_value] < corm[find_index])
  2600. max_value = find_index ;
  2601. }
  2602. switch (max_value) {
  2603. case 0:
  2604. state->found_nfft = TRANSMISSION_MODE_2K;
  2605. break;
  2606. case 1:
  2607. state->found_nfft = TRANSMISSION_MODE_4K;
  2608. break;
  2609. case 2:
  2610. default:
  2611. state->found_nfft = TRANSMISSION_MODE_8K;
  2612. break;
  2613. }
  2614. /* dprintk("Autosearch FFT has found Mode %d", max_value + 1); */
  2615. *tune_state = CT_DEMOD_SEARCH_NEXT;
  2616. state->autosearch_state = AS_SEARCHING_GUARD;
  2617. if (state->revision == 0x8090)
  2618. ret = 50;
  2619. else
  2620. ret = 10;
  2621. break;
  2622. case AS_SEARCHING_GUARD:
  2623. /* searching for the correct guard interval */
  2624. if (state->revision == 0x8090)
  2625. state->found_guard = dib8000_read_word(state, 572) & 0x3;
  2626. else
  2627. state->found_guard = dib8000_read_word(state, 570) & 0x3;
  2628. /* dprintk("guard interval found=%i", state->found_guard); */
  2629. *tune_state = CT_DEMOD_STEP_3;
  2630. break;
  2631. default:
  2632. /* the demod should never be in this state */
  2633. state->status = FE_STATUS_TUNE_FAILED;
  2634. state->autosearch_state = AS_DONE;
  2635. *tune_state = CT_DEMOD_STOP; /* else we are done here */
  2636. break;
  2637. }
  2638. break;
  2639. case CT_DEMOD_STEP_3: /* 33 */
  2640. dib8000_set_isdbt_loop_params(state, LOOP_TUNE_1);
  2641. dib8000_set_isdbt_common_channel(state, 0, 0);/* setting the known channel parameters here */
  2642. *tune_state = CT_DEMOD_STEP_4;
  2643. break;
  2644. case CT_DEMOD_STEP_4: /* (34) */
  2645. dib8000_demod_restart(state);
  2646. dib8000_set_sync_wait(state);
  2647. dib8000_set_diversity_in(state->fe[0], state->diversity_onoff);
  2648. locks = (dib8000_read_word(state, 180) >> 6) & 0x3f; /* P_coff_winlen ? */
  2649. /* coff should lock over P_coff_winlen ofdm symbols : give 3 times this length to lock */
  2650. *timeout = dib8000_get_timeout(state, 2 * locks, SYMBOL_DEPENDENT_ON);
  2651. *tune_state = CT_DEMOD_STEP_5;
  2652. break;
  2653. case CT_DEMOD_STEP_5: /* (35) */
  2654. locks = dib8000_read_lock(fe);
  2655. if (locks & (0x3 << 11)) { /* coff-lock and off_cpil_lock achieved */
  2656. dib8000_update_timf(state); /* we achieved a coff_cpil_lock - it's time to update the timf */
  2657. if (!state->differential_constellation) {
  2658. /* 2 times lmod4_win_len + 10 symbols (pipe delay after coff + nb to compute a 1st correlation) */
  2659. *timeout = dib8000_get_timeout(state, (20 * ((dib8000_read_word(state, 188)>>5)&0x1f)), SYMBOL_DEPENDENT_ON);
  2660. *tune_state = CT_DEMOD_STEP_7;
  2661. } else {
  2662. *tune_state = CT_DEMOD_STEP_8;
  2663. }
  2664. } else if (time_after(now, *timeout)) {
  2665. *tune_state = CT_DEMOD_STEP_6; /* goto check for diversity input connection */
  2666. }
  2667. break;
  2668. case CT_DEMOD_STEP_6: /* (36) if there is an input (diversity) */
  2669. if ((state->fe[1] != NULL) && (state->output_mode != OUTMODE_DIVERSITY)) {
  2670. /* if there is a diversity fe in input and this fe is has not already failled : wait here until this this fe has succedeed or failled */
  2671. if (dib8000_get_status(state->fe[1]) <= FE_STATUS_STD_SUCCESS) /* Something is locked on the input fe */
  2672. *tune_state = CT_DEMOD_STEP_8; /* go for mpeg */
  2673. else if (dib8000_get_status(state->fe[1]) >= FE_STATUS_TUNE_TIME_TOO_SHORT) { /* fe in input failled also, break the current one */
  2674. *tune_state = CT_DEMOD_STOP; /* else we are done here ; step 8 will close the loops and exit */
  2675. dib8000_viterbi_state(state, 1); /* start viterbi chandec */
  2676. dib8000_set_isdbt_loop_params(state, LOOP_TUNE_2);
  2677. state->status = FE_STATUS_TUNE_FAILED;
  2678. }
  2679. } else {
  2680. dib8000_viterbi_state(state, 1); /* start viterbi chandec */
  2681. dib8000_set_isdbt_loop_params(state, LOOP_TUNE_2);
  2682. *tune_state = CT_DEMOD_STOP; /* else we are done here ; step 8 will close the loops and exit */
  2683. state->status = FE_STATUS_TUNE_FAILED;
  2684. }
  2685. break;
  2686. case CT_DEMOD_STEP_7: /* 37 */
  2687. locks = dib8000_read_lock(fe);
  2688. if (locks & (1<<10)) { /* lmod4_lock */
  2689. ret = 14; /* wait for 14 symbols */
  2690. *tune_state = CT_DEMOD_STEP_8;
  2691. } else if (time_after(now, *timeout))
  2692. *tune_state = CT_DEMOD_STEP_6; /* goto check for diversity input connection */
  2693. break;
  2694. case CT_DEMOD_STEP_8: /* 38 */
  2695. dib8000_viterbi_state(state, 1); /* start viterbi chandec */
  2696. dib8000_set_isdbt_loop_params(state, LOOP_TUNE_2);
  2697. /* mpeg will never lock on this condition because init_prbs is not set : search for it !*/
  2698. if (c->isdbt_sb_mode
  2699. && c->isdbt_sb_subchannel < 14
  2700. && !state->differential_constellation) {
  2701. state->subchannel = 0;
  2702. *tune_state = CT_DEMOD_STEP_11;
  2703. } else {
  2704. *tune_state = CT_DEMOD_STEP_9;
  2705. state->status = FE_STATUS_LOCKED;
  2706. }
  2707. break;
  2708. case CT_DEMOD_STEP_9: /* 39 */
  2709. if ((state->revision == 0x8090) || ((dib8000_read_word(state, 1291) >> 9) & 0x1)) { /* fe capable of deinterleaving : esram */
  2710. /* defines timeout for mpeg lock depending on interleaver length of longest layer */
  2711. for (i = 0; i < 3; i++) {
  2712. if (c->layer[i].interleaving >= deeper_interleaver) {
  2713. dprintk("layer%i: time interleaver = %d ", i, c->layer[i].interleaving);
  2714. if (c->layer[i].segment_count > 0) { /* valid layer */
  2715. deeper_interleaver = c->layer[0].interleaving;
  2716. state->longest_intlv_layer = i;
  2717. }
  2718. }
  2719. }
  2720. if (deeper_interleaver == 0)
  2721. locks = 2; /* locks is the tmp local variable name */
  2722. else if (deeper_interleaver == 3)
  2723. locks = 8;
  2724. else
  2725. locks = 2 * deeper_interleaver;
  2726. if (state->diversity_onoff != 0) /* because of diversity sync */
  2727. locks *= 2;
  2728. *timeout = now + msecs_to_jiffies(200 * locks); /* give the mpeg lock 800ms if sram is present */
  2729. dprintk("Deeper interleaver mode = %d on layer %d : timeout mult factor = %d => will use timeout = %ld",
  2730. deeper_interleaver, state->longest_intlv_layer, locks, *timeout);
  2731. *tune_state = CT_DEMOD_STEP_10;
  2732. } else
  2733. *tune_state = CT_DEMOD_STOP;
  2734. break;
  2735. case CT_DEMOD_STEP_10: /* 40 */
  2736. locks = dib8000_read_lock(fe);
  2737. if (locks&(1<<(7-state->longest_intlv_layer))) { /* mpeg lock : check the longest one */
  2738. dprintk("ISDB-T layer locks: Layer A %s, Layer B %s, Layer C %s",
  2739. c->layer[0].segment_count ? (locks >> 7) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled",
  2740. c->layer[1].segment_count ? (locks >> 6) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled",
  2741. c->layer[2].segment_count ? (locks >> 5) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled");
  2742. if (c->isdbt_sb_mode
  2743. && c->isdbt_sb_subchannel < 14
  2744. && !state->differential_constellation)
  2745. /* signal to the upper layer, that there was a channel found and the parameters can be read */
  2746. state->status = FE_STATUS_DEMOD_SUCCESS;
  2747. else
  2748. state->status = FE_STATUS_DATA_LOCKED;
  2749. *tune_state = CT_DEMOD_STOP;
  2750. } else if (time_after(now, *timeout)) {
  2751. if (c->isdbt_sb_mode
  2752. && c->isdbt_sb_subchannel < 14
  2753. && !state->differential_constellation) { /* continue to try init prbs autosearch */
  2754. state->subchannel += 3;
  2755. *tune_state = CT_DEMOD_STEP_11;
  2756. } else { /* we are done mpeg of the longest interleaver xas not locking but let's try if an other layer has locked in the same time */
  2757. if (locks & (0x7 << 5)) {
  2758. dprintk("Not all ISDB-T layers locked in %d ms: Layer A %s, Layer B %s, Layer C %s",
  2759. jiffies_to_msecs(now - *timeout),
  2760. c->layer[0].segment_count ? (locks >> 7) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled",
  2761. c->layer[1].segment_count ? (locks >> 6) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled",
  2762. c->layer[2].segment_count ? (locks >> 5) & 0x1 ? "locked" : "NOT LOCKED" : "not enabled");
  2763. state->status = FE_STATUS_DATA_LOCKED;
  2764. } else
  2765. state->status = FE_STATUS_TUNE_FAILED;
  2766. *tune_state = CT_DEMOD_STOP;
  2767. }
  2768. }
  2769. break;
  2770. case CT_DEMOD_STEP_11: /* 41 : init prbs autosearch */
  2771. if (state->subchannel <= 41) {
  2772. dib8000_set_subchannel_prbs(state, dib8000_get_init_prbs(state, state->subchannel));
  2773. *tune_state = CT_DEMOD_STEP_9;
  2774. } else {
  2775. *tune_state = CT_DEMOD_STOP;
  2776. state->status = FE_STATUS_TUNE_FAILED;
  2777. }
  2778. break;
  2779. default:
  2780. break;
  2781. }
  2782. /* tuning is finished - cleanup the demod */
  2783. switch (*tune_state) {
  2784. case CT_DEMOD_STOP: /* (42) */
  2785. #ifdef DIB8000_AGC_FREEZE
  2786. if ((state->revision != 0x8090) && (state->agc1_max != 0)) {
  2787. dib8000_write_word(state, 108, state->agc1_max);
  2788. dib8000_write_word(state, 109, state->agc1_min);
  2789. dib8000_write_word(state, 110, state->agc2_max);
  2790. dib8000_write_word(state, 111, state->agc2_min);
  2791. state->agc1_max = 0;
  2792. state->agc1_min = 0;
  2793. state->agc2_max = 0;
  2794. state->agc2_min = 0;
  2795. }
  2796. #endif
  2797. ret = 0;
  2798. break;
  2799. default:
  2800. break;
  2801. }
  2802. if ((ret > 0) && (*tune_state > CT_DEMOD_STEP_3))
  2803. return ret * state->symbol_duration;
  2804. if ((ret > 0) && (ret < state->symbol_duration))
  2805. return state->symbol_duration; /* at least one symbol */
  2806. return ret;
  2807. }
  2808. static int dib8000_wakeup(struct dvb_frontend *fe)
  2809. {
  2810. struct dib8000_state *state = fe->demodulator_priv;
  2811. u8 index_frontend;
  2812. int ret;
  2813. dib8000_set_power_mode(state, DIB8000_POWER_ALL);
  2814. dib8000_set_adc_state(state, DIBX000_ADC_ON);
  2815. if (dib8000_set_adc_state(state, DIBX000_SLOW_ADC_ON) != 0)
  2816. dprintk("could not start Slow ADC");
  2817. if (state->revision == 0x8090)
  2818. dib8000_sad_calib(state);
  2819. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2820. ret = state->fe[index_frontend]->ops.init(state->fe[index_frontend]);
  2821. if (ret < 0)
  2822. return ret;
  2823. }
  2824. return 0;
  2825. }
  2826. static int dib8000_sleep(struct dvb_frontend *fe)
  2827. {
  2828. struct dib8000_state *state = fe->demodulator_priv;
  2829. u8 index_frontend;
  2830. int ret;
  2831. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2832. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  2833. if (ret < 0)
  2834. return ret;
  2835. }
  2836. if (state->revision != 0x8090)
  2837. dib8000_set_output_mode(fe, OUTMODE_HIGH_Z);
  2838. dib8000_set_power_mode(state, DIB8000_POWER_INTERFACE_ONLY);
  2839. return dib8000_set_adc_state(state, DIBX000_SLOW_ADC_OFF) | dib8000_set_adc_state(state, DIBX000_ADC_OFF);
  2840. }
  2841. static int dib8000_read_status(struct dvb_frontend *fe, enum fe_status *stat);
  2842. static int dib8000_get_frontend(struct dvb_frontend *fe)
  2843. {
  2844. struct dib8000_state *state = fe->demodulator_priv;
  2845. u16 i, val = 0;
  2846. enum fe_status stat = 0;
  2847. u8 index_frontend, sub_index_frontend;
  2848. fe->dtv_property_cache.bandwidth_hz = 6000000;
  2849. /*
  2850. * If called to early, get_frontend makes dib8000_tune to either
  2851. * not lock or not sync. This causes dvbv5-scan/dvbv5-zap to fail.
  2852. * So, let's just return if frontend 0 has not locked.
  2853. */
  2854. dib8000_read_status(fe, &stat);
  2855. if (!(stat & FE_HAS_SYNC))
  2856. return 0;
  2857. dprintk("dib8000_get_frontend: TMCC lock");
  2858. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2859. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  2860. if (stat&FE_HAS_SYNC) {
  2861. dprintk("TMCC lock on the slave%i", index_frontend);
  2862. /* synchronize the cache with the other frontends */
  2863. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend]);
  2864. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL); sub_index_frontend++) {
  2865. if (sub_index_frontend != index_frontend) {
  2866. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_sb_mode = state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode;
  2867. state->fe[sub_index_frontend]->dtv_property_cache.inversion = state->fe[index_frontend]->dtv_property_cache.inversion;
  2868. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode = state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  2869. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval = state->fe[index_frontend]->dtv_property_cache.guard_interval;
  2870. state->fe[sub_index_frontend]->dtv_property_cache.isdbt_partial_reception = state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception;
  2871. for (i = 0; i < 3; i++) {
  2872. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].segment_count = state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count;
  2873. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].interleaving = state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving;
  2874. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].fec = state->fe[index_frontend]->dtv_property_cache.layer[i].fec;
  2875. state->fe[sub_index_frontend]->dtv_property_cache.layer[i].modulation = state->fe[index_frontend]->dtv_property_cache.layer[i].modulation;
  2876. }
  2877. }
  2878. }
  2879. return 0;
  2880. }
  2881. }
  2882. fe->dtv_property_cache.isdbt_sb_mode = dib8000_read_word(state, 508) & 0x1;
  2883. if (state->revision == 0x8090)
  2884. val = dib8000_read_word(state, 572);
  2885. else
  2886. val = dib8000_read_word(state, 570);
  2887. fe->dtv_property_cache.inversion = (val & 0x40) >> 6;
  2888. switch ((val & 0x30) >> 4) {
  2889. case 1:
  2890. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  2891. dprintk("dib8000_get_frontend: transmission mode 2K");
  2892. break;
  2893. case 2:
  2894. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
  2895. dprintk("dib8000_get_frontend: transmission mode 4K");
  2896. break;
  2897. case 3:
  2898. default:
  2899. fe->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  2900. dprintk("dib8000_get_frontend: transmission mode 8K");
  2901. break;
  2902. }
  2903. switch (val & 0x3) {
  2904. case 0:
  2905. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  2906. dprintk("dib8000_get_frontend: Guard Interval = 1/32 ");
  2907. break;
  2908. case 1:
  2909. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  2910. dprintk("dib8000_get_frontend: Guard Interval = 1/16 ");
  2911. break;
  2912. case 2:
  2913. dprintk("dib8000_get_frontend: Guard Interval = 1/8 ");
  2914. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  2915. break;
  2916. case 3:
  2917. dprintk("dib8000_get_frontend: Guard Interval = 1/4 ");
  2918. fe->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  2919. break;
  2920. }
  2921. val = dib8000_read_word(state, 505);
  2922. fe->dtv_property_cache.isdbt_partial_reception = val & 1;
  2923. dprintk("dib8000_get_frontend: partial_reception = %d ", fe->dtv_property_cache.isdbt_partial_reception);
  2924. for (i = 0; i < 3; i++) {
  2925. int show;
  2926. val = dib8000_read_word(state, 493 + i) & 0x0f;
  2927. fe->dtv_property_cache.layer[i].segment_count = val;
  2928. if (val == 0 || val > 13)
  2929. show = 0;
  2930. else
  2931. show = 1;
  2932. if (show)
  2933. dprintk("dib8000_get_frontend: Layer %d segments = %d ",
  2934. i, fe->dtv_property_cache.layer[i].segment_count);
  2935. val = dib8000_read_word(state, 499 + i) & 0x3;
  2936. /* Interleaving can be 0, 1, 2 or 4 */
  2937. if (val == 3)
  2938. val = 4;
  2939. fe->dtv_property_cache.layer[i].interleaving = val;
  2940. if (show)
  2941. dprintk("dib8000_get_frontend: Layer %d time_intlv = %d ",
  2942. i, fe->dtv_property_cache.layer[i].interleaving);
  2943. val = dib8000_read_word(state, 481 + i);
  2944. switch (val & 0x7) {
  2945. case 1:
  2946. fe->dtv_property_cache.layer[i].fec = FEC_1_2;
  2947. if (show)
  2948. dprintk("dib8000_get_frontend: Layer %d Code Rate = 1/2 ", i);
  2949. break;
  2950. case 2:
  2951. fe->dtv_property_cache.layer[i].fec = FEC_2_3;
  2952. if (show)
  2953. dprintk("dib8000_get_frontend: Layer %d Code Rate = 2/3 ", i);
  2954. break;
  2955. case 3:
  2956. fe->dtv_property_cache.layer[i].fec = FEC_3_4;
  2957. if (show)
  2958. dprintk("dib8000_get_frontend: Layer %d Code Rate = 3/4 ", i);
  2959. break;
  2960. case 5:
  2961. fe->dtv_property_cache.layer[i].fec = FEC_5_6;
  2962. if (show)
  2963. dprintk("dib8000_get_frontend: Layer %d Code Rate = 5/6 ", i);
  2964. break;
  2965. default:
  2966. fe->dtv_property_cache.layer[i].fec = FEC_7_8;
  2967. if (show)
  2968. dprintk("dib8000_get_frontend: Layer %d Code Rate = 7/8 ", i);
  2969. break;
  2970. }
  2971. val = dib8000_read_word(state, 487 + i);
  2972. switch (val & 0x3) {
  2973. case 0:
  2974. fe->dtv_property_cache.layer[i].modulation = DQPSK;
  2975. if (show)
  2976. dprintk("dib8000_get_frontend: Layer %d DQPSK ", i);
  2977. break;
  2978. case 1:
  2979. fe->dtv_property_cache.layer[i].modulation = QPSK;
  2980. if (show)
  2981. dprintk("dib8000_get_frontend: Layer %d QPSK ", i);
  2982. break;
  2983. case 2:
  2984. fe->dtv_property_cache.layer[i].modulation = QAM_16;
  2985. if (show)
  2986. dprintk("dib8000_get_frontend: Layer %d QAM16 ", i);
  2987. break;
  2988. case 3:
  2989. default:
  2990. fe->dtv_property_cache.layer[i].modulation = QAM_64;
  2991. if (show)
  2992. dprintk("dib8000_get_frontend: Layer %d QAM64 ", i);
  2993. break;
  2994. }
  2995. }
  2996. /* synchronize the cache with the other frontends */
  2997. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  2998. state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode = fe->dtv_property_cache.isdbt_sb_mode;
  2999. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  3000. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  3001. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  3002. state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception = fe->dtv_property_cache.isdbt_partial_reception;
  3003. for (i = 0; i < 3; i++) {
  3004. state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count = fe->dtv_property_cache.layer[i].segment_count;
  3005. state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving = fe->dtv_property_cache.layer[i].interleaving;
  3006. state->fe[index_frontend]->dtv_property_cache.layer[i].fec = fe->dtv_property_cache.layer[i].fec;
  3007. state->fe[index_frontend]->dtv_property_cache.layer[i].modulation = fe->dtv_property_cache.layer[i].modulation;
  3008. }
  3009. }
  3010. return 0;
  3011. }
  3012. static int dib8000_set_frontend(struct dvb_frontend *fe)
  3013. {
  3014. struct dib8000_state *state = fe->demodulator_priv;
  3015. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  3016. int l, i, active, time, time_slave = 0;
  3017. u8 exit_condition, index_frontend;
  3018. unsigned long delay, callback_time;
  3019. if (c->frequency == 0) {
  3020. dprintk("dib8000: must at least specify frequency ");
  3021. return 0;
  3022. }
  3023. if (c->bandwidth_hz == 0) {
  3024. dprintk("dib8000: no bandwidth specified, set to default ");
  3025. c->bandwidth_hz = 6000000;
  3026. }
  3027. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3028. /* synchronization of the cache */
  3029. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_ISDBT;
  3030. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  3031. /* set output mode and diversity input */
  3032. if (state->revision != 0x8090) {
  3033. dib8000_set_diversity_in(state->fe[index_frontend], 1);
  3034. if (index_frontend != 0)
  3035. dib8000_set_output_mode(state->fe[index_frontend],
  3036. OUTMODE_DIVERSITY);
  3037. else
  3038. dib8000_set_output_mode(state->fe[0], OUTMODE_HIGH_Z);
  3039. } else {
  3040. dib8096p_set_diversity_in(state->fe[index_frontend], 1);
  3041. if (index_frontend != 0)
  3042. dib8096p_set_output_mode(state->fe[index_frontend],
  3043. OUTMODE_DIVERSITY);
  3044. else
  3045. dib8096p_set_output_mode(state->fe[0], OUTMODE_HIGH_Z);
  3046. }
  3047. /* tune the tuner */
  3048. if (state->fe[index_frontend]->ops.tuner_ops.set_params)
  3049. state->fe[index_frontend]->ops.tuner_ops.set_params(state->fe[index_frontend]);
  3050. dib8000_set_tune_state(state->fe[index_frontend], CT_AGC_START);
  3051. }
  3052. /* turn off the diversity of the last chip */
  3053. if (state->revision != 0x8090)
  3054. dib8000_set_diversity_in(state->fe[index_frontend - 1], 0);
  3055. else
  3056. dib8096p_set_diversity_in(state->fe[index_frontend - 1], 0);
  3057. /* start up the AGC */
  3058. do {
  3059. time = dib8000_agc_startup(state->fe[0]);
  3060. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3061. time_slave = dib8000_agc_startup(state->fe[index_frontend]);
  3062. if (time == 0)
  3063. time = time_slave;
  3064. else if ((time_slave != 0) && (time_slave > time))
  3065. time = time_slave;
  3066. }
  3067. if (time == 0)
  3068. break;
  3069. /*
  3070. * Despite dib8000_agc_startup returns time at a 0.1 ms range,
  3071. * the actual sleep time depends on CONFIG_HZ. The worse case
  3072. * is when CONFIG_HZ=100. In such case, the minimum granularity
  3073. * is 10ms. On some real field tests, the tuner sometimes don't
  3074. * lock when this timer is lower than 10ms. So, enforce a 10ms
  3075. * granularity.
  3076. */
  3077. time = 10 * (time + 99)/100;
  3078. usleep_range(time * 1000, (time + 1) * 1000);
  3079. exit_condition = 1;
  3080. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3081. if (dib8000_get_tune_state(state->fe[index_frontend]) != CT_AGC_STOP) {
  3082. exit_condition = 0;
  3083. break;
  3084. }
  3085. }
  3086. } while (exit_condition == 0);
  3087. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  3088. dib8000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  3089. active = 1;
  3090. do {
  3091. callback_time = 0;
  3092. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3093. delay = dib8000_tune(state->fe[index_frontend]);
  3094. if (delay != 0) {
  3095. delay = jiffies + usecs_to_jiffies(100 * delay);
  3096. if (!callback_time || delay < callback_time)
  3097. callback_time = delay;
  3098. }
  3099. /* we are in autosearch */
  3100. if (state->channel_parameters_set == 0) { /* searching */
  3101. if ((dib8000_get_status(state->fe[index_frontend]) == FE_STATUS_DEMOD_SUCCESS) || (dib8000_get_status(state->fe[index_frontend]) == FE_STATUS_FFT_SUCCESS)) {
  3102. dprintk("autosearch succeeded on fe%i", index_frontend);
  3103. dib8000_get_frontend(state->fe[index_frontend]); /* we read the channel parameters from the frontend which was successful */
  3104. state->channel_parameters_set = 1;
  3105. for (l = 0; (l < MAX_NUMBER_OF_FRONTENDS) && (state->fe[l] != NULL); l++) {
  3106. if (l != index_frontend) { /* and for all frontend except the successful one */
  3107. dprintk("Restarting frontend %d\n", l);
  3108. dib8000_tune_restart_from_demod(state->fe[l]);
  3109. state->fe[l]->dtv_property_cache.isdbt_sb_mode = state->fe[index_frontend]->dtv_property_cache.isdbt_sb_mode;
  3110. state->fe[l]->dtv_property_cache.inversion = state->fe[index_frontend]->dtv_property_cache.inversion;
  3111. state->fe[l]->dtv_property_cache.transmission_mode = state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  3112. state->fe[l]->dtv_property_cache.guard_interval = state->fe[index_frontend]->dtv_property_cache.guard_interval;
  3113. state->fe[l]->dtv_property_cache.isdbt_partial_reception = state->fe[index_frontend]->dtv_property_cache.isdbt_partial_reception;
  3114. for (i = 0; i < 3; i++) {
  3115. state->fe[l]->dtv_property_cache.layer[i].segment_count = state->fe[index_frontend]->dtv_property_cache.layer[i].segment_count;
  3116. state->fe[l]->dtv_property_cache.layer[i].interleaving = state->fe[index_frontend]->dtv_property_cache.layer[i].interleaving;
  3117. state->fe[l]->dtv_property_cache.layer[i].fec = state->fe[index_frontend]->dtv_property_cache.layer[i].fec;
  3118. state->fe[l]->dtv_property_cache.layer[i].modulation = state->fe[index_frontend]->dtv_property_cache.layer[i].modulation;
  3119. }
  3120. }
  3121. }
  3122. }
  3123. }
  3124. }
  3125. /* tuning is done when the master frontend is done (failed or success) */
  3126. if (dib8000_get_status(state->fe[0]) == FE_STATUS_TUNE_FAILED ||
  3127. dib8000_get_status(state->fe[0]) == FE_STATUS_LOCKED ||
  3128. dib8000_get_status(state->fe[0]) == FE_STATUS_DATA_LOCKED) {
  3129. active = 0;
  3130. /* we need to wait for all frontends to be finished */
  3131. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3132. if (dib8000_get_tune_state(state->fe[index_frontend]) != CT_DEMOD_STOP)
  3133. active = 1;
  3134. }
  3135. if (active == 0)
  3136. dprintk("tuning done with status %d", dib8000_get_status(state->fe[0]));
  3137. }
  3138. if ((active == 1) && (callback_time == 0)) {
  3139. dprintk("strange callback time something went wrong");
  3140. active = 0;
  3141. }
  3142. while ((active == 1) && (time_before(jiffies, callback_time)))
  3143. msleep(100);
  3144. } while (active);
  3145. /* set output mode */
  3146. if (state->revision != 0x8090)
  3147. dib8000_set_output_mode(state->fe[0], state->cfg.output_mode);
  3148. else {
  3149. dib8096p_set_output_mode(state->fe[0], state->cfg.output_mode);
  3150. if (state->cfg.enMpegOutput == 0) {
  3151. dib8096p_setDibTxMux(state, MPEG_ON_DIBTX);
  3152. dib8096p_setHostBusMux(state, DIBTX_ON_HOSTBUS);
  3153. }
  3154. }
  3155. return 0;
  3156. }
  3157. static int dib8000_get_stats(struct dvb_frontend *fe, enum fe_status stat);
  3158. static int dib8000_read_status(struct dvb_frontend *fe, enum fe_status *stat)
  3159. {
  3160. struct dib8000_state *state = fe->demodulator_priv;
  3161. u16 lock_slave = 0, lock;
  3162. u8 index_frontend;
  3163. lock = dib8000_read_lock(fe);
  3164. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  3165. lock_slave |= dib8000_read_lock(state->fe[index_frontend]);
  3166. *stat = 0;
  3167. if (((lock >> 13) & 1) || ((lock_slave >> 13) & 1))
  3168. *stat |= FE_HAS_SIGNAL;
  3169. if (((lock >> 8) & 1) || ((lock_slave >> 8) & 1)) /* Equal */
  3170. *stat |= FE_HAS_CARRIER;
  3171. if ((((lock >> 1) & 0xf) == 0xf) || (((lock_slave >> 1) & 0xf) == 0xf)) /* TMCC_SYNC */
  3172. *stat |= FE_HAS_SYNC;
  3173. if ((((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) && ((lock >> 5) & 7)) /* FEC MPEG */
  3174. *stat |= FE_HAS_LOCK;
  3175. if (((lock >> 12) & 1) || ((lock_slave >> 12) & 1)) {
  3176. lock = dib8000_read_word(state, 554); /* Viterbi Layer A */
  3177. if (lock & 0x01)
  3178. *stat |= FE_HAS_VITERBI;
  3179. lock = dib8000_read_word(state, 555); /* Viterbi Layer B */
  3180. if (lock & 0x01)
  3181. *stat |= FE_HAS_VITERBI;
  3182. lock = dib8000_read_word(state, 556); /* Viterbi Layer C */
  3183. if (lock & 0x01)
  3184. *stat |= FE_HAS_VITERBI;
  3185. }
  3186. dib8000_get_stats(fe, *stat);
  3187. return 0;
  3188. }
  3189. static int dib8000_read_ber(struct dvb_frontend *fe, u32 * ber)
  3190. {
  3191. struct dib8000_state *state = fe->demodulator_priv;
  3192. /* 13 segments */
  3193. if (state->revision == 0x8090)
  3194. *ber = (dib8000_read_word(state, 562) << 16) |
  3195. dib8000_read_word(state, 563);
  3196. else
  3197. *ber = (dib8000_read_word(state, 560) << 16) |
  3198. dib8000_read_word(state, 561);
  3199. return 0;
  3200. }
  3201. static int dib8000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  3202. {
  3203. struct dib8000_state *state = fe->demodulator_priv;
  3204. /* packet error on 13 seg */
  3205. if (state->revision == 0x8090)
  3206. *unc = dib8000_read_word(state, 567);
  3207. else
  3208. *unc = dib8000_read_word(state, 565);
  3209. return 0;
  3210. }
  3211. static int dib8000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  3212. {
  3213. struct dib8000_state *state = fe->demodulator_priv;
  3214. u8 index_frontend;
  3215. u16 val;
  3216. *strength = 0;
  3217. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  3218. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  3219. if (val > 65535 - *strength)
  3220. *strength = 65535;
  3221. else
  3222. *strength += val;
  3223. }
  3224. val = 65535 - dib8000_read_word(state, 390);
  3225. if (val > 65535 - *strength)
  3226. *strength = 65535;
  3227. else
  3228. *strength += val;
  3229. return 0;
  3230. }
  3231. static u32 dib8000_get_snr(struct dvb_frontend *fe)
  3232. {
  3233. struct dib8000_state *state = fe->demodulator_priv;
  3234. u32 n, s, exp;
  3235. u16 val;
  3236. if (state->revision != 0x8090)
  3237. val = dib8000_read_word(state, 542);
  3238. else
  3239. val = dib8000_read_word(state, 544);
  3240. n = (val >> 6) & 0xff;
  3241. exp = (val & 0x3f);
  3242. if ((exp & 0x20) != 0)
  3243. exp -= 0x40;
  3244. n <<= exp+16;
  3245. if (state->revision != 0x8090)
  3246. val = dib8000_read_word(state, 543);
  3247. else
  3248. val = dib8000_read_word(state, 545);
  3249. s = (val >> 6) & 0xff;
  3250. exp = (val & 0x3f);
  3251. if ((exp & 0x20) != 0)
  3252. exp -= 0x40;
  3253. s <<= exp+16;
  3254. if (n > 0) {
  3255. u32 t = (s/n) << 16;
  3256. return t + ((s << 16) - n*t) / n;
  3257. }
  3258. return 0xffffffff;
  3259. }
  3260. static int dib8000_read_snr(struct dvb_frontend *fe, u16 * snr)
  3261. {
  3262. struct dib8000_state *state = fe->demodulator_priv;
  3263. u8 index_frontend;
  3264. u32 snr_master;
  3265. snr_master = dib8000_get_snr(fe);
  3266. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  3267. snr_master += dib8000_get_snr(state->fe[index_frontend]);
  3268. if ((snr_master >> 16) != 0) {
  3269. snr_master = 10*intlog10(snr_master>>16);
  3270. *snr = snr_master / ((1 << 24) / 10);
  3271. }
  3272. else
  3273. *snr = 0;
  3274. return 0;
  3275. }
  3276. struct per_layer_regs {
  3277. u16 lock, ber, per;
  3278. };
  3279. static const struct per_layer_regs per_layer_regs[] = {
  3280. { 554, 560, 562 },
  3281. { 555, 576, 578 },
  3282. { 556, 581, 583 },
  3283. };
  3284. struct linear_segments {
  3285. unsigned x;
  3286. signed y;
  3287. };
  3288. /*
  3289. * Table to estimate signal strength in dBm.
  3290. * This table was empirically determinated by measuring the signal
  3291. * strength generated by a DTA-2111 RF generator directly connected into
  3292. * a dib8076 device (a PixelView PV-D231U stick), using a good quality
  3293. * 3 meters RC6 cable and good RC6 connectors.
  3294. * The real value can actually be different on other devices, depending
  3295. * on several factors, like if LNA is enabled or not, if diversity is
  3296. * enabled, type of connectors, etc.
  3297. * Yet, it is better to use this measure in dB than a random non-linear
  3298. * percentage value, especially for antenna adjustments.
  3299. * On my tests, the precision of the measure using this table is about
  3300. * 0.5 dB, with sounds reasonable enough.
  3301. */
  3302. static struct linear_segments strength_to_db_table[] = {
  3303. { 55953, 108500 }, /* -22.5 dBm */
  3304. { 55394, 108000 },
  3305. { 53834, 107000 },
  3306. { 52863, 106000 },
  3307. { 52239, 105000 },
  3308. { 52012, 104000 },
  3309. { 51803, 103000 },
  3310. { 51566, 102000 },
  3311. { 51356, 101000 },
  3312. { 51112, 100000 },
  3313. { 50869, 99000 },
  3314. { 50600, 98000 },
  3315. { 50363, 97000 },
  3316. { 50117, 96000 }, /* -35 dBm */
  3317. { 49889, 95000 },
  3318. { 49680, 94000 },
  3319. { 49493, 93000 },
  3320. { 49302, 92000 },
  3321. { 48929, 91000 },
  3322. { 48416, 90000 },
  3323. { 48035, 89000 },
  3324. { 47593, 88000 },
  3325. { 47282, 87000 },
  3326. { 46953, 86000 },
  3327. { 46698, 85000 },
  3328. { 45617, 84000 },
  3329. { 44773, 83000 },
  3330. { 43845, 82000 },
  3331. { 43020, 81000 },
  3332. { 42010, 80000 }, /* -51 dBm */
  3333. { 0, 0 },
  3334. };
  3335. static u32 interpolate_value(u32 value, struct linear_segments *segments,
  3336. unsigned len)
  3337. {
  3338. u64 tmp64;
  3339. u32 dx;
  3340. s32 dy;
  3341. int i, ret;
  3342. if (value >= segments[0].x)
  3343. return segments[0].y;
  3344. if (value < segments[len-1].x)
  3345. return segments[len-1].y;
  3346. for (i = 1; i < len - 1; i++) {
  3347. /* If value is identical, no need to interpolate */
  3348. if (value == segments[i].x)
  3349. return segments[i].y;
  3350. if (value > segments[i].x)
  3351. break;
  3352. }
  3353. /* Linear interpolation between the two (x,y) points */
  3354. dy = segments[i - 1].y - segments[i].y;
  3355. dx = segments[i - 1].x - segments[i].x;
  3356. tmp64 = value - segments[i].x;
  3357. tmp64 *= dy;
  3358. do_div(tmp64, dx);
  3359. ret = segments[i].y + tmp64;
  3360. return ret;
  3361. }
  3362. static u32 dib8000_get_time_us(struct dvb_frontend *fe, int layer)
  3363. {
  3364. struct dib8000_state *state = fe->demodulator_priv;
  3365. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  3366. int ini_layer, end_layer, i;
  3367. u64 time_us, tmp64;
  3368. u32 tmp, denom;
  3369. int guard, rate_num, rate_denum = 1, bits_per_symbol, nsegs;
  3370. int interleaving = 0, fft_div;
  3371. if (layer >= 0) {
  3372. ini_layer = layer;
  3373. end_layer = layer + 1;
  3374. } else {
  3375. ini_layer = 0;
  3376. end_layer = 3;
  3377. }
  3378. switch (c->guard_interval) {
  3379. case GUARD_INTERVAL_1_4:
  3380. guard = 4;
  3381. break;
  3382. case GUARD_INTERVAL_1_8:
  3383. guard = 8;
  3384. break;
  3385. case GUARD_INTERVAL_1_16:
  3386. guard = 16;
  3387. break;
  3388. default:
  3389. case GUARD_INTERVAL_1_32:
  3390. guard = 32;
  3391. break;
  3392. }
  3393. switch (c->transmission_mode) {
  3394. case TRANSMISSION_MODE_2K:
  3395. fft_div = 4;
  3396. break;
  3397. case TRANSMISSION_MODE_4K:
  3398. fft_div = 2;
  3399. break;
  3400. default:
  3401. case TRANSMISSION_MODE_8K:
  3402. fft_div = 1;
  3403. break;
  3404. }
  3405. denom = 0;
  3406. for (i = ini_layer; i < end_layer; i++) {
  3407. nsegs = c->layer[i].segment_count;
  3408. if (nsegs == 0 || nsegs > 13)
  3409. continue;
  3410. switch (c->layer[i].modulation) {
  3411. case DQPSK:
  3412. case QPSK:
  3413. bits_per_symbol = 2;
  3414. break;
  3415. case QAM_16:
  3416. bits_per_symbol = 4;
  3417. break;
  3418. default:
  3419. case QAM_64:
  3420. bits_per_symbol = 6;
  3421. break;
  3422. }
  3423. switch (c->layer[i].fec) {
  3424. case FEC_1_2:
  3425. rate_num = 1;
  3426. rate_denum = 2;
  3427. break;
  3428. case FEC_2_3:
  3429. rate_num = 2;
  3430. rate_denum = 3;
  3431. break;
  3432. case FEC_3_4:
  3433. rate_num = 3;
  3434. rate_denum = 4;
  3435. break;
  3436. case FEC_5_6:
  3437. rate_num = 5;
  3438. rate_denum = 6;
  3439. break;
  3440. default:
  3441. case FEC_7_8:
  3442. rate_num = 7;
  3443. rate_denum = 8;
  3444. break;
  3445. }
  3446. interleaving = c->layer[i].interleaving;
  3447. denom += bits_per_symbol * rate_num * fft_div * nsegs * 384;
  3448. }
  3449. /* If all goes wrong, wait for 1s for the next stats */
  3450. if (!denom)
  3451. return 0;
  3452. /* Estimate the period for the total bit rate */
  3453. time_us = rate_denum * (1008 * 1562500L);
  3454. tmp64 = time_us;
  3455. do_div(tmp64, guard);
  3456. time_us = time_us + tmp64;
  3457. time_us += denom / 2;
  3458. do_div(time_us, denom);
  3459. tmp = 1008 * 96 * interleaving;
  3460. time_us += tmp + tmp / guard;
  3461. return time_us;
  3462. }
  3463. static int dib8000_get_stats(struct dvb_frontend *fe, enum fe_status stat)
  3464. {
  3465. struct dib8000_state *state = fe->demodulator_priv;
  3466. struct dtv_frontend_properties *c = &state->fe[0]->dtv_property_cache;
  3467. int i;
  3468. int show_per_stats = 0;
  3469. u32 time_us = 0, snr, val;
  3470. u64 blocks;
  3471. s32 db;
  3472. u16 strength;
  3473. /* Get Signal strength */
  3474. dib8000_read_signal_strength(fe, &strength);
  3475. val = strength;
  3476. db = interpolate_value(val,
  3477. strength_to_db_table,
  3478. ARRAY_SIZE(strength_to_db_table)) - 131000;
  3479. c->strength.stat[0].svalue = db;
  3480. /* UCB/BER/CNR measures require lock */
  3481. if (!(stat & FE_HAS_LOCK)) {
  3482. c->cnr.len = 1;
  3483. c->block_count.len = 1;
  3484. c->block_error.len = 1;
  3485. c->post_bit_error.len = 1;
  3486. c->post_bit_count.len = 1;
  3487. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3488. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3489. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3490. c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3491. c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  3492. return 0;
  3493. }
  3494. /* Check if time for stats was elapsed */
  3495. if (time_after(jiffies, state->per_jiffies_stats)) {
  3496. state->per_jiffies_stats = jiffies + msecs_to_jiffies(1000);
  3497. /* Get SNR */
  3498. snr = dib8000_get_snr(fe);
  3499. for (i = 1; i < MAX_NUMBER_OF_FRONTENDS; i++) {
  3500. if (state->fe[i])
  3501. snr += dib8000_get_snr(state->fe[i]);
  3502. }
  3503. snr = snr >> 16;
  3504. if (snr) {
  3505. snr = 10 * intlog10(snr);
  3506. snr = (1000L * snr) >> 24;
  3507. } else {
  3508. snr = 0;
  3509. }
  3510. c->cnr.stat[0].svalue = snr;
  3511. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  3512. /* Get UCB measures */
  3513. dib8000_read_unc_blocks(fe, &val);
  3514. if (val < state->init_ucb)
  3515. state->init_ucb += 0x100000000LL;
  3516. c->block_error.stat[0].scale = FE_SCALE_COUNTER;
  3517. c->block_error.stat[0].uvalue = val + state->init_ucb;
  3518. /* Estimate the number of packets based on bitrate */
  3519. if (!time_us)
  3520. time_us = dib8000_get_time_us(fe, -1);
  3521. if (time_us) {
  3522. blocks = 1250000ULL * 1000000ULL;
  3523. do_div(blocks, time_us * 8 * 204);
  3524. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  3525. c->block_count.stat[0].uvalue += blocks;
  3526. }
  3527. show_per_stats = 1;
  3528. }
  3529. /* Get post-BER measures */
  3530. if (time_after(jiffies, state->ber_jiffies_stats)) {
  3531. time_us = dib8000_get_time_us(fe, -1);
  3532. state->ber_jiffies_stats = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
  3533. dprintk("Next all layers stats available in %u us.", time_us);
  3534. dib8000_read_ber(fe, &val);
  3535. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  3536. c->post_bit_error.stat[0].uvalue += val;
  3537. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  3538. c->post_bit_count.stat[0].uvalue += 100000000;
  3539. }
  3540. if (state->revision < 0x8002)
  3541. return 0;
  3542. c->block_error.len = 4;
  3543. c->post_bit_error.len = 4;
  3544. c->post_bit_count.len = 4;
  3545. for (i = 0; i < 3; i++) {
  3546. unsigned nsegs = c->layer[i].segment_count;
  3547. if (nsegs == 0 || nsegs > 13)
  3548. continue;
  3549. time_us = 0;
  3550. if (time_after(jiffies, state->ber_jiffies_stats_layer[i])) {
  3551. time_us = dib8000_get_time_us(fe, i);
  3552. state->ber_jiffies_stats_layer[i] = jiffies + msecs_to_jiffies((time_us + 500) / 1000);
  3553. dprintk("Next layer %c stats will be available in %u us\n",
  3554. 'A' + i, time_us);
  3555. val = dib8000_read_word(state, per_layer_regs[i].ber);
  3556. c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  3557. c->post_bit_error.stat[1 + i].uvalue += val;
  3558. c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
  3559. c->post_bit_count.stat[1 + i].uvalue += 100000000;
  3560. }
  3561. if (show_per_stats) {
  3562. val = dib8000_read_word(state, per_layer_regs[i].per);
  3563. c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
  3564. c->block_error.stat[1 + i].uvalue += val;
  3565. if (!time_us)
  3566. time_us = dib8000_get_time_us(fe, i);
  3567. if (time_us) {
  3568. blocks = 1250000ULL * 1000000ULL;
  3569. do_div(blocks, time_us * 8 * 204);
  3570. c->block_count.stat[0].scale = FE_SCALE_COUNTER;
  3571. c->block_count.stat[0].uvalue += blocks;
  3572. }
  3573. }
  3574. }
  3575. return 0;
  3576. }
  3577. static int dib8000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  3578. {
  3579. struct dib8000_state *state = fe->demodulator_priv;
  3580. u8 index_frontend = 1;
  3581. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  3582. index_frontend++;
  3583. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  3584. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  3585. state->fe[index_frontend] = fe_slave;
  3586. return 0;
  3587. }
  3588. dprintk("too many slave frontend");
  3589. return -ENOMEM;
  3590. }
  3591. static int dib8000_remove_slave_frontend(struct dvb_frontend *fe)
  3592. {
  3593. struct dib8000_state *state = fe->demodulator_priv;
  3594. u8 index_frontend = 1;
  3595. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  3596. index_frontend++;
  3597. if (index_frontend != 1) {
  3598. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend-1], index_frontend-1);
  3599. state->fe[index_frontend] = NULL;
  3600. return 0;
  3601. }
  3602. dprintk("no frontend to be removed");
  3603. return -ENODEV;
  3604. }
  3605. static struct dvb_frontend *dib8000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  3606. {
  3607. struct dib8000_state *state = fe->demodulator_priv;
  3608. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  3609. return NULL;
  3610. return state->fe[slave_index];
  3611. }
  3612. static int dib8000_i2c_enumeration(struct i2c_adapter *host, int no_of_demods,
  3613. u8 default_addr, u8 first_addr, u8 is_dib8096p)
  3614. {
  3615. int k = 0, ret = 0;
  3616. u8 new_addr = 0;
  3617. struct i2c_device client = {.adap = host };
  3618. client.i2c_write_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  3619. if (!client.i2c_write_buffer) {
  3620. dprintk("%s: not enough memory", __func__);
  3621. return -ENOMEM;
  3622. }
  3623. client.i2c_read_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  3624. if (!client.i2c_read_buffer) {
  3625. dprintk("%s: not enough memory", __func__);
  3626. ret = -ENOMEM;
  3627. goto error_memory_read;
  3628. }
  3629. client.i2c_buffer_lock = kzalloc(sizeof(struct mutex), GFP_KERNEL);
  3630. if (!client.i2c_buffer_lock) {
  3631. dprintk("%s: not enough memory", __func__);
  3632. ret = -ENOMEM;
  3633. goto error_memory_lock;
  3634. }
  3635. mutex_init(client.i2c_buffer_lock);
  3636. for (k = no_of_demods - 1; k >= 0; k--) {
  3637. /* designated i2c address */
  3638. new_addr = first_addr + (k << 1);
  3639. client.addr = new_addr;
  3640. if (!is_dib8096p)
  3641. dib8000_i2c_write16(&client, 1287, 0x0003); /* sram lead in, rdy */
  3642. if (dib8000_identify(&client) == 0) {
  3643. /* sram lead in, rdy */
  3644. if (!is_dib8096p)
  3645. dib8000_i2c_write16(&client, 1287, 0x0003);
  3646. client.addr = default_addr;
  3647. if (dib8000_identify(&client) == 0) {
  3648. dprintk("#%d: not identified", k);
  3649. ret = -EINVAL;
  3650. goto error;
  3651. }
  3652. }
  3653. /* start diversity to pull_down div_str - just for i2c-enumeration */
  3654. dib8000_i2c_write16(&client, 1286, (1 << 10) | (4 << 6));
  3655. /* set new i2c address and force divstart */
  3656. dib8000_i2c_write16(&client, 1285, (new_addr << 2) | 0x2);
  3657. client.addr = new_addr;
  3658. dib8000_identify(&client);
  3659. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  3660. }
  3661. for (k = 0; k < no_of_demods; k++) {
  3662. new_addr = first_addr | (k << 1);
  3663. client.addr = new_addr;
  3664. // unforce divstr
  3665. dib8000_i2c_write16(&client, 1285, new_addr << 2);
  3666. /* deactivate div - it was just for i2c-enumeration */
  3667. dib8000_i2c_write16(&client, 1286, 0);
  3668. }
  3669. error:
  3670. kfree(client.i2c_buffer_lock);
  3671. error_memory_lock:
  3672. kfree(client.i2c_read_buffer);
  3673. error_memory_read:
  3674. kfree(client.i2c_write_buffer);
  3675. return ret;
  3676. }
  3677. static int dib8000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  3678. {
  3679. tune->min_delay_ms = 1000;
  3680. tune->step_size = 0;
  3681. tune->max_drift = 0;
  3682. return 0;
  3683. }
  3684. static void dib8000_release(struct dvb_frontend *fe)
  3685. {
  3686. struct dib8000_state *st = fe->demodulator_priv;
  3687. u8 index_frontend;
  3688. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  3689. dvb_frontend_detach(st->fe[index_frontend]);
  3690. dibx000_exit_i2c_master(&st->i2c_master);
  3691. i2c_del_adapter(&st->dib8096p_tuner_adap);
  3692. kfree(st->fe[0]);
  3693. kfree(st);
  3694. }
  3695. static struct i2c_adapter *dib8000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  3696. {
  3697. struct dib8000_state *st = fe->demodulator_priv;
  3698. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  3699. }
  3700. static int dib8000_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  3701. {
  3702. struct dib8000_state *st = fe->demodulator_priv;
  3703. u16 val = dib8000_read_word(st, 299) & 0xffef;
  3704. val |= (onoff & 0x1) << 4;
  3705. dprintk("pid filter enabled %d", onoff);
  3706. return dib8000_write_word(st, 299, val);
  3707. }
  3708. static int dib8000_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  3709. {
  3710. struct dib8000_state *st = fe->demodulator_priv;
  3711. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  3712. return dib8000_write_word(st, 305 + id, onoff ? (1 << 13) | pid : 0);
  3713. }
  3714. static const struct dvb_frontend_ops dib8000_ops = {
  3715. .delsys = { SYS_ISDBT },
  3716. .info = {
  3717. .name = "DiBcom 8000 ISDB-T",
  3718. .frequency_min = 44250000,
  3719. .frequency_max = 867250000,
  3720. .frequency_stepsize = 62500,
  3721. .caps = FE_CAN_INVERSION_AUTO |
  3722. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  3723. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  3724. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  3725. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  3726. },
  3727. .release = dib8000_release,
  3728. .init = dib8000_wakeup,
  3729. .sleep = dib8000_sleep,
  3730. .set_frontend = dib8000_set_frontend,
  3731. .get_tune_settings = dib8000_fe_get_tune_settings,
  3732. .get_frontend = dib8000_get_frontend,
  3733. .read_status = dib8000_read_status,
  3734. .read_ber = dib8000_read_ber,
  3735. .read_signal_strength = dib8000_read_signal_strength,
  3736. .read_snr = dib8000_read_snr,
  3737. .read_ucblocks = dib8000_read_unc_blocks,
  3738. };
  3739. static struct dvb_frontend *dib8000_init(struct i2c_adapter *i2c_adap, u8 i2c_addr, struct dib8000_config *cfg)
  3740. {
  3741. struct dvb_frontend *fe;
  3742. struct dib8000_state *state;
  3743. dprintk("dib8000_init");
  3744. state = kzalloc(sizeof(struct dib8000_state), GFP_KERNEL);
  3745. if (state == NULL)
  3746. return NULL;
  3747. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  3748. if (fe == NULL)
  3749. goto error;
  3750. memcpy(&state->cfg, cfg, sizeof(struct dib8000_config));
  3751. state->i2c.adap = i2c_adap;
  3752. state->i2c.addr = i2c_addr;
  3753. state->i2c.i2c_write_buffer = state->i2c_write_buffer;
  3754. state->i2c.i2c_read_buffer = state->i2c_read_buffer;
  3755. mutex_init(&state->i2c_buffer_lock);
  3756. state->i2c.i2c_buffer_lock = &state->i2c_buffer_lock;
  3757. state->gpio_val = cfg->gpio_val;
  3758. state->gpio_dir = cfg->gpio_dir;
  3759. /* Ensure the output mode remains at the previous default if it's
  3760. * not specifically set by the caller.
  3761. */
  3762. if ((state->cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (state->cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  3763. state->cfg.output_mode = OUTMODE_MPEG2_FIFO;
  3764. state->fe[0] = fe;
  3765. fe->demodulator_priv = state;
  3766. memcpy(&state->fe[0]->ops, &dib8000_ops, sizeof(struct dvb_frontend_ops));
  3767. state->timf_default = cfg->pll->timf;
  3768. if (dib8000_identify(&state->i2c) == 0)
  3769. goto error;
  3770. dibx000_init_i2c_master(&state->i2c_master, DIB8000, state->i2c.adap, state->i2c.addr);
  3771. /* init 8096p tuner adapter */
  3772. strncpy(state->dib8096p_tuner_adap.name, "DiB8096P tuner interface",
  3773. sizeof(state->dib8096p_tuner_adap.name));
  3774. state->dib8096p_tuner_adap.algo = &dib8096p_tuner_xfer_algo;
  3775. state->dib8096p_tuner_adap.algo_data = NULL;
  3776. state->dib8096p_tuner_adap.dev.parent = state->i2c.adap->dev.parent;
  3777. i2c_set_adapdata(&state->dib8096p_tuner_adap, state);
  3778. i2c_add_adapter(&state->dib8096p_tuner_adap);
  3779. dib8000_reset(fe);
  3780. dib8000_write_word(state, 285, (dib8000_read_word(state, 285) & ~0x60) | (3 << 5)); /* ber_rs_len = 3 */
  3781. state->current_demod_bw = 6000;
  3782. return fe;
  3783. error:
  3784. kfree(state);
  3785. return NULL;
  3786. }
  3787. void *dib8000_attach(struct dib8000_ops *ops)
  3788. {
  3789. if (!ops)
  3790. return NULL;
  3791. ops->pwm_agc_reset = dib8000_pwm_agc_reset;
  3792. ops->get_dc_power = dib8090p_get_dc_power;
  3793. ops->set_gpio = dib8000_set_gpio;
  3794. ops->get_slave_frontend = dib8000_get_slave_frontend;
  3795. ops->set_tune_state = dib8000_set_tune_state;
  3796. ops->pid_filter_ctrl = dib8000_pid_filter_ctrl;
  3797. ops->remove_slave_frontend = dib8000_remove_slave_frontend;
  3798. ops->get_adc_power = dib8000_get_adc_power;
  3799. ops->update_pll = dib8000_update_pll;
  3800. ops->tuner_sleep = dib8096p_tuner_sleep;
  3801. ops->get_tune_state = dib8000_get_tune_state;
  3802. ops->get_i2c_tuner = dib8096p_get_i2c_tuner;
  3803. ops->set_slave_frontend = dib8000_set_slave_frontend;
  3804. ops->pid_filter = dib8000_pid_filter;
  3805. ops->ctrl_timf = dib8000_ctrl_timf;
  3806. ops->init = dib8000_init;
  3807. ops->get_i2c_master = dib8000_get_i2c_master;
  3808. ops->i2c_enumeration = dib8000_i2c_enumeration;
  3809. ops->set_wbd_ref = dib8000_set_wbd_ref;
  3810. return ops;
  3811. }
  3812. EXPORT_SYMBOL(dib8000_attach);
  3813. MODULE_AUTHOR("Olivier Grenie <Olivier.Grenie@dibcom.fr, " "Patrick Boettcher <pboettcher@dibcom.fr>");
  3814. MODULE_DESCRIPTION("Driver for the DiBcom 8000 ISDB-T demodulator");
  3815. MODULE_LICENSE("GPL");