dib8000.c 133 KB

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