dib8000.c 134 KB

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