dib8000.c 133 KB

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